1 / 27

Organization of Programming Languages ( Final Review )

Organization of Programming Languages ( Final Review ). Hongwei Xi University of Cincinnati. Type Inference. It can be burdensome to write types Type inference infers missing types from program context

tmalbon
Download Presentation

Organization of Programming Languages ( Final Review )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Organization of Programming Languages (Final Review) Hongwei Xi University of Cincinnati Final Review

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

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

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

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

  6. Soundness and Completeness • The bidirectional algorithm is sound • The bidirectional algorithm is complete Final Review

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

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

  9. Stack Machines • Run-time Stacks sigmaV ::= . | V :: sigmaV • Expression Stacks sigmae ::= . | e :: sigmae • Value stacks sigmav ::= . | v :: sigmav Final Review

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

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

  12. Compilation ------------ ----------- ------------b | [load b] i | [load i] x | [load x] e1 | Is1 … en | Isn-----------------------------------------Op(e1,…,en) | Is1 @ … @ Isn @ [Op] Final Review

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

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

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

  16. Decompilation ------------ ([e]; .) | e (b :: sigmae; Is) | e ----------------------- (sigmae; load b :: Is) Final Review

  17. Decompilation (i :: sigmae; Is) | e ----------------------- (sigmae; load i :: Is) (x :: sigmae; Is) | e ----------------------- (sigmae; load x :: Is) Final Review

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

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

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

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

  22. Data Flow • Substitution is a complex and expressive operation in practice. • We introduce the notion of closure to avoid explicitly performing substitution. Final Review

  23. Closures • Closures C ::= b | I | <> | <C, C> | (fun f(x) is Is, Theta) • Closure substitution Theta ::= [] | Theta [x -> C] Final Review

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

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

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

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

More Related