Haskell 2010 Language Report
Total Page:16
File Type:pdf, Size:1020Kb
Haskell 2010 Language Report Simon Marlow (editor) Copyright notice. The authors and publisher intend this Report to belong to the entire Haskell community, and grant permission to copy and distribute it for any purpose, provided that it is reproduced in its entirety, including this Notice. Modified versions of this Report may also be copied and distributed for any purpose, provided that the mod- ified version is clearly presented as such, and that it does not claim to be a definition of the language Haskell 2010. Contents I The Haskell 2010 Language 1 1 Introduction 3 1.1 Program Structure . .3 1.2 The Haskell Kernel . .4 1.3 Values and Types . .4 1.4 Namespaces . .4 2 Lexical Structure 7 2.1 Notational Conventions . .7 2.2 Lexical Program Structure . .8 2.3 Comments . .9 2.4 Identifiers and Operators . .9 2.5 Numeric Literals . 11 2.6 Character and String Literals . 11 2.7 Layout . 12 3 Expressions 15 3.1 Errors . 16 3.2 Variables, Constructors, Operators, and Literals . 17 3.3 Curried Applications and Lambda Abstractions . 18 3.4 Operator Applications . 18 3.5 Sections . 19 i ii CONTENTS 3.6 Conditionals . 19 3.7 Lists . 20 3.8 Tuples . 20 3.9 Unit Expressions and Parenthesized Expressions . 21 3.10 Arithmetic Sequences . 21 3.11 List Comprehensions . 21 3.12 Let Expressions . 22 3.13 Case Expressions . 23 3.14 Do Expressions . 25 3.15 Datatypes with Field Labels . 25 3.15.1 Field Selection . 26 3.15.2 Construction Using Field Labels . 26 3.15.3 Updates Using Field Labels . 27 3.16 Expression Type-Signatures . 28 3.17 Pattern Matching . 28 3.17.1 Patterns . 28 3.17.2 Informal Semantics of Pattern Matching . 29 3.17.3 Formal Semantics of Pattern Matching . 31 4 Declarations and Bindings 35 4.1 Overview of Types and Classes . 36 4.1.1 Kinds . 37 4.1.2 Syntax of Types . 37 4.1.3 Syntax of Class Assertions and Contexts . 39 4.1.4 Semantics of Types and Classes . 39 4.2 User-Defined Datatypes . 40 4.2.1 Algebraic Datatype Declarations . 40 4.2.2 Type Synonym Declarations . 42 4.2.3 Datatype Renamings . 43 CONTENTS iii 4.3 Type Classes and Overloading . 44 4.3.1 Class Declarations . 44 4.3.2 Instance Declarations . 45 4.3.3 Derived Instances . 47 4.3.4 Ambiguous Types, and Defaults for Overloaded Numeric Operations . 48 4.4 Nested Declarations . 49 4.4.1 Type Signatures . 49 4.4.2 Fixity Declarations . 50 4.4.3 Function and Pattern Bindings . 51 4.4.3.1 Function bindings . 52 4.4.3.2 Pattern bindings . 53 4.5 Static Semantics of Function and Pattern Bindings . 53 4.5.1 Dependency Analysis . 54 4.5.2 Generalization . 54 4.5.3 Context Reduction Errors . 55 4.5.4 Monomorphism . 56 4.5.5 The Monomorphism Restriction . 56 4.6 Kind Inference . 58 5 Modules 61 5.1 Module Structure . 62 5.2 Export Lists . 63 5.3 Import Declarations . 64 5.3.1 What is imported . 65 5.3.2 Qualified import . 65 5.3.3 Local aliases . 66 5.3.4 Examples . 66 5.4 Importing and Exporting Instance Declarations . 67 5.5 Name Clashes and Closure . 67 iv CONTENTS 5.5.1 Qualified names . 67 5.5.2 Name clashes . 68 5.5.3 Closure . 69 5.6 Standard Prelude . 70 5.6.1 The Prelude Module . 70 5.6.2 Shadowing Prelude Names . 70 5.7 Separate Compilation . 71 5.8 Abstract Datatypes . 71 6 Predefined Types and Classes 73 6.1 Standard Haskell Types . 73 6.1.1 Booleans . 73 6.1.2 Characters and Strings . 73 6.1.3 Lists . 74 6.1.4 Tuples . 74 6.1.5 The Unit Datatype . 74 6.1.6 Function Types . 74 6.1.7 The IO and IOError Types . 75 6.1.8 Other Types . ..