Com S 342 meeting -*- Outline -*- * Variable Assignment (5.5) Note: when drawing pictures of environments, use pictures like those needed for chapter 6. That is, draw it showing names labeling the cells empty-ff ^ | init-env [ *-]---> [extended-ff* | *| *| * ] | | v v + [ *-]---> [prim-proc | + ] - [ *-]---> [prim-proc | - ] * [ *-]---> [prim-proc | * ] add1 [ *-]---> [prim-proc | add1 ] or more schemematically ^ | parent [ * ] env [ *-]----> x [ *-]---> 3 y [ *-]---> 4 z [ *-]---> 2 ** syntax ------------------------------------------ ASSIGNMENT (5.5) Concrete syntax: ::= := Examples: x := 3 y := +(y, x) Abstract syntax: ------------------------------------------ Note that it's :=, not = in the concrete syntax give another example! Q: What are the advantages of this concrete syntax vs. FORTRAN or C? less error prone for humans ... (define-record varassign (var exp)) ** semantics Q: How would you describe an assignment? changes the value of a variable What's a variable, semantically? a box, object, cell Do we have those around? no *** domains ------------------------------------------ PROBLEM WITH ENVIRONMENTS Currently: Environment = -> Denoted Value Denoted Value = Number + Procedure Can an expression change the environment like assignment should? Example: let x = 5 in let addx = proc(n) +(n,x) in let first = proc(ignored) addx(20) in first(x := 10) ------------------------------------------ ... it can extend it, but that's different than assignment, let extends the environment, but doesn't affect caller Q: How could we make that happen? use cells ------------------------------------------ USING CELLS Change to: Environment = -> Denoted Value Denoted Value = where Expressed Value = Number + Procedure + Void In file ch5-5-denoted-value.scm (load-from-lib "cell.scm") In file ch5-5-denoted-value.def (defrep Denoted-Value (cell Expressed-Value)) (deftype make-cell (-> (Expressed-Value) Denoted-Value)) (deftype cell-ref (-> (Denoted-Value) Expressed-Value)) (deftype cell-set! (-> (Denoted-Value Expressed-Value) void)) (deftype cell-swap! (-> (Denoted-Value Denoted-Value) void)) ------------------------------------------ ... Cell (Expressed Value) ------------------------------------------ CONVERTING BETWEEN EXPRESSED AND DENOTED VALUES In file ch5-5-expressed-value.scm (define denoted->expressed ;; TYPE: (-> (Denoted-Value) ;; Expressed-Value) (define expressed->denoted ;; TYPE: (-> (Expressed-Value) ;; Denoted-Value) ------------------------------------------ ... cell-ref) ... make-cell) *** semantic functions ------------------------------------------ THE INTERPRETER WITH ASSIGNMENT What changes to the interpreter? ;;; Figure 5.5.1 : page 157 (define eval-exp (lambda (exp env) (variant-case exp (lit (datum) (number->expressed datum)) (app (rator rands) (let ((proc (expressed->procedure (eval-exp rator env))) (args (eval-rands rands env))) (apply-proc proc args))) (varref (var) (apply-env env var) ;; ... (varassign (var exp) ------------------------------------------ ... (denoted->expressed (apply-env env var)) ... (void->expressed (cell-set! (apply-env env var) (eval-exp exp env)))) ------------------------------------------ APPLYING PROCEDURES (define apply-proc ;; TYPE: (-> (Procedure ;; (list Expressed-Value)) ;; Expressed-Value) (lambda (proc args) (variant-case proc (prim-proc (prim-op) (apply-prim-op prim-op args)) (closure (formals body env) (eval-exp body (extend-env formals env))) (else (error "Invalid procedure: " proc))))) def: an L-value is def: an R-value is ------------------------------------------ Q: What is the type of the second argument to extend-env? (list Denoted-Value) Q: And what is that? a cell Q: And what is the type of args? (list Expressed-Value) Q: So what should be the second argument here? ... (map expressed->denoted args) or (map make-cell args) Q: Why don't we do that just before we apply a procedure, and not here in apply-proc? because the primitives don't expect cells How would we change that? Change apply-prim-op! ... a Denoted Value (left of an assignment, a cell) ... an Expressed Value (right of an assignment, a value) Q: What else needs to be changed in the interpreter? Why? the initial environment, it doesn't map to cells. ** sequencing (exercise 5.5.4) add begin to the interepreter Q: Once we have sequencing and assignment, what other syntax could we have in the defined language? - for, while, as in the homework ** semantic ideas (exercise 5.5.9) model store as a finite function, and pass it around