Com S 342 meeting -*- Outline -*- * Local Binding (3.4) ** syntax ------------------------------------------ NAMING AND LET (3.4) Concrete syntax: ::= let { = }* in Examples: let x = 5 y = 6 in +(x, y) let x = 3 in let x = *(x, x) in +(x, x) Abstract syntax: ------------------------------------------ Q: What should the values of the examples be? Syntax in the-grammar (expression ("let" (arbno identifier "=" expression) "in" expression) let-exp) ASTs: ... (define-datatype expression expression? ... (let-exp (ids (list-of symbol?)) (rands (list-of expression?)) (body expression?)) ** scoping Q: What should the scope rules be? lexical: binding region is body, inner lets create holes Q: Where are we going to record the values of these variables? Q: Will that work with the scoping? Q: If the interpreter took both an expression and an environment, what would have to change? ------------------------------------------ INTERPRETER WITH LET (deftype eval-expression (-> (expression environment) Expressed-Value)) (define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) (number->expressed datum)) (var-exp (id) (apply-env env id)) ; ... ------------------------------------------ ... (let-exp (ids rands body) (let ((args (eval-rands rands env))) (eval-expression body (extend-env ids args env)))) ** semantic implications and variations Q: How does this make the scope rules for let work out? (see exercise 12) *** sequential let Like let* in Scheme That is, a sugared version of: let x = 3 in let y = x in +(x,y) Syntax: (expression ("seq-let" (arbno identifier "=" expression) "in" expression) seq-let-exp) ASTs: (seq-let-exp (ids (list-of symbol?)) (rands (list-of expression?)) (body expression?)) Case in eval-expression: (seq-let-exp (ids rands body) (eval-expression body (seq-let-env ids rands env))) Work out seq-let-env from examples ( (run "seq-let x = 3 in +(x, zero)") ==> 3 ) ( (run "seq-let x = 3 y = x in +(x,y)") ==> 6 )