[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

case-lambda and interpreter environments



Since there's been some talk of case-lambda, thought I'd say something
about a simple use of this syntax to get a nice implementation of 
interpreter environments. The definition of the initial environment
(which doesn't associate any identifier with any value) goes as
follows:

(define init-env
  (letrec ([extend (lambda (r x v)
		     (rec r1
			 (case-lambda
			  [(y) (if (eq? y x) v (r y))]
			  [(y w) (extend r1 y w)])))])
    (rec r
	(case-lambda
	 [(x) (error "unbound ~a" x)]
	 [(x v) (extend r x v)]))))

No other function is required! When called with a single argument (an
identifier), the environment yields the value of the identifier; when
called with two arguments (an identifier and a value) a new
environment is produced which is the extension of the original one
associating the new identifier with the new value. 

One attraction (fatal? |-]) with this is that the use of the
environment closely mimics denotational semantics notation, viz.,

r[x] 	;for looking up identifier x in environment r; and
r[x/v]	;for extending environment r to associate x with v.

--dorai