270 likes | 282 Views
This final review covers type inference, bidirectional algorithms, control flow, stack machines, compilation, decompilation, and theorems. Learn about key concepts and transition rules in programming languages.
E N D
Organization of Programming Languages (Final Review) Hongwei Xi University of Cincinnati Final Review
Type Inference • It can be burdensome to write types • Type inference infers missing types from program context • For TFPL, type inference is extremly successful as virtually all missing types can be inferred Final Review
External Language for TFPL • Expressions e ::=x | f | b | I | op(e1,…,en) |if e then e1 else e2 fi |<> | <e1, e2> | fst(e) | snd(e) |fun f(x) is e |fun f(x:tau1):tau2 is e |app(e1, e2) | let x=e1 in e2 end |(e: tau) Final Review
Bidirectional Algorithm Existential Type Variables X Types T ::= X | bool | int | 1 | T * T | T -> T Contexts G ::= . | G, x:T Type Constraints Phi ::=T | T1=T2 | Phi1 and Phi2 Substitution Theta ::= [] | Theta[X->tau] Final Review
Bidirectional Algorithm • We have two kinds of judgments • G |- e | T => Phi: Given a context Gand an expression e, we can generate T and Phi such that e can be assigned type T if Phi is solvable • G |- e || T => Phi: Given a context G, an expression e and a type T, we can generate Phi such that e can be assigned type T if Phi is solvable Final Review
Soundness and Completeness • The bidirectional algorithm is sound • The bidirectional algorithm is complete Final Review
Control Flow • Direct implementation of the reduction semantics for UFPL contains a great deal of overhead • We would like to make control flow in a program explicit so that there is no need for locating the redex that needs to be reduced immediately Final Review
Stack Machines • Run-time Values V ::=b | I | <> | <V, V> | fun f(x) is Is • Instructions I ::= Load V | op | if(Is, Is) | tup | fst | snd | app | let (x, Is) • Instruction Lists Is ::= . | I :: Is • Run-time Substitutions Theta ::= [] | Theta[x->V] Final Review
Stack Machines • Run-time Stacks sigmaV ::= . | V :: sigmaV • Expression Stacks sigmae ::= . | e :: sigmae • Value stacks sigmav ::= . | v :: sigmav Final Review
Transition Rules (sigmaV; load V :: Is) => (V :: sigmaV, Is) (V1::…::Vn::sigmaV, Op :: Is) => (V :: sigmaV, Is) (true::sigmaV, if(Is1, Is2) :: Is) => (sigmaV, Is1@Is) (false::sigmaV, if(Is1, Is2) :: Is) => (sigmaV, Is2@Is) (V2::V1::sigmaV, tup :: Is) => (<V1,V2>::sigmaV, Is) (<V1,V2>::sigmaV, fst :: Is) => (V1::sigmaV, Is) (<V1,V2>::sigmaV, snd :: Is) => (V2::sigmaV, Is) Final Review
Transition Rules (V::fun f(x) is Is0 :: sigmav; app :: Is) =>(sigmav; Is0[f-> fun f(x) is Is0, x->V]@Is) (V:: sigmav; let(x, Is0) :: Is) =>(sigmav; Is0[x->V] :: Is) Final Review
Compilation ------------ ----------- ------------b | [load b] i | [load i] x | [load x] e1 | Is1 … en | Isn-----------------------------------------Op(e1,…,en) | Is1 @ … @ Isn @ [Op] Final Review
Compilation e | Is e1 | Is1 e2 | Is2-----------------------------------------if e then e1 else e2 fi | Is @ [if(Is1, Is2)] e1 | Is1 e2 | Is2 ------------------------------------- <e1, e2> | Is1 @ Is2 @ [tup] Final Review
Compilation e | Is e | Is------------------- --------------------fst(e) | Is@[fst] snd(e) | Is@[snd] e | Is----------------------------------------- fun f(x) is e | [load (fun f(x) is Is)] Final Review
Compilation e1 | Is1 e2 | Is2 ----------------------------------- app(e1, e2) | Is1 @ Is2 @ [app] e1 | Is1 e2 | Is2-----------------------------------------let x = e1 in e2 end | Is1 @ [let(x,Is2)] Final Review
Decompilation ------------ ([e]; .) | e (b :: sigmae; Is) | e ----------------------- (sigmae; load b :: Is) Final Review
Decompilation (i :: sigmae; Is) | e ----------------------- (sigmae; load i :: Is) (x :: sigmae; Is) | e ----------------------- (sigmae; load x :: Is) Final Review
Decompilation (op(e1,…,en) :: sigmae; Is) | e ----------------------------------- (en::…::e1::sigmae; op::Is) | e (. ; Is1) | e1 (. ; Is2) | e2 (if e0 then e1 else e2 :: sigmae; Is) | e------------------------------------------- (e0 :: sigmae; if(Is1, Is2) :: Is) | e Final Review
Decompilation (<e1, e2> :: sigmae; Is) | e-------------------------------------- (e2 :: e1 :: sigmae; tup :: Is) | e (fst(e0)::sigmae; Is) | e (snd(e0)::sigmae; Is) | e --------------------------- --------------------------- (e0::sigmae; fst::Is) (e0::sigmae; snd::Is) Final Review
Decompilation (. ; Is0) | e0 ((fun f(x) is e0) :: sigmae; Is) | e------------------------------------------------------- (sigmae; load (fun f(x) is Is0) :: Is) | e (app(e1, e2) :: sigmae; Is) | e --------------------------------- (e2 :: e1 :: sigmae; app :: Is) | e (. ; Is0) | e2 (let x=e1 in e2 end :: sigmae; Is) | e-------------------------------------------------------- (e1 :: sigmae; let (x, Is0) :: Is) | e Final Review
Theorems • If e | Is is derivable then (. ; Is) | e is also derivable. • Assume e | Is is derivable. Then e ->* v holds if and only if (. ; Is) =>* ([V], .) holds for some V satisfying v(V) = V, that is, v | load V is derivable. Final Review
Data Flow • Substitution is a complex and expressive operation in practice. • We introduce the notion of closure to avoid explicitly performing substitution. Final Review
Closures • Closures C ::= b | I | <> | <C, C> | (fun f(x) is Is, Theta) • Closure substitution Theta ::= [] | Theta [x -> C] Final Review
From Closures to Run-time Values || b || = b || i || = i ||<>|| = <> ||<C1, C2>|| = <||C1||, ||C2||> ||(fun f(x) is Is, Theta)|| =fun f(x) is Is[||Theta||[f->f][x->x]] ||[]|| = [] ||Theta[x->C]|| = ||Theta||[x->||C||] Final Review
Mutual Recursion • We can define n functions mutual recursively:fun f1(x1) is e1 and … and fun fn(xn) is en • We can then choose the ith defined function:(fun f1(x1) is e1 and … and fun fn(xn) is en).i Final Review
New Redex • An expression of the following form is a redex:app((fun f1(x1) is e1 and … and fun fn(xn) is en).i,v) • Its reduction isei[f1->e.1,…,fn->e.n,xi->v],where e is fun f1(x1) is e1 and … and fun fn(xn) is en Final Review
Typing Rules • Types tau ::= … | (tau1,…,taun) • Gamma’, x1:tau1 |- e1:tau’1 … … Gamma’,xn:taun |- en:tau’n---------------------------------------------------------------------------------------------------------------------------(type-funs)Gamma |- fun f1(x1) is e1 and … and fun fn(xn) is en: (tau1->tau’1,…,taun->tau’n)where we haveGamma’ = Gamma, f1:tau1->tau’1,…,fn:taun->tau’n • Gamma | e: (tau1,…,taun) 1 <= i <= n------------------------------------------------(type-choose)Gamma |- e.i : taui Final Review