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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
(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
(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
(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
What’s It Gonna Be? Problem: Locking Message passing Both Solution: Static permissions Session types ? in Chalice in Sing# 7
What’s It Gonna Be? Problem: Locking Message passing Both Solution: Static permissions Session types ? in Chalice in Sing# 7
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
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
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
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
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
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
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
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
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
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
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
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
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
Goal A practical and expressive programming language with general-purpose affine types 9
Goal A practical and expressive programming language nlimited with general-purpose affine types A ffine U 9
Goal A practical and expressive programming language nlimited with general-purpose affine types A ≤ 1 U 9
Goal A practical and expressive programming language with general-purpose affine types A ≤ 1 U 9
Goal A practical and expressive programming language with general-purpose affine types A U 9
Goal A practical and expressive programming language with general-purpose affine types A U 9
Goal think ML pay-as-you-go A practical and expressive programming language with general-purpose affine types A U 9
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
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
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
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
Alms by Example or Your Language Is a Library in My Language
Alms by Example or Your Language Is a Library in My Language
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
#- 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
#- 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
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
#- (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
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
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
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
#- 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
#- 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
#- 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
#- 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
#- 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
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
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
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
Example: Mutual Exclusion let deposit (arr: int array) (acct: int) (amt: int) = Array.set arr acct (Array.get arr acct + amt) 15
: 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
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
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
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
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
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
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
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
(* 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
(* 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
(* 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
Using Affine Arrays let deposit arr acct amt = let (balance, arr) = AfArray.get arr acct in AfArray.set arr acct (balance + amt) 17
Using Affine Arrays let deposit arr acct amt = let (balance, arr) = AfArray.get arr acct in AfArray.set arr acct (balance + amt) 17
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