2015-01-19 05:04:01 +03:00
< div class = "pagetitle" >
2015-01-06 18:09:41 +03:00
![](img/titles/basics.png)
2015-01-19 05:04:01 +03:00
< / div >
2015-01-06 18:09:41 +03:00
<!--
< blockquote >
An elegant weapon for a more civilized age.
< cite > Obi-Wan Kenobi< / cite >
< / blockquote >
-->
< p class = "halfbreak" >
< / p >
Haskell Basics
==============
Let us now survey a few of the core concepts that will be used throughout the
text. This will be a very fast and informal discussion. If you are familiar
with all of these concepts then it is very likely you will be able to read the
entirety of this tutorial and focus on the subject domain and not the supporting
code. The domain material itself should largely be accessible to an ambitious
high school student or undergraduate; and requires nothing more than a general
knowledge of functional programming.
Functions
---------
2015-01-06 22:17:28 +03:00
Functions are the primary building block of all of Haskell logic.
2015-01-06 18:09:41 +03:00
```haskell
add :: Integer -> Integer -> Integer
add x y = x + y
```
2015-01-27 15:57:22 +03:00
In Haskell all functions are pure. The only thing a function may do is return a
2015-01-19 05:04:01 +03:00
value.
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
All functions in Haskell are curried. For example, when a function of three
arguments receives less than three arguments, it yields a partially applied
2015-12-12 20:46:28 +03:00
function, which, when given additional arguments, yields yet another function or
the resulting value if all the arguments were supplied.
2015-01-06 18:09:41 +03:00
```haskell
g :: Int -> Int -> Int -> Int
g x y z = x + y + z
h :: Int -> Int
2015-01-08 05:33:59 +03:00
h = g 2 3
2015-01-06 18:09:41 +03:00
```
2015-12-12 20:46:28 +03:00
Haskell supports higher-order functions, i.e., functions which take functions as
arguments and yield other functions. For example the ``compose`` function takes
two functions as arguments f and g and returns the composite function of
applying f then g.
2015-01-06 18:09:41 +03:00
```haskell
compose f g = \x -> f (g x)
```
```haskell
iterate :: (a -> a) -> a -> [a]
iterate f x = x : (iterate f (f x))
```
Datatypes
---------
2015-01-18 00:21:47 +03:00
Constructors for datatypes come in two flavors: *sum types* and *product types* .
2015-01-06 18:09:41 +03:00
A sum type consists of multiple options of *type constructors* under the same
type. The two cases can be used at all locations the type is specified, and are
discriminated using pattern matching.
```haskell
data Sum = A Int | B Bool
```
2015-12-12 20:46:28 +03:00
A product type combines multiple fields into the same type.
2015-01-06 18:09:41 +03:00
```haskell
data Prod = Prod Int Bool
```
2015-01-18 00:21:47 +03:00
Records are a special product type that, in addition to generating code for the
constructors, generates a special set of functions known as *selectors* which
2015-01-06 18:09:41 +03:00
extract the values of a specific field from the record.
```haskell
2015-01-19 05:04:01 +03:00
data Prod = Prod { a :: Int , b :: Bool }
2015-01-06 18:09:41 +03:00
-- a :: Prod -> Int
-- b :: Prod -> Bool
```
2015-01-18 00:21:47 +03:00
Sums and products can be combined.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-08 05:33:59 +03:00
data T1
2015-01-06 18:09:41 +03:00
= A Int Int
| B Bool Bool
```
2015-01-18 00:21:47 +03:00
The fields of a datatype may be *parameterized* , in which case the type depends
2015-01-06 18:09:41 +03:00
on the specific types the fields are instantiated with.
```haskell
data Maybe a = Nothing | Just a
```
Values
------
2015-01-18 00:21:47 +03:00
A list is a homogeneous, inductively defined sum type of linked cells parameterized over the type of its
2015-01-06 18:09:41 +03:00
values.
```haskell
data List a = Nil | Cons a (List a)
```
```haskell
a = [1,2,3]
a = Cons 1 (Cons 2 (Cons 3 Nil))
```
2015-01-06 22:17:28 +03:00
List have special value-level syntax:
2015-01-06 18:09:41 +03:00
```haskell
(:) = Cons
[] = Nil
```
```haskell
(1 : (2 : (3 : []))) = [1,2,3]
```
2015-01-06 22:17:28 +03:00
A tuple is a heterogeneous product type parameterized over the types of its two values.
2015-01-06 18:09:41 +03:00
Tuples also have special value-level syntax.
```haskell
data Pair a b = Pair a b
```
```haskell
a = (1,2)
a = Pair 1 2
```
```haskell
(,) = Pair
```
Pattern matching
----------------
2015-01-19 05:04:01 +03:00
Pattern matching allows us to discriminate on the constructors of a datatype,
2015-12-12 20:46:28 +03:00
mapping separate cases to separate code paths and binding variables for each of
the fields of the datatype.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-16 01:48:46 +03:00
data Maybe a = Nothing | Just a
2015-01-06 18:09:41 +03:00
maybe :: b -> (a -> b) -> Maybe a -> b
2015-01-06 22:17:28 +03:00
maybe n f Nothing = n
2015-01-06 18:09:41 +03:00
maybe n f (Just a) = f a
```
2015-01-18 00:21:47 +03:00
Top-level pattern matches can always be written identically as case statements.
2015-01-06 18:09:41 +03:00
```haskell
maybe :: b -> (a -> b) -> Maybe a -> b
maybe n f x = case x of
Nothing -> n
Just a -> f a
```
Wildcards can be placed for patterns where the resulting value is not used.
```haskell
const :: a -> b -> a
const x _ = x
```
2015-12-12 20:46:28 +03:00
Subexpression in the pattern can be explicitly bound to variables scoped on the
right hand side of the pattern match.
```haskell
f :: Maybe (Maybe a) -> Maybe a
f (Just x @ (Just _)) = x
```
2015-01-06 18:09:41 +03:00
List and tuples have special pattern syntax.
```haskell
length :: [a] -> Int
2015-01-06 22:17:28 +03:00
length [] = 0
2015-01-06 18:09:41 +03:00
length (x:xs) = 1 + (length xs)
```
```haskell
fst :: (a, b) -> a
fst (a,b) = a
```
2015-01-18 00:21:47 +03:00
Patterns may be guarded by predicates (functions which yield a boolean). Guards
only allow the execution of a branch if the corresponding predicate yields True.
2015-01-06 18:09:41 +03:00
```haskell
filter :: (a -> Bool) -> [a] -> [a]
filter pred [] = []
filter pred (x:xs)
| pred x = x : filter pred xs
| otherwise = filter pred xs
```
Recursion
---------
In Haskell all iteration over data structures is performed by recursion.
Entering a function in Haskell does not create a new stack frame, the logic of
the function is simply entered with the arguments on the stack and yields result
2015-01-27 15:57:22 +03:00
to the register. In the case where a function returns an invocation of itself
2015-01-19 05:04:01 +03:00
invoked in the *tail position* the resulting logic is compiled identically to
``while`` loops in other languages, via a ``jmp`` instruction instead of a
``call``.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-19 05:04:01 +03:00
sum :: [Int] -> [Int]
sum ys = go ys 0
where
go (x:xs) i = go xs (i+x)
go [] i = i
2015-01-06 18:09:41 +03:00
```
Functions can be defined to recurse mutually on each other.
```haskell
2015-01-06 22:17:28 +03:00
even 0 = True
2015-01-06 19:06:11 +03:00
even n = odd (n-1)
2015-01-06 18:09:41 +03:00
2015-01-06 22:17:28 +03:00
odd 0 = False
2015-01-06 19:06:11 +03:00
odd n = even (n-1)
2015-01-06 18:09:41 +03:00
```
Laziness
--------
2015-01-18 00:21:47 +03:00
A Haskell program can be thought of as being equivalent to a large directed
2015-12-12 20:46:28 +03:00
graph. Each edge represents the use of a value, and each node is the source of a
value. A node can be:
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
* A *thunk* , i.e., the application of a function to values that have not been
evaluated yet
2015-01-06 18:09:41 +03:00
* A thunk that is currently being evaluated, which may induce the evaluation of
2015-01-18 00:21:47 +03:00
other thunks in the process
* An expression in *weak head normal form* , which is only evaluated to the
outermost constructor or lambda abstraction
2015-01-06 18:09:41 +03:00
The runtime has the task of determining which thunks are to be evaluated by the
2015-01-18 00:21:47 +03:00
order in which they are connected to the main function node. This is the essence
2015-01-08 05:33:59 +03:00
of all evaluation in Haskell and is called *graph reduction* .
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
Self-referential functions are allowed in Haskell. For example, the following
functions generate infinite lists of values. However, they are only evaluated
up to the depth that is necessary.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-09 17:04:56 +03:00
-- Infinite stream of 1's
2015-01-06 18:09:41 +03:00
ones = 1 : ones
2015-01-09 17:04:56 +03:00
-- Infinite count from n
2015-01-06 18:09:41 +03:00
numsFrom n = n : numsFrom (n+1)
2015-01-09 17:04:56 +03:00
-- Infinite stream of integer squares
squares = map (^2) (numsfrom 0)
2015-01-06 18:09:41 +03:00
```
2015-01-18 00:21:47 +03:00
The function ``take`` consumes an infinite stream and only evaluates the values
that are needed for the computation.
2015-01-09 17:04:56 +03:00
2015-01-06 18:09:41 +03:00
```haskell
2015-01-09 17:04:56 +03:00
take :: Int -> [a] -> [a]
take n _ | n < = 0 = []
take n [] = []
take n (x:xs) = x : take (n-1) xs
2015-01-06 18:09:41 +03:00
```
```haskell
take 5 squares
2015-01-09 17:04:56 +03:00
-- [0,1,4,9,16]
2015-01-06 18:09:41 +03:00
```
2015-01-18 00:21:47 +03:00
This also admits diverging terms (called *bottoms* ), which have no normal form.
Under lazy evaluation, these values can be threaded around and will never diverge
2015-01-09 17:04:56 +03:00
unless actually forced.
2015-01-06 18:09:41 +03:00
```haskell
bot = bot
```
2015-01-18 00:21:47 +03:00
So, for instance, the following expression does not diverge since the second
2015-01-06 18:09:41 +03:00
argument is not used in the body of ``const``.
```haskell
const 42 bot
```
2015-01-09 17:04:56 +03:00
The two bottom terms we will use frequently are used to write the scaffolding
2015-01-18 00:21:47 +03:00
for incomplete programs.
2015-01-09 17:04:56 +03:00
```haskell
error :: String -> a
undefined :: a
```
2015-01-18 00:21:47 +03:00
Higher-Kinded Types
2015-01-06 18:09:41 +03:00
-------------------
2015-01-18 00:21:47 +03:00
The "type of types" in Haskell is the language of kinds. Kinds are either an
2015-01-27 18:16:55 +03:00
arrow (``k -> k'``) or a star (``*``).
2015-01-06 18:09:41 +03:00
2015-01-27 15:57:22 +03:00
The kind of Int is ``*``, while the kind of ``Maybe`` is ``* -> *``. Haskell
2015-01-18 00:21:47 +03:00
supports higher-kinded types, which are types that take other types and
construct a new type. A type constructor in Haskell always has a kind which
terminates in a ``*``.
2015-01-06 18:09:41 +03:00
```haskell
-- T1 :: (* -> *) -> * -> *
data T1 f a = T1 (f a)
```
The three special types ``(,)``, ``(->)``, ``[]`` have special type-level
syntactic sugar:
```haskell
(,) Int Int = (Int, Int)
(->) Int Int = Int -> Int
[] Int = [Int]
```
Typeclasses
-----------
2015-01-18 00:21:47 +03:00
A typeclass is a collection of functions which conform to a given interface. An implementation of an
2015-01-06 18:09:41 +03:00
interface is called an instance. Typeclasses are effectively syntactic sugar for records of functions and
2015-01-18 00:21:47 +03:00
nested records (called *dictionaries* ) of functions parameterized over the instance type. These
2015-01-06 18:09:41 +03:00
dictionaries are implicitly threaded throughout the program whenever an overloaded identifier is used. When a
2015-01-18 00:21:47 +03:00
typeclass is used over a concrete type, the implementation is simply spliced in at the call site. When a
typeclass is used over a polymorphic type, an implicit dictionary parameter is added to the function so that
2015-01-06 18:09:41 +03:00
the implementation of the necessary functionality is passed with the polymorphic value.
2015-01-18 00:21:47 +03:00
Typeclasses are "open" and additional instances can always be added, but the defining feature of a typeclass is
that the instance search always converges to a single type to make the process of resolving overloaded identifiers globally unambiguous.
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
For instance, the Functor typeclass allows us to "map" a function generically
over any type of kind (``* -> *``) and apply it to its internal structure.
2015-01-06 18:09:41 +03:00
```haskell
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor [] where
2015-01-06 22:17:28 +03:00
fmap f [] = []
fmap f (x:xs) = f x : fmap f xs
2015-01-06 18:09:41 +03:00
instance Functor ((,) a) where
fmap f (a,b) = (a, f b)
```
Operators
---------
2015-01-18 00:21:47 +03:00
In Haskell, infix operators are simply functions, and quite often they are used in
2015-01-06 18:09:41 +03:00
place of alphanumerical names when the functions involved combine in common ways
and are subject to algebraic laws.
```haskell
infixl 6 +
infixl 6 -
infixl 7 /
infixl 7 *
infixr 5 ++
infixr 9 .
```
Operators can be written in section form:
```haskell
(x+) = \y -> x+y
(+y) = \x -> x+y
(+) = \x y -> x+y
```
2015-01-06 22:17:28 +03:00
Any binary function can be written in infix form by surrounding the name in
2015-01-06 18:09:41 +03:00
backticks.
```haskell
(+1) `fmap` [1,2,3] -- [2,3,4]
```
Monads
------
2015-01-18 00:21:47 +03:00
A monad is a typeclass with two functions: ``bind`` and ``return``.
2015-01-06 18:09:41 +03:00
```haskell
class Monad m where
2015-01-06 22:17:28 +03:00
bind :: m a -> (a -> m b) -> m b
2015-01-06 18:09:41 +03:00
return :: a -> m a
```
The bind function is usually written as an infix operator.
```haskell
infixl 1 >>=
class Monad m where
2015-01-06 22:17:28 +03:00
(>>=) :: m a -> (a -> m b) -> m b
2015-01-06 18:09:41 +03:00
return :: a -> m a
```
This defines the structure, but the monad itself also requires three laws that
all monad instances must satisfy.
**Law 1**
```haskell
2015-01-19 05:04:01 +03:00
return a >>= f = f a
2015-01-06 18:09:41 +03:00
```
**Law 2**
```haskell
2015-01-19 05:04:01 +03:00
m >>= return = m
2015-01-06 18:09:41 +03:00
```
**Law 3**
```haskell
2015-01-19 05:04:01 +03:00
(m >>= f) >>= g = m >>= (\x -> f x >>= g)
2015-01-06 18:09:41 +03:00
```
Haskell has a level of syntactic sugar for monads known as do-notation. In this
2015-01-18 00:21:47 +03:00
form, binds are written sequentially in block form which extract the variable
2015-01-06 22:17:28 +03:00
from the binder.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-19 05:04:01 +03:00
do { a < - f ; m } = f > >= \a -> do { m }
do { f ; m } = f >> do { m }
do { m } = m
2015-01-06 18:09:41 +03:00
```
2015-01-18 00:21:47 +03:00
So, for example, the following are equivalent:
2015-01-06 18:09:41 +03:00
```haskell
do
a < - f
b < - g
c < - h
return (a, b, c)
```
```haskell
f >>= \a ->
g >>= \b ->
h >>= \c ->
return (a, b, c)
```
Applicatives
-------------
2015-01-18 00:21:47 +03:00
Applicatives allow sequencing parts of some contextual computation, but do not
bind variables therein. Strictly speaking, applicatives are less expressive
than monads.
2015-01-06 18:09:41 +03:00
```haskell
class Functor f => Applicative f where
2015-01-06 22:17:28 +03:00
pure :: a -> f a
2015-01-06 18:09:41 +03:00
(< *>) :: f (a -> b) -> f a -> f b
(< $>) :: Functor f => (a -> b) -> f a -> f b
(< $>) = fmap
```
2015-01-18 00:21:47 +03:00
Applicatives satisfy the following laws:
2015-01-06 18:09:41 +03:00
```haskell
2015-01-18 00:21:47 +03:00
pure id < *> v = v -- Identity
pure f < *> pure x = pure (f x) -- Homomorphism
u < *> pure y = pure ($ y) < *> u -- Interchange
u < *> (v < *> w) = pure (.) < *> u < *> v < *> w -- Composition
2015-01-06 18:09:41 +03:00
```
For example:
```haskell
example1 :: Maybe Integer
example1 = (+) < $> m1 < *> m2
where
m1 = Just 3
m2 = Nothing
```
2015-01-18 00:21:47 +03:00
Instances of the ``Applicative`` typeclass also have available the functions
``*>`` and ``< *``. These functions sequence applicative actions while
discarding the value of one of the arguments. The operator ``*>`` discards the
left argument, while ``< *`` discards the right. For example, in a monadic
parser combinator library, the ``*>`` would discard the value of the first
argument but return the value of the second.
2015-01-06 18:09:41 +03:00
2015-01-19 05:04:01 +03:00
Monoids
-------
2015-02-01 17:13:47 +03:00
Monoids provide an interface for structures which have an associative operation
2015-02-14 00:28:27 +03:00
(``mappend``, there is also the synonym ``< >``) and a neutral
(also: unit or zero) element (``mempty``) for that operation.
2015-02-01 17:13:47 +03:00
2015-01-19 05:04:01 +03:00
```haskell
class Monoid a where
mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> a
```
2015-02-14 00:28:27 +03:00
The canonical example is the list type with concatenation as the operation
and the empty list as zero.
2015-02-01 17:13:47 +03:00
```haskell
import Data.Monoid
a :: [Integer]
a = [1,2,3] < > [4,5,6]
b :: [Integer]
b = ([1,2,3] < > mempty) < > (mempty < > [4,5,6])
```
2015-01-06 18:09:41 +03:00
Deriving
--------
2015-12-12 20:46:28 +03:00
Instances for typeclasses like ``Read``, ``Show``, ``Eq`` and ``Ord`` can be
derived automatically by the Haskell compiler.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-08 05:33:59 +03:00
data PlatonicSolid
2015-01-06 18:09:41 +03:00
= Tetrahedron
| Cube
| Octahedron
| Dodecahedron
| Icosahedron
deriving (Show, Eq, Ord, Read)
```
```haskell
example = show Icosahedron
example = read "Tetrahedron"
example = Cube == Octahedron
example = sort [Cube, Dodecahedron]
```
IO
--
2015-01-18 00:21:47 +03:00
A value of type ``IO a`` is a computation which, when performed, does some I/O
2015-01-06 18:09:41 +03:00
before returning a value of type ``a``. The notable feature of Haskell is that
2015-01-18 00:21:47 +03:00
IO is still functionally pure; a value of type ``IO a`` is simply a value which
stands for a computation which, when invoked, will perform IO. There is no way
to peek into its contents without running it.
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
For instance, the following function does not print the numbers 1 to 5 to the
screen. Instead, it builds a list of IO computations:
2015-01-06 18:09:41 +03:00
```haskell
2015-01-06 22:17:28 +03:00
fmap print [1..5] :: [IO ()]
2015-01-06 18:09:41 +03:00
```
2015-01-18 00:21:47 +03:00
We can then manipulate them as an ordinary list of values:
2015-01-06 18:09:41 +03:00
```haskell
2015-01-06 22:17:28 +03:00
reverse (fmap print [1..5]) :: [IO ()]
2015-01-06 18:09:41 +03:00
```
2015-01-18 00:21:47 +03:00
We can then build a composite computation of each of the IO actions in the list
using ``sequence_``, which will evaluate the actions from left to right. The
resulting ``IO`` computation can be evaluated in ``main`` (or the GHCi repl,
which effectively is embedded inside of ``IO``).
2015-01-06 18:09:41 +03:00
```haskell
>> sequence_ (fmap print [1..5]) :: IO ()
1
2
3
4
5
>> sequence_ (reverse (fmap print [1..5])) :: IO ()
5
4
3
2
1
```
2015-02-01 17:15:14 +03:00
The IO monad is wired into the runtime with compiler support. It is a special
case and most monads in Haskell have nothing to do with effects in this sense.
2015-01-06 18:09:41 +03:00
```haskell
putStrLn :: String -> IO ()
2015-01-06 22:17:28 +03:00
print :: Show a => a -> IO ()
2015-01-06 18:09:41 +03:00
```
2015-01-06 22:17:28 +03:00
The type of ``main`` is always ``IO ()``.
2015-01-06 18:09:41 +03:00
```haskell
main :: IO ()
main = do
putStrLn "Enter a number greater than 3: "
2015-01-08 05:33:59 +03:00
x < - readLn
2015-01-06 18:09:41 +03:00
print (x > 3)
```
The essence of monadic IO in Haskell is that *effects are reified as first class
values in the language and reflected in the type system*. This is one of
2015-01-18 00:21:47 +03:00
foundational ideas of Haskell, although it is not unique to Haskell.
2015-01-06 18:09:41 +03:00
Monad Transformers
------------------
Monads can be combined together to form composite monads. Each of the composite
2015-01-18 00:21:47 +03:00
monads consists of *layers* of different monad functionality. For example, we
2015-12-12 20:46:28 +03:00
can combine an error-reporting monad with a state monad to encapsulate a certain
set of computations that need both functionalities. The use of monad
2015-01-18 00:21:47 +03:00
transformers, while not always necessary, is often one of the primary ways to
2015-01-06 22:17:28 +03:00
structure modern Haskell programs.
2015-01-06 18:09:41 +03:00
```haskell
class MonadTrans t where
lift :: Monad m => m a -> t m a
```
The implementation of monad transformers is comprised of two different
2015-01-09 08:20:09 +03:00
complementary libraries, ``transformers`` and ``mtl``. The ``transformers``
library provides the monad transformer layers and ``mtl`` extends this
2015-01-18 00:21:47 +03:00
functionality to allow implicit lifting between several layers.
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
To use transformers, we simply import the *Trans* variants of each of the
layers we want to compose and then wrap them in a newtype.
2015-01-06 18:09:41 +03:00
```haskell
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Monad.Trans
import Control.Monad.Trans.State
import Control.Monad.Trans.Writer
newtype Stack a = Stack { unStack :: StateT Int (WriterT [Int] IO) a }
deriving (Monad)
foo :: Stack ()
foo = Stack $ do
put 1 -- State layer
lift $ tell [2] -- Writer layer
lift $ lift $ print 3 -- IO Layer
return ()
evalStack :: Stack a -> IO [Int]
evalStack m = execWriterT (evalStateT (unStack m) 0)
```
2015-01-09 08:20:09 +03:00
As illustrated by the following stack diagram:
![](img/stack.png)
2015-01-18 00:21:47 +03:00
Using ``mtl`` and ``GeneralizedNewtypeDeriving``, we can produce the same stack
but with a simpler forward-facing interface to the transformer stack. Under the
hood, ``mtl`` is using an extension called ``FunctionalDependencies`` to
2015-12-12 20:46:28 +03:00
automatically infer which layer of a transformer stack a function belongs to and
can then lift into it.
2015-01-06 18:09:41 +03:00
```haskell
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Monad.Trans
import Control.Monad.State
import Control.Monad.Writer
newtype Stack a = Stack { unStack :: StateT Int (WriterT [Int] IO) a }
deriving (Monad, MonadState Int, MonadWriter [Int], MonadIO)
foo :: Stack ()
foo = do
put 1 -- State layer
tell [2] -- Writer layer
liftIO $ print 3 -- IO Layer
return ()
evalStack :: Stack a -> IO [Int]
evalStack m = execWriterT (evalStateT (unStack m) 0)
```
**StateT**
The state monad allows functions within a stateful monadic context to access and
modify shared state.
```haskell
put :: s -> State s () -- set the state value
get :: State s s -- get the state
gets :: (s -> a) -> State s a -- apply a function over the state, and return the result
modify :: (s -> s) -> State s () -- set the state, using a modifier function
```
2015-01-18 00:21:47 +03:00
Evaluation functions often follow the naming convention of using the prefixes
``run``, ``eval``, and ``exec``:
2015-01-06 18:09:41 +03:00
```haskell
execState :: State s a -> s -> s -- yield the state
evalState :: State s a -> s -> a -- yield the return value
runState :: State s a -> s -> (a, s) -- yield the state and return value
```
For example:
```haskell
import Control.Monad.State
test :: State Int Int
test = do
put 3
modify (+1)
get
main :: IO ()
main = print $ execState test 0
```
**ReaderT**
2015-01-06 22:17:28 +03:00
The Reader monad allows a fixed value to be passed around inside the monadic
2015-01-08 05:33:59 +03:00
context.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-06 22:17:28 +03:00
ask :: Reader r r -- get the value
2015-01-27 18:16:55 +03:00
asks :: (r -> a) -> Reader r a -- apply a function to the value, and return the result
2015-01-06 18:09:41 +03:00
local :: (r -> r) -> Reader r a -> Reader r a -- run a monadic action, with the value modified by a function
```
For example:
```haskell
import Control.Monad.Reader
data MyContext = MyContext
{ foo :: String
, bar :: Int
} deriving (Show)
computation :: Reader MyContext (Maybe String)
computation = do
n < - asks bar
x < - asks foo
if n > 0
then return (Just x)
else return Nothing
ex1 :: Maybe String
ex1 = runReader computation $ MyContext "hello" 1
ex2 :: Maybe String
ex2 = runReader computation $ MyContext "haskell" 0
```
**WriterT**
The writer monad lets us emit a lazy stream of values from within a monadic
context. The primary function ``tell`` adds a value to the writer context.
```haskell
2015-01-06 22:17:28 +03:00
tell :: (Monoid w) => w -> Writer w ()
2015-01-06 18:09:41 +03:00
```
2015-01-30 20:11:41 +03:00
The monad can be evaluated returning the collected writer context and
optionally the returned value.
2015-01-06 18:09:41 +03:00
```haskell
2015-01-06 22:17:28 +03:00
execWriter :: (Monoid w) => Writer w a -> w
runWriter :: (Monoid w) => Writer w a -> (a, w)
2015-01-06 18:09:41 +03:00
```
```haskell
import Control.Monad.Writer
type MyWriter = Writer [Int] String
example :: MyWriter
2015-01-06 22:17:28 +03:00
example = do
2015-01-06 18:09:41 +03:00
tell [1..5]
tell [5..10]
return "foo"
output :: (String, [Int])
output = runWriter example
```
**ExceptT**
The Exception monad allows logic to fail at any point during computation with a
user-defined exception. The exception type is the first parameter of the monad
2015-01-08 05:33:59 +03:00
type.
2015-01-06 18:09:41 +03:00
```haskell
throwError :: e -> Except e a
2015-01-06 22:17:28 +03:00
runExcept :: Except e a -> Either e a
2015-01-06 18:09:41 +03:00
```
2015-01-18 00:21:47 +03:00
2015-01-06 18:09:41 +03:00
For example:
```haskell
import Control.Monad.Except
type Err = String
safeDiv :: Int -> Int -> Except Err Int
safeDiv a 0 = throwError "Divide by zero"
safeDiv a b = return (a `div` b)
example :: Either Err Int
example = runExcept $ do
x < - safeDiv 2 3
y < - safeDiv 2 0
return (x + y)
```
**Kleisli Arrows**
2015-01-20 00:29:42 +03:00
The additional combinators for monads (``(>=>)``, ``(< =< )``) compose two different monadic actions in
sequence. ``(< =< )`` is the monadic equivalent of the regular function composition
operator ``(.)`` and ``(>=>)`` is just ``flip (< =< )``.
2015-01-06 18:09:41 +03:00
```haskell
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
```
The monad laws can be expressed equivalently in terms of Kleisli composition.
```haskell
2015-01-09 04:36:34 +03:00
(f >=> g) >=> h = f >=> (g >=> h)
return >=> f = f
f >=> return = f
2015-01-06 18:09:41 +03:00
```
2015-01-09 09:15:14 +03:00
Text
----
2015-01-18 00:21:47 +03:00
The usual ``String`` type is a singly-linked list of characters, which,
although simple, is not efficient in storage or locality. The letters of the
string are not stored contiguously in memory and are instead allocated across
the heap.
2015-01-09 09:15:14 +03:00
2015-01-18 00:21:47 +03:00
The ``Text`` and ``ByteString`` libraries provide alternative efficient
structures for working with contiguous blocks of text data. ``ByteString`` is
useful when working with the ASCII character set, while ``Text`` provides a
text type for use with Unicode.
2015-01-09 09:15:14 +03:00
The ``OverloadedStrings`` extension allows us to overload the string type in
2015-01-18 00:21:47 +03:00
the frontend language to use any one of the available string representations.
2015-01-09 09:15:14 +03:00
```haskell
class IsString a where
fromString :: String -> a
pack :: String -> Text
unpack :: Text -> String
```
2015-01-18 00:21:47 +03:00
So, for example:
2015-01-09 09:15:14 +03:00
```haskell
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
str :: T.Text
str = "bar"
```
2015-12-11 20:13:00 +03:00
Cabal & Stack
-------------
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
To set up an existing project with a sandbox, run:
2015-01-06 18:09:41 +03:00
```bash
$ cabal sandbox init
```
2015-01-18 00:21:47 +03:00
This will create the ``.cabal-sandbox`` directory, which is the local path GHC
will use to look for dependencies when building the project.
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
To install dependencies from Hackage, run:
2015-01-06 18:09:41 +03:00
```bash
$ cabal install --only-dependencies
```
2015-01-18 00:21:47 +03:00
Finally, configure the library for building:
2015-01-06 18:09:41 +03:00
```bash
$ cabal configure
```
Now we can launch a GHCi shell scoped with the modules from the project in
scope:
```bash
$ cabal repl
```
Resources
2015-01-09 07:37:53 +03:00
---------
2015-01-06 18:09:41 +03:00
If any of these concepts are unfamiliar, there are some external resources that
2015-12-13 21:58:00 +03:00
will try to explain them. The most thorough is the Stanford course lecture
notes.
2015-01-06 18:09:41 +03:00
2015-01-11 22:17:54 +03:00
* [Stanford CS240h ](http://www.scs.stanford.edu/14sp-cs240h/ ) by Bryan O'Sullivan, David Terei
2015-01-09 07:46:30 +03:00
* [Real World Haskell ](http://www.amazon.com/Real-World-Haskell-Bryan-OSullivan/dp/05965149800 ) by Bryan O'Sullivan, Don Stewart, and John Goerzen
2015-01-06 18:09:41 +03:00
2015-01-18 00:21:47 +03:00
There are some books as well, but your mileage may vary with these. Much of the
2015-12-11 20:13:00 +03:00
material is dated and only covers basic programming and not "programming in the
large".
2015-01-06 18:09:41 +03:00
2015-12-12 20:46:28 +03:00
* [Introduction to Functioanl Programming ](http://www.amazon.com/Introduction-Functional-Programming-International-Computing/dp/0134841891 ) by Richard Bird and Philip Wadler
2015-01-10 23:17:44 +03:00
* [Learn you a Haskell ](http://learnyouahaskell.com/ ) by Miran Lipovača
2015-01-06 18:09:41 +03:00
* [Programming in Haskell ](http://www.amazon.com/gp/product/0521692695 ) by Graham Hutton
* [Thinking Functionally ](http://www.cambridge.org/us/academic/subjects/computer-science/programming-languages-and-applied-logic/thinking-functionally-haskell ) by Richard Bird
2015-01-09 07:46:30 +03:00
\pagebreak