1 / 74

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421). Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421. Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha. Simple Example. Let  = [ x:int ; y:bool] Show  |- y || (x + 3 > 6) : bool

casea
Télécharger la présentation

Programming Languages and Compilers (CS 421)

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. Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

  2. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Start building the proof tree from the bottom up ?  |- y || (x + 3 > 6) : bool

  3. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- y || (x + 3 > 6) : bool

  4. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Boolean Connectives: ||  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  5. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Pick an assumption to prove ?  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  6. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  7. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Axiom for variables  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  8. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Pick an assumption to prove ?  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  9. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  10. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Arithmetic relations  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  11. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Pick an assumption to prove ?  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  12. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  13. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Axiom for constants  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  14. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Pick an assumption to prove ?  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  15. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  16. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Arithmetic operations  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  17. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Pick an assumption to prove ?  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  18. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  19. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Axiom for constants  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  20. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Pick an assumption to prove ?  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  21. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Which rule has this as a conclusion? ?  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  22. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • Axiom for variables  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  23. Simple Example • Let  = [ x:int ; y:bool] • Show  |- y || (x + 3 > 6) : bool • No more assumptions! DONE!  |- x : int  |- 3 : int  |- x + 3 : int  |- 6 : int  |- y : bool  |- x + 3 > 6 : bool  |- y || (x + 3 > 6) : bool

  24. Type Variables in Rules • If_then_else rule:  |- e1 : bool  |- e2 :   |- e3 :   |- (if e1 then e2 elsee3):  •  is a type variable (meta-variable) • Can take any type at all • All instances in a rule application must get same type • Then branch, else branch and if_then_else must all have same type

  25. Function Application • Application rule:  |- e1 : 1  2 |- e2 : 1  |- (e1e2): 2 • If you have a function expression e1 of type 1  2 applied to an argument of type 1, the resulting expression has type 2

  26. Application Examples  |- print_int : int  unit  |- 5 : int  |- (print_int 5) : unit • e1 = print_int, e2 = 5, • 1 = int, 2 = unit  |- map print_int : int list  unit list  |- [3;7] : int list  |- (map print_int [3; 7]) : unit list • e1 = map print_int, e2 = [3; 7], • 1 = int list, 2 = unit list

  27. Fun Rule • Rules describe types, but also how the environment  may change • Can only do what rule allows! • fun rule: [x : 1 ] +  |- e: 2  |- fun x -> e: 1  2

  28. Fun Examples [y : int ] +  |- y + 3: int  |- fun y -> y + 3: int  int [f : int  bool] +  |- f 2 :: [true]: bool list  |- (fun f -> f 2 :: [true]) : (int  bool)  bool list

  29. (Monomorphic) Let and Let Rec • let rule:  |- e1: 1 [x : 1 ] +  |- e2 : 2  |- (let x = e1 in e2 ): 2 • let rec rule: [x: 1 ] + |- e1:1 [x: 1 ] + |- e2:2  |- (let rec x = e1 in e2 ): 2

  30. Example (Assume Prim Op rule for ::) • Which rule do we apply? ? |- (let rec one = 1 :: one in let x = 2 in fun y -> (x :: y :: one) ) : int  int list

  31. Example • Let rec rule: 2 [one : int list] |- 1 (let x = 2 in [one : int list] |- fun y -> (x :: y :: one)) (1 :: one) : int list : int  int list |- (let rec one = 1 :: one in let x = 2 in fun y -> (x :: y :: one) ) : int  int list

  32. Proof of 1 • Which rule? [one : int list] |- (1 :: one) : int list

  33. Proof of 1 • Prim Op rule for :: (at int) 3 4 [one : int list] |- [one : int list] |- 1: intone : int list [one : int list] |- (1 :: one) : int list

  34. Proof of 3 • Constant Rule 3 4 [one : int list] |- [one : int list] |- 1: intone : int list [one : int list] |- (1 :: one) : int list

  35. Proof of 4 • Rule for variables [one : int list] |- one:int list

  36. Proof of 2 5 [x:int; one : int list] |- • Constant fun y -> (x :: y :: one)) [one : int list] |- 2:int : int  int list [one : int list] |- (let x = 2 in fun y -> (x :: y :: one)) : int  int list

  37. Proof of 5 Use function rule ? [x:int; one : int list] |- fun y -> (x :: y :: one)) : int  int list

  38. Proof of 5 Use Prim Op rule for :: ? [y:int; x:int; one : int list] |- (x :: y :: one) : int list [x:int; one : int list] |- fun y -> (x :: y :: one)) : int  int list

  39. Proof of 5 Use Prim Op rule for :: 6 7 [y:int; x:int; one:int list]|- [y:int; x:int; one:int list] |- x:int (y :: one) : int list [y:int; x:int; one : int list] |- (x :: y :: one) : int list [x:int; one : int list] |- fun y -> (x :: y :: one)) : int  int list

  40. Proof of 6 6 Variable Rule 7 [y:int; x:int; one:int list]|- [y:int; x:int; one:int list] |- x:int (y :: one) : int list [y:int; x:int; one : int list] |- (x :: y :: one) : int list [x:int; one : int list] |- fun y -> (x :: y :: one)) : int  int list

  41. Proof of 7 Prim Op Rule :: 8 9 [y:int; x:int; one:int list]|- [y:int; x:int; one:int list] |- y:int one : int list [y:int; x:int; one : int list] |- (y :: one)): int list

  42. Proof of 8 and 9 Variable Rule 8 9 [y:int; x:int; one:int list]|- [y:int; x:int; one:int list] |- y:int (y :: one) : int list [y:int; x:int; one : int list] |- (y :: one)): int list

  43. Curry - Howard Isomorphism • Type Systems are logics; logics are type systems • Types are propositions; propositions are types • Terms are proofs; proofs are terms • Functions space arrow corresponds to implication; application corresponds to modus ponens

  44. Curry - Howard Isomorphism • Modus Ponens A  B A B • Application  |- e1 :     |- e2 :   |- (e1e2): 

  45. Support for Polymorphic Types • Monomorpic Types (): • Basic Types: int, bool, float, string, unit, … • Type Variables: , , g d e • Compound Types:   , int * string, bool list, … • Polymorphic Types: • Monomorphic types  • Universally quantified monomorphic types • 1, … , n .  • Can think of  as same as .  A A

  46. Support for Polymorphic Types • Typing Environment  supplies polymorphic types (which will often just be monomorphic) for variables • Free variables of monomorphic type just type variables that occur in it • Write FreeVars() • Free variables of polymorphic type removes variables that are universally quantified • FreeVars( 1, … , n . ) = FreeVars() – {1, … , n } • FreeVars() = all FreeVars of types in range of  A

  47. Monomorphic to Polymorphic • Given: • type environment  • monomorphic type  •  shares type variables with  • Want most polymorphic type for  that doesn’t break sharing type variables with  • Gen(, ) = 1, … , n .  where {1, … , n} = freeVars() – freeVars() A

  48. Polymorphic Typing Rules • A type judgement has the form  |- exp :  •  uses polymorphic types •  still monomorphic • Most rules stay same (except use more general typing environments) • Rules that change: • Variables • Let and Let Rec • Allow polymorphic constants • Worth noting functions again

  49. Polymorphic Let and Let Rec • let rule:  |- e1: 1 [x : Gen(1,)] +  |- e2 : 2  |- (let x = e1 in e2 ): 2 • let rec rule: [x: 1] + |- e1:1 [x:Gen(1,)] + |- e2:2  |- (let rec x = e1 in e2 ): 2

  50. Polymorphic Variables (Identifiers) Variable axiom:  |- x : j() if (x) = 1, … , n .  • Where j replaces all occurrences of 1, … , n by monotypes 1, … , n • Note: Monomorphic rule special case:  |- x :  if (x) =  • Constants treated same way A

More Related