180 likes | 192 Views
Principles of Programming Languages (Final Review). Hongwei Xi Comp. Sci. Dept. Boston University. Untyped Lambda-Calculus. Terms t ::= x | l x.t | t 1 (t 2 ) Values v ::= x | l x.t
E N D
Principles of Programming Languages(Final Review) Hongwei Xi Comp. Sci. Dept. Boston University BU CAS CS520
Untyped Lambda-Calculus • Terms t ::= x | lx.t | t1(t2) • Values v ::= x | lx.t • Evaluation rules: t1-> t1’ t2-> t2’ ----------------- ------------------ t1(t2) -> t1’(t2) v1(t2) -> v1(t2’) -------------------------- (lx.t)(v) -> [x -> v] t BU CAS CS520BU CAS CS520
Nameless Representation • Terms t ::= One | Shift (t) | Lam (t) | App(t1, t2) • For instance,the term lx.ly.y(x) is represented as: Lam(Lam(App(One, Shift(One)))) BU CAS CS520BU CAS CS520
Simply Typed Lambda-Calculus • We use B for base types. • Types T ::= B | T1-> T2 • Terms t ::= x | lx:T.t | t1(t2) • Contexts G ::= | G, x:T • Static semantics: typing rules • Dynamic semantics: evaluation rules BU CAS CS520BU CAS CS520
Some important properties of STLC • Canonical forms • Substitution lemma: if G, x:S |- t1:T and G |- t2:S, then G |- [x -> t2] t1:T • Subject reduction: if G |- t:T and t -> t’, then G |- t’:T • Progress if |- t:T, then either t is a value or t -> t’ for some term t’ BU CAS CS520BU CAS CS520
Simple Extensions to STLC • Tuples: terms t ::= … | {t1, t2} | t.1 | t.2 types T ::= T1* T2 • Local binding: terms t ::= … | let x = t1 in t2 • Sums: terms t ::= … | inl(t) | inr(t) | (case t of inl(x) => t1 | inr(x) => t2) types T ::= T1 + T2 • Recursion: terms t ::= … | fix (t) • … … BU CAS CS520BU CAS CS520
Normalization of STLC • Reducibility predicates RT for types T • T is a base type: RT(t) if t terminates. • T = T1 * T2: RT(t) if t terminates and RT1(v1) and RT2(v2) whenever t ->* {v1, v2}. • T = T1->T2: RT(t) if t terminates and RT2([x->v1] t0]) whenever t ->* lx:T1.t0 and RT1(v1). BU CAS CS520BU CAS CS520
References • Terms t ::= … | ref (t) | !t | t1 := t2 | l • Values v ::= … | l • Types T ::= … | Ref (T) • Stores m ::= [] | m[l -> v] • Store typings S ::= [] | [l -> T] BU CAS CS520BU CAS CS520
Exceptions • Terms t ::= raise (t) | try t1 with t2 • Answers ans ::= v | raise v • Evaluation rules:------------------------------- ----------------------------- (raise v) (t) -> raise v v(raise v’) -> raise v’… … • The typing rule for ‘raise’:G |- t: Exn -------------------G |- raise t: T BU CAS CS520BU CAS CS520
Recursive Types • Types t ::= … | X | mX. T • Typing rules: fold/unfold BU CAS CS520BU CAS CS520
Universal Types • System F / The 2nd order polymorphically typed lambda-calculus (l2) • Terms t ::= …| LX.t | t[T] • Types T ::= … | X | X.T • Value restriction: only values can occur under L. BU CAS CS520BU CAS CS520
Existential Types • Terms t ::= {*T, t} | open t1 as {*X,x} in t2 • Types T ::= … | X.T • Encoding existential types via universal types BU CAS CS520BU CAS CS520
Type Reconstruction BU CAS CS520BU CAS CS520
Subtyping • Coercion: a subtyping proof of T1 <= T2 can be translated into a function of type T1 -> T2. • Coherence: there may be different subtyping proofs of T1 <= T2 for some T1 and T2. BU CAS CS520BU CAS CS520
Evaluation Contexts • Evaluation contexts: E ::= [] | E(t) | v(E) | if E then t1 else t2 | {E, t} | {v, E} | E.1 | E.2 | let x = E in t | … • Redexes and their reductions: • (lx.t)(v) -> [x->v] t • {v1, v2}.1 -> v1 • {v1, v2}.2 -> v2 • … … BU CAS CS520BU CAS CS520
Callcc and Throw • Terms t ::= … | *E | callcc (t) | throw (t1, t2) • Values v ::= … | *E • Evaluation contexts E ::= … | callcc(E) | throw (E, t) • E[callcc(lk.t)] -> E[[k -> *E]t] • E[throw(*E’, t)] -> E’[t] BU CAS CS520BU CAS CS520
Imperative Objects • Subtyping • Inheritance • Open recursion BU CAS CS520BU CAS CS520
The End Questions? BU CAS CS520BU CAS CS520