because propositions
play

Because propositions in such a logic may no longer be freely copied - PowerPoint PPT Presentation

Because propositions in such a logic may no longer be freely copied or ig- standing propositions in sub- structural logics as repre- senting resources rather than truth. nored, this suggests under- Programming with Affine Types CS 51 &


  1. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  2. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  3. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  4. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  5. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  6. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  7. let buyer name limit ch = send (Request name) ch; let rec loop () = match recv ch with Reject None Offer price if price ≤ limit then send Accept ch; let Resource r = recv ch in Some r else send (Offer limit) ch; loop () in loop () type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let Request name = recv ch in match lookup name with | None → send Reject ch | Some (r, price) → send (Offer price) ch; match recv ch with | Accept → send (Resource r) ch | Reject → () 6

  8. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  9. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  10. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  11. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  12. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  13. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  14. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; Pattern match failure loop () in loop () 6

  15. type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  16. (Fähndrich et al. 2006) It’s a language feature in Sing# Solution: Session types Problem: Simple channel is too permissive type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  17. (Fähndrich et al. 2006) It’s a language feature in Sing# Solution: Session types Problem: Simple channel is too permissive type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  18. (Fähndrich et al. 2006) It’s a language feature in Sing# Solution: Session types Problem: Simple channel is too permissive type bid_msg = Request of string | Offer of int | Reject | Accept | Resource of resource type bid_chan = bid_msg chan let seller ch = let buyer name limit ch = let Request name = recv ch send (Request name) ch; in match lookup name with let rec loop () = | None → match recv ch with send Reject ch | Reject → None | Some (r, price) → | Offer price → send (Offer price) ch; if price ≤ limit then match recv ch with send Accept ch; | Accept → let Resource r = recv ch send (Resource r) ch in Some r | Reject → () else send (Offer limit) ch; loop () in loop () 6

  19. What’s It Gonna Be? Problem: Locking Message passing Both Solution: Static permissions Session types ? in Chalice in Sing# 7

  20. What’s It Gonna Be? Problem: Locking Message passing Both Solution: Static permissions Session types ? in Chalice in Sing# 7

  21. q u e c a i p n a l p u a n e o r b i m t i c i l a s i t s r i i f e o n s s s e s s i o n t y p e s Honda 1998, Vasconcelos 2004 Capability Calculus Use types F URAL ILL LNL Special 2003 Boyland General Purpose Theoretical Practical Chalice Moose Sing#, Purpose p e r m i s s i o n s s e s s i o n t y p e s 8

  22. q u e c a i p n a l p u a n e o r b i m t i c i l a s i t s r i i f e o n s s s e s s i o n t y p e s Boyland Capability 2003 Use types F URAL Honda 1998, Vasconcelos 2004 ILL LNL Calculus Special Purpose Moose Chalice Plaid Sing#, Cyclone Theoretical Practical Purpose General Vault, e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e 8

  23. Special F Honda 1998, Vault, Plaid LNL ILL URAL Purpose Chalice types Use Calculus Capability Boyland 2003 2004 Sing#, Moose Cyclone Practical General Purpose Theoretical Vasconcelos e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i c i l a s i t s r i i f e o n s s s e s s i o n t y p e s 8

  24. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  25. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  26. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  27. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  28. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  29. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  30. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  31. Special types Honda 1998, Moose Vault, Plaid LNL ILL Use Cyclone Purpose Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  32. Special ILL Honda 1998, Moose Vault, Plaid ? LNL types Cyclone Purpose Use Calculus Capability Boyland 2003 2004 Chalice Sing#, Vasconcelos General Practical Theoretical Purpose e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  33. Special ILL Moose Vault, Plaid Alms ? LNL types Chalice Purpose Use Calculus Capability Boyland 2003 2004 Honda 1998, Sing#, Cyclone Practical General Purpose Theoretical Vasconcelos e m o m r y p e r r m e i g s i s o i o n n s s s e s s i o t n y p t e y s p t a e t s e q u e c a i p n a l p u a n e o r b i m t i F ◦ c i l a s i t s r i i f e o n s s λ URAL s e s s i o n t y p e s 8

  34. Goal A practical and expressive programming language with general-purpose affine types 9

  35. Goal A practical and expressive programming language nlimited with general-purpose affine types A ffine U 9

  36. Goal A practical and expressive programming language nlimited with general-purpose affine types A ≤ 1 U 9

  37. Goal A practical and expressive programming language with general-purpose affine types A ≤ 1 U 9

  38. Goal A practical and expressive programming language with general-purpose affine types A U 9

  39. Goal A practical and expressive programming language with general-purpose affine types A U 9

  40. Goal think ML pay-as-you-go A practical and expressive programming language with general-purpose affine types A U 9

  41. Goal think ML pay-as-you-go stateful type systems as libraries A practical and expressive programming language with general-purpose affine types A U 9

  42. Goal think ML pay-as-you-go stateful type systems as libraries Alms A practical and expressive programming language with general-purpose affine types A U 9

  43. Goal think ML pay-as-you-go stateful type systems as libraries Alms language design core model prototype implementation A practical and expressive programming language with general-purpose affine types A U 9

  44. Goal with general-purpose affine types think ML pay-as-you-go stateful type systems as libraries Alms language design core model prototype implementation A practical and expressive programming language A U 9

  45. Alms by Example or Your Language Is a Library in My Language

  46. Alms by Example or Your Language Is a Library in My Language

  47. Alms vs. OCaml Algebraic data types No Yes Functors, classes, … Yes Yes Exceptions Yes Yes Concurrency Yes Yes Pattern matching Yes Yes Yes OCaml Yes Alms Affine types No Yes First-class polymorphism Awkward Type inference Yes Yes Yes Modules Yes Yes Opaque signatures 11

  48. #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) Error: This expression has type char but an expression was expected of type int #- (nth 1 [1,3,5], nth 1 [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) ML Polymorphism val nth : int → ’a list → ’a option 12

  49. #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) Error: This expression has type char but an expression was expected of type int #- (nth 1 [1,3,5], nth 1 [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) ML Polymorphism val nth : int → ’a list → ’a option #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) 12

  50. Error: This expression has type char but an expression was expected of type int #- (nth 1 [1,3,5], nth 1 [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) ML Polymorphism val nth : int → ’a list → ’a option #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) 12

  51. #- (nth 1 [1,3,5], nth 1 [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) ML Polymorphism val nth : int → ’a list → ’a option #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) Error: This expression has type char but an expression was expected of type int 12

  52. ML Polymorphism val nth : int → ’a list → ’a option #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) Error: This expression has type char but an expression was expected of type int #- (nth 1 [1,3,5], nth 1 [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) 12

  53. ML Polymorphism val nth : ∀ ∀ ’a. int → ’a list → ’a option ∀ #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) Error: This expression has type char but an expression was expected of type int #- (nth 1 [1,3,5], nth 1 [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) 12

  54. First-Class Polymorphism val nth : int → ∀ ∀ ’a. ’a list → ’a option ∀ #- let second = nth 1 in (second [1,3,5], second [2,4,6]) it : int option × int option = (Some 3, Some 4) #- let second = nth 1 in (second [1,3,5], second [’a’,’b’,’c’]) it : int option × char option = (Some 3, Some ’b’) 13

  55. #- let bee = (’b’, string_of_char) : ’a. ’a × (’a string) bee : ’a. ’a × (’a string) #- let both = [one, bee] both : ( ’a. ’a × (’a string)) list #- let show ((x, f) : ’a. ’a × (’a string)) = f x show : ( ’a. ’a × (’a string)) string #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = bee in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ’a. ’a × (’a → string) ∃ one : ∃ ∃ ’a. ’a × (’a → string) ∃ 14

  56. #- let both = [one, bee] both : ( ’a. ’a × (’a string)) list #- let show ((x, f) : ’a. ’a × (’a string)) = f x show : ( ’a. ’a × (’a string)) string #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = bee in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ’a. ’a × (’a → string) ∃ one : ∃ ∃ ’a. ’a × (’a → string) ∃ #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ’a. ’a × (’a → string) ∃ 14

  57. #- let show ((x, f) : ’a. ’a × (’a string)) = f x show : ( ’a. ’a × (’a string)) string #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = bee in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ’a. ’a × (’a → string) ∃ one : ∃ ∃ ’a. ’a × (’a → string) ∃ #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ’a. ’a × (’a → string) ∃ #- let both = [one, bee] both : ( ∃ ∃ ’a. ’a × (’a → string)) list ∃ 14

  58. #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = bee in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ’a. ’a × (’a → string) ∃ one : ∃ ∃ ’a. ’a × (’a → string) ∃ #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let both = [one, bee] both : ( ∃ ∃ ’a. ’a × (’a → string)) list ∃ #- let show ((x, f) : ∃ ∃ ’a. ’a × (’a → string)) = f x ∃ show : ( ∃ ∃ ’a. ’a × (’a → string)) → string ∃ 14

  59. #- let (x, f) = one in let (y, g) = bee in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ’a. ’a × (’a → string) ∃ one : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let both = [one, bee] both : ( ∃ ∃ ’a. ’a × (’a → string)) list ∃ #- let show ((x, f) : ∃ ∃ ∃ ’a. ’a × (’a → string)) = f x show : ( ∃ ∃ ’a. ’a × (’a → string)) → string ∃ #- map show both it : string list = [”1”, ”b”] 14

  60. Error: This expression has type ’_a2 but an expression was expected of type ’_a6 Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ∃ ’a. ’a × (’a → string) one : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let both = [one, bee] both : ( ∃ ∃ ’a. ’a × (’a → string)) list ∃ #- let show ((x, f) : ∃ ∃ ∃ ’a. ’a × (’a → string)) = f x show : ( ∃ ∃ ’a. ’a × (’a → string)) → string ∃ #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = bee in f y 14

  61. Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ∃ ’a. ’a × (’a → string) one : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let both = [one, bee] both : ( ∃ ∃ ’a. ’a × (’a → string)) list ∃ #- let show ((x, f) : ∃ ∃ ∃ ’a. ’a × (’a → string)) = f x show : ( ∃ ∃ ’a. ’a × (’a → string)) → string ∃ #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = bee in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 14

  62. Existential Quantification #- let one = (1, string_of_int) : ∃ ∃ ∃ ’a. ’a × (’a → string) one : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let bee = (’b’, string_of_char) : ∃ ∃ ’a. ’a × (’a → string) ∃ bee : ∃ ∃ ∃ ’a. ’a × (’a → string) #- let both = [one, bee] both : ( ∃ ∃ ’a. ’a × (’a → string)) list ∃ #- let show ((x, f) : ∃ ∃ ∃ ’a. ’a × (’a → string)) = f x show : ( ∃ ∃ ’a. ’a × (’a → string)) → string ∃ #- map show both it : string list = [”1”, ”b”] #- let (x, f) = one in let (y, g) = one in f y Error: This expression has type ’_a2 but an expression was expected of type ’_a6 14

  63. Example: Mutual Exclusion let deposit (arr: int array) (acct: int) (amt: int) = Array.set arr acct (Array.get arr acct + amt) 15

  64. : U module type AF_ARRAY = sig type ’a array : A val new : int ’a ’a array val set : ’a array int ’a ’a array val get : ’a array int ’a × ’a array end Unlimited Arrays to Affine Arrays module Array : sig type ’a array val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end 16

  65. module type AF_ARRAY = sig type ’a array : A val new : int ’a ’a array val set : ’a array int ’a ’a array val get : ’a array int ’a × ’a array end Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end 16

  66. Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end 16

  67. Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end 16

  68. Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end 16

  69. Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end 16

  70. type ’a array = ’a Array.array (* U A *) let new = Array.new let set arr ix v = Array.set arr ix v; arr let get arr ix = (Array.get arr ix, arr) Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end module AfArray : AF_ARRAY = struct end 16

  71. let new = Array.new let set arr ix v = Array.set arr ix v; arr let get arr ix = (Array.get arr ix, arr) Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end module AfArray : AF_ARRAY = struct type ’a array = ’a Array.array (* U ⊑ A *) end 16

  72. (* U A *) let set arr ix v = Array.set arr ix v; arr let get arr ix = (Array.get arr ix, arr) Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end module AfArray : AF_ARRAY = struct type ’a array = ’a Array.array let new = Array.new end 16

  73. (* U A *) let get arr ix = (Array.get arr ix, arr) Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end module AfArray : AF_ARRAY = struct type ’a array = ’a Array.array let new = Array.new let set arr ix v = Array.set arr ix v; arr end 16

  74. (* U A *) Unlimited Arrays to Affine Arrays module Array : sig type ’a array : U val new : int → ’a → ’a array val set : ’a array → int → ’a → unit val get : ’a array → int → ’a end module type AF_ARRAY = sig type ’a array : A val new : int → ’a → ’a array val set : ’a array → int → ’a → ’a array val get : ’a array → int → ’a × ’a array end module AfArray : AF_ARRAY = struct type ’a array = ’a Array.array let new = Array.new let set arr ix v = Array.set arr ix v; arr let get arr ix = (Array.get arr ix, arr) end 16

  75. Using Affine Arrays let deposit arr acct amt = let (balance, arr) = AfArray.get arr acct in AfArray.set arr acct (balance + amt) 17

  76. Using Affine Arrays let deposit arr acct amt = let (balance, arr) = AfArray.get arr acct in AfArray.set arr acct (balance + amt) 17

  77. Using Affine Arrays let deposit arr acct amt = let (balance, arr) = AfArray.get arr acct in r := arr; AfArray.set arr acct (balance + amt) 17

Recommend


More recommend