Spine-local Type Inference Christopher Jenkins and Aaron Stump - - PowerPoint PPT Presentation

spine local type inference
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Outline

1

Background and Motivation Local Type Inference Spine-local Type Inference

2

The Specificational System Terms and Terminology Type Inference

3

Discussion Specificational System Properties Algorithmic System Properties Future Work

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 2 / 33

slide-3
SLIDE 3

Outline

1

Background and Motivation Local Type Inference Spine-local Type Inference

2

The Specificational System Terms and Terminology Type Inference

3

Discussion Specificational System Properties Algorithmic System Properties Future Work

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 3 / 33

slide-4
SLIDE 4

What is “Local Type Inference”?

Introduced by Pierce and Turner in ’98 Extended by Odersky et al. in ’01 Uses two main techniques

◮ Bidirectional typing rules: ◮ Local type-argument inference: Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 4 / 33

slide-5
SLIDE 5

What is “Local Type Inference”?

Introduced by Pierce and Turner in ’98 Extended by Odersky et al. in ’01 Uses two main techniques

◮ Bidirectional typing rules:

Synthesis mode: λ x :Nat. x ⇑ Nat → Nat Checking mode: λ x. x ⇓ Nat → Nat

◮ Local type-argument inference: Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 4 / 33

slide-6
SLIDE 6

What is “Local Type Inference”?

Introduced by Pierce and Turner in ’98 Extended by Odersky et al. in ’01 Uses two main techniques

◮ Bidirectional typing rules:

Synthesis mode: λ x :Nat. x ⇑ Nat → Nat Checking mode: λ x. x ⇓ Nat → Nat

◮ Local type-argument inference:

Let id :∀ X. X → X Type id 0 ⇑ Nat Infer X =Nat from 0 Local and Synthetic

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 4 / 33

slide-7
SLIDE 7

Why use local type inference?

It is a method of partial type inference

◮ Complete type inference: no annotations ever

(e.g. Damas-Hindley-Milner and ML)

◮ Undecidable for System F and beyond Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 5 / 33

slide-8
SLIDE 8

Why use local type inference?

It is a method of partial type inference

◮ Complete type inference: no annotations ever

(e.g. Damas-Hindley-Milner and ML)

◮ Undecidable for System F and beyond

It is user-friendly

◮ Infers many type annotations ◮ Predictable annotation requirements ◮ Better-quality error messages Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 5 / 33

slide-9
SLIDE 9

Why use local type inference?

It is a method of partial type inference

◮ Complete type inference: no annotations ever

(e.g. Damas-Hindley-Milner and ML)

◮ Undecidable for System F and beyond

It is user-friendly

◮ Infers many type annotations ◮ Predictable annotation requirements ◮ Better-quality error messages

It is implementer-friendly

◮ Relatively simple implementation ◮ Extensible: new features added without threatening decidability Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 5 / 33

slide-10
SLIDE 10

Limitations

Local type inference in its published form can sometimes still require “silly” type annotations, i.e. those for which there should be enough contextual information to omit Let pair :∀ X, Y . X → Y → X × Y Type pair (λ x. x) 0

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33

slide-11
SLIDE 11

Limitations

Local type inference in its published form can sometimes still require “silly” type annotations, i.e. those for which there should be enough contextual information to omit Let pair :∀ X, Y . X → Y → X × Y Type pair (λ x. x) 0 ⇑ ??? We do not expect to locally synthesize a type

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33

slide-12
SLIDE 12

Limitations

Local type inference in its published form can sometimes still require “silly” type annotations, i.e. those for which there should be enough contextual information to omit Let pair :∀ X, Y . X → Y → X × Y Type pair (λ x. x) 0 ⇑ ??? We do not expect to locally synthesize a type

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33

slide-13
SLIDE 13

Limitations

Local type inference in its published form can sometimes still require “silly” type annotations, i.e. those for which there should be enough contextual information to omit Let pair :∀ X, Y . X → Y → X × Y Type pair (λ x. x) 0 ⇑ ??? Type pair (λ x. x) 0 ⇓ Nat → Nat × Nat We do not expect to locally synthesize a type ... but we would expect to check it against a type

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33

slide-14
SLIDE 14

Limitations

Local type inference in its published form can sometimes still require “silly” type annotations, i.e. those for which there should be enough contextual information to omit Let pair :∀ X, Y . X → Y → X × Y Type pair (λ x. x) 0 ⇑ ??? Type pair (λ x. x) 0 ⇓ Nat → Nat × Nat We do not expect to locally synthesize a type ... but we would expect to check it against a type

◮ We could call this “contextual” type-argument inference. Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33

slide-15
SLIDE 15

Limitations

Local type inference in its published form can sometimes still require “silly” type annotations, i.e. those for which there should be enough contextual information to omit Let pair :∀ X, Y . X → Y → X × Y Type pair (λ x. x) 0 ⇑ ??? Type pair (λ x. x) 0 ⇓ Nat → Nat × Nat We do not expect to locally synthesize a type ... but we would expect to check it against a type

◮ We could call this “contextual” type-argument inference.

Unfortunately, this is not done in the two major published systems

◮ Popular “unofficial” extension (used in e.g. Scala, Rust) Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 6 / 33

slide-16
SLIDE 16

Limitations (cont.)

Usually uses “fully-uncurried” function applications

f (t1, .., tn)

◮ Maximize available info at a single application Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 7 / 33

slide-17
SLIDE 17

Limitations (cont.)

Usually uses “fully-uncurried” function applications

f (t1, .., tn)

◮ Maximize available info at a single application

Usually without partial type application (“all-or-nothing”)

f [T1, ..., Tm](t1, ..., tn)

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 7 / 33

slide-18
SLIDE 18

Our Contributions

Type inference for some expressions not typed by other variants of local type inference, by using contextual type-argument inference

t⇑⇓

1

t⇑⇓

1

Precise, specificational account of this technique Better support function currying and partial type applications by being “spine-local.”

f (, a) f (a, )

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33

slide-19
SLIDE 19

Our Contributions

Type inference for some expressions not typed by other variants of local type inference, by using contextual type-argument inference

t⇑⇓

1

f t⇑

1 t⇑ 2 t⇓ 3 ⇓ T t⇑⇓ 1

Precise, specificational account of this technique Better support function currying and partial type applications by being “spine-local.”

f (, a) f (a, )

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33

slide-20
SLIDE 20

Our Contributions

Type inference for some expressions not typed by other variants of local type inference, by using contextual type-argument inference

t⇑⇓

1

f t⇑

1 t⇓ 2 t⇓ 3 ⇓ T t⇑⇓ 1

Precise, specificational account of this technique Better support function currying and partial type applications by being “spine-local.”

f (, a) f (a, )

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33

slide-21
SLIDE 21

Our Contributions

Type inference for some expressions not typed by other variants of local type inference, by using contextual type-argument inference

t⇑⇓

1

f t⇑

1 t⇓ 2 t⇓ 3 ⇓ T t⇑⇓ 1

Precise, specificational account of this technique Better support function currying and partial type applications by being “spine-local.”

f (, a) f [S, T, V ](t1, t2, t3) f (a, )

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33

slide-22
SLIDE 22

Our Contributions

Type inference for some expressions not typed by other variants of local type inference, by using contextual type-argument inference

t⇑⇓

1

f t⇑

1 t⇓ 2 t⇓ 3 ⇓ T t⇑⇓ 1

Precise, specificational account of this technique Better support function currying and partial type applications by being “spine-local.”

f (, a) f [S][T][V ] t1 t2 t3 f (a, )

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33

slide-23
SLIDE 23

Our Contributions

Type inference for some expressions not typed by other variants of local type inference, by using contextual type-argument inference

t⇑⇓

1

f t⇑

1 t⇓ 2 t⇓ 3 ⇓ T t⇑⇓ 1

Precise, specificational account of this technique Better support function currying and partial type applications by being “spine-local.”

f (, a) f [S][T][V ] t1 t2 t3 f (a, )

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 8 / 33

slide-24
SLIDE 24

Our type system(s)

Two type systems: one specificational and one algorithmic

  • Spec. system abstracts contextual type-argument inference

◮ Non-deterministic

Sanity checks for spec. system, annotation requirements Equivalence of the two systems

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 9 / 33

slide-25
SLIDE 25

Outline

1

Background and Motivation Local Type Inference Spine-local Type Inference

2

The Specificational System Terms and Terminology Type Inference

3

Discussion Specificational System Properties Algorithmic System Properties Future Work

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 10 / 33

slide-26
SLIDE 26

Our Setting

The setting for our type inference system is (impredicative) System F Internal and external term languages:

◮ internal: all type annotations and arguments are provided ◮ external: some of these can be elided

Type inference viewed as relation between these two langauges

◮ Elaborate external internal terms Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 11 / 33

slide-27
SLIDE 27

Language Syntax

Types S, T, U, V ::= X, Y , Z | S → T | ∀ X. T | S × T Contexts Γ ::= · | Γ, X | Γ, x :T Internal Terms e, p ::= x | λ x :T. e | Λ X. e | e e′ | e[T] External Terms t ::= x | λ x :T. t | Λ X. t | t t′ | t[T]

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33

slide-28
SLIDE 28

Language Syntax

Types S, T, U, V ::= X, Y , Z | S → T | ∀ X. T | S × T Contexts Γ ::= · | Γ, X | Γ, x :T Internal Terms e, p ::= x | λ x :T. e | Λ X. e | e e′ | e[T] External Terms t ::= x | λ x :T. t | Λ X. t | t t′ | t[T] Pair types for illustration

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33

slide-29
SLIDE 29

Language Syntax

Types S, T, U, V ::= X, Y , Z | S → T | ∀ X. T | S × T Contexts Γ ::= · | Γ, X | Γ, x :T Internal Terms e, p ::= x | λ x :T. e | Λ X. e | e e′ | e[T] External Terms t ::= x | λ x :T. t | Λ X. t | t t′ | t[T] | λ x. t Pair types for illustration Will try to infer binder annotations and type arguments in external language

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33

slide-30
SLIDE 30

Language Syntax

Types S, T, U, V ::= X, Y , Z | S → T | ∀ X. T | S × T Contexts Γ ::= · | Γ, X | Γ, x :T Internal Terms e, p ::= x | λ x :T. e | Λ X. e | e e′ | e[T] External Terms t ::= x | λ x :T. t | Λ X. t | t t′ | t[T] | λ x. t Pair types for illustration Will try to infer binder annotations and type arguments in external language

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 12 / 33

slide-31
SLIDE 31

Terminology

Application head: variable or abstraction x, Λ X. t, λ x. t

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33

slide-32
SLIDE 32

Terminology

Application head: variable or abstraction x, Λ X. t, λ x. t Application spine: head followed by seq. of term, type arguments xt1 t1 t2 t3 vs (((x t1) t2) t3)

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33

slide-33
SLIDE 33

Terminology

Application head: variable or abstraction x, Λ X. t, λ x. t Application spine: head followed by seq. of term, type arguments xt1 t1 t2 t3 vs (((x t1) t2) t3) Applicand: Term in the function position of an application t1 in t1 t2

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33

slide-34
SLIDE 34

Terminology

Application head: variable or abstraction x, Λ X. t, λ x. t Application spine: head followed by seq. of term, type arguments xt1 t1 t2 t3 vs (((x t1) t2) t3) Applicand: Term in the function position of an application t1 in t1 t2 Maximal application: spine that is not an applicand Not max x t1 t2 t3 Max x t1 t2 t3

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 13 / 33

slide-35
SLIDE 35

Example – High Level Goals

Example from the intro: Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat “Under context Γ, the expression checks against the given type” (Where pair and 0 are suitably defined)

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33

slide-36
SLIDE 36

Example – High Level Goals

Example from the intro: Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat “Under context Γ, the expression checks against the given type” (Where pair and 0 are suitably defined) System will elaborate to pair[Nat → Nat][Nat] (λ x :Nat. x) 0 For illustration, example shows synthetic and contextual type-arg. inference

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33

slide-37
SLIDE 37

Example – High Level Goals

Example from the intro: Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat “Under context Γ, the expression checks against the given type” (Where pair and 0 are suitably defined) System will elaborate to pair[Nat → Nat][Nat] (λ x :Nat. x) 0 For illustration, example shows synthetic and contextual type-arg. inference

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33

slide-38
SLIDE 38

Example – High Level Goals

Example from the intro: Γ ⊢⇓ pair (λ x. x) 0 : (Nat → Nat) × Nat “Under context Γ, the expression checks against the given type” (Where pair and 0 are suitably defined) System will elaborate to pair[Nat → Nat][Nat] (λ x :Nat. x) 0 For illustration, example shows synthetic and contextual type-arg. inference ... however, elaboration clutters the rules, so omitted for the example

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 14 / 33

slide-39
SLIDE 39

Spine Judgment Γ ⊢P t : T σ

“Spine t partially synthesizes type T with contextual type-args. σ” Big idea: enforce locality, contextuality at maximal applications

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 15 / 33

slide-40
SLIDE 40

Spine Judgment Γ ⊢P t : T σ

“Spine t partially synthesizes type T with contextual type-args. σ” Big idea: enforce locality, contextuality at maximal applications

cage meta-variables to just the spine with spine judgment (locality)

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 15 / 33

slide-41
SLIDE 41

Spine Judgment Γ ⊢P t : T σ

“Spine t partially synthesizes type T with contextual type-args. σ” Big idea: enforce locality, contextuality at maximal applications

cage meta-variables to just the spine with spine judgment (locality)

◮ require meta-variable “guesses” justified contextuality Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 15 / 33

slide-42
SLIDE 42

Spine Judgment (Ex.)

Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X]

Γ∀ ⊢PDummy Γ∀ ⊢PDummy

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33

slide-43
SLIDE 43

Spine Judgment (Ex.)

Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X]

Base case: synthesize type for head Γ∀ ⊢PDummy Γ ⊢⇑ pair : ∀ X,Y . X → Y → X × Y Γ∀ ⊢PDummy

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33

slide-44
SLIDE 44

Spine Judgment (Ex.)

Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X]

Begin walking up spine Γ∀ ⊢PDummy Γ ⊢P pair : ∀ X,Y . X → Y → X × Y σid (σid is identity subst.) Γ∀ ⊢PDummy

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33

slide-45
SLIDE 45

Spine Judgment (Ex.)

Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X]

Encounter term app. with missing type arg. Γ∀ ⊢PDummy Γ ⊢P pair : ∀ X,Y . X → Y → X × Y σid (σid is identity subst.) Γ∀ ⊢PDummy

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33

slide-46
SLIDE 46

Spine Judgment (Ex.)

Γ ⊢P pair (λ x. x) 0 : X × Nat [Nat → Nat/X]

Defer to last judgment form: application judgment Γ∀ ⊢PDummy Γ ⊢· (∀ X,Y . X → Y → X × Y , σid) · (λ x. x) : Y → X × Y [Nat → Nat/X]

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 16 / 33

slide-47
SLIDE 47

Application Judgment Γ ⊢· (T, σ) · t : T ′ σ′

“An applicand of type T with ctxt. solutions σ can be applied to argument t, producing result type T ′ and result ctxt. solutions σ′” Infer missing type-args in term apps., synthetically and contextually Type application when arrow revealed

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 17 / 33

slide-48
SLIDE 48

Application Judgment Γ ⊢· (T, σ) · t : T ′ σ′

“An applicand of type T with ctxt. solutions σ can be applied to argument t, producing result type T ′ and result ctxt. solutions σ′” Infer missing type-args in term apps., synthetically and contextually

◮ the whether and what of contextual inference is non-deterministic

Type application when arrow revealed

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 17 / 33

slide-49
SLIDE 49

Application Judgment (Ctx.)

Γ ⊢· (∀ 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

slide-50
SLIDE 50

Application Judgment (Ctx.)

Γ ⊢· (∀ 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

slide-51
SLIDE 51

Application Judgment (Ctx.)

Γ ⊢· (∀ 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

slide-52
SLIDE 52

Application Judgment (Ctx.)

Γ ⊢· (∀ 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

slide-53
SLIDE 53

Application Judgment (Ctx.)

Γ ⊢· (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

slide-54
SLIDE 54

Application Judgment (Ctx.)

Γ ⊢· (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

slide-55
SLIDE 55

Application Judgment (Arrow)

Two cases arise when we reveal an arrow.

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 19 / 33

slide-56
SLIDE 56

Application Judgment (Arrow)

Two cases arise when we reveal an arrow. Expected type of arg. is fully known (from spine head, contextual type, previous arguments) Use checking mode for arg.

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 19 / 33

slide-57
SLIDE 57

Application Judgment (Arrow)

Two cases arise when we reveal an arrow. Expected type of arg. is fully known (from spine head, contextual type, previous arguments) Use checking mode for arg. Expected type has unsolved meta-vars Use synthesis mode for arg. to learn instantiations

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 19 / 33

slide-58
SLIDE 58

Application Judgment (Ctx)

Γ ⊢· (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

slide-59
SLIDE 59

Application Judgment (Ctx)

Γ ⊢· (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

slide-60
SLIDE 60

Application Judgment (Ctx)

Γ ⊢· (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

slide-61
SLIDE 61

Application Judgment (Syn)

Last part of the spine judgment is typing pair (λ x. x) to 0 We defer again to application judgment Y will be inferred synthetically from 0

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 21 / 33

slide-62
SLIDE 62

Application Judgment (Syn)

Γ ⊢· (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

slide-63
SLIDE 63

Application Judgment (Syn)

Γ ⊢· (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

slide-64
SLIDE 64

Application Judgment (Syn)

Γ ⊢· (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

slide-65
SLIDE 65

Application Judgment (Syn)

Γ ⊢· (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

slide-66
SLIDE 66

Application Judgment (Syn)

Γ ⊢· (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

slide-67
SLIDE 67

Application Judgment (Syn)

Γ ⊢· (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

slide-68
SLIDE 68

Enforcement at Maximal Application

Γ ⊢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

Earlier I said “enforce locality, contextuality...” how?

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33

slide-69
SLIDE 69

Enforcement at Maximal Application

Γ ⊢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

Earlier I said “enforce locality, contextuality...” how? All remaining meta-variables are solved by σ MV (Γ, T): meta-vars of T wrt declared variables of Γ

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33

slide-70
SLIDE 70

Enforcement at Maximal Application

Γ ⊢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

Earlier I said “enforce locality, contextuality...” how? All remaining meta-variables are solved by σ MV (Γ, T): meta-vars of T wrt declared variables of Γ Contextual solutions really are contextual

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33

slide-71
SLIDE 71

Enforcement at Maximal Application

Γ ⊢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

Earlier I said “enforce locality, contextuality...” how? All remaining meta-variables are solved by σ MV (Γ, T): meta-vars of T wrt declared variables of Γ Contextual solutions really are contextual We clear these conditions and can type the expression

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 23 / 33

slide-72
SLIDE 72

Outline

1

Background and Motivation Local Type Inference Spine-local Type Inference

2

The Specificational System Terms and Terminology Type Inference

3

Discussion Specificational System Properties Algorithmic System Properties Future Work

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 24 / 33

slide-73
SLIDE 73

Specificational System Properties

Sanity check wrt. internal language (System F; Γ ⊢ t : T)

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 25 / 33

slide-74
SLIDE 74

Specificational System Properties

Sanity check wrt. internal language (System F; Γ ⊢ t : T) Soundness: Γ ⊢δ t : T e implies Γ ⊢ e : T Trivial completeness: Γ ⊢ e : T implies Γ ⊢⇑ e : T e

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 25 / 33

slide-75
SLIDE 75

Specificational System Properties (cont.)

Typeability of the external language (i.e. type annotation requirements) Assume Γ ⊢ e : T. Erase binder, type args to get external term t. Γ ⊢⇑ t : T e when given

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 26 / 33

slide-76
SLIDE 76

Specificational System Properties (cont.)

Typeability of the external language (i.e. type annotation requirements) Assume Γ ⊢ e : T. Erase binder, type args to get external term t. Γ ⊢⇑ t : T e when given

◮ Binder annotations to λs when its context or spine-context lack this

info

◮ Instantiations for “phantom” type-arguments

∀ X,Y . X → X

◮ Enough info to “see” a term or type application

e.g. applicand of type X given [S] or t

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 26 / 33

slide-77
SLIDE 77

Algorithmic system

“Prototypes” track expected result type, num args to spine head ? →? → Nat

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 27 / 33

slide-78
SLIDE 78

Algorithmic system

“Prototypes” track expected result type, num args to spine head ? →? → Nat Matched against head type, produces a “decorated” function type ∀ X =Nat. ∀ Y =Y . X → Y → X

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 27 / 33

slide-79
SLIDE 79

Prototype Matching (Ex. 1)

Check pair (λ x. x) 0 against (Nat → Nat) × Nat

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 28 / 33

slide-80
SLIDE 80

Prototype Matching (Ex. 1)

Check pair (λ x. x) 0 against (Nat → Nat) × Nat

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

slide-81
SLIDE 81

Prototype Matching (Ex. 1)

Check pair (λ x. x) 0 against (Nat → Nat) × Nat

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

slide-82
SLIDE 82

Prototype Matching (Ex. 1)

Check pair (λ x. x) 0 against (Nat → Nat) × Nat

Prototype: ? → ? → (Nat → Nat)× Nat Head type: ∀ X. ∀ Y . X → Y → X× Y Decoration: ∀ X =Nat → Nat. ∀ Y =Nat. X → Y → X× Y

No “guessing” for contextual type-args.

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 28 / 33

slide-83
SLIDE 83

Prototype Matching (Ex. 2)

Careful handling needed when prototype arity exceeds the spine head’s

Check id suc 0 against type Nat

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33

slide-84
SLIDE 84

Prototype Matching (Ex. 2)

Careful handling needed when prototype arity exceeds the spine head’s

Check id suc 0 against type Nat

Prototype: ? → ? → Nat Head type: ∀ X . X → X

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33

slide-85
SLIDE 85

Prototype Matching (Ex. 2)

Careful handling needed when prototype arity exceeds the spine head’s

Check id suc 0 against type Nat

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

slide-86
SLIDE 86

Prototype Matching (Ex. 2)

Careful handling needed when prototype arity exceeds the spine head’s

Check id suc 0 against type Nat

Prototype: ? → ? → Nat Head type: ∀ X . X → X Decoration: ∀ X =X . X → (X, ? → Nat)

Don’t know how to instantiate X, save for later

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33

slide-87
SLIDE 87

Prototype Matching (Ex. 2)

Careful handling needed when prototype arity exceeds the spine head’s

Check id suc 0 against type Nat

Prototype: ? → ? → Nat Head type: ∀ X . X → X Decoration: ∀ X =X . X → (X, ? → Nat)

Don’t know how to instantiate X, save for later From synthesis instantiate X, then compare

Match Nat → Nat with ? → Nat once we reach first arg. suc

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 29 / 33

slide-88
SLIDE 88

Algorithmic Systems Properties

Γ δ t : T e Algorithmic: The system is given as a set of syntax-directed inference rules Equivalent to Specification:

◮ Soundness:

Γ δ t : T e implies Γ ⊢δ t : T e

◮ Completeness:

Γ ⊢δ t : T e implies Γ δ t : T e

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 30 / 33

slide-89
SLIDE 89

Algorithmic Systems Properties

Γ δ t : T e Algorithmic: The system is given as a set of syntax-directed inference rules Equivalent to Specification:

◮ Soundness:

Γ δ t : T e implies Γ ⊢δ t : T e

◮ Completeness:

Γ ⊢δ t : T e implies Γ δ t : T e

... even though we never mentioned prototype matching or “stuck” decorations in the spec!

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 30 / 33

slide-90
SLIDE 90

Algorithmic System Properties

Γ ⊢P t : T σ Γ ⊢· (T, σ) · t : T ′ σ′ ≡ Γ; P ? t : W σ Γ · (W , σ) · t : W ′ σ′ X := T :=P ⇒ (W , σ)

  • Spec. system
  • Alg. system

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 31 / 33

slide-91
SLIDE 91

Algorithmic System Properties

Γ ⊢P t : T σ Γ ⊢· (T, σ) · t : T ′ σ′ ≡ Γ; P ? t : W σ Γ · (W , σ) · t : W ′ σ′ X := T :=P ⇒ (W , σ)

  • Spec. system
  • Alg. system

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 31 / 33

slide-92
SLIDE 92

Future Work

Type inference algorithm is implemented in Cedille, a language with impredicativity, dependent types, and dependent intersections. A local type inference system will be a good foundation for considering the following extensions:

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 32 / 33

slide-93
SLIDE 93

Future Work

Type inference algorithm is implemented in Cedille, a language with impredicativity, dependent types, and dependent intersections. A local type inference system will be a good foundation for considering the following extensions: partial type propagation a la “Colored Local Type Inference” higher-order type inference using matching inference for erased term arguments (Cedille feature) subsumption based on some form of “type containment”

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 32 / 33

slide-94
SLIDE 94

Thanks!

Questions?

Jenkins, Stump (CS, U. Iowa) Spine-local Type Inference IFL ’18 33 / 33