set theoretic foundation of parametric polymorphism and
play

Set-theoretic Foundation of Parametric Polymorphism and Subtyping - PowerPoint PPT Presentation

ICFP11 Set-theoretic Foundation of Parametric Polymorphism and Subtyping Giuseppe Castagna 1 and Zhiwu Xu 1 , 2 1 CNRS, Laboratoire Preuves, Programmes et Syst` emes, Universit e Paris Diderot, Paris, France. 2 State Key Laboratory of


  1. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Real case example: active pages To create a dynamically generated page in the Ocsigen web development systems: 1 define a function from the query string to Xhtml: let page_fun(p: {title: string, ...}) : Xhtml = ... 2 bind page fun to the path $WEBROOT/w/index by: register new service register new service register new service(page fun,"w/index") The (wished) type of register_new_service is → ∀ ( X ≤ Params ) . (( X → → Xhtml ) × Path ) → unit logoP7 where Params is a specification of all possible query strings Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 4/27

  2. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Real case example: active pages To create a dynamically generated page in the Ocsigen web development systems: 1 define a function from the query string to Xhtml: let page_fun(p: {title: string, ...}) : Xhtml = ... 2 bind page fun to the path $WEBROOT/w/index by: register new service register new service register new service(page fun,"w/index") The (wished) type of register_new_service is → ∀ ( X ≤ Params ) . (( X → → Xhtml ) × Path ) → unit logoP7 where Params is a specification of all possible query strings Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 4/27

  3. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Real case example: active pages To create a dynamically generated page in the Ocsigen web development systems: 1 define a function from the query string to Xhtml: let page_fun(p: {title: string, ...}) : Xhtml = ... 2 bind page fun to the path $WEBROOT/w/index by: register new service register new service register new service(page fun,"w/index") The (wished) type of register_new_service is → ∀ ( X ≤ Params ) . (( X → → Xhtml ) × Path ) → unit logoP7 where Params is a specification of all possible query strings Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 4/27

  4. ❈ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Current status Study of a type system of (recursive/regular) types with × → ∨ ∧ α t ::= B | t × × t | t → → t | t ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ | α α type constructors logical connectives type variables logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 5/27

  5. ❈ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Current status Study of a type system of (recursive/regular) types with × → ∨ ∧ α t ::= B | t × × t | t → → t | t ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ | α α type constructors logical connectives type variables Logical connectives: Well-known how to implement a functional language with pattern-matching, higher-order functions, and connectives with set theoretic interpretation . logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 5/27

  6. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Current status Study of a type system of (recursive/regular) types with × → ∨ ∧ α t ::= B | t × × t | t → → t | t ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ | α α type constructors logical connectives type variables Logical connectives: Well-known how to implement a functional language with pattern-matching, higher-order functions, and connectives with set theoretic interpretation . Semantic subtyping (implemented by the language ❈ Duce ). logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 5/27

  7. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Current status Study of a type system of (recursive/regular) types with × → ∨ ∧ α t ::= B | t × × t | t → → t | t ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ | α α type constructors logical connectives type variables Logical connectives: Well-known how to implement a functional language with pattern-matching, higher-order functions, and connectives with set theoretic interpretation . Semantic subtyping (implemented by the language ❈ Duce ). Type variables: A set-theoretic approach was deemed unfeasible or even impossible: logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 5/27

  8. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Current status Study of a type system of (recursive/regular) types with × → ∨ ∧ α t ::= B | t × × t | t → → t | t ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ | α α type constructors logical connectives type variables Logical connectives: Well-known how to implement a functional language with pattern-matching, higher-order functions, and connectives with set theoretic interpretation . Semantic subtyping (implemented by the language ❈ Duce ). Type variables: A set-theoretic approach was deemed unfeasible or even impossible: This work (built on the work of semantic subtyping) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 5/27

  9. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic Subtyping in a nutshell logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 6/27

  10. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping t ::= B | t × × t | t → × → → t | t ∨ ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ ∧ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 7/27

  11. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping t ::= B | t × × × t | t → → → t | t ∨ ∨ ∨ t | t ∧ ∧ ∧ t | ¬ t | ✵ | ✶ Constructor subtyping is easy : constructors do not mix, eg. : s 2 ≤ s 1 t 1 ≤ t 2 s 1 → → → t 1 ≤ s 2 → → t 2 → logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 7/27

  12. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping t ::= B | t × × × t | t → → → t | t ∨ ∨ ∨ t | t ∧ ∧ ∧ t | ¬ t | ✵ | ✶ Constructor subtyping is easy : constructors do not mix, eg. : s 2 ≤ s 1 t 1 ≤ t 2 s 1 → → t 1 ≤ s 2 → → → → t 2 Connective subtyping is harder : connectives distribute over constructors , eg. � ∨ → → ∧ → ( s 1 ∨ ∨ s 2 ) → → t ( s 1 → → t ) ∧ ∧ ( s 2 → → t ) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 7/27

  13. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping t ::= B | t × × t | t → × → → t | t ∨ ∨ ∨ t | t ∧ ∧ ∧ t | ¬ t | ✵ | ✶ Constructor subtyping is easy : constructors do not mix, eg. : s 2 ≤ s 1 t 1 ≤ t 2 s 1 → → → t 1 ≤ s 2 → → t 2 → Connective subtyping is harder : connectives distribute over constructors , eg. � ∨ → → ∧ → ( s 1 ∨ ∨ s 2 ) → → t ( s 1 → → t ) ∧ ∧ ( s 2 → → t ) Define subtyping semantically: [Hosoya, Pierce] 1 Interpret types as sets (of values) 2 Define subtyping as set containment. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 7/27

  14. ✵ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  15. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ � t 2 � ∩ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  16. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ � t � \ � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ ∩ � t 2 � Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = { f | f function from � t 1 � to � t 2 � } logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  17. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = { f | f function from � t 1 � to � t 2 � } Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  18. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = { f | f function from � t 1 � to � t 2 � } Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  19. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = { f | f function from � t 1 � to � t 2 � } Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  20. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ t 2 � = � t 1 � ∩ ∧ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = { f | f function from � t 1 � to � t 2 � } Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  21. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ t 2 � = � t 1 � ∩ ∧ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = { f | f function from � t 1 � to � t 2 � } Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  22. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ t 2 � = � t 1 � ∩ ∧ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ � t 1 × × × t 2 � = � t 1 � × × × � t 2 � { f ⊆ D 2 | ( d 1 , d 2 ) ∈ f , d 1 ∈ � t 1 � ⇒ d 2 ∈ � t 2 � } D D ⊆ D � t 1 → → → t 2 � = Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  23. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ t 2 � = � t 1 � ∩ ∧ ∩ � t 2 � ∩ Constructors have their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = P ( � t 1 � × � t 2 � ) Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  24. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ t 2 � = � t 1 � ∩ ∧ ∩ � t 2 � ∩ Constructors have their natural interpretation : D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = P ( � t 1 � × � t 2 � ) Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  25. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ t 2 � = � t 1 � ∩ ∧ ∩ � t 2 � ∩ Constructors have the same ⊆ as their natural interpretation: D 2 ⊆ D � t 1 × × × t 2 � = � t 1 � × × × � t 2 � D D ⊆ D � t 1 → → → t 2 � = P ( � t 1 � × � t 2 � ) Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  26. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ ¬ t � = D\ \ \ � t � � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ � t 2 � ∩ Constructors have the same ⊆ as their natural interpretation: � s 1 × × s 2 � ⊆ � t 1 × × × × t 2 � ⇐ ⇒ � s 1 � × × × � s 2 � ⊆ � t 1 � × × × � t 2 � � s 1 → → s 2 � ⊆ � t 1 → → → → t 2 � ⇐ ⇒ P ( � s 1 � × � s 2 � ) ⊆ P ( � t 1 � × � t 2 � ) Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Key idea Do not define what types are logoP7 define how they are related Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  27. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Semantic subtyping: formalization First , define an interpretation of types into sets. � � : Types → P ( D ) such that Connectives have their set-theoretic interpretation: ∨ ∪ � ✵ � = ∅ � t 1 ∨ ∨ t 2 � = � t 1 � ∪ ∪ � t 2 � � ¬ ¬ t � = D\ ¬ \ \ � t � � t 1 ∧ ∧ ∧ t 2 � = � t 1 � ∩ ∩ ∩ � t 2 � Constructors have the same ⊆ as their natural interpretation: � s 1 × × s 2 � ⊆ � t 1 × × × × t 2 � ⇐ ⇒ � s 1 � × × × � s 2 � ⊆ � t 1 � × × × � t 2 � � s 1 → → s 2 � ⊆ � t 1 → → → → t 2 � ⇐ ⇒ P ( � s 1 � × � s 2 � ) ⊆ P ( � t 1 � × � t 2 � ) Then define the subtyping relation as set-containment. def s ≤ t ⇐ ⇒ � s � ⊆ � t � Semantic subtyping [Benzaken, Castagna, Frisch] 1 Gives an interpretation satisfying the above constraints; logoP7 2 Gives an algorithm to decide the induced subtyping relation. Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 8/27

  28. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Polymorphic extension: adding type variables logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 9/27

  29. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Naive solution t ::= B | t × × t | t → × → → t | t ∨ ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ ∧ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 10/27

  30. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Naive solution t ::= B | t × × × t | t → → → t | t ∨ ∨ ∨ t | t ∧ ∧ ∧ t | ¬ t | ✵ | ✶ | α α α logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 10/27

  31. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Naive solution t ::= B | t × × × t | t → → → t | t ∨ ∨ ∨ t | t ∧ ∧ ∧ t | ¬ t | ✵ | ✶ | α α α Idea: Use the previous relation since is defined for “ground types” Let σ : Vars → ClosedTypes denote ground substitutions. Define: def s ≤ t ⇐ ⇒ ∀ σ . s σ ≤ t σ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 10/27

  32. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Naive solution t ::= B | t × × × t | t → → → t | t ∨ ∨ ∨ t | t ∧ ∧ ∧ t | ¬ t | ✵ | ✶ | α α α Idea: Use the previous relation since is defined for “ground types” Let σ : Vars → ClosedTypes denote ground substitutions. Define: def s ≤ t ⇐ ⇒ ∀ σ . s σ ≤ t σ or equivalently def s ≤ t ⇐ ⇒ ∀ σ. � s σ � ⊆ � t σ � logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 10/27

  33. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Naive solution t ::= B | t × × × t | t → → → t | t ∨ ∨ ∨ t | t ∧ ∧ t | ¬ t | ✵ | ✶ | α ∧ α α Idea: Use the previous relation since is defined for “ground types” Let σ : Vars → ClosedTypes denote ground substitutions. Define: def s ≤ t ⇐ ⇒ ∀ σ . s σ ≤ t σ or equivalently def s ≤ t ⇐ ⇒ ∀ σ. � s σ � ⊆ � t σ � THIS IS A WRONG WAY: TOO MANY PROBLEMS logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 10/27

  34. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution 1 Haruo Hosoya conjectured that deciding ∀ σ . s σ ≤ t σ is at least as hard as solving Diophantine equations logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 11/27

  35. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution 1 Haruo Hosoya conjectured that deciding ∀ σ . s σ ≤ t σ is at least as hard as solving Diophantine equations 2 It breaks parametricity: logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 11/27

  36. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution 1 Haruo Hosoya conjectured that deciding ∀ σ . s σ ≤ t σ is at least as hard as solving Diophantine equations 2 It breaks parametricity: × α × ¬ ∨ α × ( t × × α α ) ≤ ( t × ×¬ ¬ t ) ∨ ∨ ( α α × × t ) (1) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 11/27

  37. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution 1 Haruo Hosoya conjectured that deciding ∀ σ . s σ ≤ t σ is at least as hard as solving Diophantine equations 2 It breaks parametricity: × α × ¬ ∨ α × ( t × × α α ) ≤ ( t × ×¬ ¬ t ) ∨ ∨ ( α α × × t ) (1) This inclusion holds if and only if t is an indivisible type ( eg. , a singleton or a basic type): logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 11/27

  38. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution 1 Haruo Hosoya conjectured that deciding ∀ σ . s σ ≤ t σ is at least as hard as solving Diophantine equations 2 It breaks parametricity: × α × ¬ ∨ α × ( t × × α α ) ≤ ( t × ×¬ ¬ t ) ∨ ∨ ( α α × × t ) (1) This inclusion holds if and only if t is an indivisible type ( eg. , a singleton or a basic type): Property of indivisible types If t is an indivisible type , then for α all possible interpretations of α α α α ¬ t ≤ α α or α α ≤ ¬ ¬ t holds. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 11/27

  39. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution 1 Haruo Hosoya conjectured that deciding ∀ σ . s σ ≤ t σ is at least as hard as solving Diophantine equations 2 It breaks parametricity: × α × ¬ ∨ α × ( t × × α α ) ≤ ( t × ×¬ ¬ t ) ∨ ∨ ( α α × × t ) (1) This inclusion holds if and only if t is an indivisible type ( eg. , a singleton or a basic type): Property of indivisible types If t is an indivisible type , then for α all possible interpretations of α α α α ¬ t ≤ α α or α α ≤ ¬ ¬ t holds. If α α α ≤ ¬ ¬ ¬ t then the left element of the union in (18) suffices; If t ≤ α α , then α α α = ( α α α α \ t ) ∨ ∨ ∨ t . Thus ( t × × α × α α ) = ( t × × × ( α α α \ t )) ∨ ∨ ∨ ( t × × t ). × logoP7 This union is contained component-wise in the one in (18). Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 11/27

  40. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution The fact that ( t × × × α α α ) ≤ ( t × × ×¬ ¬ ¬ t ) ∨ ∨ ∨ ( α α α × × × t ) holds if and only if t is indivisible is really catastrophic: logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 12/27

  41. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution The fact that ( t × × × α α α ) ≤ ( t × ×¬ × ¬ ¬ t ) ∨ ∨ ∨ ( α α α × × × t ) holds if and only if t is indivisible is really catastrophic: Deciding subtyping needs deciding indivisibility ... which is very hard. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 12/27

  42. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution The fact that ( t × × × α α α ) ≤ ( t × ×¬ × ¬ ¬ t ) ∨ ∨ ∨ ( α α α × × × t ) holds if and only if t is indivisible is really catastrophic: Deciding subtyping needs deciding indivisibility ... which is very hard. This subtyping relation breaks parametricity : by subsumption a function generic in its first argument, becomes generic on its second argument. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 12/27

  43. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution The fact that ( t × × × α α α ) ≤ ( t × × ×¬ ¬ ¬ t ) ∨ ∨ ∨ ( α α α × × × t ) holds if and only if t is indivisible is really catastrophic: Deciding subtyping needs deciding indivisibility ... which is very hard. This subtyping relation breaks parametricity : by subsumption a function generic in its first argument, becomes generic on its second argument. A semantic solution was deemed unfeasible (even w/o arrows) Problem eschewed by resorting to syntactic solutions: [Hosoya, Frisch, Castagna: POPL 05], [Vouillon: POPL 06]. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 12/27

  44. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Problems with the naive solution The fact that ( t × × × α α α ) ≤ ( t × × ×¬ ¬ ¬ t ) ∨ ∨ ∨ ( α α α × × × t ) holds if and only if t is indivisible is really catastrophic: Deciding subtyping needs deciding indivisibility ... which is very hard. This subtyping relation breaks parametricity : by subsumption a function generic in its first argument, becomes generic on its second argument. A semantic solution was deemed unfeasible (even w/o arrows) Problem eschewed by resorting to syntactic solutions: [Hosoya, Frisch, Castagna: POPL 05], [Vouillon: POPL 06]. logoP7 A SEMANTIC SOLUTION IS POSSIBLE Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 12/27

  45. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution A faint intuition The loss of parametricity is only due to the interpretation of indivisible types, all the rest works (more or less) smoothly logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 13/27

  46. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution A faint intuition The loss of parametricity is only due to the interpretation of indivisible types, all the rest works (more or less) smoothly The crux of the problem is that for an indivisible type i i i i i ≤ α α α or α α α ≤ ¬ ¬ i ¬ i i i validity can stutter from one formula to another, missing in this way the uniformity typical of parametricity logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 13/27

  47. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution A faint intuition The loss of parametricity is only due to the interpretation of indivisible types, all the rest works (more or less) smoothly The crux of the problem is that for an indivisible type i i i i i ≤ α α α or α α α ≤ ¬ ¬ ¬ i i i i validity can stutter from one formula to another, missing in this way the uniformity typical of parametricity The leitmotif of this work A semantic characterization of models where stuttering is absent, should yield a subtyping relation that is: 1 Semantic 2 Intuitive for the programmer logoP7 3 Decidable Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 13/27

  48. ✵ ✶ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution Rough idea Make indivisible types “splittable” so that type variables can range over strict subsets of every type, indivisible types included. [intuition: interpret all non-empty types into infinite sets] logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 14/27

  49. ✵ ✶ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution Rough idea Make indivisible types “splittable” so that type variables can range over strict subsets of every type, indivisible types included. [intuition: interpret all non-empty types into infinite sets] Since this cannot be done at syntactic level, move to the semantic one and replace ground substitutions by semantic assignments: η : Vars → P ( D ) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 14/27

  50. ✵ ✶ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution Rough idea Make indivisible types “splittable” so that type variables can range over strict subsets of every type, indivisible types included. [intuition: interpret all non-empty types into infinite sets] Since this cannot be done at syntactic level, move to the semantic one and replace ground substitutions by semantic assignments: η : Vars → P ( D ) and now the interpretation function takes an extra parameter � � : Types → P ( D ) Vars → P ( D ) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 14/27

  51. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution Rough idea Make indivisible types “splittable” so that type variables can range over strict subsets of every type, indivisible types included. [intuition: interpret all non-empty types into infinite sets] Since this cannot be done at syntactic level, move to the semantic one and replace ground substitutions by semantic assignments: η : Vars → P ( D ) and now the interpretation function takes an extra parameter � � : Types → P ( D ) Vars → P ( D ) with � α α � η α = η ( α α ) α � ¬ ¬ ¬ t � η = D\ � t � η ∨ ∧ � t 1 ∨ ∨ t 2 � η = � t 1 � η ∪ � t 2 � η � t 1 ∧ ∧ t 2 � η = � t 1 � η ∩ � t 2 � η � ✵ � η = � ✶ � η = D ∅ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 14/27

  52. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 A semantic solution Rough idea Make indivisible types “splittable” so that type variables can range over strict subsets of every type, indivisible types included. [intuition: interpret all non-empty types into infinite sets] Since this cannot be done at syntactic level, move to the semantic one and replace ground substitutions by semantic assignments: η : Vars → P ( D ) and now the interpretation function takes an extra parameter � � : Types → P ( D ) Vars → P ( D ) with � α α α � η = η ( α α ) α � ¬ ¬ ¬ t � η = D\ � t � η ∨ ∧ � t 1 ∨ ∨ t 2 � η = � t 1 � η ∪ � t 2 � η � t 1 ∧ ∧ t 2 � η = � t 1 � η ∩ � t 2 � η � ✵ � η = � ✶ � η = D ∅ and such that it satisfies: logoP7 � t 1 → → → s 1 � η ⊆ � t 2 → → → s 2 � η ⇐ ⇒ P ( � t 1 � η × � s 1 � η ) ⊆ P ( � t 2 � η × � s 2 � η ) Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 14/27

  53. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Subtyping relation In this framework the natural definition of subtyping is def s ≤ t ⇐ ⇒ ∀ η . � s � η ⊆ � t � η It “just” remains to find the uniformity condition to avoid stuttering and recover parametricity. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 15/27

  54. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 The magic property: convexity Consider only models of semantic subtyping in which the following convexity property holds ∀ η. ( � t 1 � η = ∅ or � t 2 � η = ∅ ) ⇐ ⇒ ( ∀ η. � t 1 � η = ∅ ) or ( ∀ η. � t 2 � η = ∅ ) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 16/27

  55. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 The magic property: convexity Consider only models of semantic subtyping in which the following convexity property holds ∀ η. ( � t 1 � η = ∅ or � t 2 � η = ∅ ) ⇐ ⇒ ( ∀ η. � t 1 � η = ∅ ) or ( ∀ η. � t 2 � η = ∅ ) It avoids stuttering: ∀ η. ( � t ∧ ∧ ∧¬ ¬ α ¬ α α � η = ∅ or � t ∧ ∧ ∧ α α α � η = ∅ ) —that α α ¬ is, ( t ≤ α α or α α ≤ ¬ ¬ t )— holds if and only if t is empty. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 16/27

  56. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 The magic property: convexity Consider only models of semantic subtyping in which the following convexity property holds ∀ η. ( � t 1 � η = ∅ or � t 2 � η = ∅ ) ⇐ ⇒ ( ∀ η. � t 1 � η = ∅ ) or ( ∀ η. � t 2 � η = ∅ ) It avoids stuttering: ∀ η. ( � t ∧ ∧¬ ∧ ¬ α ¬ α α � η = ∅ or � t ∧ ∧ ∧ α α α � η = ∅ ) —that α α ¬ is, ( t ≤ α α or α α ≤ ¬ ¬ t )— holds if and only if t is empty. There are natural models: all models that map all non-empty types into infinite sets satisfy it [our initial intuition]. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 16/27

  57. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 The magic property: convexity Consider only models of semantic subtyping in which the following convexity property holds ∀ η. ( � t 1 � η = ∅ or � t 2 � η = ∅ ) ⇐ ⇒ ( ∀ η. � t 1 � η = ∅ ) or ( ∀ η. � t 2 � η = ∅ ) It avoids stuttering: ∀ η. ( � t ∧ ∧ ∧¬ ¬ ¬ α α α � η = ∅ or � t ∧ ∧ ∧ α α α � η = ∅ ) —that α α ¬ is, ( t ≤ α α or α α ≤ ¬ ¬ t )— holds if and only if t is empty. There are natural models: all models that map all non-empty types into infinite sets satisfy it [our initial intuition]. A sound, complete, and terminating decision algorithm: the condition gives us exactly the right conditions needed to reuse the subtyping algorithm devised for ground types. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 16/27

  58. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 The magic property: convexity Consider only models of semantic subtyping in which the following convexity property holds ∀ η. ( � t 1 � η = ∅ or � t 2 � η = ∅ ) ⇐ ⇒ ( ∀ η. � t 1 � η = ∅ ) or ( ∀ η. � t 2 � η = ∅ ) It avoids stuttering: ∀ η. ( � t ∧ ∧¬ ∧ ¬ α ¬ α α � η = ∅ or � t ∧ ∧ α ∧ α α � η = ∅ ) —that α α ¬ is, ( t ≤ α α or α α ≤ ¬ ¬ t )— holds if and only if t is empty. There are natural models: all models that map all non-empty types into infinite sets satisfy it [our initial intuition]. A sound, complete, and terminating decision algorithm: the condition gives us exactly the right conditions needed to reuse the subtyping algorithm devised for ground types. An intuitive relation: the algorithm returns intuitive results logoP7 (actually, it helps to better understand twisted examples) Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 16/27

  59. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 The magic property: convexity Consider only models of semantic subtyping in which the following convexity property holds ∀ η. ( � t 1 � η = ∅ or � t 2 � η = ∅ ) ⇐ ⇒ ( ∀ η. � t 1 � η = ∅ ) or ( ∀ η. � t 2 � η = ∅ ) It avoids stuttering: ∀ η. ( � t ∧ ∧¬ ∧ ¬ α ¬ α α � η = ∅ or � t ∧ ∧ α ∧ α α � η = ∅ ) —that α α ¬ is, ( t ≤ α α or α α ≤ ¬ ¬ t )— holds if and only if t is empty. There are natural models: all models that map all non-empty types into infinite sets satisfy it [our initial intuition]. A sound, complete, and terminating decision algorithm: the condition gives us exactly the right conditions needed to reuse the subtyping algorithm devised for ground types. An intuitive relation: the algorithm returns intuitive results logoP7 (actually, it helps to better understand twisted examples) Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 16/27

  60. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Examples of subtyping relations logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 17/27

  61. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Examples We can internalize properties such as: ( α → γ ) ∧ ( β → γ ) ∼ α ∨ β → γ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 18/27

  62. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Examples We can internalize properties such as: ( α → γ ) ∧ ( β → γ ) ∼ α ∨ β → γ or distributivity laws: ( α ∨ β × γ ) ∼ ( α × γ ) ∨ ( β × γ ) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 18/27

  63. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Examples We can internalize properties such as: ( α → γ ) ∧ ( β → γ ) ∼ α ∨ β → γ or distributivity laws: ( α ∨ β × γ ) ∼ ( α × γ ) ∨ ( β × γ ) and combining them deduce: ( α × γ → δ 1 ) ∧ ( β × γ → δ 2 ) ≤ ( α ∨ β × γ ) → δ 1 ∨ δ 2 logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 18/27

  64. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Examples We can internalize properties such as: ( α → γ ) ∧ ( β → γ ) ∼ α ∨ β → γ or distributivity laws: ( α ∨ β × γ ) ∼ ( α × γ ) ∨ ( β × γ ) and combining them deduce: ( α × γ → δ 1 ) ∧ ( β × γ → δ 2 ) ≤ ( α ∨ β × γ ) → δ 1 ∨ δ 2 Of course the problematic relation never holds, whatever the t : ( t × × α × α α ) �≤ ( t × × ×¬ ¬ ¬ t ) ∨ ∨ ∨ ( α α α × × t ) × logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 18/27

  65. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 We can prove relevant relations on infinite types, eg. , for the type of generic α α α -lists: α -list = µ z . ( α α α α α × × × z ) ∨ ∨ ∨ nil logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 19/27

  66. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 We can prove relevant relations on infinite types, eg. , for the type of generic α α α -lists: α -list = µ z . ( α α α α × α × z ) ∨ × ∨ ∨ nil we can prove that it contains both the α -lists of even length µ z . ( α α α × × × ( α α α × × × z )) ∨ ∨ nil ∨ ≤ ≤ ≤ µ z . ( α α α × × × z ) ∨ ∨ nil ∨ � �� � � �� � α -lists of even length α -lists and the α -lists with of odd length µ z . ( α α α × × × ( α α α × × × z )) ∨ ∨ ( α ∨ α α × × × nil) ≤ ≤ µ z . ( α ≤ α α × × × z ) ∨ ∨ ∨ nil � �� � � �� � α -lists of odd length α -lists logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 19/27

  67. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 We can prove relevant relations on infinite types, eg. , for the type of generic α α α -lists: α α α -list = µ z . ( α α × α × × z ) ∨ ∨ ∨ nil we can prove that it contains both the α -lists of even length µ z . ( α α α × × × ( α α × α × z )) ∨ × ∨ nil ∨ ≤ ≤ ≤ µ z . ( α α α × × × z ) ∨ ∨ ∨ nil � �� � � �� � α -lists of even length α -lists and the α -lists with of odd length µ z . ( α α α × × ( α × α × α × z )) ∨ × ∨ ∨ ( α α α × × × nil) ≤ ≤ µ z . ( α ≤ α α × × × z ) ∨ ∨ ∨ nil � �� � � �� � α -lists of odd length α -lists and that it is itself contained in the union of the two, that is: α α -list ∼ α ∼ ∼ ( µ z . ( α α α × × × ( α α α × × × z )) ∨ ∨ ∨ nil) ∨ ∨ ∨ ( µ z . ( α α α × × × ( α α α × × × z )) ∨ ∨ ∨ ( α α α × × nil)) × logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 19/27

  68. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 We can prove relevant relations on infinite types, eg. , for the type of generic α α α -lists: α -list = µ z . ( α α α α α × × × z ) ∨ ∨ ∨ nil we can prove that it contains both the α -lists of even length µ z . ( α α α × × × ( α α × α × z )) ∨ × ∨ ∨ nil ≤ ≤ ≤ µ z . ( α α α × × × z ) ∨ ∨ ∨ nil � �� � � �� � α -lists of even length α -lists and the α -lists with of odd length µ z . ( α α α × × ( α × α × α × × z )) ∨ ∨ ( α ∨ α × α × × nil) ≤ ≤ µ z . ( α ≤ α α × × × z ) ∨ ∨ ∨ nil � �� � � �� � α -lists of odd length α -lists and that it is itself contained in the union of the two, that is: α α -list ∼ α ∼ ( µ z . ( α ∼ α α × × × ( α α α × × × z )) ∨ ∨ ∨ nil) ∨ ∨ ∨ ( µ z . ( α α α × × × ( α α α × × × z )) ∨ ∨ ∨ ( α α × α × nil)) × logoP7 And we can prove far more complicated relations (see paper). Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 19/27

  69. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Subtyping algorithm logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 20/27

  70. ✵ ✶ 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Subtyping Algorithm: t 1 ≤ t 2 Step 1: Transform the subtyping problem into an emptiness decision problem: t 1 ≤ t 2 ⇐ ⇒ ∀ η. � t 1 � η ⊆ � t 2 � η ⇐ ⇒ ∀ η. � t 1 ∧¬ t 2 � η = ∅ ⇐ ⇒ t 1 ∧¬ t 2 ≤ ✵ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 21/27

  71. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Subtyping Algorithm: t 1 ≤ t 2 Step 1: Transform the subtyping problem into an emptiness decision problem: t 1 ≤ t 2 ⇐ ⇒ ∀ η. � t 1 � η ⊆ � t 2 � η ⇐ ⇒ ∀ η. � t 1 ∧¬ t 2 � η = ∅ ⇐ ⇒ t 1 ∧¬ t 2 ≤ ✵ Step 2: Put the type whose emptiness is to be decided in disjunctive normal form. � � ℓ ij i ∈ I j ∈ J where a ::= b | t × t | t → t | ✵ | ✶ | α and ℓ ::= a | ¬ a logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 21/27

  72. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Subtyping Algorithm: t 1 ≤ t 2 Step 1: Transform the subtyping problem into an emptiness decision problem: t 1 ≤ t 2 ⇐ ⇒ ∀ η. � t 1 � η ⊆ � t 2 � η ⇐ ⇒ ∀ η. � t 1 ∧¬ t 2 � η = ∅ ⇐ ⇒ t 1 ∧¬ t 2 ≤ ✵ Step 2: Put the type whose emptiness is to be decided in disjunctive normal form. � � ℓ ij i ∈ I j ∈ J where a ::= b | t × t | t → t | ✵ | ✶ | α and ℓ ::= a | ¬ a Step 3: Simplify mixed intersections: Consider each summand of the union: cases such as t 1 × t 2 ∧ t 1 → t 2 or t 1 × t 2 ∧ ¬ ( t 1 → t 2 ) are straightforward. � � � � ¬ a ′ Solve: a i α h ¬ β k j i ∈ I j ∈ J h ∈ H k ∈ K logoP7 where all a are of the same kind. Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 21/27

  73. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Step 4: Eliminate toplevel negative variables. , ⇒ ∀ η. � t { ¬ α ∀ η. � t � η = ∅ ⇐ / α } � η = ∅ so replace ¬ β k for β k (forall k ∈ K ) � � � ¬ a ′ Solve: α h a i j i ∈ I j ∈ J h ∈ H logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 22/27

  74. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Step 4: Eliminate toplevel negative variables. , ⇒ ∀ η. � t { ¬ α ∀ η. � t � η = ∅ ⇐ / α } � η = ∅ so replace ¬ β k for β k (forall k ∈ K ) � � � ¬ a ′ Solve: α h a i j i ∈ I j ∈ J h ∈ H Step 5: Eliminate toplevel variables. � � � t ′ 1 × t ′ t 1 × t 2 α h ≤ 2 t 1 × t 2 ∈ P h ∈ H t ′ 1 × t ′ 2 ∈ N holds if and only if � � � γ 1 h × γ 2 t ′ 1 σ × t ′ t 1 σ × t 2 σ ≤ 2 σ h t 1 × t 2 ∈ P h ∈ H t ′ 1 × t ′ 2 ∈ N logoP7 where σ = { ( γ 1 h × γ 2 h ) ∨ α h / α h } h ∈ H (similarly for arrows) Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 22/27

  75. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Step 6: Eliminate toplevel constructors, memoize, and recurse . Thanks to convexity and (set-theoretic) product decomposition rules � � t ′ 1 × t ′ t 1 × t 2 ≤ (2) 2 t 1 × t 2 ∈ P t ′ 1 × t ′ 2 ∈ N is equivalent to     � � � � ∀ N ′ ⊆ N . t ′  or t ′ t 1 ≤ t 2 ≤    1 2 t 1 × t 2 ∈ P t ′ 1 × t ′ t 1 × t 2 ∈ P t ′ 1 × t ′ 2 ∈ N ′ 2 ∈ N \ N ′ (similarly for arrows) logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 23/27

  76. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Conclusion and New Directions logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 24/27

  77. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Conclusion We presented the first known solution to the problem of defining a semantic subtyping relation for a polymorphic regular tree types. A solution to this problem was considered unfeasible or even impossible. Our solution immediately applies to functional XML processing, but the potential fields of application seem much more numerous. Finally, our work opens both practical and theoretical new directions of research. logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 25/27

  78. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ Int ) \ logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

  79. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ Int ) \ Local type inference: Let map map : ( α → β ) → α list → β list, map then for map even map even map even we wish to deduce the following type: ( Int list → Bool list ) ∧ ∧ ∧ int lists return bool lists ( ( α α α \ \ \ Int ) list → ( α α \ α \ \ Int ) list ) ∧ ∧ ∧ lists w/o ints return the same type α α \ ∨ ( α α list → (( α α \ \ Int ) ∨ ∨ Bool ) list ) ints in the argument are replaced by bools logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

  80. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ Int ) \ Local type inference: Let map map : ( α → β ) → α list → β list, map then for map even map even map even we wish to deduce the following type: ( Int list → Bool list ) ∧ ∧ ∧ int lists return bool lists ( ( α α α \ \ \ Int ) list → ( α α \ α \ \ Int ) list ) ∧ ∧ ∧ lists w/o ints return the same type α α \ ∨ ( α α list → (( α α \ \ Int ) ∨ ∨ Bool ) list ) ints in the argument are replaced by bools logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

  81. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ Int ) \ Local type inference: Let map map : ( α → β ) → α list → β list, map then for map even map even map even we wish to deduce the following type: ( Int list → Bool list ) ∧ ∧ ∧ int lists return bool lists ( ( α α α \ \ \ Int ) list → ( α α \ α \ \ Int ) list ) ∧ ∧ ∧ lists w/o ints return the same type α α \ ∨ ( α α list → (( α α \ \ Int ) ∨ ∨ Bool ) list ) ints in the argument are replaced by bools logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

  82. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ Int ) \ Local type inference: Let map map : ( α → β ) → α list → β list, map then for map even map even map even we wish to deduce the following type: ( Int list → Bool list ) ∧ ∧ ∧ int lists return bool lists ( ( α α α \ \ \ Int ) list → ( α α \ α \ \ Int ) list ) ∧ ∧ ∧ lists w/o ints return the same type α α \ ∨ ( α α list → (( α α \ \ Int ) ∨ ∨ Bool ) list ) ints in the argument are replaced by bools logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

  83. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ Int ) \ Local type inference: Let map map : ( α → β ) → α list → β list, map then for map even map even map even we wish to deduce the following type: ( Int list → Bool list ) ∧ ∧ ∧ int lists return bool lists ( ( α α α \ \ \ Int ) list → ( α α \ α \ \ Int ) list ) ∧ ∧ ∧ lists w/o ints return the same type α α \ ∨ ( α α list → (( α α \ \ Int ) ∨ ∨ Bool ) list ) ints in the argument are replaced by bools Cannot be obtained by just instantiating the type of map logoP7 Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

  84. 1. Motivations – 2. Semantic subtyping 3. Polymorphic extension 4. Examples 5. Subtyping algorithm 6. New directions ICFP’11 Practical problems New typing possibilities: fun even even even = | Int -> (x mod 2) == 0 | _ -> x Intuitively we want to type it by ( Int → → → Bool ) ∧ ∧ ∧ ( α α α \ \ \ Int → α α α \ \ \ Int ) Local type inference: Let map map : ( α → β ) → α list → β list, map then for map even map even map even we wish to deduce the following type: ( Int list → Bool list ) ∧ ∧ ∧ int lists return bool lists ( ( α α α \ \ \ Int ) list → ( α α \ α \ Int ) list ) ∧ \ ∧ ∧ lists w/o ints return the same type α α \ ∨ ( α α list → (( α α \ \ Int ) ∨ ∨ Bool ) list ) ints in the argument are replaced by bools Cannot be obtained by just instantiating the type of map logoP7 No principal typing (needs infinite connectives) Giuseppe Castagna and Zhiwu Xu Set-theoretic Foundation of Parametric Polymorphism and Subtyping 26/27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend