01 December 2007
Erlang, the functional Smalltalk?
Let’s start with a confession. I’m not a Erlang weenie, I’m not even a decent part-time Erlang hacker. But for some reason, I keep coming back to it, even through I don’t normally work in the problem domain I think Erlang is best suited for, like servers of various kinds (from MMORPG games to Jabber), or large scale, distributed applications. But in this case, it works differently - I keep coming up with problems that could be nicely solved in Erlang, instead of matching the language to the problem.
I wrote about the great fun factor of programming in Erlang before, but I didn’t quite get the reason this is the case. Now, after thinking about for a while, I think I know what Erlang does so well, that it makes programming a joy.
You see, Erlang creators criticize OOP eagerly, distancing from it as much as possible, which is a bit misleading. I believe Erlang is the quintessentional cross between object orientation and functional programming. Even through Erlang doesn’t have implict support for “objects” of any kind, it does have processes. Contrary to what the Erlang crowd says, they seem to share all the common properties of object/classes in traditional object oriented languages - they capture state, express behaviour and you can even (sort of) inherit a process from another process or compose them, if you think this is necessary to call something object-oriented, and/or if you think this has use in Erlang. You can also build reusable components with them, as you can see for example in the OTP behaviours.
This is a simple observation, but it has some strong consequences - this programming model seems to has some unique advantages. I observed that while using traditional OOP, I tend to think much more about classes, one piece at a time, thinking in terms of fields/methods. Erlang shifts the focus from classes/methods/state towards interactions, or messages crossing, between objects/processes in time. Also, you think about the whole thing at once, about all the components involved and cooperating in solving a subproblem/problem of your program. To put it in another way, you don’t think only about the internal construction of the entities involved, but you also think about how they’re playing together to solve the problem. It feels like the concurrent language was promoting more “concurrent”, cooperative, big-picture thinking. Than, when you get the picture, you can compose the the bigger things (processes) from little, stateless functions doing the things you want.
As a result of this paradigm change, it seems that it’s easier to write more modular and coherent applications. It doesn’t even have anything to do with so-called “concurrent” programs, think of it for a while. Aren’t objects in your traditional language exchanging messages all the time? Aren’t they independent “beings” exisiting concurrently in some “space”? That’s where the title reference to Smalltalk came from. Erlang only removes the limitation that only two object can talk to each other at a time (when your traditional program isn’t threaded, and I’m talking concept-wise, not implementation-wise). At the same time, the functional nature forces you to use as small amounts of state as possible, which in consequence rewards writting very small modules.
Obviously, they are also strong differencies ie. you have many things in Erlang that aren’t at all realized using processes, whereas in “true” OO languages everything is an object. Processes aren’t also used as a data type, as you have records and tuples for this. Still, through, the essential modelling is done using processes. I believe that what Erlang does with them is the best way you currently can cross a variant of object orientation with functional programming, even if concurrency isn’t a priority. It certainly seems much more reasonable than trying to fit the traditional model with classes to the functional world.
Joe Armstrong certainly knows all this, and talks about it between the lines sometimes, but I didn’t seen it discussed in depth anywhere. For some more insight into the language, you may want to see this great talk with Joe: part1, part2, and also this article on oop.
Comments ():
Mark Aufflick, 1 December 2007, 2:35 pm
I completely agree. It’s interesting to note that Alan Kay (one of the creators of SmallTalk and OO) says that OO is more about message passing than it is about objects - and Erlang provides a really interesting different angle on message passing.
she, 1 December 2007, 5:09 pm
But this is problematic because you can say that a message is in fact an object as well. What I believe will be a good new language is one that easliy combines events and trigger hooks listening on multiple events (i.e. multiple servers), and each “master” object that controls these multiple events can clone itself spawning more events that trigger other events….
I am curious how swarm AI will turn out, in essence I think we could in a few years have very strong interacting robots (real robot teams, not those stupid soccer robots…)
Anyway, I
Isaac Gouy, 1 December 2007, 7:46 pm
“The rest of the chapter was a discussion of why we thought that OO *design* might be acceptable but why OO *languages* are not.”
http://www.erlang.org/pipermail/erlang-questions/2007-November/030920.html
> “you have many things in Erlang that aren’t at all realized using processes, whereas in “true” OO languages everything is an object.”
iow sequential Erlang is a functional programming language - nothing about it could be confused with an OO programming language.
> You can also build reusable components with them …
And you can build reusable components in non-OO imperative languages.
> seems much more reasonable than trying to fit the traditional model with classes to the functional world
Have you looked at Scala?
Isaac Gouy, 1 December 2007, 7:48 pm
Mark Aufflick wrote
> It’s interesting to note that Alan Kay …
It’s also interesting to note how comprehensively what Alan Kay said seems to have been ignored.
Pierre R, 1 December 2007, 8:15 pm
What Alan Kay said about objects ?
“OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things”
Another well known and respected equation for objects is (not from me) :
Objects == Lambda Abstraction + Message Dispatch + Local Side Effects
Now there is another model that some OO people seem to ignored completely called the actor model :
Actor == Lambda Abstraction + Message Dispatch (asynchronous between actors) .
These two models are known since the early 70 (and before ).
Erlang obviously does not encourage local side effect and communication between processes is asynchronous.
That is why Erlang is not really perceived as an OO languages …
I hope it helps.
stiff, 1 December 2007, 9:28 pm
Pierre, Mark, she: Thanks for the comments
Issac: I think you misunderstood my intentions. I don’t want to prove that Erlang processes are exactly equal to objects in other languages, nor do I want to bring traditional OOP to Erlang, what the mailing list post seems to be about. I just believe that Erlang processes are conceptually very similar to what we intuitively understand as “objects”, and in some ways, ie. by focusing on the cooperation/message-exchange part, they encourage better mapping of real world concepts to “objects” than the mainstream pseudo-OO languages like Java.
Isaac Gouy, 1 December 2007, 11:18 pm
> I think you misunderstood my intentions
I don’t know your intentions, just what you write.
links for 2007-12-04 « Mike Does Tech, 4 December 2007, 2:45 am
[...] Stifflog - Erlang, the functional Smalltalk? [...]