Towards Unification for Dependent Types
Ningning Xie, Bruno C. d. S. Oliveira
The University of Hong Kong
June 2017
- N. Xie, B.C.d.S. Oliveira
Towards Unification for Dependent Types TFP 2017 1 / 26
Towards Unification for Dependent Types Ningning Xie , Bruno C. d. S. - - PowerPoint PPT Presentation
Towards Unification for Dependent Types Ningning Xie , Bruno C. d. S. Oliveira The University of Hong Kong June 2017 N. Xie, B.C.d.S. Oliveira Towards Unification for Dependent Types TFP 2017 1 / 26 Outline Motivation and Background 1
Ningning Xie, Bruno C. d. S. Oliveira
The University of Hong Kong
June 2017
Towards Unification for Dependent Types TFP 2017 1 / 26
1
Motivation and Background
2
Unification Algorithm
3
Extension: Implicit polymorphism
4
Conclusion
Towards Unification for Dependent Types TFP 2017 2 / 26
1
Motivation and Background
2
Unification Algorithm
3
Extension: Implicit polymorphism
4
Conclusion
Towards Unification for Dependent Types TFP 2017 3 / 26
Developments on type unification techniques for sophisticated dependent type systems.
Features: higher-order, polymorphism, subtyping, etc. powerful, but complicated, complex, and hard to reason.
1
1Ziliani, Beta, and Matthieu Sozeau. ”A unification algorithm for Coq featuring universe polymorphism and overloading.”
ACM SIGPLAN Notices. Vol. 50. No. 9. ACM, 2015.
Towards Unification for Dependent Types TFP 2017 4 / 26
Developments on type unification techniques for sophisticated dependent type systems.
Features: higher-order, polymorphism, subtyping, etc. powerful, but complicated, complex, and hard to reason.
Developments on dependent type systems that give programmers more control.
Manage type-level computations using explicit casts.
1 2 3 4
Decidable type checking based on alpha-equality. Easy to combine recursive types.
1Yang, Yanpeng, Xuan Bi, and Bruno C. D. S. Oliveira. ”Unified Syntax with Iso-types.” Asian Symposium on Programming
Languages and Systems. Springer International Publishing, 2016.
2van Doorn, Floris, Herman Geuvers, and Freek Wiedijk. ”Explicit convertibility proofs in pure type systems.” Proceedings of
the Eighth ACM SIGPLAN international workshop on Logical frameworks & meta-languages: theory & practice. ACM, 2013.
3Kimmell, Garrin, et al. ”Equational reasoning about programs with general recursion and call-by-value semantics.”
Proceedings of the sixth workshop on Programming languages meets program verification. ACM, 2012.
4Sjberg, Vilhelm, and Stephanie Weirich. ”Programming up to congruence.” ACM SIGPLAN Notices. Vol. 50. No. 1. ACM,
Towards Unification for Dependent Types TFP 2017 4 / 26
Developments on type unification techniques for sophisticated dependent type systems.
Features: higher-order, polymorphism, subtyping, etc. powerful, but complicated, complex, and hard to reason.
Developments on dependent type systems that give programmers more control.
Manage type-level computations using explicit casts. Decidable type checking based on alpha-equality. Easy to combine recursive types.
Question: can we get rid of the complication of the algorithms in those systems?
Towards Unification for Dependent Types TFP 2017 4 / 26
Our goal is to present a simple and complete unification algorithm for first-order dependent type systems with alpha-equality based type checking fill the gap between delicate unification algorithms for simple types and sophisticated unification algorithms for dependent types. We do not intend to solve more problems than existing unification algorithms. serve for beta-equality based dependent type systems.
Towards Unification for Dependent Types TFP 2017 5 / 26
Strategy: type sanitization that resolves the dependency between types. Algorithm: an alpha-equality based unification algorithm for first-order dependent types. Extension: subtyping in implicit polymorphism. Meta-theory Study: undergoing.
Towards Unification for Dependent Types TFP 2017 6 / 26
Types depends on terms. Vector of integers
definition without dependent types: data Vect = Nil | Cons Int Vect
Towards Unification for Dependent Types TFP 2017 7 / 26
Types depends on terms. Vector of integers
definition without dependent types: data Vect = Nil | Cons Int Vect
head :: Vect → Int
Towards Unification for Dependent Types TFP 2017 7 / 26
Types depends on terms. Vector of integers
definition without dependent types: data Vect = Nil | Cons Int Vect
head :: Vect → Int make it total head :: Vect → Maybe Int
Towards Unification for Dependent Types TFP 2017 7 / 26
Types depends on terms. Vector of integers
definition without dependent types: data Vect = Nil | Cons Int Vect
head :: Vect → Int make it total head :: Vect → Maybe Int
definition with dependent type: sized Vector data Vect :: Nat → Type = | Nil :: Vect Z | Cons :: Int → Vect k → Vect (S k)
Towards Unification for Dependent Types TFP 2017 7 / 26
Types depends on terms. Vector of integers
definition without dependent types: data Vect = Nil | Cons Int Vect
head :: Vect → Int make it total head :: Vect → Maybe Int
definition with dependent type: sized Vector data Vect :: Nat → Type = | Nil :: Vect Z | Cons :: Int → Vect k → Vect (S k) head :: Vect (S k) → Int
Towards Unification for Dependent Types TFP 2017 7 / 26
Unification
Given two terms containing some unification variables, find the substitution which makes two terms equal.
Towards Unification for Dependent Types TFP 2017 8 / 26
Unification
Given two terms containing some unification variables, find the substitution which makes two terms equal.
Bool → Int
Towards Unification for Dependent Types TFP 2017 8 / 26
Unification
Given two terms containing some unification variables, find the substitution which makes two terms equal.
Bool → Int Solution: α = Bool.
Towards Unification for Dependent Types TFP 2017 8 / 26
1
Motivation and Background
2
Unification Algorithm
3
Extension: Implicit polymorphism
4
Conclusion
Towards Unification for Dependent Types TFP 2017 9 / 26
Unified syntax based on λC
Syntax
Type σ, τ ::= α | e Expr e ::= x | ⋆ | e1 e2 | λx : σ. e | Πx : σ1. σ2 λx. e ≡ λx : α. e Example: (λx : ⋆. λy : x. y) :: Πx : ⋆. Πy : x. x A → B for Πx : A. B if x does not appear in B.
Towards Unification for Dependent Types TFP 2017 10 / 26
Key ideas:
1:
Algorithmic typing context
Contexts Γ, Θ, ∆ ::=∅ | Γ, x : σ | Γ, α | Γ, α = τ scope constraint
λx : α. λy : β. y
1Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 11 / 26
Key ideas:
1:
Algorithmic typing context
Contexts Γ, Θ, ∆ ::=∅ | Γ, x : σ | Γ, α | Γ, α = τ scope constraint
λx : α. λy : β. y
judgment: Γ ⊢ τ1 ≃ τ2⊣ Θ
1Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 11 / 26
Key ideas:
1:
Algorithmic typing context
Contexts Γ, Θ, ∆ ::=∅ | Γ, x : σ | Γ, α | Γ, α = τ scope constraint
λx : α. λy : β. y
judgment: Γ ⊢ τ1 ≃ τ2⊣ Θ invariant: inputs are already fully substituted under current context.
α ≃ Bool invalid
1Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 11 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
solve α = α1 → α2. unify Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α1 ≃ A Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α2 ≃ B
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
solve α = α1 → α2. unify Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α1 ≃ A Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α2 ≃ B
Then Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x?
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
solve α = α1 → α2. unify Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α1 ≃ A Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α2 ≃ B
Then Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x?
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
solve α = α1 → α2. unify Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α1 ≃ A Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α2 ≃ B
Then Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x?
Γ1, α1, x, α2, α = α1 → α2, Γ2.
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
solve α = α1 → α2. unify Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α1 ≃ A Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α2 ≃ B
Then Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x?
Γ1, α1, x, α2, α = α1 → α2, Γ2.
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
The case when we have a unification variable on one side: Γ, α, ∆ ⊢ α ≃ τ what we will do if τ is a function? Γ, α, ∆ ⊢ α ≃ A → B
try directly scope constraint? No.
β ⊢ α ≃ Int → β
α = Int → α1, β = α1 unification variables need special treatments in scope constraint!
In Dunfield and Krishnaswami 2013
2:
solve α = α1 → α2. unify Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α1 ≃ A Γ1, α1, α2, α = α1 → α2, Γ2 ⊢ α2 ≃ B
Then Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x?
Γ1, α1, x, α2, α = α1 → α2, Γ2. No.
2Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
Towards Unification for Dependent Types TFP 2017 12 / 26
can not use scope constraint directly because of unification variables
β ⊢ α ≃ Int → β
α = Int → α1, β = α1
cannot destruct a Pi type because of the type dependency
Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x
Towards Unification for Dependent Types TFP 2017 13 / 26
can not use scope constraint directly because of unification variables
β ⊢ α ≃ Int → β
α = Int → α1, β = α1
cannot destruct a Pi type because of the type dependency
Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x
satisfies the scope constraint.
Towards Unification for Dependent Types TFP 2017 13 / 26
can not use scope constraint directly because of unification variables
β ⊢ α ≃ Int → β
α = Int → α1, β = α1
cannot destruct a Pi type because of the type dependency
Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x
satisfies the scope constraint. Our solution: for unification problem Γ, α, ∆ ⊢ α ≃ τ, we sanitize the unification variables in τ before we check the scope constraint.
Towards Unification for Dependent Types TFP 2017 13 / 26
Type Sanitization
Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α.
Towards Unification for Dependent Types TFP 2017 14 / 26
Type Sanitization
Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α. Example
Towards Unification for Dependent Types TFP 2017 14 / 26
Type Sanitization
Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α. Example
β ⊢ α ≃ Int → β
type sanitization: α, β ⊢ Int → β − → Int → α1 ⊣ α1, α, β = α1 after scope constraint: α1, α = Int → α1, β = α1
Towards Unification for Dependent Types TFP 2017 14 / 26
Type Sanitization
Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α. Example
β ⊢ α ≃ Int → β
type sanitization: α, β ⊢ Int → β − → Int → α1 ⊣ α1, α, β = α1 after scope constraint: α1, α = Int → α1, β = α1
β, x ⊢ α ≃ x → β
Towards Unification for Dependent Types TFP 2017 14 / 26
Type Sanitization
Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α. Example
β ⊢ α ≃ Int → β
type sanitization: α, β ⊢ Int → β − → Int → α1 ⊣ α1, α, β = α1 after scope constraint: α1, α = Int → α1, β = α1
β, x ⊢ α ≃ x → β
type sanitization: α, β, x ⊢ x → β − → x → α1 ⊣ α1, α, β = α1
Towards Unification for Dependent Types TFP 2017 14 / 26
Type Sanitization
Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α. Example
β ⊢ α ≃ Int → β
type sanitization: α, β ⊢ Int → β − → Int → α1 ⊣ α1, α, β = α1 after scope constraint: α1, α = Int → α1, β = α1
β, x ⊢ α ≃ x → β
type sanitization: α, β, x ⊢ x → β − → x → α1 ⊣ α1, α, β = α1 after scope constraint: fail.
Towards Unification for Dependent Types TFP 2017 14 / 26
Key ideas:
judgment: Γ ⊢ τ1 ≃ τ2 ⊣ Θ invariant: inputs are already fully substituted under current context.
Towards Unification for Dependent Types TFP 2017 15 / 26
Key ideas:
judgment: Γ ⊢ τ1 ≃ τ2 ⊣ Θ invariant: inputs are already fully substituted under current context. strategy: type sanitization
Towards Unification for Dependent Types TFP 2017 15 / 26
Key ideas:
judgment: Γ ⊢ τ1 ≃ τ2 ⊣ Θ invariant: inputs are already fully substituted under current context. strategy: type sanitization ...Find more explanations in the paper.
Towards Unification for Dependent Types TFP 2017 15 / 26
1
Motivation and Background
2
Unification Algorithm
3
Extension: Implicit polymorphism
4
Conclusion
Towards Unification for Dependent Types TFP 2017 16 / 26
Syntax
Type σ ::= α | e Expr e ::= x | ⋆ | e1 e2 | λx : σ. e | Πx : σ1. σ2 | ∀x : ⋆.σ Monotype τ ::= {σ′ ∈ σ, ∀ / ∈ σ′} A restricted version of polymorphic types. We write ∀a.a → a for ∀a : ⋆.a → a.
Towards Unification for Dependent Types TFP 2017 17 / 26
Syntax
Type σ ::= α | e Expr e ::= x | ⋆ | e1 e2 | λx : σ. e | Πx : σ1. σ2 | ∀x : ⋆.σ Monotype τ ::= {σ′ ∈ σ, ∀ / ∈ σ′} A restricted version of polymorphic types. We write ∀a.a → a for ∀a : ⋆.a → a. Predictivity: universal quantifiers can only be instantiated by monotypes.
Towards Unification for Dependent Types TFP 2017 17 / 26
Syntax
Type σ ::= α | e Expr e ::= x | ⋆ | e1 e2 | λx : σ. e | Πx : σ1. σ2 | ∀x : ⋆.σ Monotype τ ::= {σ′ ∈ σ, ∀ / ∈ σ′} A restricted version of polymorphic types. We write ∀a.a → a for ∀a : ⋆.a → a. Predictivity: universal quantifiers can only be instantiated by monotypes. Unification is between monotypes. Unification variables can only have monotypes.
Towards Unification for Dependent Types TFP 2017 17 / 26
Polymorphic Subtyping
σ1 is a subtype of σ2, denoted by Γ ⊢ σ1 ⊑ σ2, if σ1 is more polymorphic than σ2 under Γ. examples:
Γ ⊢ ∀a.a → a ⊑ Int → Int Γ ⊢ Int → (∀a.a → a) ⊑ Int → (Int → Int) Γ ⊢ (Int → Int) → Int ⊑ (∀a.a → a) → Int
Towards Unification for Dependent Types TFP 2017 18 / 26
What happen if we have a unification variable on one side?
Towards Unification for Dependent Types TFP 2017 19 / 26
What happen if we have a unification variable on one side? do unification? Γ ⊢ α ≃ σ ⊣ Θ Γ ⊢ α ⊑ σ ⊣ Θ
Towards Unification for Dependent Types TFP 2017 19 / 26
What happen if we have a unification variable on one side? do unification? Γ ⊢ α ≃ σ ⊣ Θ Γ ⊢ α ⊑ σ ⊣ Θ unification variables can only be solved by monotypes!
Towards Unification for Dependent Types TFP 2017 19 / 26
What happen if we have a unification variable on one side? do unification? Γ ⊢ α ≃ σ ⊣ Θ Γ ⊢ α ⊑ σ ⊣ Θ unification variables can only be solved by monotypes! however, we cannot restrict σ to be a monotype Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int
Towards Unification for Dependent Types TFP 2017 19 / 26
What happen if we have a unification variable on one side? do unification? Γ ⊢ α ≃ σ ⊣ Θ Γ ⊢ α ⊑ σ ⊣ Θ unification variables can only be solved by monotypes! however, we cannot restrict σ to be a monotype Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int again, we cannot destruct pi type because of type dependency. Γ ⊢ α ⊑ Πx : ⋆. x
Towards Unification for Dependent Types TFP 2017 19 / 26
Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int
Towards Unification for Dependent Types TFP 2017 20 / 26
Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int Observation: when the unification variable is on the left, even though there can be polymorphic components on the right, those polymorphic components must appear contra-variantly.
Towards Unification for Dependent Types TFP 2017 20 / 26
Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int Observation: when the unification variable is on the left, even though there can be polymorphic components on the right, those polymorphic components must appear contra-variantly. similar observation for when the unification variable on the right, polymorphic components must appear co-variantly.
Towards Unification for Dependent Types TFP 2017 20 / 26
Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int Observation: when the unification variable is on the left, even though there can be polymorphic components on the right, those polymorphic components must appear contra-variantly. similar observation for when the unification variable on the right, polymorphic components must appear co-variantly.
Towards Unification for Dependent Types TFP 2017 20 / 26
Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int Observation: when the unification variable is on the left, even though there can be polymorphic components on the right, those polymorphic components must appear contra-variantly. similar observation for when the unification variable on the right, polymorphic components must appear co-variantly.
α1 → α1). Int
Towards Unification for Dependent Types TFP 2017 20 / 26
Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int Observation: when the unification variable is on the left, even though there can be polymorphic components on the right, those polymorphic components must appear contra-variantly. similar observation for when the unification variable on the right, polymorphic components must appear co-variantly.
α1 → α1). Int How to turn Πx : (∀y.y → y). Int into Πx : ( α1 → α1). Int
Towards Unification for Dependent Types TFP 2017 20 / 26
How to turn Πx : (∀y.y → y). Int into Πx : ( α1 → α1). Int we can always replace universal quantifiers that appear contra-variantly by a fresh unification variable.
Towards Unification for Dependent Types TFP 2017 21 / 26
How to turn Πx : (∀y.y → y). Int into Πx : ( α1 → α1). Int we can always replace universal quantifiers that appear contra-variantly by a fresh unification variable.
α and σ, we sanitize the contra-variant universal quantifiers in σ before we use unification.
Towards Unification for Dependent Types TFP 2017 21 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable.
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
α ⊑ (∀a.a → a)
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
α ⊑ (∀a.a → a)
polymorphic type sanitization fail.
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
α ⊑ (∀a.a → a)
polymorphic type sanitization fail.
β ⊢ α ⊑ Int → β
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
α ⊑ (∀a.a → a)
polymorphic type sanitization fail.
β ⊢ α ⊑ Int → β
polymorphic type sanitization: α, β ⊢ Int → β − → Int → β ⊣ α, β
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
α ⊑ (∀a.a → a)
polymorphic type sanitization fail.
β ⊢ α ⊑ Int → β
polymorphic type sanitization: α, β ⊢ Int → β − → Int → β ⊣ α, β unification: α1, α = Int → α1, β = α1
Towards Unification for Dependent Types TFP 2017 22 / 26
Polymorphic Type Sanitization
Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable. Example
α ⊑ (∀a.a → a) → Int
polymorphic type sanitization:
→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int
α ⊑ (∀a.a → a)
polymorphic type sanitization fail.
β ⊢ α ⊑ Int → β
polymorphic type sanitization: α, β ⊢ Int → β − → Int → β ⊣ α, β unification: α1, α = Int → α1, β = α1
Find more explanations in the paper!
Towards Unification for Dependent Types TFP 2017 22 / 26
1
Motivation and Background
2
Unification Algorithm
3
Extension: Implicit polymorphism
4
Conclusion
Towards Unification for Dependent Types TFP 2017 23 / 26
Powerful but complicated unification algorithms for dependent types:
Ziliani, B., Sozeau, M. (2015, August) 3; Elliott, C. (1989). 4; Abel, A., Pientka, B. (2011, June) 5
Complete and easy unification/subtyping algorithm for simple types and System F types:
Hindley-Milner algorithm 6 7; Dunfield, J., Krishnaswami, N. R. (2013, September). 8; Jones, S. P., Vytiniotis, D., Weirich, S., Shields, M. (2007) 9;
Dependent type systems with alpha-equality based type checking:
type-level computation by explicit casts 10 11 12 13
3Ziliani, Beta, and Matthieu Sozeau. ”A unification algorithm for Coq featuring universe polymorphism and overloading.”
ACM SIGPLAN Notices. Vol. 50. No. 9. ACM, 2015.
4Elliott, Conal. ”Higher-order unification with dependent function types.” Rewriting Techniques and Applications. Springer
Berlin/Heidelberg, 1989.
5Abel, Andreas, and Brigitte Pientka. ”Higher-order dynamic pattern unification for dependent types and records.”
International Conference on Typed Lambda Calculi and Applications. Springer Berlin Heidelberg, 2011.
6Damas, Luis, and Robin Milner. ”Principal type-schemes for functional programs.” Proceedings of the 9th ACM
SIGPLAN-SIGACT symposium on Principles of programming languages. ACM, 1982.
7Hindley, Roger. ”The principal type-scheme of an object in combinatory logic.” Transactions of the american mathematical
society 146 (1969): 29-60.
8Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank
polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.
9Jones, Simon Peyton, et al. ”Practical type inference for arbitrary-rank types.” Journal of functional programming 17.01
(2007): 1-82.
Towards Unification for Dependent Types TFP 2017 24 / 26
Strategy: a both simple to understand and simple to implement strategy called type sanitization Algorithm: A simple and complete alpha-equality based unification algorithm Extension: polymorphic type sanitization to deal with polymorphic subtyping. Meta-theory: proof sketches.
Towards Unification for Dependent Types TFP 2017 25 / 26
Towards Unification for Dependent Types TFP 2017 26 / 26