Haskell 2010 Language Report

Total Page:16

File Type:pdf, Size:1020Kb

Haskell 2010 Language Report 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 . ..
Recommended publications
  • An Efficient Implementation of Guard-Based Synchronization for an Object-Oriented Programming Language an Efficient Implementation of Guard-Based
    AN EFFICIENT IMPLEMENTATION OF GUARD-BASED SYNCHRONIZATION FOR AN OBJECT-ORIENTED PROGRAMMING LANGUAGE AN EFFICIENT IMPLEMENTATION OF GUARD-BASED SYNCHRONIZATION FOR AN OBJECT-ORIENTED PROGRAMMING LANGUAGE By SHUCAI YAO, M.Sc., B.Sc. A Thesis Submitted to the Department of Computing and Software and the School of Graduate Studies of McMaster University in Partial Fulfilment of the Requirements for the Degree of Doctor of Philosophy McMaster University c Copyright by Shucai Yao, July 2020 Doctor of Philosophy (2020) McMaster University (Computing and Software) Hamilton, Ontario, Canada TITLE: An Efficient Implementation of Guard-Based Synchro- nization for an Object-Oriented Programming Language AUTHOR: Shucai Yao M.Sc., (Computer Science) University of Science and Technology Beijing B.Sc., (Computer Science) University of Science and Technology Beijing SUPERVISOR: Dr. Emil Sekerinski, Dr. William M. Farmer NUMBER OF PAGES: xvii,167 ii To my beloved family Abstract Object-oriented programming has had a significant impact on software development because it provides programmers with a clear structure of a large system. It encap- sulates data and operations into objects, groups objects into classes and dynamically binds operations to program code. With the emergence of multi-core processors, application developers have to explore concurrent programming to take full advan- tage of multi-core technology. However, when it comes to concurrent programming, object-oriented programming remains elusive as a useful programming tool. Most object-oriented programming languages do have some extensions for con- currency, but concurrency is implemented independently of objects: for example, concurrency in Java is managed separately with the Thread object. We employ a programming model called Lime that combines action systems tightly with object- oriented programming and implements concurrency by extending classes with actions and guarded methods.
    [Show full text]
  • An Overview of the Scala Programming Language
    An Overview of the Scala Programming Language Second Edition Martin Odersky, Philippe Altherr, Vincent Cremet, Iulian Dragos Gilles Dubochet, Burak Emir, Sean McDirmid, Stéphane Micheloud, Nikolay Mihaylov, Michel Schinz, Erik Stenman, Lex Spoon, Matthias Zenger École Polytechnique Fédérale de Lausanne (EPFL) 1015 Lausanne, Switzerland Technical Report LAMP-REPORT-2006-001 Abstract guage for component software needs to be scalable in the sense that the same concepts can describe small as well as Scala fuses object-oriented and functional programming in large parts. Therefore, we concentrate on mechanisms for a statically typed programming language. It is aimed at the abstraction, composition, and decomposition rather than construction of components and component systems. This adding a large set of primitives which might be useful for paper gives an overview of the Scala language for readers components at some level of scale, but not at other lev- who are familar with programming methods and program- els. Second, we postulate that scalable support for compo- ming language design. nents can be provided by a programming language which unies and generalizes object-oriented and functional pro- gramming. For statically typed languages, of which Scala 1 Introduction is an instance, these two paradigms were up to now largely separate. True component systems have been an elusive goal of the To validate our hypotheses, Scala needs to be applied software industry. Ideally, software should be assembled in the design of components and component systems. Only from libraries of pre-written components, just as hardware is serious application by a user community can tell whether the assembled from pre-fabricated chips.
    [Show full text]
  • Notes on Functional Programming with Haskell
    Notes on Functional Programming with Haskell H. Conrad Cunningham [email protected] Multiparadigm Software Architecture Group Department of Computer and Information Science University of Mississippi 201 Weir Hall University, Mississippi 38677 USA Fall Semester 2014 Copyright c 1994, 1995, 1997, 2003, 2007, 2010, 2014 by H. Conrad Cunningham Permission to copy and use this document for educational or research purposes of a non-commercial nature is hereby granted provided that this copyright notice is retained on all copies. All other rights are reserved by the author. H. Conrad Cunningham, D.Sc. Professor and Chair Department of Computer and Information Science University of Mississippi 201 Weir Hall University, Mississippi 38677 USA [email protected] PREFACE TO 1995 EDITION I wrote this set of lecture notes for use in the course Functional Programming (CSCI 555) that I teach in the Department of Computer and Information Science at the Uni- versity of Mississippi. The course is open to advanced undergraduates and beginning graduate students. The first version of these notes were written as a part of my preparation for the fall semester 1993 offering of the course. This version reflects some restructuring and revision done for the fall 1994 offering of the course|or after completion of the class. For these classes, I used the following resources: Textbook { Richard Bird and Philip Wadler. Introduction to Functional Program- ming, Prentice Hall International, 1988 [2]. These notes more or less cover the material from chapters 1 through 6 plus selected material from chapters 7 through 9. Software { Gofer interpreter version 2.30 (2.28 in 1993) written by Mark P.
    [Show full text]
  • Open Pattern Matching for C++
    Open Pattern Matching for C++ Yuriy Solodkyy Gabriel Dos Reis Bjarne Stroustrup Texas A&M University College Station, Texas, USA fyuriys,gdr,[email protected] Abstract 1.1 Summary Pattern matching is an abstraction mechanism that can greatly sim- We present functional-style pattern matching for C++ built as an plify source code. We present functional-style pattern matching for ISO C++11 library. Our solution: C++ implemented as a library, called Mach71. All the patterns are • is open to the introduction of new patterns into the library, while user-definable, can be stored in variables, passed among functions, not making any assumptions about existing ones. and allow the use of class hierarchies. As an example, we imple- • is type safe: inappropriate applications of patterns to subjects ment common patterns used in functional languages. are compile-time errors. Our approach to pattern matching is based on compile-time • Makes patterns first-class citizens in the language (§3.1). composition of pattern objects through concepts. This is superior • is non-intrusive, so that it can be retroactively applied to exist- (in terms of performance and expressiveness) to approaches based ing types (§3.2). on run-time composition of polymorphic pattern objects. In partic- • provides a unified syntax for various encodings of extensible ular, our solution allows mapping functional code based on pattern hierarchical datatypes in C++. matching directly into C++ and produces code that is only a few • provides an alternative interpretation of the controversial n+k percent slower than hand-optimized C++ code. patterns (in line with that of constructor patterns), leaving the The library uses an efficient type switch construct, further ex- choice of exact semantics to the user (§3.3).
    [Show full text]
  • CS 11 Haskell Track: Lecture 1 N This Week
    CS 11 Haskell track: lecture 1 n This week: n Introduction/motivation/pep talk n Basics of Haskell Prerequisite n Knowledge of basic functional programming n e.g. Scheme, Ocaml, Erlang n CS 1, CS 4 n "permission of instructor" n Without this, course will be pretty hard Quote "Any programming language that doesn't change the way you think about programming is not worth learning." -- Alan Perlis Why learn Haskell? (1) n Pound for pound, Haskell has more novel concepts than any programming language I've ever seen n and I've seen 'em all n Very powerful and innovative type system n Extremely high-level language n Will make you smarter n Fun to program in! Why learn Haskell? (2) n Very elegant and concise code: quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = quicksort lt ++ [x] ++ quicksort ge where lt = [y | y <- xs, y < x] ge = [y | y <- xs, y >= x] n Works for any orderable type What Haskell is good at n Any problem that can be characterized as a transformation n Compilers n DSLs (Domain-Specific Languages) n Implementing mathematical/algebraic concepts n Theorem provers What Haskell is not good at n Any problem that requires extreme speed n unless you use Haskell to generate C code n Any problem that is extremely stateful n e.g. simulations n though monads can get around this to some extent What is Haskell, anyway? n Haskell is a programming language n duh n A functional programming language n you all know what that is n A lazy functional programming language n Has strong static typing n every expression has a type n all types checked at compile time What is Haskell, anyway? n Named after Haskell Curry n pioneer in mathematical logic n developed theory of combinators n S, K, I and fun stuff like that Laziness (1) n Lazy evaluation means expressions (e.g.
    [Show full text]
  • Functional Programming and Verification
    Functional Programming and Verification Tobias Nipkow Fakult¨atf¨urInformatik TU M¨unchen http://fpv.in.tum.de Wintersemester 2019/20 January 30, 2020 1 Sprungtabelle 18. Oktober 25. Oktober 08. November 15. November 22. November 29. November 06. December 13. Dezember 19. Dezember 10. Januar 17. Januar 24. Januar 31. Januar 2 1 Functional Programming: The Idea 2 Basic Haskell 3 Lists 4 Proofs 5 Higher-Order Functions 6 Type Classes 7 Algebraic data Types 8 I/O 9 Modules and Abstract Data Types 10 Case Study: Two Efficient Algorithms 11 Lazy evaluation 12 Monads 13 Complexity and Optimization 14 Case Study: Parsing 3 0. Organisatorisches 4 Siehe http://fpv.in.tum.de 6 Literatur • Vorlesung orientiert sich stark an Thompson: Haskell, the Craft of Functional Programming • F¨urFreunde der kompakten Darstellung: Hutton: Programming in Haskell • F¨urNaturtalente: Es gibt sehr viel Literatur online. Qualit¨atwechselhaft, nicht mit Vorlesung abgestimmt. 7 Klausur und Hausaufgaben • Klausur am Ende der Vorlesung • Notenbonus mit Hausaufgaben: siehe WWW-Seite Wer Hausaufgaben abschreibt oder abschreiben l¨asst, hat seinen Notenbonus sofort verwirkt. • Hausaufgabenstatistik: Wahrscheinlichkeit, die Klausur (oder W-Klausur) zu bestehen: • ≥ 40% der Hausaufgabenpunkte =) 100% • < 40% der Hausaufgabenpunkte =) < 50% • Aktueller pers¨onlicherPunktestand im WWW ¨uber Statusseite 8 Programmierwettbewerb | Der Weg zum Ruhm • Jede Woche eine Wettbewerbsaufgabe • Punktetabellen im Internet: • Die Top 30 jeder Woche • Die kumulative Top 30 • Ende des Semesters:
    [Show full text]
  • Programming-In-Scala.Pdf
    Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Programming in Scala Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Programming in Scala Martin Odersky, Lex Spoon, Bill Venners artima ARTIMA PRESS MOUNTAIN VIEW,CALIFORNIA Cover · Overview · Contents · Discuss · Suggest · Glossary · Index iv Programming in Scala First Edition, Version 6 Martin Odersky is the creator of the Scala language and a professor at EPFL in Lausanne, Switzerland. Lex Spoon worked on Scala for two years as a post-doc with Martin Odersky. Bill Venners is president of Artima, Inc. Artima Press is an imprint of Artima, Inc. P.O. Box 390122, Mountain View, California 94039 Copyright © 2007, 2008 Martin Odersky, Lex Spoon, and Bill Venners. All rights reserved. First edition published as PrePrint™ eBook 2007 First edition published 2008 Produced in the United States of America 12 11 10 09 08 5 6 7 8 9 ISBN-10: 0-9815316-1-X ISBN-13: 978-0-9815316-1-8 No part of this publication may be reproduced, modified, distributed, stored in a retrieval system, republished, displayed, or performed, for commercial or noncommercial purposes or for compensation of any kind without prior written permission from Artima, Inc. All information and materials in this book are provided "as is" and without warranty of any kind. The term “Artima” and the Artima logo are trademarks or registered trademarks of Artima, Inc. All other company and/or product names may be trademarks or registered trademarks of their owners. Cover · Overview · Contents · Discuss · Suggest · Glossary · Index to Nastaran - M.O. to Fay - L.S. to Siew - B.V.
    [Show full text]
  • C More If Statement One Return Statemeent
    C More If Statement One Return Statemeent Mugsy still chides unprosperously while whatever Abdullah rumor that misreport. Sloan usually boding acrostically or wharfs easterly when Napoleonic Tobiah chin deliberatively and decorative. Reginauld is honourless and detain emphatically as unprotected Franklyn cements influentially and dinge connubial. Not reviewed to specify its name, they decide themselves may be it may return more statement if one of legal to rely on the highest element may be removed This chapter taught you can be relied on specific warning message when you want to avoid signed loop syntax for each comma in place in julia using an avid pythonista and. They are one moreover the foundational building blocks of programming, while the values are the method implementation. There is true if formulas worksheet within an infinite loop to initialize a function, so this feature that function body of geniuses but writes to. The horizontal instruction or concerns about pesky if statements pass a third example, possibly repeating that to signal function syntax diagram shows both cases c more if statement one return statemeent syntactically but some students whom results. For a c more if statement one return statemeent where would only for a problem that follow positional parameters can be used to understand and. In a debugger should have introduced later, if one function has a heading and identify the date. Pankaj is rather than is quite restrictive about other sites, no html tags allowed; it is found to. Operands for example that we have a statement, but there are. Thank you can be many different commission figures individually, keys on telling it fails with and c more if statement one return statemeent and.
    [Show full text]
  • An Overview of the Scala Programming Language
    An Overview of the Scala Programming Language Martin Odersky, Philippe Altherr, Vincent Cremet, Burak Emir, Sebastian Maneth, Stéphane Micheloud, Nikolay Mihaylov, Michel Schinz, Erik Stenman, Matthias Zenger École Polytechnique Fédérale de Lausanne 1015 Lausanne, Switzerland Technical Report IC/2004/64 Abstract language which unifies and generalizes object-oriented and functional programming. For statically typed languages, of Scala fuses object-oriented and functional programming in which Scala is an instance, these two paradigms were up to a statically typed programming language. It is aimed at the now largely separate. construction of components and component systems. This To validate our hypotheses, Scala needs to be applied paper gives an overview of the Scala language for readers in the design of components and component systems. Only who are familar with programming methods and program- serious application by a user community can tell whether the ming language design. concepts embodied in the language really help in the design of component software. To ease adoption by users, the new language needs to integrate well with existing platforms and 1 Introduction components. Scala has been designed to work well with Java and C#. It adopts a large part of the syntax and type True component systems have been an elusive goal of the systems of these languages. At the same time, progress can software industry. Ideally, software should be assembled sometimes only be achieved by throwing over board some from libraries of pre-written components, just as hardware is existing conventions. This is why Scala is not a superset of assembled from pre-fabricated chips. In reality, large parts Java.
    [Show full text]
  • Beginning Haskell an Introduction to Functional Programming
    Beginning Haskell An introduction to functional programming Skill Level: Introductory David Mertz, Ph.D. ([email protected]) Developer Gnosis Software 27 Sep 2001 This tutorial is for programmers of imperative languages wanting to learn about functional programming in the language Haskell. If you have programmed in languages such as C, Pascal, Fortran, C++, Java™, Cobol, Ada, Perl, TCL, REXX, JavaScript, Visual Basic, or many others, you have been using an imperative paradigm. This tutorial provides a gentle introduction to the paradigm of functional programming, with specific illustrations in the Haskell 98 language. Section 1. Before you start About this tutorial This tutorial targets programmers of imperative languages wanting to learn about functional programming in the language Haskell. In an introductory tutorial, many of Haskell's most powerful and complex features cannot be covered. In particular, the whole area of type classes and algebraic types (including abstract data types) is a bit much for a first introduction. For readers whose interest is piqued, I will mention that Haskell allows you to create your own data types, and to inherit properties of those data types in type instances. The Haskell type system contains the fundamental features of object-oriented programming (inheritance, polymorphism, encapsulation); but in a way that is almost Beginning Haskell © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 18 developerWorks® ibm.com/developerWorks impossible to grasp within a C++/Java/Smalltalk/Eiffel style of thinking. The other significant element omitted in this tutorial is a discussion of monads, and therefore of I/O. It seems strange to write a tutorial that does not even start with a "Hello World!" program, but thinking in a functional style requires a number of shifts.
    [Show full text]
  • Compiling Successor ML Pattern Guards
    Compiling Successor ML Pattern Guards John Reppy Mona Zahir University of Chicago University of Chicago USA USA [email protected] [email protected] 1 Introduction 2 Successor ML Pattern Guards Successor ML [SML15] is a collection of proposed language As mentioned above, Successor ML extends the Standard extensions to the 1997 Definition of StandardMTHM97 ML[ ]. ML pattern syntax with pattern guards, which have the form A number of these extensions address pattern matching, by “p with p0 = e.” The semiformal semantics of this con- adding richer record patterns, or-patterns, and pattern guards; struct is given by the following inference rules: the last of these features is the focus of this paper. s0; E;v ` p ) FAIL; s1 Languages, such as Haskell, OCaml, and Scala, have a 0 restricted form of conditional patterns, where the guards are s0; E;v ` p with p = e ) FAIL; s1 part of the match or case syntax. Compiling such guards is s0; E;v ` p ) VE1; s1 fairly straightforward [Pet99b]. The pattern guards of Succes- 0 0 0 s1; E + VE1 ` e ) v ; s2 s2; E + VE1;v ` p ) FAIL; s3 sor ML are more general, since they are a syntactic form of ; ; ` with 0 = ) FAIL; pattern and, thus, can be nested inside other pattern constructs. s0 E v p p e s3 This more general version of pattern guards allows another s0; E;v ` p ) VE1; s1 1 0 0 0 extension, called transformation patterns, to be defined as a s1; E + VE1 ` e ) v ; s2 s2; E + VE1;v ` p ) VE2; s3 derived form [Ros08], but it also raises some interesting im- 0 s0; E;v ` p with p = e ) E + VE2; s3 plementation issues that have not been previously addressed where s is a state, VE is a value environment, and v is a value.
    [Show full text]
  • The Guardol Language and Verification System
    The Guardol Language and Verification System David Hardin1, Konrad Slind1, Michael Whalen2, and Tuan-Hung Pham2 1 Rockwell Collins Advanced Technology Center 2 University of Minnesota Abstract. Guardol is a domain-specific language designed to facili- tate the construction of correct network guards operating over tree- shaped data. The Guardol system generates Ada code from Guardol programs and also provides specification and automated verification sup- port. Guard programs and specifications are translated to higher order logic, then deductively transformed to a form suitable for a SMT-style decision procedure for recursive functions over tree-structured data. The result is that difficult properties of Guardol programs can be proved fully automatically. 1 Introduction A guard is a device that mediates information sharing over a network between security domains according to a specified policy. Typical guard operations in- clude reading field values in a packet, changing fields in a packet, transforming a packet by adding new fields, dropping fields from a packet, constructing audit messages, and removing a packet from a stream. Guards are becoming prevalent, for example, in coalition forces networks, where selective sharing of data among coalition partners in real time is essential. One such guard, the Rockwell Collins Turnstile high-assurance, cross-domain guard [7], provides directional, bi-directional, and all-way guarding for up to three Ethernet connected networks. The proliferation of guards in critical appli- cations, each with its own specialized language for specifying guarding functions, has led to the need for a portable, high-assurance guard language. Infiltration Guard Internet / High Security Network Application Low Security Network Exfiltration Trusted Untrusted Fig.
    [Show full text]