Lecture 8 Design and Implementation of PLs [0.5em] (4)...
Transcript of Lecture 8 Design and Implementation of PLs [0.5em] (4)...
![Page 1: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/1.jpg)
ENE4014: Programming Languages
Lecture 8 — Design and Implementation of PLs
(4) States
Woosuk Lee2020 Spring
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 1 / 30
![Page 2: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/2.jpg)
Review: Our Language So Far
Our language has expressions and procedures.
Syntax
P → E
E → n| x| E + E| E − E| iszero E| if E then E else E| let x = E in E| read
| letrec f(x) = E in E| proc x E| E E
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 2 / 30
![Page 3: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/3.jpg)
Review: Our Language So Far
Semantics
ρ ` n ⇒ n ρ ` x ⇒ ρ(x)
ρ ` E1 ⇒ n1 ρ ` E2 ⇒ n2
ρ ` E1 + E2 ⇒ n1 + n2
ρ ` E ⇒ 0
ρ ` iszero E ⇒ true
ρ ` E ⇒ n
ρ ` iszero E ⇒ falsen 6= 0
ρ ` read ⇒ n
ρ ` E1 ⇒ true ρ ` E2 ⇒ v
ρ ` if E1 then E2 else E3 ⇒ v
ρ ` E1 ⇒ false ρ ` E3 ⇒ v
ρ ` if E1 then E2 else E3 ⇒ v
ρ ` E1 ⇒ v1 [x 7→ v1]ρ ` E2 ⇒ v
ρ ` let x = E1 in E2 ⇒ v
[f 7→ (f, x, E1, ρ)]ρ ` E2 ⇒ v
ρ ` letrec f(x) = E1 in E2 ⇒ v
ρ ` proc x E ⇒ (x,E, ρ)
ρ ` E1 ⇒ (x,E, ρ′) ρ ` E2 ⇒ v [x 7→ v]ρ′ ` E ⇒ v′
ρ ` E1 E2 ⇒ v′
ρ ` E1 ⇒ (f, x, E, ρ′) ρ ` E2 ⇒ v [x 7→ v, f 7→ (f, x, E, ρ′)]ρ′ ` E ⇒ v′
ρ ` E1 E2 ⇒ v′
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 3 / 30
![Page 4: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/4.jpg)
This Lecture: Adding States to the Language
So far, our language only had the values produced by computation.
But computation also has effects: it may change the state of memory.
We will extend the language to support computational effects:I Syntax for creating and using memory locationsI Semantics for manipulating memory states
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 4 / 30
![Page 5: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/5.jpg)
Motivating Example
How can we compute the number of times f has been called?
let f = proc (x) (x)
in (f (f 1))
Does the following program work?
let counter = 0
in let f = proc (x) (let counter = counter + 1
in x)
in let a = (f (f 1))
in counter
The binding of counter is local. We need global effects.
Effects are implemented by introducing memory (store) and locations(reference).
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 5 / 30
![Page 6: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/6.jpg)
Motivating Example
How can we compute the number of times f has been called?
let f = proc (x) (x)
in (f (f 1))
Does the following program work?
let counter = 0
in let f = proc (x) (let counter = counter + 1
in x)
in let a = (f (f 1))
in counter
The binding of counter is local. We need global effects.
Effects are implemented by introducing memory (store) and locations(reference).
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 5 / 30
![Page 7: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/7.jpg)
Motivating Example
How can we compute the number of times f has been called?
let f = proc (x) (x)
in (f (f 1))
Does the following program work?
let counter = 0
in let f = proc (x) (let counter = counter + 1
in x)
in let a = (f (f 1))
in counter
The binding of counter is local. We need global effects.
Effects are implemented by introducing memory (store) and locations(reference).
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 5 / 30
![Page 8: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/8.jpg)
Two Approaches
Programming languages support references explicitly or implicitly.
Languages with explicit references provide a clear account ofallocation, dereference, and mutation of memory cells.
I e.g., OCaml, F#
In languages with implicit references, references are built-in.References are not explicitly manipulated.
I e.g., C and Java.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 6 / 30
![Page 9: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/9.jpg)
A Language with Explicit References
P → E
E → n | x| E + E | E − E| iszero E | if E then E else E| let x = E in E| proc x E | E E| ref E| ! E| E := E| E;E
ref E allocates a new location, store the value of E in it, and returns it.
! E returns the contents of the location that E refers to.
E1 := E2 changes the contents of the location (E1) by the value of E2.
E1;E2 executes E1 and then E2 while accumulating effects.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 7 / 30
![Page 10: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/10.jpg)
Example 1
let counter = ref 0
in let f = proc (x) (counter := !counter + 1; !counter)
in let a = (f 0)
in let b = (f 0)
in (a - b)
let f = let counter = ref 0
in proc (x) (counter := !counter + 1; !counter)
in let a = (f 0)
in let b = (f 0)
in (a - b)
let f = proc (x) (let counter = ref 0
in (counter := !counter + 1; !counter))
in let a = (f 0)
in let b = (f 0)
in (a - b)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 8 / 30
![Page 11: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/11.jpg)
Example 1
let counter = ref 0
in let f = proc (x) (counter := !counter + 1; !counter)
in let a = (f 0)
in let b = (f 0)
in (a - b)
let f = let counter = ref 0
in proc (x) (counter := !counter + 1; !counter)
in let a = (f 0)
in let b = (f 0)
in (a - b)
let f = proc (x) (let counter = ref 0
in (counter := !counter + 1; !counter))
in let a = (f 0)
in let b = (f 0)
in (a - b)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 8 / 30
![Page 12: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/12.jpg)
Example 1
let counter = ref 0
in let f = proc (x) (counter := !counter + 1; !counter)
in let a = (f 0)
in let b = (f 0)
in (a - b)
let f = let counter = ref 0
in proc (x) (counter := !counter + 1; !counter)
in let a = (f 0)
in let b = (f 0)
in (a - b)
let f = proc (x) (let counter = ref 0
in (counter := !counter + 1; !counter))
in let a = (f 0)
in let b = (f 0)
in (a - b)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 8 / 30
![Page 13: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/13.jpg)
Example 2
We can make chains of references:
let x = ref (ref 0)
in (!x := 11; !(!x))
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 9 / 30
![Page 14: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/14.jpg)
Semantics
Memory is modeled as a finite map from locations to values:
Val = Z + Bool + Procedure+LocProcedure = Var × E × Envρ ∈ Env = Var → Valσ ∈Mem = Loc → Val
Semantics rules additionally describe memory effects:
ρ, σ ` E ⇒ v, σ′
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 10 / 30
![Page 15: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/15.jpg)
SemanticsExisting rules are enriched with memory effects:
ρ, σ ` n ⇒ n, σ ρ, σ ` x ⇒ ρ(x), σ
ρ, σ0 ` E1 ⇒ n1, σ1 ρ, σ1 ` E2 ⇒ n2, σ2
ρ, σ0 ` E1 + E2 ⇒ n1 + n2, σ2
ρ, σ0 ` E ⇒ 0, σ1
ρ, σ0 ` iszero E ⇒ true, σ1
ρ, σ0 ` E ⇒ n, σ1
ρ, σ0 ` iszero E ⇒ false, σ1n 6= 0
ρ, σ0 ` E1 ⇒ true, σ1 ρ, σ1 ` E2 ⇒ v, σ2
ρ, σ0 ` if E1 then E2 else E3 ⇒ v, σ2
ρ, σ0 ` E1 ⇒ false, σ1 ρ, σ1 ` E3 ⇒ v, σ2
ρ, σ0 ` if E1 then E2 else E3 ⇒ v, σ2
ρ, σ0 ` E1 ⇒ v1, σ1 [x 7→ v1]ρ, σ1 ` E2 ⇒ v, σ2
ρ, σ0 ` let x = E1 in E2 ⇒ v, σ2
ρ, σ ` proc x E ⇒ (x,E, ρ), σ
ρ, σ0 ` E1 ⇒ (x,E, ρ′), σ1 ρ, σ1 ` E2 ⇒ v, σ2 [x 7→ v]ρ′, σ2 ` E ⇒ v′, σ3
ρ, σ0 ` E1 E2 ⇒ v′, σ3
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 11 / 30
![Page 16: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/16.jpg)
Semantics
Rules for new constructs:
ρ, σ0 ` E ⇒ v, σ1
ρ, σ0 ` ref E ⇒ l, [l 7→ v]σ1l 6∈ Dom(σ1)
ρ, σ0 ` E ⇒ l, σ1
ρ, σ0 ` ! E ⇒ σ1(l), σ1
ρ, σ0 ` E1 ⇒ l, σ1 ρ, σ1 ` E2 ⇒ v, σ2
ρ, σ0 ` E1 := E2 ⇒ v, [l 7→ v]σ2
ρ, σ0 ` E1 ⇒ v1, σ1 ρ, σ1 ` E2 ⇒ v2, σ2
ρ, σ0 ` E1;E2 ⇒ v2, σ2
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 12 / 30
![Page 17: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/17.jpg)
Example
ρ, σ0 ` let x = ref (ref 0) in (!x := 11; !(!x))⇒
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 13 / 30
![Page 18: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/18.jpg)
Exercise
Extend the language with recursive procedures:
P → E
E → n | x| E + E | E − E| iszero E | if E then E else E| let x = E in E| letrec f(x) = E in E| proc x E | E E| ref E| ! E| E := E| E;E
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 14 / 30
![Page 19: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/19.jpg)
Exercise (Continued)
Domain:
Val = Z + Bool + Procedure+LocProcedure = Var × E × Envρ ∈ Env = Var → Valσ ∈Mem = Loc → Val
Semantics rules:
ρ, σ0 ` letrec f(x) = E1 in E2 ⇒
ρ, σ0 ` E1 E2 ⇒
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 15 / 30
![Page 20: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/20.jpg)
A Language with Implicit References
P → E
E → n | x| E + E | E − E| iszero E | if E then E else E| let x = E in E| proc x E | E E| x := E| E;E
In this design, every variable denotes a reference and is mutable.
x := E changes the contents of x by the value of E.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 16 / 30
![Page 21: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/21.jpg)
Examples
Computing the number of times f has been called:
let counter = 0
in let f = proc (x) (counter := counter + 1; counter)
in let a = (f 0)
in let b = (f 0)
in (a-b)
let f = let counter = 0
in proc (x) (counter := counter + 1; counter)
in let a = (f 0)
in let b = (f 0)
in (a-b)
let f = proc (x) (let counter = 0
in (counter := counter + 1; counter))
in let a = (f 0)
in let b = (f 0)
in (a-b)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 17 / 30
![Page 22: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/22.jpg)
Examples
Computing the number of times f has been called:
let counter = 0
in let f = proc (x) (counter := counter + 1; counter)
in let a = (f 0)
in let b = (f 0)
in (a-b)
let f = let counter = 0
in proc (x) (counter := counter + 1; counter)
in let a = (f 0)
in let b = (f 0)
in (a-b)
let f = proc (x) (let counter = 0
in (counter := counter + 1; counter))
in let a = (f 0)
in let b = (f 0)
in (a-b)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 17 / 30
![Page 23: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/23.jpg)
Examples
Computing the number of times f has been called:
let counter = 0
in let f = proc (x) (counter := counter + 1; counter)
in let a = (f 0)
in let b = (f 0)
in (a-b)
let f = let counter = 0
in proc (x) (counter := counter + 1; counter)
in let a = (f 0)
in let b = (f 0)
in (a-b)
let f = proc (x) (let counter = 0
in (counter := counter + 1; counter))
in let a = (f 0)
in let b = (f 0)
in (a-b)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 17 / 30
![Page 24: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/24.jpg)
Exercise
What is the result of the program?
let f = proc (x)
proc (y)
(x := x + 1; x - y)
in ((f 44) 33)
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 18 / 30
![Page 25: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/25.jpg)
Semantics
References are no longer values and every variable denotes a reference:
Val = Z + Bool + ProcedureProcedure = Var × E × Envρ ∈ Env = Var → Locσ ∈Mem = Loc → Val
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 19 / 30
![Page 26: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/26.jpg)
Semantics
ρ, σ ` n ⇒ n, σ ρ, σ ` x ⇒ σ(ρ(x)), σ
ρ, σ0 ` E1 ⇒ n1, σ1 ρ, σ1 ` E2 ⇒ n2, σ2
ρ, σ0 ` E1 + E2 ⇒ n1 + n2, σ2
ρ, σ0 ` E ⇒ 0, σ1
ρ, σ0 ` iszero E ⇒ true, σ1
ρ, σ0 ` E1 ⇒ true, σ1 ρ, σ1 ` E2 ⇒ v, σ2
ρ, σ0 ` if E1 then E2 else E3 ⇒ v, σ2
ρ, σ ` proc x E ⇒ (x,E, ρ), σ
ρ, σ0 ` E ⇒ v, σ1
ρ, σ0 ` x := E ⇒ v, [ρ(x) 7→ v]σ1
ρ, σ0 ` E1 ⇒ v1, σ1 [x 7→ l]ρ, [l 7→ v1]σ1 ` E2 ⇒ v, σ2
ρ, σ0 ` let x = E1 in E2 ⇒ v, σ2l 6∈ Dom(σ1)
ρ, σ0 ` E1 ⇒ (x,E, ρ′), σ1 ρ, σ1 ` E2 ⇒ v, σ2
[x 7→ l]ρ′, [l 7→ v]σ2 ` E ⇒ v′, σ3
ρ, σ0 ` E1 E2 ⇒ v′, σ3l 6∈ Dom(σ2)
ρ, σ0 ` E1 ⇒ v1, σ1 ρ, σ1 ` E2 ⇒ v2, σ2
ρ, σ0 ` E1;E2 ⇒ v2, σ2
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 20 / 30
![Page 27: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/27.jpg)
Example
let f = let count = 0
in proc (x) (count := count + 1; count)
in let a = (f 0)
in let b = (f 0)
in a - b
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 21 / 30
![Page 28: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/28.jpg)
Exercise
Extend the language with recursive procedures:
P → E
E → n | x| E + E | E − E| iszero E | if E then E else E| let x = E in E| letrec f(x) = E in E| proc x E | E E| x := E| E;E
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 22 / 30
![Page 29: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/29.jpg)
Exercise (Continued)
Domain:Val = Z + Bool + Procedure
Procedure = Var × E × Envρ ∈ Env = Var → Locσ ∈Mem = Loc → Val
Semantics rules:
ρ, σ0 ` letrec f(x) = E1 in E2 ⇒
ρ, σ0 ` E1 E2 ⇒
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 23 / 30
![Page 30: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/30.jpg)
Parameter-Passing Variations
Our current strategy of calling a procedure is call-by-value. Theformal parameter refers to a new location containing the value of theactual parameter:
ρ, σ0 ` E1 ⇒ (x,E, ρ′), σ1 ρ, σ1 ` E2 ⇒ v, σ2
[x 7→ l]ρ′, [l 7→ v]σ2 ` E ⇒ v′, σ3
ρ, σ0 ` E1 E2 ⇒ v′, σ3l 6∈ Dom(σ2)
The most commonly used form of parameter-passing.
For example, the assignment to x has no effect on the contents of a:
let p = proc (x) (x := 4)
in let a = 3
in ((p a); a)
Under call-by-reference, the assignment changes the value of a afterthe call.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 24 / 30
![Page 31: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/31.jpg)
Parameter-Passing Variations
Our current strategy of calling a procedure is call-by-value. Theformal parameter refers to a new location containing the value of theactual parameter:
ρ, σ0 ` E1 ⇒ (x,E, ρ′), σ1 ρ, σ1 ` E2 ⇒ v, σ2
[x 7→ l]ρ′, [l 7→ v]σ2 ` E ⇒ v′, σ3
ρ, σ0 ` E1 E2 ⇒ v′, σ3l 6∈ Dom(σ2)
The most commonly used form of parameter-passing.
For example, the assignment to x has no effect on the contents of a:
let p = proc (x) (x := 4)
in let a = 3
in ((p a); a)
Under call-by-reference, the assignment changes the value of a afterthe call.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 24 / 30
![Page 32: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/32.jpg)
Parameter-Passing Variations
Our current strategy of calling a procedure is call-by-value. Theformal parameter refers to a new location containing the value of theactual parameter:
ρ, σ0 ` E1 ⇒ (x,E, ρ′), σ1 ρ, σ1 ` E2 ⇒ v, σ2
[x 7→ l]ρ′, [l 7→ v]σ2 ` E ⇒ v′, σ3
ρ, σ0 ` E1 E2 ⇒ v′, σ3l 6∈ Dom(σ2)
The most commonly used form of parameter-passing.
For example, the assignment to x has no effect on the contents of a:
let p = proc (x) (x := 4)
in let a = 3
in ((p a); a)
Under call-by-reference, the assignment changes the value of a afterthe call.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 24 / 30
![Page 33: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/33.jpg)
Call-By-Reference Parameter-Passing
The location of the caller’s variable is passed, rather than the contents ofthe variable.
Extend the syntax:
E →...
| E E| E 〈y〉
Extend the semantics:
ρ, σ0 ` E1 ⇒ (x,E, ρ′), σ1 [x 7→ ρ(y)]ρ′, σ1 ` E ⇒ v′, σ2
ρ, σ0 ` E1 〈y〉 ⇒ v′, σ2
What is the benefit of call-by-reference compared to call-by-value?
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 25 / 30
![Page 34: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/34.jpg)
Examples
let p = proc (x) (x := 4)
in let a = 3
in ((p <a>); a)
let f = proc (x) (x := 44)
in let g = proc (y) (f <y>)
in let z = 55
in ((g <z>); z)
let swap = proc (x) proc (y)
let temp = x
in (x := y; set y = temp)
in let a = 33
in let b = 44
in (((swap <a>) <b>); (a-b))
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 26 / 30
![Page 35: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/35.jpg)
Examples
let p = proc (x) (x := 4)
in let a = 3
in ((p <a>); a)
let f = proc (x) (x := 44)
in let g = proc (y) (f <y>)
in let z = 55
in ((g <z>); z)
let swap = proc (x) proc (y)
let temp = x
in (x := y; set y = temp)
in let a = 33
in let b = 44
in (((swap <a>) <b>); (a-b))
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 26 / 30
![Page 36: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/36.jpg)
Examples
let p = proc (x) (x := 4)
in let a = 3
in ((p <a>); a)
let f = proc (x) (x := 44)
in let g = proc (y) (f <y>)
in let z = 55
in ((g <z>); z)
let swap = proc (x) proc (y)
let temp = x
in (x := y; set y = temp)
in let a = 33
in let b = 44
in (((swap <a>) <b>); (a-b))
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 26 / 30
![Page 37: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/37.jpg)
Variable Aliasing
More than one call-by-reference parameter may refer to the same location:
let b = 3
in let p = proc (x) proc (y)
(x := 4; y)
in ((p <b>) <b>)
A variable aliasing is created: x and y refer to the same location
With aliasing, reasoning about program behavior is very difficult,because an assignment to one variable may change the value ofanother.
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 27 / 30
![Page 38: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/38.jpg)
Lazy Evaluation
So far all the parameter-passing strategies are eager in that theyalways evaluate the actual parameter before calling a procedure.
In eager evaluation, procedure arguments are completely evaluatedbefore passing them to the procedure.
On the other hand, lazy evaluation delays the evaluation of argumentsuntil it is actually needed. If the procedure body never uses theparameter, it will never be evaluated.
Lazy evaluation potentially avoids non-termination:
letrec infinite(x) = (infinite x)
in let f = proc (x) (1)
in (f (infinite 0))
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 28 / 30
![Page 39: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/39.jpg)
Lazy Evaluation
Comparison to Eager evaluation
Eager Evaluation Lazy Evaluation
Space-efficient Time-efficient
Cannot avoid non-termination Can avoid non-termination
Easy to reason with the order of evaluation Hard
Easy to reason with programs with effects Hard
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 29 / 30
![Page 40: Lecture 8 Design and Implementation of PLs [0.5em] (4) Statespsl.hanyang.ac.kr/~wslee/courses/ene4014/lec8.pdfIn this design, every variable denotes a reference and is mutable. x:=](https://reader034.fdocuments.us/reader034/viewer/2022050205/5f585bf9d6ca2704e66355e1/html5/thumbnails/40.jpg)
Summary
Our language is now (somewhat) realistic:
expressions, procedures, recursion,
states with explicit/implicit references
parameter-passing variations
Woosuk Lee ENE4014 2020 Spring, Lecture 8 April 26, 2020 30 / 30