Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015

852 views

Published on

Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015

Published in: Software
  • Be the first to comment

  • Be the first to like this

Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015

  1. 1. Let's Write a Type Checker Ionut G. Stan — I T. A.K. — May 2015
  2. 2. The Plan - Part1 G Compilers Overview ~ Type Checking vs Type Inference - Vehicle Language c Wand's Type Inference Algorithm ~ Part 2 ® Live Demonstration
  3. 3. Compilers Overview
  4. 4. Compilers Overview
  5. 5. Compilers Overview Source Code _ T)» Comp]_]_er T)»
  6. 6. Compilers Overview Source Code _ T)» Comp]_]_er T)» (fn a = > a) 2
  7. 7. Compilers Overview Source Code _ Target Language TI» Compller T)’ (fn a = > a) 2
  8. 8. Compilers Overview Source Code _ Target Language TI» Compller T)’ (fn a = > a) 2 (function(a){return a; })(2);
  9. 9. Compilers Overview Source Code _ Target Language TI‘ Compller T)‘ (fn a = > a) 2 (function(a){return a; })(2); a Interpreter T
  10. 10. Compilers Overview Source Code _ Target Language TI‘ Compller T)‘ (fn a = > a) 2 (function(a){return a; })(2); Source Code a Interpreter T
  11. 11. Compilers Overview Source Code _ Target Language T)‘ Compller T)‘ (fn a = > a) 2 (function(a){return a; })(2); Source Code T) Interpreter T) (fn a = > a) 2
  12. 12. Compilers Overview Source Code _ Target Language T)‘ Comp1ler T)‘ (fn a = > a) 2 (function(a){return a; })(2); Source Code Evaluation Result T) Interpreter T) (fn a = > a) 2
  13. 13. Compilers Overview Source Code _ Target Language T)‘ Compiler T)‘ (fn a = > a) 2 (function(a){return a; })(2); Source Code Evaluation Result T) Interpreter T) (fn a = > a) 2 2
  14. 14. Compilers Overview
  15. 15. Parsing Compiler ‘ Parser
  16. 16. Abstract Syntax Tree
  17. 17. Abstract Syntax Tree Compiler Abstract Syntax Tree (AST) APP XX FUN INT / /” | a VAR 2 8.
  18. 18. Code Generation Compiler Abstract Syntax Tree (AST) Parser T» APP XX FUN INT / /” | a VAR 2 l 8.
  19. 19. Many Intermediate Phases
  20. 20. Type Checking Compiler Type Checker ~
  21. 21. Today's Talk Compiler 7 . é_. ___. . _ , ., _- 3:: 2.. .--. -.__ Tbdagfi ii d W Type Chec ke r -lrarwpe AST : I it it . =—» V ' ‘ae——v ‘ ‘~5- , -_. . . _—_~- _ _ 7.4-.
  22. 22. Type Checking VS Type Inference
  23. 23. Type Checking vs Inference - Type Checking » Ensures declared types are used consistently % All types must be declared a Traverse AST and compare def site with use site a Type Inference 6 Ensures consistency as well ~ Types need not be declared, though; are deduced « Two main classes of algorithms e We'll see one instance today
  24. 24. Vehicle Language
  25. 25. Vehicle Language ~ Surface Syntax - What's the concrete syntax of the language 6 Type System e What types are supported by the language
  26. 26. Surface Syntax 1. Numbers: 1, 2, 3,
  27. 27. Surface Syntax 1. Numbers: 1, 2, 3, 2. Booleans: true and false
  28. 28. Surface Syntax 1. Numbers: 1, 2, 3, 2. Booleans: true and false 3. Anonymous functions (lambdas): fn a = > a
  29. 29. Surface Syntax 1. Numbers: 1, 2, 3, 2. Booleans: true and false 3. Anonymous functions (lambdas): fn a = > a 4. Function application: inc 42
  30. 30. Surface Syntax 1. Numbers: 1, 2, 3, 2. Booleans: true and false 3. Anonymous functions (lambdas): fn a = > a 4. Function application: inc 42 5. If expressions: if cond then t else f
  31. 31. Surface Syntax 6. Let blocks/ expressions: let val name = .. . in name end
  32. 32. Small Example let val inc = fn a = > a + 1 in inc 42 end
  33. 33. Language Types 1. Integer type: int
  34. 34. Language Types 1. Integer type: int 2. Boolean type: bool
  35. 35. Language Types 1. Integer type: int 2. Boolean type: bool 3. Function type: int —> bool
  36. 36. Language Types . Integer type: int . Boolean type: bool . Function type: int —> bool . Generic type variables: 'a, 'b, 'c, etc.
  37. 37. Today's Algorithm Overview
  38. 38. Wand's Algorithm Overview
  39. 39. Wand's Algorithm Overview . AST T C t ‘t C t 't Parsing j) ype. _) ons rain _) ons rain Annotation Generation Solving
  40. 40. Wand s Algorithm Overview fn isZero = > if isZero 1 then 2 else 3
  41. 41. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > / if isZero 1 iszero IF then 2 -> / y APP INT INT else 3 / I 1 VAR INT 2 3 isZero 1
  42. 42. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > ' if isZero 1 iszero IF then 2 -> / y APP INT INT else 3 / I I VAR INT 2 3 isZero 1
  43. 43. Wand's Algorithm Overview AST ' ' Parsing Type. _) Constraint _) Constraint Annotation Generation Solving FUN fn{isZerol= > if isZero 1 IF -> % then 2 APP INT INT else 3 / / I I VAR I NT 2 3 isZero 1
  44. 44. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > if isZero 1 then 2 else 3 isZero APP INT INT -> VAR INT 2 3 isZero 1
  45. 45. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > / /// //A if isZero 13 iflmw N then 2 3) APP INT INT else 3 I I VAR INT 2 3 isZero 1
  46. 46. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > / if isZero 1 iszero IF then 2 -> / y APP INT INT else 3 I I I ‘VAR INT 2 3 isZero 1
  47. 47. Wand's Algorithm Overview AST ' ' Parsing Type. _) Constraint _) Constraint Annotation Generation Solving FUN fn isZero = > / /// //A if 152910 [1] isZero IF then 2 ->‘ / /// /T APP INT INT else 3 I I V2 3 l isZero 1
  48. 48. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > / if isZero 1 iszero IF then[:2] -> APP INT INT else 3 / I VAR INT 2 3 isZero 1
  49. 49. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > / if isZero 1 iszero IF then 2 -> APP INT INT else[:3] / I VAR INT 2 3 isZero 1
  50. 50. Wand's Algorithm Overview Parsin AST, _ Type _> Constraint _> Constraint g Annotation Generation Solving FUN fn isZero = > / if isZero 1 iszero IF then 2 -> / y APP INT INT else 3 / I I VAR INT 2 3 isZero 1
  51. 51. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving FUN IF iszeroh IFQ , »// /// //{ / /// /// //l APP INT INT App“ INTt6 INTt7 . R INT 2 3 VAR“ INTt5 2 3 ero 1 isZero 1
  52. 52. Wand's Algorithm Overview AST ' ' Parsing Type Constraint Constraint Annotation I Generation Solving FUN IF APP INT INT Appm TNTII6 TNTII7 / I l i i . R INT 2 3 V U INT“ 2 3 | I ero 1 isZero 1
  53. 53. Wand's Algorithm Overview Parsing fl) Type Annotation Generation Solving Constraint Constraint FUNQ Constraint Set iszeroh IFQ APPM INTQ INTW / //A I I VAR“ INT% 2 8 l l isZero 1
  54. 54. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving FUNt2 Constraint Set / t1Et59t4 isZerot1 IFt3
  55. 55. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving FUNQ Constraint Set 111 1:5 9 1:4 t5 int isZerot1 IFt3
  56. 56. Wand's Algorithm Overview Constraint _ Constraint Parsing 3 Type . Annotation Generation Solving FUNQ Constraint Set t1 E t5 9 t4 t5 E int bool iszeroh t4 VAR“ INT, ,5 2 3 l l isZero 1
  57. 57. Wand's Algorithm Overview Constraint _ Constraint Parsing 3 Type . Annotation Generation Solving FUNQ Constraint Set t1 E t5 9 t4 t5 E int iszeroh IFQ t4 E bool t6 int APPM INTH / //T VAR“ INT“ l l isZero 1
  58. 58. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving FUNQ Constraint Set t1 5 t5 9 t4 t5 5 int iszeroh IFQ t4 E bool t6 5 int -> . t7 5 int APP“ INTm / //“ VAR“ INT% 2 l l isZero 1
  59. 59. Wand's Algorithm Overview Constraint _ Constraint Parsing 3 Type . Annotation Generation Solving FUNQ Constraint Set t1 E t5 9 t4 t5 E int iszeroh IFQ t4 E bool E t6 E int t7 E int APPI4 INTt6' INTI, t6 5 t3 A I VARII INT, ,5 2 3 isZero 1
  60. 60. Wand's Algorithm Overview Constraint _ Constraint Parsing 3 Type . Annotation Generation Solving FUNQ Constraint Set t1 E t5 9 t4 t5 E int iszeroh IFQ t4 E bool E t6 E int t7 E int APP“ INTt6 E / I t7 2 t3 VARII INT, ,5 2 3 isZero 1
  61. 61. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving FUNI2 Constraint Set t1 E t5 9 t4 t5 5 int iszeroh IFQ t4 E bool t6 5 int flI ) t7 5 int APPI4 INTI6 INTI, t6 2 t3 / I l t7 2 t3 VARI, INTI, 2 3 t2 2 t1 9 t3 isZero 1
  62. 62. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving FUNI2 Constraint Set t1 E t5 9 t4 t5 5 int isZero“ IFQ t4 E bool t6 5 int flI ) t7 5 int APPI4 INTI6 INTI, t6 2 t3 / I I t7 2 t3 VARI, INTI5 2 3 t2 2 t1 e t3 isZero 1
  63. 63. Wand's Algorithm Overview Constraint Constraint Parsing 3 Type Annotation Generation Solving Constraint Set Solution Map t1Et59t4 t5 5 int t4 5 bool t6 5 int t7 5 int t6Et3 t7Et3 ‘C2 t19t3
  64. 64. Wand's Algorithm Overview Constraint Constraint Parsing 3 Type Annotation Generation Solving Constraint Set Solution Map t5 5 int t1: t5 9 t4 t4 5 bool t6 5 int t7 5 int t6Et3 t7Et3 t2 t19t3
  65. 65. Wand's Algorithm Overview Constraint Constraint Parsing 3 Type Annotation Generation Solving Constraint Set Solution Map t5 5 int t1: t5 9 t4 t4 5 bool t6 5 int t7 5 int t6Et3 t7Et3 t2 2 (t5 9 t4) 9 t3
  66. 66. Wand's Algorithm Overview Constraint Constraint Parsing 3 Type Annotation Generation Solving Constraint Set Solution Map t4 5 bool t1: t5 9 t4 t6 5 int t5: int t7 5 int t6Et3 t7Et3 t2 2 (t5 9 t4) 9 t3
  67. 67. Wand's Algorithm Overview Constraint Constraint Parsing 3 Type Annotation Generation Solving Constraint Set Solution Map t4 5 bool t1: int 9 t4 t6 5 int t5: int t7 5 int t6Et3 t7Et3 t2 2 (Int 9 t4) 9 t3
  68. 68. Wand's Algorithm Overview Constraint Constraint Parsing 3 Type Annotation Generation Solving Constraint Set Solution Map t6 5 int t1: int 9 t4 t7 5 int t5: int t6 5 t3 t4: bool t7Et3 t2 2 (int 9 t4) 9 t3
  69. 69. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map t6 2 int t1: int 9 bool t7 2 int t5: int t6 2 t3 t4: bool t72t3 t2 (int 9 bool) 9 t3
  70. 70. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map t7 2 int t1: int 9 bool 116 E t3 t5: int t7 2 t3 t4: bool t2 2 (int 9 bool) 9 t3 t6: int
  71. 71. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map t7 2 int t1: int 9 bool int 2 t3 t5: int t7 2 t3 t4: bool t2 2 (int 9 bool) 9 t3 t6: int
  72. 72. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map int 2 t3 t1: int 9 bool t7 5 t3 t5: int t2 2 (int 9 bool) 9 t3 t4: bool t6: int t7: int
  73. 73. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map int 2 t3 t1: int 9 bool int 2 t3 t5: int t2 2 (int 9 bool) 9 t3 t4: bool t6: int t7: int
  74. 74. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map int 2 t3 t1: int 9 bool t2 2 (int 9 bool) 9 t3 t5: int t4: bool t6: int t7: int t3: int
  75. 75. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map int 2 int t1: int 9 bool t2 2 (int 9 bool) 9 int t5: int t4: bool t6: int t7: int t3: int
  76. 76. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map int 2 int t1: int 9 bool t2 2 (int 9 bool) 9 int t5: int t4: bool t6: int t7: int t3: int
  77. 77. Wand's Algorithm Overview _ AST Parsing —> Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map t2 2 (int 9 bool) 9 int t1: int 9 bool t5: int t4: bool t6: int 117: int t3: int
  78. 78. Wand's Algorithm Overview AST Parsing Type Constraint Constraint Annotation Generation Solving Constraint Set Solution Map t1: int 9 bool t5: int t4: bool t6: int t7: int t3: int t2: (int 9 bool) 9 int
  79. 79. Parsing Solution Map t1: t5: t4: t6: t7: t3: t2: Wand's Algorithm Overview AST Type Annotation int 9 bool int bool int int int (int 9 bool) 9 int isZero“ Constraint Generation APP“ / //T VAR“ I isZero INT“ I 1 Constraint Solving INT“ I 2 INT“ I 3
  80. 80. Wand s Algorithm Overview fn isZero = > if isZero 1 then 2 ->» (int —> bool) —> int else 3
  81. 81. Type Checking Algorithms - As I said, there are two main classes - Constraint—based ones. We've just seen one example — Wand's algorithm - Substitution—based ones, where constraint generation and solving are not two separate processes, they are interleaved. Example: the classic Hindley—Milner Algorithm W.
  82. 82. Live Demonstration
  83. 83. htt s: ithub. comi stan itake—2015
  84. 84. Thank You!
  85. 85. Resources Sunil Kothari and James L. Caldwell. Type Reconstruction Algorithms — A Survey Mitchell Wand. A simple algorithm and proof for type inference. Bastiaan Heeren, Jurriaan Hage and Doaitse Swierstra. Generalizing HindIev—Milner Tvpe Inference Algorithms Oleg Kiselyov and Chung-chieh Shan. Interpreting Tvpes as Abstract Values Shriram Krishnamurthi. Programming Langies: Application and Interpretation chapter 15 Shriram Krishnamurthi. Programming Langfiges: Application and Interpretation lecture 24 Shriram Krishnamurthi. Programming Langies: Application and Interpretation lecture 25 Bastiaan Heeren. Top Quality Type Error Messages Stephen Diehl. Write You a Haskell chapter 6 Andrew Appel. Modern Compiler Implementation in ML chapter 16 Benjamin Pierce. Tvpes and Programming Langfiges chapter 22 Martin Odersky. §caia by Example chapter 16 Danny Gratzer. htt s: ithub. com 'ozef hm Arlen Cox. ML Type Inference and Unification Radu Rugina. £23 312 Type Inference

×