









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the let language, which includes the syntax and semantics of expressions, procedures, and recursion. Additionally, it covers the handling of state using explicit and implicit references. Examples and specifications for evaluating expressions and procedures.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










〈program〉 ::= 〈expression〉 〈expression〉 ::= 〈number〉 ::= 〈identifier〉 ::= −(〈expression〉, 〈expression〉) ::= zero?(〈expression〉) ::= if 〈expression〉 then 〈expression〉 else 〈expression〉 ::= let 〈identifier〉 = 〈expression〉 in 〈expression〉
〈expression〉 ::=...
::= proc(〈identifier〉)〈expression〉 proc-exp (var body)
::= (〈expression〉 〈expression〉) call-exp (rator rand)
Examples (^1) let f = proc(x) -(x,11) in (f (f 69)) [draw parse tree] 2 ( proc(f) (f (f 69)) proc(x) -(x,11)
) [draw parse tree]
Dealing with Recursion
〈expression〉 ::= letrec 〈identifier〉(〈identifier〉) = 〈expression〉 in 〈expression〉 letrec-exp (p-name b-var p-body letrec-body)
Example letrec double(x) = if zero?(x) then 0 else -((double -(x,1)),-2) in (double 6)
Dealing with Recursion
(value-of (letrec-exp proc-name bound-var proc-body letrec-body) ρ ) = (value-of letrec-body (extend-env-rec proc-name bound-var proc-body ρ )) Let ρ 1 = (extend-env-rec proc-name bound-var proc-body ρ ) (apply-env ρ 1 proc-name) = (proc-val (procedure bound-var proc-body ρ 1 )) (apply-env ρ 1 v) = (apply-env ρ v), if v 6 = proc-name
let x = newref(0) in letrec even(dummy) = if zero?(deref(x)) then zero?(0) else begin setref(x, -(deref(x),1)); (odd 888) end odd(dummy) = if zero?(deref(x)) then zero?(1) else begin setref(x, -(deref(x),1)); (even 888) end in begin setref(x,5); (odd 888) end
let g = let counter = newref(0) in proc (dummy) begin setref(counter, -(deref(counter), -1)); deref(counter) end in let a = (g 11) in let b = (g 11) in let c = (g 11) in c
σ : variable over stores ` : variable over locations [] ≡ the empty store [l = v] σ ≡ the store with v in location l and o/w like σ value-of : Expression × Environment × Store → Value × Store
(value-of exp 1 ρ σ 0 ) = (val 1 , σ 1 ) (value-of exp 2 ρ σ 1 ) = (val 2 , σ 2 ) (value-of (diff-exp exp 1 exp 2 ) ρ σ 0 ) = (dbval 1 c − bval 2 ce, σ 2 )
(value-of exp ρ σ 0 ) = (val, σ 1 ) ∈/ domain( _σ_ 1 ) (value-of (newref-exp exp) _ρ σ_ 0 ) = ((ref-val), [` = val] σ 1 )
(value-of exp ρ σ 0 ) = (, _σ_ 1 ) (value-of (deref-exp exp) _ρ σ_ 0 ) = ( _σ_ 1 (), σ 1 )
(value-of exp 1 ρ σ 0 ) = (, _σ_ 1 ) (value-of exp 2 _ρ σ_ 1 ) = (val, _σ_ 2 ) (value-of (setref-exp exp 1 exp 2 ) _ρ σ_ 0 ) = (d 23 e, [ = val] σ 2 )
ExpVal = Int + Bool + Proc DenVal = Ref (ExpVal)
〈expression〉 ::=... ::= set 〈identifier〉 = 〈expression〉 assign-exp (var exp1)
Note that there is no Ref (Ref (... )) in this set up.
let x = 0 in letrec even(dummy) = if zero?(x) then zero?(0) else begin set x = -(x,1); (odd 888) end odd(dummy) = if zero?(x) then zero?(1) else begin set x = -(x,1); (even 888) end in begin set x = 3; (odd 888) end
Metavariables ρ : environments σ : stores ` : locations [] ≡ the empty store [l = v] σ ≡ the store with v in location l and o/w like σ value-of : Expression × Environment × Store → Value × Store
(value-of (var-exp var) ρ σ ) = ( σ ( ρ (var)), σ )
(value-of exp 1 ρ σ 0 ) = (val 1 , σ 1 ) (value-of (assign-exp var exp 1 ) ρ σ 0 ) = (d 27 e, [ ρ (var) = val 1 ] σ 1 )
(apply-procedure (procedure var body ρ ) val σ ) = (value-of body [var = ] _ρ_ [ = val] σ )
In value-of dereference at each var-exp. (See value-of.) Do the obvious thing for assign-exp. (See value-of.) New locations need to be created where new bindings are created.
(^1) in the initial environment (See init-env.) (^2) in let’s (See value-of.) (^3) in procedure calls (See apply-procedure.) (^4) in letrec’s (See apply-env.) See Figure 4.8 for a sample evaluation.
— Next class —