Report On The Programming Language
Report On The Programming Language
net/publication/2743675
CITATIONS READS
109 4,096
13 authors, including:
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by John Peterson on 22 February 2013.
Lennart Augustsson 3
Brian Boutel 4
Warren Burton 5
Joseph Fasel 6
Andrew D. Gordon 7
John Hughes 3
Paul Hudak 1
Thomas Johnsson 3
Mark Jones 9
Erik Meijer 10
Alastair Reid 1
Philip Wadler 11
Contents
1 Introduction 1
1.1 Program Structure : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1
1.2 The Haskell Kernel : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2
1.3 Values and Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2
1.4 Namespaces : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2
1.5 Layout : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3
2 Lexical Structure 6
2.1 Notational Conventions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6
2.2 Lexical Program Structure : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6
2.3 Identiers and Operators : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8
2.4 Numeric Literals : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9
2.5 Character and String Literals : : : : : : : : : : : : : : : : : : : : : : : : : : 10
3 Expressions 11
3.1 Errors : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 13
3.2 Variables, Constructors, and Operators : : : : : : : : : : : : : : : : : : : : : 14
3.3 Curried Applications and Lambda Abstractions : : : : : : : : : : : : : : : : 15
3.4 Operator Applications : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 15
3.5 Sections : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 16
3.6 Conditionals : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 16
3.7 Lists : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 16
3.8 Tuples : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 17
3.9 Unit Expressions and Parenthesized Expressions : : : : : : : : : : : : : : : 17
3.10 Arithmetic Sequences : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 17
3.11 List Comprehensions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 18
3.12 Let Expressions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 19
3.13 Case Expressions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 20
3.14 Do Expressions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 21
3.15 Datatypes with Field Labels : : : : : : : : : : : : : : : : : : : : : : : : : : : 22
3.16 Expression Type-Signatures : : : : : : : : : : : : : : : : : : : : : : : : : : : 24
3.17 Pattern Matching : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 24
4 Declarations and Bindings 31
4.1 Overview of Types and Classes : : : : : : : : : : : : : : : : : : : : : : : : : 31
4.2 User-Dened Datatypes : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 36
4.3 Type Classes and Overloading : : : : : : : : : : : : : : : : : : : : : : : : : : 41
4.4 Nested Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 45
4.5 Static Semantics of Function and Pattern Bindings : : : : : : : : : : : : : : 48
4.6 Kind Inference : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 53
ii CONTENTS
5 Modules 55
5.1 Module Structure : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 55
5.2 Closure : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 60
5.3 Standard Prelude : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 60
5.4 Separate Compilation : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 62
5.5 Abstract Datatypes : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 62
5.6 Fixity Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 62
6 Predened Types and Classes 64
6.1 Standard Haskell Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 64
6.2 Standard Haskell Classes : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 66
6.3 Numbers : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 72
7 Basic Input/Output 78
7.1 Standard I/O Functions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 78
7.2 Sequencing I/O Operations : : : : : : : : : : : : : : : : : : : : : : : : : : : 80
7.3 Exception Handling in the I/O Monad : : : : : : : : : : : : : : : : : : : : : 80
A Standard Prelude 82
A.1 Prelude PreludeList : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 96
A.2 Prelude PreludeText : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 103
A.3 Prelude PreludeIO : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 109
B Syntax 111
B.1 Notational Conventions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 111
B.2 Lexical Syntax : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 111
B.3 Layout : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 113
B.4 Context-Free Syntax : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 115
C Literate comments 120
D Specication of Derived Instances 122
D.1 An example : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 125
E Compiler Pragmas 127
E.1 Inlining : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 127
E.2 Specialization : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 127
E.3 Optimization : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 128
References 129
Index 131
PREFACE iii
Preface
(January 1, 1997)
\Some half dozen persons have written technically on combinatory logic, and
most of these, including ourselves, have published something erroneous. Since
some of our fellow sinners are among the most careful and competent logicians
on the contemporary scene, we regard this as evidence that the subject is re-
fractory. Thus fullness of exposition is necessary for accuracy; and excessive
condensation would be false economy here, even more than it is ordinarily."
Haskell B. Curry and Robert Feys
in the Preface to Combinatory Logic [2], May 31, 1956
Goals
The committee's primary goal was to design a language that satised these constraints:
1. It should be suitable for teaching, research, and applications, including building large
systems.
2. It should be completely described via the publication of a formal syntax and semantics.
3. It should be freely available. Anyone should be permitted to implement the language
and distribute it to whomever they please.
4. It should be based on ideas that enjoy a wide consensus.
5. It should reduce unnecessary diversity in functional programming languages.
The committee hopes that Haskell can serve as a basis for future research in language
design. We hope that extensions or variants of the language may appear, incorporating
experimental features.
iv PREFACE
This Report
This report is the ocial specication of the Haskell language and should be suitable for
writing programs and building implementations. It is not a tutorial on programming in
Haskell such as the `Gentle Introduction' [5], so some familiarity with functional languages
is assumed.
Version 1.4 of the report was unveiled in 1997. It makes some minor corrections to
version 1.3 and adds a few new features as described below. Version 1.4 is described in two
separate documents: the Haskell Language Report (this document) and the Haskell Library
Report[8].
Monadic I/O
Monadic I/O has proven to be more general and in many respects simpler than the stream-
based I/O system used in Haskell 1.2. Here are the highlights of the I/O denition.
We dene a monadic programming model for Haskell. Expressions of type IO a denote
computations that may engage in I/O before returning an answer of type a.
The IO monad admits computations that fail and recovers from such failures.
We dene a new type of handles, to mediate I/O operations on les and other I/O
devices. Handles are part of the I/O library.
We dene input polling and input of characters. In contrast, Haskell 1.2 represented
character input as a single String (that is, a lazy list of characters), containing all
the characters available for input throughout the program execution.
Monadic I/O provides an extensible framework capable of incorporating advanced
operating system and GUI interfaces in libraries.
Monadic programming has been made more readable through the introduction of a
special do syntax.
PREFACE v
Constructor Classes
Constructor classes are a natural generalization of the original Haskell type system, sup-
porting polymorphism over type constructors. For example, the monadic operators used by
the I/O system have been generalized using constructor classes to arbitrary monads just as
(+) has been generalized to arbitrary numeric types using type classes.
Haskell Resources
We welcome your comments, suggestions, and criticisms on the language or its presentation
in the report. A common mailing list for technical discussion of Haskell uses the following
electronic mail addresses:
[email protected] forwards mail to all subscribers of the Haskell list.
[email protected] is used to add and remove subscribers from the mail-
ing list. To subscribe or unsubscribe send messages of the form:
subscribe haskell
unsubscribe haskell
You may wish to subscribe or remove a mailing address other than the reply-to address
contained in your mail message. These commands may include an explicit email
address:
subscribe haskell [email protected]
Please do not send subscription requests direct to the mailing list.
Each implementation has an email address for discussions of specic Haskell systems.
Please send questions and comments regarding these directly to the associated groups
instead of the global Haskell community.
Web pages for Haskell, which includes an on-line version of this report, a tutorial,
extensions to Haskell, information about upgrading programs from prior Haskell versions,
and information about Haskell implementations can be found at the following sites:
PREFACE vii
http://haskell.org
http://www.dcs.gla.ac.uk/fp/software/ghc
http://www.cs.chalmers.se/Haskell
http://www.cs.nott.ac.uk/Research/fpg/haskell.html
Acknowledgements
We heartily thank these people for their useful contributions to this report: Richard Bird,
Stephen Blott, Tom Blenko, Duke Briscoe, Magnus Carlsson, Franklin Chen, Chris Clack,
Guy Cousineau, Tony Davie, Chris Fasel, Pat Fasel, Andy Gill, Cordy Hall, Thomas Hall-
gren, Bob Hiromoto, Nic Holt, Ian Holyer, Randy Hudson, Simon B. Jones, Stef Joosten,
Mike Joy, Stefan Kahrs, Kent Karlsson, Richard Kelsey, Siau-Cheng Khoo, Amir Kishon,
John Launchbury, Mark Lillibridge, Sandra Loosemore, Olaf Lubeck, Jim Mattson, Randy
Michelsen, Rick Mohr, Arthur Norman, Nick North, Paul Otto, Larne Pekowsky, Rinus Plas-
meijer, Ian Poole, John Robson, Colin Runciman, Patrick Sansom, Lauren Smith, Raman
Sundaresh, Satish Thatte, Tom Thomson, Pradeep Varma, Tony Warnock, Stuart Wray,
and Bonnie Yantis. We are especially grateful to past members of the Haskell committee|
Arvind, Jon Fairbairn, Maria M. Guzman, Dick Kieburtz, Rishiyur Nikhil, Mike Reeve,
David Wise, and Jonathan Young|for the major contributions they have made to previous
versions of this report, which we have been able to build upon, and for their support for
this latest revision of Haskell. We also thank those who have participated in the lively
discussions about Haskell on the FP and Haskell mailing lists.
Finally, aside from the important foundational work laid by Church, Rosser, Curry, and
others on the lambda calculus, we wish to acknowledge the in
uence of many noteworthy
programming languages developed over the years. Although it is dicult to pinpoint the
origin of many ideas, we particularly wish to acknowledge the in
uence of Lisp (and its
modern-day incarnations Common Lisp and Scheme); Landin's ISWIM; APL; Backus's FP
[1]; ML and Standard ML; Hope and Hope+ ; Clean; Id; Gofer; Sisal; and Turner's series
of languages culminating in Miranda.1 Without these forerunners Haskell would not have
been possible.
1
Miranda is a trademark of Research Software Ltd.
viii PREFACE
1
1 Introduction
Haskell is a general purpose, purely functional programming language incorporating many
recent innovations in programming language design. Haskell provides higher-order func-
tions, non-strict semantics, static polymorphic typing, user-dened algebraic datatypes,
pattern-matching, list comprehensions, a module system, a monadic I/O system, and a rich
set of primitive datatypes, including lists, arrays, arbitrary and xed precision integers, and
oating-point numbers. Haskell is both the culmination and solidication of many years of
research on lazy functional languages.
This report denes the syntax for Haskell programs and an informal abstract semantics
for the meaning of such programs. We leave as implementation dependent the ways in
which Haskell programs are to be manipulated, interpreted, compiled, etc. This includes
such issues as the nature of programming environments and the error messages returned for
undened programs (i.e. programs that formally evaluate to ?).
\Holes" in program fragments representing arbitrary pieces of Haskell code are written in
italics, as in if e1 then e2 else e3 . Generally the italicized names are mnemonic, such as
e for expressions, d for declarations, t for types, etc.
1.4 Namespaces
Haskell provides a lexical syntax for inx operators (either functions or constructors). To
emphasize that operators are bound to the same things as identiers, and to allow the two
to be used interchangeably, there is a simple way to convert between the two: any function
or constructor identier may be converted into an operator by enclosing it in backquotes,
and any operator may be converted into an identier by enclosing it in parentheses. For
example, x + y is equivalent to (+) x y, and f x y is the same as x f y. These lexical
matters are discussed further in Section 2.
There are six kinds of names in Haskell: those for variables and constructors denote
values; those for type variables, type constructors, and type classes refer to entities related
to the type system; and module names refer to modules. There are three constraints on
naming:
1. Names for variables and type variables are identiers beginning with lowercase letters;
the other four kinds of names are identiers beginning with uppercase letters.
2. Constructor operators are operators beginning with \:"; variable operators are oper-
ators not beginning with \:".
1.5 Layout 3
3. An identier must not be used as the name of a type constructor and a class in the
same scope.
These are the only constraints; for example, Int may simultaneously be the name of a
module, class, and constructor within a single scope.
1.5 Layout
In the syntax given in the rest of the report, layout lists are always preceded by the keyword
where, let, do, or of, and are enclosed within curly braces ({ }) with the individual
declarations separated by semicolons (;). Layout lists usually contain declarations, but do
and case introduce lists of other sorts. For example, the syntax of a let expression is:
let { decl1 ; decl2 ; ::: ; decln [;] } in exp
Haskell permits the omission of the braces and semicolons by using layout to convey the
same information. This allows both layout-sensitive and -insensitive styles of coding, which
can be freely mixed within one program. Because layout is not required, Haskell programs
can be straightforwardly produced by other programs.
The layout (or \o-side") rule takes eect whenever the open brace is omitted after the
keyword where, let, do, or of. When this happens, the indentation of the next lexeme
(whether or not on a new line) is remembered and the omitted open brace is inserted (the
whitespace preceding the lexeme may include comments). For each subsequent line, if it
contains only whitespace or is indented more, then the previous item is continued (nothing
is inserted); if it is indented the same amount, then a new item begins (a semicolon is
inserted); and if it is indented less, then the layout list ends (a close brace is inserted). A
close brace is also inserted whenever the syntactic category containing the layout list ends;
that is, if an illegal lexeme is encountered at a point where a close brace would be legal, a
close brace is inserted. The layout rule matches only those open braces that it has inserted;
an explicit open brace must be matched by an explicit close brace. Within these explicit
open braces, no layout processing is performed for constructs outside the braces, even if a
line is indented to the left of an earlier implicit open brace.
Given these rules, a single newline may actually terminate several layout lists. Also,
these rules permit:
f x = let a = 1; b = 2
g y = exp2
in exp1
making a, b and g all part of the same layout list.
To facilitate the use of layout at the top level of a module (an implementation may
allow several modules may reside in one le), the keyword module and the end-of-le token
are assumed to occur in column 0 (whereas normally the rst column is 1). Otherwise, all
top-level declarations would have to be indented.
4 1. INTRODUCTION
2 Lexical Structure
In this section, we describe the low-level lexical structure of Haskell. Most of the details
may be skipped in a rst reading of the report.
special ! ( j)j,j;j[j]j_jj{j}
whitespace ! whitestu fwhitestu g
whitestu ! whitechar j comment j ncomment
whitechar ! newline j return j linefeed j vertab j formfeed
j space j tab j UNIwhite
newline ! a newline (system dependent)
return ! a carriage return
linefeed ! a line feed
space ! a space
tab ! a horizontal tab
vertab ! a vertical tab
formfeed ! a form feed
uniWhite ! any UNIcode character dened as whitespace
comment ! -- fany g newline
ncomment ! {- ANYseq fncomment ANYseq g -}
ANYseq ! fANY ghfANY g ( {- j -} ) fANY gi
ANY ! any j newline j vertab j formfeed
any ! graphic j space j tab j nonbrkspc
graphic ! large j small j digit j symbol j special j : j " j '
small ! ASCsmall j UNIsmall
ASCsmall ! a j b j : : : j z
UNIsmall ! any Unicode lowercase letter
large ! ASClarge j UNIlarge
ASClarge ! A j B j : : : j Z
UNIlarge ! any uppercase or titlecase Unicode letter
symbol ! ASCsymbol j UNIsymbol
ASCsymbol ! ! j # j $ j % j & j * j + j . j / j < j = j > j ? j @
j \ j^j|j-j~
UNIsymbol ! Any Unicode symbol or punctuation
digit ! 0 j 1 j ::: j 9
udigit ! digit j UNIdigit
UNIdigit ! A Unicode numberic
octit ! 0 j 1 j ::: j 7
hexit ! digit j A j : : : j F j a j : : : j f
Characters not in the category ANY are not valid in Haskell programs and should result
in a lexing error. Comments are valid whitespace. An ordinary comment begins with two
consecutive dashes (--) and extends to the following newline. A nested comment begins
with {- and ends with -}; it can be between any two lexemes. All character sequences
not containing {- nor -} are ignored within a nested comment. Nested comments may
be nested to any depth: any occurrence of {- within the nested comment starts a new
8 2. LEXICAL STRUCTURE
An identier consists of a letter followed by zero or more letters, digits, underscores, and
single quotes. Identiers are lexically distinguished into two classes: those that begin with
a lower-case letter (variable identiers) and those that begin with an upper-case letter
(constructor identiers). Identiers are case sensitive: name, naMe, and Name are three
distinct identiers (the rst two are variable identiers, the last is a constructor identier).
Some identiers, here indicated by specialid , have special meanings in certain contexts but
can be used as ordinary identiers.
varsym ! ( symbol fsymbol j :g )hreservedopi
consym ! (: fsymbol j :g)hreservedopi
reservedop ! .. j :: j = j \ j | j <- j -> j @ j ~ j =>
specialop ! -j!
Operator symbols are formed from one or more symbol characters, as dened above,
and are lexically distinguished into two classes: those that start with a colon (constructors)
and those that do not (functions). Some operators, here indicated by specialop , have special
meanings in certain contexts but can be used as ordinary operators.
The sequence -- immediately terminates a symbol; thus +--+ parses as the symbol +
followed by a comment.
Other than the special syntax for prex negation, all operators are inx, although each
inx operator can be used in a section to yield partially applied operators (see Section 3.5).
All of the standard inx operators are just predened symbols and may be rebound.
Although case is a reserved word, cases is not. Similarly, although = is reserved, == and
~= are not. At each point, the longest possible lexeme is read, using a context-independent
2.4 Numeric Literals 9
deterministic lexical analysis (i.e. no lookahead beyond the current character is required).
Any kind of whitespace is also a proper delimiter for lexemes.
In the remainder of the report six dierent kinds of names will be used:
varid (variables)
conid (constructors)
tyvar ! varid (type variables)
tycon ! conid (type constructors)
tycls ! conid (type classes)
modid ! conid (modules)
Variables and type variables are represented by identiers beginning with small letters, and
the other four by identiers beginning with capitals; also, variables and constructors have
inx forms, the other four do not. Namespaces are also discussed in Section 1.4.
External names may optionally be qualied in certain circumstances by prepending them
with a module identier. This applies to variable, constructor, type constructor and type
class names, but not type variables or module names. Qualied names are discussed in
detail in Section 5.1.2.
qvarid ! [modid .] varid
qconid ! [modid .] conid
qtycon ! [modid .] tycon
qtycls ! [modid .] tycls
qvarsym ! [modid .] varsym
qconsym ! [modid .] consym
integer ! decimal
j 0o octal j 0O octal
j 0x hexadecimal j 0X hexadecimal
oat ! decimal . decimal [(e j E)[- j +]decimal ]
There are two distinct kinds of numeric literals: integer and
oating. Integer literals may
be given in decimal (the default), octal (prexed by 0o or 0O) or hexadecimal notation
(prexed by 0x or 0X). Floating literals are always decimal. A
oating literal must contain
digits both before and after the decimal point; this ensures that a decimal point cannot be
mistaken for another use of the dot character. Negative numeric literals are discussed in
Section 3.4. The typing of numeric literals is discussed in Section 6.3.1.
10 2. LEXICAL STRUCTURE
String literals are actually abbreviations for lists of characters (see Section 3.7).
11
3 Expressions
In this section, we describe the syntax and informal semantics of Haskell expressions, includ-
ing their translations into the Haskell kernel, where appropriate. Except in the case of let
expressions, these translations preserve both the static and dynamic semantics. Some of the
names and symbols used in the syntax are not reserved. These are indicated by the `special'
productions in the lexical syntax. Examples include ! (used only in data declarations) and
as (used in import declarations).
Free variables and constructors used in these translations refer to entities dened by
the Prelude. To avoid clutter, we use True instead of Prelude.True or map instead of
Prelude.map. (Prelude.True is a qualied name as described in Section 5.1.2.)
In the syntax that follows, there are some families of nonterminals indexed by precedence
levels (written as a superscript). Similarly, the nonterminals op , varop , and conop may
have a double index: a letter l , r , or n for left-, right- or non-associativity and a precedence
level. A precedence-level variable i ranges from 0 to 9; an associativity variable a varies
over fl ; r ; n g. Thus, for example
aexp ! ( exp i +1 qop (a ;i ) )
actually stands for 30 productions, with 10 substitutions for i and 3 for a .
exp ! exp 0 :: [context =>] type (expression type signature)
j exp 0
exp i ! exp i +1 [qop (n;i ) exp i +1 ]
j lexp i
j rexp i
lexp i ! (lexp i j exp i +1 ) qop (l;i ) exp i +1
lexp 6 ! - exp 7
rexp i ! exp i +1 qop (r;i ) (rexp i j exp i +1 )
exp 10 ! \ apat1 : : : apatn -> exp (lambda abstraction; n 1 )
j let decllist in exp (let expression)
j if exp then exp else exp (conditional)
j case exp of { alts [;] } (case expression)
j do { stmts [;] } (do expression)
j fexp
fexp ! [fexp ] aexp (function application)
aexp ! qvar (variable)
j gcon (general constructor)
j literal
j ( exp ) (parenthesized expression)
j ( exp1 , : : : , expk ) (tuple; k 2 )
j [ exp1 , : : : , expk ] (list; k 1 )
j [ exp1 [, exp2 ] .. [exp3 ] ] (arithmetic sequence)
12 3. EXPRESSIONS
Item Associativity
simple terms, parenthesized terms {
irrefutable patterns (~) {
as-patterns (@) right
function application left
do, if, let, lambda(\), case (leftwards) right
case (rightwards) right
inx operators, prec. 9 as dened
... ...
inx operators, prec. 0 as dened
function types (->) right
contexts (=>) {
type constraints (::) {
do, if, let, lambda(\) (rightwards) right
sequences (..) {
generators (<-) {
grouping (,) n-ary
guards (|) {
case alternatives (->) {
denitions (=) {
separation (;) n-ary
The separation of function arrows from case alternatives solves the ambiguity that oth-
erwise arises when an unparenthesized function type is used in an expression, such as the
guard in a case expression.
Sample parses are shown below.
This Parses as
f x + g y (f x) + (g y)
- f x + y (- (f x)) + y
let { ... } in x + y let { ... } in (x + y)
z + let { ... } in x + y z + (let { ... } in (x + y))
f x y :: Int (f x y) :: Int
\ x -> a+b :: Int \ x -> ((a+b) :: Int)
For the sake of clarity, the rest of this section shows the syntax of expressions without
their precedences.
3.1 Errors
Errors during expression evaluation, denoted by ?, are indistinguishable from non-termination.
Since Haskell is a lazy language, all Haskell types include ?. That is, a value of any type
may be bound to a computation that, when demanded, results in an error. When evalu-
ated, errors cause immediate program termination and cannot be caught by the user. The
Prelude provides two functions to directly cause such errors:
error :: String -> a
undefined :: a
A call to error terminates execution of the program and returns an appropriate error indi-
cation to the operating system. It should also display the string in some system-dependent
manner. When undefined is used, the error message is created by the compiler.
Translations of Haskell expressions use error and undefined to explicitly indicate where
execution time errors may occur. The actual program behavior when an error occurs is up
to the implementation. The messages passed to the error function in these translations
are only suggestions; implementations may choose to display more or less information when
an error occurs.
14 3. EXPRESSIONS
gcon ! ()
j []
j (,f,g)
j qcon
qvar ! qvarid j ( qvarsym ) (qualied variable)
qcon ! qconid j ( qconsym ) (qualied constructor)
Alphanumeric operators are formed by enclosing an identier between grave accents
(backquotes). Any variable or constructor may be used as an operator in this way. If fun
is an identier (either variable or constructor), then an expression of the form fun x y is
equivalent to x fun y . If no xity declaration is given for fun then it defaults to highest
precedence and left associativity (see Section 5.6).
Similarly, any symbolic operator may be used as a (curried) variable or constructor by
enclosing it in parentheses. If op is an inx operator, then an expression or pattern of the
form x op y is equivalent to (op ) x y .
Qualied names may only be used to reference an imported variable or constructor (see
Section 5.1.2) but not in the denition of a new variable or constructor. Thus
let F.x = 1 in F.x -- invalid
incorrectly uses a qualier in the denition of x, regardless of the module containing this
denition. Qualication does not aect the nature of an operator: F.+ is an inx operator
just as + is.
Special syntax is used to name some constructors for some of the built-in types, as found
in the production for gcon and literal . These are described in Section 6.1.
An integer literal represents the application of the function fromInteger to the appro-
priate value of type Integer. Similarly, a
oating point literal stands for an application of
fromRational to a value of type Rational (that is, Ratio Integer).
The form e1 qop e2 is the inx application of binary operator qop to expressions e1 and
e2 .
The special form -e denotes prex negation, the only prex operator in Haskell, and is
syntax for negate (e ). The binary - operator does not necessarily refer to the denition of
- in the Prelude; it may be rebound by the module system. However, unary - will always
refer to the negate function dened in the Prelude. There is no link between the local
meaning of the - operator and unary negation.
Prex negation has the same precedence as the inx operator - dened in the Prelude
(see Table 2, page 63). Because e1-e2 parses as an inx application of the binary oper-
ator -, one must write e1(-e2) for the alternative parsing. Similarly, (-) is syntax for
(\ x y -> x-y), as with any inx operator, and does not denote (\ x -> -x)|one must
use negate for that.
3.5 Sections
aexp ! ( exp qop )
j ( qop exp )
3.6 Conditionals
exp ! if exp1 then exp2 else exp3
A conditional expression has the form if e1 then e2 else e3 and returns the value of e2
if the value of e1 is True, e3 if e1 is False, and ? otherwise.
where True and False are the two nullary constructors from the type Bool, as dened
in the Prelude.
3.7 Lists
aexp ! [ exp1 , : : : , expk ] (k 1 )
Lists are written [e1 , : : : , ek ], where k 1 ; the empty list is written []. Standard
operations on lists are given in the Prelude (see Appendix A, notably Section A.1).
3.8 Tuples 17
where : and [] are constructors for lists, as dened in the Prelude (see Section 6.1.3).
The types of e1 through ek must all be the same (call it t ), and the type of the overall
expression is [t ] (see Section 4.1.1).
3.8 Tuples
aexp ! ( exp1 , : : : , expk ) (k 2 )
Tuples are written (e1 , : : : , ek ), and may be of arbitrary length k 2 . Standard opera-
tions on tuples are given in the Prelude (see Appendix A).
The form (e ) is simply a parenthesized expression, and is equivalent to e . The unit expres-
sion () has type () (see Section 4.1.1); it is the only member of that type apart from ? (it
can be thought of as the \nullary tuple")|see Section 6.1.5.
Translation: (e ) is equivalent to e .
The forms [e1 .. e3 ] and [e1 ..] are similar to those above, but with an implied incre-
ment of one.
Arithmetic sequences may be dened over any type in class Enum, including Char, Int,
and Integer (see Figure 5 , page 67 and Section 4.3.3). For example, ['a'..'z'] denotes
the list of lowercase letters in alphabetical order.
yields the list [4,2]. If a qualier is a guard, it must evaluate to True for the previous
pattern match to succeed. As usual, bindings in list comprehensions can shadow those in
outer scopes; for example:
[ x | x <- x, x <- x ] = [ z | y <- x, z <- y]
As indicated by the translation of list comprehensions, variables bound by let have fully
polymorphic types while those dened by <- are lambda bound and are thus monomorphic
(see Section 4.5.4).
Let expressions have the general form let { d1 ; : : : ; dn } in e , and introduce a nested,
lexically-scoped, mutually-recursive list of declarations (let is often called letrec in other
languages). The scope of the declarations is the expression e and the right hand side of the
declarations. Declarations are described in Section 4. Pattern bindings are matched lazily;
an implicit ~ makes these patterns irrefutable. For example,
let (x,y) = undefined in e
Each alternative pi matchi consists of a pattern pi and its matches, matchi , which consists
of pairs of guards gij and bodies eij (expressions), as well as optional bindings (decllisti )
that scope over all of the guards and expressions of the alternative. An alternative of the
form
pat -> exp where decllist
is treated as shorthand for:
pat | True -> expr
where decllist
3.14 Do Expressions 21
A case expression must have at least one alternative and each alternative must have at
least one body. Each body must have the same type, and the type of the whole expression
is that type.
A case expression is evaluated by pattern matching the expression e against the in-
dividual alternatives. The matches are tried sequentially, from top to bottom. The rst
successful match causes evaluation of the corresponding alternative body, in the environ-
ment of the case expression extended by the bindings created during the matching of that
alternative and by the decllisti associated with that alternative. If no match succeeds, the
result is ?. Pattern matching is described in Section 3.17, with the formal semantics of
case expressions in Section 3.17.3.
3.14 Do Expressions
exp ! do { stmts [;]} (do expression)
stmts ! exp [; stmts ]
j pat <- exp ; stmts
j let decllist ; stmts
A do expression provides a more readable syntax for monadic programming.
>>, >>=, and zero are operations in the classes Monad and MonadZero, as dened in the
Prelude., and ok is a new identier not appearing in p.
A failure-free pattern is one that can only be refuted by ?. Failure-free patterns are
dened as follows:
All irrefutable patterns are failure-free (irrefutable patterns are described in Section
3.17.1).
If C is the only constructor in its type, then C p1 : : : pn is failure-free when each of
the pi is failure free.
22 3. EXPRESSIONS
A constructor with labeled elds may be used to construct a value in which the components
are specied by name rather than by position. Unlike the braces used in declaration lists,
these are not subject to layout; the { and } characters must be explicit. (This is also true of
eld updates and eld patterns.) Construction using eld names is subject to the following
constraints:
Only eld labels declared with the specied constructor may be mentioned.
A eld name may not be mentioned more than once.
Fields not mentioned are initialized to ?.
When the = exp is omitted and there is a variable with the same name as the eld
label in scope, the eld is initialized to the value of that variable.
A compile-time error occurs when any strict elds (elds whose declared types are
prexed by !) are omitted during construction. Strict elds are discussed in Sec-
tion 4.2.1.
Translation: In the binding f = v , the eld f labels v . Any binding f that omits the
= v is expanded to f = f .
C { bs } = C (pick1C bs undefined) : : : (pickkC bs undefined)
k is the arity of C .
The auxiliary function pickiC bs d is dened as follows:
If the i th component of a constructor C has the eld name f , and if f = v
appears in the binding list bs , then pickiC bs d is v . Otherwise, pickiC bs d
is the default value d .
Values belonging to a datatype with eld names may be non-destructively updated. This
creates a new value in which the specied eld values replace those in the existing value.
Updates are restricted in the following ways:
All labels must be taken from the same datatype.
At least one constructor must dene all of the labels mentioned in the update.
No label may be mentioned more than once.
An execution error occurs when the value being updated does not contain all of the
specied labels.
24 3. EXPRESSIONS
When the = exp is omitted, the eld is updated to the value of the variable in scope
with the same name as the eld label.
Expression Translation
C1 {f1 = 3} C1' 3 undefined
C2 {f1 = 1, f4 = 'A', f3 = 'B'} C2' 1 'B' 'A'
x {f1 = 1} case x of C1' _ f2 -> C1' 1 f2
C2' _ f3 f4 -> C2' 1 f3 f4
The eld f1 is common to both constructors in T. The constructors C1' and C2' are `hidden
constructors', see the translation in Section 4.2.1. A compile-time error will result if no single
constructor denes the set of eld names used in an update, such as x {f2 = 1, f3 = 'x'}.
3.17.1 Patterns
Patterns have this syntax:
pat ! var + integer (successor pattern)
pat j pat 0
pat i ! pat i +1 [qconop (n;i ) pat i +1 ]
j lpat i
j rpat i
lpat i ! (lpat i j pat i +1 ) qconop (l;i ) pat i +1
lpat 6 ! - (integer j
oat ) (negative literal)
rpat i ! pat i +1 qconop (r;i ) (rpat i j pat i +1 )
pat 10 ! apat
j gcon apat1 : : : apatk (arity gcon = k ; k 1 )
apat ! var [ @ apat ] (as pattern)
j gcon (arity gcon = 0 )
j qcon { fpat1 , : : : , fpatk } (labeled pattern; k 0 )
j literal
j _ (wildcard)
j ( pat ) (parenthesized pattern)
j ( pat1 , : : : , patk ) (tuple pattern; k 2 )
j [ pat1 , : : : , patk ] (list pattern; k 1 )
j ~ apat (irrefutable pattern)
fpat ! var = pat
j var
The arity of a constructor must match the number of sub-patterns associated with it; one
cannot match against a partially-applied constructor.
All patterns must be linear |no variable may appear more than once.
Patterns of the form var @pat are called as-patterns, and allow one to use var as a name
for the value being matched by pat . For example,
case e of { xs@(x:rest) -> if x==0 then rest else xs }
is equivalent to:
let { xs = e } in
case xs of { (x:rest) -> if x==0 then rest else xs }
Patterns of the form _ are wildcards and are useful when some part of a pattern is not
referenced on the right-hand-side. It is as if an identier not used elsewhere were put in its
place. For example,
case e of { [x,_,_] -> if x==0 then True else False }
26 3. EXPRESSIONS
is equivalent to:
case e of { [x,y,z] -> if x==0 then True else False }
In the pattern matching rules given below we distinguish two kinds of patterns: an
irrefutable pattern is: a variable, a wildcard, N apat where N is a constructor dened by
newtype and apat is irrefutable (see Section 4.2.3), var @apat where apat is irrefutable, or
of the form ~apat (whether or not apat is irrefutable). All other patterns are refutable.
(c) Matching a non-? value x against a pattern of the form n +k (where n is a variable
and k is a positive integer literal) succeeds if x k , resulting in the binding of
n to x ? k , and fails if x < k . The behavior of n +k patterns depends entirely on
the underlying denitions of >=, fromInteger, and - for the type of the object
being matched.
5. Matching against a constructor using labeled elds is the same as matching ordinary
constructor patterns except that the elds are matched in the order they are named
in the eld list. All elds listed must be declared by the constructor; elds may not
be named more than once. Fields not named by the pattern are ignored (matched
against _).
6. The result of matching a value v against an as-pattern var @apat is the result of
matching v against apat augmented with the binding of var to v . If the match of v
against apat fails or diverges, then so does the overall match.
Aside from the obvious static type constraints (for example, it is a static error to match
a character against a boolean), these static class constraints hold: an integer literal pattern
can only be matched against a value in the class Num and a
oating literal pattern can only
be matched against a value in the class Fractional. A n +k pattern can only be matched
against a value in the class Integral.
Many people feel that n +k patterns should not be used. These patterns may be removed
or changed in future versions of Haskell. Compilers should support a
ag that disables the
use of these patterns.
Here are some examples:
1. If the pattern [1,2] is matched against [0,?], then 1 fails to match against 0, and
the result is a failed match. But if [1,2] is matched against [?,0], then attempting
to match 1 against ? causes the match to diverge .
2. These examples demonstrate refutable vs. irrefutable matching:
(\ ~(x,y) -> 0) ? ) 0
(\ (x,y) -> 0) ? ) ?
(\ ~[x] -> 0) [] ) 0
(\ ~[x] -> x) [] ) ?
(\ ~[x,~(a,b)] -> x) [(0,1), ] ? ) (0,1)
(\ ~[x, (a,b)] -> x) [(0,1), ] ? ) ?
(\ (x:xs) -> x:x:xs) ? ) ?
(\ ~(x:xs) -> x:x:xs) ? ) ?:?:?
Additional examples illustrating some of the subtleties of pattern matching may be found
in Section 4.2.3.
28 3. EXPRESSIONS
Top level patterns in case expressions and the set of top level patterns in function or
pattern bindings may have zero or more associated guards. A guard is a boolean expression
that is evaluated only after all of the arguments have been successfully matched, and it
must be true for the overall pattern match to succeed. The environment of the guard is
the same as the right-hand-side of the case-expression alternative, function denition, or
pattern binding to which it is attached.
The guard semantics have an obvious in
uence on the strictness characteristics of a
function or case expression. In particular, an otherwise irrefutable pattern may be evaluated
because of a guard. For example, in
f ~(x,y,z) [a] | a==y = 1
(g) case v
of { K p1 : : :pn -> e; _ -> e0 }
v
= case of {
K x1 : : :xn -> case x1 of {
p1 -> :::
case xn of { pn -> e ; _ -> e0 } :::
_ -> 0 }
e
_ -> e0 }
at least one of p1 ; : : :; pn is not a variable; x1; : : :; xn are new variables
(h) case v of { k -> e; _ -> e0 } = if (v ==k) then e else e0
(i) case v of { x -> e; _ -> e0 } = case v of { x -> e }
(j) case v of { x -> e } = ( \ x -> e ) v
(k) case N v of { N p -> e; _ -> e0 }
= case v of { p -> e; _ -> e0 }
where N is a newtype constructor
(l) case ? of { N p -> e; _ -> e0 } = case ? of { p -> e }
where N is a newtype constructor
(m) case v of { K { f1 = p1 , f2 = p2 , : : : } -> e ; _ -> e0 }
= case e0 of {
y ->
case v of {
K { f1 = p1 } ->
case v of { K { f2 = p2 , ::: } -> e; _ -> y };
_ -> y }}
where f1 , f2 , : : : are elds of constructor K ; y is a new variable
(n) case v of { K { f = p} -> e; _ -> e0 }
= case v of {
K p1 : : : pn -> e ; _ -> e0 }
where pi is p if f labels the ith component of K , _ otherwise
(o) case v of { K {} -> e ; _ -> e0 }
= case v of {
K _ : : : _ -> e ; _ -> e0 }
(p) case (K 0 e1 : : : em ) of { K x1 : : : xn -> e; _ -> e0 } = e0
where K and K 0 are distinct data constructors of arity n and m, respectively
(q) case (K e1 : : : en ) of { K x1 : : : xn -> e; _ -> e0 }
= case e1 of { x01 -> : : : case en of { x0n -> e[x01=x1 : : :x0n =xn ] }: : :}
where K is a constructor of arity n; x01 : : :x0n are completely new variables
(r) case e0 of { x+k -> e; _ -> e0 }
= if e0 >= k then let {x0 = e0 -k} in e[x0 =x] else e0 (x0 is a new variable)
A class declaration (Section 4.3.1) introduces a new type class and the overloaded
operations that must be supported by any type that is an instance of that class. An
instance declaration (Section 4.3.2) declares that a type is an instance of a class and
includes the denitions of the overloaded operations|called class methods|instantiated
on the named type.
For example, suppose we wish to overload the operations (+) and negate on types Int
and Float. We introduce a new type class called Num:
class Num a where -- simplified class declaration for Num
(+) :: a -> a -> a
negate :: a -> a
This declaration may be read \a type a is an instance of the class Num if there are (over-
loaded) class methods (+) and negate, of the appropriate types, dened on it."
We may then declare Int and Float to be instances of this class:
instance Num Int where -- simplified instance of Num Int
x + y = addInt x y
negate x = negateInt x
instance Num Float where -- simplified instance of Num Float
x + y = addFloat x y
negate x = negateFloat x
where addInt, negateInt, addFloat, and negateFloat are assumed in this case to be
primitive functions, but in general could be any user-dened function. The rst declaration
above may be read \Int is an instance of the class Num as witnessed by these denitions
(i.e. class methods) for (+) and negate."
More examples of type and constructor classes can be found in the papers by Jones [6]
or Wadler and Blott [11]. The term `type class' was used to describe the original Haskell
1.0 type system; `constructor class' was used to describe an extension to the original type
classes. There is no longer any reason to use two dierent terms: in this report, `type class'
includes both the original Haskell type classes and the constructor classes introduced by
Jones.
gtycon ! qtycon
j () (unit type)
j [] (list constructor)
j (->) (function constructor)
j (,f,g) (tupling constructors)
The syntax for Haskell type expressions is given above. Just as data values are built using
data constructors, type values are built from type constructors . As with data constructors,
the names of type constructors start with uppercase letters.
To ensure that they are valid, type expressions are classied into dierent kinds, which
take one of two possible forms:
The symbol represents the kind of all nullary type constructors.
If and are kinds, then ! is the kind of types that take a type of kind
1 2 1 2 1
and return a type of kind .
2
For example, the type expression IO a can be understood as the application of a constant,
IO, to the variable a. Since the IO type constructor has kind ! , it follows that both
the variable a and the whole expression, IO a, must have kind . In general, a process of
kind inference (see Section 4.6) is needed to determine appropriate kinds for user-dened
datatypes, type synonyms, and classes.
Special syntax is provided to allow certain type expressions to be written in a more
traditional style:
1. A function type has the form t1 -> t2 , which is equivalent to the type (->) t1 t2 .
Function arrows associate to the right.
2. A tuple type has the form (t1 , : : : , tk ) where k 2 , which is equivalent to the type
(,. . . ,) t1 : : : tk where there are k ? 1 commas between the parenthesis. It denotes
the type of k -tuples with the rst component of type t1 , the second component of
type t2 , and so on (see Sections 3.8 and 6.1.4).
3. A list type has the form [t ], which is equivalent to the type [] t . It denotes the type
of lists with elements of type t (see Sections 3.7 and 6.1.3).
Although the tuple, list, and function types have special syntax, they are not dierent from
user-dened types with equivalent functionality.
Expressions and types have a consistent syntax. If ti is the type of expression or pattern
ei , then the expressions (\ e1 -> e2 ), [e1 ], and (e1 ; e2 ) have the types (t1 -> t2 ), [t1 ],
and (t1 ; t2 ), respectively.
With one exception, the type variables in a Haskell type expression are all assumed to
be universally quantied; there is no explicit syntax for universal quantication [3]. For
example, the type expression a -> a denotes the type 8 a : a ! a . For clarity, however,
we often write quantication explicitly when discussing the types of Haskell programs.
The exception referred to is that of the distinguished type variable in a class declaration
(Section 4.3.1).
A class assertion has form qtycls tyvar , and indicates the membership of the parameterized
type tyvar in the class qtycls . A class identier begins with an uppercase letter.
4.1 Overview of Types and Classes 35
A context consists of one or more class assertions, and has the general form
( C1 u1 ; : : :; Cn un )
where C1 ; : : :; Cn are class identiers, and u1 ; : : :; un are type variables; the parentheses
may be omitted when n = 1 . In general, we use c to denote a context and we write c => t
to indicate the type t restricted by the context c . The context c must only contain type
variables referenced in t . For convenience, we write c => t even if the context c is empty,
although in this case the concrete syntax contains no =>.
where c is a context. This declaration introduces a new type constructor T with constituent
data constructors K1 ; : : :; Kn whose types are given by:
Ki :: 8 u1 : : : uk : ci ) ti1 ! ! tiki ! (T u1 : : : uk )
where ci is the largest subset of c that constrains only those type variables free in the types
ti1 ; : : :; tiki . The type variables u1 through uk must be distinct and may appear in c and
the tij ; it is a static error for any other type variable to appear in c or on the right-hand-side.
The new type constant T has a kind of the form 1 ! : : : ! k ! where the kinds i of
the argument variables ui are determined by kind inference as described in Section 4.6. This
means that T may be used in type expressions with anywhere between 0 and k arguments.
For example, the declaration
data Eq a => Set a = NilSet | ConsSet a (Set a)
introduces a type constructor Set of kind ! , and constructors NilSet and ConsSet
with types
NilSet :: 8 a : Set a
ConsSet :: 8 a : Eq a ) a ! Set a ! Set a
4.2 User-Dened Datatypes 37
In the example given, the overloaded type for ConsSet ensures that ConsSet can only
be applied to values whose type is an instance of the class Eq. The context in the data
declaration has no other eect whatsoever.
The visibility of a datatype's constructors (i.e. the \abstractness" of the datatype) out-
side of the module in which the datatype is dened is controlled by the form of the datatype's
name in the export list as described in Section 5.5.
The optional deriving part of a data declaration has to do with derived instances, and
is described in Section 4.3.3.
Labeled Fields A data constructor of arity k creates an object with k components. These
components are normally accessed positionally as arguments to the constructor in expres-
sions or patterns. For large datatypes it is useful to assign eld labels to the components
of a data object. This allows a specic eld to be referenced independently of its location
within the constructor.
A constructor denition in a data declaration using the { } syntax assigns labels to the
components of the constructor. Constructors using eld labels may be freely mixed with
constructors without them. A constructor with associated eld labels may still be used as
an ordinary constructor; features using labels are simply a shorthand for operations using
an underlying positional constructor. The arguments to the positional constructor occur in
the same order as the labeled elds. For example, the declaration
data C = F { f1,f2 :: Int, f3 :: Bool}
Operations using eld labels are described in Section 3.15. A data declaration may use
the same eld label in multiple constructors as long as the typing of the eld is the same
in all cases after type synonym expansion. A label cannot be shared by more than one
type in scope. Field names share the top level namespace with ordinary variables and class
methods and must not con
ict with other top level names in scope.
Strictness Flags Whenever a data constructor is applied, each argument to the construc-
tor is evaluated if and only if the corresponding type in the algebraic datatype declaration
has a strictness
ag (!).
38 4. DECLARATIONS AND BINDINGS
Strictness
ags may require the explicit inclusion of an Eval context in a data declaration
(see Section 6.2.7). This occurs precisely when the context of a strict function used in the
above translation propagates to a type variable. For example, in
data (Eval a) => Pair a b = MakePair !a b
the class assertion (Eval a) is required by the use of strict in the translation of the
constructor MakePair. This context must be explicitly supplied by the programmer. The
Eval context may be implied by a more general one; for example, the Num class includes
Eval as a superclass to avoid mentioning Eval in the following:
For some types, the Eval context may not be expressible (see Section 4.5.3. For example,
in
data T a b = T !(a b)
the context Eval (a b) would be required. Since this context is not legal, the strictness
ag cannot be used in this situation.
A type synonym declaration introduces a new type that is equivalent to an old type. It has
the form
type T u1 : : : uk = t
which introduces a new type constructor, T . The type (T t1 : : : tk ) is equivalent to the type
t [t1 =u1 ; : : :; tk =uk ]. The type variables u1 through uk must be distinct and are scoped
only over t ; it is a static error for any other type variable to appear in t . The kind of
the new type constructor T is of the form 1 ! : : : ! k ! where the kinds i of the
arguments ui and of the right hand side t are determined by kind inference as described
4.2 User-Dened Datatypes 39
in Section 4.6. For example, the following denition can be used to provide an alternative
way of writing the list type constructor:
type List = []
data D1 = D1 Int
data D2 = D2 !Int
type S = Int
newtype N = N Int
d1 (D1 i) = 42
d2 (D2 i) = 42
s i = 42
n (N i) = 42
the expressions ( d1 ? ), ( d2 ? ) and (d2 (D2 ? ) ) are all equivalent to ?, whereas
( n ? ), ( n ( N ? ) ), ( d1 ( D1 ? ) ) and ( s ? ) are all equivalent to 42. In partic-
ular, ( N ? ) is equivalent to ? while ( D1 ? ) is not equivalent to ?.
The optional deriving part of a newtype declaration is treated in the same way as the
deriving component of a data declaration; see Section 4.3.3.
Every type, both those declared by data and newtype, is made an instance of the Eval
class by an implicit derived instance declaration for Eval (see Section 6.2.7). It is as if there
was an implicit \deriving(Eval)" on every type declaration. For newtype, the instance
declaration has the form
instance CEval => Eval (T u1 : : : uk ) where
(N x) `seq` y = x `seq` y
where CEval is the context obtained by simplifying Eval t . For example, the declaration
newtype Age = MkAge Int
gives rise to
instance Eval a => Eval (Id a) where
(MkId a) `seq` b = a `seq` b
This derived instance may lead to a context reduction error (see Section 4.5.3). A static
error occurs when it is not possible to nd CEval for a newtype declaration (just as with
other derived instances). For example
newtype T a = MkT (a Int)
is illegal, because one cannot reduce the context Eval (a Int). The derived Eval instance
for data declarations has an empty context and thus will never generate static errors. Types
that cannot be renamed by newtype due to this context problem are the same as those that
cannot be marked as strict in a data declaration (see Section 4.2.1).
4.3 Type Classes and Overloading 41
A class declaration introduces a new class and the operations (class methods) on it. A class
declaration has the general form:
class c => Cu where { v1 :: c1 => t1 ; : : : ; vn :: cn => tn ;
valdef1 ; : : : ; valdefm }
This introduces a new class name C ; the type variable u is scoped only over the class method
signatures in the class body. The context c species the superclasses of C , as described
below; the only type variable that may be referred to in c is u . The class declaration intro-
duces new class methods v1 ; : : :; vn , whose scope extends outside the class declaration,
with types:
vi :: 8u; w: (Cu; ci) ) ti
The ti must mention u ; they may mention type variables w other than u , and the type of
vi is polymorphic in both u and w . The ci may constrain only w ; in particular, the ci may
not constrain u . For example:
class Foo a where
op :: Num b => a -> b -> a
Here the type of op is 8 a ; b : (Foo a ; Num b ) ) a ! b ! a .
Default class methods for any of the vi may be included in the class declaration as a
normal valdef ; no other denitions are permitted. The default class method for vi is used
if no binding for it is given in a particular instance declaration (see Section 4.3.2).
Class methods share the top level namespace with variable bindings and eld names;
they must not con
ict with other top level bindings in scope. That is, a class method can
not have the same name as a top level denition, a eld name, or another class method.
A class declaration with no where part may be useful for combining a collection of
classes into a larger one that inherits all of the class methods in the original ones. For
example:
class (Read a, Show a) => Textual a
be a class declaration. The general form of the corresponding instance declaration is:
instance c 0 => C (T u1 : : : uk ) where { d }
2. Any constraints on the type variables in the instance type that are required for
the class method declarations in d to be well-typed must also be satised.
In fact, except in pathological cases it is possible to infer from the instance declaration
the most general instance context c 0 satisfying the above two constraints, but it is
nevertheless mandatory to write an explicit instance context.
The following illustrates the restrictions imposed by superclass instances:
class Foo a => Bar a where ...
A static error results if it is not possible to derive an instance declaration over a class
named in a deriving form. For example, not all datatypes can properly support class
methods in Enum. It is also a static error to give an explicit instance declaration for a class
that is also derived.
If the deriving form is omitted from a data or newtype declaration, then no instance
declarations (except for Eval) are derived for that datatype; that is, omitting a deriving
form is equivalent to including an empty deriving form: deriving ().
could be satised by instantiating a as either Int in both cases, or Bool. Such expressions
are considered ill-typed, a static error.
We say that an expression e is ambiguously overloaded if, in its type 8 u : c ) t , there
is a type variable u in u that occurs in c but not in t . Such types are invalid.
For example, the earlier expression involving show and read is ambiguously overloaded
since its type is 8 a : Show a ; Read a ) String.
Overloading ambiguity can only be circumvented by input from the user. One way is
through the use of expression type-signatures as described in Section 3.16. For example, for
the ambiguous expression given earlier, one could write:
let x = read "..." in show (x::Bool)
which disambiguates the type.
Occasionally, an otherwise ambiguous expression needs to be made the same type as
some variable, rather than being given a xed type with an expression type-signature. This
is the purpose of the function asTypeOf (Appendix A): x `asTypeOf` y has the value of
x , but x and y are forced to have the same type. For example,
approxSqrt x = encodeFloat 1 (exponent x `div` 2) `asTypeOf` x
(See Section 6.3.6.)
Ambiguities in the class Num are most common, so Haskell provides another way to
resolve them|with a default declaration:
default (t1 , : : : , tn )
4.4 Nested Declarations 45
where n 0 , and each ti must be a monotype for which Num ti holds. In situations where an
ambiguous type is discovered, an ambiguous type variable is defaultable if at least one of its
classes is a numeric class (that is, Num or a subclass of Num) and if all of its classes are dened
in the Prelude or a standard library (Figures 6{7, pages 74{75 show the numeric classes,
and Figure 5, page 67, shows the classes dened in the Prelude.) Each defaultable variable
is replaced by the rst type in the default list that is an instance of all the ambiguous
variable's classes. It is a static error if no such type is found.
Only one default declaration is permitted per module, and its eect is limited to that
module. If no default declaration is given in a module then it assumed to be:
default (Int, Double)
The empty default declaration default () must be given to turn o all defaults in a module.
gd ! | exp 0
We distinguish two cases within this syntax: a pattern binding occurs when lhs is pat ;
otherwise, the binding is called a function binding. Either binding may appear at the
top-level of a module or within a where or let construct.
Function bindings. A function binding binds a variable to a function value. The general
form of a function binding for variable x is:
x p11 : : : p1k match1
:::
x pn1 : : : pnk matchn
where each pij is a pattern, and where each matchi is of the general form:
= ei where { declsi }
or
| gi1 = ei1
:::
| gimi = eimi
declsi }
where {
and where n 1 , 1 i n , mi 1 . The former is treated as shorthand for a particular
case of the latter, namely:
| True = ei where { declsi }
Note that all clauses dening a function must be contiguous, and the number of patterns
in each clause must be the same. The set of patterns corresponding to each match must be
linear|no variable is allowed to appear more than once in the entire set.
Alternative syntax is provided for binding functional values to inx operators. For
example, these two function denitions are equivalent:
plus x y z = x+y+z
x plus y = \ z -> x+y+z
Translation: The general binding form for functions is semantically equivalent to the
equation (i.e. simple pattern binding):
x x1 x2 ::: xk = case ( x1 , :::, xk ) of (p11 ; : : :; p1k ) match1
:::
(pm1 ; : : :; pmk ) matchm
where the xi are new identiers.
48 4. DECLARATIONS AND BINDINGS
Pattern bindings. A pattern binding binds variables to values. A simple pattern binding
has form p = e . The pattern p is matched \lazily" as an irrefutable pattern, as if there
were an implicit ~ in front of it. See the translation in Section 3.12.
The general form of a pattern binding is p match , where a match is the same structure
as for function bindings above; in other words, a pattern binding is:
p | g1 = e1
| g2 = e 2
:::
| gm = e m
where { decls }
4.5.2 Generalization
The Hindley-Milner type system assigns types to a let-expression in two stages. First, the
right-hand side of the declaration is typed, giving a type with no universal quantication.
Second, all type variables that occur in this type are universally quantied unless they
are associated with bound variables in the type environment; this is called generalization.
Finally, the body of the let-expression is typed.
For example, consider the declaration
f x = let g y = (y,y)
in ...
The type of g's denition is a ! (a ; a ). The generalization step attributes to g the poly-
morphic type 8 a : a ! (a ; a ), after which the typing of the \..." part can proceed.
When typing overloaded denitions, all the overloading constraints from a single dec-
laration group are collected together, to form the context for the type of each variable
declared in the group. For example, in the denition:
f x = let g1 x y = if x>y then show x else g2 y x
g2 p q = g1 q p
in ...
The types of the denitions of g1 and g2 are both a ! a ! String, and the accumulated
constraints are Ord a (arising from the use of >), and Show a (arising from the use of show).
The type variables appearing in this collection of constraints are called the constrained type
variables.
The generalization step attributes to both g1 and g2 the type
8 a : (Ord a ; Show a) ) a ! a ! String
Notice that g2 is overloaded in the same way as g1 even though the occurrences of > and
show are in the denition of g1.
If the programmer supplies explicit type signatures for more than one variable in a
declaration group, the contexts of these signatures must be identical up to renaming of the
type variables.
and not
f :: Eq [a] => [a] -> a -> Bool
Even though the equality is taken at the list type, the context must be simplied, using the
instance declaration for Eq on lists, before generalization. If no such instance is in scope, a
static error occurs.
The context may also fail to simplify, leading to a static error, because it contains a
constraint of the form C (m t ) where m is one of the the type variable being generalized.
That is, the class C applies to a type expression that is not a type variable or a type
constructor. For example:, the
f x = show (return x)
The type of return is Monad m => a -> m a; the type of show is Show a => a -> String.
The type of f should be (Monad m, Show (m a)) => a -> String. The context to be
simplied will therefore be (Monad m, Show (m a)), which cannot be further reduced,
resulting in a static error.
Code generated by derived instance functions (see Section 4.3.3) may lead to general-
ization errors. For example, in the type
data Apply a b = App (a b) deriving Show
the derived Show instance will produce a context Show (a b), which cannot be reduced and
thus results in a static error. Context reduction error may also arise from strictness
ags in
data declarations (see Section 4.2.1) and the implicitly derived Eval instance in newtype
declarations (see Section 4.2.3).
4.5.4 Monomorphism
Sometimes it is not possible to generalize over all the type variables used in the type of the
denition. For example, consider the declaration
f x = let g y z = ([x,y], z)
in ...
In an environment where x has type a , the type of g's denition is a ! b ! ([a ]; b ).
The generalization step attributes to g the type 8 b : a ! b ! ([a ]; b ); only b can be
universally quantied because a occurs in the type environment. We say that the type of g
is monomorphic in the type variable a .
The eect of such monomorphism is that the rst argument of all applications of g must
be of a single type. For example, it would be valid for the \..." to be
(g True, g False)
(which would, incidentally, force x to have type Bool) but invalid for it to be
(g True, g 'c')
4.5 Static Semantics of Function and Pattern Bindings 51
Rule 1 is required for two reasons, both of which are fairly subtle. First, it prevents com-
putations from being unexpectedly repeated. For example, genericLength is a standard
function (in library List) whose type is given by
genericLength :: Num a => [b] -> a
Now consider the following expression:
let { len = genericLength xs } in (len, len)
It looks as if len should be computed only once, but without Rule 1 it might be computed
twice, once at each of two dierent overloadings. If the programmer does actually wish the
computation to be repeated, an explicit type signature may be added:
let { len :: Num a => a; len = genericLength xs } in (len, len)
When non-simple pattern bindings are used, the types inferred are always monomorphic in
their constrained type variables, irrespective of whether a type signature is provided. For
example, in
(f,g) = ((+),(-))
both f and g are monomorphic regardless of any type signatures supplied for f or g.
Rule 1 also prevents ambiguity. For example, consider the declaration group
[(n,s)] = reads t
Recall that reads is a standard function whose type is given by the signature
reads :: (Read a) => String -> [(a,String)]
Without Rule 1, n would be assigned the type 8 a : Read a ) a and s the type 8 a :
Read a ) String. The latter is an invalid type, because it is inherently ambiguous. It is
not possible to determine at what overloading to use s. Rule 1 makes n and s monomorphic
in a .
Lastly, Rule 2 is required because there is no way to enforce monomorphic use of an
exported binding, except by performing type inference on modules outside the current
module. Exported variables are handled in the same way as non-exported ones even though
their usage outside the module could theoreticly be used to determine monomorphic type.
For example, in the program
module M(x) where
x = 1
the monomorphism restriction prevents the type of x from being generalized to Num a => a.
Since references to x outside module M cannot be used to determine the type of x, the
defaulting rule (see Section 4.3.4) assigns the type Int to x.
The monomorphism rule has a number of consequences for the programmer. Anything
dened with function syntax usually generalizes as a function is expected to. Thus in
f x y = x+y
the function f may be used at any overloading in class Num. There is no danger of recom-
putation here. However, the same function dened with pattern syntax:
4.6 Kind Inference 53
requires a type signature if f is to be fully overloaded. Many functions are most naturally
dened using simple pattern bindings; the user must be careful to ax these with type
signatures to retain full overloading. The standard prelude contains many examples of this:
sum :: (Num a) => [a] -> a
sum = foldl (+) 0
This would give kinds ( ! ) ! ! and ! for App and Tree, respectively, for
any kind , and would require an extension to allow polymorphic kinds. Instead, using the
default binding = , the actual kinds for these two constructors are ( ! ) ! ! and
! , respectively.
Defaults are applied to each dependency group without consideration of the ways in
which particular type constructor constants or classes are used in later dependency groups
or elsewhere in the program. For example, adding the following denition to those above
do not in
uence the kind inferred for Tree (by changing it to ( ! ) ! , for instance),
and instead generates a static error because the kind of [], ! , does not match the kind
that is expected for an argument of Tree:
54 4. DECLARATIONS AND BINDINGS
This is important because it ensures that each constructor and class are used consistently
with the same kind whenever they are in scope.
55
5 Modules
A module denes a collection of values, datatypes, type synonyms, classes, etc. (see Sec-
tion 4) in an environment created by a set of imports, resources brought into scope from
other modules, and exports some of these resources, making them available to other mod-
ules. We use the term entity to refer to a value, type, or classes dened in, imported into,
or perhaps exported from a module.
A Haskell program is a collection of modules, one of which, by convention, must be
called Main and must export the value main. The value of the program is the value of the
identier main in module Main, and main must have type IO () (see Section 7).
Modules may reference other modules via explicit import declarations, each giving the
name of a module to be imported and specifying its entities to be imported. Modules may
be mutually recursive.
The name-space for modules is
at, with each module being associated with a unique
module name (which are Haskell identiers beginning with a capital letter; i.e. modid ).
There is one distinguished module, Prelude, which is imported into all programs by default
(see Section 5.3), plus a set of standard library modules that may be imported as required
(see the Haskell Library Report[8]).
5. The set of all entities brought into scope from a module m by one or more unqualied
import declarations may be named by the form `module m ', which is equivalent to
listing all of the entities imported from the module. For example:
module Queue( module Stack, enqueue, dequeue ) where
import Stack
...
Here the module Queue uses the module name Stack in its export list to abbreviate
all the entities imported from Stack.
6. A module can name its own local denitions in its export list using its name in the
`module m' syntax. For example:
module Mod1(module Mod1, module Mod2) where
import Mod2
import Mod3
Here module Mod1 exports all local denitions as well as those imported from Mod2
but not those imported from Mod3. Note that module M where is the same as using
module M(module M) where.
The qualier (see Section 5.1.2) on a name only identies the module an entity is imported
from; this may be dierent from the module in which the entity is dened. For example, if
module A exports B.c, this is referenced as `A.c', not `A.B.c'. In consequence, names in
export lists must remain distinct after qualiers are removed. For example:
module A ( B.f, C.f, g, B.g ) where -- an invalid module
import qualified B(f,g)
import qualified C(f)
g = True
There are name clashes in the export list between B.f and C.f and between g and B.g even
though there are no name clashes within module A.
The entities exported by a module may be brought into scope in another module with
an import declaration at the beginning of the module. The import declaration names the
module to be imported and optionally species the entities to be imported. A single module
58 5. MODULES
may be imported by more than one import declaration. Imported names serve as top level
declarations: they scope over the entire body of the module but may be shadowed by local
non-top-level bindings. The eect of multiple import declarations is cumulative: an entity
is in scope if it is named by any of the import declarations in a module. The ordering of
imports is irrelevant.
Exactly which entities are to be imported can be specied in one of three ways:
1. The imported entities can be specied explicitly by listing them in parentheses. Items
in the list have the same form as those in export lists, except qualiers are not permit-
ted and the `module modid ' entity is not permitted. When the (..) form of import
is used for a type or class, the (..) refers to all of the constructors, methods, or eld
names exported from the module.
The list must name only entities exported by the imported module. The list may be
empty, in which case nothing except the instances are imported.
2. Entities can be excluded by using the form hiding(import1 , : : : , importn ), which
species that all entities exported by the named module should imported except for
those named in the list. Data constructors may be named directly in hiding lists
without being prexed by the associated type. Thus, in
import M hiding (C)
any constructor, class, or type named C is excluded. In contrast, using C in an import
list names only a class or type. The hiding clause only applies to unqualied names.
In the previous example, the name M.C is brought into scope. A hiding clause has no
eect in an import qualified declaration.
The eect of multiple import declarations is strictly cumulative: hiding an entity
on one import declaration does not prevent the same entity from being imported by
another import from the same module.
3. Finally, if impspec is omitted then all the entities exported by the specied module
are imported.
When an import declaration uses the qualified keyword, the names brought into scope
must be prexed by the name of the imported module (or a local alias, if an as clause is
present). A qualied name is written as modid .name . This allows full programmer control
of the unqualied namespace: a locally dened entity can share the same name as a qualied
import:
module Ring where
import qualified Prelude -- All Prelude names must be qualified
l1 + l2 = l1 ++ l2 -- This + differs from the one in the Prelude
l1 * l2 = nub (l1 + l2)
succ = (Prelude.+ 1)
The qualier does not change the syntactic treatment of a name: Prelude.+ is an inx
operator with the same xity as the denition of + in the Prelude. Qualiers may be
5.1 Module Structure 59
It may be that a particular entity is imported into a module by more than one route
| for example, because it is exported by two modules, both of which are imported by a
third module. Benign name clashes of this form are allowed, but it is a static error for two
dierent entities to have the same name. When two entities have the same name, they are
considered to be the same object if and only if they are dened by the same module. Two
dierent qualied names may refer to the same entity; the name of the importing module
does not aect the identity of an entity.
It is an error for two dierent entities to have the same name. This is valid:
module A
import B(f)
import qualified C(f)
as long as only one imported f is unqualied and f is not dened at the top level of A.
Qualiers are the only way to resolve name clashes between imported entities.
5.2 Closure
Every module in a Haskell program must be closed. That is, every name explicitly mentioned
by the source code must be either dened locally or imported from another module. Entities
that the compiler requires for type checking or other compile time analysis need not be
imported if they are not mentioned by name. The Haskell compilation system is responsible
for nding any information needed for compilation without the help of the programmer.
That is, the import of a variable x does not require that the datatypes and classes in the
signature of x be brought into the module along with x unless these entities are referenced
by name in the user program. The Haskell system silently imports any information that
must accompany an entity for type checking or any other purposes. Such entities need not
even be explicitly exported: the following program is valid even though T does not escape
M1:
module M1(x) where
data T = T
x = T
module M2 where
import M1(x)
y = x
In this example, there is no way to supply an explicit type signature for y since T is not
in scope. Whether or not T is explicitly exported, module M2 knows enough about T to
correctly type check the program.
The type of an exported entity is unaected by non-exported type synonyms. For
example, in
module M(x) where
type T = Int
x :: T
x = 1
the type of x is both T and Int; these are interchangeable even when T is not in scope. That
is, the denition of T is available to any module that encounters it whether or not the name
T is in scope. The only reason to export T is to allow other modules to refer it by name;
the type checker nds the denition of T if needed whether or not it is exported.
Prelude and library modules dier from other modules in that their semantics (but not
their implementation) are a xed part of the Haskell language denition. This means, for
example, that a compiler may optimize calls to functions in the Prelude without being
concerned that a future change to the program will alter the semantics of the Prelude
function.
B imports nothing from Prelude, but the explicit import qualified Prelude declaration
prevents the automatic import of Prelude. import MyPrelude brings the non-standard
prelude into scope. As before, the standard prelude names are hidden explicitly. Special
syntax, such as lists or tuples, always refers to prelude entities: there is no way to redene
the meaning of [x] in terms of a dierent implementation of lists.
A xity declaration gives the xity and binding precedence of a set of operators. Fixity
declarations must appear only at the start of a module and may only be given for identiers
dened in that module. Fixity declarations cannot subsequently be overridden, and an
identier can only have one xity denition.
There are three kinds of xity, non-, left- and right-associativity (infix, infixl, and
infixr, respectively), and ten precedence levels, 0 to 9 inclusive (level 0 binds least tightly,
and level 9 binds most tightly). If the digit is omitted, level 9 is assumed. Any operator
lacking a xity declaration is assumed to be infixl 9 (See Section 3 for more on the use of
xities). Table 2 lists the xities and precedences of the operators dened in the Prelude.
6.1.1 Booleans
data Bool = False | True deriving
(Read, Show, Eq, Ord, Enum, Bounded)
The boolean type Bool is an enumeration. The basic boolean functions are && (and), ||
(or), and not. The name otherwise is dened as True to make guarded expressions more
readable.
Strings may be abbreviated using the lexical syntax described in Section 2.5. For example,
"A string" abbreviates
[ 'A',' ','s','t','r', 'i','n','g']
6.1 Standard Haskell Types 65
6.1.3 Lists
data [a] = [] | a : [a] deriving (Eq, Ord)
Lists are an algebraic datatype of two constructors, although with special syntax, as de-
scribed in Section 3.7. The rst constructor is the null list, written `[]' (\nil"), and the
second is `:' (\cons"). The module PreludeList (see Appendix A.1) denes many standard
list functions. Arithmetic sequences and list comprehensions, two convenient syntaxes for
special kinds of lists, are described in Sections 3.10 and 3.11, respectively. Lists are an
instance of classes Read, Show, Eq, Ord, Monad, MonadZero, and MonadPlus.
6.1.4 Tuples
Tuples are algebraic datatypes with special syntax, as dened in Section 3.8. Each tuple
type has a single constructor. There is no upper bound on the size of a tuple. However, some
Haskell implementations may restrict the size of tuples and limit the instances associated
with larger tuples. The Prelude and libraries dene tuple functions such as zip for tuples
up to a size of 7. All tuples are instances of Eq, Ord, Bounded, Read, and Show. Classes
dened in the libraries may also supply instances for tuple types. The constructor for a tuple
is written by omitting the expressions surrounding the commas: thus (x,y) and (,) x y
produce the same value. The following functions are dened for pairs (2-tuples): fst, snd,
curry, and uncurry. Similar functions are not predened for larger tuples.
The unit datatype () has one non-? member, the nullary constructor (). See also Sec-
tion 3.9.
The Bounded class is used to name the upper and lower limits of a type. Ord is not
a superclass of Bounded since types that are not totally ordered may also have upper and
lower bounds. The types Int, Char, Bool, (), Ordering, and all tuples are instances of
Bounded. The Bounded class may be derived for any enumeration type; minBound is the
rst constructor listed in the data declaration and maxBound is the last. Bounded may also
be derived for single-constructor datatypes whose constituent types are in Bounded.
6.3 Numbers
Haskell provides several kinds of numbers; the numeric types and the operations upon
them have been heavily in
uenced by Common Lisp and Scheme. Numeric function names
and operators are usually overloaded, using several type classes with an inclusion relation
shown in Figure 5, page 67. The class Num of numeric types is a subclass of Eq, since
all numbers may be compared for equality; its subclass Real is also a subclass of Ord,
since the other comparison operations apply to all but complex numbers (dened in the
Complex library). The class Integral contains both xed- and arbitrary-precision integers;
the class Fractional contains all non-integral types; and the class Floating contains all
oating-point types, both real and complex.
The Prelude denes only the most basic numeric types: xed sized integers (Int), ar-
bitrary precision integers (Integer), single precision
oating (Float), and double precision
oating (Double). Other numeric types such as rationals and complex numbers are dened
in libraries. In particular, the type Rational is a ratio of two Integer values, as dened
in the Rational library.
The default
oating point operations dened by the Haskell Prelude do not conform to
current language independent arithmetic (LIA) standards. These standards require consid-
erable more complexity in the numeric structure and have thus been relegated to a library.
Some, but not all, aspects of the IEEE standard
oating point standard have been accounted
for in class RealFloat.
Table 3 lists the standard numeric types. The type Int covers at least the range
[ ? 2 29 ; 2 29 ? 1 ]. As Int is an instance of the Bounded class, maxBound and minBound
can be used to determine the exact Int range dened by an implementation. Float is
6.3 Numbers 73
implementation-dened; it is desirable that this type be at least equal in range and precision
to the IEEE single-precision type. Similarly, Double should cover IEEE double-precision.
The results of exceptional conditions (such as over
ow or under
ow) on the xed-precision
numeric types are undened; an implementation may choose error (?, semantically), a
truncated value, or a special value such as innity, indenite, etc.
The standard numeric classes and other numeric functions dened in the Prelude are
shown in Figures 6{7. Figure 5 shows the class dependencies and built-in types that are
instances of the numeric classes.
`quot` is integer division truncated toward zero, while the result of `div` is truncated
toward negative innity. The quotRem class method takes a dividend and a divisor as
arguments and returns a (quotient, remainder) pair; divMod is dened similarly:
quotRem x y = (x quot y, x rem y)
divMod x y = (x div y, x mod y)
Also available on integral numbers are the even and odd predicates:
even x = x rem 2 == 0
odd = not . even
Finally, there are the greatest common divisor and least common multiple functions: gcd x
y is the greatest integer that divides both x and y . lcm x y is the smallest positive integer
that both x and y divide.
6.3 Numbers 75
abs x | x >= 0 = x
| x < 0 = -x
signum x | x > 0 = 1
| x == 0 = 0
| x < 0 = -1
Two functions convert numbers to type Rational: toRational returns the rational
equivalent of its real argument with full precision; approxRational takes two real fractional
arguments x and and returns the simplest rational number within of x , where a rational
p=q in reduced form is simpler than another p0=q 0 if jpj jp0j and q q 0. Every real interval
contains a unique simplest rational; in particular, note that 0=1 is the simplest rational of
all.
The class methods of class RealFloat allow ecient, machine-independent access to
the components of a
oating-point number. The functions floatRadix, floatDigits, and
floatRange give the parameters of a
oating-point type: the radix of the representation,
the number of digits of this radix in the signicand, and the lowest and highest values the
exponent may assume, respectively. The function decodeFloat applied to a real
oating-
point number returns the signicand expressed as an Integer and an appropriately scaled
exponent (an Int). If decodeFloat x yields (m ,n ), then x is equal in value to mb n ,
6.3 Numbers 77
where b is the
oating-point radix, and furthermore, either m and n are both zero or
else b d ?1 m < b d , where d is the value of floatDigits x. encodeFloat performs the
inverse of this transformation. The functions significand and exponent together provide
the same information as decodeFloat, but rather than an Integer, significand x yields
a value of the same type as x, scaled to lie in the open interval (?1 ; 1 ). exponent 0 is zero.
scaleFloat multiplies a
oating-point number by an integer power of the radix.
7 Basic Input/Output
The I/O system in Haskell is purely functional, yet has all of the expressive power found in
conventional programming languages. To achieve this, Haskell uses a monad to integrate
I/O operations into a purely functional context.
The I/O monad used by Haskell mediates between the values natural to a functional
language and the actions that characterize I/O operations and imperative programming
in general. The order of evaluation of expressions in Haskell is constrained only by data
dependencies; an implementation has a great deal of freedom in choosing this order. Actions,
however, must be ordered in a well-dened manner for program execution { and I/O in
particular { to be meaningful. Haskell's I/O monad provides the user with a way to specify
the sequential chaining of actions, and an implementation is obliged to preserve this order.
The term monad comes from a branch of mathematics known as category theory. From
the perspective of a Haskell programmer, however, it is best to think of a monad as an
abstract datatype. In the case of the I/O monad, the abstract values are the actions men-
tioned above. Some operations are primitive actions, corresponding to conventional I/O
operations. Special operations (methods in the class Monad, see Section 6.2.5) sequentially
compose actions, corresponding to sequencing operators (such as the semi-colon) in imper-
ative languages.
Output Functions These functions write to the standard output device (this is normally
the user's terminal).
putChar :: Char -> IO ()
putStr :: String -> IO ()
putStrLn :: String -> IO () -- adds a newline
print :: Show a => a -> IO ()
The print function outputs a value of any printable type to the standard output device
(this is normally the user's terminal). Printable types are those that are instances of class
Show; print converts values to strings for output using the show operation and adds a
newline.
For example, a program to print the rst 20 integers and their powers of 2 could be
written as:
7.1 Standard I/O Functions 79
Input Functions These functions read input from the standard input device (normally
the user's terminal).
getChar :: IO Char
getLine :: IO String
getContents :: IO String
interact :: (String -> String) -> IO ()
readIO :: Read a => String -> IO a
readLine :: Read a => IO a
Both getChar and getLine raise an exception on end-of-le; the IOError value associated
with end-of-le is dened in a library. The getContents operation returns all user input as
a single string, which is read lazily as it is needed. The interact function takes a function
of type String->String as its argument. The entire input from the standard input device
(normally the user's terminal) is passed to this function as its argument, and the resulting
string is output on the standard output device. The readIO function is similar to read
except that it signals parse failure to the I/O monad instead of terminating the program.
The readLine function combines getLine and readIO.
By default, these input functions echo to standard output. Functions in the I/O library
provide full control over echoing.
The following program simply removes all non-ASCII characters from its standard input
and echoes the result on its standard output. (The isAscii function is dened in a library.)
main = interact (filter isAscii)
Files These functions operate on les of characters. Files are named by strings using some
implementation-specic method to resolve strings as le names.
The writeFile and appendFile functions write or append the string, their second
argument, to the le, their rst argument. The readFile function reads a le and returns
the contents of the le as a string. The le is read lazily, on demand, as with getContents.
type FilePath = String
writeFile :: FilePath -> String -> IO ()
appendFile :: FilePath -> String -> IO ()
readFile :: FilePath -> IO String
Note that writeFile and appendFile write a literal string to a le. To write a value of
any printable type, as with print, use the show function to convert the value to a string
rst.
main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
80 7. BASIC INPUT/OUTPUT
The return function is used to dene the result of an I/O operation. For example,
getLine is dened in terms of getChar, using return to dene the result the monad:
getLine :: IO String
getLine = do c <- getChar
if c == '\n' then return ""
else do s <- getLine
return (c:s)
Prelude function that creates an IOError value is userError. User error values include a
string describing the error.
Exceptions are raised and caught using the following functions:
fail :: IOError -> IO a
catch :: IO a -> (IOError -> IO a) -> IO a
The fail function raises an exception; the catch function establishes a handler that receives
any exception raised in the action protected by catch. An exception is caught by the most
recent handler established by catch. These handlers are not selective: all exceptions are
caught. Exception propagation must be explicitly provided in a handler by re-raising any
unwanted exceptions. For example, in
f = catch g (\e -> if IO.isEOFError e then return [] else fail e)
the function f returns [] when an end-of-le exception occurs in g; otherwise, the exception
is propagated to the next outer handler. The isEOFError function is part of IO library.
When an exception propagates outside the main program, the Haskell system prints the
associated IOError value and exits the program.
The exceptions raised by the I/O functions in the Prelude are dened in the Library
Report.
82 A. STANDARD PRELUDE
A Standard Prelude
In this appendix the entire Haskell prelude is given. It is organized into a root module
and three sub-modules. Primitives that are not denable in Haskell, indicated by names
starting with prim, are dened in a system dependent manner in module PreludeBuiltin
and are not shown here. Instance declarations that simply bind primitives to class methods
are omitted. Some of the more verbose instances with obvious functionality have been left
out for the sake of brevity.
Declarations for special types such as Integer, (), or (->) are included in the Prelude
for completeness even though the declaration may be incomplete or syntactically invalid.
83
module Prelude (
module PreludeList, module PreludeText, module PreludeIO,
Bool(False, True),
Maybe(Nothing, Just),
Either(Left, Right),
Ordering(LT, EQ, GT),
Char, String, Int, Integer, Float, Double, IO, Void,
Ratio, Rational,
-- List type: []((:), [])
-- Tuple types: (,), (,,), etc.
-- Trivial type: ()
-- Functions: (->)
Eq((==), (/=)),
Ord(compare, (<), (<=), (>=), (>), max, min),
Enum(toEnum, fromEnum, enumFrom, enumFromThen,
enumFromTo, enumFromThenTo),
Bounded(minBound, maxBound),
Eval(seq, strict),
Num((+), (-), (*), negate, abs, signum, fromInteger),
Real(toRational),
Integral(quot, rem, div, mod, quotRem, divMod, toInteger),
Fractional((/), recip, fromRational),
Floating(pi, exp, log, sqrt, (**), logBase, sin, cos, tan,
asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh),
RealFrac(properFraction, truncate, round, ceiling, floor),
RealFloat(floatRadix, floatDigits, floatRange, decodeFloat,
encodeFloat, exponent, significand, scaleFloat, isNaN,
isInfinite, isDenormalized, isIEEE, isNegativeZero),
Monad((>>=), (>>), return),
MonadZero(zero),
MonadPlus((++)),
Functor(map),
succ, pred,
mapM, mapM_, guard, accumulate, sequence, filter, concat, applyM,
maybe, either,
(&&), (||), not, otherwise,
subtract, even, odd, gcd, lcm, (^), (^^),
fromIntegral, fromRealFrac, atan2,
fst, snd, curry, uncurry, id, const, (.), flip, ($), until,
asTypeOf, error, undefined ) where
84 A. STANDARD PRELUDE
infixr 9 .
infixr 8 ^, ^^, **
infixl 7 *, /, `quot`, `rem`, `div`, `mod`
infixl 6 +, -
infixr 5 :, ++
infix 4 ==, /=, <, <=, >=, >
infixr 3 &&
infixr 2 ||
infixl 1 >>, >>=
infixr 0 $, `seq`
-- Numeric classes
class (Eq a, Show a, Eval a) => Num a where
(+), (-), (*) :: a -> a -> a
negate :: a -> a
abs, signum :: a -> a
fromInteger :: Integer -> a
x - y = x + negate y
-- Numeric functions
-- Monadic classes
-- Eval Class
-- Trivial type
-- Function type
-- identity function
id :: a -> a
id x = x
-- constant function
const :: a -> b -> a
const x _ = x
-- function composition
(.) :: (b -> c) -> (a -> b) -> a -> c
f . g = \ x -> f (g x)
-- Empty type
data Void -- No constructor for Void is exported. Import/Export
-- lists must use Void instead of Void(..) or Void()
-- Boolean type
data Bool = False | True deriving (Eq, Ord, Enum, Read, Show, Bounded)
-- Boolean functions
(&&), (||) :: Bool -> Bool -> Bool
True && x = x
False && _ = False
True || _ = True
False || x = x
otherwise :: Bool
otherwise = True
91
-- Character type
-- Maybe type
data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show)
-- Either type
-- IO type
data IO a -- abstract
-- Ordering type
data Ordering = LT | EQ | GT
deriving (Eq, Ord, Enum, Read, Show, Bounded)
-- Standard numeric types. The data declarations for these types cannot
-- be expressed directly in Haskell since the constructor lists would be
-- far too large.
data Int = minBound ... -1 | 0 | 1 ... maxBound
instance Eq Int where ...
instance Ord Int where ...
instance Num Int where ...
instance Real Int where ...
instance Integral Int where ...
instance Enum Int where ...
instance Bounded Int where ...
data Float
instance Eq Float where ...
instance Ord Float where ...
instance Num Float where ...
instance Real Float where ...
instance Fractional Float where ...
instance Floating Float where ...
instance RealFrac Float where ...
instance RealFloat Float where ...
data Double
instance Eq Double where ...
instance Ord Double where ...
instance Num Double where ...
instance Real Double where ...
instance Fractional Double where ...
instance Floating Double where ...
instance RealFrac Double where ...
instance RealFloat Double where ...
-- The Enum instances for Floats and Doubles are slightly unusual.
-- The `toEnum' function truncates numbers to Int. The definitions
-- of enumFrom and enumFromThen allow floats to be used in arithmetic
-- series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
-- dubious. This example may have either 10 or 11 elements, depending on
-- how 0.1 is represented.
instance Enum Float where
toEnum = fromIntegral
fromEnum = fromInteger . truncate -- may overflow
enumFrom = numericEnumFrom
enumFromThen = numericEnumFromThen
enumFromTo = numericEnumFromTo
enumFromThenTo = numericEnumFromThenTo
-- Lists
data [a] = [] | a : [a] deriving (Eq, Ord)
-- Tuples
data (a,b) = (a,b) deriving (Eq, Ord, Bounded)
data (a,b,c) = (a,b,c) deriving (Eq, Ord, Bounded)
-- Misc functions
module PreludeList (
head, last, tail, init, null, length, (!!),
foldl, foldl1, scanl, scanl1, foldr, foldr1, scanr, scanr1,
iterate, repeat, replicate, cycle,
take, drop, splitAt, takeWhile, dropWhile, span, break,
lines, words, unlines, unwords, reverse, and, or,
any, all, elem, notElem, lookup,
sum, product, maximum, minimum, concatMap,
zip, zip3, zipWith, zipWith3, unzip, unzip3)
where
infixl 9 !!
infix 4 `elem`, `notElem`
-- head and tail extract the first element and remaining elements,
-- respectively, of a list, which must be non-empty. last and init
-- are the dual functions working from the end of a finite list,
-- rather than the beginning.
head :: [a] -> a
head (x:_) = x
head [] = error "PreludeList.head: empty list"
-- foldr, foldr1, scanr, and scanr1 are the right-to-left duals of the
-- above functions.
-- sum and product compute the sum or product of a finite list of numbers.
sum, product :: (Num a) => [a] -> a
sum = foldl (+) 0
product = foldl (*) 1
-- maximum and minimum return the maximum or minimum value from a list,
-- which must be non-empty, finite, and of an ordered type.
maximum, minimum :: (Ord a) => [a] -> a
maximum [] = error "PreludeList.maximum: empty list"
maximum xs = foldl1 max xs
-- zip takes two lists and returns a list of corresponding pairs. If one
-- input list is short, excess elements of the longer list are discarded.
-- zip3 takes three lists and returns a list of triples. Zips for larger
-- tuples are in the List library
zip :: [a] -> [b] -> [(a,b)]
zip = zipWith (,)
-- The zipWith family generalises the zip family by zipping with the
-- function given as the first argument, instead of a tupling function.
-- For example, zipWith (+) is applied to two lists to produce the list
-- of corresponding sums.
-- Tuples
instance (Show a, Show b) => Show (a,b) where
showsPrec p (x,y) = showChar '(' . shows x . showChar ',' .
shows y . showChar ')'
-- Functions
instance Show (a->b) where
showsPrec p f = showString "<<function>>"
import PreludeBuiltin
getChar :: IO Char
getChar = primGetChar
getLine :: IO String
getLine = do c <- getChar
if c == '\n' then return "" else
do s <- getLine
return (c:s)
110 A. STANDARD PRELUDE
getContents :: IO String
getContents = primGetContents
B Syntax
B.1 Notational Conventions
These notational conventions are used for presenting syntax:
[pattern ] optional
fpattern g zero or more repetitions
(pattern ) grouping
pat1 j pat2 choice
pathpat i dierence|elements generated by pat
0
specialop ! - j!
varid (variables)
conid (constructors)
tyvar ! varid (type variables)
tycon ! conid (type constructors)
tycls ! conid (type classes)
modid ! conid (modules)
qvarid ! [ modid . ] varid
qconid ! [ modid . ] conid
qtycon ! [ modid . ] tycon
qtycls ! [ modid . ] tycls
qvarsym ! [ modid . ] varsym
qconsym ! [ modid . ] consym
decimal ! digit fdigit g
octal ! octit foctit g
hexadecimal! hexit fhexit g
integer ! decimal
j 0o octal j 0O octal
j 0x hexadecimal j 0X hexadecimal
oat ! decimal . decimal [(e j E)[- j +]decimal ]
char ! ' (graphich' j \i j space j escapeh\&i) '
string ! " fgraphich" j \i j space j escape j gap g "
escape ! \ ( charesc j ascii j decimal j o octal j x hexadecimal )
charesc ! ajbjfjnjrjtjvj\j"j'j&
ascii ! ^cntrl j NUL j SOH j STX j ETX j EOT j ENQ j ACK
j BEL j BS j HT j LF j VT j FF j CR j SO j SI j DLE
j DC1 j DC2 j DC3 j DC4 j NAK j SYN j ETB j CAN
j EM j SUB j ESC j FS j GS j RS j US j SP j DEL
cntrl ! ASClarge j @ j [ j \ j ] j ^ j _
gap ! \ whitechar fwhitechar g \
B.3 Layout
Denitions: The indentation of a lexeme is the column number indicating the start of that
lexeme; the indentation of a line is the indentation of its leftmost lexeme. To determine
the column number, assume a xed-width font with this tab convention: tab stops are 8
characters apart, and a tab character causes the insertion of enough spaces to align the
current position with the next tab stop.
114 B. SYNTAX
In the syntax given in the rest of the report, layout lists are always preceded by the key-
word where, let, do, or of, and are enclosed within curly braces ({ }) with the individual
declarations separated by semicolons (;). Layout lists usually contain declarations, but do
and case introduce lists of other sorts. For example, the syntax of a let expression is:
let { decl1 ; decl2 ; ::: ; decln [;] } in exp
Haskell permits the omission of the braces and semicolons by using layout to convey the
same information. This allows both layout-sensitive and -insensitive styles of coding, which
can be freely mixed within one program. Because layout is not required, Haskell programs
can be straightforwardly produced by other programs.
The layout (or \o-side") rule takes eect whenever the open brace is omitted after the
keyword where, let, do, or of. When this happens, the indentation of the next lexeme
(whether or not on a new line) is remembered and the omitted open brace is inserted (the
whitespace preceding the lexeme may include comments). For each subsequent line, if it
contains only whitespace or is indented more, then the previous item is continued (nothing
is inserted); if it is indented the same amount, then a new item begins (a semicolon is
inserted); and if it is indented less, then the layout list ends (a close brace is inserted). A
close brace is also inserted whenever the syntactic category containing the layout list ends;
that is, if an illegal lexeme is encountered at a point where a close brace would be legal, a
close brace is inserted. The layout rule matches only those open braces that it has inserted;
an explicit open brace must be matched by an explicit close brace. Within these explicit
open braces, no layout processing is performed for constructs outside the braces, even if a
line is indented to the left of an earlier implicit open brace.
Given these rules, a single newline may actually terminate several layout lists. Also,
these rules permit:
f x = let a = 1; b = 2
g y = exp2
in exp1
making a, b and g all part of the same layout list.
To facilitate the use of layout at the top level of a module (an implementation may
allow several modules may reside in one le), the keyword module and the end-of-le token
are assumed to occur in column 0 (whereas normally the rst column is 1). Otherwise, all
top-level declarations would have to be indented.
Section 1.5 gives an example that uses the layout rule.
B.4 Context-Free Syntax 115
xdecls ! x1 ; : : : ; xn (n 1 )
x ! infixl [digit ] ops
j infixr [digit ] ops
j infix [digit ] ops
ops ! op1 , : : : , opn (n 1 )
inst ! gtycon
j ( gtycon tyvar1 : : : tyvark ) (k 0 ; tyvars distinct)
j ( tyvar1 , : : : , tyvark ) (k 2 ; tyvars distinct)
j [ tyvar ]
j ( tyvar1 -> tyvar2 ) tyvar1 and tyvar2 distinct
valdefs ! valdef1 ; : : : ; valdefn (n 0 )
j _ (wildcard)
j ( pat ) (parenthesized pattern)
j ( pat1 , : : : , patk ) (tuple pattern; k 2 )
j [ pat1 , : : : , patk ] (list pattern; k 1 )
j ~ apat (irrefutable pattern)
fpat ! var = pat
j var
gcon ! ()
j []
j (,f,g)
j qcon
var ! varid j ( varsym ) (variable)
qvar ! qvarid j ( qvarsym ) (qualied variable)
con ! conid j ( consym ) (constructor)
qcon ! qconid j ( qconsym ) (qualied constructor)
varop ! varsym j varid (variable operator)
qvarop ! qvarsym j qvarid (qualied variable operator)
conop ! consym j conid (constructor operator)
qconop ! qconsym j qconid (qualied constructor operator)
op ! varop j conop (operator)
qop ! qvarop j qconop (qualied operator)
120 C. LITERATE COMMENTS
C Literate comments
The \literate comment" convention, rst developed by Richard Bird and Philip Wadler for
Orwell, and inspired in turn by Donald Knuth's \literate programming", is an alternative
style for encoding Haskell source code. The literate style encourages comments by making
them the default. A line in which \>" is the rst character is treated as part of the program;
all other lines are comment. Within the program part, the usual \--" and \{- -}" comment
conventions may still be used. To capture some cases where one omits an \>" by mistake,
it is an error for a program line to appear adjacent to a non-blank comment line, where a
line is taken as blank if it consists only of whitespace.
By convention, the style of comment is indicated by the le extension, with \.hs"
indicating a usual Haskell le and \.lhs" indicating a literate Haskell le. Using this style,
a simple factorial program would be:
This program prompts the user for a number and prints the factorial
of that number:
> main :: IO ()
> main = do putStr "Enter a number: "
> l <- readLine
> putStr "n!= "
> print (fact (read l))
This is the factorial function.
> fact :: Integer -> Integer
> fact 0 = 1
> fact n = n * fact (n-1)
An alternative style of literate programming is particularly suitable for use with the
LaTeX text processing system. In this convention, only those parts of the literate program
that are entirely enclosed between \begin{code}: : :\end{code} delimiters are treated as
program text; all other lines are comment. It is not necessary to insert additional blank
lines before or after these delimiters, though it may be stylistically desirable. For example,
\documentstyle{article}
\begin{document}
\section{Introduction}
This style uses the same le extension. It is not advisable to mix these two styles in the
same le.
122 D. SPECIFICATION OF DERIVED INSTANCES
where d is derived automatically depending on Ci and the data type declaration for T (as
will be described in the remainder of this section), and u10 through uj0 form a subset of u1
through uk . When inferring the context for the derived instances, type synonyms must be
expanded out rst. Free names in the declarations d are all dened in the Prelude; the
qualier `Prelude.' is implicit here. The remaining details of the derived instances for each
of the derivable Prelude classes are now given.
123
Derived instances of Eq and Ord. The class methods automatically introduced by de-
rived instances of Eq and Ord are (==), (/=), compare, (<), (<=), (>), (>=), max, and min.
The latter seven operators are dened so as to compare their arguments lexicographically
with respect to the constructor set given, with earlier constructors in the datatype declara-
tion counting as smaller than later ones. For example, for the Bool datatype, we have that
(True > False) == True.
Derived comparisons always traverse constructors from left to right. These examples
illustrate this property:
(1,undefined) == (2,undefined) ) False
(undefined,1) == (undefined,2) ) ?
Derived instances of Enum Derived instance declarations for the class Enum are only
possible for enumerations. The nullary constructors are assumed to be numbered left-
to-right with the indices 0 through n ? 1. Enum introduces the class methods toEnum,
fromEnum, enumFrom, enumFromThen, enumFromTo, and enumFromThenTo, which are used to
dene arithmetic sequences as described in Section 3.10.
The toEnum and fromEnum operators map enumerated values to and from the Int type.
enumFrom n returns a list corresponding to the complete enumeration of n's type starting at
the value n. Similarly, enumFromThen n n' is the enumeration starting at n, but with second
element n', and with subsequent elements generated at a spacing equal to the dierence
between n and n'. enumFromTo and enumFromThenTo are as dened by the default class
methods for Enum (see Figure 5, page 67). For example, given the datatype:
data Color = Red | Orange | Yellow | Green deriving (Enum)
we would have:
[Orange..] == [Orange, Yellow, Green]
fromEnum Yellow == 2
Derived instances of Bounded. The Bounded class introduces the class methods minBound
and maxBound, which dene the minimal and maximal elements of the type. For an enumer-
ation, the rst and last constructors listed in the data declaration are the bounds. For a
type with a single constructor, the constructor is applied to the bounds for the constituent
types. For example, the following datatype:
data Pair a b = Pair a b deriving Bounded
Derived instances of Read and Show. The class methods automatically introduced by
derived instances of Read and Show are showsPrec, readsPrec, showList, and readList.
They are used to coerce values into strings and parse strings into values.
The function showsPrec d x r accepts a precedence level d (a number from 0 to 10),
a value x, and a string r. It returns a string representing x concatenated to r. showsPrec
satises the law:
showsPrec d x r ++ s == showsPrec d x (r ++ s)
The representation will be enclosed in parentheses if the precedence of the top-level con-
structor operator in x is less than d. Thus, if d is 0 then the result is never surrounded in
parentheses; if d is 10 it is always surrounded in parentheses, unless it is an atomic expres-
sion. The extra parameter r is essential if tree-like structures are to be printed in linear
time rather than time quadratic in the size of the tree.
The function readsPrec d s accepts a precedence level d (a number from 0 to 10) and
a string s, and returns a list of pairs (x,r) such that showsPrec d x r == s. readsPrec
is a parse function, returning a list of (parsed value, remaining string) pairs. If there is no
successful parse, the returned list is empty.
showList and readList allow lists of objects to be represented using non-standard
denotations. This is especially useful for strings (lists of Char).
readsPrec will parse any valid representation of the standard types apart from lists, for
which only the bracketed form [. . . ] is accepted. See Appendix A for full details.
A precise denition of the derived Read and Show instances for general types is beyond
the scope of this report. However, the derived Read and Show instances have the following
properties:
The result of show is a syntactically correct Haskell expression containing only con-
stants given the xity declarations in force at the point where the type is declared.
The result of show is readable by read if all component types are readable. (This
is true for all instances dened in the Prelude but may not be true for user-dened
instances.)
The instance generated by Read allows arbitrary whitespace between tokens on the
input string. Extra parenthesis are also allowed.
The result of show contains only the constructor names dened in the data type,
parenthesis, and spaces. When labeled constructor elds are used, braces, commas,
eld names, and equal signs are also used. No leading or trailing spaces are generated.
Parenthesis are only added where needed. No line breaks are added.
If a constructor is dened using labeled eld syntax then the derived show for that
constructor will this same syntax; the elds will be in the order declared in the data
declaration. The derived Read instance will use this same syntax: all elds must be
present and the declared order must be maintained.
D.1 An example 125
If a constructor is dened in the inx style, the derived Show instance will also use
inx style. The derived Read instance will require that the constructor be inx.
The derived Read and Show instances may be unsuitable for some uses. Some problems
include:
Circular structures cannot be printed or read by these instances.
The printer loses shared substructure; the printed representation of an object may be
much larger that necessary.
The parsing techniques used by the reader are very inecient; reading a large structure
may be quite slow.
There is no user control over the printing of types dened in the Prelude. For example,
there is no way to change the formatting of
oating point numbers.
D.1 An example
As a complete example, consider a tree datatype:
data Tree a = Leaf a | Tree a :^: Tree a
deriving (Eq, Ord, Read, Show)
Automatic derivation of instance declarations for Bounded and Enum are not possible, as Tree
is not an enumeration or single-constructor datatype. The complete instance declarations
for Tree are shown in Figure 8, Note the implicit use of default class method denitions|for
example, only <= is dened for Ord, with the other class methods (<, >, >=, max, and min)
being dened by the defaults given in the class declaration shown in Figure 5 (page 67).
126 D. SPECIFICATION OF DERIVED INSTANCES
infix 4 :^:
data Tree a = Leaf a | Tree a :^: Tree a
++ readParen (d > 9)
(\r -> [(Leaf m,t) |
("Leaf",s) <- lex r,
(m,t) <- readsPrec 10 s]) r
E Compiler Pragmas
Some compiler implementations support compiler pragmas, which are used to give additional
instructions or hints to the compiler, but which do not form part of the Haskell language
proper and do not change a program's semantics. This section summarizes this existing
practice. An implementation is not required to respect any pragma, but the pragma should
be ignored if an implementation is not prepared to handle it. Lexically, pragmas appear as
comments, except that the enclosing syntax is {-# #-}.
E.1 Inlining
decl ! {-# inline [digit ] qvars #-}
decl ! {-# notInline qvars #-}
The optional digit represents the level of optimization at which the inlining is to occur. If
omitted, it is assumed to be 0. A compiler may use a numeric optimization level setting
in which increasing level numbers indicate increasing amounts of optimization. Trivial
inlinings that have no impact on compilation time or code size should have an optimization
level of 0; more complex inlinings that may lead to slow compilation or large executables
should be associated with higher optimization levels.
Compilers will often automatically inline simple expressions. This may be prevented by
the notInline pragma.
E.2 Specialization
decl ! {-# specialize spec1 , : : : , speck #-} (k 1 )
spec ! vars :: type
Specialization is used to avoid ineciencies involved in dispatching overloaded functions.
For example, in
factorial :: Num a => a -> a
factorial 0 = 0
factorial n = n * factorial (n-1)
{-# specialize factorial :: Int -> Int,
factorial :: Integer -> Integer #-}
calls to factorial in that the compiler can detect that the parameter is either Int or
Integer will use specialized versions of factorial which do not involved overloaded nu-
meric operations.
128 E. COMPILER PRAGMAS
E.3 Optimization
decl ! optdecl
exp 0 ! optdecl exp 0
optdecl ! {-# optimize optd1 , : : : , optdk #-} (k 1 )
optd ! digit
j speed digit
j space digit
j compilationSpeed digit
j debug digit
The optimize pragma provides explicit control over the optimization levels of the compiler.
If used as a declaration, this applies to all values dened in the declaration group (and
recursively to any nested values). Used as an expression, it applies only to the prexed
expression. If no attribute is named, the speed attribute is assumed.
REFERENCES 129
References
[1] J. Backus. Can programming be liberated from the von Neumann style? A functional
style and its algebra of programs. CACM, 21(8):613{641, August 1978.
[2] H.B. Curry and R. Feys. Combinatory Logic. North-Holland Pub. Co., Amsterdam,
1958.
[3] L. Damas and R. Milner. Principal type schemes for functional programs. In Pro-
ceedings of the 9th ACM Symposium on Principles of Programming Languages, pages
207{212, Albuquerque, N.M., January 1982.
[4] R. Hindley. The principal type scheme of an object in combinatory logic. Transactions
of the American Mathematical Society, 146:29{60, December 1969.
[5] P. Hudak, J. Fasel, and J. Peterson. A gentle introduction to Haskell. Technical Report
YALEU/DCS/RR-901, Yale University, May 1996.
[6] Mark P. Jones. A system of constructor classes: overloading and implicit higher-order
polymorphism. Journal of Functional Programming, 5(1), January 1995.
[7] P. Peneld, Jr. Principal values and branch cuts in complex APL. In APL '81 Con-
ference Proceedings, pages 248{256, San Francisco, September 1981.
[8] J. Peterson (editor). The Haskell Library Report. Technical Report YALEU/DCS/RR-
1105, Yale University, May 1996.
[9] S.L. Peyton Jones. The Implementation of Functional Programming Languages.
Prentice-Hall International, Englewood Clis, New Jersey, 1987.
[10] Unicode Consortium. Unicode Character Data and Mappings. unicode.org.
[11] P. Wadler and S. Blott. How to make ad hoc polymorphism less ad hoc. In Proceedings
of the 16th ACM Symposium on Principles of Programming Languages, pages 60{76,
Austin, Texas, January 1989.
130 REFERENCES
Index
Index entries that refer to nonterminals in the Haskell syntax are shown in an italic font.
Code entities dened in the standard prelude (Appendix A) or in the Haskell Library
Report[8] are shown in typewriter font. Ordinary index entries are shown in a roman
font.
131
132 INDEX
whitechar, 7, 112
whitespace, 7, 112
whitestu, 7, 112
wildcard pattern (_), 25
words, 100
writeFile, 79, 110
zero, 70, 89
zip, 65, 101
zip3, 101
zipWith, 102
zipWith3, 102