P. 1


|Views: 39|Likes:
Published by Riccardo Ricci
Haskell 98 Tutorial
Haskell 98 Tutorial

More info:

Published by: Riccardo Ricci on Oct 02, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






The Haskell type system contains a restriction related to type classes that is not found in ordinary
Hindley-Milner type systems: the monomorphism restriction. The reason for this restriction is
related to a subtle type ambiguity and is explained in full detail in the Report (§4.5.5). A simpler
explanation follows:

The monomorphism restriction says that any identifier bound by a pattern binding (which in-
cludes bindings to a single identifier), and having no explicit type signature, must be monomorphic.
An identifier is monomorphic if is either not overloaded, or is overloaded but is used in at most one
specific overloading and is not exported.
Violations of this restriction result in a static type error. The simplest way to avoid the problem
is to provide an explicit type signature. Note that any type signature will do (as long it is type

A common violation of the restriction happens with functions defined in a higher-order manner,
as in this definition of sumfrom the Standard Prelude:

foldl (+) 0


As is, this would cause a static type error. We can fix the problem by adding the type signature:


:: (Num a) =) [a] -) a

Also note that this problem would not have arisen if we had written:

sum xs

foldl (+) 0 xs
because the restriction only applies to pattern bindings.

13 Arrays

Ideally, arrays in a functional language would be regarded simply as functions from indices to values,
but pragmatically, in order to assure efficient access to array elements, we need to be sure we can
take advantage of the special properties of the domains of these functions, which are isomorphic
to finite contiguous subsets of the integers. Haskell, therefore, does not treat arrays as general
functions with an application operation, but as abstract data types with a subscript operation.
Two main approaches to functional arrays may be discerned: incremental and monolithic def-
inition. In the incremental case, we have a function that produces an empty array of a given size
and another that takes an array, an index, and a value, producing a new array that differs from
the old one only at the given index. Obviously, a naive implementation of such an array semantics
would be intolerably inefficient, either requiring a new copy of an array for each incremental redefi-
nition, or taking linear time for array lookup; thus, serious attempts at using this approach employ
sophisticated static analysis and clever run-time devices to avoid excessive copying. The monolithic
approach, on the other hand, constructs an array all at once, without reference to intermediate
array values. Although Haskell has an incremental array update operator, the main thrust of the
array facility is monolithic.
Arrays are not part of the Standard Prelude-the standard library contains the array operators.
Any module using arrays must import the Array module.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->