Extending Hindley-Milner Type Inference with Coercive Structural - - PowerPoint PPT Presentation

extending hindley milner type inference with coercive
SMART_READER_LITE
LIVE PREVIEW

Extending Hindley-Milner Type Inference with Coercive Structural - - PowerPoint PPT Presentation

Why coercions? A naive algorithm Constraint-based algorithm Conclusion Extending Hindley-Milner Type Inference with Coercive Structural Subtyping Dmitriy Traytel Stefan Berghofer Tobias Nipkow APLAS 2011 Isabelle nat<:int =


slide-1
SLIDE 1

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Extending Hindley-Milner Type Inference with Coercive Structural Subtyping

Dmitriy Traytel Stefan Berghofer Tobias Nipkow APLAS 2011

λ → ∀

=

Isabelle

β α

nat<:int

slide-2
SLIDE 2

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Outline

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

slide-3
SLIDE 3

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Real-world examples

  • 2004: Avigad verifies in Isabelle:

(λx. pi x * ln (real x) / (real x)) ----> 1

slide-4
SLIDE 4

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Real-world examples

  • 2004: Avigad verifies in Isabelle:

(λx. pi x * ln (real x) / (real x)) ----> 1 i.e. the prime number theorem lim

x→∞

π(x) ln x x = 1

slide-5
SLIDE 5

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Real-world examples

  • 2004: Avigad verifies in Isabelle:

(λx. pi x * ln (real x) / (real x)) ----> 1 i.e. the prime number theorem lim

x→∞

π(x) ln x x = 1

  • 2009: Hölzl uses 1061 explicit conversions in a single theory
slide-6
SLIDE 6

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Real-world examples

  • 2004: Avigad verifies in Isabelle:

(λx. pi x * ln (real x) / (real x)) ----> 1 i.e. the prime number theorem lim

x→∞

π(x) ln x x = 1

  • 2009: Hölzl uses 1061 explicit conversions in a single theory
  • Both report “headaches”
slide-7
SLIDE 7

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Solution: coercive structural subtyping

Related work

  • Subtyping part of the type system:

Mitchell, Fuh & Mishra, Wand & O’Keefe, Pottier, Simonet

Cardelli, Pratt & Tiuryn, Luo, Kießling, Frey, Benke, Barthe, Chen

Reynolds, Swamy, Hicks & Bierman, Nordlander, Shields & Peyton Jones . . .

slide-8
SLIDE 8

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Solution: coercive structural subtyping

Related work

  • Subtyping part of the type system:

Mitchell, Fuh & Mishra, Wand & O’Keefe, Pottier, Simonet

Cardelli, Pratt & Tiuryn, Luo, Kießling, Frey, Benke, Barthe, Chen

Reynolds, Swamy, Hicks & Bierman, Nordlander, Shields & Peyton Jones . . .

  • Incomplete coercion inference system:

Saïbi, Luo

slide-9
SLIDE 9

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Solution: coercive structural subtyping

Related work

  • Subtyping part of the type system:

Mitchell, Fuh & Mishra, Wand & O’Keefe, Pottier, Simonet

Cardelli, Pratt & Tiuryn, Luo, Kießling, Frey, Benke, Barthe, Chen

Reynolds, Swamy, Hicks & Bierman, Nordlander, Shields & Peyton Jones . . .

  • Incomplete coercion inference system:

Saïbi, Luo

  • Complete coercion inference system:

this publication

slide-10
SLIDE 10

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The Hindley-Milner typing rules remain unchanged: No subtypes here Type inference is extended with coercion inference and coercion insertion

slide-11
SLIDE 11

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Our coercion inference system

  • Coercions: N <:real R
  • Lifted by map functions: N list <:map real R list
  • Programmer inputs terms omitting coercions
  • The system infers and inserts coercions
  • Result is well-typed according to Hindley-Milner
slide-12
SLIDE 12

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Our coercion inference system

  • Coercions: N <:real R
  • Lifted by map functions: N list <:map real R list
  • Programmer inputs terms omitting coercions
  • The system infers and inserts coercions
  • Result is well-typed according to Hindley-Milner
  • The coercion inference system:
  • is sound and complete
  • does not change the underlying type system
slide-13
SLIDE 13

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Local coercion insertion

  • Use judgement Γ ⊢ t u : τ
  • Idea: insert coercions whenever the function’s domain does not

match the argument type: ⊢ t1 u1 : τ11 → τ12 ⊢ t2 u2 : τ2 τ2 <:c τ11 ⊢ t1 t2 u1 (c u2) : τ12

slide-14
SLIDE 14

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Local coercion insertion

  • Use judgement Γ ⊢ t u : τ
  • Idea: insert coercions whenever the function’s domain does not

match the argument type: ⊢ t1 u1 : τ11 → τ12 ⊢ t2 u2 : τ2 τ2 <:c τ11 ⊢ t1 t2 u1 (c u2) : τ12

  • Used in Coq
slide-15
SLIDE 15

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Problematic example

Example: leq i n vs. leq n i

  • Signatures: leq :: α → α → B, n :: N and i :: Z
  • Declared coercion: N <:int Z
slide-16
SLIDE 16

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Problematic example

Example: leq i n vs. leq n i

  • Signatures: leq :: α → α → B, n :: N and i :: Z
  • Declared coercion: N <:int Z
  • Correctly, leq i n becomes leq i (int n), as
  • leq i :: Z → B
  • n :: N
slide-17
SLIDE 17

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Problematic example

Example: leq i n vs. leq n i

  • Signatures: leq :: α → α → B, n :: N and i :: Z
  • Declared coercion: N <:int Z
  • Correctly, leq i n becomes leq i (int n), as
  • leq i :: Z → B
  • n :: N
  • Unfortunately, the coercion inference of leq n i fails, as
  • leq n :: N → B
  • i :: Z
  • no coercion from Z to N
slide-18
SLIDE 18

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

This is “normal” behaviour of coercions. Coq Reference Manual

slide-19
SLIDE 19

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The subtyping pipeline

Input term t, context Γ Constraint generation Γ ⊢ t : τ S Weak unification test Constraint simplification Build constraint graph Constraint resolution Unification Coercion generation and insertion θΓ ⊢ θt u : θτ Output term u, type θτ, context θΓ

slide-20
SLIDE 20

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The subtyping pipeline

Input term t, context Γ Constraint generation Γ ⊢ t : τ S Weak unification test Constraint simplification Build constraint graph Constraint resolution Unification Coercion generation and insertion θΓ ⊢ θt u : θτ Output term u, type θτ, context θΓ

slide-21
SLIDE 21

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The subtyping pipeline

Input term t, context Γ Constraint generation Γ ⊢ t : τ S Weak unification test Constraint simplification Build constraint graph Constraint resolution Unification Coercion generation and insertion θΓ ⊢ θt u : θτ Output term u, type θτ, context θΓ

slide-22
SLIDE 22

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint generation

⊢ t1 : τ S1 ⊢ t2 : σ S2 α, β fresh ⊢ t1 t2 : β S1 ∪ S2 ∪ {τ

.

= α → β, σ <: α}

slide-23
SLIDE 23

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint generation

⊢ t1 : τ S1 ⊢ t2 : σ S2 α, β fresh ⊢ t1 t2 : β S1 ∪ S2 ∪ {τ

.

= α → β, σ <: α}

Example: leq n i

leq :: α → α → B ⊢ leq : α → α → B ∅ n :: N ⊢ n : N ∅ ⊢ leq n : β2 {α → α → B . = α2 → β2, N <: α2} i :: Z ⊢ i : Z ∅ ⊢ leq n i : β1        α → α → B

.

= α2 → β2, β2

.

= α1 → β1, N <: α2, Z <: α1       

slide-24
SLIDE 24

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The subtyping pipeline

Input term t, context Γ Constraint generation Γ ⊢ t : τ S Weak unification test Constraint simplification Build constraint graph Constraint resolution Unification Coercion generation and insertion θΓ ⊢ θt u : θτ Output term u, type θτ, context θΓ

slide-25
SLIDE 25

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification

  • Goal: only atomic constraints α <: β, α <: T, T <: α
slide-26
SLIDE 26

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification

  • Goal: only atomic constraints α <: β, α <: T, T <: α

σ list <: τ list ⇔ σ <: τ

slide-27
SLIDE 27

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification

  • Goal: only atomic constraints α <: β, α <: T, T <: α

σ list <: τ list ⇔ σ <: τ σ1 → σ2 <: τ1 → τ2 ⇔ τ1 <: σ1 and σ2 <: τ2

slide-28
SLIDE 28

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification

  • Goal: only atomic constraints α <: β, α <: T, T <: α

σ list <: τ list ⇔ σ <: τ σ1 → σ2 <: τ1 → τ2 ⇔ τ1 <: σ1 and σ2 <: τ2 α <: τ list ⇔ ∃α′. α . = α′ list ∧ α′ list <: τ list

slide-29
SLIDE 29

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification

  • Goal: only atomic constraints α <: β, α <: T, T <: α

σ list <: τ list ⇔ σ <: τ σ1 → σ2 <: τ1 → τ2 ⇔ τ1 <: σ1 and σ2 <: τ2 α <: τ list ⇔ ∃α′. α . = α′ list ∧ α′ list <: τ list

  • ⇒ corresponds to simplification
  • ⇐ corresponds to coercion generation
  • variances are derived from map functions
  • map :: (α → β) → α list → β list
  • λf g h. g ◦ h ◦ f ::

(β1 → α1) → (α2 → β2) → (α1 → α2) → (β1 → β2)

slide-30
SLIDE 30

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Weak unification

α <: α list ⇔ ∃α′. α . = α′ list and α′ list <: α list

slide-31
SLIDE 31

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Weak unification

α <: α list ⇔ ∃α′. α . = α′ list and α′ list <: α list ⇔ ∃α′. α . = α′ list and α′ <: α ⇔ α′ <: α′ list

slide-32
SLIDE 32

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Weak unification

α <: α list ⇔ ∃α′. α . = α′ list and α′ list <: α list ⇔ ∃α′. α . = α′ list and α′ <: α ⇔ α′ <: α′ list

  • Simplification process does not terminate
  • Not solvable with structural coercions
slide-33
SLIDE 33

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Weak unification

α <: α list ⇔ ∃α′. α . = α′ list and α′ list <: α list ⇔ ∃α′. α . = α′ list and α′ <: α ⇔ α′ <: α′ list

  • Simplification process does not terminate
  • Not solvable with structural coercions
  • Weak unification := unification after identifying all base types
  • Initial constraint set weakly unifiable ⇒ termination proof
slide-34
SLIDE 34

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification (example)

Example: leq n i

{α → α → B . = α2 → β2, β2

.

= α1 → β1, N <: α2, Z <: α1}

slide-35
SLIDE 35

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification (example)

Example: leq n i

{α → α → B . = α2 → β2, β2

.

= α1 → β1, N <: α2, Z <: α1} ⇓ {N <: α, Z <: α}

slide-36
SLIDE 36

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification (example)

Example: leq n i

{α → α → B . = α2 → β2, β2

.

= α1 → β1, N <: α2, Z <: α1} ⇓ {N <: α, Z <: α} ⇓ N α Z

Constraint graph

slide-37
SLIDE 37

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint simplification (example)

Example: leq n i

{α → α → B . = α2 → β2, β2

.

= α1 → β1, N <: α2, Z <: α1} ⇓ {N <: α, Z <: α} ⇓ N Z Z

Constraint graph

slide-38
SLIDE 38

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The subtyping pipeline

Input term t, context Γ Constraint generation Γ ⊢ t : τ S Weak unification test Constraint simplification Build constraint graph Constraint resolution Unification Coercion generation and insertion θΓ ⊢ θt u : θτ Output term u, type θτ, context θΓ

slide-39
SLIDE 39

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution

α β1 . . . βk T1 . . . Tm γ1 . . . γl U1 . . . Un

variable predecessors base type predecessors base type successors variable successors

  • Compute the intersection of sets of all supertypes of base type

predecessors of α

  • Assign α the “smallest” type from the intersection
  • Check that the assignment is subtype of all base type successors
slide-40
SLIDE 40

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution (example)

R α β N N R C

Constraint graph Partial order on base types

slide-41
SLIDE 41

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution (example)

R R β N N R C

Constraint graph Partial order on base types

  • Possibly, the algorithm assigns α the type R first
slide-42
SLIDE 42

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution (example)

R R N N N R C

Constraint graph Partial order on base types

  • Possibly, the algorithm assigns α the type R first
  • Then β is assigned the infimum of {N, R}
slide-43
SLIDE 43

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution (example)

R α β N N R C

Constraint graph Partial order on base types

  • Same constraints, different coercion declarations
slide-44
SLIDE 44

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution (example)

R R β N N R C

Constraint graph Partial order on base types

  • Same constraints, different coercion declarations
  • Then, there is no allowed assignment for β

⇒ Coercion inference fails

slide-45
SLIDE 45

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Constraint resolution (example)

R C N N N R C

Constraint graph Partial order on base types

  • Same constraints, different coercion declarations
  • Then, there is no allowed assignment for β

⇒ Coercion inference fails

  • But: {α → C, β → N} is a solution
slide-46
SLIDE 46

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

The subtyping pipeline

Input term t, context Γ Constraint generation Γ ⊢ t : τ S Weak unification test Constraint simplification Build constraint graph Constraint resolution Unification Coercion generation and insertion θΓ ⊢ θt u : θτ Output term u, type θτ, context θΓ

slide-47
SLIDE 47

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Correctness & Completeness

  • Total correctness
  • The algorithm terminates for any input t and Γ
  • The output term u has type θτ in context θΓ
slide-48
SLIDE 48

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Correctness & Completeness

  • Total correctness
  • The algorithm terminates for any input t and Γ
  • The output term u has type θτ in context θΓ
  • Completeness
  • Assumption: subtyping relation is a disjoint union of lattices
  • If t can be coerced to a well-typed term u in the context Γ,

then the algorithm will output a term u’

slide-49
SLIDE 49

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Correctness & Completeness

  • Total correctness
  • The algorithm terminates for any input t and Γ
  • The output term u has type θτ in context θΓ
  • Completeness
  • Assumption: subtyping relation is a disjoint union of lattices
  • If t can be coerced to a well-typed term u in the context Γ,

then the algorithm will output a term u’

  • Can’t guarantee u = u’
  • ⇒ refined notion of completeness
slide-50
SLIDE 50

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Ambiguity example

Example: sin (- n)

  • Signatures: sin :: R → R, - :: α → α and n :: N
  • Declared coercion: N <:real R
slide-51
SLIDE 51

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Ambiguity example

Example: sin (- n)

  • Signatures: sin :: R → R, - :: α → α and n :: N
  • Declared coercion: N <:real R
  • Two possible output terms:
  • sin (real (- n))
  • sin (- (real n))
slide-52
SLIDE 52

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Headache reduction factor

  • Necessary coercions in Hölzl’s theory

2 4 6 8 1 1061 no coercion inference

slide-53
SLIDE 53

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Headache reduction factor

  • Necessary coercions in Hölzl’s theory

2 4 6 8 1 1061 no coercion inference 666 naive

slide-54
SLIDE 54

Why coercions? A naive algorithm Constraint-based algorithm Conclusion

Headache reduction factor

  • Necessary coercions in Hölzl’s theory

2 4 6 8 1 1061 no coercion inference 666 naive 221 constraint- based

slide-55
SLIDE 55

Thank you for your attention! Questions?

slide-56
SLIDE 56

Extending Hindley-Milner Type Inference with Coercive Structural Subtyping

Dmitriy Traytel Stefan Berghofer Tobias Nipkow APLAS 2011

λ → ∀

=

Isabelle

β α

nat<:int

slide-57
SLIDE 57

Backup slides

Another ambiguity example

Example: sin (- n)

  • Signatures: sin :: R → R, - :: α → α and n :: N
  • Declared coercions: N <:int Z, Z <:real R
  • Derived coercion: N <:real ◦ int R
slide-58
SLIDE 58

Backup slides

Another ambiguity example

Example: sin (- n)

  • Signatures: sin :: R → R, - :: α → α and n :: N
  • Declared coercions: N <:int Z, Z <:real R
  • Derived coercion: N <:real ◦ int R
  • Two possible output terms:
  • sin ((real ◦ int) (- n)))
  • sin (- ((real ◦ int) n)))
  • Impossible output term:
  • sin (real (- (int n)))
slide-59
SLIDE 59

Backup slides

Coercive subtyping and let-polymorphism

Example: let f = s in u

where s ≡ λx. if x > n ∧ sin x > r then x else x and u ≡ (Suc (f n), f r)

  • Signatures: Σ(sin) = R → R, Σ(Suc) = N → N,

Σ(>) = α → α → B, Σ(n) = N and Σ(r) = R

  • Declared coercion: N <:real R
slide-60
SLIDE 60

Backup slides

Coercive subtyping and let-polymorphism

Example: let f = s in u

where s ≡ λx. if x > n ∧ sin x > r then x else x and u ≡ (Suc (f n), f r)

  • Signatures: Σ(sin) = R → R, Σ(Suc) = N → N,

Σ(>) = α → α → B, Σ(n) = N and Σ(r) = R

  • Declared coercion: N <:real R
  • Possible types for s: N → N and R → R
  • Any algorithm that only inserts coercions has to choose one type
slide-61
SLIDE 61

Backup slides

Coercive subtyping and let-polymorphism

Example: let f = s in u

where s ≡ λx. if x > n ∧ sin x > r then x else x and u ≡ (Suc (f n), f r)

  • Signatures: Σ(sin) = R → R, Σ(Suc) = N → N,

Σ(>) = α → α → B, Σ(n) = N and Σ(r) = R

  • Declared coercion: N <:real R
  • Possible types for s: N → N and R → R
  • Any algorithm that only inserts coercions has to choose one type
  • let f = s in u is not coercible either way
  • On the other hand u[s/f] can be coerced