\documentclass{book}
\renewcommand{\chaptername}{Lecture}
\usepackage{amsmath}
\usepackage{ctable}
\usepackage[mathletters]{ucs}
\usepackage[utf8x]{inputenc}
% Redefine labelwidth for lists; otherwise, the enumerate package will cause
% markers to extend beyond the left margin.
\makeatletter\AtBeginDocument{%
  \renewcommand{\@listi}
    {\setlength{\labelwidth}{4em}}
}\makeatother
\usepackage{enumerate}
\usepackage[breaklinks=true,unicode=true]{hyperref}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
%\setcounter{secnumdepth}{0}


\begin{document}

\setcounter{chapter}{6}

\chapter{Data types}

We have already seen an example of a compound data type namely list.
Recall that, a list is either an empty list or a list with a head
element and rest of the list. We begin by defining a list data type.
Haskell already provides a list data type so we do not need to define a
user defined data type. However, we do this for illustration

\begin{verbatim}

> import Prelude hiding (sum) -- hide the standard sum function
> data List a = EmptyList
>             | Cons  a (List a)
\end{verbatim}
One reads this as follows ``List of \texttt{a} is either
\texttt{EmptyList} or a \texttt{Cons} of \texttt{a} and \texttt{List} of
\texttt{a}''. Here the variable \texttt{a} is a type variable. The
result of this is that \texttt{List} is now a polymorphic data type. We
can instatiate this with any other Haskell data types. A list of
integers is then \texttt{List Integer}.

The identifiers \texttt{EmptyList} and \texttt{Cons} are the two
\emph{constructors} of our new data type \texttt{List}. The constructors
can now be used in Haskell expressions. For example \texttt{EmptyList}
is a valid Haskell expression. So is \texttt{Cons 2 EmptyList} and
\texttt{Cons 1 (Cons 2 EmptyList)}. The standard list actually has two
constructors, namely \texttt{{[}{]}} and \texttt{(:)}.

\section{Pattern Matching}

We can now define functions on our new data type \texttt{List} using
pattern matching in the most obvious way. Here is a version of
\texttt{sum} that works with \texttt{List Int} instead of
\texttt{{[}Int{]}}

\begin{verbatim}

> sum :: List Int -> Int
> sum EmptyList   = 0
> sum (Cons x xs) = x + sum xs
\end{verbatim}
As the next example, we two functions to convert from our list type to
the standard list type.

\begin{verbatim}

> toStdList   :: List a -> [a]
> fromStdList :: [a] -> List a

> toStdList EmptyList   = []
> toStdList (Cons x xs) = x : toStdList xs

> fromStdList []     = EmptyList
> fromStdList (x:xs) = Cons x (fromStdList xs)
\end{verbatim}
\begin{enumerate}[1.]
\item
  \textbf{Exercise}: Define the functions \texttt{map} \texttt{foldr}
  and \texttt{foldl} for our new list type.
\end{enumerate}
\section{Syntax of a data type}

We now give the general syntax for defining data types.

\begin{verbatim}

data Typename tv_1 tv_2 tv_n = C1 te_11 te_12 ... te_1r1
                             | C2 te_21 te_22 ... te_2r2
                             |     . . .
                             | Cm te_m1 te_m2 ... te_mrm
\end{verbatim}
Here data is a key word that tells the complier that the next equation
is a data type definition. This gives a polymorphic data type with $n$
\emph{type arguments} \texttt{tv\_1,...,tv\_n}. The \texttt{te\_ij}'s
are arbitrary type expressions and the identifiers \texttt{C1} to
\texttt{Cm} are the constructors of the type. Recall that in Haskell
there is a constraint that each variable, or for that matter type
variable, \emph{should} be an identifer which starts with a lower case
alphabet. In the case of type names and constructors, they \emph{should}
start with upper case alphabet.

\section{Constructors}

Constructors of a data type play a dual role. In expressions they behave
like functions. For example in the \texttt{List} data type that we
defined the \texttt{EmptyList} constructor is a constant List (which is
the same as 0-argument function) and \texttt{Cons} has type
\texttt{a -\textgreater{} List a -\textgreater{} List a}. On the other
hand constructors can be used in pattern matching when defining
functions.

\section{The Binary tree}

We now look at another example the binary tree. Recall that a binary
tree is either an empty tree or has root and two children. In haskell
this can be captured as follows

\begin{verbatim}

>
> data Tree a = EmptyTree
>             | Node (Tree a) a (Tree a)
>
\end{verbatim}
To illustrate function on tree let us define the \texttt{depth} function

\begin{verbatim}

> depth :: Tree a -> Int
> depth EmptyTree           = 0
> depth (Node left _ right) | dLeft <= dRight = dRight + 1
>                           | otherwise       = dLeft  + 1
>       where dLeft  = depth left
>             dRight = depth right
\end{verbatim}

\end{document}
