wiki:BigIdeas/MessagePassing

Message passing

Message passing how things communicate in the world (biology) and, naturally, it is the fundamental concept behind original OOP paradigm.

Messages are asynchronous and have a unique structure. That is it.

Lexical closures

Closures has their private inner frames of the environment attached to it where the captured bindings of free variables and bindings for formal parameters are stored. In other words, a closure (an object produced by lambda, so to speak) encapsulates its state in its own frame of environment.

These closures could be communicated with by sending messages to them. A message could be implemented as a parameter for a procedure call.

This vision of closures which completely encapsulate its internal state and communicate only by message-passing has been popularized by Alan Kay and his Smalltalk. This is the essence of OOP paradigm, not class hierarchies or even inheritance. Mixins or Traits is a refinement of classes.

(define (cons x y)
  (lambda (msg)
    (cond ((eq? msg 'CAR) x)
          ((eq? msg 'CDR) y)
          ((eq? msg 'PAIR) #t)
          (else (error "Pair can't " msg)))))

to complete the ADT (which is also a closure)

(define (car p) (p 'CAR))
(define (cdr p) (p 'CDR))

(define (pair? p) (and (procedure? p) (p 'PAIR)))

There is a more sophisticated closure with has its own internal procedures (which have access to the same private frame of environment of the enclosing closure), and has variable number of arguments.

(define (cons x y)
  (define (change-car new-car) (set! x new-car))
  (define (change-cdr new-cdr) (set! y new-cdr))
  (lambda (msg . args)
    (cond ((eq? msg 'CAR) x)
          ((eq? msg 'CDR) y)
          ((eq? msg 'PAIR) #t)
          ((eq? msg 'SET-CAR!) (change-car (car args)))
          ((eq? msg 'SET-CDR!) (change-cdr (car args)))
          (else (error "Pair can't " msg)))))

extending the ADT

(define (set-car! p val) (p 'SET-CAR! val))
(define (set-cdr! p val) (p 'SET-CDR! val))
Last modified 20 months ago Last modified on Nov 4, 2018, 5:08:24 AM
Note: See TracWiki for help on using the wiki.