Spine-local Type Inference
Christopher Jenkins and Aaron Stump
Computer Science University of Iowa
IFL ’18
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 1 / 33
Spine-local Type Inference Christopher Jenkins and Aaron Stump - - PowerPoint PPT Presentation
Spine-local Type Inference Christopher Jenkins and Aaron Stump Computer Science University of Iowa IFL 18 Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL 18 1 / 33 Outline Background and Motivation 1 Local Type
Computer Science University of Iowa
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 1 / 33
1
2
3
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 2 / 33
1
2
3
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 3 / 33
◮ Bidirectional typing rules: ◮ Local type-argument inference: Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 4 / 33
◮ Bidirectional typing rules:
◮ Local type-argument inference: Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 4 / 33
◮ Bidirectional typing rules:
◮ Local type-argument inference:
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 4 / 33
◮ Complete type inference: no annotations ever
◮ Undecidable for System F and beyond Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 5 / 33
◮ Complete type inference: no annotations ever
◮ Undecidable for System F and beyond
◮ Infers many type annotations ◮ Predictable annotation requirements ◮ Better-quality error messages Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 5 / 33
◮ Complete type inference: no annotations ever
◮ Undecidable for System F and beyond
◮ Infers many type annotations ◮ Predictable annotation requirements ◮ Better-quality error messages
◮ Relatively simple implementation ◮ Extensible: new features added without threatening decidability Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 5 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33
◮ We could call this “contextual” type-argument inference. Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33
◮ We could call this “contextual” type-argument inference.
◮ Popular “unofficial” extension (used in e.g. Scala, Rust) Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33
◮ Maximize available info at a single application Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 7 / 33
◮ Maximize available info at a single application
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 7 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33
◮ Non-deterministic
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 9 / 33
1
2
3
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 10 / 33
◮ internal: all type annotations and arguments are provided ◮ external: some of these can be elided
◮ Elaborate external internal terms Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 11 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 15 / 33
◮
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 15 / 33
◮
◮ require meta-variable “guesses” justified contextuality Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 15 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 17 / 33
◮ the whether and what of contextual inference is non-deterministic
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 17 / 33
Γ ⊢· (∀ X,Y . X → Y → X × Y , σid) · (λ x. x) : Y → X × Y [Nat → Nat/X] Γ∀ ⊢PD Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 18 / 33
Γ ⊢· (∀ X,Y . X → Y → X × Y , σid) · (λ x. x) : Y → X × Y [Nat → Nat/X] Make a contextual guess for X, Nat → Nat Γ∀ ⊢PD Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 18 / 33
Γ ⊢· (∀ Y . X → Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] Γ∀ ⊢PD Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 18 / 33
Γ ⊢· (∀ Y . X → Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] Non-deterministically choose to instantiate Y synthetically Γ∀ ⊢PD Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 18 / 33
Γ ⊢· (X →Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] ∀ Γ∀ ⊢PD Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 18 / 33
Γ ⊢· (X →Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] ∀ Reveal an arrow in applicand type Γ∀ ⊢PD Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 18 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 19 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 19 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 19 / 33
Γ ⊢· (X →Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] Γ∀ ⊢P⊢⇓D Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 20 / 33
Γ ⊢· (X →Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] Type is fully known: Γ ⊢⇓ λ x. x : Nat → Nat Γ∀ ⊢P⊢⇓D Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 20 / 33
Γ ⊢· (X →Y → X × Y , [Nat → Nat/X]) · (λ x. x) : Y → X × Y [Nat → Nat/X] Produced result type of the app, with ctxt. solution Γ∀ ⊢P⊢⇓D Γ∀ ⊢PD
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 20 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 21 / 33
Γ ⊢· (Y →X × Y , [Nat → Nat/X]) · 0 : X × Nat [Nat → Nat/X] Γ∀ ⊢P⊢⇓D Γ∀ ⊢P⊢⇑D
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 22 / 33
Γ ⊢· (Y →X × Y , [Nat → Nat/X]) · 0 : X × Nat [Nat → Nat/X] Arrow revealed Γ∀ ⊢P⊢⇓D Γ∀ ⊢P⊢⇑D
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 22 / 33
Γ ⊢· (Y →X × Y , [Nat → Nat/X]) · 0 : X × Nat [Nat → Nat/X] Incomplete info. for expected arg. type Y Γ∀ ⊢P⊢⇓D Γ∀ ⊢P⊢⇑D
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 22 / 33
Γ ⊢· (Y →X × Y , [Nat → Nat/X]) · 0 : X × Nat [Nat → Nat/X] Synthesize type for arg. (note Y not passed down!) Γ∀ ⊢P⊢⇓D Γ ⊢⇑ 0 : Nat Γ∀ ⊢P⊢⇑D
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 22 / 33
Γ ⊢· (Y →X × Y , [Nat → Nat/X]) · 0 : X × Nat [Nat → Nat/X] Must match expectation Y , provide instantiation [Nat/Y ] Γ∀ ⊢P⊢⇓D Γ ⊢⇑ 0 : [Nat/Y ]Y Γ∀ ⊢P⊢⇑D
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 22 / 33
Γ ⊢· (Y → X × Y , [Nat → Nat/X]) · 0 : X × [Nat/Y ] Y [Nat → Nat/X] Use syn. type-arg in result type of app Γ∀ ⊢P⊢⇓D Γ∀ ⊢P⊢⇑D
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 22 / 33
Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X] dom([Nat → Nat/X])=X =MV (Γ, X × Nat) [Nat → Nat/X] (X × Nat)=(Nat → Nat) × Nat Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33
Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X] dom([Nat → Nat/X])=X =MV (Γ, X × Nat) [Nat → Nat/X] (X × Nat)=(Nat → Nat) × Nat Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33
Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X] dom([Nat → Nat/X])=X =MV (Γ, X × Nat) [Nat → Nat/X] (X × Nat)=(Nat → Nat) × Nat Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33
Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X] dom([Nat → Nat/X])=X =MV (Γ, X × Nat) [Nat → Nat/X] (X × Nat)=(Nat → Nat) × Nat Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33
1
2
3
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 24 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 25 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 25 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 26 / 33
◮ Binder annotations to λs when its context or spine-context lack this
◮ Instantiations for “phantom” type-arguments
◮ Enough info to “see” a term or type application
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 26 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 27 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 27 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 28 / 33
Prototype: ? → ? → (Nat → Nat)× Nat Head type: ∀ X. ∀ Y . X → Y → X× Y
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 28 / 33
Prototype: ? → ? → (Nat → Nat)× Nat Head type: ∀ X. ∀ Y . X → Y → X× Y Decoration: ∀ X =Nat → Nat. ∀ Y =Nat. X → Y → X× Y
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 28 / 33
Prototype: ? → ? → (Nat → Nat)× Nat Head type: ∀ X. ∀ Y . X → Y → X× Y Decoration: ∀ X =Nat → Nat. ∀ Y =Nat. X → Y → X× Y
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 28 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33
Prototype: ? → ? → Nat Head type: ∀ X . X → X
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33
Prototype: ? → ? → Nat Head type: ∀ X . X → X Decoration: ∀ X =X . X → (X, ? → Nat)
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33
Prototype: ? → ? → Nat Head type: ∀ X . X → X Decoration: ∀ X =X . X → (X, ? → Nat)
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33
Prototype: ? → ? → Nat Head type: ∀ X . X → X Decoration: ∀ X =X . X → (X, ? → Nat)
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33
◮ Soundness:
◮ Completeness:
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 30 / 33
◮ Soundness:
◮ Completeness:
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 30 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 31 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 31 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 32 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 32 / 33
Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 33 / 33