[erlang-questions] Why do we need modules at all?
Tue May 24 10:06:19 CEST 2011
Why do we need modules at all?
This is a brain-dump-stream-of-consciousness-thing. I've been
thinking about this for a while.
I'm proposing a slightly different way of programming here
The basic idea is
- do away with modules
- all functions have unique distinct names
- all functions have (lots of) meta data
- all functions go into a global (searchable) Key-value database
- we need letrec
- contribution to open source can be as simple as
contributing a single function
- there are no "open source projects" - only "the open source
Key-Value database of all functions"
- Content is peer reviewed
These are discussed in no particular order below:
Why does Erlang have modules?
There's a good an bad side to modules:
Good: Provides a unit of compilation, a unit of code
distribution. unit of code replacement
Bad: It's very difficult to decide which module to put an individual
function in. Break encapsulation (see later)
When I'm programming I often get to the point were I say there should
a function foo/2 in lists.erl but their isn't. There should be but
there isn't - foo/2 is a small self contained thing. Why should it be
in lists.erl because it "feels right".
Strings are lists, so why do we have two modules lists.erl and
string.erl how should I decide in which module my new string/list
processing function should go.
To avoid all mental anguish when I need a small function that
should be somewhere else and isn't I stick it in
a module elib1_misc.erl.
My elib1_misc exports the following:
Now I find this very convenient when I write a new small utility function
I stick in in elib1_misc.erl - no mental anguish in choosing a module
name is involved.
The observation that I find this very-convenient is telling me something
about modules - I like my elib1_misc it feels right.
(aside - It seems many development projects have their own private
Which brings me to the point of my question.
Do we need module's at all? Erlang programs are composed of lots of small
functions, the only place where modules seem useful is to hide a letrec.
The classic example is fibonacci. We want to expose fib/1 but hide the
helper function fib/3. Using modules we say
fib(N, 1, 0).
fib(N, A, B) when N < 2 -> A;
fib(N, A, B) -> fib(N-1, A+B, A).
The downside is we have had to *invent* one module name math - whose *only*
purpose is to hide the definition of fib/3 which we don't want to be made
If we put a second function into the module math, then this second function
could call fib/3 which breaks the encapsulation of fib/3.
We could say:
let fib = fun(N) -> fib(N, 1, 0) end
fib(N, A, B) when N < 2 -> A;
fib(N, A, B) -> fib(N-1, A+B, A).
I hardly dare suggest a syntax for this since I've been following
another thread in this forum where syntax discussion seem to encourage
** Please do suggest alternative syntax's here - but do not comment on
other peoples suggestions ...
I would like to just talk about why we have modules.
Does the idea of a module come from the idea that functions have to be
stored somewhere, so we store them in a file, and we slurp the
file (as a unit) into the system, so the file becomes a module?
If all the files were store by themselves in a database would this
I am thinking more and more that if would be nice to have *all* functions in
a key_value database with unique names.
lookup(foo,2) would get the definition foo foo/2 from a database.
The unique names bit is interesting - is this a good idea. Qualified
names (ie names like xxx:foo/2) or (a.b.c.foo/2) sounds like a good
idea but but when I'm programming I have to invent the xxx or the
a.b.c which is very difficult. It also involves the "decision problem"
if the namespaces xxx and a.b.c already exist I have to *choose* which
to put my new function in.
I think there might be a case for alises here joe:foo/2 could be used
while developing "joe" would expand to a horrible random local string the
real name being ab123aZwerasch123123_foo/2 but I would not be able to
publish my code or make it available to a third_part before I had
chosen a sensible name.
(( managing namespaces seems really tricky, a lot of peoople seem
to thing that the problem goes away by adding "." 's to the name
but managing a namespace with namees like foo.bar.baz.z is just as complex
as managing a namespace with names like foo_bar_baz_z or names like
0x3af312a78a3f1ae123 - the problem is that we have to go from a symbolic
name like www.a.b to a reference like 126.96.36.199 - but how do we discover
the initial name www.a.b? - there are two answers - a) we are given the name
(ie we click on a link) - we do not know the name but we search fo it ))
When programs are small we can live with "just the code" in "a few
modules" the ratio of code to meta data is high.
When programs are large we need a lot of meta-data to understand them.
I would like to see all functions with all meta-data in a data base.
I'd like to say:
lookup(foo,2,Attribute) when Attribute =
code|source|documentation|type signatures|revision history|authors|...
The more I think about it the more I think program development should
viewed as changing the state of a Key-Value database.
So I imagine:
1) all functions have unique names
2) there are no modules
3) we discover the name of a function by searching metadata
describing the function in a database
4) all public functions (think open source) are in the same
We could make a system to do this.
I think this would make open-source projects easier, since the
granularity of contribution goes down. You could contribute
a single function - not an entire application.
(( A problem with GUT style open source projects is there is
not one database of functions, I often what one function from
this project, another function from another project -- the
granularity of reusable parts should be the individual function.
functions are really easy to reuse
modules are more difficult to reuse
entire applications are very difficult to reuse
(Unless there are isolated through a communication channel))
1) Voting for promotion
2) A review process
Given a raw database will *all* functions in it - we could derive an
"approved" functions database.
Popular functions could be moved to the approved database - the
review process would need to be discussed - so kind of peer-review/wiki
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions