
Write You a Haskell Building a modern functional compiler from first principles Stephen Diehl January 2015 (Draft) Write You a Haskell by Stephen Diehl Copyright © 2014-2016. www.stephendiehl.com is written work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. You may reproduce and edit this work with attribution for all non-commercial purposes. e included source is released under the terms of the MIT License. Git commit: 4e913157bb4ffb7900cd25ef88445acff642ece2 Contents Introduction 7 Goals ............................................... 7 Prerequisites ............................................ 7 Concepts 8 Functional Languages ...................................... 8 Static Typing ........................................... 9 Functional Compilers ...................................... 10 Parsing .............................................. 11 Desugaring ............................................ 12 Type Inference .......................................... 12 Transformation .......................................... 13 Code Generation ......................................... 13 Haskell Basics 15 Functions ............................................. 15 Datatypes ............................................. 16 Values ............................................... 16 Pattern matching ......................................... 17 Recursion ............................................. 18 Laziness .............................................. 19 Higher-Kinded Types ....................................... 20 Typeclasses ............................................ 20 Operators ............................................. 21 Monads .............................................. 21 Applicatives ............................................ 23 Monoids ............................................. 23 Deriving .............................................. 24 IO ................................................. 24 Monad Transformers ....................................... 25 1 Text ................................................ 30 Cabal & Stack .......................................... 30 Resources ............................................. 31 Parsing 32 Parser Combinators ........................................ 32 NanoParsec ............................................ 32 Parsec ............................................... 38 Evaluation ............................................. 41 REPL ............................................... 42 Soundness ............................................. 43 Full Source ............................................ 44 Lambda Calculus 45 SKI Combinators ......................................... 47 Implementation .......................................... 47 Substitution ............................................ 48 Conversion and Equivalences .................................. 49 Reduction ............................................. 50 Let ................................................. 51 Everything Can Be a λ term ................................... 51 Recursion ............................................. 52 Pretty Printing .......................................... 54 Full Source ............................................ 55 Type Systems 56 Rules ............................................... 56 Type Safety ............................................ 57 Types ............................................... 58 Small-Step Semantics ....................................... 59 Observations ........................................... 63 Simply Typed Lambda Calculus ................................. 64 2 Type Checker ........................................... 64 Evaluation ............................................. 67 Observations ........................................... 67 Notation Reference ........................................ 68 Full Source ............................................ 69 Evaluation 70 Evaluation Models ........................................ 70 Call-by-value ........................................... 71 Call-by-name ........................................... 72 Call-by-need ........................................... 73 Higher Order Abstract Syntax (HOAS) ............................. 74 Parametric Higher Order Abstract Syntax (PHOAS) ...................... 76 Embedding IO .......................................... 78 Full Source ............................................ 79 Hindley-Milner Inference 81 Syntax ............................................... 82 Polymorphism .......................................... 83 Types ............................................... 83 Context .............................................. 84 Inference Monad ......................................... 85 Substitution ............................................ 85 Unification ............................................ 87 Generalization and Instantiation ................................. 89 Typing Rules ........................................... 90 Constraint Generation ...................................... 94 Typing ............................................... 95 Constraint Solver ......................................... 96 Worked Examples ........................................ 97 Interpreter ............................................. 99 Interactive Shell .......................................... 100 3 Observations ........................................... 103 Full Source ............................................ 105 Design of ProtoHaskell 106 Haskell: A Rich Language .................................... 106 Scope ............................................... 107 Intermediate Forms ........................................ 108 Compiler Monad ......................................... 109 Engineering Overview 110 REPL ............................................... 110 Parser ............................................... 112 Renamer ............................................. 113 Datatypes ............................................. 113 Desugaring ............................................ 113 Core ................................................ 116 Type Classes ............................................ 117 Type Checker ........................................... 118 Interpreter ............................................. 119 Error Reporting .......................................... 119 Frontend 119 Data Declarations ........................................ 122 Function Declarations ...................................... 123 Fixity Declarations ........................................ 124 Typeclass Declarations ...................................... 125 Wired-in Types .......................................... 126 Traversals ............................................. 127 Misc Infrastructure 129 Repline .............................................. 129 Command Line Arguments ................................... 129 GraphSCC ............................................ 129 4 Optparse Applicative ....................................... 129 Full Source ............................................ 129 Resources ............................................. 130 Extended Parser 131 Toolchain ............................................. 131 Alex ................................................ 133 Happy ............................................... 134 Syntax Errors ........................................... 136 Type Error Provenance ...................................... 137 Indentation ............................................ 140 Extensible Operators ....................................... 142 Full Source ............................................ 145 Resources ............................................. 145 Datatypes 146 Algebraic data types ....................................... 146 Isorecursive Types ......................................... 147 Memory Layout ......................................... 148 Pattern Scrutiny ......................................... 149 Syntax ............................................... 151 GHC.Generics .......................................... 151 Wadler’s Algorithm ........................................ 152 Full Source ............................................ 153 Renamer 154 Renaming Pass .......................................... 154 Full Source ............................................ 154 LLVM 155 JIT Compilation ......................................... 155 LLVM ............................................... 175 Types ............................................... 176 5 Variables ............................................. 178 Instructions ............................................ 178 Data ................................................ 179 Blocks ............................................... 179 Control Flow ........................................... 180 Calls ................................................ 188 Memory .............................................. 188 GetElementPtr .......................................... 189 Casts ............................................... 189 Toolchain ............................................. 189 llvm-general ............................................ 191 Code Generation (LLVM) 191 Resources ............................................. 191 6 Introduction Goals Off we go on our Adventure in Haskell Compilers! It will be intense, long, informative, and hopefully fun. It’s important to stress several points about the goals before we start
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages193 Page
-
File Size-