Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper

Preview:

Citation preview

Strict Bidirectional Type Checking

Adam Chlipala, Leaf Petersen, and Robert Harper

1/10/05 2

Type Systems

Less Type Annotation More Type Annotation

ML

•Concise syntax.•Compact representation.•Complex (global) type reconstruction problem.•Undecidable past a certain point.

•Verbose syntax.•Representation issues.•Simple or no type reconstruction.•Powerful type systems are still decidable.

Haskell JavaTILT IL

1/10/05 3

Type Systems

Less Annotation More Annotation

ML

•Concise (enough) syntax.•Compact representation.•Simple syntax directed typechecking.•Powerful and decidable type systems.

Haskell JavaTILT IL

Pierce, Turner (POPL

‘98)

1/10/05 4

Simply Typed Calculus

1/10/05 5

Terms

Types

1 0

1/10/05 6

Terms

Types

1 0

2 3

1/10/05 7

Terms

Types

1 0

2 3

3 8

1/10/05 8

Terms

Types

1 0

2 3

3 8

4 15

1/10/05 9

Terms

Types

1 0

2 3

3 8

4 15

… …

i i2-1

Quadratic!

1/10/05 10

Algorithmic view

Inputs are black.Outputs are red.

1/10/05 11

Algorithmic view

Inputs are black.Outputs are red.

Redundant

Redundant

1/10/05 12

Algorithmic view

Inputs are black.Outputs are red.

1/10/05 13

Algorithmic view

Inputs are black.Outputs are red.

Redundant

1/10/05 14

Bidirectional Typechecking

Synthesis: Typed terms.

Analysis: Checkable terms.

1/10/05 15

Terms

Types

1 1

1/10/05 16

Terms

Types

1 1

2 3

1/10/05 17

Terms

Types

1 1

2 3

3 5

1/10/05 18

Terms

Types

1 1

2 3

3 5

4 7

1/10/05 19

Terms

Types

1 1

2 3

3 5

4 7

… …

i 2i-1

Linear!

1/10/05 20

Unnecessary!

Much simpler technique works fine.

• Good asymptotic behavior.• Syntax directed.• Extends directly to sum types.

1/10/05 21

Recursive types.

1/10/05 22

Simple term

1/10/05 23

1/10/05 24

Fold Annotations Only

1/10/05 25

With Sum Annotations

1/10/05 26

Bidirectional version

1/10/05 27

Let Binding

• Surprisingly, bidirectional typechecking interacts badly with let binding.– Quadratic increase in annotation size in

bad cases.

• Bad - programmers use let.• Bad - compilers use let.

– Put code in “named form” (sequentialization)

1/10/05 28

Binding forms

• Analysis binding:

• Synthesis binding:

1/10/05 29

1/10/05 30

1/10/05 31

1/10/05 32

Sequentializing bidirectional terms may lead to quadratic increase in type size!

1/10/05 33

Binding forms (revisited)

• Analysis binding:–

• Synthesis binding:–

• Undecorated analysis binding?– – Might solve our problem.

1/10/05 34

1/10/05 35

1/10/05 36

1/10/05 37

Can we typecheck it?

1/10/05 38

1/10/05 39

1/10/05 40

1/10/05 41

1/10/05 42

Key points: • Typecheck body first.• Context of occurrence gives a unique type to z0 because it occurs in an analysis position.

1/10/05 43

Algorithmically Speaking

• Divide context into input and output zones.

• Input zone variables must declare types.– Explicitly:– Implicitly:

• Output zone variables get types from occurrences:– – Check A after getting type for u from E.– Must ensure variables actually occur!!

1/10/05 44

Formally

• Look to strict logic – Substructural logic in which hypotheses must

be used at least once.

• Normal variables in unrestricted context ( may occur zero or more times.

• Strict variables in restricted context ( must occur at least once.

• Contrast with linear logic:– Linear variables in restricted context () must

occur exactly once.

1/10/05 45

Strict Bidirectional Typechecking

• Judgments:– Analysis:– Synthesis:

• Unrestricted context ( is input mapping variables to their types.

• Restricted context ( is output mapping variables to their types.

• Strict variables must occur in analysis positions.

1/10/05 46

Cool Fact #1

Theorem: The strict bidirectional typechecking rules constitute an algorithm.– Syntax directed.– Uniquely determined outputs.– Proof is in paper.

1/10/05 47

Cool Fact #2

Theorem: Sequentialization into the strict language never increases type size.– Size of sequentialized form is bounded

above by the size of the original.– Proof is in paper.

1/10/05 48

Recap

• Bidirectional representation yields asymptotic improvements in type size.

• Let binding destroys these benefits.

• Recognizing strictness allows benefits to be recaptured without unification.