4 @

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==0then rest else xs }

is equivalent to:

let{ xs = e }incase xs of{(x:rest)->if x==0then rest else xs }

5 !

Whenever a data constructor is applied, each argument to the
constructor is evaluated if and only if the corresponding type in the
algebraic datatype declaration has a strictness flag, denoted by an
exclamation point. For example:

data STList a
= STCons a !(STList a)-- the second argument to STCons will be -- evaluated before STCons is applied| STNil

to illustrate the difference between strict versus lazy constructor
application, consider the following:

! is also used in the "bang patterns" (GHC extension), to indicate
strictness in patterns:

f !x !y = x + y

Finally, it is the array subscript operator:

let x = arr !10

6 ::

Read as "has type":

length::[a]->Int

"Length has type list-of-'a' to Int"

7 _

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
identifier not used elsewhere were put in its place. For example,

case e of{[x,_,_]->if x==0then True else False }

is equivalent to:

case e of{[x,y,z]->if x==0then True else False }

8 ~

Lazy pattern bindings. Matching the pattern ~pat against a value always
suceeds, and matching will only diverge when one of the variables bound
in the pattern is used.

11 as

12 case, of

A case expression has the general form

case e of{ p1 match1 ; ... ; pn matchn }

where each matchi is of the general form

| g1 -> e1
...| gm -> em
where decls

Each alternative consists of patterns pi and their matches, matchi. Each
matchi in turn consists of a sequence of pairs of guards gij and bodies eij
(expressions), followed by optional bindings (declsi) that scope over all
of the guards and expressions of the alternative. An alternative of the
form

pat ->expwhere decls

is treated as shorthand for:

pat | True ->expwhere decls

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 individual alternatives. The alternatives are tried
sequentially, from top to bottom. If e matches the pattern in the
alternative, the guards for that alternative are tried sequentially from
top to bottom, in the environment of the case expression extended first
by the bindings created during the matching of the pattern, and then by
the declsi in the where clause associated with that alternative. If one
of the guards evaluates to True, the corresponding right-hand side is
evaluated in the same environment as the guard. If all the guards
evaluate to False, matching continues with the next alternative. If no
match succeeds, the result is _|_.

13 class

A class declaration
introduces a new type class and the overloaded operations that must be
supported by any type that is an instance of that class.

classNum a where(+):: a -> a -> a
negate:: a -> a

14 data

The data
declaration is how one introduces new algebraic data types into
Haskell. For example:

data Set a = NilSet
| ConsSet a (Set a)

Another example, to create a datatype to hold an abstract syntax tree for an expression, one could use:

15 data family

16 data instance

17 default

Ambiguities in the class Num are most common, so Haskell provides
a way to resolve them---with a default declaration:

default(Int)

Only one default declaration is permitted per module, and its effect is
limited to that module. If no default declaration is given in a module
then it assumed to be:

default(Integer,Double)

18 deriving

data and newtype declarations contain an optional deriving form. If the
form is included, then derived instance declarations are automatically
generated for the datatype in each of the named classes.

Derived instances provide convenient commonly-used operations for user-defined
datatypes. For example, derived instances for datatypes in the class Eq
define the operations == and /=, freeing the programmer from the need to
define them.

data T = A
| B
| C
deriving(Eq,Ord,Show)

19 do

Syntactic sugar for use with monadic expressions. For example:

do{ x ; result <- y ; foo result }

is shorthand for:

x >>
y >>= \result ->
foo result

20 forall

This is a GHC/Hugs extension, and as such is not portable Haskell 98.
It is only a reserved word within types.

Type variables in a Haskell type expression are all assumed to be
universally quantified; there is no explicit syntax for universal
quantification, in standard Haskell 98. For example, the type expression

a -> a

denotes the type

forall a. a ->a

.

For clarity, however, we often write quantification explicitly when
discussing the types of Haskell programs. When we write an explicitly
quantified type, the scope of the forall extends as far to the right as
possible; for example,

25 infix, infixl, infixr

A fixity declaration gives
the fixity and binding precedence of one or more operators. The integer
in a fixity declaration must be in the range 0 to 9. A fixity
declaration may appear anywhere that a type signature appears and, like
a type signature, declares a property of a particular operator.

26 instance

An instance declaration declares that a type is an instance of a class
and includes the definitions of the overloaded operations - called class
methods - instantiated on the named type.

instanceNumIntwhere
x + y = addInt x y
negate x = negateInt x

27 let, in

Let expressions have the general form:

let{ d1 ; ... ; dn }in e

They 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.

30 newtype

The newtype declaration is how one introduces a renaming for an algebraic data type into Haskell. This is different from type below, as a newtype requires a new constructor as well. As an example, when writing a compiler
one sometimes further qualifies Identifiers to assist in type safety checks: