Docsity

CS 421—Summer 2006 Final Study Guide for Programming Languages and Compilers, Papers of Computer Science

Toggle sidebar
bg1
CS 421—Summer 2006 1 CS421 Summer 2006 Final Study Guide
CS421 Summer 2006 Final Study Guide
CS 421 — Programming Languages and Compilers
Summer Term 2006
1. Type Derivations:
(a) Provide a full type derivation for the following term. Feel free to write on the paper
sideways – the trees tend to be wider than they are tall. You can also write out type
environments separately and just reference them in the rules (so you could say Γ1={x:
int}and then just reference Γ1in the derivation) – this is especially useful if you use
the same environment in multiple places. Typing rules are on page 11.
let f =fun a −>(a+9)in (f 4)
University of Illinois at Urbana-Champaign
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS 421—Summer 2006 Final Study Guide for Programming Languages and Compilers and more Papers Computer Science in PDF only on Docsity!

CS421 Summer 2006 Final Study Guide

CS 421 — Programming Languages and Compilers

Summer Term 2006

1. Type Derivations:

(a) Provide a full type derivation for the following term. Feel free to write on the paper

sideways – the trees tend to be wider than they are tall. You can also write out type

environments separately and just reference them in the rules (so you could say Γ 1 = {x :

int} and then just reference Γ 1 in the derivation) – this is especially useful if you use

the same environment in multiple places. Typing rules are on page 11.

let f = fun a −> (a + 9 ) in (f 4)

(b) Provide a full type derivation for the following term. Feel free to write on the paper

sideways – the trees tend to be wider than they are tall. You can also write out type

environments separately and just reference them in the rules (so you could say Γ 1 = {x :

int} and then just reference Γ 1 in the derivation) – this is especially useful if you use

the same environment in multiple places. Typing rules are on page 11.

let rec f = fun a −> (a < 9 ) in (if (f 4) then 3 else 5)

(c) Recall our definition of pairs in the λ calculus – a pair can be represented as a term

λx.x a b where x is the constructor tag (here we only have one constructor, so we only

need one tag) and a and b are the two elements in the pair. Provide a λ abstraction

swappair that takes a pair and returns a new pair with the element swapped: applying

this to pair (a, b) will return pair (b, a).

3. Context-Free Grammars, Derivations, and Parse Trees: The following exercises make

use of this grammar for arithmetic expressions. Here, the start symbol is E, id refers to iden-

tifiers made up of one lowercase character (a, b, · · · , y, z), and num refers to any integer:

E → E + T T → T ∗ F F → id

E → E − T T → T /F F → num

E → T T → F F → (E)

(a) Show a leftmost derivation for the following term:

y/ 3

(b) Show a rightmost derivation for the same term:

(c) Provide a parse tree for the same term:

4. Semantics:

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

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

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

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, σ} ⇓ σ