LET Language, PROC Language, and Explicit-Refs: Concepts and Implementations, Study notes of Computer Science

An introduction to the let language, proc language, and explicit-refs concepts in the context of the cis 352 programming languages course. It covers the syntax, semantics, and specifications of these languages, as well as examples and implementation details. Students can use this document as study notes, summaries, or references for understanding these topics.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-qt3
koofers-user-qt3 🇺🇸

9 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The LET Language
hprogrami::=hexpressioni
hexpressioni::=hnumberi
::=hidentifieri
::=(hexpressioni,hexpressioni)
::=zero?(hexpressioni)
::=if hexpressionithen hexpressionielse hexpressioni
::=let hidentifieri=hexpressioniin hexpressioni
CIS 352 vProgramming Languages () Random Slides April 7, 2009 1 / 25
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download LET Language, PROC Language, and Explicit-Refs: Concepts and Implementations 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]

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)

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

Exceptions, Continued

let list-index = proc(str) letrec inner(lst) = if null?(lst) then raise("ListIndexFailed") else if string-equal?(car(list), str) then 0 else -((inner cdr(lst)), -1) in ...

A possible handler let find-member-number = proc(member-name) ... try (list-index member-name member-list) catch(exn) raise("CannotFindMemberNumber") or in place of raise("CannotFindMemberNumber") we could have the-default-member-number

Exceptions, Implementation I

We add two new continuation-builders (try-cont (var symbol?) (handler-exp expression?) (env environment?) (cont continuation?)) (raise1-cont (saved-cont continuation?))