






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







(a) Reduce the following term using lazy evaluation. (λa b.a b)(λx y.x)((λt.t)(λu.u))(λc.c c c)
(λa b.a b)(λx y.x)((λt.t)(λu.u))(λc.c c c) →β (λb.(λx y.x)b)((λt.t)(λu.u))(λc.c c c) →β ((λx y.x)((λt.t)(λu.u)))(λc.c c c) →β (λy.((λt.t)(λu.u)))(λc.c c c) →β (λt.t)(λu.u) λu.u
(b) Reduce the same term using eager evaluation.
(λa b.a b)(λx y.x)((λt.t)(λu.u))(λc.c c c)
(λa b.a b)(λx y.x)((λt.t)(λu.u))(λc.c c c) →β (λb.(λx y.x)b)((λt.t)(λu.u))(λc.c c c) →β (λb.(λx y.x)b)(λu.u)(λc.c c c) →β ((λx y.x)(λu.u))(λc.c c c) →β (λy.(λu.u))(λc.c c c) →β λu.u
(c) Write the λ-calculus version of or. You will need the following definitions: True ≡ λx.λy.x False ≡ λx.λy.y if then else ≡ λb c d.b c d
Your intuition should be the following: when taking the logical or of two values, if the first value is true, the result will always be true. If the first value is false, then the result will match the second value – if it is true, the or is true, if it is false, the or is false. Using that, we can write or as: or ≡ λa b.a True b You can also think of this using the if then else construct. In that case, you would want to say if a then True else b, where a and b are the two booleans. Substituting in for if then else, we get the same result (technically, we get a True b, but if we lambda-lift a and b, we get the same result as above).
(d) Write a function that, given a pair p, will return pair q such that the first element of p is the second of q and the second of p is the first of q (i.e. that flips the two elements of the pair). You will need the following definitions: pair a, b ≡ λa b x.x a b fst p ≡ λp.p(λx y.x) snd p ≡ λp.p(λx y.y)
If we think of this outside of λ calculus, we would want something like flip p = (snd p, fst p) We can encode that with λ terms as flip ≡ λp.pair (snd p)(fst p) Substituting in for our “macros”, we get flip ≡ λp.(λa b x.x a b)((λp.p(λx y.y)) p)((λp.p(λx y.x)) p) We can then β reduce it a bit flip ≡ λp.(λa b x.x a b)(p(λx y.y))(p(λx y.x)) And a bit more, to get our final answer flip ≡ λp.(λx.x(p(λx y.y))(p(λx y.x)))
(a) Derivations: Using natural semantics (rules are on pages 14–15), provide a derivation for the following term: if (i ≤ n) then (n := n + 1 ) else (i := i + 1 ) Assume the starting σ = {i 7 → 1 , n 7 → 3 }. Feel free to turn the page sideways, or to number parts of the derivation and provide them separately (so you can say # somewhere in the derivation and then elsewhere provide the definition for the #1 part of the derivation tree).
To save space, we will name the following σs: σ 0 = {i 7 → 1 , n 7 → 3 }, σ 1 = {i 7 → 1 , n 7 → 4 }
〈i, σ 0 〉 ⇓ 1 〈n, σ 0 〉 ⇓ 3 true = 1 ≤int 3 〈i ≤ n, σ 0 〉 ⇓ true
〈n, σ 0 〉 ⇓ 3 〈 1 , σ 0 〉 ⇓ 1 4 = 3 +int 1 〈n + 1 , σ 0 〉 ⇓ 4 〈n := n + 1 , σ 0 〉 ⇓ σ 0 [4/n] 〈if (i ≤ n) then (n := n + 1 ) else (i := i + 1 ), σ 0 〉 ⇓ σ 1
(b) Derivations: Using transition semantics (rules are on pages 12–13), provide a derivation for the following term: if (i ≤ n) then (n := n + 1 ) else (i := i + 1 ) Assume the starting σ = {i 7 → 1 , n 7 → 3 }. Feel free to turn the page sideways, or to number parts of the derivation and provide them separately (so you can say # somewhere in the derivation and then elsewhere provide the definition for the #1 part of the derivation tree).
To save space, we will name the following σs: σ 0 = {i 7 → 1 , n 7 → 3 }, σ 1 = {i 7 → 1 , n 7 → 4 }
i.
〈i, σ 0 〉 → σ 0 (i) = 1 〈i ≤ n, σ 0 〉 → 〈 1 ≤ n, σ 0 〉 〈if (i ≤ n) then (n := n + 1 ) else (i := i + 1 ), σ 0 〉 → 〈if ( 1 ≤ n) then (n := n + 1 ) else (i := i + 1 ), σ 0 〉
ii.
〈n, σ 0 〉 → σ 0 (n) = 3 〈 1 ≤ n, σ 0 〉 → 〈 1 ≤ 3 , σ 0 〉 〈if ( 1 ≤ n) then (n := n + 1 ) else (i := i + 1 ), σ 0 〉 → 〈if ( 1 ≤ 3 ) then (n := n + 1 ) else (i := i + 1 ), σ 0 〉
iii.
〈 1 ≤ 3 , σ 0 〉 → true 〈if ( 1 ≤ 3 ) then (n := n + 1 ) else (i := i + 1 ), σ 0 〉 → 〈if true then (n := n + 1 ) else (i := i + 1 ), σ 0 〉
iv. 〈if true then (n := n + 1 ) else (i := i + 1 ), σ 0 〉 → 〈n := n + 1 , σ 0 〉
v.
〈n, σ 0 〉 → σ 0 (n) = 3 〈n + 1 , σ 0 〉 → 〈 3 + 1 , σ 0 〉 〈n := n + 1 , σ 0 〉 → 〈n := 3 + 1 , σ 0 〉
vi.
〈3 + 1, σ 0 〉 → 4 〈n := 3 + 1 , σ 0 〉 → 〈n := 4 , σ 0 〉
vii. 〈n := 4 , σ 0 〉 → σ 0 [4/n] = σ 1
(d) Semantics Rules: Define the necessary natural semantics rules for the for construct defined above. You can reference the natural semantics rules for the IMP language, on pages 14–15, for guidance on how the rules should look.
〈a 0 , σ〉 ⇓ n 0 〈a 1 , σ〉 ⇓ n 1 〈while X ≤ n 1 do (c 0 ; X := X + 1 ), σ[n 0 /X]〉 ⇓ σ′ 〈for X := a 0 to a 1 do c 0 done, σ〉 ⇓ σ′′ where σ′′^ is equal to σ′^ except for X, where it is equal to σ(X), which may be undefined. I believe in class I had σ′^ instead of σ[n 1 /X], and then σ′′^ instead of σ′^ and σ′′′^ instead of σ′′. Both are fine – here it just seems easier to directly write in σ[n 0 /X] instead of defining it first as σ′^ = σ[n 0 /X].
Rules for type derivations:
Constants
` n : int (assuming n is an int)
` true : bool
` false : bool
Variables
Γ ` x : τ if(x : τ ) ∈ Γ
Arithmetic Operators Γ e 1 : int Γ e 2 : int Γ ` e 1 ⊕ e 2 : int
(⊕ ∈ {+, −, ∗, /, · · · })
Relational Operators Γ e 1 : int Γ e 2 : int Γ ` e 1 ∼ e 2 : bool
(∼ ∈ {<, >, ≤, ≥, =, 6 =, · · · })
Booleans Γ e 1 : bool Γ e 2 : bool Γ e 1 && e 2 : bool Γ e 1 : bool Γ e 2 : bool Γ e 1 || e 2 : bool Γ e 1 : bool Γ! e 1 : bool If Γ e 1 : bool Γ e 2 : τ Γ e 3 : τ Γ if e 1 then e 2 else e 3 : τ Function Abstraction Γ ∪ [x : τ 1 ] e : τ 2 Γ fun x−>e : τ 1 → τ 2 Function Application Γ e 1 : τ 1 → τ 2 Γ e 2 : τ 1 Γ e 1 e 2 : τ 2 Let Γ e 1 : τ Γ ∪ [x : τ ] e 2 : τ ′ Γ let x = e 1 in e 2 : τ ′ Letrec Γ ∪ [x : τ ] e 1 : τ Γ ∪ [x : τ ] e 2 : τ ′ Γ ` let rec x = e 1 in e 2 : τ ′
Assignment 〈a, σ〉 → 〈a′, σ〉 〈X := a, σ〉 → 〈X := a′, σ〉
〈X := n, σ〉 → σ[n/X]
Sequencing 〈c 0 , σ〉 → 〈c′ 0 , σ′〉 〈c 0 ; c 1 , σ〉 → 〈c′ 0 ; c 1 , σ′〉
〈c 0 , σ〉 → σ′ 〈c 0 ; c 1 , σ〉 → 〈c 1 , σ′〉
If 〈b, σ〉 → 〈b′, σ〉 〈if b then c 0 else c 1 , σ〉 → 〈if b′^ then c 0 else c 1 , σ〉
〈if true then c 0 else c 1 , σ〉 → 〈c 0 , σ〉
〈if false then c 0 else c 1 , σ〉 → 〈c 1 , σ〉
While 〈while b do c, σ〉 → 〈if b then (c; while b do c) else skip, σ〉
Natural Semantics, Dynamic Semantics Rules:
Aexp a ::= n | X | a 0 + a 1 | a 0 − a 1 | a 0 × a 1 Bexp b ::= true | false | a 0 = a 1 | a 0 ≤ a 1 | ¬b | b 0 ∧ b 1 | b 0 ∨ b 1 Com c ::= skip | X := a | c 0 ; c 1 | if b then c 0 else c 1 | while b do c
Identifiers 〈X, σ〉 ⇓ σ(X) Constants 〈n, σ〉 ⇓ n 〈true, σ〉 ⇓ true 〈false, σ〉 ⇓ false
Arithmetic Operators 〈a 0 , σ〉 ⇓ n 0 〈a 1 , σ〉 ⇓ n 1 n = n 0 opint n 1 〈a 0 op a 1 , σ〉 ⇓ n
(op ∈ {+, −, ×})
Relational Operators 〈a 0 , σ〉 ⇓ n 0 〈a 1 , σ〉 ⇓ n 1 b = n 0 opint n 1 〈a 0 op a 1 , σ〉 ⇓ b
(op ∈ {=, ≤})
Logical Operators 〈b 0 , σ〉 ⇓ false 〈b 0 ∧ b 1 , σ〉 ⇓ false
〈b 0 , σ〉 ⇓ true 〈b 1 , σ〉 ⇓ b 〈b 0 ∧ b 1 , σ〉 ⇓ b
〈b 0 , σ〉 ⇓ true 〈b 0 ∨ b 1 , σ〉 ⇓ true
〈b 0 , σ〉 ⇓ false 〈b 1 , σ〉 ⇓ b 〈b 0 ∨ b 1 , σ〉 ⇓ b
〈b, σ〉 ⇓ false 〈¬b, σ〉 ⇓ true
〈b, σ〉 ⇓ true 〈¬b, σ〉 ⇓ false
Skip {skip, σ} ⇓ σ