4 ms·
In CLOS.
by dschiptsov 12y ago
In CLOS.
- dschiptsov 12y agoWhy there are so many ignorant idiots in the world? According to Alan Kay, the essence of OO approach (or paradigm if you wish) is a synthesis of a few fundamental concepts (like a Lisp itself) where not a single one can be removed because harmony (or balance) would be destroyed, and an ugly, mediocre construction would emerge instead (Java). The concepts are: * First-class closures - isolated, share-nothing abstractions. * Nesting of closures, so they could have an internal structure. * Message passing as the only way of communication among closures. As long as you follow these three principles, you have "generics" for free, because all the operations are mere messages. These are three of four principles upon which Smalltalk has been built upon. Here is an illustration, assuming that we are implementing message passing as simple as a procedures call. (define (kons x y) (lambda (m) (cond ((eq? m 'car) x) ((eq? m 'cdr) y) (else (error "bottom!"))))) (define (kar x) (x 'car)) (define (kdr x) (x 'cdr)) Notice that using only these abstractions we could implement a unified list structure of Lisps (recall Escher's hands). This is (surprise!) the "core" of Erlang - extending of closure-based language with immutable data with explicit message-passing, to create first-class agents (servers, etc). Where is inheritance? Inheritance is just a protocol which specifies what to do with unknown message instead of signaling an error or returning a bottom like in Haskell. What is MOP could be found in Wikipedia. Smalltalk was its ancestor. What is CLOS? It is a DSL embedded in a Lisp (based on structures made out of closures) which implements some or other subset of MOP. MIT Scheme, for example, has SOS. So FP meet OP in CLOS.
- gclaramunt 12y ago> Where is inheritance? Inheritance is just a protocol which specifies what to do with unknown message instead of signaling an error or returning a bottom like in Haskell. Care to explain? Doesn't make any sense to me...
- JoelMcCracken 12y agoAssume a class Run that inherits from class Action. If a message is sent to an instance of run Run that the Run class has no knowledge of, the message will be sent to the parent. Thus, if message m is unknown to Run, see if the parent classes know it.
- dschiptsov 12y agoA protocol is like a set of rules. It it specifies, for example, that each object (a closure) has special bindings, which holds references to its class (which is also a closure) and to itself, so it could send a massage to its super or to self. Classes could have its own methods (inner closures). Basically, when you send a message, you have to specify a symbol, that corresponds to a method name, and parameters. The protocol specifies the "routing", like if there is no method that corresponds to this name, then send (forward) the very same message to your superior (exactly like they do in army, which is an implementation of the actor model). If supers doesn't know what to do with the message, then there is panic - an exception. The protocol also specifies the rules of shadowing of symbols, such that a method could be redefined (shadowed) within a closure, so it "overrides" a method with the same name from its ancestors. Object methods (inner closures) could send messages to classes (invoking class methods) and then doing something with a returned value. Usually, there is some syntactic sugar for that. In short, inheritance is a set of rules, and these rules could be implemented using various mechanism, like explicit message-passing (a-la Erlang) or implicitly, using procedures calls. Brian Harvey in classic Berkeley CS61A explained these ideas much better than me. https://www.youtube.com/watch?v=7u3pp7JCAIU https://www.youtube.com/watch?v=7u3pp7JCAIU
- gclaramunt 12y agoyes, that part makes sense, but in Haskell, an unknown "message" is a compiler error, and bottom represents abnormal termination (e.g exception or even non termination), I don't really understand how that's related to inheritance.
- dschiptsov 12y ago