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

48
Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

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

Page 1: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

Strict Bidirectional Type Checking

Adam Chlipala, Leaf Petersen, and Robert Harper

Page 2: 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

Page 3: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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)

Page 4: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 4

Simply Typed Calculus

Page 5: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 5

Terms

Types

1 0

Page 6: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 6

Terms

Types

1 0

2 3

Page 7: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 7

Terms

Types

1 0

2 3

3 8

Page 8: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 8

Terms

Types

1 0

2 3

3 8

4 15

Page 9: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 9

Terms

Types

1 0

2 3

3 8

4 15

… …

i i2-1

Quadratic!

Page 10: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 10

Algorithmic view

Inputs are black.Outputs are red.

Page 11: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 11

Algorithmic view

Inputs are black.Outputs are red.

Redundant

Redundant

Page 12: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 12

Algorithmic view

Inputs are black.Outputs are red.

Page 13: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 13

Algorithmic view

Inputs are black.Outputs are red.

Redundant

Page 14: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 14

Bidirectional Typechecking

Synthesis: Typed terms.

Analysis: Checkable terms.

Page 15: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 15

Terms

Types

1 1

Page 16: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 16

Terms

Types

1 1

2 3

Page 17: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 17

Terms

Types

1 1

2 3

3 5

Page 18: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 18

Terms

Types

1 1

2 3

3 5

4 7

Page 19: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 19

Terms

Types

1 1

2 3

3 5

4 7

… …

i 2i-1

Linear!

Page 20: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 20

Unnecessary!

Much simpler technique works fine.

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

Page 21: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 21

Recursive types.

Page 22: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 22

Simple term

Page 23: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 23

Page 24: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 24

Fold Annotations Only

Page 25: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 25

With Sum Annotations

Page 26: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 26

Bidirectional version

Page 27: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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)

Page 28: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 28

Binding forms

• Analysis binding:

• Synthesis binding:

Page 29: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 29

Page 30: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 30

Page 31: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 31

Page 32: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 32

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

Page 33: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 33

Binding forms (revisited)

• Analysis binding:–

• Synthesis binding:–

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

Page 34: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 34

Page 35: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 35

Page 36: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 36

Page 37: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 37

Can we typecheck it?

Page 38: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 38

Page 39: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 39

Page 40: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 40

Page 41: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

1/10/05 41

Page 42: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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.

Page 43: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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!!

Page 44: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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.

Page 45: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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.

Page 46: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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.

Page 47: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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.

Page 48: Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.

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.