Towards Unification for Dependent Types Ningning Xie , Bruno C. d. S. - - PowerPoint PPT Presentation

towards unification for dependent types
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Outline

1

Motivation and Background

2

Unification Algorithm

3

Extension: Implicit polymorphism

4

Conclusion

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 2 / 26

slide-3
SLIDE 3

Outline

1

Motivation and Background

2

Unification Algorithm

3

Extension: Implicit polymorphism

4

Conclusion

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 3 / 26

slide-4
SLIDE 4

Motivation

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 4 / 26

slide-5
SLIDE 5

Motivation

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,

  • 2015. N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 4 / 26

slide-6
SLIDE 6

Motivation

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?

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 4 / 26

slide-7
SLIDE 7

Goals

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 5 / 26

slide-8
SLIDE 8

Contributions

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 6 / 26

slide-9
SLIDE 9

Background: Dependent Types

Types depends on terms. Vector of integers

definition without dependent types: data Vect = Nil | Cons Int Vect

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 7 / 26

slide-10
SLIDE 10

Background: Dependent Types

Types depends on terms. Vector of integers

definition without dependent types: data Vect = Nil | Cons Int Vect

  • ne definition that could cause run-time error

head :: Vect → Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 7 / 26

slide-11
SLIDE 11

Background: Dependent Types

Types depends on terms. Vector of integers

definition without dependent types: data Vect = Nil | Cons Int Vect

  • ne definition that could cause run-time error

head :: Vect → Int make it total head :: Vect → Maybe Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 7 / 26

slide-12
SLIDE 12

Background: Dependent Types

Types depends on terms. Vector of integers

definition without dependent types: data Vect = Nil | Cons Int Vect

  • ne definition that could cause run-time error

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)

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 7 / 26

slide-13
SLIDE 13

Background: Dependent Types

Types depends on terms. Vector of integers

definition without dependent types: data Vect = Nil | Cons Int Vect

  • ne definition that could cause run-time error

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 7 / 26

slide-14
SLIDE 14

Background: Unification Problem

Unification

Given two terms containing some unification variables, find the substitution which makes two terms equal.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 8 / 26

slide-15
SLIDE 15

Background: Unification Problem

Unification

Given two terms containing some unification variables, find the substitution which makes two terms equal.

  • α → Int

Bool → Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 8 / 26

slide-16
SLIDE 16

Background: Unification Problem

Unification

Given two terms containing some unification variables, find the substitution which makes two terms equal.

  • α → Int

Bool → Int Solution: α = Bool.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 8 / 26

slide-17
SLIDE 17

Outline

1

Motivation and Background

2

Unification Algorithm

3

Extension: Implicit polymorphism

4

Conclusion

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 9 / 26

slide-18
SLIDE 18

Language

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 10 / 26

slide-19
SLIDE 19

Unification Algorithm

Key ideas:

  • rdered typing context

1:

Algorithmic typing context

Contexts Γ, Θ, ∆ ::=∅ | Γ, x : σ | Γ, α | Γ, α = τ scope constraint

λx : α. λy : β. y

  • α = y invalid
  • β = x valid

1Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank

polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 11 / 26

slide-20
SLIDE 20

Unification Algorithm

Key ideas:

  • rdered typing context

1:

Algorithmic typing context

Contexts Γ, Θ, ∆ ::=∅ | Γ, x : σ | Γ, α | Γ, α = τ scope constraint

λx : α. λy : β. y

  • α = y invalid
  • β = x valid

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 11 / 26

slide-21
SLIDE 21

Unification Algorithm

Key ideas:

  • rdered typing context

1:

Algorithmic typing context

Contexts Γ, Θ, ∆ ::=∅ | Γ, x : σ | Γ, α | Γ, α = τ scope constraint

λx : α. λy : β. y

  • α = y invalid
  • β = x valid

judgment: Γ ⊢ τ1 ≃ τ2⊣ Θ invariant: inputs are already fully substituted under current context.

  • α = Int ⊢

α ≃ Bool invalid

  • α = Int ⊢ Int ≃ Bool valid

1Dunfield, Joshua, and Neelakantan R. Krishnaswami. ”Complete and easy bidirectional typechecking for higher-rank

polymorphism.” ACM SIGPLAN Notices. Vol. 48. No. 9. ACM, 2013.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 11 / 26

slide-22
SLIDE 22

Problem

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-23
SLIDE 23

Problem

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-24
SLIDE 24

Problem

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-25
SLIDE 25

Problem

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-26
SLIDE 26

Problem

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 → β

  • α1,

α = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-27
SLIDE 27

Problem

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 → β

  • α1,

α = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-28
SLIDE 28

Problem

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 → β

  • α1,

α = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-29
SLIDE 29

Problem

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 → β

  • α1,

α = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-30
SLIDE 30

Problem

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 → β

  • α1,

α = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-31
SLIDE 31

Problem

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 → β

  • α1,

α = 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?

  • α2 = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-32
SLIDE 32

Problem

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 → β

  • α1,

α = 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?

  • α2 = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-33
SLIDE 33

Problem

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 → β

  • α1,

α = 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?

  • α2 = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-34
SLIDE 34

Problem

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 → β

  • α1,

α = 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?

  • α2 = 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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 12 / 26

slide-35
SLIDE 35

Problem

can not use scope constraint directly because of unification variables

  • α,

β ⊢ α ≃ Int → β

  • α1,

α = Int → α1, β = α1

cannot destruct a Pi type because of the type dependency

Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 13 / 26

slide-36
SLIDE 36

Problem

can not use scope constraint directly because of unification variables

  • α,

β ⊢ α ≃ Int → β

  • α1,

α = Int → α1, β = α1

cannot destruct a Pi type because of the type dependency

Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x

  • bservation: we can always solve it by a fresh unification variable that

satisfies the scope constraint.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 13 / 26

slide-37
SLIDE 37

Problem

can not use scope constraint directly because of unification variables

  • α,

β ⊢ α ≃ Int → β

  • α1,

α = Int → α1, β = α1

cannot destruct a Pi type because of the type dependency

Γ1, α, Γ2 ⊢ α ≃ Πx : ⋆. x

  • bservation: we can always solve it by a fresh unification variable that

satisfies the scope constraint. Our solution: for unification problem Γ, α, ∆ ⊢ α ≃ τ, we sanitize the unification variables in τ before we check the scope constraint.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 13 / 26

slide-38
SLIDE 38

Strategy

Type Sanitization

Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 14 / 26

slide-39
SLIDE 39

Strategy

Type Sanitization

Given α, τ, solve unification variables in τ out of scope of α by fresh unification variables that in that scope of α. Example

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 14 / 26

slide-40
SLIDE 40

Strategy

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 14 / 26

slide-41
SLIDE 41

Strategy

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 → β

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 14 / 26

slide-42
SLIDE 42

Strategy

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 14 / 26

slide-43
SLIDE 43

Strategy

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 14 / 26

slide-44
SLIDE 44

Unification

Key ideas:

  • rdered typing context. scope constraint.

judgment: Γ ⊢ τ1 ≃ τ2 ⊣ Θ invariant: inputs are already fully substituted under current context.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 15 / 26

slide-45
SLIDE 45

Unification

Key ideas:

  • rdered typing context. scope constraint.

judgment: Γ ⊢ τ1 ≃ τ2 ⊣ Θ invariant: inputs are already fully substituted under current context. strategy: type sanitization

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 15 / 26

slide-46
SLIDE 46

Unification

Key ideas:

  • rdered typing context. scope constraint.

judgment: Γ ⊢ τ1 ≃ τ2 ⊣ Θ invariant: inputs are already fully substituted under current context. strategy: type sanitization ...Find more explanations in the paper.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 15 / 26

slide-47
SLIDE 47

Outline

1

Motivation and Background

2

Unification Algorithm

3

Extension: Implicit polymorphism

4

Conclusion

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 16 / 26

slide-48
SLIDE 48

Language

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 17 / 26

slide-49
SLIDE 49

Language

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 17 / 26

slide-50
SLIDE 50

Language

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 17 / 26

slide-51
SLIDE 51

Subtyping

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 18 / 26

slide-52
SLIDE 52

Problem

What happen if we have a unification variable on one side?

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 19 / 26

slide-53
SLIDE 53

Problem

What happen if we have a unification variable on one side? do unification? Γ ⊢ α ≃ σ ⊣ Θ Γ ⊢ α ⊑ σ ⊣ Θ

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 19 / 26

slide-54
SLIDE 54

Problem

What happen if we have a unification variable on one side? do unification? Γ ⊢ α ≃ σ ⊣ Θ Γ ⊢ α ⊑ σ ⊣ Θ unification variables can only be solved by monotypes!

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 19 / 26

slide-55
SLIDE 55

Problem

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 19 / 26

slide-56
SLIDE 56

Problem

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 19 / 26

slide-57
SLIDE 57

Problem

Γ ⊢ α ⊑ Πx : (∀y.y → y). Int with solution α = Πx : (Int → Int). Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 20 / 26

slide-58
SLIDE 58

Problem

Γ ⊢ α ⊑ Π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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 20 / 26

slide-59
SLIDE 59

Problem

Γ ⊢ α ⊑ Π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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 20 / 26

slide-60
SLIDE 60

Problem

Γ ⊢ α ⊑ Π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.

  • α = Πx : (Bool → Bool). Int
  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 20 / 26

slide-61
SLIDE 61

Problem

Γ ⊢ α ⊑ Π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.

  • α = Πx : (Bool → Bool). Int
  • α = Πx : (

α1 → α1). Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 20 / 26

slide-62
SLIDE 62

Problem

Γ ⊢ α ⊑ Π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.

  • α = Πx : (Bool → Bool). Int
  • α = Πx : (

α1 → α1). Int How to turn Πx : (∀y.y → y). Int into Πx : ( α1 → α1). Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 20 / 26

slide-63
SLIDE 63

Strategy

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 21 / 26

slide-64
SLIDE 64

Strategy

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.

  • ur solution: for subtyping problem between

α and σ, we sanitize the contra-variant universal quantifiers in σ before we use unification.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 21 / 26

slide-65
SLIDE 65

Strategy

Polymorphic Type Sanitization

Given α, σ, remove universal quantifiers appearing contra-variantly, and replace corresponding type variables by a fresh unification variable.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-66
SLIDE 66

Strategy

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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-67
SLIDE 67

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α1 → α1) → Int ⊣ α1, α

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-68
SLIDE 68

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-69
SLIDE 69

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int

  • α ⊢

α ⊑ (∀a.a → a)

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-70
SLIDE 70

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int

  • α ⊢

α ⊑ (∀a.a → a)

polymorphic type sanitization fail.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-71
SLIDE 71

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int

  • α ⊢

α ⊑ (∀a.a → a)

polymorphic type sanitization fail.

  • α,

β ⊢ α ⊑ Int → β

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-72
SLIDE 72

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α1 → α1) → Int ⊣ α1, α after unification: α1, α = ( α1 → α1) → Int

  • α ⊢

α ⊑ (∀a.a → a)

polymorphic type sanitization fail.

  • α,

β ⊢ α ⊑ Int → β

polymorphic type sanitization: α, β ⊢ Int → β − → Int → β ⊣ α, β

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-73
SLIDE 73

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α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

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-74
SLIDE 74

Strategy

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:

  • α ⊢ (∀a.a → a) → Int −

→ ( α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!

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 22 / 26

slide-75
SLIDE 75

Outline

1

Motivation and Background

2

Unification Algorithm

3

Extension: Implicit polymorphism

4

Conclusion

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 23 / 26

slide-76
SLIDE 76

Related Work

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 24 / 26

slide-77
SLIDE 77

Conclusion

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.

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 25 / 26

slide-78
SLIDE 78

Thanks for listening!

  • N. Xie, B.C.d.S. Oliveira

Towards Unification for Dependent Types TFP 2017 26 / 26