Docsity

LET Language and Implicit vs Explicit References, Study notes of Computer Science

Partial preview of the text

Download LET Language and Implicit vs Explicit References and more Study notes Computer Science in PDF only on Docsity!

The LET Language

〈program〉 ::= 〈expression〉 〈expression〉 ::= 〈number〉 ::= 〈identifier〉 ::= −(〈expression〉, 〈expression〉) ::= zero?(〈expression〉) ::= if 〈expression〉 then 〈expression〉 else 〈expression〉 ::= let 〈identifier〉 = 〈expression〉 in 〈expression〉

The PROC Language

〈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

The LETREC Language

〈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

Specification of how to evaluate letrec’s

(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

EXPLICIT-REFS: Example 1

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

EXPLICIT-REFS: Example 2

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

EXPLICIT-REFS: Store-Passing Specifications, I

σ : 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 )

EXPLICIT-REFS: Store-Passing Specifications, II

(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 )

IMPLICIT-REFS: Basics

Call-by-value/Implicit-references

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.

IMPLICIT-REFS: Example 1

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

IMPLICIT-REFS: Specifications

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] σ )

IMPLICIT-REFS: Implementation

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 —

Parameter Passing Madness