parametricity
play

Parametricity Leo White Jane Street February 2016 1/ 59 - PowerPoint PPT Presentation

Parametricity Leo White Jane Street February 2016 1/ 59 Parametricity with multiple types. uniformly . from the outside world, parametricity hides details about the outside world from an implementation. 2/ 59 Polymorphism allows a


  1. Parametricity Leo White Jane Street February 2016 1/ 59

  2. Parametricity with multiple types. uniformly . from the outside world, parametricity hides details about the outside world from an implementation. 2/ 59 ▶ Polymorphism allows a single piece of code to be instantiated ▶ Polymorphism is parametric when all of the instances behave ▶ Where abstraction hides details about an implementation

  3. Parametricity in OCaml 3/ 59

  4. Universal types in OCaml 4/ 59

  5. Universal types in OCaml (* ∀𝛽.𝛽 → 𝛽 *) let f x = x 5/ 59

  6. Universal types in OCaml (* (∀𝛽. List 𝛽 → 𝐽𝑜𝑢) → 𝐽𝑜𝑢 *) let g h = h [1; 2; 3] + h [1.0; 2.0; 3.0] Characters 27-30: let g h = h [1; 2; 3] + h [1.0; 2.0; 3.0] ^^^ Error: This expression has type float but an expression was expected of type int 6/ 59

  7. Universal types in OCaml Λ 𝛽 ::*. 𝜇 f: 𝛽 → Int. 𝜇 x: 𝛽 . 𝜇 y: 𝛽 . plus (f x) (f y) Λ 𝛽 ::*. Λ𝛾 ::*. 𝜇 f: ∀𝛿 . 𝛿 → Int. 𝜇 x: 𝛽 . 𝜇 y: 𝛾 . plus (f [ 𝛽 ] x) (f [ 𝛾 ] y) 7/ 59

  8. Universal types in OCaml Λ 𝛽 ::*. 𝜇 f: 𝛽 → Int. 𝜇 x: 𝛽 . 𝜇 y: 𝛽 . plus (f x) (f y) Λ 𝛽 ::*. Λ𝛾 ::*. 𝜇 f: ∀𝛿 . 𝛿 → Int. 𝜇 x: 𝛽 . 𝜇 y: 𝛾 . 7/ 59 plus (f [ 𝛽 ] x) (f [ 𝛾 ] y)

  9. Universal types in OCaml fun f x y -> f x + f y ∀𝛽 ::*. ( 𝛽 → Int) → 𝛽 → 𝛽 → Int ∀𝛽 ::*. ∀𝛾 ::*. ( ∀𝛿 ::*. 𝛿 → Int) → 𝛽 → 𝛾 → Int 8/ 59

  10. Universal types in OCaml (* ∀𝛽. List 𝛽 → 𝐽𝑜𝑢 *) type t = { h : 'a. 'a list -> int } let len = {h = List.length} (* (∀𝛽. List 𝛽 → 𝐽𝑜𝑢) → 𝐽𝑜𝑢 *) let g r = r.h [1; 2; 3] + r.h [1.0; 2.0; 3.0] 9/ 59

  11. Higher-kinded polymorphism f : ∀ F::* → *. ∀𝛽 ::*. F 𝛽 → (F 𝛽 → 𝛽 ) → 𝛽 x : List (Int × Int) f x 10/ 59

  12. Higher-kinded polymorphism 𝐺 𝛽 ∼ List ( Int × Int ) 𝐺 = List 𝛽 = Int × Int 𝐺 = Λ𝛾. List (𝛾 × 𝛾) 𝛽 = Int 𝐺 = Λ𝛾. List ( Int × Int ) 11/ 59

  13. Lightweight higher-kinded polymorphism A set 𝐆 of functions such that: ∀𝐺, 𝐻 ∈ 𝐆. 𝐺 ≠ 𝐻 ⇒ ∀𝑢.𝐺(𝑢) ≠ 𝐻(𝑢) 12/ 59

  14. Lightweight higher-kinded polymorphism type 'a t = ('a * 'a) list 13/ 59

  15. Lightweight higher-kinded polymorphism type lst = List type opt = Option type ('a, 'f) app = | Lst : 'a list -> ('a, lst) app | Opt : 'a option -> ('a, opt) app ('a, lst)app ≈ 'a list ('a, opt)app ≈ 'a option 14/ 59

  16. Lightweight higher-kinded polymorphism type 'f map = { map: 'a 'b. ('a -> 'b) -> ('a, 'f) app -> ('b, 'f) app; } let f : 'b map -> (int, 'b) app -> (string, 'b) app = fun m c -> m.map (fun x -> "Int: " ^ (string_of_int x)) c 15/ 59

  17. Lightweight higher-kinded polymorphism let lmap : lst map = let l = f lmap (Lst [1; 2; 3]) let omap : opt map = let o = f omap (Opt (Some 6)) 16/ 59 {map = fun f (Lst l) -> Lst (List.map f l)} {map = fun f (Opt o) -> Opt (Option.map f o)}

  18. Lightweight higher-kinded polymorphism Generalised in the Higher library 17/ 59

  19. Functors 18/ 59

  20. Functors module type Eq = sig val equal : t -> t -> bool end type elt val empty : t val is_empty : t -> bool val mem : elt -> t -> bool val add : elt -> t -> t val remove : elt -> t -> t val to_list : t -> elt list end 19/ 59 type t module type SetS = sig type t

  21. Functors SetS with type elt = foo expands to sig type elt = foo val empty : t val is_empty : t -> bool val mem : elt -> t -> bool val add : elt -> t -> t val remove : elt -> t -> t val to_list : t -> elt list end 20/ 59 type t

  22. Functors SetS with type elt := foo expands to sig val empty : t val is_empty : t -> bool val mem : foo -> t -> bool val add : foo -> t -> t val remove : foo -> t -> t val to_list : t -> foo list end 21/ 59 type t

  23. Functors | [] -> false else x :: t if (mem x t) then t let add x t = else mem x rest if (E.equal x y) then true | y :: rest -> 22/ 59 module Set (E : Eq) | _ -> false | [] -> true let is_empty = function let empty = [] : SetS with type elt := E.t = struct type t = E.t list let rec mem x = function

  24. Functors let rec remove x = function | [] -> [] | y :: rest -> if (E.equal x y) then rest else y :: (remove x rest) let to_list t = t end 23/ 59

  25. Functors module IntEq = struct let equal (x : int) (y : int) = x = y end 24/ 59 type t = int module IntSet = Set(IntEq)

  26. Parametricity in System F 𝜕 25/ 59

  27. Universal types SetImpl = 𝜇𝛿 ::*. 𝜇𝛽 ::*. 𝛽 × ( 𝛽 → Bool) × ( 𝛿 → 𝛽 → Bool) × ( 𝛿 → 𝛽 → 𝛽 ) × ( 𝛿 → 𝛽 → 𝛽 ) × ( 𝛽 → List 𝛿 ) 26/ 59 empty = Λ𝛿 ::*. Λ𝛽 ::*. 𝜇 s:SetImpl 𝛿 𝛽 . 𝜌 1 s is_empty = Λ𝛿 ::*. Λ𝛽 ::*. 𝜇 s:SetImpl 𝛿 𝛽 . 𝜌 2 s mem = Λ𝛿 ::*. Λ𝛽 ::*. 𝜇 s:SetImpl 𝛿 𝛽 . 𝜌 3 s add = Λ𝛿 ::*. Λ𝛽 ::*. 𝜇 s:SetImpl 𝛿 𝛽 . 𝜌 4 s remove = Λ𝛿 ::*. Λ𝛽 ::*. 𝜇 s:SetImpl 𝛿 𝛽 . 𝜌 5 s to_list = Λ𝛿 ::*. Λ𝛽 ::*. 𝜇 s:SetImpl 𝛿 𝛽 . 𝜌 6 s

  28. Universal types EqImpl = 𝜇𝛿 ::*. 𝛿 → 𝛿 → Bool equal = Λ𝛿 ::*. 𝜇 s:EqImpl 𝛿 .s 27/ 59

  29. Universal types 𝜇 n: 𝛿 .fold [ 𝛿 ] [List 𝛿 ] as ∃𝛽 ::*.SetImpl 𝛿 𝛽 𝜇 l:List 𝛿 .l 〉 (nil [ 𝛿 ]), (cons [ 𝛿 ] x l)) if (equal [ 𝛿 ] eq n x) [List 𝛿 ] l ( 𝜇 x: 𝛿 . 𝜇 l:List 𝛿 . cons [ 𝛿 ], set_package = false, ( 𝜇 x: 𝛿 . 𝜇 y:Bool.or y (equal [ 𝛿 ] eq n x)) 𝜇 n: 𝛿 .fold [ 𝛿 ] [Bool] isempty [ 𝛿 ], nil [ 𝛿 ], pack List 𝛿 ,〈 Λ𝛿 ::*. 𝜇 eq:EqImpl 𝛿 . 28/ 59

  30. Universal types Γ ⊢ 𝑁 ∶ ∀𝛽∶∶𝐿.𝐵 Γ ⊢ 𝑁 [𝐶] ∶ 𝐵[𝛽 ∶= 𝐶] 29/ 59 Γ ⊢ 𝐶 ∶∶ 𝐿 ∀ -elim

  31. Relational parametricity 30/ 59

  32. Relational parametricity We can give precise descriptions of parametricity using relations between types. 31/ 59

  33. Relational parametricity Given a type 𝑈 with free variables 𝛽, 𝛾 1 , … , 𝛾 𝑜 : ∀𝛾 1 . … ∀𝛾 𝑜 .∀𝑦 ∶ (∀𝛽.𝑈). ∀𝛿. ∀𝜀. ∀𝜍 ⊂ 𝛿 × 𝜀. 𝑈[𝜍, = 𝛾 1 , … , = 𝛾 𝑜 ](𝑦[𝛿], 𝑦[𝜀]) 32/ 59

  34. Relational parametricity Any value with a universal type must preserve all relations between any two types that it can be instantiated with. 33/ 59

  35. Theorems for free 34/ 59

  36. Theorems for free Parametricity applied to ∀𝛽.𝛽 → 𝛽 : ∀𝑔 ∶ (∀𝛽.𝛽 → 𝛽). ∀𝛿. ∀𝜀. ∀𝜍 ⊂ 𝛿 × 𝜀. ∀𝑣 ∶ 𝛿. ∀𝑤 ∶ 𝜀. 𝜍(𝑣, 𝑤) ⇒ 𝜍(𝑔[𝛿] 𝑣, 𝑔[𝜀] 𝑤) 35/ 59

  37. Theorems for free is 𝑣 (𝑦 ∶ 𝑈, 𝑧 ∶ 𝑈) = 36/ 59 Defjne a relation is 𝑣 to represent being equal to a value 𝑣 ∶ 𝑈 : (𝑦 = 𝑈 𝑣) ∧ (𝑧 = 𝑈 𝑣)

  38. Theorems for free ∀𝑔 ∶ (∀𝛽.𝛽 → 𝛽). ∀𝛿.∀𝑣 ∶ 𝛿. is 𝑣 (𝑣, 𝑣) ⇒ is 𝑣 (𝑔[𝛿]𝑣, 𝑔[𝛿]𝑣) 37/ 59

  39. Theorems for free ∀𝑔 ∶ (∀𝛽.𝛽 → 𝛽). ∀𝛿.∀𝑣 ∶ 𝛿. 38/ 59 𝑔[𝛿] 𝑣 = 𝛿 𝑣

  40. Theorems for free Parametricity applied to ∀𝛽. List 𝛽 → List 𝛽 : ∀𝑔 ∶ (∀𝛽. List 𝛽 → List 𝛽). ∀𝛿. ∀𝜀. ∀𝜍 ⊂ 𝛿 × 𝜀. ∀𝑣 ∶ List 𝛿. ∀𝑤 ∶ List 𝜀. ( List 𝛽)[𝜍](𝑣, 𝑤) ⇒ ( List 𝛽)[𝜍](𝑔[𝛿] 𝑣, 𝑔[𝜀] 𝑤) 39/ 59

  41. Theorems for free The System F encoding for lists: List 𝛽 = ∀𝛾. 𝛾 → (𝛽 → 𝛾 → 𝛾) → 𝛾 Λ𝛾. 𝜇 n: 𝛾. 𝜇 c: 𝛽 → 𝛾 → 𝛾. c x (xs [ 𝛾 ] n c) 40/ 59 nil 𝛽 = Λ𝛾. 𝜇 n: 𝛾. 𝜇 c: 𝛽 → 𝛾 → 𝛾. n cons 𝛽 = 𝜇 x: 𝛽. 𝜇 xs:List 𝛽.

  42. Theorems for free The relational substitution of the System F encoding for lists: ( List 𝛽)[𝜍] = (𝑦 ∶ List 𝐵, 𝑧 ∶ List 𝐶). ∀𝑜 ∶ 𝛿. ∀𝑛 ∶ 𝜀. ∀𝑑 ∶ 𝐵 → 𝛿 → 𝛿. ∀𝑒 ∶ 𝐶 → 𝜀 → 𝜀. 𝜍 ′ (𝑜, 𝑛) ⇒ (∀𝑏 ∶ 𝐵. ∀𝑐 ∶ 𝐶. ∀𝑣 ∶ 𝛿. ∀𝑤 ∶ 𝜀. 𝜍(𝑏, 𝑐) ⇒ 𝜍 ′ (𝑣, 𝑤) ⇒ 𝜍 ′ (𝑑𝑏𝑣, 𝑒𝑐𝑤)) ⇒ 𝜍 ′ (𝑦[𝛿]𝑜𝑑, 𝑧[𝜀]𝑛𝑒) 41/ 59 ∀𝛿. ∀𝜀. ∀𝜍 ′ ⊂ 𝛿 × 𝜀.

  43. Theorems for free ∀𝑜 ∶ 𝛿. ∀𝑛 ∶ 𝜀. ∀𝑑 ∶ 𝐵 → 𝛿 → 𝛿. ∀𝑒 ∶ 𝐶 → 𝜀 → 𝜀. ∀𝑏 ∶ 𝐵.∀𝑣 ∶ 𝛿. ∀𝑐 ∶ 𝐶.∀𝑤 ∶ 𝛿. 𝜍 ′ (𝑜, 𝑛) ⇒ (∀𝑏 ∶ 𝐵. ∀𝑐 ∶ 𝐶. ∀𝑣 ∶ 𝛿. ∀𝑤 ∶ 𝜀. 𝜍(𝑏, 𝑐) ⇒ 𝜍 ′ (𝑣, 𝑤) ⇒ 𝜍 ′ (𝑑𝑏𝑣, 𝑒𝑐𝑤)) ⇒ 𝜍 ′ (𝑜𝑗𝑚 𝐵 [𝛿]𝑜𝑑, 𝑜𝑗𝑚 𝐶 [𝜀]𝑛𝑒) 42/ 59 If 𝑦 = 𝑜𝑗𝑚 𝐵 and 𝑧 = 𝑜𝑗𝑚 𝐶 : ∀𝛿. ∀𝜀. ∀𝜍 ′ ⊂ 𝛿 × 𝜀.

Recommend


More recommend