Personal tools

A brief introduction to Haskell

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
m (more notes)
m (typos)
Line 334: Line 334:
 
=== Lists ===
 
=== Lists ===
   
Lists are ... lists of Haskell values. Defining a new list is a
+
Lists are ... lists of Haskell values. Defining a new list is trivial,
triviality, even easier than in Java.
+
easier than in Java.
   
 
<haskell>
 
<haskell>
Line 357: Line 357:
 
Notice how the function call <hask>concat ["f","g"]</hask> does not require
 
Notice how the function call <hask>concat ["f","g"]</hask> does not require
 
parenthesis to delimit the function's arguments. Haskell
 
parenthesis to delimit the function's arguments. Haskell
uses whitespace, and not commas, because:
+
uses whitespace, and not commas, and:
   
 
* You don't need parentheses for function application in Haskell: <hask>sin 0.3</hask>
 
* You don't need parentheses for function application in Haskell: <hask>sin 0.3</hask>
Line 367: Line 367:
 
Prelude> ['x', True]
 
Prelude> ['x', True]
   
Couldn't match `Char' against `Bool'
+
Couldn't match `Char' against `Bool'
 
</haskell>
 
</haskell>
   
 
Here we tried to build a list containing a Char and a Boolean, but the
 
Here we tried to build a list containing a Char and a Boolean, but the
list ''constructor'', </hask>[]<hask>, has type:
+
list ''constructor'', <hask>[]</hask>, has type:
   
 
<haskell>
 
<haskell>
Line 411: Line 411:
 
Haskell supports pattern matching on data structures. This is a powerful
 
Haskell supports pattern matching on data structures. This is a powerful
 
language feature, making code that manipulates data structures
 
language feature, making code that manipulates data structures
incredibly simple:
+
incredibly simple. The core language feature for pattern matching is the
+
<hask>case</hask> expression:
The core language feature for pattern matching is the <hask>case</hask>
 
expression:
 
   
 
<haskell>
 
<haskell>
Line 421: Line 421:
 
The <hask>case</hask> forces <hask>x</hask> (the scrutinee) to match
 
The <hask>case</hask> forces <hask>x</hask> (the scrutinee) to match
 
pattern <hask>h:t</hask>, a list with head and tail, and then we extract
 
pattern <hask>h:t</hask>, a list with head and tail, and then we extract
the head, <hask>h</hask>.
+
the head, <hask>h</hask>. Tail is similar, and we can use a ''wildcard
+
pattern'' to ignore the part of the pattern we don't care about:
Tail is similar, and we can use a ''wildcard pattern'' to ignore the
 
part of the pattern we don't care about:
 
   
 
<haskell>
 
<haskell>

Revision as of 02:08, 27 October 2006


Haskell is:

  • A language developed by the programming languages research community.
  • Is a lazy, purely functional language (that also has imperative features such as side effects and mutable state, along with strict evaluation)
  • Born as an open source vehicle for programming language research
  • One of the youngest children of ML and Lisp
  • Particularly useful for programs that manipulate data structures (such as compilers and interpreters), and for concurrent/parallel programming

Inspired by the Introduction to OCaml.

Contents

1 Background

History:

  • 1990. Haskell 1.0
  • 1991. Haskell 1.1
  • 1993. Haskell 1.2
  • 1996. Haskell 1.3
  • 1997. Haskell 1.4
  • 1998. Haskell 98
  • 2000-2006. Period of rapid language and community growth
  • ~2007. Haskell Prime

Implementations:

2 Haskell features

Has some novel features relative to Java (and C++).

  • Immutable variables by default (mutable state programmed via monads)
  • Pure by default (side effects are programmed via monads)
  • Lazy evaluation: results are only computed if they're required (strictness optional)
  • Everything is an expression
  • First-class functions: functions can be defined anywhere, passed as arguments, and returned as values.
  • Both compiled and interpreted implementations available
  • Full type inference -- type declarations optional
  • Pattern matching on data structures -- data structures are first class!
  • Parametric polymorphism
  • Bounded parametric polymorphism

These are all conceptually more advanced ideas.

Compared to similar functional languages, Haskell differs in that it has support for:

  • Lazy evaluation
  • Pure functions by default
  • Monadic side effects
  • Type classes
  • Syntax based on layout

The GHC Haskell compiler, in particular, provides some interesting extensions:

  • Generalised algebraic data types
  • Impredicative types system
  • Software transactional memory
  • Parallel, SMP runtime system

3 The Basics

Read the language definition to supplement these notes. For more depth and examples see the Haskell wiki.

3.1 Interacting with the language

Haskell is both compiled and interpreted. For exploration purposes, we'll consider interacting with Haskell via the GHCi interpreter:

  • expressions are entered at the prompt
  • newline signals end of input

Here is a GHCi sessoin, starting from a UNIX prompt.

   $ ghci
      ___         ___ _
     / _ \ /\  /\/ __(_)
    / /_\// /_/ / /  | |      GHC Interactive, version 6.4.2, for Haskell 98.
   / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
   \____/\/ /_/\____/|_|      Type :? for help.
   Loading package base-1.0 ... linking ... done.
Here the incredibly simple Haskell program
let x = 3+4
is compiled and loaded, and available via the variable
x
.
Prelude> let x = 3 + 4

We can ask the system what type it automaticaly inferred for our

variable.
x :: Integer
means that the variable
x
"has type"
Integer
, the type of unbounded

integer values.

Prelude> :t x
x :: Integer

A variable evaluates to its value.

Prelude> x
7
The variable
x
is in scope, so we can reuse it in later

expressions.

Prelude> x + 4
11
Local variables may be bound using
let
, which declares a

new binding for a variable with local scope.

Prelude> let x = 4 in x + 3
7

Alternatively, declarations typed in at the top level are like an open-ended let:

Prelude> let x = 4
Prelude> let y = x + 3
Prelude> x * x
16
 
Prelude> :t x
x :: Integer
Prelude> :t y
y :: Integer
Prelude> :t x * x
x * x :: Integer

Notice that type inference infers the correct type for all the expressions, without us having to ever specify the type explicitly.

4 Basic types

There is a range of basic types, defined in the language Prelude.

Int         -- bounded, word-sized integers
Integer     -- unbounded integers
Double      -- floating point values
Char        -- characters
String      -- strings
()          -- the unit type
Bool        -- booleans
[a]         -- lists
(a,b)       -- tuples / product types
Either a b  -- sum types
Maybe a     -- optional values

For example:

7
12312412412412321
3.1415
'x'
"haskell"
()
True, False
[1,2,3,4,5]
('x', 42)
Left True, Right "string"
Nothing, Just True

These types have all the usual operations on them, in the standard libraries.

4.1 Libraries

  • The Prelude contains the core operations on basic types. It is imported by default into every Haskell module. For example;
+ - div mod && || not < > == /=

Learn the Prelude well. Less basic functions are found in the standard libraries. For data structures such as List, Array and finite maps.

To use functions from these modules you have to import them, or in GHCi, refer to the qualified name, for example to use the toUpper function on Chars:

Prelude> Char.toUpper 'x'
'X'
 
Prelude> :m + Char
Prelude Char> toUpper 'y'
'Y'

In a source file, you have to import the module explicitly:

import Char

4.2 Overloading

Haskell uses typeclasses to methodically allow overloading. A typeclass describes a set of functions, and any type which provides those functions can be made an instance of that type. This avoids the syntactic redundancy of languages like OCaml.

For example, the function
*
is a method of the typeclass
Num
, as we can see from its type:
Prelude> :t (*)
(*) :: (Num a) => a -> a -> a

Which can be read as "* is a polymorphic function, taking two types 'a', and returning a result of the same type, where those types are members of the class Num".

This means that it will operate on any type in the
Num
class, of which the following types are members:
Double, Float,
Int, Integer
. Thus:
Prelude> 2.3 * 5.7
13.11

or on integers:

Prelude> 2 * 5
10
The type of the arguments determines which instance of
*
is

used. Haskell also never performs implicit coercions, all coercions must be explicit. For example, if we try to multiply two different types,

then the type check against
* :: Num a => a -> a -> a
will

fail.

Prelude> (2.3 :: Double) * (5 :: Int)
 
<interactive>:1:19:
    Couldn't match `Double' against `Int'
      Expected type: Double
      Inferred type: Int
    In the expression: 5 :: Int
    In the second argument of `(*)', namely `(5 :: Int)'
To convert 5 to a
Double
we'd write:
Prelude> (2.3 :: Double) * fromIntegral (5 :: Int)
11.5

Why bother -- why not allow the system to implicitly coerce types? Implicit type conversions by the system are the source of innumerable hard to find bugs in languages that support them, and makes reasoning about a program harder, since you must apply not just the language's semantics, but an extra set of coercion rules.

Note that If we leave off the type signatures however, Haskell will helpfully infer the most general type:

Prelude> 2.3 * 5
11.5

4.3 Expressions

In Haskell expressions are everything. There are no pure "statements" like in Java/C++. instead, statements are also expressions, they return values.

Prelude> (if 2 == 3 then 5 else 6) + 1
7
Prelude> (if 2 == 3 then 5 else 6.5) + 1
7.5

4.4 Local bindings

In Haskell
let
allows local declarations to be defined.
let x = 1 + 2 in x + 3

is analogous to:

   {
   int x = 1 + 2;
   ... x + 3 ... ;
   }

in C, but the Haskell variable x is given a value that is immutable (can never change).

4.5 Allocation

When you declare a new variable, Haskell automatically allocates that value for you -- no need to explicitly manage memory. The garbage collector will then collect any ureachable values once they go out of scope.

Advanced users can also manage memory by hand using the foreign function interface.

4.6 Lists

Lists are ... lists of Haskell values. Defining a new list is trivial, easier than in Java.

Prelude> [2, 1+2, 4]
[2,3,4]
 
Prelude> :t [2, 1+2, 4]
[2, 1+2, 4] :: (Num a) => [a]

This automatically allocates space for the list and puts in the elements. Haskell is garbage-collected like Java so no explicit de-allocation is needed. The type of the list is inferred automatically. All elements of a list must be of the same type.

Prelude> ["e", concat ["f", "g"], "h"]
["e","fg","h"]
Notice how the function call
concat ["f","g"]
does not require

parenthesis to delimit the function's arguments. Haskell uses whitespace, and not commas, and:

  • You don't need parentheses for function application in Haskell:
    sin 0.3
  • Multiple arguments can be passed in one at a time (curried) which means they can be separated by spaces:
    max 3 4
    .

Lists must be uniform in their type ("homogenous").

Prelude> ['x', True]
 
Couldn't match `Char' against `Bool'

Here we tried to build a list containing a Char and a Boolean, but the

list constructor,
[]
, has type:
Prelude> :t []
[] :: [a]
meaning that all elements must be of the same type,
a
.

(For those wondering how to build a list of heterogenous values, you would use a sum type):

Prelude> [Left 'x', Right True]
[Left 'x',Right True]
 
Prelude> :t [Left 'x', Right True]
[Left 'x', Right True] :: [Either Char Bool]

List operations are numerous, as can be seen in the Data.List library.

Prelude> let x = [2,3]
Prelude> let y = 1 : x  -- 'cons' the value 1 onto the list
Prelude> x              -- the list is immutable
[2,3]
Prelude> y
[1,2,3]
Prelude> x ++ y         -- joining lists
[2,3,1,2,3]
Prelude> head y         -- first element of the list is the 'head'
1
Prelude> tail y         -- the rest of the list is the 'tail'
[2,3]

4.7 Pattern matching

Haskell supports pattern matching on data structures. This is a powerful language feature, making code that manipulates data structures incredibly simple. The core language feature for pattern matching is the

case
expression:
Prelude> case x of h:t -> h
2
The
case
forces
x
(the scrutinee) to match pattern
h:t
, a list with head and tail, and then we extract the head,
h
. Tail is similar, and we can use a wildcard

pattern to ignore the part of the pattern we don't care about:

Prelude> case x of _:t -> t
[3]

4.8 Tuples

Tupes are fixed length structures, whose fields may be of differing types ("heterogenous"). They are known as product types in programming language theory.

Prelude> let x = (2, "hi")
Prelude> case x of (y,_) -> y
2

Unlike the ML family of languages, Haskell uses the same syntax for the value level as on the type level. So the type of the above tuple is:

Prelude> :t x
x :: (Integer, [Char])

All the data mentioned so far is immutable - it is impossible to change an entry in an existing list, tuple, or record without implicitly copying the data! Also, all variables are immutable. By default Haskell is a pure language. Side effects, such as mutation, are discussed later.

5 Functions (including Patterns and Higher-Order Functions)

6 Declaring our own types

7 Imperative features: monadic IO, references, mutable arrays, exceptions

8 Type classes