\documentstyle[a4,fleqn]{report}
\begin{document}
\title{{\Huge\bf GOFER}}
\author{{\Large Mark P.\ Jones}}

\newcommand{\I}[1]{\mbox{{\it #1}}}
\newcommand{\TT}[1]{\mbox{{\tt #1}}}
\newcommand{\T}[1]{\fbox{\rule[-0.5ex]{0ex}{2ex}\tt #1}}
%\newcommand{\T}[1]{\fbox{\tt #1}}
\newcommand{\sub}{[}
\newcommand{\bus}{]}

\newcommand{\BQ}{\begin{quote}}
\newcommand{\EQ}{\end{quote}}
\newcommand{\BI}{\begin{itemize}}
\newcommand{\EI}{\end{itemize}}
\newcommand{\BSI}{\begin{simpleitemize}}
\newcommand{\ESI}{\end{simpleitemize}}
\newcommand{\IT}{\item}
\newcommand{\bottom}{\perp}

\newenvironment{simpleitemize}{%
\begin{list}{$\bullet$}{
\parsep  = 0pt
\parskip = 0pt
\topsep  = 0pt
\itemsep = 0pt
}}{\end{list}}


\maketitle


{
\parskip=3pt
\parindent=0pt
\begin{verbatim}
           __________   __________   __________   __________   ________
          /  _______/  /  ____   /  /  _______/  /  _______/  /  ____  \
         /  / _____   /  /   /  /  /  /______   /  /______   /  /___/  /
        /  / /_   /  /  /   /  /  /  _______/  /  _______/  /  __   __/
       /  /___/  /  /  /___/  /  /  /         /  /______   /  /  \  \ 
      /_________/  /_________/  /__/         /_________/  /__/    \__\
\end{verbatim}
\begin{center}
Functional programming environment, Version 2.20\\
\copyright\ Copyright Mark P.\ Jones 1991.
\end{center}
\vspace{2cm}

\begin{center}
{\large\bf An Introduction to Gofer}\\
draft version only \\ 
please report any errors, suggestions for improvements, \\
extensions (or deletions!) to {\tt jones-mark@cs.yale.edu} \\[2em]
This version includes a number of small corrections \\
made since the original release.
\end{center}

\newpage

   Permission to use, copy, modify, and distribute this software and its
   documentation for any personal or educational use without fee is hereby
   granted, provided that:
\begin{itemize}
\item
   This copyright notice  is  retained  in  both  source  code  and
       supporting documentation.

\item
   Modified versions of this software are redistributed only if
       accompanied by a complete history (date, author, description) of
       modifications made; the intention here is to give appropriate
       credit to those involved, whilst simultaneously ensuring that any
       recipient can determine the origin of the software.

\item
   The same conditions are also applied to any software system
       derived either in full or in part from Gofer.
\end{itemize}

   The name `Gofer' is not a trademark, registered  or  otherwise,  and
   you are free to mention this name in published material, public  and
   private correspondence, or other documents  without  restriction  or
   obligation.

   Gofer is provided `as is' without express or implied warranty.

This \LaTeX\ version of the manual was prepared
by Jeroen Fokker ({\tt jeroen@cs.ruu.nl}).
}


\tableofcontents

\setlength{\parindent}{0pt}
\setlength{\parskip}{3pt}


%    1. INTRODUCTION. . . . . . . . . . . . . . . . . . . . . . . . . .  1
%
%    2. BACKGROUND AND ACKNOWLEDGEMENTS . . . . . . . . . . . . . . . .  2
%
%    3. STARTING GOFER. . . . . . . . . . . . . . . . . . . . . . . . .  4
%
%    4. USING GOFER - A BASIC INTRODUCTION. . . . . . . . . . . . . . .  5
%
%    5. STANDARD AND USER-DEFINED FUNCTIONS . . . . . . . . . . . . . .  6
%
%    6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS. . . . . . . . . . .  8
%
%    7. BUILT-IN TYPES. . . . . . . . . . . . . . . . . . . . . . . . . 12
%    7.1  Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 12
%    7.2  Booleans. . . . . . . . . . . . . . . . . . . . . . . . . . . 13
%    7.3  Integers. . . . . . . . . . . . . . . . . . . . . . . . . . . 13
%    7.4  Floating point numbers. . . . . . . . . . . . . . . . . . . . 14
%    7.5  Characters. . . . . . . . . . . . . . . . . . . . . . . . . . 14
%    7.6  Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
%    7.7  Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
%    7.8  Tuples and the unit type. . . . . . . . . . . . . . . . . . . 18
%
%    8. ERRORS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
%    8.1  Errors detected on input. . . . . . . . . . . . . . . . . . . 19
%    8.2  Errors during evaluation. . . . . . . . . . . . . . . . . . . 19
%
%    9. MORE ABOUT VALUE DECLARATIONS . . . . . . . . . . . . . . . . . 21
%    9.1  Simple pattern matching . . . . . . . . . . . . . . . . . . . 21
%    9.2  Guarded equations . . . . . . . . . . . . . . . . . . . . . . 23
%    9.3  Local definitions . . . . . . . . . . . . . . . . . . . . . . 24
%    9.4  Recursion with integers . . . . . . . . . . . . . . . . . . . 24
%    9.5  Recursion with lists. . . . . . . . . . . . . . . . . . . . . 26
%    9.6  Lazy evaluation . . . . . . . . . . . . . . . . . . . . . . . 27
%    9.7  Infinite data structures. . . . . . . . . . . . . . . . . . . 29
%    9.8  Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . 30
%    9.9  Higher-order functions. . . . . . . . . . . . . . . . . . . . 31
%    9.10 Variable declarations . . . . . . . . . . . . . . . . . . . . 32
%    9.11 Pattern bindings and irrefutable patterns . . . . . . . . . . 33
%    9.12 Type declarations . . . . . . . . . . . . . . . . . . . . . . 35
%
%    10. INCREASING YOUR POWER OF EXPRESSION. . . . . . . . . . . . . . 37
%    10.1 Arithmetic sequences. . . . . . . . . . . . . . . . . . . . . 37
%    10.2 List comprehensions . . . . . . . . . . . . . . . . . . . . . 38
%    10.3 Lambda expressions. . . . . . . . . . . . . . . . . . . . . . 41
%    10.4 Case expressions. . . . . . . . . . . . . . . . . . . . . . . 42
%    10.5 Operator sections . . . . . . . . . . . . . . . . . . . . . . 43
%    10.6 Explicitly typed expressions. . . . . . . . . . . . . . . . . 44
%
%    11. USER-DEFINED DATATYPES AND TYPE SYNONYMS . . . . . . . . . . . 46
%    11.1 Datatype definitions. . . . . . . . . . . . . . . . . . . . . 46
%    11.2 Type synonyms . . . . . . . . . . . . . . . . . . . . . . . . 47
%
%    12. DIALOGUES: INPUT AND OUTPUT. . . . . . . . . . . . . . . . . . 49
%    12.1 Basic description . . . . . . . . . . . . . . . . . . . . . . 49
%    12.2 Continuation style I/O. . . . . . . . . . . . . . . . . . . . 52
%    12.3 Interactive programs. . . . . . . . . . . . . . . . . . . . . 55
%
%    13. LAYOUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
%    13.1 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . 57
%    13.2 The layout rule . . . . . . . . . . . . . . . . . . . . . . . 57
%
%    14. OVERLOADING IN GOFER . . . . . . . . . . . . . . . . . . . . . 61
%    14.1 Type classes and predicates . . . . . . . . . . . . . . . . . 61
%    14.2 The type class Eq . . . . . . . . . . . . . . . . . . . . . . 62
%    14.2.1 Implicit overloading. . . . . . . . . . . . . . . . . . . . 62
%    14.2.2 Instances of class Eq . . . . . . . . . . . . . . . . . . . 63
%    14.2.3 Testing equality of represented values. . . . . . . . . . . 65
%    14.2.4 Instance declarations without members . . . . . . . . . . . 66
%    14.2.5 Equality on function types. . . . . . . . . . . . . . . . . 66
%    14.2.6 Non-overlapping instances . . . . . . . . . . . . . . . . . 67
%    14.3 Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . 68
%    14.3.1 Superclasses. . . . . . . . . . . . . . . . . . . . . . . . 71
%    14.3.2 Combining classes . . . . . . . . . . . . . . . . . . . . . 73
%    14.3.3 Simplified contexts . . . . . . . . . . . . . . . . . . . . 74
%    14.4 Other issues. . . . . . . . . . . . . . . . . . . . . . . . . 76
%    14.4.1 Unresolved overloading. . . . . . . . . . . . . . . . . . . 76
%    14.4.2 `Recursive' dictionaries. . . . . . . . . . . . . . . . . . 79
%    14.4.3 Classes with multiple parameters. . . . . . . . . . . . . . 81
%    14.4.4 Overloading and numeric values. . . . . . . . . . . . . . . 83
%    14.4.5 Constants in dictionaries . . . . . . . . . . . . . . . . . 86
%    14.4.6 The monomorphism restriction. . . . . . . . . . . . . . . . 88
%
%    APPENDIX A: SUMMARY OF GRAMMAR . . . . . . . . . . . . . . . . . . 93
%
%    APPENDIX B: CONTENTS OF STANDARD PRELUDE . . . . . . . . . . . . . 97
%
%    APPENDIX C: RELATIONSHIP WITH HASKELL 1.1. . . . . . . . . . . . .111
%
%    APPENDIX D: USING GOFER WITH BIRD+WADLER . . . . . . . . . . . . .115
%
%    APPENDIX E: PRIMITIVES . . . . . . . . . . . . . . . . . . . . . .117
%
%    APPENDIX F: INTERPRETER COMMAND SUMMARY. . . . . . . . . . . . . .119
%
%    APPENDIX G: BIBLIOGRAPHY . . . . . . . . . . . . . . . . . . . . .121
%
%

\chapter{Introduction}

Gofer is a functional  programming  environment  (in  other  words,  an
interpreter) that I have implemented for my own personal use as part of
my research  into  `qualified  types'.   Nevertheless,  the  system  is
sufficiently complete for me to believe that Gofer may be  of  interest
and use to others interested in the field of functional programming.

These notes give a brief introduction to the Gofer system  and  include
some examples of Gofer  programs.   They  are  not  the  notes  that  I
originally intended to write, being somewhat longer  and  perhaps  more
tutorial in nature.  Nevertheless,  you  will  not  be  able  to  learn
functional programming from this document alone.  A  number  of  useful
references are given in the reading list at the end of  this  document.
In particular, the book by Bird and Wadler [1] is particularly good  as
a general introduction to the use, techniques and theory of  functional
programming.  Although their notation is  a  little  different from the
language used by Gofer, it is  a  relatively  straightforward  task  to
translate between the two, and some suggestions for this are given in a
appendix D.  More importantly, the underlying  semantics  of  Gofer  do
correspond to those expected by the authors of [1].

Whereas the work involved in investigating and implementing  the  ideas
on which Gofer is based were motivated largely by  my  own  program  of
work, the writing of these notes has rather more to do  with  the  hope
that Gofer will be  useful  to  others.   I  would  therefore  be  very
grateful for any feedback on any aspect of the these notes (or  of  the
Gofer system itself).  Please let me know if you discover  any  errors,
or if you find particular  sections  of  these  notes  rather  hard  to
follow.  Suggestions for  improvements  or  extensions  are  more  than
welcome.

\chapter{Background and acknowledgements}

The language supported by Gofer is both syntactically and  semantically
similar to that of the functional programming language Haskell [5].  My
principal task in the implementation of Gofer  has  therefore  been  to
decide which  features  I  should  omit  and  then  to  implement  what
remains.  Features common to both include:
\BSI
\item Non-strict semantics (lazy evaluation).
\item Higher-order functions.
\item Extended polymorphic type system  with  support  for  user-defined
     overloading.
\item User-defined algebraic datatypes.
\item Pattern matching.
\item List comprehensions.
\item Facilities for  I/O,  whilst  retaining  referential  transparency
     within a program.
\ESI
For the  benefit  of  readers  familiar  with  Haskell,  the  following
features of Haskell are not supported in the standard version of Gofer:
\BSI
\IT Modules.
\IT Arrays.
\IT Defaults for unresolved overloading.
\IT Derived instances of standard classes.
\IT Contexts in datatype definitions.
\IT Full range of numeric types and classes.
\ESI
But Gofer is not just a partial  implementation  of  Haskell;  it  also
includes a number of experimental features which extend the type system
in several ways:
\BSI
\IT An alternative approach to type classes which avoids the need  for
     construction  of  dictionaries  during  the   evaluation   of   an
     expression.
\IT Type classes may take multiple parameters.
\IT Instances  of  type  classes   may   be   defined   at   arbitrary
     non-overlapping types.
\IT Contexts may include arbitrary type expressions.
\ESI
These extensions stem from my own research [8, 9, 10, 11, 12] and  were
among the principal motivations for the  development  of  Gofer.   Full
details of the differences between Gofer and Haskell 1.1 are  given  in
appendix C.

Gofer would not have been implemented without my original  introduction
to functional programming using  Orwell  [6],  and  I  am  particularly
grateful to Quentin Miller for answering so many of my questions  about
functional programming and about the Orwell system  in  particular.   I
should also like to mention the influence of the  Haskell  B.  compiler
from Lennart Augustsson and Thomas Johnsson and based on their  earlier
LML compiler [7].

Right from the beginning, I wanted to be able to use Gofer on  a  range
of machines - and in particular, on the humble PC that I use  at  home.
With this in mind, Gofer was actually developed on that same  PC  using
Borland's Turbo C 1.5 and a public domain version of  the  yacc  parser
generator that I picked up some time ago.  Gofer was also written  with
some degree of portability in mind and has subsequently  been  compiled
to run on Sun workstations.  I hope it will also be possible to port it
to other platforms.  It is  my  intention  that  Gofer  be  distributed
complete with source code and I hope that this will be of  interest  to
some users.

Many of the ideas used in the back-end of the Gofer  system  (i.e.\  the
compiler and abstract machine) originate from  the  chapters  of  Simon
Peyton Jones textbook [2]; I very much doubt whether Gofer  would  have
been  completed  without  frequent  reference  to   that   book.    The
lambda-lifter used in Gofer is based  on  Thomas  Johnsson's  algorithm
described in [3].

On  the  theoretical  side,  I'm  grateful  to  Phil  Wadler  for   the
encouragement that he has given me with my  work  on  qualified  types.
Many of the basic ideas that I have used were inspired by his  original
paper motivating the use of type classes [4].

\chapter{Starting Gofer}

The Gofer interpreter is usually entered by giving the command {\tt gofer},
after which a display something like the  following  will  normally  be
produced:
\begin{verbatim}
    Gofer Version 2.20
 
    Reading script file "/gofer/prelude":
    Parsing........................................................
    Dependency analysis............................................
    Type checking..................................................
    Compiling......................................................

    Gofer session for:
    /gofer/prelude
    Type :? for help
    ?
\end{verbatim}
The file name \verb"/gofer/prelude" mentioned in the  output  above  is  the
name of a file of standard definitions which are loaded into Gofer each
time that the interpreter is started.  By default,  Gofer  reads  these
definitions from  a  file  called  \verb"prelude"  in  the  current  working
directory.  Alternatively you can set the environment variable \verb"GOFER" to
the name of the  standard  prelude  file,  which  will  then  be  used,
whatever the current working directory might be.

Most commands in Gofer take the form of a colon followed by one or more
characters which distinguish one command from another.  There  are  two
commands which are particularly worth remembering:
\BI
\IT \verb":q"  exits the  Gofer  interpreter.   On most systems, you can also
     exit from Gofer by typing the end of  file  character  (\verb=^Z=  on  an
     {\sc ms-dos} machine, usually \verb=^D= on a Unix based machine).
\IT \verb":?"  prints a list of all the commands,  which can be useful if you
     forget the name of the command that you want to use.
\EI
The complete range of commands supported by the  Gofer  interpreter  is
described in appendix F.

Note that the interrupt key (\verb=^C= on most systems) can  be  used  at  any
time whilst using Gofer to abandon the process of reading in a file  of
function definitions or the evaluation  of  an  expression.   When  the
interrupt key is detected, Gofer prints the string \verb={Interrupted!}= and
prints the `\verb=? =' prompt so that further commands can be entered.


\chapter{Using Gofer} % - a basic introduction

Using Gofer is rather like using a high-level programmable  calculator;
Once the interpreter is loaded, the system  prints  a  prompt  \verb"?"  and
waits for you to enter an expression, and then press the enter (return)
key.  Once the input is complete, Gofer evaluates  the  expression  and
prints its value on the terminal,  before  returning  to  the  original
prompt and waiting for the next expression.  For example:
\begin{verbatim}
    ? (2+3)*8
    40
    (5 reductions, 9 cells)
    ? sum [1..10]
    55
    (91 reductions, 130 cells)
    ? 
\end{verbatim}
In the first example, the user entered the expression \verb"(2+3)*8",  which
was evaluated by Gofer and the result \verb"40" printed on the terminal.  At
the end of any calculation, Gofer displays the number of reductions  (a
measure of the amount of work) and cells (a measure of  the  amount  of
memory) that were used during the calculation.  These  figures  can  be
useful for comparing the performance of different ways of carrying  out
the same calculation.

In the second example, the user typed  the  expression  \verb"sum  [1..10]".
The notation \verb"[1..10]" represents the list of integers between 1 and 10
inclusive, and \verb"sum" is a  standard  function  which  can  be  used  to
determine the sum of a list of integers.  Thus the result  obtained  by
Gofer is:
\[
          1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10  =  55
\]
We could have typed this sum into Gofer directly:
\begin{verbatim}
    ? 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
    55
    (10 reductions, 23 cells)
    ? 
\end{verbatim}
and this calculation is certainly more efficient as it uses only
$\frac{1}{9}$th of the number of reductions and 
$\frac{1}{5}$th of the number  of  cells  as  the
original calculation.  On the other hand, the  original  expression  is
much shorter and you are much less likely to make a mistake  typing  in
the expression \verb"sum [1..200]" than you would be if you tried  to  enter
the sum of the integers from 1 to 200 directly.

You will learn more about the kind of expressions that can  be  entered
into Gofer in the rest of this document.


\chapter{Standard and user-defined functions}

The function \verb"sum" used in the examples above, and indeed the  addition
and multiplication functions ($+$) and ($*$), are  all  standard  functions
which are included as part of a large collection  of  functions  called
the `standard prelude' which are loaded into the Gofer system each time
that you start the interpreter.  Quite a number of useful  calculations
can be carried out using these functions alone, but  for  more  general
use you can also define your own functions and store the definitions in
a file so that these functions can be loaded and used by by  the  Gofer
system.  For example, suppose that you create a file \verb"fact"  containing
the following definition:
\begin{verbatim}
     fact n = product [1..n]
\end{verbatim}
The \verb"product" function is another standard function which can  be  used
to calculate the product of a list of integers, and so the  line  above
defines a  function  \verb"fact"  which  calculates  the  factorial  of  its
argument.  In standard  mathematical notation,  $\I{fact}\; n = n!$   which  is
usually defined informally by an equation of the form:
\[
     n! = 1 * 2 * \cdots * (n-1) * n
\]
Once you become familiar with the notation used by Gofer, you will  see
that the Gofer definition of the  factorial  function  is  really  very
similar to this informal mathematical definition.

In order to use this definition from the  Gofer  interpreter,  we  must
first load the definitions of  the  file  into  the  interpreter.   The
simplest way to do this uses the \verb":l" command:
\begin{verbatim}
    ? :l fact
    Reading script file "fact":
    Parsing......................................................
    Dependency analysis..........................................
    Type checking................................................
    Compiling....................................................

    Gofer session for:
    /gofer/prelude
    fact
    ?
\end{verbatim}
Notice the list of filenames displayed after \verb"Gofer session for:"; this
tells you which files of definitions are currently being used by Gofer,
the first of which is the  file  containing  the  definitions  for  the
standard prelude.  Since the file  containing  the  definition  of  the
factorial function has now  been  loaded,  we  can  make  use  of  this
function in expressions entered to the interpreter:
\begin{verbatim}
    ? fact 6
    720
    (57 reductions, 85 cells)
\end{verbatim}
For another example, recall the  standard  mathematical  formula  which
tells us that  the  number  of  ways  of  choosing  $r$  objects  from  a
collection of $n$ objects is given by $n! / (r! * (n-r)!)$.  In Gofer, this
function can be defined by:
\begin{verbatim}
    comb n r = fact n /(fact r * fact (n-r))
\end{verbatim}
In order to use this function, we can either edit the file \verb"fact" which
contains  the  definition  of  the  factorial  function,   adding   the
definition of \verb"comb" on a new line, or we can include the definition as
part of an expression entered whilst using Gofer:
\begin{verbatim}
    ? comb 5 2 where comb n r = fact n /(fact r * fact (n-r))
    10
    (110 reductions, 161 cells)
    ? 
\end{verbatim}
The ability to define a function as part of an expression like this  is
often quite useful.  However, if the function \verb"comb" were likely to  be
wanted on a number of occasions, it would be more sensible to  add  its
definition to the contents of the file \verb"fact",  instead  of  having  to
repeat the definition each time it is used.

You will learn more about the functions defined in the standard prelude
and find out  how  to  define  your  own  functions  in  the  following
sections.

\chapter{Function names: identifiers and operators}

As the examples of the previous section show, there are  two  kinds  of
name that can be used for a function; identifiers  such  as  \verb"sum"  and
operator symbols such as \verb"+" and \verb"*".
Choosing the appropriate kind of
name for a particular function  can  often  help  to  make  expressions
involving that function easier to read.  If for  example  the  addition
function was represented by the name \verb"plus" rather  than  the  operator
symbol \verb"+" then the sum of the integers from 1 to 5 would  have  to  be
written as:
\begin{verbatim}
    plus (plus (plus (plus 1 2) 3) 4) 5
\end{verbatim}
In this particular case, another way of writing the same sum is:
\begin{verbatim}
    plus 1 (plus 2 (plus 3 (plus 4 5)))
\end{verbatim}
Not only does the use of the identifier \verb"plus" make  these  expressions
larger and more difficult to read than the equivalent expressions using
\verb"+"; it  also  makes  it  very  much  harder  to  see  that  these  two
expressions do actually have the same value.

Gofer distinguishes between the two types of name according to the  way
that they are written:
\BI
\IT  An  identifier  begins with a  letter  of the  alphabet optionally
     followed by a sequence of characters, each of which  is  either  a
     letter,  a  digit,  an  apostrophe  (\verb='=)  or   an   underbar
     (\verb=_=).
     Identifiers representing functions or variables must begin  with  a
     lower case letter (identifiers beginning with an upper case letter
     are  used  to  denote  a  special  kind  of  function   called   a
     `constructor function' described in section 11.1).  The  following
     identifiers are examples of Gofer variable and function names:
\begin{verbatim}
    sum    f    f''    integerSum    african_queen    do'until'zero
\end{verbatim}
     The following identifiers are reserved words in Gofer  and  cannot
     be used as the name of a function or variable:
\begin{verbatim}
    case      of         where      let        in         if
    then      else       data       type       infix      infixl
    infixr    primitive  class      instance
\end{verbatim}
\IT  An  operator symbol  is written using one or more of the following
     symbol characters:
\begin{verbatim}
    :  !  #  $  %  &  *  +  .  /  <  =  >  ?  @  \  ^  |  -
\end{verbatim}
     In addition, the tilde character (\verb=~=) is also  permitted,  although
     only in the first position of an  operator  name\footnote{Haskell
     also makes the  same  restriction  for  the  minus/dash  character
     (\verb=-=).}.   Operator  names  beginning  with  a  colon  are  used  for
     constructor functions in the same  way  as  identifiers  beginning
     with a capital  letter  as  mentioned  above.   In  addition,  the
     following operator symbols have special uses in Gofer:
\begin{verbatim}
    ::    =    ..    @    \    |    <-    ->    ~    =>
\end{verbatim}
     All other operator  symbols can be used as variables  or  function
     names, including each of the following examples:
\begin{verbatim}
    +    ++    &&    ||     <=    ==    /=    //  .
    ==>  $     @@     -*-   \/    /\    ...   ?
\end{verbatim}
     Note that each of the symbols in the first line is  used  in  the
     standard prelude.  If you are interested in using Gofer to develop
     programs for use with a Haskell compiler, you might also  want  to
     avoid using the operator symbols \verb":=", 
     \verb"!", \verb":+" and \verb":%" which are used  to
     support features in Haskell not currently provided  by  the  Gofer
     standard prelude.
\EI
Gofer provides two simple mechanisms which make it possible to  use  an
identifier  as  an  operator  symbol,  or  an  operator  symbol  as  an
identifier:
\BI
\IT  Any  identifier  will be treated as an  operator  symbol  if it is
     enclosed in backquotes (\verb=`=) -- for example, the  expressions  using
     the \verb"plus" function above are a little easier to read  using  this
     technique:
\begin{verbatim}
    (((1 `plus` 2) `plus` 3) `plus` 4) `plus` 5
\end{verbatim}
     In general, an expression of the form \verb"x `op` y" is equivalent  to
     the corresponding expression \verb"op x y", whilst an  expression  such
     as \verb"f x y z" can also be written as \verb"(x `f` y) z".%
     \footnote{For those using Gofer on a  PC,  you  may  find  that  your
     keyboard does not have a backquote key!  In this case  you  should
     still be able to enter a backquote by holding down the key  marked
     ALT, pressing the keys `9' and then `6' on the numeric keypad  and
     then releasing the ALT key.}
\IT  Any  operator symbol  can be treated as an identifier by enclosing
     it in parentheses.  For example, the addition function denoted  by
     the operator symbol \verb"+" is often written as \verb"(+)".
     Any expression
     of the form \verb"x + y" can also be written in the form \verb"(+) x y".
\EI
There are two more technical problems which have to be dealt with  when
working with operator symbols:
\BI
\IT  Precedence: Given operator symbols $(+)$ and 
     $(*)$, should $2*3+4$
     be treated as either $(2*3)+4$ or $2*(3+4)$?

     This problem is solved by assigning  each  operator  a  precedence
     value (an integer in the range 0 to 9).  In a  situation  such  as
     the  above,  we  simply  compare  the  precedence  values  of  the
     operators involved, and carry out  the  calculation  associated
     with  the  highest  precedence  operator  first.    The   standard
     precedence values for $(+)$ and $(*)$
     are 6 and 7 respectively so that
     the expression above will actually be treated as $(2*3)+4$.

\IT  Grouping: The above rule  is only useful when the operator symbols
     involved have  distinct  precedences.   For  example,  should  the
     expression $1-2-3$ be treated as either
     $(1-2)-3$ giving a
     result of $-4$, or as $1-(2-3)$ giving a result of $2$?

     This problem is  solved  by  giving  each  operator  a  `grouping'
     (sometimes called its associativity).  An operator symbol  $(\oplus)$  is
     said to:
     \BI
     \IT  group to the left  if $x \oplus y \oplus z$ 
          is treated as $(x \oplus y) \oplus z$
     \IT  group to the right if $x \oplus y \oplus z$ 
          is treated as $x \oplus (y \oplus z)$
     \EI
     A third possibility is that an expression of the form 
     $x \oplus y \oplus  z$
     is to be treated as ambiguous and will  be  flagged  as  a  syntax
     error.   In  this  case  we  say  that   the   operator   $(\oplus)$   is
     non-associative.

     The standard approach in Gofer is to treat $(-)$ as grouping to  the
     left so that $1 - 2 - 3$ will actually be treated as $(1-2)-3$.
\EI
By  default,  every  operator   symbol   in   Gofer   is   treated   as
non-associative with precedence 9.  These values can be  changed  by  a
declaration of one of the following forms:
\BQ
\begin{tabular}{ll}
    {\tt infixl digit ops} &     to declare operators which group to the left\\
    {\tt infixr digit ops} &     to declare operators which group to the right\\
    {\tt infix  digit ops} &     to declare non-associative operators
\end{tabular}
\EQ
In each of these declarations \verb"ops" represents a  list  of  one  or  more
operator symbols separated by commas and \verb"digit" is an integer between  0
and 9 which gives the precedence value for each of the listed  operator
symbols.  The precedence digit may be omitted in which case a value  of
9 is assumed.  There are a number of restrictions on the use  of  these
declarations:
\BI
\IT  Operator  declarations  can  only  appear  in  files  of  function
     definitions which are loaded into Gofer; they  cannot  be  entered
     directly whilst using the Gofer interpreter.

\IT  At most one operator declaration is permitted for  any  particular
     operator symbol (even if repeated  declarations  all  specify  the
     same precedence and grouping as the original declaration).

\IT  Any file containing a declaration for an operator  precedence  and
     grouping must also contain  a  (top-level)  declaration  for  that
     operator.
\EI
In theory, it is possible to use an operator declaration at  any  point
in a file of definitions.  In practice, it is sensible to  ensure  that
each operator is declared before  the  symbol  is  used.   One  way  to
guarantee this is to place all operator declarations at  the  beginning
of the file [this condition is enforced in Haskell].  Note  that  until
an operator declaration for a particular  symbol  is  encountered,  any
occurrence of that symbol will be treated as a non-associative operator
with precedence 9.

The following operator declarations are taken from the standard prelude:
\begin{verbatim}
    -- Operator precedence table

    infixl 9 !!
    infixr 9 .
    infixr 8 ^
    infixl 7 *
    infix  7 /, `div`, `rem`, `mod`
    infixl 6 +, -
    infix  5 \\
    infixr 5 ++, :
    infix  4 ==, /=, <, <=, >=, >
    infix  4 `elem`, `notElem`
    infixr 3 &&
    infixr 2 ||
\end{verbatim}
and their use is illustrated by the following examples:
\BQ
\begin{tabular}{llp{6cm}}
 Expression: &    Equivalent to: &  Reasons:   \\
 \verb"1 + 2 - 3" & \verb"(1 + 2) - 3"  &  \verb"(+)" and \verb"(-)"
                                           have the same  precedence
                                           and group to the left. \\
 \verb"x : ys ++ zs" &  \verb"x : (ys ++ zs)" &   \verb"(:)" and 
                                           \verb"(++)" have the same precedence
                                           and group to the right.\\
 \verb"x == y || z" & \verb"(x == y) || z" &    \verb"(==)" has higher
                                            precedence than \verb"(||)".\\
 \verb"3 * 4 + 5" & \verb"(3 * 4) + 5" &   \verb"(*)" has higher 
                                           precedence than \verb"(+)".\\
 \verb"y `elem` z:zs" & \verb"y `elem` (z:zs)" & \verb"(:)"  has higher 
                                          precedence than \verb"elem". \\
 \verb"12 / 6 / 3" & syntax error &    ambiguous  use  of  \verb"(/)";
                                      could  mean
                                  either \verb"(12/6)/3" or \verb"12/(6/3)".
\end{tabular}
\EQ
Note that function application always binds more tightly than any infix
operator symbol.  For example, the expression \verb"f x + g y" is equivalent
to \verb"(f x) + (g y)".  Another example which often causes problems is the
expression  \verb"f x + 1", 
which is treated as \verb"(f x)  +  1"  and  not  as
\verb"f (x+1)" as is sometimes expected.


\chapter{Built-in types}

An important part of Gofer is the type system which is used  to  detect
errors  in  expressions  and  function  definitions.    Starting   with
primitive expressions such as numeric constants, Gofer assigns  a  type
to each expression that describes the kind of value represented by  the
expression.

In  general  we write  \I{object} \verb"::" \I{type}
to indicate  that  a  particular
expression has the indicated type.  For example:
\BQ
\begin{tabular}{lp{8cm}}
    \verb"42   :: Int" &   indicating that  42  is an  integer 
                           (\verb"Int" is the
                            name for the type of integer values).\\
    \verb"fact :: Int -> Int" &  indicating  that  \verb"fact"  
                         is a  function  which
                        takes  an  integer  argument  and  returns   an
                        integer value (its factorial).
\end{tabular}
\EQ
The most important property of the type system is that it  is  possible
to determine the type of an expression without having to  evaluate  it.
For example, the information given above  is  sufficient  to  determine
that \verb"fact 42 :: Int" without needing to calculate $42!$ first.

Gofer has a wide range of built-in types, described  in  the  following
sections.  In addition, Gofer also includes facilities for defining new
types as well as types acting as  abbreviations  for  complicated  type
expressions as described in section 11.


\section{Functions}
If \verb"t1" and \verb"t2" are types then \verb"t1 -> t2" 
is the type of a  function  which,
given an argument of type \verb"t1" produces a result of type \verb"t2".
A  function
of type \verb"t1 -> t2" is said to have argument type \verb"t1"
and result type \verb"t2".

In mathematics, the result of applying a function $f$ to an argument $x$ is
traditionally written as $f(x)$.  In many situations,  these  parentheses
are unnecessary and may be omitted when using Gofer.
For example, if \verb"f :: t1 -> t2" and \verb"x :: t1"
then \verb"f x" is the result of applying
\verb"f" to \verb"x" and has type \verb"t2".


If $t_1$, $t_2$,\dots , $t_n$ are type expressions then:
\[
  t_1 \to t_2 \to \dots \to t_n
\]
can be used as an abbreviation for the type:
\[
  t_1 \to (t_2 \to ( \dots \to t_n) \dots ) 
\]
In a similar way, an expression of the form 
$f\; x_1\; x_2 \dots x_n$ is simply an
abbreviation for the expression 
$(\dots ((f\; x_1)\; x_2) \dots x_n)$.

These two conventions allow us to deal with functions taking more  than
one argument rather elegantly.  For example, the type of  the  addition
function \verb"(+)" is:
\begin{verbatim}
    Int -> Int -> Int
\end{verbatim}
In other words, \verb"(+)" is a function which takes an integer argument and
returns a value of type \verb"(Int -> Int)".  For  example,  
\verb"(+)  5"  is  the
function which takes an integer value $n$ and returns the  value  of  the
integer $n$ plus 5.  Hence \verb"(+) 5 4", which is equivalent  to  
\verb"5 + 4",
evaluates to the integer 9 as expected.


\section{Booleans}
Represented by the type \verb"Bool", there are two boolean values written as
\verb"True" and \verb"False". 
The  standard  prelude  includes  several  useful
functions for manipulating boolean values:
\BI
\IT    \verb"(&&), (||) :: Bool -> Bool -> Bool"

        The value of the expression \verb"b && d" is \verb"True"
        if and only if  both
        \verb"b" and \verb"d" are \verb"True".
        If \verb"b" is \verb"False" then \verb"d" is not evaluated.

        The value of the expression \verb"b || d" is \verb"True"
        if either of \verb"b" or  \verb"d"
        is \verb"True".  If \verb"b" is \verb"True" then \verb"d"
        is not evaluated.

\IT    \verb"not  :: Bool -> Bool"

        The value of the expression \verb"not b" is the opposite boolean value
        to that of \verb"b"; \verb"not True = False", \verb"not False = True".
\EI
Gofer includes a special form of `conditional expression' which enables
an expression to select between two alternatives according to the value
of a boolean expression:
\begin{verbatim}
    if b then t else f 
\end{verbatim}
is an expression which is equivalent to \verb"t" if \verb"b"
evaluates to \verb"True", or to
\verb"f" if \verb"b" evaluates to \verb"False".
Note that an expression  of  this  form  is
only acceptable if \verb"b" is an expression of type \verb"Bool"
and if the types  of
\verb"t" and \verb"f" are the same, 
in which case the whole expression also has  that
type.


\section{Integers}
Represented by the type \verb"Int", the integer type includes both  positive
and negative integers such as $-273$, $0$  and  $383$.   Like  many  computer
systems, the range of integer values that can be used is restricted and
calculations using large positive  or  negative  numbers  may  lead  to
(undetected) overflow.

A wide range of operators and functions are  defined  in  the  standard
prelude for use with integers:
\BQ
\begin{tabular}{ll}
    \verb"(+)"&     addition\\
    \verb"(*)"&     multiplication\\
    \verb"(-)"&     subtraction\\
    \verb"(^)"&     raise to power\\
    \verb"negate"&  unary negation\\
    \verb"(/)"&     integer division\\
    \verb"div"&     integer division\\
    \verb"rem"&     remainder\\
    \verb"mod"&     modulo\\
    \verb"odd"&     returns \verb"True" if argument is odd, 
                    \verb"False" otherwise\\
    \verb"even"&    returns \verb"True" if argument is even,
                    \verb"False" otherwise\\
    \verb"gcd"&     returns the greatest common divisor of its two arguments\\
    \verb"lcm"&     returns the least common multiple of its two arguments\\
    \verb"abs"&     returns the absolute value of its argument\\
    \verb"signum"&  returns $-1$, $0$ or $1$ indicating that its argument 
                    is \\
                 &  negative, zero or positive respectively
\end{tabular}
\EQ
An expression of the form \verb"-x" is treated
as the expression \verb"negate x".
`Remainder' is related to integer division by the law:
\begin{verbatim}
    (x `div` y)*y + (x `rem` y) == x
\end{verbatim}
`Modulo' is like remainder except that the modulo has the same
sign as the divisor.
The  less  familiar  operators  are  illustrated   by   the   following
identities:
\begin{verbatim}
    3^4 == 81,          7 `div` 3 == 2,      even 23 == False
    7 `rem` 3 == 1,    -7 `rem` 3 == -1,     7 `rem` -3 == 1
    7 `mod` 3 == 1,    -7 `mod` 3 == 2,      7 `mod` -3 == -2
    gcd 32 12 == 4,    abs (-2) == 2,        signum 12 == 1
\end{verbatim}

\section{Floating point numbers}
Represented by the type \verb"Float", elements of this type can be  used  to
represent fractional values  as  well  as  very  large  or  very  small
quantities.  Such values are however usually only accurate to  a  fixed
number of digits and rounding errors may  occur  in  some  calculations
making significant use of floating point quantities.  A  numeric  value
in an input expression will only be treated as a floating point  number
if it either includes a decimal point such as $3.14159$, or if the number
is too large to be stored as a value of type Int.  Scientific  notation
may also be used to enter floating point quantities; for example  \verb"1.0e3"
is equivalent to $1000.0$, whilst \verb"5.0e-2" is equivalent to $0.05$.
Floating point numbers are not included in all implementations of
Gofer.


\section{Characters}
Represented by  the  type  \verb"Char",  elements  of  this  type  represent
individual characters such as those entered at a  keyboard.   Character
values  are  written  as  single  characters  enclosed  by   apostrophe
characters: e.g.\ \verb='a'=,  \verb='0'=,  \verb='Z'=.
Some  special  characters  must  be
entered using an `escape code'; each of these begins with  a  backslash
character `\verb=\=', followed  by  one  or  more  characters  to  select  the
required character.  Some of the most useful escape codes are:
\BQ
\begin{tabular}{ll}
     \verb=\\=                 & backslash\\
     \verb=\'=                 & apostrophe\\
     \verb=\"=                 & double quote\\
     \verb=\n=                 & newline\\
     \verb=\b= or \verb=\BS=   & backspace\\
     \verb=\DEL=               & delete\\
     \verb=\t= or \verb=\HT=   & tab\\
     \verb=\a= or \verb=\BEL=  & alarm (bell)\\
     \verb=\f= or \verb=\FF=   & formfeed
\end{tabular}
\EQ
Additional escape characters include:
\BQ
\begin{tabular}{lp{10cm}}
     \verb=\^=\I{c}     &control character, where \I{c} is replaced by
                         one of the characters:
                         \verb"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
                         For example, \verb"'\^A'" represents control-A.\\
     \verb=\=\I{number} &representing the character with {\sc ascii} value
                         specified by the given decimal \I{number}.\\
     \verb=\o=\I{number}&representing the character with {\sc ascii} value
                         specified by the given octal \I{number}.\\
     \verb=\x=\I{number}&representing the character with {\sc ascii} value
                         specified by the given hexadecimal \I{number}.\\
     \verb=\=\I{name}   &named {\sc ascii} control character, where
                         \I{name} is replaced by one of the standard
                         {\sc ascii} names e.g.\ \verb"'\DC3'".
\end{tabular}
\EQ
In contrast with  some  common  languages  (such  as  C,  for  example)
character values are quite distinct from integers; however the standard
prelude does include functions:
\begin{verbatim}
    ord :: Char -> Int
    chr :: Int -> Char
\end{verbatim}
which enable you to map a character to its corresponding  {\sc ascii}  value,
or from an {\sc ascii} value to the corresponding character:
\begin{verbatim}
    ? ord 'a'
    97
    (2 reductions, 6 cells)
    ? chr 65
    'A'
    (2 reductions, 7 cells)
    ?         
\end{verbatim}

\section{Lists}
If \verb"t" is a type then \verb"[t]" 
is the type whose elements are lists of  values
of type \verb"t".
There are several ways of writing list expressions:
\BI
\IT   The simplest list of any type is the empty list, written \verb"[]".
\IT   Non-empty lists  can be constructed either by  explicitly listing
      the members of the list (for example: \verb"[1,3,10]") or  by  adding  a
      single element onto the front  of  another  list  using  the  \verb"(:)"
      operator (pronounced `cons').  These notations are equivalent:
\begin{verbatim}
    [1,3,10]  =  1 : [3,10]  =  1 : 3 : [10]  =  1 : 3 : 10 : []
\end{verbatim}
      (the \verb"(:)" operator groups to the right so 
      \verb"1:3:10:[]"  is
      equivalent to \verb"(1:(3:(10:[])))" -- a list whose first element is 1,
      second element is 3 and last element is 10).
\EI
The  standard  prelude  includes  a  wide  range   of   functions   for
calculations involving lists.  For example:
\BSI
\IT  \verb"length xs"  returns the number of elements in the list \verb"xs".
\IT  \verb"xs ++ ys"   returns the list of elements in \verb"xs" followed by the
                elements in \verb"ys"
\IT  \verb"concat xss" returns the list of elements in each of the lists in
                \verb"xss"
\IT  \verb"map f xs"   returns the list of values obtained by applying the
                function \verb"f" to each of the values in the 
                list \verb"xs" in turn.
\ESI
Here are some examples using these functions:
\begin{verbatim}
    ? length [1,3,10]
    3
    (15 reductions, 28 cells)

    ? [1,3,10] ++ [2,6,5,7]
    [1, 3, 10, 2, 6, 5, 7]
    (19 reductions, 77 cells)

    ? concat [[1], [2,3], [], [4,5,6]]
    [1, 2, 3, 4, 5, 6]
    (29 reductions, 93 cells)

    ? map ord ['H', 'e', 'l', 'l', 'o']
    [72, 101, 108, 108, 111]
    (22 reductions, 73 cells)

    ?
\end{verbatim}
Note that all of the elements in a list must be of the  same  type,  so
that an expression such as \verb"['a', 2, False]" is not permitted.

At this point  it  might  be  useful  to  mention  an  informal
convention that is used by a  number  of  functional  programmers  when
choosing names for variables  representing  elements  of  lists,  lists
themselves, lists of lists and  so  on.   If  for  example,  a  typical
element of a list is called \verb"x", then it is often useful to use the  name
\verb"xs" for a list of such values, suggesting that a list contains a  number
of `\verb"x"'s.  Similarly, a list of lists might be  called  
\verb"xss".   Once  you
have understood this convention it  is  much  easier  to  remember  the
relationship between the variables in the  expression  \verb"(x:xs)"  than  it
would be if different names had been used such as \verb"(a:b)".

\section{Strings}
A string is treated as a list of characters  and  the  type  \verb"String"  is
simply an abbreviation for the type \verb"[Char]".   Strings  are  written  as
sequences of characters enclosed between  speech  marks.   All  of  the
escape codes that can be used for characters may  also  be  used  in  a
string:
\begin{verbatim}
    ? "hello, world"
    hello, world
    (0 reductions, 13 cells)

    ? "hello\nworld"
    hello
    world
    (0 reductions, 12 cells)
    ?
\end{verbatim}
In addition, strings may contain the escape sequence \verb"\&" which can  be
used to separate otherwise  ambiguous  pairs  of  characters  within  a
string, e.g.:
\BQ
\begin{tabular}{ll}
    \verb"\123h"   &represents the string \verb"['\123', 'h']"\\
    \verb"\12\&3h" &represents the string \verb"['\12', '3', 'h']"
\end{tabular}
\EQ
A string expression may be spread over a number of lines using a gap --
a non-empty sequence of space, tab and new line characters enclosed  by
backslash characters:
\begin{verbatim}
    ? "hell\   \o"
    hello
    (0 reductions, 6 cells)
    ? 
\end{verbatim}
Notice that strings are printed  differently from other  values,  which
gives the programmer complete control over the  format  of  the  output
produced by a program.  The only values that Gofer can in fact  display
on the terminal are strings.  If the type of an expression entered into
Gofer is equivalent to String then the expression is  printed  directly
by evaluating and printing each character  in  the  list  in  sequence.
Otherwise, the expression to  be  evaluated,  \verb"e",  is  replaced  by  the
expression \verb"show' e" where \verb"show'"
is a built-in function (defined as  part
of the standard prelude)  which  converts  any  value  to  a  printable
representation.  The  only way of printing a  string value  in the same
way as any other value is by explicitly using the \verb"show'" function:
\begin{verbatim}
    ? show' "hello"
    "hello"
    (7 reductions, 24 cells)
    ?
\end{verbatim}
The careful reader may have been puzzled by  the  fact  the  number  of
reductions used in the first three examples above was zero.  This is in
fact quite correct since these expressions are constants and no further
evaluation can be carried out.  For constant expressions of  any  other
type there will always be at least one reduction needed  to  print  the
value since the constant  must  first  be  translated  to  a  printable
representation using the \verb"show'" function.

Because strings are represented as lists  of  characters,  all  of  the
standard prelude functions for manipulating lists can also be used with
strings:
\begin{verbatim}
    ? length "Hello"
    5
    (22 reductions, 36 cells)

    ? "Hello, " ++ "world"
    Hello, world
    (8 reductions, 37 cells)

    ? concat ["super","cali","fragi","listic"]
    supercalifragilistic
    (29 reductions, 101 cells)

    ? map ord "Hello"
    [72, 101, 108, 108, 111]
    (22 reductions, 69 cells)
\end{verbatim}

\section{Tuples and the unit type}
If $t_1$, $t_2$, \dots, $t_n$ are types and $n\geq 2$,
then there is a type of $n$-tuples
written $(t_1, t_2, \dots, t_n)$ whose elements are also written in  the  form
$(x_1, x_2, \dots, x_n)$ where the expressions $x_1$, $x_2$, \dots, $x_n$
have types  $t_1$,
$t_2$, \dots, $t_n$ respectively. For example,
\begin{verbatim}
    (1, [2], 3)   :: (Int, [Int], Int)
    ('a', False)  :: (Char, Bool)
    ((1,2),(3,4)) :: ((Int, Int), (Int, Int))
\end{verbatim}
Note that, unlike lists, the elements in a  tuple  may  have  different
types, although the number of elements in the tuple is fixed.

The unit type is written \verb"()" and has a  single  element  which  is  also
written as \verb"()".  The unit type is of particular interest in  theoretical
treatments of the type system of Gofer, although you  may  occasionally
find a use for it in practical programs.


\chapter{Errors}

\section{Errors detected on input}
After an expression has been entered, but before any attempt is made to
evaluate it, Gofer carries out a number of checks to make sure that the
expression that you typed does not contain any errors.  Here  are  some
examples of the kind of problem that might occur:
\BI
\IT  Syntax errors.  The most common situation in which this happens is
     when  you  make  a  typing  mistake,  either  leaving   out   some
     characters, or perhaps pressing the wrong keys  instead.   In  the
     following example, the user has missed out a `\verb"["' character:
\begin{verbatim}
    ? sum 1..100]
    ERROR: Syntax error in input (unexpected `..')
\end{verbatim}
\IT  Undefined variables.  This happens when you  enter  an  expression
     using a variable or function name that is not defined  in  any  of
     the files of definitions loaded into Gofer.  This can  often  mean
     that you have misspelt the name of a function, or that  the  files
     defining a function have not yet been loaded.  For example:
\begin{verbatim}
    ? sum [1..n]
    ERROR: Undefined variable "n"
\end{verbatim}
\IT  Type errors.  Certain  expressions  are  sensible  only  when  the
     functions used in those expressions are applied to values  of  the
     appropriate type.  For example, whilst the factorial function  can
     be used to calculate the factorial of an integer,  it  is  clearly
     meaningless to try to  determine  the  factorial  of  a  character
     value.  This kind of problem can be detected using  the  types  of
     the components of an expression.  In the expression \verb"fact 'A'", we
     can see that the argument \verb"'A'" has type \verb"Char"
     which does  not  match
     the argument type Int of the factorial function.  This error  will
     be detected by Gofer if you try to evaluate the expression:
\begin{verbatim}
         ? fact 'A'
         ERROR: Type error in application
         *** expression     : fact 'A'
         *** term           : 'A'
         *** type           : Char
         *** does not match : Int
\end{verbatim}
\EI

\section{Errors during evaluation}
If no errors are detected in an input expression, Gofer then begins  to
evaluate that expression.  Despite all of the checks that  are  carried
out before the evaluation begins, it is still possible for an error  to
occur during the evaluation of an expression.   A  typical  example  of
this is an attempt to divide a number by zero.   In  this  case,  Gofer
prints the part of the  expression  being  evaluated  that  caused  the
error, surrounded by braces `\verb"{"' and `\verb"}"':
\begin{verbatim}
    ? 3/0
    {primDivInt 3 0}
    (4 reductions, 30 cells)
    ? 
\end{verbatim}
The function \verb"primDivInt" which appears here is a  primitive  function
used to divide  one  integer  (its  first  argument)  by  another  (the
second).  If an error occurs in just one part of  an  expression,
only the part causing the problem will be displayed:
\begin{verbatim}
    ? 4 + (5/0)
    {primDivInt 5 0}
    (5 reductions, 32 cells)
    ? 
\end{verbatim}
A standard function called \verb"error" is defined in the  standard  prelude
which is often useful for ensuring that appropriate error messages  are
produced when an error occurs:
\begin{verbatim}
    ? error "Problem has occurred"
    {error "Problem has occurred"}
    (23 reductions, 99 cells)
    ? 
\end{verbatim}

\chapter{More about value declarations}

\section{Simple pattern matching}
Although the Gofer standard prelude includes many useful functions, you
will usually need to define a collection of new functions for  specific
problems and calculations.  The declaration of a function  \verb"f"  usually
takes the form of a number of equations of the form:
\[
    \TT{f}\; \I{pat}_1\; \I{pat}_2\; \dots\; \I{pat}_n \;\TT{=}\; \I{rhs}
\]
(or an equivalent expression, if \verb"f"  is  written  as  by  an  operator
symbol).   Each  of  the  expressions  
$\I{pat}_1$, $\I{pat}_2$, \dots, $\I{pat}_n$
represents an argument to the function \verb"f" and is called  a  `pattern'.
The number of such arguments is called the arity of  \verb"f".
If  \verb"f"  is
defined by more than one equation then they must  be  entered  together
and each one must give the same arity for \verb"f".

When a function is defined by more than one equation, it  will  usually
be necessary to evaluate one or more of the arguments to  the  function
to  determine  which  equation  applies.   This   process   is   called
`pattern-matching'.  In all of the previous examples we have used  only
the simplest kind of pattern -- a variable.  As  an  example,  consider
the factorial function defined in section 5:
\begin{verbatim}
    fact n = product [1..n]
\end{verbatim}
If we then wish to evaluate the expression \verb"fact 6" we first match  the
expression \verb"6" against the pattern \verb"n" 
and then evaluate the expression
obtained from \verb"product [1..n]" by replacing 
the variable \verb"n"  with  the
expression \verb"6".  The process of matching the arguments  of  a  function
against the patterns in its definition and obtaining another expression
to be evaluated is called a `reduction'.  Using Gofer, it  is  easy  to
verify that the evaluation of \verb"fact 6" takes one  more  reduction  than
that of \verb"product [1..6]":
\begin{verbatim}
    ? fact 6
    720
    (57 reductions, 85 cells)
    ? product [1..6]
    720
    (56 reductions, 85 cells)
    ? 
\end{verbatim}
Many kinds of constants such as the boolean values True and  False  can
also be used in  patterns,  as  in  the  following  definition  of  the
function \verb"not" taken from the standard prelude:
\begin{verbatim}
    not True  = False
    not False = True
\end{verbatim}
In order to determine the value of an expression of the form  \verb"not b",
we must first evaluate the expression \verb"b".  If  the  result  is  \verb"True"
then we use the first equation  and  the  value  of  \verb"not b"  will  be
\verb"False".  If the value of \verb"b" is \verb"False", then the second  equation  is
used and the value of \verb"not b" will be \verb"True".
Other constants, including integers, characters and strings may also be
used in patterns.  For example, if we define a function \verb"hello" by:
\begin{verbatim}
    hello "Mark"  =  "Howdy"
    hello name    =  "Hello " ++ name ++ ", nice to meet you!"
\end{verbatim}
then:
\begin{verbatim}
    ? hello "Mark"
    Howdy
    (1 reduction, 12 cells)
    ? hello "Fred"
    Hello Fred, nice to meet you!
    (13 reductions, 66 cells)
    ?
\end{verbatim}
Note that the  order  in  which  the  equations  are  written  is  very
important because Gofer always uses the first applicable equation.   If
instead we had defined the function with the equations:
\begin{verbatim}
    hello name    =  "Hello " ++ name ++ ", nice to meet you!"
    hello "Mark"  =  "Howdy"
\end{verbatim}
then the results obtained using this function would have been a  little
different:
\begin{verbatim}
    ? hello "Mark"
    Hello Mark, nice to meet you!
    (13 reductions, 66 cells)
    ? hello "Fred"
    Hello Fred, nice to meet you!
    (13 reductions, 66 cells)
    ?
\end{verbatim}
There are a number of other useful kinds of pattern, some of which  are
illustrated by the following examples:
\BQ
\begin{tabular}{llp{8cm}}
  Wildcard: &      \verb"_"  &      matches  any value  at all;  it is like a
                              variable pattern, except that there is no
                              way of referring to the matched value.\\
  Tuples:   &      \verb"(x,y)" &   matches a  pair  whose  first  and second
                              elements are called \verb"x" and \verb"y"
                              respectively.\\
  Lists:    &      \verb"[x]"   &   matches a list with precisely one element
                              called \verb"x".\\
            &      \verb"[_,2,_]"&  matches  a   list  with   precisely three
                              elements,  the  second  of  which  is the
                              integer 2.\\
            &      \verb"[]"   &    matches the empty list.\\
            &      \verb"(x:xs)"&   matches a non-empty  list with 
                                    head \verb"x" and
                                    tail \verb"xs".\\
  As patterns:&    \verb"p@(x,y)"&  matches a  pair  whose  first and  second
                              components  are  called  \verb"x"  
                              and  \verb"y".   The
                              complete pair can  also  be  referred  to
                              directly as \verb"p".\\
  (n+k) patterns:& \verb"(m+1)"  &  matches an integer value  greater than or
                              equal to 1.  The value referred to by the
                              variable \verb"m" is one  less  than  the  value
                              matched.
\end{tabular}
\EQ
A further kind of pattern (called an irrefutable pattern) is introduced
in section 9.11.

Note that no variable name can be used more than once on the left  hand
side of each equation in a function definition.  The following example:
\begin{verbatim}
    areTheyTheSame x x = True 
    areTheyTheSame _ _ = False 
\end{verbatim}
will not be accepted by the Gofer system, but should instead be defined
using the notation of guards introduced in the next section:
\begin{verbatim}
    areTheyTheSame x y
            | x==y      = True
            | otherwise = False
\end{verbatim}

\section{Guarded equations}
Each of the equations in a function  definition  may  contain  `guards'
which require certain conditions  on  the  values  of  the  function's
arguments to be met.  As an example, here is a function which uses  the
standard prelude function \verb"even :: Int -> Bool" to determine whether  its
argument is an even integer or not, and returns the  string  
\verb="even"=  or
\verb="odd"= as appropriate:
\begin{verbatim}
    oddity n | even n    = "even"
             | otherwise = "odd"
\end{verbatim}
In general, an equation using guards takes the form:
\begin{verbatim}
    f x1 x2 ... xn | condition1  =  e1
                   | condition2  =  e2
                   .
                   . 
                   | conditionm  =  em
\end{verbatim}
This equation is used by evaluating each  of  the  conditions  in  turn
until one of them evaluates to \verb"True", in which case the value  of  the
function is given by the corresponding expression e on the  right  hand
side of the `\verb"="' sign.  In Gofer, the variable 
\verb"otherwise" is defined to
be equal to 
\verb"True", so that writing 
\verb"otherwise" as the condition  in  a
guard means that the corresponding expression will always be used if no
previous guard has been satisfied.

As an aside: in the notation of [1], the above examples would be written as:
\begin{verbatim}
    oddity n        =  "even",   if even n
                    =  "odd",    otherwise
    f x1 x2 ... xn  = e1,     if condition1
                    = e2,     if condition2
                      .
                      .
                    = em,     if conditionm
\end{verbatim}
Translation between the two notations is relatively straightforward.


\section{Local definitions}
Function definitions may include local definitions for variables  which
can be used both in guards and on the right hand side of  an  equation.
Consider the following function which calculates the number of distinct
real roots for a quadratic equation of the form $a*x^2+b*x+c=0$:
\begin{verbatim}
    numberOfRoots a b c | discr>0   =  2
                        | discr==0  =  1
                        | discr<0   =  0
                          where discr = b*b - 4*a*c
\end{verbatim}
The operator \verb"(==)" is used to test whether two values are  equal
or not.  You should take care not to confuse this with the  single  \verb"="
sign used in function definitions.

Local definitions can also be introduced at an arbitrary  point  in  an
expression using an expression of the form:
\BQ
    \TT{let} \I{decls} \TT{in} \I{expr}
\EQ
For example:
\begin{verbatim}
    ? let x = 1 + 4 in x*x + 3*x + 1
    41
    (8 reductions, 15 cells)
    ? let p x = x*x + 3*x + 1  in  p (1 + 4)
    41
    (7 reductions, 15 cells)
    ?
\end{verbatim}

\section{Recursion with integers}
Recursion  is  a  particularly  important  and  powerful  technique  in
functional programming which is useful for defining functions involving
a wide range of datatypes.  In this section, we describe one particular
application of recursion to give  an  alternative  definition  for  the
factorial function from section 5.

Suppose that we wish to calculate the factorial of a given  integer  $n$.
We can split the problem up into two special cases:
\BSI
\IT  If $n$ is zero then the value of $n!$ is 1.
\IT  Otherwise, $n!  = 1 * 2 * \dots * (n\!-\!1) * n = (n\!-\!1)! * n$  
     and  so  we
     can calculate the value of $n!$ by 
     calculating the value  of  $(n\!-\!1)!$
     and then multiplying it by $n$.
\ESI
This process can be expressed directly in  Gofer  using  a  conditional
expression:
\begin{verbatim}
    fact1 n  =  if n==0 then 1 else n * fact1 (n-1)
\end{verbatim}
This definition may seem rather circular; in  order  to  calculate  the
value of $n!$, we must first calculate $(n-1)!$, and unless $n$  is  1,  this
requires the calculation of $(n-2)!$ etcetera \dots
However, if  we  start  with
some positive value for the variable $n$, then we will  eventually  reach
the case where the value of $0!$ is required -- and this does not require
any further calculation.  The following diagram illustrates how  $6!$  is
evaluated using \verb"fact1":
\begin{verbatim}
    fact1 6  ==>  6 * fact1 5
             ==>  6 * (5 * fact1 4)
             ==>  6 * (5 * (4 * fact1 3))
             ==>  6 * (5 * (4 * (3 * fact1 2)))
             ==>  6 * (5 * (4 * (3 * (2 * fact1 1))))
             ==>  6 * (5 * (4 * (3 * (2 * (1 * fact1 0)))))
             ==>  6 * (5 * (4 * (3 * (2 * (1 * 1)))))
             ==>  6 * (5 * (4 * (3 * (2 * 1))))
             ==>  6 * (5 * (4 * (3 * 2)))
             ==>  6 * (5 * (4 * 6))
             ==>  6 * (5 * 24)
             ==>  6 * 120
             ==>  720
\end{verbatim}
Incidentally, there are several other ways  of  writing  the  recursive
definition of \verb"fact1" above in Gofer.  For example, using guards:
\begin{verbatim}
    fact2 n
      | n==0        =  1
      | otherwise   =  n * fact2 (n-1)
\end{verbatim}
or using pattern matching with an integer constant:
\begin{verbatim}
    fact3 0         =  1
    fact3 n         =  n * fact3 (n-1)
\end{verbatim}
Which of these you use is largely a matter of personal taste.

Yet another style of definition uses the \verb=(n+k)=  patterns  mentioned  in
section 9.1:
\begin{verbatim}
    fact4 0         =  1
    fact4 (n+1)     =  (n+1) * fact4 n
\end{verbatim}
which is equivalent to:
\begin{verbatim}
    fact5 n | n==0  =  1
            | n>=1  =  n * fact5 (n-1)
\end{verbatim}
Although each of the above definitions gives the same  result
as the original \verb"fact" function  for  each  non-negative  integer,  the
functions can still be distinguished by the values obtained  when  they
are applied to negative integers:
\BSI
\IT  \verb"fact (-1)" evaluates to the integer 1.
\IT  \verb"fact1 (-1)" causes Gofer to enter an infinite loop, which is only
     eventually terminated when Gofer runs out of `stack space'.
\IT  \verb"fact4 (-1)" causes an evaluation error and prints the
      message \verb"{fact4 (-1)}" on the screen.
\ESI
To most people, this suggests that the definition of \verb"fact4" is perhaps
preferable to that of either \verb"fact" or \verb"fact1" 
as it neither gives  the
wrong answer  without  allowing  this  to  be  detected  nor  causes  a
potentially non-terminating computation.


\section{Recursion with lists}
The same kind of  technique  that  can  be  used  to  define  recursive
functions with integers can also be used to define recursive  functions
on lists.  As an example, suppose that we wish to define a function  to
calculate the length of  a  list.   As  the  standard  prelude  already
includes such a function called \verb"length", we  will  call  the  function
developed here \verb"len" to avoid any conflict.  Now suppose that  we  wish
to find the length of a given list.  There are two cases to consider:
\BSI
\IT  If the list is empty then it has length 0
\IT  Otherwise, it is non-empty and can be written in the  form  \verb"(x:xs)"
     for some element \verb"x" and some list \verb"xs".
     Thus the  original  list  is
     one element longer than \verb"xs", and so has length \verb"1+len xs".
\ESI
Writing these two cases out leads directly to the following definition:
\begin{verbatim}
    len []      =  0
    len (x:xs)  =  1 + len xs
\end{verbatim}
The following diagram illustrates the way that  this  function  can  be
used to determine the length of the list \verb"[1,2,3,4]" (remember that  this
is just an abbreviation for \verb"1:2:3:4:[])":
\begin{verbatim}
    len [1,2,3,4]  ==>  1 + len [2,3,4]
                   ==>  1 + (1 + len [3,4])
                   ==>  1 + (1 + (1 + len [4]))
                   ==>  1 + (1 + (1 + (1 + len [])))
                   ==>  1 + (1 + (1 + (1 + 0)))
                   ==>  1 + (1 + (1 + 1))
                   ==>  1 + (1 + 2)
                   ==>  1 + 3
                   ==>  4
\end{verbatim}
As  further  examples,  you  might  like  to  look  at  the   following
definitions which use similar ideas to define the functions product and
map introduced in earlier sections:
\begin{verbatim}
    product []     = 1
    product (x:xs) = x * product xs
    map f []      =  []
    map f (x:xs)  =  f x : map f xs
\end{verbatim}

\section{Lazy evaluation}
Gofer evaluates expressions using a technique  sometimes  described  as
`lazy evaluation' which means that:
\BSI
\IT  No expression is evaluated until its value is needed.
\IT  No  shared  expression  is  evaluated  more  than  once;  if   the
     expression is ever evaluated then the result is shared between all
     those places in which it is used.
\ESI
The first of these ideas is illustrated by the following function:
\begin{verbatim}
    ignoreArgument x = "I didn't need to evaluate x"
\end{verbatim}
Since the result of the function \verb"ignoreArgument" doesn't depend on the
value of its argument \verb"x", that argument will not be evaluated:
\begin{verbatim}
    ? ignoreArgument (1/0)
    I didn't need to evaluate x
    (1 reduction, 31 cells)
    ?
\end{verbatim}
In some situations, it is useful to be able to force Gofer to  evaluate
the argument to a function before the function is applied.  This can be
achieved using the function \verb"strict" defined in the  standard  prelude;
An expression of the form \verb"strict f x" is evaluated by first evaluating
the argument \verb"x" and then applying the function \verb"f" to the result:
\begin{verbatim}
    ? strict ignoreArgument (1/0)
    {primDivInt 1 0}
    (4 reductions, 29 cells)
    ?
\end{verbatim}
The second  basic  idea  behind  lazy  evaluation  is  that  no  shared
expression should be  evaluated  more  than  once.   For  example,  the
following two expressions can be used to calculate $3*3*3*3$:
\begin{verbatim}
    ? square * square where square = 3 * 3
    81
    (3 reductions, 9 cells)
    ? (3 * 3) * (3 * 3)
    81
    (4 reductions, 11 cells)
    ?
\end{verbatim}
Notice that the first expression requires one less reduction  than  the
second.  Excluding the single reduction step  needed  to  convert  each
integer into a string, the sequences of reductions that will be used in
each case are as follows:
\begin{verbatim}
    square * square where square = 3 * 3
       -- calculate the value of square by reducing 3 * 3 ==> 9
       -- and replace each occurrence of square with this result
       ==> 9 * 9
       ==> 81

    (3 * 3) * (3 * 3)   -- evaluate first (3 * 3)
       ==> 9 * (3 * 3)  -- evaluate second (3 * 3)
       ==> 9 * 9
       ==> 81
\end{verbatim}
Lazy evaluation is a very powerful feature of programming in a language
like Gofer, and means that only the minimum amount  of  calculation  is
used to determine the result of an expression.  The  following  example
is often used to illustrate this point.

Consider the task  of  finding  the  smallest  element  of  a  list  of
integers.  The standard prelude includes a function \verb"minimum" which can
be used for this very purpose:
\begin{verbatim}
    ? minimum [100,99..1]
    1
    (809 reductions, 1322 cells)
    ?
\end{verbatim}
(The expression \verb"[100,99..1]" denotes the list of integers from 1 to  100
arranged in decreasing order, as described in section 10.1).

A rather different approach involves sorting the elements of  the  list
into increasing  order  (using  the  function  \verb"sort"  defined  in  the
standard prelude) and  then  take  the  element  at  the  head  of  the
resulting list  (using  the  standard  function  \verb"head").   Of  course,
sorting the list in its entirety is  likely  to  require  significantly
more work than the previous approach:
\begin{verbatim}
    ? sort [100,99..1]
    [1, 2, 3, 4, 5, 6, 7, 8, ... etc ..., 99, 100]
    (10712 reductions, 21519 cells)
    ?
\end{verbatim}
However, thanks to lazy-evaluation, calculating just the first  element
of the sorted list actually requires less work in this particular  case
than the first solution using \verb"minimum":
\begin{verbatim}
    ? head (sort [100,99..1])
    1
    (713 reductions, 1227 cells)
    ?
\end{verbatim}
Incidentally, it is  probably worth  pointing  out  that  this  example
depends rather heavily on the particular algorithm  used  to  \verb"sort"  a
list of elements.  The results are rather different if we  compare  the
same two approaches used to calculate the maximum value in the list:
\begin{verbatim}
    ? maximum [100,99..1]
    100
    (812 reductions, 1225 cells)
    ? last (sort [100,99..1])
    100
    (10612 reductions, 20732 cells)
    ?
\end{verbatim}
This difference is caused by the fact that each  element  in  the  list
produced by \verb"sort" is  only  known  once  the  values  of  all  of  the
preceding elements are also known.  Thus  the  complete  list  must  be
sorted in order to obtain the last element.


\section{Infinite data structures}
One particular benefit of lazy evaluation is that it makes it  possible
for functions  in  Gofer  to  manipulate  `infinite'  data  structures.
Obviously we cannot hope either to   construct  or  store  an  infinite
object in its entirety -- the advantage of lazy evaluation is  that  it
allows us to construct infinite objects piece  by  piece  as  necessary
(and to reuse the storage space used by parts of the object  when  they
are no longer required).

As a simple example, consider the following function which can be  used
to produce infinite lists of integer values:
\begin{verbatim}
    countFrom n = n : countFrom (n+1)
\end{verbatim}
If we evaluate the expression \verb"countFrom 1", Gofer just prints the list
of integer values beginning with 1 until it is interrupted.  Once  each
element in the list has been printed, the storage  used  to  hold  that
element can be reused to hold later elements in the  list.   Evaluating
this expression is equivalent to using an `infinite' loop to print  the
list of integers in an imperative programming language:
\begin{verbatim}
    ? countFrom 1
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,^C{Interrupted!}
    (53 reductions, 160 cells)
    ?
\end{verbatim}
For practical applications, we are usually only interested in  using  a
finite portion of an infinite data  structure  (just  as  loops  in  an
imperative programming language are usually terminated  after  finitely
many iterations).  For example, using  \verb"countFrom"  together  with  the
function \verb"take" defined in the standard  prelude,  we  can  repeat  the
calculation from section 4 to find the sum of the integers 1 to 10:
\begin{verbatim}
    ? sum (take 10 (countFrom 1))
    55
    (62 reductions, 119 cells)
    ?
\end{verbatim}
The expression \verb"take n xs" evaluates to a list  containing  the
first \verb"n" elements of the list \verb"xs" (or to \verb"xs"
itself if the  list  contains
fewer than n elements).  Thus \verb"countFrom 1" generates the infinite list
of integers, \verb"take 10" ensures that only the  first  ten  elements  are
calculated, and \verb"sum" calculates the sum of those integers as before.

A particular advantage of using infinite data  structures  is  that  it
enables us to describe an object without being tied to  one  particular
application of that object.  Consider the following definition for  the
infinite list of powers of two [1, 2, 4, 8, \dots]:
\begin{verbatim}
    powersOfTwo = 1 : map double powersOfTwo 
                  where double n = 2*n
\end{verbatim}
This list be used in a variety of ways; using the operator \verb"(!!)" defined
in the standard prelude (\verb"xs!!n" evaluates to the \verb"n"th
element of the list
\verb"xs"), we can define a function to find the $n$th power of 2 for  
any given
integer $n$:
\begin{verbatim}
    twoToThe n = powersOfTwo !! n 
\end{verbatim}
Alternatively, we can use the list \verb"powersOfTwo" to define  a  function
mapping lists of  bits  (represented  by  integers  0  and  1)  to  the
corresponding decimal number: simply reverse the order of  the  digits,
multiply each by the corresponding power of two and calculate the  sum.
Using functions from the standard  prelude,  this  translates  directly
into the definition:
\begin{verbatim}
    binToDec ds = sum (zipWith (*) (reverse ds) powersOfTwo)
\end{verbatim}
For example:
\begin{verbatim}
    ? twoToThe 12
    4096
    (15 reductions, 21 cells)
    ? binToDec [1,0,1,1,0]
    22
    (40 reductions, 85 cells)
    ?
\end{verbatim}

\section{Polymorphism}
Given the definition of \verb"product" in section 9.5, it  is  easy  to  see
that product takes a single argument which is a list  of  integers  and
returns a single integer value -- the product of the  elements  of  the
list.  In other words, \verb"product" has type \verb"[Int] -> Int".
On  the  other
hand, it is not immediately clear what the type of the  function  \verb"map"
should be.  Clearly the first argument of \verb"map" must be a function  and
both the second argument and the result are lists, so that the type  of
\verb"map" must be of the form:
\[
               \underbrace{(a\to b)}_{\mbox{type of argument {\tt f}}}
            \to\underbrace{[c]}_{\mbox{type of argument {\tt xs}}}
            \to\underbrace{[d]}_{\mbox{type of result {\tt map f xs}}}
\]
But what can be said about the types $a$, $b$, $c$ and  $d$?   One  possibility
would be to choose $a = b = c = d = \TT{Int}$ which would  be  acceptable  for
expressions such as  \verb"map fact [1,2,3,4]",  but  this  would  not  be
suitable in an expression such as  \verb"map chr [65,75,32]"  because  the
\verb"chr" function does not have type \verb"Int -> Int".

Notice however that the argument type of \verb"f" must be the  same  as  the
type of elements in the second argument (i.e.\  $a=c$)  since  \verb"f"  is
applied to each element in that list.  Similarly, the  result  type  of
\verb"f" must be the same as the type of elements in the result list (i.e.\ $b
= d$) since each element in  this  list  is  obtained  as  a  result  of
applying the function \verb"f" to some value.  It is therefore reasonable to
treat the \verb"map" function as having any type of the form:
\[
                  (a \to b)  \to  [a]  \to  [b]
\]
The letters  $a$  and  $b$  used  in  this  type  expression  represent
arbitrary types and are called type variables.  An  object  whose  type
includes one or more type variables can be thought of  as  having  many
different types and is often described as having a  `polymorphic  type'
(literally: its type has `many shapes').

The ability to define and use polymorphic functions in Gofer turns  out
to be very useful.  Here are the types of some of the other polymorphic
functions which have been used in previous  examples  which  illustrate
this point:
\begin{verbatim}
    length :: [a] -> Int
    (++)   :: [a] -> [a] -> [a]
    concat :: [[a]] -> [a]
\end{verbatim}
Thus we can use precisely the same \verb"length" function to determine  both
the length of a list of integers as well as finding  the  length  of  a
string:
\begin{verbatim}
    ? length [1..10]
    10
    (98 reductions, 138 cells)
    ? length "Hello"
    5
    (22 reductions, 36 cells)
    ? 
\end{verbatim}

\section{Higher-order functions}
In Gofer, function values are treated in much the same way as any other
kind of value; in particular, they can be used both  as  arguments  to,
and results of other functions.

Functions which manipulate  other  functions  in  this  way  are  often
described as `higher-order functions'.  Consider the following example,
taken from the standard prelude:
\begin{verbatim}
    (.)       :: (b -> c) -> (a -> b) -> (a -> c)
    (f . g) x  = f (g x)
\end{verbatim}
As indicated by the type declaration, we think of the \verb"(.)" operator as a
function taking two function arguments and returning  another  function
value as its result.  If \verb"f" and  \verb"g"
are  functions  of  the  appropriate
types, then \verb"(f . g)" 
is a function called the composition of \verb"f"  with  \verb"g".
Applying \verb"(f . g)" to a value is equivalent to applying \verb"g"
to that  value,
and then applying \verb"f" to the result (as described, far  more  eloquently,
by the second line of the declaration above!).

Many problems can often be described very elegantly as a composition of
other functions.  Consider the problem of calculating the total  number
of characters used in a list of strings.  A simple  recursive  function
provides one solution:
\begin{verbatim}
    countChars []     = 0
    countChars (w:ws) = length w + countChars ws 

    ? countChars ["super","cali","fragi","listic"]
    20
    (96 reductions, 152 cells)
    ?
\end{verbatim}
An alternative approach is to notice that we can  calculate  the  total
number of characters by  first  combining  all  of  the  words  in  the
argument list into a single word (using concat) and  then  finding  the
length of that word:
\begin{verbatim}
    ? (length . concat) ["super","cali","fragi","listic"]
    20
    (113 reductions, 211 cells)
    ?
\end{verbatim}
Another solution is to first find the length of each word in  the  list
(using the \verb"map" function to apply \verb"length"  
to  each  word)  and  then
calculate the sum of these individual lengths:
\begin{verbatim}
    ? (sum . map length) ["super","cali","fragi","listic"]
    20
    (105 reductions, 172 cells)
    ?
\end{verbatim}

\section{Variable declarations}
A variable declaration  is  a  special  form  of  function  definition,
almost always consisting of a single equation of the form:
\BQ
     \I{var} \TT{=} \I{rhs}
\EQ
(i.e.\ a function declaration of arity 0).  Whereas the  values  defined
by function declarations of arity $>0$ are guaranteed to be functions, the
values defined by variable declarations may or may not be functions:
\begin{verbatim}
    odd = not . even   -- if an integer is not even then it must be odd
    val = sum [1..100]
\end{verbatim}
Note that variables defined like this at the top level  of  a  file  of
definitions will be evaluated using lazy evaluation.  The first time we
refer  to  the  variable  \verb"val"  defined  above  (either  directly   or
indirectly), Gofer evaluates the sum of the integers from 1 to 100  and
overwrites the definition of \verb"val" with this number.  This  calculation
can then be avoided for each subsequent use of \verb"val" (unless  the  file
containing the definition of \verb"val" is reloaded).
\begin{verbatim}
    ? val
    5050
    (809 reductions, 1120 cells)

    ? val
    5050
    (1 reduction, 7 cells)

    ?
\end{verbatim}
Because of this behaviour, we should probably try to avoid using  variable
declarations where the resulting value will require a  lot  of  storage
space.  If we load a file of definitions including the line:
\begin{verbatim}
    longList = [1..10000]
\end{verbatim}
and  then  evaluate  the  expression  \verb"length longList"   (eventually
obtaining the expected result of 10000), then Gofer will  evaluate  the
definition of \verb"longList" and replace  it  with  the  complete  list  of
integers from  1  upto  10000.   Unlike  other  memory  used  during  a
calculation, it will not be possible to  reuse  this  space  for  other
calculations without reloading the file defining \verb"longList", or loading
other files instead.


\section{Pattern bindings and irrefutable patterns}
Another useful way of defining variables uses `pattern bindings'  which
are equations of the form:
\BQ
      \I{pat} \TT{=} \I{rhs}
\EQ
where the expression on the left hand side is a pattern as described in
section 9.1.  As a simple example of  pattern  bindings,  here  is  one
possible definition for the function \verb"head"  which  returns  the  first
element in a list of values:
\begin{verbatim}
    head xs  =  x  where  (x:ys) = xs
\end{verbatim}
The definition \verb"head (x:_) = x"  used  in  the  standard  prelude  is
slightly more efficient, but otherwise equivalent.

Note that pattern bindings are treated quite  differently  from
function bindings (of which the variable declarations described in  the
last section are a special case).  There are two situations in which an
ambiguity may occur; i.e.\ if the left hand side of  an  equation  is  a
simple variable or an \verb"(n+k)" pattern of the kind  described  in  section
9.1.  In both cases, these are treated as function bindings, the former
being a variable declaration whilst the latter will  be  treated  as  a
definition for the operator symbol \verb"(+)".

Pattern bindings are often useful for defining functions which we might
think of as `returning more  than  one  value'  --  although  they  are
actually packaged up in a single value such as a tuple.  As an example,
consider the function \verb"span" defined in the standard prelude.
\begin{verbatim}
    span :: (a -> Bool) -> [a] -> ([a],[a])
\end{verbatim}
If \verb"xs" is a list of values and \verb"p"
is a predicate, then \verb"span p xs"  returns
the pair of lists \verb"(ys,zs)" such that \verb"ys++zs == xs", 
all of  the  elements
in \verb"ys" satisfy the predicate \verb"p" 
and the first  element  of  \verb"zs" does  not
satisfy \verb"p." A suitable definition, using a pattern  binding  to  obtain
the two lists resulting  from  the  recursive  call  to  \verb"span"  is  as
follows:
\begin{verbatim}
    span p []               = ([],[])
    span p xs@(x:xs')
                | p x       = let (ys,zs) = span p xs' in (x:ys,zs)
                | otherwise = ([],xs)
\end{verbatim}
For consistency with the lazy evaluation strategy used  in  Gofer,  the
right hand side of a pattern binding is not evaluated until  the  value
of one of the  variables  bound  by  that  pattern  is  required.   The
definition:
\begin{verbatim}
    (0:xs) = [1,2,3]
\end{verbatim}
will not cause any errors when it is loaded into Gofer, but will  cause
an error if we attempt to evaluate the variable \verb"xs":
\begin{verbatim}
    ? xs
    {v120 [1, 2, 3]}
    (11 reductions, 46 cells)
    ?
\end{verbatim}
The variable name \verb"v120" appearing in this expression is the name of  a
function called a `conformality check' which is  defined  automatically
by Gofer to ensure that the value on the right hand side of the pattern
binding conforms with the pattern on the left.

Compare this  with  the  behaviour  of  pattern  matching  in  function
definitions such as:
\begin{verbatim}
    ? example [1] where example (0:xs) = "Hello"
    {v126 [1]}
    (4 reductions, 22 cells)
    ?
\end{verbatim}
where  the  equivalent  of  the  conformality  check  is  carried   out
immediately even if none of the values of the variables in the  pattern
are actually required.  The reason for  this  difference  is  that  the
arguments supplied to a function must be evaluated to  determine  which
equation in the definition of the function should be used.   The  error
produced by the example above was caused by the fact that the  argument
[1] does not match the pattern used in the equation defining  \verb"example"
(represented by an internal Gofer function called \verb"v126").

A different kind of behaviour can be obtained using a  pattern  of  the
form \verb"~pat", 
known as an irrefutable (or lazy) pattern.  This pattern can
initially be matched against any value, delaying the  check  that  this
value does indeed match \verb"pat" until the value of  one  of  the  variables
appearing in it is required.  The basic idea (together with the  method
used to implement irrefutable patterns in Gofer) is illustrated by  the
identity:
\BQ
    \verb"f ~"\I{pat} \verb"=" \I{rhs} ~~~is equivalent to~~~
    \verb"f v = "\I{rhs} \verb"where "\I{pat} \verb"= v"
\EQ
The following examples, based  very  closely  on  those  given  in  the
Haskell report [5], illustrate the use of  irrefutable  patterns.   The
variable \verb"undefined" used in these examples is included in the standard
prelude  and  causes  a  run-time  error  each  time  it  is  evaluated
(technically speaking, it represents the $\bottom$ element of the relevant
semantic domain, and is the only value having all possible types):
\begin{verbatim}
   (\ (x,y) -> 0) undefined = {undefined}
   (\~(x,y) -> 0) undefined = 0

   (\ [x] -> 0) [] = {v113 []}
   (\~[x] -> 0) [] = 0

   (\~[x, (a,b)] -> x) [(0,1),undefined] = {undefined}
   (\~[x,~(a,b)] -> x) [(0,1),undefined] = (0,1)

   (\ (x:xs) -> x:x:xs) undefined = {undefined}
   (\~(x:xs) -> x:x:xs) undefined = {undefined}:{undefined}:{undefined}
\end{verbatim}
Irrefutable patterns are not used very frequently,  although  they  are
particularly convenient in some situations (see  section  12  for  some
examples).  Be careful not to use irrefutable patterns where  they  are
not appropriate.  An attempt to define a map function \verb"map'" using:
\begin{verbatim}
    map' f ~(x:xs) = f x : map' f xs
    map' f []      = []
\end{verbatim}
turns out to be equivalent to the definition:
\begin{verbatim}
    map' f ys  =  f x : map f xs where (x:xs) = ys
\end{verbatim}
and will not behave as you might have intended:
\begin{verbatim}
    ? map' ord "abc"
    [97, 98, 99, {v124 []}, {v124 []}, {v^C{Interrupted!}
    (35 reductions, 159 cells)
    ?
\end{verbatim}

\section{Type declarations}
The type system used in Gofer is sufficiently powerful to enable  Gofer
to determine the type of any function without the need to  declare  the
types of its arguments and the return  value  as  in  some  programming
languages.  Despite this, Gofer allows the use of type declarations  of
the form:
\BQ
    $\I{var}_1$, \dots $\I{var}_n$ \verb"::" \I{type}
\EQ
which enable the programmer  to  declare  the  intended  types  of  the
variables $\I{var}_1$,  \dots  $\I{var}_n$
defined  in  either  function  or  pattern
bindings.   There  are  a  number  of  benefits   of   including   type
declarations of this kind in a program:
\BI
\IT  Documentation: The  type  of  a  function  often  provides  useful
     information about the way in which a function is  to  be  used  --
     including the number and order of its arguments.
\IT  Restriction: In some situations, the type of a  function  inferred
     by Gofer is  more  general  than  is  required.   As  an  example,
     consider the following function, intended to act as  the  identity
     on integer values:
\begin{verbatim}
    idInt x  =  x
\end{verbatim}
     Without an explicit type declaration, Gofer treats  \verb"idInt"  as  a
     polymorphic function of type \verb"a -> a" and the expression \verb"idInt 'A'"
     does not cause a type error.  This problem can  be  solved
     by using an explicit type declaration  to  restrict  the  type  of
     \verb"idInt" to a particular instance of the polymorphic 
     type \verb"a -> a":
\begin{verbatim}
    idInt :: Int -> Int
\end{verbatim}
     Note that a declaration such as:
\begin{verbatim}
    idInt :: Int -> a
\end{verbatim}
     is not a valid type for the function \verb"idInt"  (the  value  of  the
     expression \verb"idInt 42" is an  integer  and  cannot  be  treated  as
     having an arbitrary type, depending  on  the  value  of  the  type
     variable \verb"a"), and hence will not be accepted by Gofer.
\IT  Consistency check: As illustrated above, declared types are always
     checked against the definition of a value to make sure  that  they
     are compatible.   Thus  Gofer  can  be  used  to  check  that  the
     programmer's intentions (as described  by  the  types  assigned  to
     variables  in  type  declarations)   are   consistent   with   the
     definitions of those values.
\IT  Overloading: Explicit type declarations can be  used  to  solve  a
     number  of  problems  associated  with  overloaded  functions  and
     values.  See section 14 for further details.
\EI

\chapter{Increasing your power of expression}
 
This section describes a number of useful extensions to the basic range
of expressions used in the previous sections.  None of  these  add  any
extra computational power to Gofer -- anything that can  be  done  with
these constructs  could  also  be  done  with  the  constructs  already
described.  They are however included in Gofer because they allow  many
expressions and function definitions to be  written  more  clearly  and
concisely than the equivalent expressions without these notations.

\section{Arithmetic sequences}
A number of useful  lists  can  be  generated  using  the  notation  of
arithmetic  sequences  (so  named  because  of  their   similarity   to
arithmetic progressions in mathematics).  The following list summarises
the four forms of sequence  expression  that  can  be  used  in  Gofer,
together with their translation using the standard functions  \verb"enumFrom",
\verb"enumFromTo", \verb"enumFromThen" and \verb"enumFromThenTo":
\BQ
\begin{tabular}{llp{6cm}}
    \verb"[ n .. ]"&         \verb"enumFrom n" &
                     Produces the (potentially infinite) list of values
                     starting with the value of  \verb"n" and  increasing  in
                     single steps.\\
    \verb"[ n .. m ]"&       \verb"enumFromTo n m"&
                     Produces the list of  elements  from  \verb"n" upto  and
                     including \verb"m" in single steps.
                     If \verb"m" is less than \verb"n"
                     then the list is empty.\\
    \verb"[ n, m .. ]"&      \verb"enumFromThen n m"&
                     Produces the (potentially infinite) list of values
                     whose first two elements are given by the values \verb"n"
                     and \verb"m".  If \verb"m"
                     is greater than \verb"n" then the  following
                     elements of the list are increasing  in  steps  of
                     the same size.  A similar result is obtained if  \verb"m"
                     is less than \verb"n" in  which  case  the  elements  of
                     \verb"[n,m..]" will be decreasing.  
                     If \verb"n" and \verb"m" are  equal
                     then \verb"[n,m..]" is an infinite  list  in  which  each
                     element is equal to \verb"n".\\
    \verb"[ n, n' .. m ]"&   \verb"enumFromThenTo n n' m"&
                     Produces the list of elements from  \verb"[n,n'..]" upto
                     the limit value \verb"m".
                     If  \verb"m" is  less  than  \verb"n" and
                     \verb"[n,n'..]" is increasing, or \verb"m"
                     is greater than \verb"n" and
                     \verb"[n,n'..]" is decreasing the resulting list will be
                     empty.
\end{tabular}
\EQ
Examples:
\BQ
\begin{tabular}{lcl}
    {\tt [1..]    }& = & {\tt [1, 2, 3, 4, 5, 6, 7, 8, 9,} etc\dots\\
    {\tt [-3..3]  }& = & {\tt [-3, -2, -1, 0, 1, 2, 3]}\\
    {\tt [1..1]   }& = & {\tt [1]}\\
    {\tt [9..0]   }& = & {\tt []}\\
    {\tt [1,3..]  }& = & {\tt [1, 3, 5, 7, 9, 11, 13,} etc\dots\\
    {\tt [0,0..]  }& = & {\tt [0, 0, 0, 0, 0, 0, 0,} etc\dots\\
    {\tt [5,4..]  }& = & {\tt [5, 4, 3, 2, 1, 0, -1,} etc\dots\\
    {\tt [1,3..12]}& = & {\tt [1, 3, 5, 7, 9, 11]}\\
    {\tt [0,0..10]}& = & {\tt [0, 0, 0, 0, 0, 0, 0,} etc\dots\\
    {\tt [5,4..1] }& = & {\tt [5, 4, 3, 2, 1]}
\end{tabular}
\EQ
In  the  standard  prelude,   the   functions   \verb"enumFrom",
\verb"enumFromTo",
\verb"enumFromThen" and \verb"enumFromThenTo" 
are overloaded and may also be used  to
enumerate lists of characters or floating point values:
\begin{verbatim}
    ? ['0'..'9'] ++ ['A'..'Z']
    0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
    (397 reductions, 542 cells)

    ? [1.2, 1.35 .. 2.00]
    [1.2, 1.35, 1.5, 1.65, 1.8, 1.95]
    (56 reductions, 133 cells)
\end{verbatim}
Arithmetic sequences such as those described above play the  same  role
in functional programming languages as the iterative  `for'  constructs
in traditional imperative languages.  A good example  of  this  is  the
example in section 4 used to calculate the sum of the integers  from  1
upto 10 -- \verb"sum [1..10]".   An  equivalent  program  in  an  imperative
language might look something like (especially if you think of C!):
\begin{verbatim}
    int i;
    int total=0;
    for (i=1; i<=10; i++)
        total = total + i;
    return total;
\end{verbatim}
The advantages of the functional notation in this case are clear:
\BSI
\IT  It is more compact.
\IT  It separates the task of  generating  the  sequence  of  integers
      \verb"[1..10]" from the task of finding their sum.
\IT  It does not require the declaration or use of auxiliary variables
      such as \verb"i" and \verb"total" in the above.
\ESI


\section{List comprehensions}
List comprehensions provide another very powerful and compact  notation
for describing certain kinds of list expression.  The basic form  of  a
list comprehension is:
\BQ
    \verb"[" \I{expr} \verb"|" \I{qualifiers} \verb"]"
\EQ
There are three kinds of qualifier that can be used in Gofer:
\BI
\IT  Generators: A qualifier of the form \I{pat} \verb"<-" \I{exp}
     is  used  to  extract
     each element that matches the pattern pat from the list exp in the
     order that they elements appear in that list.  A simple example of
     this is the expression \verb"[x*x | x<-[1..10]]" which denotes  the  list
     of the squares of the integers between  1  and  10  inclusive  and
     evaluates to [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] as expected.

     Formally, we can define the meaning of a list comprehension with a
     single generator by the equation:
\begin{verbatim}
    [ e | pat <- exp ]  =  loop exp
                           where loop []       = []
                                 loop (pat:xs) = e : loop xs
                                 loop (_:xs)   = loop xs
\end{verbatim}
     If \verb"pat" is an irrefutable pattern (for example,  a  variable)  then
     this is equivalent to:
\begin{verbatim}
    [ e | pat <- exp ]  =  map f exp
                           where f pat = e
\end{verbatim}
     The full definition is needed for those cases  where  the  pattern
     pat may not match all of the elements in the list  exp.   This  is
     the case in expressions such as \verb"[ y | (3,y)<-[(1,2),(3,4),(5,6)] ]"
     which evaluates to the singleton list \verb"[4]".
\IT  Filters: A boolean  valued  expression  may  also  be  used  as  a
     qualifier in which case it is  often  called  a  filter.   We  can
     define the meaning of a list comprehension with a single filter by
     the equation:
\begin{verbatim}
    [ e | condition ]  =  if condition then [e] else []
\end{verbatim}
     Whilst this form of list comprehension is occasionally  useful  as
     it stands, it is more common to use filters  in  conjunction  with
     generators as described below.
\IT  Local definitions: A qualifier of the form \I{pat} \verb"=" \I{expr}
     can be used to
     introduce a local definition within  a  list  comprehension.   Its
     meaning can be defined formally using the equation:
\begin{verbatim}
    [ e | pat = exp ]  =  [ let pat=exp in e ]
\end{verbatim}
     As in the case of filters, local  definitions  are  more  commonly
     used within lists of more than one qualifier as  described  below.
     Particular care should be taken to distinguish  a  filter  of  the
     form \I{pat}\verb"=="\I{expr} 
     from a local definition of the form \I{pat}\verb"="\I{expr}.

     (I originally suggested this form of qualifier in a message
     sent to the Haskell mailing list, only to discover that a  similar
     (and more comprehensive) suggestion had been made by Kevin Hammond
     almost a year earlier.  There was a certain amount of  controversy
     surrounding the choice of an appropriate syntax and semantics  for
     the construct and consequently, this feature is not currently part
     of the Haskell  standard.   The  syntax  and  semantics  above  is
     implemented by Gofer in the hope  that  it  will  give  functional
     programmers an opportunity to experiment  with  this  facility  in
     their own programs.)
\EI
The real power of this notation is that it is possible to  use  several
qualifiers, separated by commas on the right of the  vertical  bar  `\verb"|"'
symbol in a list comprehension.  Formally, if \verb"qs1" and \verb"qs2" 
are two  such
lists of qualifiers,  then  we  can  define  the  meaning  of  multiple
qualifiers using:
\begin{verbatim}
    [ e | qs1, qs2 ]  =  concat [ [ e | qs2 ] | qs1 ]
\end{verbatim}
The  following  examples  illustrate  how  this  definition  works   in
practice:
\BI
\IT  Variables generated by later qualifiers  vary  more  quickly  than
     those generated by earlier qualifiers:
\begin{verbatim}
    ? [ (x,y) | x<-[1..3], y<-[1..2] ]
    [(1,1), (1,2), (2,1), (2,2), (3,1), (3,2)]
    (107 reductions, 246 cells)
    ?
\end{verbatim}
\IT  Later qualifiers may use the values generated by earlier ones:
\begin{verbatim}
    ? [ (x,y) | x<-[1..3], y<-[1..x]]
    [(1,1), (2,1), (2,2), (3,1), (3,2), (3,3)]
    (107 reductions, 246 cells)

    ? [ x | x<-[1..10], even x ]
    [2, 4, 6, 8, 10]
    (108 reductions, 171 cells)
    ?
\end{verbatim}
\IT  Variables defined in later qualifiers  hide  those  introduced  by
     earlier  ones.   The  following   expressions   are   valid   list
     comprehensions, but this style of definition in  which  names  are
     reused can result in programs which are difficult  to  understand,
     and is not recommended:
\begin{verbatim}
    ? [ x | x<-[[1,2],[3,4]], x<-x ]
    [1, 2, 3, 4]
    (18 reductions, 53 cells)

    ? [ x | x<-[1,2], x<-[3,4] ]
    [3, 4, 3, 4]
    (18 reductions, 53 cells)
    ?
\end{verbatim}
\IT  Changing  the  order  of  qualifiers  has  a  direct   effect   on
     efficiency.  The following two examples produce the  same  result,
     but the first uses more reductions and cells  because  it  repeats
     the evaluation of \verb"even x" for each possible value of \verb"y".
\begin{verbatim}
    ? [ (x,y) | x<-[1..3], y<-[1..2], even x ]
    [(2,1), (2,2)]
    (110 reductions, 186 cells)
    ? [ (x,y) | x<-[1..3], even x, y<-[1..2] ]
    [(2,1), (2,2)]
    (62 reductions, 118 cells)
    ? 
\end{verbatim}
     The following example illustrates a similar kind of behaviour with
     local definitions; in the first case the expression  \verb"fact x"  is
     evaluated twice for each possible value of \verb"x", whilst the  second
     expression uses a local definition to ensure that  the  evaluation
     is not repeated:
\begin{verbatim}
    ? [ fact x + y | x<-[1..3], y<-[1..2] ]
    [2, 3, 3, 4, 7, 8]
    (246 reductions, 398 cells)

    ? [ factx + y | x<-[1..3], factx = fact x, y<-[1..2] ]
    [2, 3, 3, 4, 7, 8]
    (173 reductions, 294 cells)
    ?
\end{verbatim}
\EI

\section{Lambda expressions}
In addition to  named  function  definitions,  Gofer  also  allows  the
definition and use of unnamed functions using a `lambda expression'  of
the form:
\BQ
    \verb"\" \I{atomicPatterns} \verb"->" \I{expr}
\EQ
This  is  a  slight  generalisation  of  the  form  of  lambda
expression  used  in  most   theoretical   treatments   of   functional
programming and  dating  back  to  the  pioneering  work  of  logicians
including Alonzo Church and Haskell Curry, from whom the programming language
takes its name.  The `\verb"\"' character used at the  beginning  of  a  Gofer
lambda expression has been chosen for  its  resemblance  to  the  greek
letter $\lambda$ (lambda)
that might be used if the standard character set  were  a
little larger.

This expression denotes a function taking a number of  parameters  (one
for each pattern) and producing the result specified by the  expression
to the right of the \verb"->" symbol.  For example, \verb"(\x->x*x)"
represents  the
function which takes a single integer argument  \verb'x'  and  produces  the
square of that number as its result.  Another example is the lambda
expression \verb"(\x y->x+y)" which takes two integer  arguments  and  outputs
their sum; this expression is in fact equivalent to the \verb"(+)" operator:
\begin{verbatim}
    ? (\x y->x+y) 2 3
    5
    (3 reductions, 7 cells)
    ?
\end{verbatim}
A lambda expression of the form illustrated above is equivalent to  the
following expression using a local definition:
\begin{verbatim}
      (let newName <atomic patterns> = <expr> in newName)
\end{verbatim}
where \verb"newName" is a new variable name, chosen to avoid conflicts  with
other variables that are already in use.  This name will be printed  if
you enter an expression involving a lambda expression without supplying
the full number of parameters for that function:
\begin{verbatim}
    ? (\x y -> x+y) 42
    v117 42
    (2 reductions, 14 cells)
    ?
\end{verbatim}
Lambda expressions  are  particularly  useful  for  certain  styles  of
functional programming; an example of this  is  the  continuation-based
approach to I/O described in section 12.


\section{Case expressions}
A case expression can be used to evaluate an expression and,  depending
on the result, return one of a number of  possible  values.   As  such,
case statements are a  straightforward  generalisation  of  conditional
expressions.  Indeed, an expression of the form \verb"if e then t else f" is
in fact equivalent to the case expression:
\begin{verbatim}
    case e of 
      True  -> t
      False -> f
\end{verbatim}
In general, a case expression takes the form \verb"case exp of alts"  where
\verb"exp" is  the  expression  to  be  evaluated  and  \verb"alts" 
is  a  list  of
alternatives, each of which is of the form:
\begin{verbatim}
     pat -> rhs
\end{verbatim}
for a simple alternative
\begin{verbatim}
     pat | condition1 -> rhs1   
         | condition2 -> rhs2  
               .              
               .
         | conditionn -> rhsn
\end{verbatim}
using guard expressions as described in section 9.2
for function definitions.

In Gofer, a case expression of the form \verb"case e of alts"  is  implemented
by choosing a new function name \verb"newName" as in  the  previous  section
and  using  the  alternatives  in  alts  to  construct  an  appropriate
definition for this function (essentially by replacing each `\verb"->"' symbol
with a `\verb"="' symbol).  The complete case expression is  then  treated  as
being equivalent to the expression \verb"newName e".  A  simple  example  of
this is the \verb"scanl" function whose definition in the standard prelude:
\begin{verbatim}
    scanl f q xs = q : (case xs of
                        []   -> []
                        x:xs -> scanl f (f q x) xs)
\end{verbatim}
is equivalent to:
\begin{verbatim}
    scanl f q xs = q : scanl' xs
                   where scanl' []     = []
                         scanl' (x:xs) = scanl f (f q x) xs
\end{verbatim}
This latter form is precisely the definition used in [1] (but using the
name \verb"scan" where Gofer uses \verb"scanl").

Evaluating a case expression in which none of  the  alternatives  match
the value  of  the  discriminant  results  in  an  error  such  as  the
following:
\begin{verbatim}
    ? case [1,2] of [] -> "empty list"
    {v117 [1, 2]}
    (6 reductions, 31 cells)
    ?
\end{verbatim}
The function name \verb"v117" which appears here is the name of the function
which is used internally by Gofer  to  implement  the  case  expression
whilst the expression \verb"[1, 2]" gives the discriminant value which could
not be matched.

By combining case expressions with the lambda expressions introduced in
the previous section, any function declaration can be translated into a
single equation of the form \verb"functionName = expr".  For example,  the
standard function \verb"map" whose definition is usually written as:
\begin{verbatim}
    map f []     = []
    map f (x:xs) = f x : map f xs
\end{verbatim}
can also be defined by the equation:
\begin{verbatim}
    map = \f xs -> case xs of
                     []     -> []
                     (y:ys) -> f y : map f ys
\end{verbatim}
This kind  of  translation  is  used  in  the  implementation  of  many
functional programming languages, including Gofer.   See  Simon  Peyton
Jones book [2] for more details of this.


\section{Operator sections}
As we have seen, most functions in Gofer taking more than one  argument
are treated as a function of a  single  argument,  whose  result  is  a
function which can then be applied to the  remaining  arguments.   Thus
\verb"(+) 1" denotes the function which takes an integer  
argument  \verb"n"  and
returns the integer value \verb"1+n".   Functions  of  this  kind  involving
operator symbols are sufficiently common that Gofer provides a  special
syntax for them.  Using $e$ to denote an atomic expression and the symbol
$\oplus$ to represent an arbitrary infix operator, there are functions 
$(e\oplus)$
and $(\oplus e)$, known as `sections of the operator $(\oplus)$' defined by:
\begin{verbatim}
    (e *) x  = e * x
    (* e) x  = x * e
\end{verbatim}
or, using lambda expressions as introduced in section 10.3:
\begin{verbatim}
    (e *)    =  \x -> e * x
    (* e)    =  \x -> x * e
\end{verbatim}
For example:
\BQ
\begin{tabular}{lp{8cm}}
       \verb"(1+)"&   is the successor function which returns the value
                    of its argument plus 1,\\
       \verb"(1.0/)"& is the reciprocal function,\\
       \verb"(/2)"&   is the halving function,\\
       \verb"(:[])"&  is the function which maps any value to the
                    singleton list containing that element.
\end{tabular}
\EQ

In Gofer, the expressions \verb"(e *)" and \verb"(* e)" 
are actually  treated  as
abbreviations for \verb"(*) e" and \verb"flip (*) e" respectively,  
where  \verb"flip"
is the function defined by:
\begin{verbatim}
     flip        :: (a -> b -> c) -> b -> a -> c
     flip  f x y  =  f y x
\end{verbatim}
There is an important special case which occurs with an  expression  of
the form $(- e)$; this is interpreted  as  \verb"negate e"  and  not  as  the
section which subtracts the value of \verb"e" from its argument.  The latter
function can be written as the section \verb"(+ (- e))"  or  as  
\verb"subtract e"
where \verb"subtract" is the function defined in the standard prelude using:
\begin{verbatim}
    subtract = flip (-)
\end{verbatim}

\section{Explicitly typed expressions}
As described in section 9.12, it is often useful to be able to  declare
the type of a variable defined in a function or pattern  binding.   For
much the same reasons, Gofer allows expressions of the form:
\begin{verbatim}
    expr :: type
\end{verbatim}
so that the type of an expression can be  specified  explicitly.   Note
that the \verb":t" command can be used  to  find  the  type  of  a  particular
expression that is inferred by Gofer:
\begin{verbatim}
    ? :t  \x -> [x]
    \x -> [x] :: a -> [a]

    ? :t  sum . map length
    sum . map length :: [[a]] -> Int
\end{verbatim}
The types inferred in each case can be modified by  including  explicit
types in these expressions:
\begin{verbatim}
    ? :t  (\x -> [x]) :: Char -> String
    \x -> [x] :: Char -> String

    ? :t  sum . map (length :: String -> Int)
    sum . map length :: [String] -> Int
\end{verbatim}
Note that an error occurs if the type declared in an  explicitly  typed
expression is not compatible with the type inferred by Gofer:
\begin{verbatim}
    ? :t (\x -> [x]) :: Int -> a
    ERROR: Declared type too general
    *** Expression    : \x -> [x]
    *** Declared type : Int -> a
    *** Inferred type : Int -> [Int]
\end{verbatim}
Explicitly typed expressions  are  most  commonly  used  together  with
overloaded functions and values as described in section 14.


\chapter{User-defined datatypes and type synonyms}

\section{Datatype definitions}
In addition to the  wide  range  of  built-in  datatypes  described  in
section 7, Gofer also allows the  definition  of  new  datatypes  using
declarations of the form:
\BQ
     {\tt data} \I{Datatype} $a_1$ \dots $a_n$ {\tt =}
      $\I{constr}_1$ {\tt|} \dots {\tt|} $\I{constr}_m$
\EQ
where \I{Datatype} is the name of a new 
type constructor of arity $n\geq 0$,
$a_1$, \dots, $a_n$ are distinct 
type variables representing the  arguments  of
\I{DatatypeName} and $\I{constr}_1$, \dots, 
$\I{constr}_m$ $(m\geq 1)$ describe the way in which
elements of the new datatype are constructed.  Each \I{constr} can take one
of two forms:
\BI
\IT \I{Name} $\I{type}_1$ \dots $\I{type}_r$ 
    where \I{Name} is a previously unused constructor
     function  name  (i.e.\  an  identifier  beginning  with  a  capital
     letter).  This declaration introduces \I{Name} as  a  new  constructor
     function of type: 
\[
    \I{type}_1 \to \dots \to \I{type}_r \to \I{Datatype}\;a_1 \dots a_n
\]
\IT $\I{type}_1 \oplus \I{type}_2$ where $\oplus$ 
      is a previously  unused  constructor
     function  operator (i.e.\  an  operator  symbol  beginning  with  a
     colon).  This declaration introduces $(\oplus)$ as a new  constructor
     function of type: 
\[
    \I{type}_1 \to \I{type}_2 \to \I{Datatype}\;a_1 \dots a_n
\]
\EI
Only the type variables $a_1$, \dots, $a_n$
may appear in the type expressions
in each \I{constr} in the definition of \I{Datatype}.


As a simple example, the following definition introduces a new type \verb"Day"
with elements \verb"Sun", \verb"Mon", \verb"Tue", \verb"Wed", \verb"Thu", \verb"Fri" and \verb"Sat":
\begin{verbatim}
    data Day = Sun | Mon | Tue | Wed | Thu | Fri | Sat
\end{verbatim}
Simple functions manipulating elements of type \verb"Day" can be defined using
pattern matching:
\begin{verbatim}
    what_shall_I_do Sun = "relax"
    what_shall_I_do Sat = "go shopping"
    what_shall_I_do _   = "looks like I'll have to go to work"
\end{verbatim}
Another example uses a pair of constructors to provide a representation
for temperatures which may be given using either of the  centigrade  or
fahrenheit scales:
\begin{verbatim}
    data Temp = Centigrade Float | Fahrenheit Float

    freezing                  :: Temp -> Bool
    freezing (Centigrade temp) = temp <= 0.0
    freezing (Fahrenheit temp) = temp <= 32.0
\end{verbatim}
The following example uses a type variable on the left hand side of the
datatype  definition  to  implement  a   \verb"Set" type   constructor   for
representing sets using a list of values:
\begin{verbatim}
    data Set a = Set [a]
\end{verbatim}
For example, \verb"Set [1,2,3]" is an element of type  \verb"Set Int",  
representing
the set of integers $\{1, 2, 3\}$ whilst 
\verb"Set ['a']" represents  a  singleton
set of type \verb"Set Char".  As this example shows, it is possible to use the
same  name  simultaneously  as  both  a  type  constructor  and  as   a
constructor function.

Datatype definitions may also be  recursive,  using  the  name  of  the
datatype  being  defined  on  the  right  hand  side  of  the  datatype
definition  (mutually   recursive   datatype   definitions   are   also
permitted).  The following example is taken from the Haskell report [5]
and  defines  a  type  representing  binary  trees  with  values  of  a
particular type at their leaves:
\begin{verbatim}
    data Tree a = Lf a | Tree a :^: Tree a
\end{verbatim}
For example,
\begin{verbatim}
    (Lf 12 :^: (Lf 23 :^: Lf 13)) :^: Lf 10 
\end{verbatim}
has type \verb"Tree Int"
and represents the binary tree:
\BQ
\setlength{\unitlength}{1mm}
\begin{picture}(70,20)
\put(0,5){\line(1,0){20}}
\put(20,0){\line(1,0){10}}
\put(20,15){\line(1,0){10}}
\put(30,20){\line(1,0){10}}
\put(30,10){\line(1,0){10}}
\put(40,7.5){\line(1,0){10}}
\put(40,12.5){\line(1,0){10}}

\put(20,0){\line(0,1){15}}
\put(30,10){\line(0,1){10}}
\put(40,7.5){\line(0,1){5}}

\put(32,0){\makebox(0,0)[l]{10}}
\put(52,7.5){\makebox(0,0)[l]{13}}
\put(52,12.5){\makebox(0,0)[l]{23}}
\put(42,20){\makebox(0,0)[l]{12}}
\end{picture}
%                              ,--- 12
%                           ,--| 
%                           |  |  ,--- 23
%                           |  `--| 
%                           |     `--- 13
%                         --| 
%                           `--- 10
\EQ
As an example of a function defined on trees, here are two  definitions
using recursion and pattern matching on tree valued  expressions  which
calculate the list of elements at the leaves of a tree  traversing  the
branches of the tree from left to right.  The first definition  uses  a
simple definition, whilst the second uses an  `accumulating  parameter'
giving a more efficient algorithm:
\begin{verbatim}
    leaves, leaves'  :: Tree a -> [a]

    leaves (Lf l)  = [l]
    leaves (l:^:r) = leaves l ++ leaves r

    leaves' t = leavesAcc t []
                 where leavesAcc (Lf l)  = (l:)
                       leavesAcc (l:^:r) = leavesAcc l . leavesAcc r
\end{verbatim}
Using the binary tree above as an example:
\begin{verbatim}
    ? leaves ((Lf 12 :^: (Lf 23 :^: Lf 13)) :^: Lf 10)
    [12, 23, 13, 10]
    (24 reductions, 73 cells)
    ? leaves' ((Lf 12 :^: (Lf 23 :^: Lf 13)) :^: Lf 10)
    [12, 23, 13, 10]
    (20 reductions, 58 cells)
    ?
\end{verbatim}
\section{Type synonyms}
Type synonyms are used to provide  convenient  abbreviations  for  type
expressions.  A type synonym is introduced  by  a  declaration  of  the
form:
\BQ
    \verb"type" \I{Name} $a_1$ \dots $a_n$ \verb"=" \I{expansion}
\EQ
where \I{Name} is the name of a new type constructor  of  arity  
$n\geq 0$,  
$a_1$, \dots, $a_n$ are distinct type variables 
representing the arguments of  \I{Name}
and \I{expansion} is a type expression.  Note that the only type  variables
permitted in the expansion type are those on the left hand side of  the
synonym definition.  Using this declaration any type expression of  the
form:
\BQ
    \I{Name} $\I{type}_1$ \dots $\I{type}_n$ 
\EQ
is treated as an abbreviation of  the  type  expression  obtained  from
expansion by replacing each of the type variables 
$a_1$, \dots, $a_n$ with  the
corresponding type $\I{type}_1$, \dots, $\I{type}_n$.

The most frequently used type synonym is almost  certainly  the  \verb"String"
type which is a synonym for \verb"[Char]":
\begin{verbatim}
    type String = [Char]
\end{verbatim}
(This definition is actually built in to the Gofer  system,  but
the effect is the same as if this  declaration  were  included  in  the
standard prelude.)

Note that the types of expressions inferred by Gofer will  not  usually
contain any type synonyms unless an explicit type signature  is  given,
either using an explicitly typed expression (section 10.6)  or  a  type
declaration (section 9.12):
\begin{verbatim}
    ? :t ['c']
    ['c'] :: [Char]
    ? :t ['c'] :: String
    ['c'] :: String
    ?
\end{verbatim}
Unlike the datatype declarations described  in  the  previous  section,
recursive  (and  mutually  recursive)  synonym  declarations  are   not
permitted.  This rules out examples such as:
\begin{verbatim}
    type BadSynonym = [BadSynonym]
\end{verbatim}
and ensures that the process of expanding all of the type synonyms used
in any particular type expression  will  always  terminate.   The  same
property does not hold for the illegal definition above, in  which  any
attempt to expand the type BadSynonym would lead to the non-terminating
sequence:
\begin{verbatim}
    BadSynonym ==> [BadSynonym] ==> [[BadSynonym]] ==> ....
\end{verbatim}




\chapter{Dialogues: input and output}

The Gofer system implements a subset of  the  facilities  for  programs
involving I/O described in the Haskell report [5].  In particular, this
makes it possible for Gofer programs to be run  interactively,  and  to
make limited use of  text  files  for  both  reading  and  writing.   A
significant factor in the design of the Haskell I/O facilities is  that
it allows  the  use  of  such  programs  without  loss  of  referential
transparency.

\section{Basic description}
Programs using the I/O facilities in Gofer are modelled by functions of
type Dialogue, defined by the type synonym:
\begin{verbatim}
    type Dialogue    =  [Response] -> [Request]
\end{verbatim}
In other words, a Gofer program produces a list of output values,  each
of which may be thought of as a request for some  particular  input  or
output action, and obtains the corresponding list of  operating  system
responses as its input.  Note that the input list of responses will  be
evaluated lazily; i.e.\ we can ensure that we do not attempt  to  obtain
the response to a given request until that request has been completed.

The current range of requests supported by Gofer is  described  by  the
following datatype definition, taken from the standard prelude:
\begin{verbatim}
    data Request  =  -- file system requests:
                    ReadFile      String         
                  | WriteFile     String String
                  | AppendFile    String String
                     -- channel system requests:
                  | ReadChan      String 
                  | AppendChan    String String
                     -- environment requests:
                  | Echo          Bool
\end{verbatim} 
Each response is an element  of  the  type  defined  by  the  following
datatype definition, using an auxiliary datatype \verb"IOError" to describe  a
variety of error conditions that may occur:
\begin{verbatim}
    data Response = Success
                  | Str String 
                  | Failure IOError
 
    data IOError  = WriteError   String
                  | ReadError    String
                  | SearchError  String
                  | FormatError  String
                  | OtherError   String
\end{verbatim}
The following list describes the kind of  I/O  behaviour  specified  by
each form of Request and indicates the possible  Response  values  that
may be obtained in each case:
\BI
\IT  \verb"ReadFile string":  Read  contents  of  file  named  by   
     \verb"string".
     Possible responses to this request are:
     \BI
     \IT  \verb"Str contents" if the request is successful, 
          where \verb"contents" is
          a string (evaluated lazily) containing the contents of the
          file specified by the \verb"ReadFile" request.
     \IT  \verb"Failure (SearchError name)" occurs if file  \verb"name"
          cannot  be
          accessed.
     \IT  \verb"Failure (ReadError name)" occurs if some  other  error  occurs
          whilst opening the file \verb"name".
     \EI
\IT  \verb"WriteFile name string":  Write  the  given  
     \verb"string"  to  the  file
     \verb"name".  If the file does not already exist, it is created  before
     attempting to write the value to file.  If the file already exists
     then it will be truncated to zero length before the write  begins.
     No response is obtained until the string argument has  been  fully
     evaluated and its contents written to  file.   Possible  responses
     are:
     \BI
     \IT  \verb"Success" if the write to file was completed successfully.
     \IT  \verb"Failure (WriteError msg)" if  an  error  was  detected  whilst
          trying to perform the output.  If the problem occurred whilst
          attempting to open the specified file,  then  \verb"msg"  contains
          the   filename,   otherwise   it   contains    a    printable
          representation of the evaluation error which occurred.
     \EI
\IT  \verb"AppendFile name string":
     Similar to the  \verb"WriteFile" request  except
     that the value of the given \verb"string" is  appended  onto  the  file
     \verb"name" if that file already exists.  The  responses  that  may  be
     obtained from this request are the same as those for \verb"WriteFile".
\IT  \verb"ReadChan name":  Read  from  the  input  stream  \verb"name".  
     Note that
     it is an error to attempt to read from the same channel more  than
     once in the same program.  Possible responses are:
     \BI
     \IT  \verb"Str contents" if the request is successful,  
          where  \verb"contents"
          is  a  string  (evaluated  lazily)  containing  the  list  of
          characters entered on the input stream.
     \IT  \verb"Failure (SearchError name)" if the  named  channel  cannot  be
          found.  The only input channel known to Gofer is the standard
          input channel \verb"stdin".  For convenience, the standard prelude
          defines the variable stdin bound to this string.
     \IT  \verb"Failure (ReadError name)" if a \verb"ReadChan" 
          request for the  named
          channel has already been given by a previous request.
     \EI
\IT  \verb"AppendChan name string":  
     Output \verb"string" on  channel  \verb"name".   No
     response is obtained until the string has been fully evaluated and
     written to the named channel.  Possible responses are:
     \BI
     \IT  \verb"Success" if the append to channel was completed successfully.
     \IT  \verb"Failure (SearchError name)" if the  named  channel  cannot  be
          found.  The only output channels known to Gofer are \verb"stdout",
          \verb"stderr" and \verb"stdecho" 
          (which is actually just  another  name
          for  \verb"stdout"  in  Gofer).   For  convenience,  the  standard
          prelude defines variables \verb"stdout", \verb"stderr" 
          and \verb"stdecho" bound to
          the corresponding string values.
     \IT  \verb"Failure (WriteError msg)"  if  an  error  is  detected  whilst
          trying to perform the output.  The string  \verb"msg"  contains  a
          printable  representation  of  the  evaluation  error   which
          occurred.
     \EI
\IT  \verb"Echo status": Set the echo status on  the  standard  input  channel
     stdin to the given boolean value.  If the  echo  status  is  \verb"True",
     then user input will be echoed onto the screen as it is typed
     and the usual line editing facilities (such a backspace or delete)
     provided by the host system can be used to edit the input lines as
     they are entered.  If the echo status is  False,  then  individual
     characters may be read from the standard input channel without any
     echo or line editing features.

     Note that at most one \verb"Echo" request can be used in a  program,  and
     must precede any \verb"ReadChan" request for \verb"stdin". 
     If  not  set  by  an
     explicit \verb"Echo" request, 
     the echo status defaults to \verb"True".  Possible
     responses are:
     \BI
     \IT  \verb"Success" if the request was completed successfully.
     \IT  \verb"Failure (OtherError msg)"  if  the  request  could  not   be
          completed either because a \verb"readChannel" request for 
          \verb"stdin" has
          already been processed, or because a  previous  \verb"Echo" request
          has already been given.  The  corresponding  values of  \verb"msg"
          are   {\tt stdin already in use}   and
          {\tt repeated Echo request}
          respectively.
     \EI
\EI
A simple example of a program using these facilities to output a short
message on the standard output stream is:
\begin{verbatim}
    helloWorld      :: Dialogue
    helloWorld resps = [AppendChan stdout "hello, world"]
\end{verbatim} 
Any expression entered into Gofer of type \verb"Dialogue" will be treated as
a Gofer program using I/O and will be executed accordingly:
\begin{verbatim}
    ? helloWorld
    hello, world
    (1 reduction, 28 cells)
    ?
\end{verbatim}
Notice that without the explicit type declaration, the type that  would
be inferred for \verb"helloWorld" would  
be  \verb"a -> [Request]",  and  hence
\verb"helloWorld" would not be executed as a \verb"Dialogue" program.  This point can
be illustrated using lambda expressions:
\begin{verbatim}
    ? \resps -> [AppendChan stdout "hello, world"]
    v128
    (1 reduction, 7 cells)
    ? (\resps -> [AppendChan stdout "hello, world"]) :: Dialogue
    hello, world
 
    (1 reduction, 28 cells)
    ? 
\end{verbatim}
In many cases the  structure  of  an  expression  is  enough  to  fully
determine its type  as  \verb"Dialogue" (or  equivalently  as  
\verb"[Response] -> [Request]"), 
in which case no explicit types are required to ensure that
the expression is treated as a Gofer program using I/O:
\begin{verbatim}
    ? \~[Success] -> [AppendChan stdout "hello, world"]
    hello, world
    (1 reduction, 29 cells)
    ?
\end{verbatim}
Note the use of the  irrefutable  pattern  \verb"~[Success]"  for  the  lambda
expression in the last  example;  without  this,  the  usual  rules  of
pattern matching as described in section 9 would force Gofer to try  to
match the pattern \verb"[Success]" against the list of responses,  before  the
corresponding request had been produced:
\begin{verbatim}
    ? \ [Success] -> [AppendChan stdout "hello, world"]

    Aborting Dialogue:
          {error "Attempt to read response before request complete"}
    (50 reductions, 229 cells)
    ?
\end{verbatim}
The next example takes a single string as a parameter and displays  the
contents of the corresponding file:
\begin{verbatim}
    showFile               :: String -> Dialogue 
    showFile name ~(read:_) = [ReadFile name, AppendChan stdout result] 
     where result = case read of Str contents -> contents 
                                 Failure _    -> "Can't open " ++ name 
\end{verbatim}
With a few modifications, we can  implement  a  similar  program  which
prompts for, and reads, a filename from the  standard  input  and  then
reads and displays the contents of that file as before.   This  program
is based on a similar example in the Haskell report [5]:
\begin{verbatim}
    main ~(Success : ~(Str userInput : ~(r3 : _)))  
      = [ AppendChan stdout "Please type a filename: ", 
          ReadChan stdin, 
          ReadFile name, 
          AppendChan stdout (case r3 of Str contents -> contents
                                        Failure _    -> "Can't open "
                                                        ++ name)
        ] where (name : _) = lines userInput
\end{verbatim}




\section{Continuation style I/O}
As an alternative to the `stream-based' approach to programs using  the
I/O facilities in Gofer, the  standard  prelude  defines  a  family  of
functions which enables such programs to be written in a `continuation'
style.  The basic idea is to define a function  corresponding  to  each
different kind of request, whose parameters include the values required
to make the request together with two continuations.  The continuations
are functions describing `what to do next', one of which is used if the
request is successful, the other if the request fails.

As an example, the \verb"ReadFile" request  is  represented  by  the  function
\verb"readFile" whose definition is equivalent to:
\begin{verbatim}
    readFile name fail succ ~(r:rs) = ReadFile name : rest rs
     where rest = case r of Str s           -> succ s
                            Failure ioerror -> fail ioerror
\end{verbatim}
The first thing to happen  when  a  dialogue  expression  of  the  form
\verb"readFile name fail succ"  is  evaluated  is  that  the  corresponding
request \verb"ReadFile name" is added to the list of I/O  requests.   A  new
dialogue value \verb"rest" is chosen,  depending  on  the  response  to  the
ReadFile request, and the program continues by  passing  the  remaining
part of the response list to \verb"rest".  
The functions \verb"succ"  and  \verb"fail"
(called the success and failure  continuations  respectively)  describe
the way in which the new dialogue \verb"rest" is obtained.

The following example (edited a little to fit within the margins of this
document) shows how the readFile function described above can be used to
print the contents of a file called \verb"test" on the display:
\begin{verbatim}
    ? readFile "test" (\ioerror resps -> [])
                      (\s resps->[AppendChan stdout s])
    This is a test message

    (4 reductions, 52 cells)
    ?
\end{verbatim}
The success continuation \verb"(\s resps->[AppendChan stdout s])" used  here
receives the contents of the file \verb"test" in the the parameter \verb"s" and
uses an \verb"AppendChan" request to output that string on  the  display.   As
this example shows, the stream based approach of the  previous  section
can be combined with the continuation based style of  I/O  without  any
difficulty.  The failure continuation \verb"(\ioerror resps -> [])"  ignores
the error condition \verb"ioerror" which caused  the  request  to  fail  and
gives a dialogue which terminates immediately without any action.  For
example, assuming that the file \verb"Test" cannot be found:
\begin{verbatim}
    ? readFile "Test" (\ioerror resps -> [])
                      (\s resps->[AppendChan stdout s])

    (4 reductions, 24 cells)
    ?
\end{verbatim}
In practice, it is  usually  a  good  idea  to  produce  some  kind  of
diagnostic message when an error occurs:
\begin{verbatim}
    ? readFile "Test"
         (\ioerror resps -> [AppendChan stdout (show' ioerror)])
         (\s resps       -> [AppendChan stdout s])
    SearchError "Test"
    (11 reductions, 59 cells)
    ?
\end{verbatim}
In each of the  examples  above,  the  failure  continuation  has  type
\verb"FailCont" as defined by the following type  synonym  in  the  standard
prelude:
\begin{verbatim}
   type FailCont  =  IOError -> Dialogue
\end{verbatim}
Similarly, the success continuation, which takes a string  representing
an input string and produces a new Dialogue has type \verb"StrCont":
\begin{verbatim}
    type StrCont  =  String -> Dialogue
\end{verbatim}
A third kind of continuation is needed for those requests which  return
a  response  of  the  form  \verb"Success"  if  successful   (e.g.\    output
requests).  In this case the continuation is simply another dialogue:
\begin{verbatim}
    type SuccCont =  Dialogue
\end{verbatim}
The following list  gives  the  type  of  each  of  the  six  functions
corresponding to the six different kinds of I/O  request  described  in
the previous section.  Full definitions for each of these functions are
given in appendix B:
\begin{verbatim}
    readFile   :: String -> FailCont -> StrCont -> Dialogue
    writeFile  :: String -> String -> FailCont -> SuccCont -> Dialogue
    appendFile :: String -> String -> FailCont -> SuccCont -> Dialogue
    readChan   :: String -> FailCont -> StrCont  -> Dialogue
    appendChan :: String -> String -> FailCont -> SuccCont -> Dialogue
    echo       :: Bool -> FailCont -> SuccCont -> Dialogue
\end{verbatim}
As an illustration of the use of these functions, we show how  each  of
the example programs from the previous section can be  rewritten  using
the  continuation  based  style  of  I/O,  starting  with  the  program
\verb"helloWorld":
\begin{verbatim}
    helloWorld :: Dialogue
    helloWorld  = appendChan stdout "hello, world" abort done
\end{verbatim}
In this case, the explicit type declaration is  not  actually  required
since the type of the expression is completely determined by  the  type
of \verb"appendChan".  The failure continuation 
\verb"abort" is equivalent to the
function \verb"(\ioerror resps -> [])" described above  and  terminates  the
program if an error occurs without any further action.   In  a  similar
way, \verb"done"  is  the  trivial  dialogue  which  terminates  immediately
without any action.  Both of these values are defined in the standard
prelude:
\begin{verbatim}
   done         :: Dialogue
   done resps    = []
   abort        :: FailCont
   abort ioerror = done
\end{verbatim} 
Using the same approach, the \verb"showFile" and \verb"main"  programs  from  the
previous section are written as:
\begin{verbatim}
    showFile :: String -> Dialogue
    showFile name
     = readFile name (\ioerror -> appendChan stdout
                                     ("Can't open " ++ name) abort done)
                     (\contents-> appendChan stdout contents abort done)
 
    main :: Dialogue
    main  = appendChan stdout "Please type a filename: " abort
            (readChan stdin abort
            (\userInput -> let (name : _) = lines userInput in
             readFile name
              (\ioerror  -> appendChan stdout ("Can't open " ++ name)
                                abort done)
              (\contents -> appendChan stdout contents abort done)))
\end{verbatim}

\section{Interactive programs}
One of the principal motivations for including facilities  for  I/O  in
Gofer programs was to provide a way of using  interactive  programs  as
described in [1].  An interactive program is represented by a  function
of type \verb"String -> String" mapping an input string of characters  entered
at the keyboard into an output string to be displayed on the screen.

There are two functions defined in the standard prelude  which  can  be
used to `execute' functions of this kind as interactive programs:
\BI
\IT  \verb"interact f" executes 
     \verb"f::String->String" as an interactive  program
     with echo on.  This  means  that  characters  are  read  from  the
     keyboard a line at a time.  The usual editing characters  such  as
     backspace can be used to correct mistakes which are noticed before
     the return key is pressed at the end  of  each  line.   The  input
     stream can be terminated by typing an end of file character at the
     beginning of a line:
\begin{verbatim}
    ? interact (map toUpper)
    This text was entered using the interact function
    THIS TEXT WAS ENTERED USING THE INTERACT FUNCTION
    ^Z
    (874 reductions, 1037 cells)
    ?
\end{verbatim}
\IT  \verb"run f" behaves like 
     \verb"interact f" except that echo is turned  off.
     In this case, the only way of terminating the input stream without
     reaching the end of the string produced  by  \verb"f"  is  to  use  the
     interrupt key:
\begin{verbatim}
    ? run (map toUpper)     
    ALTHOUGH THIS IS ENTERED IN LOWER CASE, IT STILL
    APPEARS IN UPPER CASE !
    {Interrupted!}

    (1227 reductions, 1463 cells)
    ?
\end{verbatim}
\EI
(Of these two functions, only \verb"interact" is also included in the
standard prelude for Haskell, although \verb"run" may also  be  added  to  a
Haskell system using the definition below.)

The definitions of \verb"interact" and \verb"run"  
provide  further  examples  of
Gofer programs using simple I/O facilities:
\begin{verbatim}
    interact        :: (String -> String) -> Dialogue
    interact f       = readChan stdin abort
                            (\s -> appendChan stdout (f s) abort done)
 
    run             :: (String -> String) -> Dialogue
    run f            = echo False abort (interact f)
\end{verbatim} 
(Exercise for the interested reader:  construct alternative definitions
for these functions using the stream based approach from section 12.1.)

\chapter{Layout}

\section{Comments}
Comments provide an informal but useful way  of  annotating  a  program
with  a  description  of  its  purpose,  structure   and   development.
Following  the  definition  of  Haskell,  two  styles  of  comment  are
supported by Gofer:
\BI
\IT  A one line comment begins with the  two  characters  \verb"--"  and  is
     terminated at the end of the same line.   Note  that  an  operator
     symbol cannot begin with \verb"--" as  this  will  be  treated  as  the
     beginning of a comment.  It is however possible  to  use  the  two
     characters \verb"--" at any other position within an  operator  symbol.
     Thus a line such as:
\begin{verbatim}
    (xs ++ ys) -- xs
\end{verbatim}
     includes a comment and will actually be treated as if the line had
     been written:
\begin{verbatim}
    (xs ++ ys)
\end{verbatim}
     Whereas the line:
\begin{verbatim}
    xs >--> ys >--> zs
\end{verbatim}
     does not contain any comments (although it  will  cause  an  error
     unless \verb">-->" has been defined  using  an  
     appropriate  \verb"infixl" or
     \verb"infixr" declaration).
\IT  A nested comment begins with the characters \verb"{-",  ends  with  the
     characters \verb"-}" and may span  any  number  of  lines.  The
     initial \verb"{-" string  
     cannot  overlap  with  the  terminating  \verb"-}"
     string so that the shortest possible nested comment is \verb"{--}", and
     not \verb"{-}".  An unterminated nested comment will be treated as  an
     error.

     As the name suggests, comments of this kind may be nested so  that
\begin{verbatim}
    {- {- ... -} ... {- ... -} -}
\end{verbatim}  
     is treated as  a  single  comment.
     This makes nested comments particularly convenient  for  enclosing
     parts  of  a  program  which  may  already  contain  other  nested
     comments.
\EI
Both kinds of comment may be used in expressions entered directly  into
the Gofer system, or more usually, in files of definitions loaded  into
Gofer.  The two  styles  of  comment  may  be  mixed  within  the  same
expression or program, remembering that the string \verb"--" has no  special
significance within a nested comment and that the strings \verb"{-" 
and \verb"-}"
have no special significance in a single line comment.  Thus:
\begin{verbatim}
            [ 2, -- {-                 [ 2, {-
              3, -- -}                   -- -} 3,
              4 ]                        4 ]
\end{verbatim}
are both equivalent to the list expression \verb"[2,3,4]".

\section{The layout rule}
In a tradition dating back at least a quarter of a century to  Landin's
ISWIM family of languages,  most  Gofer  programs  use  indentation  to
indicate the structure of a program.  For example, in a definition such
as:
\begin{verbatim}
    f x y = g (x + w)
            where g u = u + v
                        where v = u * u
                  w   = 2 + y
\end{verbatim}
it is clear from the layout that the definition of w is intended to  be
local to f rather than to g.  Another example  where  layout  plays  an
important role is in distinguishing the two definitions:
\begin{verbatim}
    example x y z = a + b       example x y z = a + b
          where a = f x y             where a   = f x
                b = g z                     y b = g z
\end{verbatim}
There are three situations in Gofer where indentation is typically used
to determine the structure of a program:
\BSI
\IT  At the top-level of a file of definitions.
\IT  In a group of local declarations following either of the  keywords
     \verb"let" or \verb"where".
\IT  In a group of alternatives in a  case  expression,  following  the
     keyword \verb"of".
\ESI
In each case, Gofer actually expects to find a list of  items  enclosed
between braces `\verb"{"' and `\verb"}"' 
with individual  items  separated  from  one
another by semicolons `\verb";"'.  However, if the leading brace is not  found
then Gofer uses the layout rule described below to arrange for 
`\verb"{"', `\verb"}"'
and `\verb";"' tokens to be  inserted  into  the  input  stream  automatically
according to the indentation of each line.

In this way, the first example above will in fact be treated as if the
user had entered:
\begin{verbatim}
    f x y = g (x + w)
            where {g u = u + v
                         where {v = u * u
                  }; w   = 2 + y
    }
\end{verbatim}
or, equivalently, just:
\begin{verbatim}
    f x y = g (x + w) where {g u = u + v where {v = u * u}; w = 2 + y}
\end{verbatim}
where the additional punctuation using 
the `\verb"{"', `\verb"}"' and `\verb";"'  characters
makes the intended grouping clear, regardless of indentation.

The layout rule used in Gofer is the same as that of Haskell,  and  can
be described as follows:
\BI
\IT  An opening brace `\verb"{"' is inserted in front of the  first  token  at
     the beginning of a file or following one of the keywords  \verb"where",
     \verb"let" or \verb"of", unless that token is itself an opening brace.
\IT  A `\verb";"' token is inserted in front of the first token in any subsequent
     line with exactly the same indentation as the token  in  front  of
     which the opening brace was inserted.
\IT  The layout rule ends and a `\verb"}"' token is inserted in front  of  the
     first token in a subsequent line  whose  indentation  is  strictly
     less than that of the token in front of which  the  opening  brace
     was inserted.
\IT  A closing brace `\verb"}"' will also be inserted at any  point  where  an
     otherwise unexpected token is encountered.  This part of the rule
     makes it possible to use expressions such as:
\begin{verbatim}
    let a = fact 12 in a+a
\end{verbatim}
     without needing to use the layout characters explicitly as in:
\begin{verbatim}
    let {a = fact 12} in a+a.
\end{verbatim}
\IT  Lines containing only whitespace (blanks and tabs) and comments do
     not affect the use of the layout rule.
\IT  For the purposes of determining the indentation of each line in  a
     file, tab stops are assumed to be placed every 8 characters,  with
     the leftmost tab stop in column 9.  Each tab character inserts one
     or more spaces as necessary to move to the next tab stop.
\IT  The indentation of the end of file token is zero.
\EI
The following (rather contrived) program, is based on an example in the
Haskell report [5], and provides an extended example of the use of  the
layout rule.  A file containing the following definitions:
\begin{verbatim}
    data Stack a = Empty
                 | MkStack a (Stack a)

    push    :: a -> Stack a -> Stack a
    push x s = MkStack x s

    size  :: Stack a -> Int
    size s = length (stkToList s) where
               stkToList Empty         = []
               stkToList (MkStack x s) = x:xs where xs = stkToList s

    pop :: Stack a -> (a, Stack a)
    pop (MkStack x s) = (x, case s of r -> i r where i x = x)

    top :: Stack a -> a
    top (MkStack x s) = x
\end{verbatim}
will be treated by Gofer as if it has been written:
\begin{verbatim}
    {data Stack a = Empty
                  | MkStack a (Stack a)

    ;push    :: a -> Stack a -> Stack a
    ;push x s = MkStack x s

    ;size  :: Stack a -> Int
    ;size s = length (stkToList s) where
               {stkToList Empty = []
               ;stkToList (MkStack x s) = x:xs where {xs = stkToList s

    }};pop :: Stack a -> (a, Stack a)
    ;pop (MkStack x s) = (x, case s of {r -> i r where {i x = x}})

    ;top :: Stack a -> a
    ;top (MkStack x s) = x
    }
\end{verbatim}
Note that some of the more sophisticated forms of expression cannot  be
written on a single line (and hence entered  directly  into  the  Gofer
system) without explicit use of the layout characters 
`\verb"{"', `\verb"}"' and `\verb";"':
\begin{verbatim}
    ? len [1..10] where len [] = 0;  len (x:xs) = 1 + len xs
    10
    (81 reductions, 108 cells)

    ? f True where f x = case x of True->n where {n=not x}; False->True
    False
    (4 reductions, 11 cells)

    ?
\end{verbatim}
One situation in which the layout  rule  can  cause  problems  is  with
top-level definitions.  For example, the two lines:
\begin{verbatim}
   f x  = 1 + x
    g y = 1 - y
\end{verbatim}
will be treated as a single line 
\begin{verbatim}
   f x = 1 + x g y = 1 - y
\end{verbatim}
which  will
cause a syntax  error.   This  kind  of  problem  becomes  rather  more
difficult to spot if the two definitions are not on  subsequent  lines,
particularly if they are separated by several lines of  comments.   For
this reason, it is usually a good  idea  to  ensure  that  all  of  the
top-level definitions in a file start in the  same  column  (the  first
column is usually the most convenient).  {\sc Cobol} and Fortran  programmers
are not likely to find this problem too distressing :--)

\chapter{Overloading in Gofer}

One of the biggest differences between Gofer and most other programming
languages  (with  the  exception  of  Haskell)  is  the   approach   to
overloading; enabling the definition and use of functions in which  the
meaning of a function symbol may depend on the types of its arguments.

Like Haskell, overloading in Gofer is based around  a  system  of  type
classes which allow overloaded functions to be  grouped  together  into
related groups  of  functions.   Whilst  the  precise  details  of  the
approach to type classes used by Gofer are quite different from those  of
Haskell, both rely on the same basic ideas and use a similar syntax for
defining and using type classes.  It would therefore seem possible that
experience gained with the  overloading  system  in  one  language  can
readily by applied to the other.

The differences embodied in the Gofer system of classes  stem  from  my
own, theoretically based investigations into `qualified types' some  of
which is detailed in references [8-12].  In my  personal  opinion,  the
Gofer system has some significant advantages over the Haskell  approach
(see [12] for details) and one of the principal motivations behind  the
implementation to Gofer was to provide a way of  testing  such  claims.
One fact which I believe has already been established  using  Gofer  is
that the use and implementation of overloaded functions need  not  have
the significant effect on performance that was anticipated  with  early
implementations of Haskell.

This section outlines  the  system  of  type  classes  used  in  Gofer,
indicating briefly how they can be used and how they are implemented.


\section{Type classes and predicates}
A type class can be thought of as a family of types (or more  generally
as a family of tuples of types) whose elements are called instances  of
the class.  If $C$ is the name of  an  $n$-parameter  type  class  then  an
expression of the form $C\; t_1\; t_2 \dots t_n$
where $t_1$, $t_2$, \dots,  $t_n$  are  type
expressions is called a predicate and represents the assertion that the
specified tuple of types is an instance of the class~$C$.

Given a polymorphic function (e.g.\ \verb"map::(a->b)->[a]->[b]"), we are  free
to use the function at any type which can be obtained  by  substituting
arbitrary types for each of the type variables in its type.  In  Gofer,
a type expression may be qualified by  one  or  more  predicates  which
restrict the range of types at which a value can be used.
For example, a function of type 
\verb"C a => a -> a -> a" can be treated as a function
of type \verb"t -> t -> t" for any instance \verb"t" of the class \verb"C".

The predicate \verb"C a" in the type expression in  the  previous  example  is
called the context of the type.  Contexts may  contain  more  than  one
predicate in which case the predicates involved must  be  separated  by
commas and the context enclosed in parentheses as in \verb"(C a, D b)".   The
empty context is written \verb"()" and any type expression \verb"t" 
is equivalent  to
the qualified type \verb"() => t".  For uniformity, a context  with  only  one
element may also be enclosed by parentheses.
For technical reasons, type synonyms are  not  currently  permitted  in
predicates.  This is consistent with the use of predicates in  Haskell,
but may be relaxed, at least in certain cases,  in  later  versions  of
Gofer.


\section{The type class Eq}
The type class \verb"Eq" is a simple and useful example, whose  instances  are
precisely those types whose elements can be tested for  equality.   The
declaration of this class given in the standard prelude is as follows:
\begin{verbatim}

    class Eq a where
        (==), (/=) :: a -> a -> Bool
        x /= y      = not (x == y)
\end{verbatim}
There are three parts in any class declaration.   For  this  particular
example we have:
\BI
\IT  The first line (called the `header') of the declaration introduces
     a name \verb"Eq" for the  class  and  indicates  that  it  has  a  single
     parameter, represented by the type variable \verb"a".

\IT  The  second  line  of  the  declaration  (the  `signature   part')
     indicates that there are functions denoted by the operator symbols
     \verb"(==)" and \verb"(/=)" of type \verb"a -> a -> Bool" 
     for each instance a of  class
     \verb"Eq".  Using the notation introduced in the previous  section,  both
     of these operators have type:
\begin{verbatim}
    Eq a => a -> a -> Bool
\end{verbatim}
     These functions are called the `members' (or  `member  functions')
     of the class.  (This terminology, taken from  Haskell,  is  rather
     unfortunate; thinking of a type class  as  a  set  of  types,  the
     elements of the class are called `instances', whilst the `members'
     of the class correspond more closely  to  the  instance  variables
     that are used in the terminology of object-oriented programming.)

     The intention is that the \verb"(==)" function will be used to  implement
     an equality test for each instance of the  class,  with  the  \verb"(/=)"
     operator providing the  corresponding  inequality  function.   The
     ability to include related groups of  functions  within  a  single
     type class in this way is a useful tool in program design.

\IT  The  third  line  of   the   class   declaration   (the   `default
     definitions') provides a default definition of the  \verb"(/=)"  operator
     in terms of the \verb"(==)" operator.  Thus it is only necessary to  give
     a definition for the \verb"(==)" operator in order to define all  of  the
     member functions for the class \verb"Eq".  It  is  possible  to  override
     default member definitions by giving an alternative definition  as
     appropriate for specific instances of the class.
\EI
\subsection{Implicit overloading}
Member functions are clearly marked as overloaded  functions  by  their
definition as part of a class declaration, but this is not the only way
in which overloaded  functions  occur  in  Gofer;  the  restriction  to
particular instances of a type class is also carried over into the type
of any function defined either directly or indirectly in terms  of  the
member functions of that class.  For example, the  types  inferred  for
the following two functions:
\begin{verbatim}
    x  `elem`   xs   =   any (x==) xs
    xs `subset` ys   =   all (`elem` ys) xs
\end{verbatim}
are:
\begin{verbatim}
    elem   :: Eq a => a -> [a] -> Bool
    subset :: Eq a => [a] -> [a] -> Bool
\end{verbatim}
(On the  other  hand,  if  none  of  the  functions  used  in  a
particular expression or definition are overloaded then there will  not
be any overloading in the corresponding value.  Gofer does not  support
the concept of implicit overloading used  in  some  languages  where  a
value of a particular type might automatically be coerced to a value of
some supertype.  An example of this would be the automatic  translation
of a badly typed expression \verb"1.0 == 1" to a  well-typed  expression  of
the form \verb"1.0 == float 1" for some  (potentially  overloaded)  coercion
function \verb"float" mapping numeric values to elements of type Float.)

Note also that the types appearing in the context of a  qualified  type
reflect the types at which overloaded functions are used.  Thus:
\begin{verbatim}
    f x ys  =  [x] == ys
\end{verbatim}
has type  \verb"Eq [a] => a -> [a] -> Bool", 
and not \verb"Eq a => a -> [a] -> Bool",
which is the type that would be assigned to \verb"f" in a Haskell system.


\subsection{Instances of class Eq}
Instances of a type class are defined  using  declarations  similar  to
those used to define  the  corresponding  type  class.   The  following
examples, taken from the standard prelude, give the definitions  for  a
number of simple instances of the class \verb"Eq":
\begin{verbatim}
    instance Eq Int  where  (==) = primEqInt

    instance Eq Bool where
        True  == True   =  True
        False == False  =  True
        _     == _      =  False

    instance Eq Char  where  c == d  =  ord c == ord d

    instance (Eq a, Eq b) => Eq (a,b) where
        (x,y) == (u,v)  =  x==u && y==v

    instance Eq a => Eq [a] where
        []     == []     =  True
        []     == (y:ys) =  False
        (x:xs) == []     =  False
        (x:xs) == (y:ys) =  x==y && xs==ys
\end{verbatim}
The interpretation of these declarations is as follows:
\BI
\IT  The first declaration  makes \verb"Int" an instance  of  
     class  \verb"Eq".   The
     function \verb"primEqInt" is a primitive Gofer function which tests the
     equality of two integer values and has type \verb"Int->Int->Bool"
     which tests the equality of two integer values.

\IT  The second declaration makes  \verb"Bool" an 
     instance of  class \verb"Eq" with a
     simple definition involving pattern matching.

\IT  The third declaration makes \verb"Char" 
     an instance of  class  \verb"Eq".   This
     definition indicates that a pair of characters are equal  if  they
     have the same {\sc ascii} value,  
     which  is  obtained  using  the  \verb"ord"
     function.  Note that the two occurrences of the symbol \verb"(==)" in the
     equation:
\begin{verbatim}
    c == d  =  ord c == ord d
\end{verbatim}
     have  different  meanings;  the  first  denotes  equality  between
     characters (elements of type  \verb"Char"),  whilst  the  second  denotes
     equality between integers (elements of type \verb"Int").

\IT  The fourth declaration provides an equality  operation  on  pairs.
     Given two elements \verb"(x,y)" and \verb"(u,v)" 
     of type \verb"(a,b)" for some \verb"a", \verb"b", it
     must be possible to check that both \verb"x==u" and \verb"y==v" 
     before we can be
     sure that the two pairs are indeed equal.  In other words, both  \verb"a"
     and \verb"b" must also be instances of \verb"Eq"  
     in  order  to  make  \verb"(a,b)"  an
     instance of \verb"Eq".  This requirement is described by the  first  line
     in the instance declaration using the expression:
\begin{verbatim}
    (Eq a, Eq b) => Eq (a,b)
\end{verbatim}

\IT  The fifth declaration makes \verb"[a]" an instance of \verb"Eq", 
     whenever  \verb"a"  is
     itself an instance  of  \verb"Eq"  in  a  similar  way  to  the  previous
     example.  The context \verb"Eq" a is used in the  last  equation  in  the
     declaration:
\begin{verbatim}
    (x:xs) == (y:ys)  =  x==y && xs==ys
\end{verbatim}
     which contains three occurrences of the \verb"(==)" operator;  the  first
     and third are used to compare lists of type \verb"[a]", whilst the second
     is used to compare elements of type \verb"a", using the 
     instance \verb"Eq a".
\EI
Combining these five declarations, we obtain definitions for \verb"(==)" on an
infinite  family  of  types  including  
\verb"Int",  \verb"Char",  \verb"Bool",  \verb"(Int,Bool)",
\verb"(Char,Int)", \verb"[Char]", 
\verb"(Bool,[Int])", \verb"[(Bool,Int)]", etc.:
\begin{verbatim}
    ? 2 == 3                            -- using Eq Int
    False
    (2 reductions, 10 cells)
    ? (["Hello"],3) == (["Hello"],3)    -- using Eq ([[Char]],Int)
    True
    (31 reductions, 65 cells)
    ?
\end{verbatim}
On the other hand, any attempt to use \verb"(==)" to compare elements of  some
type not covered by a suitable instance declaration will result  in  an
error.  For example, the standard prelude does not define the  equality
operation on triples of values:
\begin{verbatim}
    ? (1,2,3) == (1,2,3)
    ERROR: Cannot derive instance in expression
    *** Expression        : (==) d125 (1,2,3) (1,2,3)
    *** Required instance : Eq (Int,Int,Int)
    ?
\end{verbatim}
This can be solved by including an instance  declaration  of  the  following form
into a file of definitions loaded into Gofer:
\begin{verbatim}
    instance (Eq a, Eq b, Eq c) => Eq (a,b,c) where
        (x,y,z) == (u,v,w)  =  x==u && y==v && z==w
\end{verbatim}
Giving:
\begin{verbatim}
    ? (1,2,3) == (1,2,3)
    True
    (6 reductions, 20 cells)
    ?
\end{verbatim}
In general, an instance declaration has the form:
\BQ
    \verb"instance"  \I{context} \verb"=>" \I{predicate}  \verb"where"\\
    {\em definitions of member functions}
\EQ

The context part of the declaration gives a list  of  predicates  which
must be satisfied for the predicate on the right hand side of the  `\verb"=>"'
sign to be valid.  Constant predicates (i.e.\ predicates  not  involving
any type variables) required by an instance declaration  (such  as  the
predicate \verb"Eq Int"  required  by  the  third  declaration)  need  not  be
included in the context.  If the resulting context is empty (as in  the
first three declarations above) then it may be omitted,  together  with
the corresponding `\verb"=>"' symbol.


\subsection{Testing equality of represented values}
Instances of  \verb"Eq"  can  also  be  defined  for  other  types,  including
user-defined datatypes, and unlike the instances described  above,  the
definition of \verb"(==)" need not be used to  determine  whether  the  values
being compared have the same structure; it  is  often  more  useful  to
check that they represent the same value.  As an example, suppose  that
we introduce a type constructor Set for representing  sets  of  values,
using a list to store the values held in the set:
\begin{verbatim}
    data Set a = Set [a]
\end{verbatim}
As usual, we say that two sets are equal if they have the same members,
ignoring any repetitions or differences in the ordering of the elements
in the lists  representing  the  sets.   This  is  achieved  using  the
following instance declaration:
\begin{verbatim}
    instance Eq a => Eq (Set a) where
        Set xs == Set ys  =  xs `subset` ys  &&  ys `subset` xs
                             where xs `subset` ys = all (`elem` ys) xs
\end{verbatim}
A couple of examples illustrate the use of this definition:
\begin{verbatim}
    ? Set [1,2,3] == Set [3,4,1]
    False
    (49 reductions, 89 cells)
    ? Set [1,2,3] == Set [1,2,2,2,1,3]
    True
    (157 reductions, 240 cells)
    ? 
\end{verbatim}

\subsection{Instance declarations without members}
It is possible to give an instance declaration without  specifying  any
definitions for the member functions of the class.  For example:
\begin{verbatim}
    instance Eq ()
\end{verbatim}
In this case, the definition of  \verb"(==)" for the instance  \verb"Eq ()"  
is  left
completely undefined, and hence so is the definition of \verb"(/=)", which  is
defined in terms of \verb"(==)":
\begin{verbatim}
    ? () == ()
    {undefined_member (==)}
    (3 reductions, 34 cells)
    ? () /= ()
    {undefined_member (==)}
    (4 reductions, 36 cells)
    ? 
\end{verbatim}

\subsection{Equality on function types}
If an expression requires an  instance  of  a  class  which  cannot  be
obtained using the rules in the given instance  declarations,  then  an
error message will be produced when  the  expression  is  type-checked.
For example, in general there is no sensible way to  determine  when  a
pair of functions are equal, and the standard prelude does not  include
a definition for an instance of the form \verb"Eq (a -> b)" for  any  
types  \verb"a"
and \verb"b":
\begin{verbatim}
    ? (1==) == (\x->1==x)
    ERROR: Cannot derive instance in expression
    *** Expression        : (==) d148 ((==) {dict} 1) (\x->(==) {dict} 1 x)
    *** Required instance : Eq (Int -> Bool)
    ?
\end{verbatim}
If for some reason, you would prefer this kind of error to  produce  an
error message when an expression is evaluated, rather than when  it  is
type-checked, you can  use  an  instance  declaration  to  specify  the
required behaviour.  For example:
\begin{verbatim}
    instance Eq (a -> b) where 
        (==) = error "Equality not defined between functions"
\end{verbatim}
Evaluating the previous expression once this instance  declaration  has
been included now produces the following result:
\begin{verbatim}
    ? (1==) == (\x->1==x)
    {error "Equality not defined between functions"}
    (42 reductions, 173 cells)
    ? 
\end{verbatim}
A limited form of equality can be defined for functions of type  \verb"(a->b)"
if \verb"a" has only finitely many elements, such as the boolean 
type \verb"Bool":
\begin{verbatim}
    instance Eq a => Eq (Bool -> a) where
        f == g   =   f False == g False   &&   f True == g True
\end{verbatim}
(This instance declaration would not be accepted  in  a  Haskell
program which insists that the predicate  on  the  right  of  the  `\verb"=>"'
symbol contains precisely one type constructor symbol.)

Using this instance declaration once for each argument, we can now test
two functions taking boolean arguments for equality (assuming of course
that their result type is also an instance of \verb"Eq").
\begin{verbatim}
    ? (&&) == (||)
    False
    (9 reductions, 21 cells)
    ? not == (\x -> if x then False else True)
    True
    (8 reductions, 16 cells)
    ? (&&) == (\x y-> if x then y else False)
    True
    (16 reductions, 30 cells)
    ? 
\end{verbatim}

\subsection{Non-overlapping instances}
Other instance declarations for types of the form \verb"a->b" can be used at
the same time, so  long  as  no  pair  of  declarations  overlap.   For
example, adding the following instance declaration
\begin{verbatim}
    instance Eq a => Eq (() -> a)  where  f == g  =  f () == g ()
\end{verbatim}
enables us to evaluate expressions such as:
\begin{verbatim}
    ? (\()->"Hello") == const "Hello"
    True
    (30 reductions, 55 cells)
    ? 
\end{verbatim}
If however, we try to use instance declarations for types of the  form
\verb"(a->b)" and \verb"(Bool->a)" 
at the same time, then Gofer produces an error
message similar to the following:
\begin{verbatim}
    ERROR "file" (line 37): Overlapping instances for class "Eq"
    *** This instance   : Eq (a -> b)
    *** Overlaps with   : Eq (Bool -> a)
    *** Common instance : Eq (Bool -> a)
    ? 
\end{verbatim}
indicating that, given the task of testing two values of type \verb"(Bool->a)"
for equality, there are (at least) two definitions of \verb"(==)"  that  could
be used, with potentially different  results  being  obtained  in  each
case.

Here is a further example of the use of non-overlapping instances of  a
class to define a function \verb"cat" (inspired by the Unix  command  of
the same name) which uses the I/O facilities  of  Gofer  to  print  the
contents of one or more files on the terminal:
\begin{verbatim}
    class    Cat a        where cat  :: a -> Dialogue
    instance Cat [Char]   where cat n = showFile n done
    instance Cat [[Char]] where cat   = foldr showFile done

    showFile name cont = readFile name abort
                             (\s->appendChan stdout s abort cont)
\end{verbatim} 
Given these declarations, an expression of the form:
\begin{verbatim}
    cat "file"
\end{verbatim}
can be used to display the contents of the named file, whilst a list of
files can be printed one after the other using  an  expression  of  the
form:
\begin{verbatim}
    cat ["file1", "file2", ..., "filen"].
\end{verbatim}

\section{Dictionaries}
In order to understand some of the messages produced by Gofer, as  well
as  some  of  the  more  subtle  problems  associated  with  overloaded
functions, it is useful to have a  rough  idea  of  the  way  in  which
overloaded functions are implemented.

The basic idea is that a function with a qualified type \verb"context => type"
where \verb"context" is a non-empty list of predicates  is  implemented  by  a
function which takes an  extra  argument  for  each  predicate  in  the
context.  When the function is used, each of these parameters is filled
by a `dictionary'  which  gives  the  values  of  each  of  the  member
functions in the appropriate class.  None of these extra parameters  is
entered by the programmer.  Instead, they  are  inserted  automatically
during type-checking.

For the class \verb"Eq", each dictionary has at least two elements  containing
definitions for each of the functions \verb"(==)" and 
\verb"(/=)".  A dictionary  for
an instance of \verb"Eq" can be depicted by a diagram of the form:
\BQ
%                     +--------+--------+---------
%                     |        |        |
%                     |  (==)  |  (/=)  |  .....
%                     |        |        |
%                     +--------+--------+---------
\setlength{\unitlength}{1mm}
\begin{picture}(60,10)
\put(0,0){\line(1,0){60}}
\put(0,10){\line(1,0){60}}
\put(0,0){\line(0,1){10}}
\put(20,0){\line(0,1){10}}
\put(40,0){\line(0,1){10}}
\put(10,5){\makebox(0,0){{\tt (==)}}}
\put(30,5){\makebox(0,0){{\tt (/=)}}}
\put(50,5){\makebox(0,0){{\dots}}}
\end{picture}
\EQ
In order to produce useful error messages and indicate the way in which
dictionary expressions are being used, Gofer uses a number  of  special
notations for printing expressions involving dictionaries:
\BQ
\begin{tabular}{lp{10cm}}
   \verb"(#1" \I{d}\verb")" &
    selects the first element of the dictionary \I{d}\\
   \verb"(#2" \I{d}\verb")" &
    selects the second element of the dictionary \I{d}\\
   \verb"(#"\I{n} \I{d}\verb")" &
    selects the \I{n}th element of the dictionary \I{d}
    (note that \verb"(#0 d)" is equivalent to the dictionary \I{d}).\\
   \verb"{"\I{dict}\verb"}" &
    denotes a specific dictionary (the contents are not
    displayed).\\
   \verb"d"\I{nnn} &
    a dictionary variable representing an unknown dictionary is
    printed as a lower case letter `\verb"d"' followed by an  integer;
    e.g.\ \verb"d231".
\end{tabular}
\EQ
Note that, whilst these notations are used in output produced by  Gofer
and in the following explanations, they cannot be entered directly into
Gofer expressions or programs -- even if you use  a  variable  such  as
\verb"d1" in an expression, Gofer will not confuse this  with  a  dictionary
variable with the same name (although Gofer might confuse you by  using
the same name in two different contexts!).

Using these notations, the member functions \verb"(==)" and \verb"(/=)" 
of the  class
\verb"Eq" behave as if they were defined by the expressions:
\begin{verbatim}
    (==) d1  =  (#1 d1)
    (/=) d1  =  (#2 d1)
\end{verbatim}
To understand how these definitions work, we need to take a look  at  a
specific dictionary.  Following the original instance declaration  used
for \verb"Eq Int", the corresponding dictionary is:
\BQ
%                    d :: Eq Int
%                    +------------+------------+
%                    |            |            |
%                    | primEqInt  |  defNeq d  |
%                    |            |            |
%                    +------------+------------+
%
\setlength{\unitlength}{1mm}
\begin{picture}(60,14)
\put(0,0){\line(1,0){40}}
\put(0,10){\line(1,0){40}}
\put(0,0){\line(0,1){10}}
\put(20,0){\line(0,1){10}}
\put(40,0){\line(0,1){10}}
\put(10,5){\makebox(0,0){{\tt primEqInt}}}
\put(30,5){\makebox(0,0){{\tt defNeq d}}}
\put(0,11){\makebox(0,0)[bl]{{\tt d :: Eq Int}}}
\end{picture}
\EQ
Note that the  dictionary  variable  \verb"d"  is  used  as  a  name  for  the
dictionary in this diagram, indicating how values within  a  dictionary
can include references to the same dictionary.

It turns out that predicates  play  a  very  similar  role  for
dictionaries as types play for normal values.  This motivates  our  use
of the notation \verb"d :: Eq Int" to indicate that \verb"d" 
is a dictionary for  the
instance \verb"Eq Int".  One difference between these, particularly  important
for theoretical work, is that dictionary values are uniquely determined
by predicates; if \verb"d1::p" and \verb"d2::p" 
for some predicate \verb"p", then \verb"d1 = d2".

The value held in the first element of the dictionary is the  primitive
equality function on  integers,  \verb"primEqInt".   The  following  diagram
shows how the dictionary is used to evaluate the expression \verb"2 == 3".
Note that this expression will first be translated to  \verb"(==) d 2 3"  by
the type checker.  The evaluation then proceeds as follows:
\begin{verbatim}
    (==) d 2 3  ==>  (#1 d) 2 3
                ==>  primEqInt 2 3
                ==>  False
\end{verbatim}
The second element of the  dictionary  is  a  little  more  interesting
because it uses the default definition for \verb"(/=)" given in  the  original
class definition  which,  after  translation,  is  represented  by  the
function \verb"defNeq" defined by:
\begin{verbatim}
    defNeq d1 x y = not ((==) d1 x y)
\end{verbatim}
Notice the way in which the  extra  dictionary  parameter  is  used  to
obtain the appropriate overloading.  For  example,  evaluation  of  the
expression  \verb"2 /= 3", which  becomes  \verb"(/=) d 2 3"  
after  translation,
proceeds as follows:
\begin{verbatim}
    (/=) d 2 3  ==>  (#2 d) 2 3
                ==>  defNeq d 2 3
                ==>  not ((==) d 2 3)
                ==>  not ((#1 d) 2 3)
                ==>  not (primEqInt 2 3)
                ==>  not False
                ==>  True
\end{verbatim}
(Clearly there is some scope for optimisation here; whilst  the  actual
reduction sequences used by Gofer are equivalent to  those  illustrated
above, the precise details are a little different.)

If an  instance  is  obtained  from  an  instance  declaration  with  a
non-empty context, then the basic two element dictionary  used  in  the
examples above is extended with an  extra  dictionary  value  for  each
predicate in the context.  As an example, the diagram below  shows  the
dictionaries that will be created  from  the  instance  definitions  in
section 14.2.2  for  the  instance  \verb"Eq (Int, [Int])".   The  functions
\verb"eqPair" and \verb"eqList" 
which are used in these dictionaries are obtained
from the definitions of \verb"(==)" given in the 
instance declarations for  \verb"Eq"
\verb"(a,b)" and \verb"Eq [a]" respectively:
\begin{verbatim}
    eqPair d (x,y) (u,v)   = (==) (#3 d) x u && (==) (#4 d) y v

    eqList d [] []         = True
    eqList d []     (y:ys) = False
    eqList d (x:xs) []     = False
    eqList d (x:xs) (y:ys) = (==) (#3 d) x y && (==) d xs ys
\end{verbatim}
The dictionary structure for \verb"Eq (Int, [Int])" is as follows.  Note  that
the Gofer system ensures that there is at most  one  dictionary  for  a
particular instance of a class, and that the dictionary \verb"d1 :: Eq Int" in
this system is automatically shared between \verb"d2" and \verb"d3":
\BQ
\setlength{\unitlength}{1mm}
\begin{picture}(120,40)
\put(20,0){\line(1,0){40}}
\put(20,8){\line(1,0){40}}
\put(60,12){\line(1,0){60}}
\put(60,20){\line(1,0){60}}
\put(0,28){\line(1,0){80}}
\put(0,36){\line(1,0){80}}
\put(20,0){\line(0,1){8}}
\put(40,0){\line(0,1){8}}
\put(60,0){\line(0,1){8}}
\put(60,12){\line(0,1){8}}
\put(80,12){\line(0,1){8}}
\put(100,12){\line(0,1){8}}
\put(120,12){\line(0,1){8}}
\put(0,28){\line(0,1){8}}
\put(20,28){\line(0,1){8}}
\put(40,28){\line(0,1){8}}
\put(60,28){\line(0,1){8}}
\put(80,28){\line(0,1){8}}
\put(50,32){\vector(0,-1){24}}
\put(90,32){\vector(0,-1){12}}
\put(110,4){\vector(-1,0){48}}
\put(110,16){\line(0,-1){12}}
\put(70,32){\line(1,0){20}}
\put(50,32){\circle*{1.5}}
\put(70,32){\circle*{1.5}}
\put(110,16){\circle*{1.5}}
\put(30,4){\makebox(0,0){{\tt primEqInt}}}
\put(50,4){\makebox(0,0){{\tt defNeq d1}}}
\put(70,16){\makebox(0,0){{\tt eqList d2}}}
\put(90,16){\makebox(0,0){{\tt defNeq d2}}}
\put(10,32){\makebox(0,0){{\tt eqPair d3}}}
\put(30,32){\makebox(0,0){{\tt defNeq d3}}}
\put(20,9){\makebox(0,0)[bl]{{\tt d1::Eq Int}}}
\put(60,21){\makebox(0,0)[bl]{{\tt d2::Eq [Int]}}}
\put(0,37){\makebox(0,0)[bl]{{\tt d3::Eq (Int,[Int])}}}
\end{picture}
\EQ
%   d3 :: Eq (Int, [Int])
%   +------------+------------+------------+------------+
%   |            |            |            |            |
%   | eqPair d3  | defNeq d3  | d1::Eq Int |d2::Eq [Int]|
%   |            |            |            |            |
%   +------------+------------+-----+------+-----+------+
%                                   |            |
%                    +--------------+            |
%                    |                           |
%                    |        d2 :: Eq [Int]     V 
%                    |        +------------+------------+------------+
%                    |        |            |            |            |
%                    |        | eqList d2  | defNeq d2  | d1::Eq Int |
%                    |        |            |            |            |
%                    |        +------------+------------+-----+------+
%                    |                                        |
%       d1 :: Eq Int V                                        |
%       +------------+------------+                           |
%       |            |            |                           |
%       | primEqInt  | defNeq d1  |<--------------------------+
%       |            |            |
%       +------------+------------+
%
Once again, it may be useful to see how these definitions are  used  to
evaluate  the  expression   \verb"(2,[1]) == (2,[1,3])"   which,   after
translation, becomes \verb"(==) d3 (2,[1]) (2,[1,3])":
\begin{verbatim}
    (==) d3 (2,[1]) (2,[1,3])
             ==>   (#1 d3) (2,[1]) (2,[1,3])
             ==>   eqPair d3 (2,[1]) (2,[1,3])
             ==>   (==) (#3 d3) 2 2  &&  (==) (#4 d3) [1] [1,3]
             ==>   (==) d1 2 2       &&  (==) (#4 d3) [1] [1,3]
             ==>   (#1 d1) 2 2       &&  (==) (#4 d3) [1] [1,3]
             ==>   primEqInt 2 2     &&  (==) (#4 d3) [1] [1,3]
             ==>   True              &&  (==) (#4 d3) [1] [1,3]
             ==>   (==) (#4 d3) [1] [1,3]
             ==>   (==) d2 [1] [1,3]
             ==>   (#1 d2) [1] [1,3]
             ==>   eqList d2 [1] [1,3]
             ==>   (==) (#3 d2) 1 1  &&  (==) d2 [] [3]
             ==>   (==) d1 1 1       &&  (==) d2 [] [3]
             ==>   (#1 d1) 1 1       &&  (==) d2 [] [3]
             ==>   primEqInt 1 1     &&  (==) d2 [] [3]
             ==>   True              &&  (==) d2 [] [3]
             ==>   (==) d2 [] [3]
             ==>   False
\end{verbatim}

\subsection{Superclasses}
In general, a type class declaration has the form:
\BQ
     \verb"class" \I{context} \verb"=>" 
             \I{Class} $a_1$ \dots $a_n$ \verb"where" \\
      {\em type declarations for member functions} \\
      {\em default definitions of member functions}
\EQ
where \I{Class} is the name of the new type class which takes $n$  arguments,
represented by distinct type variables $a_1$, \dots, $a_n$.  As in the case  of
instance declarations, the context that appears on the left  hand  side
of the `\verb"=>"'  symbol  specifies  a  list  of  predicates  that  must  be
satisfied in order to construct any instance of \I{Class}.

The predicates in the \I{context} part of a class  declaration  are  called
the superclasses of \I{Class}.  This  terminology  is  taken  from  Haskell
where all classes have a single parameter and each of the predicates in
the context part of a class declaration has the  form  $C\; a_1$;  in  this
situation, any instance of \I{Class} must also be an instance of each class
$C$ named in the context.   
In  other  words,  each  such  $C$  contains  a
superset of the types in \I{Class}.

As an example of a class declaration with a non-empty context, consider
the following declaration from the standard prelude which introduces  a
class Ord whose instances are types  with  both  strict  \verb"(<)",  
\verb"(>)"  and
non-strict  \verb"(<=)",  \verb"(>=)"  
versions  of  an  ordering  defined  on  their
elements:
\begin{verbatim}
    class Eq a => Ord a where
        (<), (<=), (>), (>=) :: a -> a -> Bool
        max, min             :: a -> a -> a

        x <  y            = x <= y && x /= y
        x >= y            = y <= x
        x >  y            = y < x

        max x y | x >= y  = x
                | y >= x  = y
        min x y | x <= y  = x
                | y <= x  = y
\end{verbatim}
Notice that this definition provides default definitions for all of the
member functions except \verb"(<=)", so  that  in  general  only  this  single
function needs to be defined to construct an instance of class \verb"Ord".

There are two reasons for defining \verb"Eq" as a superclass of \verb"Ord":
\BI
\IT  The default definition for \verb"(<)" relies on the  use  of  
     \verb"(/=)"  taken
     from class \verb"Eq".  In order to guarantee that this is always valid we
     must ensure that every instance of \verb"Ord" must also be an instance of
     \verb"Eq".

\IT  Given the definition of a non-strict ordering \verb"(<=)" on the elements
     of a type, it is always possible to construct a definition for the
     \verb"(==)" operator (and hence for \verb"(/=)") using the equation:
\begin{verbatim}
    x==y   =   x<=y && y<=x
\end{verbatim}
     There will therefore be no loss in generality by requiring  \verb"Eq"  to
     be a superclass of \verb"Ord", and conversely, no difficulty in  defining
     an instance of \verb"Eq" to accompany any instance of \verb"Ord" 
     for which an
     instance of \verb"Eq" has not already be provided.

     As an example, the following definitions  provide  an  alternative
     way to implement the equality operation on  elements  of  the  \verb"Set"
     datatype described in section  14.2.3,  in  terms  of  the  subset
     ordering defined in class \verb"Ord":
\begin{verbatim}
    instance Ord (Set a) => Eq (Set a) where
        x == y   =   x <= y  &&  y <= x

    instance Eq a => Ord (Set a) where
        Set xs <= Set ys  =  all (`elem` ys) xs
\end{verbatim}
     This definition is in fact no less efficient or effective than the
     original version.
\EI
Dictionaries for superclasses are dealt with in much the  same  way  as
the instance specific dictionaries described above.  For  example,  the
general layout of a dictionary for an instance of \verb"Ord" is illustrated in
the following diagram:
\BQ
\begin{tabular}{|c|c|c|c|c|c|c|c}
\hline
 \verb"(<)"   &  \verb"(<=)"  &  \verb"(>)"   &  \verb"(>=)"  &
 \verb"max"   &  \verb"min"   &  \verb"Eq a"  & \dots \\
\hline
\end{tabular}
\EQ
% +--------+--------+--------+--------+--------+--------+--------+-----
% |        |        |        |        |        |        |        |
% |  (<)   |  (<=)  |  (>)   |  (>=)  |  max   |  min   |  Eq a  | .....
% |        |        |        |        |        |        |        |
% +--------+--------+--------+--------+--------+--------+--------+-----
%
Note the use of the seventh element of this dictionary which points  to
the dictionary for the appropriate instance of \verb"Eq".  This is used in the
translation of the default definition for \verb"(<)" which is equivalent to:
\begin{verbatim}
    defLessThan d x y  =  (<=) d x y  &&  (/=) (#7 d) x y
\end{verbatim}

\subsection{Combining classes}
In general, a dictionary is made up of three separate parts:
\BQ
\begin{tabular}{|c|c|c|}
\hline
Implementation   & Superclass   & Instance specific \\
of class members & dictionaries & dictionaries \\
\hline
\end{tabular}
\EQ
%     +-------------------+-------------------+-------------------+
%     |  Implementation   |    Superclass     | Instance specific |
%     | of class members  |   Dictionaries    |   Dictionaries    |
%     |                   |                   |                   |
%     +-------------------+-------------------+-------------------+
%
Each of these may be empty.  We have already  seen  examples  in  which
there are no superclass dictionaries (e.g.\ instances  of  \verb"Eq")  and  in
which there are no  instance  specific  dictionaries  (e.g.\  \verb"Eq Int").
Classes with no member functions (corresponding to dictionaries with no
member functions) are sometimes useful as a convenient abbreviation for
a list of predicates.  For example:
\begin{verbatim}
    class  C a  where  cee :: a -> a
    class  D a  where  dee :: a -> a

    class  (C a, D a) => CandD a
\end{verbatim}
makes \verb"CandD" a an abbreviation for the context \verb"(C a, D a)".  
Thinking  of
single parameter type classes as sets of types, the  type  class  \verb"CandD"
corresponds to the intersection of classes \verb"C" and \verb"D".

Just as the type inferred  for  a  particular  function  definition  or
expression does not involve type synonyms unless explicit type signatures
are used, the Gofer type system will not use a single predicate of  the
form \verb"CandD" a instead of the two predicates \verb"C a" 
and \verb"D a" unless  explicit
signatures are used:
\begin{verbatim}
    ? :t dee . cee
    \d129 d130 -> dee d130 . cee d129 :: (C a, D a) => a -> a
    ? :t dee . cee :: CandD a => a -> a
    \d129 -> dee (#2 d129) . cee (#1 d129) :: CandD a => a -> a
    ?
\end{verbatim}
In Haskell, all instances of  a  class  such  as  \verb"CandD"  must  have
explicit declarations, in addition to  the  corresponding  declarations
for instances for \verb"C" and \verb"D".  
This problem can be avoided  by  using  the
more general form of instance declaration permitted in Gofer; a  single
instance declaration:
\begin{verbatim}
    instance CandD a
\end{verbatim}
is all that is required to ensure that any instance  of  \verb"CandD"  can  be
obtained, so long as corresponding instances for \verb"C" and \verb"D" 
can be found.


\subsection{Simplified contexts}
Consider the function defined by the following equation:
\begin{verbatim}
    eg1 x   =   [x] == [x]  ||  x == x
\end{verbatim}
This definition does not restrict the type of \verb"x" in any way except that,
if \verb"x::a", then there must be instances \verb"Eq [a]" 
and \verb"Eq a" which are  used
for the two occurrences of the \verb"(==)" operator in the equation.  We might
therefore expect the type of \verb"eg1" to be:
\begin{verbatim}
    (Eq [a], Eq a) => a -> Bool
\end{verbatim}
with translation:
\begin{verbatim}
    eg1 d1 d2 x  =  (==) d1 [x] [x]  ||  (==) d2 x x
\end{verbatim}
However, as can be seen  from  the  case  where  \verb"a=Int"  illustrated  in
section 14.3, given \verb"d1::Eq [a]" 
we can always find a dictionary for \verb"Eq a"
by taking the third element of \verb"d1" i.e.\ 
\verb"(#3 d1)::Eq a".  Since it is more
efficient to select an element from a  dictionary  than  to  complicate
both type and translation with extra parameters, the type  assigned  to
\verb"eg1" by default is:
\begin{verbatim}
    Eq [a] => a -> Bool
\end{verbatim}
with translation:
\begin{verbatim}
    eg1 d1 x  =  (==) d1 [x] [x]  || (==) (#3 d1) x x
\end{verbatim}
In general, given a set of predicates corresponding  to  the  instances
required by an expression,  Gofer  will  always  attempt  to  find  the
smallest possible subset of these  predicates  such  that  all  of  the
required dictionaries can still  be  obtained,  whilst  minimising  the
number of dictionary parameters that are used.

The original type and translation for eg1 given above can  be  produced
by including an explicit type signature  in  the  file  containing  the
definition of \verb"eg1":
\begin{verbatim}
    eg1   ::  (Eq [a], Eq a) => a -> Bool
    eg1 x  =  [x] == [x]  ||  x == x
\end{verbatim}
But even with this definition, Gofer will still always try to  minimise
the number of dictionaries used in any particular expression:
\begin{verbatim}
    ? :t eg1
    \d153 -> eg1 d153 (#3 d153) :: Eq [a] => a -> Bool
    ?
\end{verbatim}
As another example, consider the expression \verb"(\x y->  x==x  ||  y==y)".
The type and translation assigned to this term can  be  found  directly
using Gofer:
\begin{verbatim} 
    ? :t (\x y-> x==x || y==y)
    \d121 d122 x y -> (==) d122 x x ||
                      (==) d121 y y
                  :: (Eq b, Eq a) => a -> b -> Bool
    ?
\end{verbatim}
Note that the translation has two dictionary parameters \verb"d121"  
and  \verb"d122"
corresponding to the two predicates \verb"Eq a" 
and \verb"Eq b" respectively.   Since
both of these dictionaries can be obtained from a  dictionary  for  the
predicate \verb"Eq (a,b)", we can use an explicit type signature to produce  a
translation which needs only one dictionary parameter:
\begin{verbatim}
    ? :t (\x y-> x==x || y==y) :: Eq (a,b) => a -> b -> Bool
    \d121 x y -> (==) (#3 d121) x x ||
                 (==) (#4 d121) y y
             :: Eq (a,b) => a -> b -> Bool
    ?
\end{verbatim}

\section{Other issues}

\subsection{Unresolved overloading}
Consider  the  use  of  the  \verb"(==)"  operator  in  the  following   three
situations:
\BI
\IT  In the expression \verb"2==3", it is clear that the appropriate value
     for the equality operator in this case is \verb"primIntEq" as defined  by
     the instance declaration for \verb"Eq Int".  The expression can therefore
     be translated to \verb"primEqInt 2 3".

\IT  In the function definition \verb"f x = x==x",  we  cannot  completely
     determine the appropriate value for \verb"(==)" because it depends on the
     type assigned to the variable \verb"x",  which  may  itself  vary  with
     different uses of the function \verb"f".  It is however possible to add
     an extra parameter to the definition,  giving \verb"f d x = (==) d x x"
     and taking the type of \verb"f" to be \verb"Eq a => a -> Bool".

     In this way, the problem of finding the appropriate definition for
     the \verb"(==)" operator is deferred until the function is actually used.

\IT  In the expression \verb"[]==[]", 
     the appropriate value for \verb"(==)" must be
     obtained from the dictionary for some instance of the form \verb"Eq [a]",
     but there is not  sufficient  information  in  the  expression  to
     determine what the value of the type variable a should be.

     Looking back to the instance declaration for \verb"Eq [a]", we find  that
     the definition of \verb"(==)" depends on the value of the dictionary  for
     the instance \verb"Eq a".  In this particular case, it is clear that  the
     expression will always evaluate to \verb"True", regardless of  the  value
     of this dictionary.  Unfortunately, the only way that this can  be
     detected is by evaluating the expression to see if the calculation
     can be completed without reference to the  dictionary  value  (see
     the comments in the aside at the end of this section).

     Attempting to evaluate this expression  in  Gofer  will  therefore
     result in an error message indicating that the expression does not
     contain sufficient information to resolve the use  of  overloading
     in the expression:
\begin{verbatim}
    ? [] == []
    ERROR: Unresolved overloading
    *** type        : Eq [a] => Bool
    *** translation : \d129 -> (==) d129 [] []
    ?
\end{verbatim}
     Note  that  the  expression  has  been  converted  into  a  lambda
     expression using the dictionary variable  \verb"d129"  to  represent  the
     dictionary for the unknown instance \verb"Eq [a]".

     One simple way to resolve the overloading in an expression of this
     kind is to use an explicit type signature.   For  example,  if  we
     specify that the second empty list is an empty list of type \verb"[Int]":
\begin{verbatim}
    ? [] == ([]::[Int])
    True
    (2 reductions, 9 cells)
    ?
\end{verbatim}
\EI
The same problem occurs in Haskell, where it  is  described  using  the
idea of an `ambiguous type' -- i.e.\  a  type  expression  of  the  form
\verb"context => type" where one or more of the type  variables  appearing  in
the given context do not appear in  the  remaining  part  of  the  type
expression.

Further examples of unresolved overloading occur  with  other  classes.
As an example consider the class \verb"Reader" defined by:
\begin{verbatim}
    class Reader a where 
        parse   :: String -> a   
        unparse :: a -> String
\end{verbatim}
whose  member  functions  provide  methods  for  obtaining  the  string
representation of an element of an instance type,  and  for  converting
such representations  back  into  the  original  values.  (The  standard
Haskell \verb"Text" class  contains  similar  functions.)   Now  consider  the
expression \verb"parse . unparse" which maps values from  some  instance  of
Reader to  values  of  another  instance  via  an  intermediate  string
representation.
\begin{verbatim}
    ? parse . unparse
    ERROR: Unresolved overloading
    *** type        : (Reader a, Reader b) => a -> b
    *** translation : \d129 d130 -> parse d130 . unparse d129
    ?
\end{verbatim}
One of the first things that might surprise the reader here is that the
value produced by \verb"parse.unparse" does not have to  be  of  the  same
type as the argument; for example, we would not usually expect to  have
any sensible interpretation for a floating point number  obtained  from
the string representation of a boolean value!

This can be fixed by using an explicit type declaration,  although  the
expression still produces unresolved overloading:
\begin{verbatim}
    ? (parse . unparse) :: Reader a => a -> a
    ERROR: Unresolved overloading
    *** type        : Reader a => a -> a
    *** translation : \d130 -> parse d130 . unparse d130
    ?
\end{verbatim}
Notice however that the type of this expression  is  not  ambiguous  so
that the unresolved overloading in this example can be eliminated  when
the function is actually used:
\begin{verbatim}
    ? ((parse . unparse) :: Reader a => a -> a) 'a'
    'a'
    (4 reductions, 11 cells)
    ?
\end{verbatim}
A more serious problem occurs with the  expression  \verb"unparse . parse"
which maps string values to string values via some  intermediate  type.
Clearly this will lead to a problem with unresolved overloading:
\begin{verbatim}
    ? unparse . parse
    ERROR: Unresolved overloading
    *** type        : Reader a => String -> String
    *** translation : \d130 -> unparse d130 . parse (#0 d130)
    ?
\end{verbatim}
Notice that the type obtained in  this  case  is  ambiguous;  the  type
variable \verb"a" which appears in the predicate 
\verb"Reader" a does not  appear  in
the type \verb"String -> String".  There are a number  of  ways  of  resolving
this kind of ambiguity:
\BI
\IT  Using an explicitly typed expression: Assuming  for  example  that
     \verb"Char" is an instance of \verb"Reader", we can write:
\begin{verbatim}
    ? unparse . (parse :: String -> Char)
    v113 {dict} . v112 {dict}
    (5 reductions, 42 cells)
    ?
\end{verbatim}
     without any ambiguity.  If such type  signatures  are  used  in  a
     number of places, it  might  be  better  to  define  an  auxiliary
     function and use that instead:
\begin{verbatim}
    charParse :: String -> Char
    charParse  = parse

    ? unparse . charParse
    v113 {dict} . charParse
    (4 reductions, 37 cells)
    ?
\end{verbatim}
     In such situations, it  is  perhaps  worth  asking  if  overloaded
     functions are in  fact  the  most  appropriate  solution  for  the
     problem at hand!

\IT  Using an extra dummy parameter in a  function  definition.   In  a
     definition such as:
\begin{verbatim}
    f = unparse . parse
\end{verbatim}
     we can introduce an additional dummy parameter \verb"x"  which  is  not
     used except to determine the type of the result produced by  parse
     in \verb"f":
\begin{verbatim}
    f x  =  unparse . (parse `asTypeOf` (\""->x))
\end{verbatim}
     where the standard prelude operator \verb"asTypeOf" defined by:
\begin{verbatim}
    asTypeOf      :: a -> a -> a
    x `asTypeOf` _ = x
\end{verbatim}
     is used to ensure that the type of parse in the definition of \verb"f" is
     the same as that of the function \verb'(\""->x)' -- in other  words,  the
     type must be \verb"String -> a" where 
     \verb"a" is the type of the variable \verb"x".

     The resulting type for \verb"f" is:
\begin{verbatim}
    f :: Reader a => a -> String -> String
\end{verbatim}
     Notice how the addition of the dummy parameter has  been  used  to
     eliminate the ambiguity present in the original type.

     This kind of `coding trick' is rather messy and is not recommended
     for anything but the simplest examples.
\EI
The idea of evaluating an expression with an ambiguous type  to
see if it does actually need the unspecified  dictionaries  could  have
been implemented quite easily in Gofer using an otherwise unused
datatype \verb"Unresolved" and generating instance declarations such as:
\begin{verbatim}
    instance Eq Unresolved where
        (==) = error "unresolved overloading for (==)"
        (/=) = error "unresolved overloading for (/=)"
\end{verbatim}
for each class.  Given a particular expression, we  can  then  use  the
type \verb"Unresolved" 
in place of any ambiguous type variables in its type.   The
evaluation of the expression could then be attempted, either completing
successfully if  the  dictionaries  are  not  required,  but  otherwise
resulting in a run-time error.

This approach is not used in Gofer; instead, the programmer is notified
of any unresolved  polymorphism  when  the  program  is  type  checked,
avoiding the possibility that a program  might  contain  an  undetected
ambiguity.


\subsection{`Recursive' dictionaries}
Unlike Haskell, there are no restrictions on the form of the predicates
that may appear in the context  part  of  a  Gofer  class  or  instance
declaration.  This has a  number  of  potentially  useful  applications
because it enables the  Gofer  programs  to  use  mutually  `recursive'
systems of dictionaries.

One example of this is the ability  to  implement  a  large  family  of
related functions using a group of classes instead of having to  use  a
single class.  The following example illustrates the technique with  an
alternative definition for the class \verb"Eq" 
in  which  the  \verb"(==)"  and  \verb"(/=)"
operators are placed in different classes:
\begin{verbatim}
    class Neq a => Eq a  where  (==) :: a -> a -> Bool

    class Eq a => Neq a  where  (/=) :: a -> a -> Bool
                                x/=y  = not (x == y)
\end{verbatim}
(These declarations clash with those in the standard prelude and
hence cannot actually be used in Gofer unless a modified version of the
standard prelude is used instead.)

If we then give instance declarations:
\begin{verbatim}
    instance Eq Int  where (==) = primEqInt
    instance Neq Int
\end{verbatim}
and try to evaluate the expression \verb"2==3" then the following system  of
dictionaries will be generated:
\BQ
\setlength{\unitlength}{1mm}
\begin{picture}(130,18)
\put(20,4){\line(1,0){40}}
\put(20,12){\line(1,0){40}}
\put(80,4){\line(1,0){40}}
\put(80,12){\line(1,0){40}}
\put(20,4){\line(0,1){8}}
\put(40,4){\line(0,1){8}}
\put(60,4){\line(0,1){8}}
\put(80,4){\line(0,1){8}}
\put(100,4){\line(0,1){8}}
\put(120,4){\line(0,1){8}}

\put(10,8){\vector(1,0){10}}
\put(50,8){\vector(1,0){30}}
\put(110,8){\line(1,0){20}}
\put(130,8){\line(0,-1){8}}
\put(130,0){\line(-1,0){120}}
\put(10,0){\line(0,1){8}}
\put(50,8){\circle*{1.5}}
\put(110,8){\circle*{1.5}}

\put(30,8){\makebox(0,0){{\tt primEqInt}}}
\put(90,8){\makebox(0,0){{\tt defNeq d2}}}
\put(20,13){\makebox(0,0)[bl]{{\tt d1: Eq Int}}}
\put(80,13){\makebox(0,0)[bl]{{\tt d2: Neq Int}}}
\end{picture}
\EQ
%
%        d1 :: Eq Int                   d2 :: Neq Int
%        +-----------+-----------+      +-----------+-----------+
%        |           |           |      |           |           |
%    +-->| primEqInt |d2::Neq Int+----->| defNeq d2 |d1::Eq Int +---+
%    |   |           |           |      |           |           |   |
%    |   +-----------+-----------+      +-----------+-----------+   |
%    |                                                              |
%    +------------------------------<-------------------------------+
%
where the function \verb"defNeq" is derived from the default definition in the
class \verb"Neq" and is equivalent to:
\begin{verbatim}
               defNeq d x y  =  not ((==) (#2 d) x y)
\end{verbatim}
Incidentally, if the instance declaration for \verb"Neq Int" above  had  been
replaced by:
\begin{verbatim}
    instance Neq a
\end{verbatim}
then the effect of these declarations would be similar to the  standard
definition of the class \verb"Eq", except that it would  not  be  possible  to
override the  default  definition  for  \verb"(/=)".   In  other  words,  this
approach would give the same effect as defining  \verb"(/=)"  as  a  top-level
function rather than a member function in the class \verb"Eq":
\begin{verbatim}
    class Eq a  where  (==) :: a -> a -> Bool

    (/=)   ::  Eq a => a -> a -> Bool
    x /= y  =  not (x == y)
\end{verbatim}
There are other situations in which recursive dictionaries of the  kind
described above can be  used.   A  further  example  is  given  in  the
following section.  Unfortunately, the lack of restrictions on the form
of class and instance declarations can also lead to  problems  in  some
(mostly pathological) cases.  As an example, consider the class:
\begin{verbatim}
    class Bad [a] => Bad a  where  bad :: a -> a
\end{verbatim}
Without defining any instances of \verb"Bad", it is not possible to  construct
any dictionaries for instances of \verb"Bad":
\begin{verbatim}
    ? bad 2
    ERROR: Cannot derive instance in expression
    *** Expression        : bad d126 2
    *** Required instance : Bad Int
    ?
\end{verbatim}
If however we add the instance declarations:
\begin{verbatim}
    instance Bad Int where bad = id
    instance Bad [a] where bad = id
\end{verbatim}
then any attempt to construct  a  dictionary  for  \verb"Bad Int"  will  also
require a dictionary for the superclass \verb"Bad [Int]"  and  then  for  the
superclass of that instance \verb"Bad [[Int]]" etc.  Since Gofer has only  a
finite amount of space for  storing  dictionaries,  this  process  will
eventually terminate when that space has been used up:
\begin{verbatim}
    ? bad 2
    ERROR: Dictionary storage space exhausted
    ?
\end{verbatim}
(Depending on the configuration of your  particular  version  of
Gofer and on the nature of the class and instance declarations that are
involved, an alternative error message {\tt ERROR: Too many type  variables
in type checker} may be produced instead of the message shown above.)

From a practical point of view, this problem is unlikely to  cause  too
many real difficulties:
\BI
\IT  Class declarations involving  predicates  such  as  those  in  the
     declaration of \verb"Bad" are unlikely to be used in realistic programs.

\IT  All dictionaries are constructed before evaluation  begins.   This
     process is guaranteed to terminate  because  each  new  dictionary
     that is created uses up part of  the  space  used  to  hold  Gofer
     dictionaries.  The  construction  process  will  either  terminate
     successfully once complete, or be aborted as soon as  all  of  the
     dictionary space has been used.
\EI
It remains to see what impact (if any) this has on realistic  programs,
and if later versions of  Gofer  should  be  modified  to  impose  some
syntactic restrictions (as in Haskell) or perhaps some form  of  static
checking of the contexts appearing in class and instance  declarations.


\subsection{Classes with multiple parameters}
Gofer is the first language to support the use  of  type  classes  with
multiple parameters.  This again is  an  experimental  feature  of  the
language, intended to make it possible to explore  the  claims  from  a
number of researchers about the use of such classes.

Initial experiments suggest that multiple parameter  type  classes  are
likely  to  lead  to  large  numbers  of   problems   with   unresolved
overloading.  Ultimately, this may mean that such classes are  only  of
practical use in explicitly typed languages, or  alternatively  that  a
more powerful and general defaulting mechanism (similar to that used in
Haskell with numeric classes) is required to  support  user  controlled
overloading resolution.

The following declaration introduces a class  \verb"Iso"  whose  elements  are
pairs of isomorphic types:
\begin{verbatim}
    class Iso b a => Iso a b  where  iso :: a -> b
\end{verbatim}
The single member function \verb"iso"  represents  the  isomorphism  mapping
elements of type \verb"a" 
to corresponding  elements  of  type  \verb"b".   Note  the
`superclass' context in this declaration which formalises the idea that
if \verb"a" is isomorphic to \verb"b" 
then \verb"b" is also isomorphic to \verb"a".  The class  \verb"Iso"
therefore provides  further  examples  of  the  recursive  dictionaries
described in the previous section.

The fact that any type is isomorphic to itself can be described by the
following instance declaration:
\begin{verbatim}
    instance Iso a a where iso x = x
\end{verbatim}
For example, the dictionary structure created in order to evaluate the
expression \verb"iso 2 == 3" is:
\BQ
\setlength{\unitlength}{1mm}
\begin{picture}(70,18)
\put(20,4){\line(1,0){40}}
\put(20,12){\line(1,0){40}}
\put(20,4){\line(0,1){8}}
\put(40,4){\line(0,1){8}}
\put(60,4){\line(0,1){8}}

\put(10,8){\vector(1,0){10}}
\put(50,8){\line(1,0){20}}
\put(70,8){\line(0,-1){8}}
\put(70,0){\line(-1,0){60}}
\put(10,0){\line(0,1){8}}
\put(50,8){\circle*{1.5}}

\put(30,8){\makebox(0,0){{\tt id}}}
\put(20,13){\makebox(0,0)[bl]{{\tt d:: Iso Int Int}}}
\end{picture}
\EQ
%
%                   d :: Iso Int Int
%                   +--------------+--------------+
%                   |              |              |
%               +-->|      id      |d::Iso Int Int+--+
%               |   |              |              |  |
%               |   +--------------+--------------+  |
%               |                                    |
%               +------------------<-----------------+
%
\begin{verbatim}
    ? iso 2 == 3
    False
    (4 reductions, 11 cells)
    ? 
\end{verbatim}
Our first taste of the problems to come occurs when we try to  evaluate
the expression \verb"iso 2 == iso 3":
\begin{verbatim}
    ? iso 2 == iso 3
    ERROR: Unresolved overloading
    *** type        : (Eq a, Iso Int a) => Bool
    *** translation : \d130 d132 -> (==) d130 (iso d132 2) (iso d132 3)
    ?
\end{verbatim}
In this case, the \verb"iso" function is used to map the integers 2 and 3 to
elements of some type \verb"a", 
isomorphic to \verb"Int", and the values produced are
the compared using \verb"(==)" at the instance  \verb"Eq a";  
there  is  no  way  of
discovering what the value of a should be  without  using  an  explicit
type signature.

Further instances can be defined.  The following two  declarations  are
needed to describe the (approximate) isomorphism between lists of pairs
and pairs of lists:
\begin{verbatim}
    instance Iso [(a,b)] ([a],[b]) where  
        iso xs = (map fst xs, map snd xs)
 
    instance Iso ([a],[b]) [(a,b)] where
        iso (xs,ys) = zip xs ys
\end{verbatim}
Unfortunately, even apparently straightforward examples  give  problems
with  unresolved  overloading,  forcing  the  use  of   explicit   type
declarations:
\begin{verbatim}
    ? iso [(1,2),(3,4)]
    ERROR: Unresolved overloading
    *** type        : Iso [(Int,Int)] a => a
    *** translation : \d126 -> iso d126 [(1,2),(3,4)]

    ? (iso [(1,2),(3,4)]) :: ([Int],[Int])
    ([1, 3],[2, 4])
    (22 reductions, 64 cells)
    ?
\end{verbatim}
A second example of a multiple  parameter  type  class  is  defined  as
follows:
\begin{verbatim}
    class Ord a => Collects a b where
        emptyCollection :: b
        addToCollection :: a -> b -> b
        listCollection  :: b -> [a]
\end{verbatim}
The basic intuition is that the predicate \verb"Collects a b"  indicates  that
elements of type \verb"b" can be used to represent collections of elements  of
type \verb"a".  A number of people have suggested using type classes  in  this
way to provide features similar to the (similarly named, but  otherwise
different) classes that occur in object-oriented languages.

Obvious implementations involve the use  of  ordered  lists  or  binary
search trees defined by instances of the form:
\begin{verbatim}
    data STree a = Empty | Node a (STree a) (STree a)
 
    instance Collects a [a] where ....
    instance Collects a (STree a) where ....
\end{verbatim}
Once again, there are significant problems even  with  simple  examples
using these functions.  As an example, the standard way of  defining  a
function of type:
\begin{verbatim}
                  Collects a b => [a] -> b
\end{verbatim}
mapping a list of values to a collection  of  those  values  using  the
higher order function \verb"foldr":
\begin{verbatim}
    listToCollection = foldr addToCollection emptyCollection
\end{verbatim}
actually produces a function with ambiguous type:
\begin{verbatim}
    ? :t foldr addToCollection emptyCollection
    \d139 d140 -> foldr (addToCollection d140) (emptyCollection d139)
              :: (Collects c b, Collects a b) => [a] -> b
    ?
\end{verbatim}
which cannot be resolved, even with an explicit type declaration.

\subsection{Overloading and numeric values}
One of the most common uses of overloading is to allow the use  of  the
standard arithmetic operators such as \verb"(+)", \verb"(*)" 
etc.\ on the elements  of
a range of numeric types including integers and floating point  values  in
addition to user defined numeric  types  such  as  arbitrary  precision
integers,  complex  and  rational  numbers,   vectors   and   matrices,
polynomials etc.  In Haskell, these features are supported by a  number
of built-in types and a complex hierarchy of  type  classes  describing
the operations defined on the elements of each numeric type.

As an experimental language, intended primarily for  the  investigation
of general purpose overloading, Gofer has  only  two  built-in  numeric
types; \verb"Int" and \verb"Float" 
(the second of  which  is  not  supported  in  all
implementations).  Similarly, although the Gofer system could  be  used
to implement the  fully  hierarchy  of  Haskell  numeric  classes,  the
standard prelude uses a single numeric type class Num defined by:
\begin{verbatim}
    class Eq a => Num a where           -- simplified numeric class
        (+), (-), (*), (/) :: a -> a -> a
        negate             :: a -> a
        fromInteger        :: Int -> a
\end{verbatim}
The first four member functions \verb"(+)", \verb"(-)", 
\verb"(*)",  \verb"(/)"  are  the  standard
arithmetic functions on instances of \verb"Num", whilst 
\verb"negate" denotes unary
negation.  The final member function, fromInteger is used to coerce any
integer value to the corresponding value in another  instance  of  \verb"Num".
An expression such as \verb"fromInteger 3" is called an  overloaded  numeric
constant and has type \verb"Num a => a" indicating that it can be  used  as  a
value of any instance of \verb"Num".  See below for examples.

Both \verb"Float" and \verb"Int" are defined as  
instances  of  \verb"Num"  using  primitive
functions for integer and floating point arithmetic:
\begin{verbatim}
    instance Num Int where
        (+)           = primPlusInt
        (-)           = primMinusInt
        (*)           = primMulInt
        (/)           = primDivInt
        negate        = primNegInt
        fromInteger x = x

    instance Num Float where
        (+)         = primPlusFloat
        (-)         = primMinusFloat
        (*)         = primMulFloat
        (/)         = primDivFloat 
        negate      = primNegFloat
        fromInteger = primIntToFloat
\end{verbatim}
These definitions make it  possible  to  evaluate  numeric  expressions
involving both types:
\begin{verbatim}
    ? 2 + 3
    5
    (3 reductions, 6 cells)
    ? 3.2 + 4.321
    7.521
    (3 reductions, 13 cells)
    ?
\end{verbatim}
Note  however  that  any  attempt  to  evaluate  an  expression  mixing
different arithmetic types is likely to cause a type error:
\begin{verbatim}
    ? 4.2 * 4
    ERROR: Type error in application
    *** expression     : 4.2 * 4
    *** term           : 4.2
    *** type           : Float
    *** does not match : Int
    ?
\end{verbatim}
Further problems occur when we try to define functions intended  to  be
used with arbitrary instances  of  \verb"Num"  rather  than  specific  numeric
types.  As an example of this, the  standard  prelude  function  \verb"sum",
roughly equivalent to:
\begin{verbatim}
    sum []     = 0
    sum (x:xs) = x + sum xs
\end{verbatim}
has type \verb"[Int] -> Int",  
rather than the  more general \verb"Num a => [a] -> a"
which could be used to find the sum of a list of numeric values in  any
instance of \verb"Num".  The problem in this particular case is caused by the
integer constant 0 in the first line of the definition.  Replacing this
with the expression fromInteger 0 leads to the following definition for
a generic sum function of the required type:
\begin{verbatim}
    genericSum       :: Num a => [a] -> a
    genericSum []     = fromInteger 0
    genericSum (x:xs) = x + genericSum xs
\end{verbatim}
For example:
\begin{verbatim}
    ? genericSum [1,2,3]
    6
    (10 reductions, 18 cells)
    ? genericSum [1.0,2.0,3.0]
    6.0
    (11 reductions, 27 cells)
    ?
\end{verbatim}
The \verb"fromInteger" function  can  also  be  used  to  solve  the  previous
problem:
\begin{verbatim}
    ? 4.2 * fromInteger 4
    16.8
    (3 reductions, 13 cells)
    ?
\end{verbatim}
In Haskell, any integer  constant  \verb"k"  appearing  in  an  expression  is
treated as if the programmer had actually written  \verb"fromInteger k"  so
that  both  of  the  preceding  problems  are  automatically  resolved.
Unfortunately, this  also  creates  some  new  problems;  applying  the
function fromInteger to each  integer  constant  in  the previous  examples
causes problems with unresolved overloading:
\begin{verbatim}
    ? fromInteger 2 + fromInteger 3
    ERROR: Unresolved overloading
    *** type        : Num a => a
    *** translation : \d143 -> (+) d143 (fromInteger d143 2)
                                        (fromInteger d143 3)
    ?
\end{verbatim}
Once again, Haskell provides a solution to this problem in the form  of
a `default mechanism' for  numeric  types  which,  once  the  following
problem has been detected, will typically `default'  the  unknown  type
represented by the type variable a above to be Int, so that the  result
is actually equivalent to the following:
\begin{verbatim}
    ? (fromInteger 2 + fromInteger 3) :: Int
    5
    (4 reductions, 8 cells)
    ?
\end{verbatim}
There are a number of problems with the Haskell default mechanism; both
theoretical and practical.  In addition, if a default mechanism of some
form is used then it should also be capable of dealing  with  arbitrary
user-defined type classes, rather than  a  small  group  of  `standard'
classes, in order to provide solutions to  the  unresolved  overloading
problems described in  previous  sections.   Therefore,  for  the  time
being, Gofer does  not  support  any  form  of  default  mechanism  and
overloaded numeric constants can only be obtained by  explicit  use  of
the fromInteger function.


\subsection{Constants in dictionaries}
The Gofer system constructs new dictionaries as necessary, and  deletes
them when they are no longer required.  At any one time,  there  is  at
most one dictionary for each instance of a class.   Coupled  with  lazy
evaluation, this has a number of advantages for classes in which member
functions are defined by variable declarations as in section 9.10.   As
an example, consider the class Finite defined by:
\begin{verbatim}
    class Finite a  where  members :: [a]
\end{verbatim}
The only member in this class is a list enumerating the elements of the
type.  For example:
\begin{verbatim}
    instance Finite Bool  where  members = [False, True]
 
    instance (Finite a, Finite b) => Finite (a,b) where
        members = [ (x,y) | x<-members, y<-members ]
\end{verbatim}
In order to overcome any problems with unresolved overloading, explicit
type signatures are often needed to resolve overloading:
\begin{verbatim}
    ? members :: [Bool]
    [False, True]
    (6 reductions, 26 cells)
    ? length (members :: [((Bool,Bool),(Bool,Bool))])
    16
    (103 reductions, 195 cells)
    ?
\end{verbatim}
In some cases, the required overloading is implicit  from  the  context
and no additional type information is required,  as  in  the  following
example:
\begin{verbatim}
    ? [ x && y | (x,y) <- members ]
    [False, False, False, True]
    (29 reductions, 90 cells)
    ?
\end{verbatim}
We can also use the technique of passing a `dummy' parameter to resolve
overloading problems in a function definition:
\begin{verbatim}
    size  :: Finite a => a -> Int
    size x = length (members `asTypeOf` [x])
\end{verbatim}
which calculates the number of elements of  a  finite  type,  given  an
arbitrary element of that type:
\begin{verbatim}
    ? size (True,False)
    4
    (31 reductions, 60 cells)
    ?
\end{verbatim}
Now consider the expression \verb"size (True,False) + size (True,False)".
At first glance, we expect  this  to  repeat  the  calculation  in  the
previous example two  times,  requiring  approximately  twice  as  many
reductions and cells as before.  However,  before  this  expression  is
evaluated, Gofer constructs a dictionary for \verb"Finite (Bool,Bool)". The
evaluation of the first summand forces Gofer to evaluate the value  for
"members" in this dictionary.  Since precisely the same  dictionary  is
used to calculate the value of the second summand,  the  evaluation  of
"members" is not repeated and the complete  calculation  actually  uses
rather fewer reductions and cells:
\begin{verbatim}
    ? size (True,False) + size (True,False)
    8
    (51 reductions, 90 cells)
    ?
\end{verbatim}
On the other hand, repeating the original calculation gives exactly the
same number of reductions and cells as before, because the dictionaries
constructed at the beginning of each calculation are not  retained  for
use in subsequent calculations.

We can force Gofer to construct specific  dictionaries  whilst  reading
from a file of definitions, so that they are not deleted at the end  of
each calculation, using an explicitly typed  variable  definition  such
as:
\begin{verbatim}
    boolBoolMembers = members :: [(Bool,Bool)]
\end{verbatim}
This forces Gofer to construct the dictionary \verb"Finite (Bool,Bool)"  when
the file of definitions is loaded and prevents it from being deleted at
the end of each calculation.  Having  loaded  a  file  containing  this
definition, the  first two  attempts  to  evaluate  \verb"size (True,False)"
give:
\begin{verbatim}
    ? size (True,False)
    4
    (31 reductions, 60 cells)
    ? size (True,False)
    4
    (20 reductions, 32 cells)
    ?
\end{verbatim}

\subsection{The monomorphism restriction}
This section  describes  a  technique  used  to  limit  the  amount  of
overloading used in the definition of certain values to avoid a  number
of technical problems.  This particular topic has attracted quite a lot
of attention within the Haskell community where  it  is  affectionately
known as the `dreaded monomorphism restriction'.  Although the  initial
formulation of the rule was rather cumbersome and limiting, the current
version used in both  Gofer  and  Haskell  is  unlikely  to  cause  any
problems in practice.  In  addition,  many  of  the  examples  used  to
motivate the need for the monomorphism restriction in Haskell occur  as
a result  of  the  use  of  implicitly  overloaded  numeric  constants,
described in section 14.4.4, and hence do not occur in Gofer.

The monomorphism restriction takes its name from the way  in  which  it
limits the amount of polymorphism that can be used in particular  kinds
of declaration.  Although we touch  on  this  point  in  the  following
discussion, the description given here uses  an  equivalent,  but  less
abstract approach, based on observations about  the  implementation  of
overloaded functions.

\paragraph{Basic ideas:}
As we have seen,  the  implementation  of  overloading  used  by  Gofer
depends on being able to add extra arguments to a  function  definition
to supply the required dictionary parameters.   For  example,  given  a
function definition such as:
\begin{verbatim}
    isElement x []     =  False
    isElement x (y:ys) =  x==y || isElement x ys
\end{verbatim}
we first add a dictionary parameter for the use of the overloaded  \verb"(==)"
operator on the right hand side, obtaining:
\begin{verbatim}
    isElement x []     = False
    isElement x (y:ys) = (==) d x y || isElement x ys
\end{verbatim}
Finally, we have to add the variable \verb"d"  as  a  new  parameter  for  the
function \verb"isElement", on both the  left  and  right  hand  sides  of  the
definition:
\begin{verbatim}
    isElement d x []     = False
    isElement d x (y:ys) = (==) d x y || isElement d x ys
\end{verbatim}
The monomorphism restriction imposes conditions which prevent this last
step from being used for certain kinds  of  value  binding.

\paragraph{Declaration groups:}
Before giving the full details, it  is  worth  pointing  out  that,  in
general,  the  monomorphism  restriction  affects   groups   of   value
declarations rather than just individual  definitions.   To  illustrate
this point, consider the function definitions:
\begin{verbatim}
    f x y  =  x==y || g x y
    g x y  =  not (f x y)
\end{verbatim}
Adding an appropriate dictionary parameter for the \verb"(==)" operator gives:
\begin{verbatim}
    f x y  =  (==) d x y || g x y
    g x y  =  not (f x y)
\end{verbatim}
The next stage is to  make  this  dictionary  variable  into  an  extra
parameter to the function \verb"f" wherever it appears, giving:
\begin{verbatim}
    f d x y  =  (==) d x y || g x y
    g x y    =  not (f d x y)
\end{verbatim}
But now the right hand side  of  the  second  definition  mentions  the
dictionary variable \verb"d"  which  must  therefore  be  added  as  an  extra
parameter to \verb"g":
\begin{verbatim}
    f d x y  =  (==) d x y || g d x y
    g d x y  =  not (f d x y)
\end{verbatim}
In other words, if dictionary parameters are added  to  any  particular
function  definition,  then  each  use  of  that  function  in  another
definition will also be require  extra  dictionary  parameters.   As  a
result, the monomorphism restriction has to be applied to the  smallest
groups of  declarations  such  that  any  pair  of  mutually  recursive
bindings are in the same group.

As the example above shows, if one (or more) of the bindings in a given
declaration group is affected by the monomorphism restriction  so  that
the appropriate dictionary parameters cannot be added as parameters for
that definition, then the same condition must also be imposed on all of
the other bindings in the group.  (Adding the extra parameter to  \verb"f"  in
the example forces us to  add  an  extra  parameter  for  \verb"g";  if  extra
parameters were not permitted for \verb"g" 
then they could not be added to \verb"f".)

\paragraph{Restricted bindings:}
There are three main reasons for avoiding adding dictionary  parameters
to a particular value binding:
\BI
\IT  Dictionary parameters unnecessary.  If the dictionary  values  are
     completely determined by context then it is not necessary to  pass
     the appropriate values as dictionary parameters.  For example, the
     function definition:
\begin{verbatim}
    f x  =   x == 0  ||  x == 2
\end{verbatim}
     can be translated as:
\begin{verbatim}
    f x  =   (==) {dict} x 0  ||  (==) {dict} x 2
\end{verbatim}
     where, in both cases, the symbol \verb"{dict}" denotes the dictionary for
     \verb"Eq Int".  As a further optimisation, once the dictionary  is  fully
     determined, this can be simplified to:
\begin{verbatim}
    f x  =   primEqInt x 0 || primEqInt x 2
\end{verbatim}
\IT  Dictionary parameters cannot be added in a pattern  binding.   One
     potential solution to this problem would be to replace the pattern
     binding by an equivalent set of function bindings.   In  practice,
     we do not use this technique because it typically causes ambiguity
     problems, as illustrated by the pattern binding:
\begin{verbatim}
    (plus,times) = ((+), (*))
\end{verbatim}
     Translating this into a group of function bindings gives:
\begin{verbatim}
    newVariable  = ((+), (*))
    plus         = fst newVariable     -- fst (x,_) = x
    times        = snd newVariable     -- snd (_,y) = y
\end{verbatim}
     The type of \verb"newVariable" is 
     \verb"(Num a, Num b) => (a->a->a, b->b->b)" so
     that  the  correct  translation  of  these  bindings   using   two
     dictionary variables gives:
\begin{verbatim}
    newVariable da db = ((+) da, (*) db)
    plus da db        = fst (newVariable da db)
    times da db       = snd (newVariable da db)
\end{verbatim} 
     and hence the correct types for \verb"plus" and \verb"times" are:
\begin{verbatim} 
    plus  :: (Num a, Num b) => a -> a -> a
    times :: (Num a, Num b) => b -> b -> b
\end{verbatim}   
     both of which are ambiguous.

\IT  Adding dictionary parameters may translate a  variable  definition
     into  a  function  definition,  loosing  the  benefits  of  shared
     evaluation.  As an  example,  consider  the  following  definition
     using the function \verb"size" and the class 
     \verb"Finite"  described  in  the
     previous section:
\begin{verbatim}
    twiceSize x = n + n  where n = size x
\end{verbatim}
     Since the variable n is defined using a local definition, we would
     not expect to have to evaluate \verb"size x" more than once to  determine
     the  value  of  twiceSize.   However,  adding   extra   dictionary
     parameters without restriction gives:
\begin{verbatim}
    twiceSize d x  = n d + n d  where  n d = size d x
\end{verbatim}
     Now that \verb"n" has been replaced by a function, the evaluation will be
     repeated, once for each occurrence of the expression  \verb"n d".   In
     order to avoid this kind of problem, the monomorphism  restriction
     does not usually allow extra parameters to be added to a  variable
     definition.  Thus the original definition above will be translated
     to give:
\begin{verbatim}
    twiceSize d x  =  n + n  where n = size d x
\end{verbatim}
     Note that the same rule is applied to variable definitions at  the
     top-level of a file of definitions, resulting in an error  if  any
     dictionary parameters are required for the right hand side of  the
     definition.  As an example of this:
\begin{verbatim}
    twiceMembers = members ++ members
\end{verbatim}
     which produces an error message of the form:
\begin{verbatim}
    ERROR "ex" (line 157): Unresolved top-level overloading
    *** Binding             : twiceMembers
    *** Inferred type       : [_7]
    *** Outstanding context : Finite _7
    ?
\end{verbatim}
     (A type expression of the form \verb"_n" (such  as  \verb"_7"  in  this
     particular example) represents a  fixed  (i.e.\  monomorphic)  type
     variable.)

     In  the  case  of  a  variable   declaration,   the   monomorphism
     restriction can be overcome by giving an explicit  type  signature
     including an appropriate context, to indicate  that  the  variable
     defined is intended to be used as an overloaded  value.   In  this
     case, we need only include the declaration:
\begin{verbatim}
    twiceMembers :: Finite a => [a]
\end{verbatim}
     in the file containing the definition for \verb"twiceMembers" to suppress
     the previous error message and allow the function to be used as  a
     fully overloaded variable.

     Note that the monomorphism restriction interferes with the use  of
     polymorphism.  For example, the definition:
\begin{verbatim}
    aNumber = length (twiceMembers::[Bool]) +
              length (twiceMembers::[(Bool,Bool)])
              where twiceMembers = members ++ members
\end{verbatim}
     will not be accepted because the monomorphism  restriction  forces
     the local definition of  \verb"twiceMembers"  to  be  restricted  to  a
     single overloading (the dictionary parameter supplied to each  use
     of members must be constant throughout the local definition):
\begin{verbatim}
    ERROR "ex" (line 12): Type error in type signature expression
    *** term           : twiceMembers
    *** type           : [(Bool,Bool)]
    *** does not match : [Bool]
    ?
\end{verbatim}
     Once again, this problem can  be  fixed  using  an  explicit  type
     declaration:
\begin{verbatim}
    aNumber = length (twiceMembers::[Bool]) +
              length (twiceMembers::[(Bool,Bool)])  
              where twiceMembers :: Finite a => [a]
                    twiceMembers  = members ++ members
\end{verbatim}
\EI

\paragraph{Formal definition:}
The  examples  above  describe  the  motivation  for  the  monomorphism
restriction, captured by the following definition:

Dictionary variables will not  be  used  as  extra  parameters  in  the
definition of a value in a given declaration group $G$ if:
\BSI
\IT either:  $G$ includes a pattern binding
\IT or:  $G$ includes a variable declaration, but does not include  an
            explicit type signature for any of  the  variables  in  the
            group.
\ESI
If neither of these conditions hold, then equivalent sets of dictionary
parameters will be added to each declaration in the group.






\appendix

\chapter{Summary of grammar}

This section gives a summary of the grammar for the  language  used  by
Gofer.  The non-terminals `interp' and `module' describe the syntax  of
expressions that can be entered into the Gofer interpreter and that  of
files of definitions that can be loaded into Gofer respectively.

The following notational conventions are used in the Grammar  which  is
specified using a variant of {\sc bnf}:
\BSI
\IT nonterminals are set in roman type;
    %<angle brackets> are used to distinguish names of nonterminals from
    %keywords.
\IT vertical `$|$' bars  are used to separate alternatives;
\IT \{braces\} enclose items which may be repeated zero or more times;
\IT \sub brackets\bus\ are used for optional items;
\IT (parentheses) are used for grouping;
\IT terminal sybols are enclosed in \fbox{boxes} and are
    set in {\tt typewriter type}.
    %"quotes" surround characters which might otherwise be confused with
    %the notations introduced above.
\ESI
The following terminal symbols are used but not defined by the grammar:
\BQ
\begin{tabular}{ll}
  \I{varid}&    identifier beginning with lower case letter as described in
                section 6 \\
  \I{conid}&    like \I{varid}, but beginning with upper case letter \\
  \I{varop}&    operator symbol not beginning with a colon, as described in
                section 6 \\
  \I{conop}&    constructor function operator, like \I{varop}, but beginning
                with a colon character \\
  \I{integer}&  integer constant, as described in section 7.3 \\
  \I{float}&    floating point constant, as described in section 7.4 \\
  \I{char}&     character constant, as described in section 7.5 \\
  \I{string}&   string constant, as described in section 7.7
\end{tabular}
\EQ


\subsubsection*{Top-level grammar}
\begin{tabular}{p{2cm}cp{6.5cm}l}
 module   & ::= & \T{\char123} topdecls \T{\char125}&module\\
 interp   & ::= & exp [where]                       &top-level expression\\
 topdecls & ::= & topdecls \T{;} topdecls           &multiple declarations\\
          & $|$ & \T{data} typeLhs \T{=} constrs    &datatype declaration\\
          & $|$ & \T{type} typeLhs \T{=} type       &synonym declaration\\
          & $|$ & \T{infixl} [digit] op \{\T{,} op\}&fixity declarations\\
          & $|$ & \T{infixr} [digit] op \{\T{,} op\}\\
          & $|$ & \T{infix}  [digit] op \{\T{,} op\}\\
          & $|$ & \T{primitive} prims \T{::} type   &primitive bindings\\
          & $|$ &  class                            &class declaration\\
          & $|$ &  inst                             &instance declaration\\
          & $|$ &  decls                            &value declarations\\
\end{tabular}

\begin{tabular}{p{2cm}cp{6.5cm}l}
 typeLhs  & ::= & \I{conid} \{\I{varid}\/\}         &type declaration lhs\\

 constrs  & ::= & constrs \T{|} constrs             &multiple constructors\\
          & $|$ &  type \I{conop} type              &infix constructor\\
          & $|$ &  \I{conid} \{type\}               &constructor\\

 prims    & ::= & prims \T{,} prims                 &multiple bindings\\
          & $|$ &  var \I{string}                   &primitive binding
\end{tabular}

\subsubsection*{Type expressions}
\begin{tabular}{p{2cm}cp{6.5cm}l}
 sigType  & ::= & \sub context \T{=>} \bus type      &[qualified] type\\
 context  & ::= & \T{(} [pred \{\T{,} pred\} ] \T{)} &general form\\
          & $|$ &  pred                             &singleton context\\
 pred     & ::= & \I{conid} type \{type\}           &predicate\\
 type     & ::= & ctype [ \T{->} type ]             &function type\\
 ctype    & ::= & \I{conid} \{atype\}               &datatype or synonym\\
          & $|$ &  atype\\
 atype    & ::= & \I{varid}                         &type variable\\
          & $|$ &  \T{()}                           &unit type\\
          & $|$ &  \T{(} type \T{)}                 &parenthesised type\\
          & $|$ &  \T{(} type \T{,} type \{\T{,} type\} \T{)}  &tuple type\\
          & $|$ &  \T{[} type \T{]}                 &list type
\end{tabular}

\subsubsection*{Class and instance declarations}

\begin{tabular}{p{2cm}cp{6.5cm}l}
 class    & ::= & \T{class} [context \T{=>} ] pred [cbody]\\
 cbody    & ::= & \T{where} \T{\char123} cdecls \T{\char125}    &class body\\
 cdecls   & ::= & cdecls \T{;} cdecls               &multiple declarations\\
          & $|$ &  var \{\T{,} var\} \T{::} type    &member functions\\
          & $|$ &  fun rhs [where]                  &default bindings\\

 inst     & ::= & \T{instance} [context \T{=>} ] pred [ibody]\\
 ibody    & ::= & \T{where} \T{\char123} idecls \T{\char125}  &instance body\\
% inst     & ::= & inst  [context \T{=>} ] pred [ibody]\\
% ibody    & ::= & where \T{\char123} idecls \T{\char125}  &instance body\\
 idecls   & ::= & idecls \T{;} idecls               &multiple declarations\\
          & $|$ &  fun rhs [where]                  &member definition
\end{tabular}

\subsubsection*{Value declarations}

\begin{tabular}{p{2cm}cp{6.5cm}l}
 decls  & ::= & decls \T{;} decls                   &multiple declarations\\
        & $|$ &  var \{\T{,} var\} \T{::} sigType   &type declaration\\
        & $|$ &  fun rhs [where]                    &function binding\\
        & $|$ &  pat rhs [where]                    &pattern binding\\

 rhs    & ::= & \T{=} exp                           &simple right hand side\\
        & $|$ &  gdRhs \{gdRhs\}                    &guarded right hand sides\\

 gdRhs  & ::= & \T{|} exp \T{=} exp                 &guarded right hand side\\

 where  & ::= & \T{where} \T{\char123} decls \T{\char125}  &local definitions\\

 fun    & ::= & var                                 &function of arity 0\\
        & $|$ &  pat varop pat                      &infix operator\\
        & $|$ &  \T{(} pat varop \T{)}              &section-like notation\\
        & $|$ &  \T{(} varop pat \T{)}\\
        & $|$ &  fun apat                           &function with argument\\
        & $|$ &  \T{(} fun \T{)}                    &parenthesised lhs
\end{tabular}

\subsubsection*{Expressions}

\begin{tabular}{p{2cm}cp{6.5cm}l}
 exp    & ::= & \T{\char92} apat \{apat\} \T{->} exp    &lambda expression\\
        & $|$ &  \T{let} \T{\char123} decls \T{\char125} \T{in} exp  &local definition\\
        & $|$ &  \T{if} exp \T{then} exp \T{else} exp    &conditional expression\\
        & $|$ &  \T{case} exp \T{of} \T{\char123} alts \T{\char125}  &case expression\\
        & $|$ &  opExp \T{::} sigType                    &typed expression\\
        & $|$ &  opExp\\
    opExp  & ::= & opExp op opExp                   &operator application\\
        & $|$ &  pfxExp\\
    pfxExp & ::= & \T{-} appExp                     &negation\\
        & $|$ &  appExp\\
    appExp & ::= & appExp atomic                    &function application\\
        & $|$ &  atomic\\
    atomic & ::= & var                              &variable\\
        & $|$ &  conid                              &constructor\\
        & $|$ &  \I{integer}                        &integer literal\\
        & $|$ &  \I{float}                          &floating point literal\\
        & $|$ &  \I{char}                           &character literal\\
        & $|$ &  \I{string}                         &string literal\\
        & $|$ &  \T{()}                             &unit element\\
        & $|$ &  \T{(} exp \T{)}                    &parenthesised expr.\\
        & $|$ &  \T{(} exp op \T{)}                 &sections\\
        & $|$ &  \T{(} op exp \T{)}\\
        & $|$ &  \T{[} list \T{]}                   &list expression\\
        & $|$ &  \T{(} exp \T{,} exp \{\T{,} exp\} \T{)}   &tuple\\

 list   & ::= &  \sub\ exp \{\T{,} exp\} \bus       &enumerated list\\
%% list   & ::= &   \sub  exp \{\T{,} exp\} \bus      &enumerated list\\
        & $|$ &  exp \T{|} quals                    &list comprehension\\
        & $|$ &  exp \T{..}                         &arithmetic sequence\\
        & $|$ &  exp \T{,} exp \T{..}\\
        & $|$ &  exp \T{..} exp\\
        & $|$ &  exp \T{,} exp \T{..} exp\\
 quals  & ::= & quals \T{,} quals                   &multiple qualifiers\\
        & $|$ &  pat \T{<-} exp                     &generator\\
        & $|$ &  pat \T{=} exp                      &local definition\\
        & $|$ &  exp                                &boolean guard\\

 alts   & ::= & alts \T{;} alts                     &multiple alternatives\\
        & $|$ &  pat altRhs [where]                 &alternative\\
 altRhs & ::= & \T{->} exp                          &single alternative\\
        & $|$ &  gdAlt {gdAlt}                      &guarded alternatives\\
 gdAlt  & ::= & \T{|} exp \T{->} exp                &guarded alternative
\end{tabular}

\subsubsection*{Patterns}

\begin{tabular}{p{2cm}cp{6.5cm}l}
 pat    & ::= & pat conop pat                       &operator application\\
        & $|$ &  var \T{+} \I{integer}              &$(n+k)$ pattern\\
        & $|$ &  appPat\\
 appPat & ::= & appPat apat                         &application\\
        & $|$ &  apat\\
 apat   & ::= & var                                 &variable\\
        & $|$ &  var \T{@} pat                      &as pattern\\
        & $|$ &  \T{\char126} pat                   &irrefutable pattern\\
        & $|$ &  \T{\_}                             &wildcard\\
        & $|$ &  conid                              &constructor\\
        & $|$ &  \I{integer}                        &integer literal\\
        & $|$ &  \I{char}                           &character literal\\
        & $|$ &  \I{string}                         &string literal\\
        & $|$ &  \T{()}                             &unit element\\
        & $|$ &  \T{(} pat \T{)}                    &parenthesised expr.\\
        & $|$ &  \T{(} pat conop \T{)}              &sections\\
        & $|$ &  \T{(} conop pat \T{)}\\
        & $|$ &  \T{[} [ pat \{\T{,} pat\} ] \T{]}  &list\\
        & $|$ &  \T{(} pat \T{,} pat \{\T{,} pat\} \T{)}   &tuple
\end{tabular}

\subsubsection*{Variables and operators}

\begin{tabular}{p{2cm}cp{6.5cm}l}
 var    & ::= & varid  \\
        & $|$ & \T{(-)}                             &variable\\
 op     & ::= & varop  \\
        & $|$ & conop   \\
        & $|$ & \T{-}                               &operator\\
 varid  & ::= & \I{varid} \\
        & $|$ & \T{(} \I{varop} \T{)}               &variable identifier\\
 varop  & ::= & \I{varop}    \\
        & $|$ & \T{`} \I{varid} \T{`}               &variable operator\\
 conid  & ::= & \I{conid}    \\
        & $|$ & \T{(} \I{conop} \T{)}               &constructor identifier\\
 conop  & ::= & \I{conop}    \\
        & $|$ & \T{`} \I{conid} \T{`}               &constructor operator
\end{tabular}




\chapter{Contents of standard prelude}

\begin{verbatim}
--         __________   __________   __________   __________   ________
--        /  _______/  /  ____   /  /  _______/  /  _______/  /  ____  \
--       /  / _____   /  /   /  /  /  /______   /  /______   /  /___/  /
--      /  / /_   /  /  /   /  /  /  _______/  /  _______/  /  __   __/
--     /  /___/  /  /  /___/  /  /  /         /  /______   /  /  \  \ 
--    /_________/  /_________/  /__/         /_________/  /__/    \__\
--
--    Functional programming environment, Version 2.20 (beta-release)
--    Copyright Mark P Jones 1991.
--
--    Standard prelude for use of overloaded values using type classes.
--    Based on the Haskell standard prelude version 1.1.

help = "press :? for a list of commands"
\end{verbatim}

\subsubsection*{Operator precedence table}
\begin{verbatim}
infixl 9 !!
infixr 9 .
infixr 8 ^
infixl 7 *
infix  7 /, `div`, `rem`, `mod`
infixl 6 +, -
infix  5 \\
infixr 5 ++, :
infix  4 ==, /=, <, <=, >=, >
infix  4 `elem`, `notElem`
infixr 3 &&
infixr 2 ||
\end{verbatim}
\subsubsection*{Standard combinators}
\begin{verbatim}
primitive strict "primStrict" :: (a -> b) -> a -> b

const          :: a -> b -> a
const k x       = k

id             :: a -> a
id    x         = x

curry          :: ((a,b) -> c) -> a -> b -> c
curry f a b     =  f (a,b)

uncurry        :: (a -> b -> c) -> (a,b) -> c
uncurry f (a,b) = f a b

fst            :: (a,b) -> a
fst (x,_)       = x

snd            :: (a,b) -> b
snd (_,y)       = y

fst3           :: (a,b,c) -> a
fst3 (x,_,_)    = x

snd3           :: (a,b,c) -> b
snd3 (_,x,_)    = x

thd3           :: (a,b,c) -> c
thd3 (_,_,x)    = x

(.)	       :: (b -> c) -> (a -> b) -> (a -> c)
(f . g) x       = f (g x)

flip           :: (a -> b -> c) -> b -> a -> c
flip  f x y     = f y x
\end{verbatim}
\subsubsection*{Boolean functions}
\begin{verbatim}
(&&), (||)     :: Bool -> Bool -> Bool
False && x      = False
True  && x      = x

False || x      = x
True  || x      = True

not            :: Bool -> Bool
not True        = False
not False       = True

and, or        :: [Bool] -> Bool
and             = foldr (&&) True
or              = foldr (||) False

any, all       :: (a -> Bool) -> [a] -> Bool
any p           = or  . map p
all p           = and . map p

otherwise      :: Bool
otherwise       = True
\end{verbatim}
\subsubsection*{Character functions}
\begin{verbatim}
primitive ord "primCharToInt" :: Char -> Int
primitive chr "primIntToChar" :: Int -> Char


isAscii, isControl, isPrint, isSpace            :: Char -> Bool
isUpper, isLower, isAlpha, isDigit, isAlphanum  :: Char -> Bool

isAscii c     =  ord c < 128

isControl c   =  c < ' '    ||  c == '\DEL'

isPrint c     =  c >= ' '   &&  c <= '~'

isSpace c     =  c == ' '   || c == '\t'  || c == '\n'  || c == '\r'  ||
                               c == '\f'  || c == '\v'

isUpper c     =  c >= 'A'   &&  c <= 'Z'
isLower c     =  c >= 'a'   &&  c <= 'z'
isAlpha c     =  isUpper c  ||  isLower c
isDigit c     =  c >= '0'   &&  c <= '9'
isAlphanum c  =  isAlpha c  ||  isDigit c


toUpper, toLower      :: Char -> Char

toUpper c | isLower c  = chr (ord c - ord 'a' + ord 'A')
          | otherwise  = c

toLower c | isUpper c  = chr (ord c - ord 'A' + ord 'a')
          | otherwise  = c
\end{verbatim}
\subsubsection*{Standard type classes}
\begin{verbatim}
class Eq a where
    (==), (/=) :: a -> a -> Bool
    x /= y      = not (x == y)

class Eq a => Ord a where
    (<), (<=), (>), (>=) :: a -> a -> Bool
    max, min             :: a -> a -> a

    x <  y            = x <= y && x /= y
    x >= y            = y <= x
    x >  y            = y < x

    max x y | x >= y  = x
            | y >= x  = y
    min x y | x <= y  = x
            | y <= x  = y

class Ord a => Ix a where
    range   :: (a,a) -> [a]
    index   :: (a,a) -> a -> Int
    inRange :: (a,a) -> a -> Bool

class Ord a => Enum a where
    enumFrom       :: a -> [a]              -- [n..]
    enumFromThen   :: a -> a -> [a]         -- [n,m..]
    enumFromTo     :: a -> a -> [a]         -- [n..m]
    enumFromThenTo :: a -> a -> a -> [a]    -- [n,n'..m]

    enumFromTo n m        = takeWhile (m>=) (enumFrom n)
    enumFromThenTo n n' m = takeWhile ((if n'>=n then (>=) else (<=)) m)
                                      (enumFromThen n n')

class Eq a => Num a where               -- simplified numeric class
    (+), (-), (*), (/) :: a -> a -> a
    negate             :: a -> a
    fromInteger	       :: Int -> a
\end{verbatim}
\subsubsection*{Type class instances}
\begin{verbatim}
primitive primEqInt    "primEqInt",
	  primLeInt    "primLeInt"   :: Int -> Int -> Bool
primitive primPlusInt  "primPlusInt",
	  primMinusInt "primMinusInt",
	  primDivInt   "primDivInt",
	  primMulInt   "primMulInt"  :: Int -> Int -> Int
primitive primNegInt   "primNegInt"  :: Int -> Int

instance Eq Int  where (==) = primEqInt

instance Ord Int where (<=) = primLeInt

instance Ix Int where
    range (m,n)      = [m..n]
    index (m,n) i    = i - m
    inRange (m,n) i  = m <= i && i <= n

instance Enum Int where
    enumFrom n       = iterate (1+) n
    enumFromThen n m = iterate ((m-n)+) n

instance Num Int where
    (+)           = primPlusInt
    (-)           = primMinusInt
    (*)           = primMulInt
    (/)           = primDivInt
    negate        = primNegInt
    fromInteger x = x

primitive primEqFloat    "primEqFloat",
          primLeFloat    "primLeFloat"    :: Float -> Float -> Bool
primitive primPlusFloat  "primPlusFloat", 
          primMinusFloat "primMinusFloat", 
          primDivFloat   "primDivFloat",
          primMulFloat   "primMulFloat"   :: Float -> Float -> Float 
primitive primNegFloat   "primNegFloat"   :: Float -> Float
primitive primIntToFloat "primIntToFloat" :: Int -> Float

instance Eq Float where (==) = primEqFloat

instance Ord Float where (<=) = primLeFloat

instance Enum Float where
    enumFrom n       = iterate (1.0+) n
    enumFromThen n m = iterate ((m-n)+) n

instance Num Float where
    (+)         = primPlusFloat
    (-)         = primMinusFloat
    (*)         = primMulFloat
    (/)         = primDivFloat 
    negate      = primNegFloat
    fromInteger = primIntToFloat

instance Eq Char where c == d  =  ord c == ord d

instance Ord Char where c <= d  =  ord c <= ord d

instance Ix Char where
    range (c,c')      = [c..c']
    index (c,c') ci   = ord ci - ord c
    inRange (c,c') ci = ord c <= i && i <= ord c' where i = ord ci

instance Enum Char where
    enumFrom c        = map chr [ord c ..]
    enumFromThen c c' = map chr [ord c, ord c' ..]

instance Eq a => Eq [a] where
    []     == []     =  True
    []     == (y:ys) =  False
    (x:xs) == []     =  False
    (x:xs) == (y:ys) =  x==y && xs==ys

instance Ord a => Ord [a] where
    []     <= _      =  True
    (_:_)  <= []     =  False
    (x:xs) <= (y:ys) =  x<y || (x==y && xs<=ys)

instance (Eq a, Eq b) => Eq (a,b) where
    (x,y) == (u,v)  =  x==u && y==v

instance Eq Bool where
    True  == True   =  True
    False == False  =  True
    _     == _      =  False
\end{verbatim}
\subsubsection*{Standard numerical functions}
\begin{verbatim}
primitive div    "primDivInt",
          rem    "primRemInt",
          mod    "primModInt"    :: Int -> Int -> Int

subtract  :: Num a => a -> a -> a
subtract   = flip (-)

even, odd :: Int -> Bool
even x     = x `rem` 2 == 0
odd        = not . even

gcd       :: Int -> Int -> Int
gcd x y    = gcd' (abs x) (abs y)
             where gcd' x 0 = x
                   gcd' x y = gcd' y (x `rem` y)

lcm       :: Int -> Int -> Int
lcm _ 0    = 0
lcm 0 _    = 0
lcm x y    = abs ((x `div` gcd x y) * y)

(^)       :: Int -> Int -> Int
x ^ 0      = 1
x ^ (n+1)  = f x n x
             where f _ 0 y = y
                   f x n y = g x n where
                             g x n | even n    = g (x*x) (n`div`2)
                                   | otherwise = f x (n-1) (x*y)

abs :: Int -> Int
abs x    | x >= 0  = x
         | x <  0  = - x

signum :: Int -> Int
signum x | x == 0  = 0
         | x > 0   = 1
         | x < 0   = -1

sum, product    :: [Int] -> Int
sum              = foldl' (+) 0
product          = foldl' (*) 1

sums, products	:: [Int] -> [Int]
sums             = scanl (+) 0
products         = scanl (*) 1
\end{verbatim}
\subsubsection*{Standard list processing functions}
\begin{verbatim}
head             :: [a] -> a
head (x:_)        = x

last             :: [a] -> a
last [x]          = x
last (_:xs)       = last xs

tail             :: [a] -> [a]
tail (_:xs)       = xs

init             :: [a] -> [a]
init [x]          = [x]
init (x:xs)       = x : init xs

(++)             :: [a] -> [a] -> [a]    -- append lists.  Associative with
[]     ++ ys      = ys                   -- left and right identity [].
(x:xs) ++ ys      = x:(xs++ys)

length		 :: [a] -> Int           -- calculate length of list
length            = foldl' (\n _ -> n+1) 0

(!!)             :: [a] -> Int -> a      -- xs!!n selects the nth element of
(x:_)  !! 0       = x                    -- the list xs (first element xs!!0)
(_:xs) !! (n+1)   = xs !! n              -- for any n < length xs.

iterate          :: (a -> a) -> a -> [a] -- generate the infinite list
iterate f x       = x : iterate f (f x)  -- [x, f x, f (f x), ...

repeat           :: a -> [a]             -- generate the infinite list
repeat x          = xs where xs = x:xs   -- [x, x, x, x, ...

cycle            :: [a] -> [a]           -- generate the infinite list
cycle xs          = xs' where xs'=xs++xs'-- xs ++ xs ++ xs ++ ...

copy             :: Int -> a -> [a]      -- make list of n copies of x
copy n x          = take n xs where xs = x:xs

nub              :: Eq a => [a] -> [a]   -- remove duplicates from list
nub []            = []
nub (x:xs)        = x : nub (filter (x/=) xs)

reverse          :: [a] -> [a]           -- reverse elements of list
reverse           = foldl (flip (:)) []

elem, notElem    :: Eq a => a -> [a] -> Bool
elem              = any . (==)           -- test for membership in list
notElem           = all . (/=)           -- test for non-membership

maximum, minimum :: Ord a => [a] -> a
maximum           = foldl1 max          -- max element in non-empty list
minimum           = foldl1 min          -- min element in non-empty list

concat           :: [[a]] -> [a]        -- concatenate list of lists
concat            = foldr (++) []

transpose        :: [[a]] -> [[a]]      -- transpose list of lists
transpose         = foldr
                      (\xs xss -> zipWith (:) xs (xss ++ repeat []))
                      []

-- null provides a simple and efficient way of determining whether a given
-- list is empty, without using (==) and hence avoiding a constraint of the
-- form Eq [a].

null             :: [a] -> Bool
null []           = True
null (_:_)        = False

-- (\\) is used to remove the first occurrence of each element in the second
-- list from the first list.  It is a kind of inverse of (++) in the sense
-- that  (xs ++ ys) \\ xs = ys for any finite list xs of proper values xs.

(\\)             :: Eq a => [a] -> [a] -> [a]
(\\)              = foldl del
                    where []     `del` _  = []
                          (x:xs) `del` y
                             | x == y     = xs
                             | otherwise  = x : xs `del` y


-- map f xs applies the function f to each element of the list xs returning
-- the corresponding list of results.  filter p xs returns the sublist of xs
-- containing those elements which satisfy the predicate p.
 
map              :: (a -> b) -> [a] -> [b]
map f []          = []
map f (x:xs)      = f x : map f xs

filter           :: (a -> Bool) -> [a] -> [a]
filter _ []       = []
filter p (x:xs)
    | p x         = x : xs'
    | otherwise   = xs'
                  where xs' = filter p xs

-- Fold primitives:  The foldl and scanl functions, variants foldl1 and
-- scanl1 for non-empty lists, and strict variants foldl' scanl' describe
-- common patterns of recursion over lists.  Informally:
--
--  foldl f a [x1, x2, ..., xn]  = f (...(f (f a x1) x2)...) xn
--                               = (...((a `f` x1) `f` x2)...) `f` xn
-- etc...
--
-- The functions foldr, scanr and variants foldr1, scanr1 are duals of these
-- functions:
-- e.g.  foldr f a xs = foldl (flip f) a (reverse xs)  for finite lists xs.

foldl            :: (a -> b -> a) -> a -> [b] -> a
foldl f z []      = z
foldl f z (x:xs)  = foldl f (f z x) xs

foldl1           :: (a -> a -> a) -> [a] -> a
foldl1 f (x:xs)   = foldl f x xs

foldl'           :: (a -> b -> a) -> a -> [b] -> a
foldl' f a []     =  a
foldl' f a (x:xs) =  strict (foldl' f) (f a x) xs

scanl            :: (a -> b -> a) -> a -> [b] -> [a]
scanl f q xs      = q : (case xs of
                         []   -> []
                         x:xs -> scanl f (f q x) xs)

scanl1           :: (a -> a -> a) -> [a] -> [a]
scanl1 f (x:xs)   = scanl f x xs

scanl'           :: (a -> b -> a) -> a -> [b] -> [a]
scanl' f q xs     = q : (case xs of
                         []   -> []
                         x:xs -> strict (scanl' f) (f q x) xs)

foldr            :: (a -> b -> b) -> b -> [a] -> b
foldr f z []      = z
foldr f z (x:xs)  = f x (foldr f z xs)

foldr1           :: (a -> a -> a) -> [a] -> a
foldr1 f [x]      = x
foldr1 f (x:xs)   = f x (foldr1 f xs)

scanr            :: (a -> b -> b) -> b -> [a] -> [b]
scanr f q0 []     = [q0]
scanr f q0 (x:xs) = f x q : qs
                    where qs@(q:_) = scanr f q0 xs

scanr1           :: (a -> a -> a) -> [a] -> [a]
scanr1 f [x]      = [x]
scanr1 f (x:xs)   = f x q : qs
                    where qs@(q:_) = scanr1 f xs
\end{verbatim}
\subsubsection*{List breaking functions}
\begin{verbatim}
--
--   take n xs       returns the first n elements of xs
--   drop n xs       returns the remaining elements of xs
--   splitAt n xs    = (take n xs, drop n xs)
--
--   takeWhile p xs  returns the longest initial segment of xs whose
--                   elements satisfy p
--   dropWhile p xs  returns the remaining portion of the list
--   span p xs       = (takeWhile p xs, dropWhile p xs)
--
--   takeUntil p xs  returns the list of elements upto and including the
--                   first element of xs which satisfies p

take                :: Int -> [a] -> [a]
take 0     _         = []
take _     []        = []
take (n+1) (x:xs)    = x : take n xs

drop                :: Int -> [a] -> [a]
drop 0     xs        = xs
drop _     []        = []
drop (n+1) (_:xs)    = drop n xs

splitAt             :: Int -> [a] -> ([a], [a])
splitAt 0     xs     = ([],xs)
splitAt _     []     = ([],[])
splitAt (n+1) (x:xs) = (x:xs',xs'') where (xs',xs'') = splitAt n xs

takeWhile           :: (a -> Bool) -> [a] -> [a]
takeWhile p []       = []
takeWhile p (x:xs)
         | p x       = x : takeWhile p xs
         | otherwise = []

takeUntil           :: (a -> Bool) -> [a] -> [a]
takeUntil p []       = []
takeUntil p (x:xs)
       | p x         = [x]
       | otherwise   = x : takeUntil p xs

dropWhile           :: (a -> Bool) -> [a] -> [a]
dropWhile p []       = []
dropWhile p xs@(x:xs')
         | p x       = dropWhile p xs'
         | otherwise = xs

span, break         :: (a -> Bool) -> [a] -> ([a],[a])
span p []            = ([],[])
span p xs@(x:xs')
         | p x       = let (ys,zs) = span p xs' in (x:ys,zs)
         | otherwise = ([],xs)
break p              = span (not . p)

-- Text processing:
--   lines s     returns the list of lines in the string s.
--   words s     returns the list of words in the string s.
--   unlines ls  joins the list of lines ls into a single string
--               with lines separated by newline characters.
--   unwords ws  joins the list of words ws into a single string
--               with words separated by spaces.

lines     :: String -> [String]
lines ""   = []
lines s    = l : (if null s' then [] else lines (tail s'))
             where (l, s') = break ('\n'==) s

words     :: String -> [String]
words s    = case dropWhile isSpace s of
                  "" -> []
                  s' -> w : words s''
                        where (w,s'') = break isSpace s'

unlines   :: [String] -> String
unlines    = concat . map (\l -> l ++ "\n")

unwords   :: [String] -> String
unwords [] = []
unwords ws = foldr1 (\w s -> w ++ ' ':s) ws
\end{verbatim}
\subsubsection*{Merging and sorting lists}
\begin{verbatim}
merge               :: Ord a => [a] -> [a] -> [a] 
merge []     ys      = ys
merge xs     []      = xs
merge (x:xs) (y:ys)
        | x <= y     = x : merge xs (y:ys)
        | otherwise  = y : merge (x:xs) ys

sort                :: Ord a => [a] -> [a]
sort                 = foldr insert []

insert              :: Ord a => a -> [a] -> [a]
insert x []          = [x]
insert x (y:ys)
        | x <= y     = x:y:ys
        | otherwise  = y:insert x ys

qsort               :: Ord a => [a] -> [a]
qsort []             = []
qsort (x:xs)         = qsort [ u | u<-xs, u<x ] ++
                             [ x ] ++
                       qsort [ u | u<-xs, u>=x ]
\end{verbatim}
\subsubsection*{zip and zipWith families of functions}
\begin{verbatim}
zip  :: [a] -> [b] -> [(a,b)]
zip   = zipWith  (\a b -> (a,b))

zip3 :: [a] -> [b] -> [c] -> [(a,b,c)]
zip3  = zipWith3 (\a b c -> (a,b,c))

zip4 :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
zip4  = zipWith4 (\a b c d -> (a,b,c,d))

zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
zip5  = zipWith5 (\a b c d e -> (a,b,c,d,e))

zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [(a,b,c,d,e,f)]
zip6  = zipWith6 (\a b c d e f -> (a,b,c,d,e,f))

zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [(a,b,c,d,e,f,g)]
zip7  = zipWith7 (\a b c d e f g -> (a,b,c,d,e,f,g))


zipWith                  :: (a->b->c) -> [a]->[b]->[c]
zipWith z (a:as) (b:bs)   = z a b : zipWith z as bs
zipWith _ _      _        = []

zipWith3                 :: (a->b->c->d) -> [a]->[b]->[c]->[d]
zipWith3 z (a:as) (b:bs) (c:cs)
                          = z a b c : zipWith3 z as bs cs
zipWith3 _ _ _ _          = []

zipWith4                 :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
                          = z a b c d : zipWith4 z as bs cs ds
zipWith4 _ _ _ _ _        = []

zipWith5                 :: (a->b->c->d->e->f) -> [a]->[b]->[c]->[d]->[e]->[f]
zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
                          = z a b c d e : zipWith5 z as bs cs ds es
zipWith5 _ _ _ _ _ _      = []

zipWith6                 :: (a->b->c->d->e->f->g)
                            -> [a]->[b]->[c]->[d]->[e]->[f]->[g]
zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
                          = z a b c d e f : zipWith6 z as bs cs ds es fs
zipWith6 _ _ _ _ _ _ _    = []

zipWith7                 :: (a->b->c->d->e->f->g->h)
                             -> [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
                          = z a b c d e f g : zipWith7 z as bs cs ds es fs gs
zipWith7 _ _ _ _ _ _ _ _  = []
\end{verbatim}
\subsubsection*{Formatted output}
\begin{verbatim}
primitive primPrint "primPrint"  :: Int -> a -> String -> String

show'       :: a -> String
show' x      = primPrint 0 x []

cjustify, ljustify, rjustify :: Int -> String -> String

cjustify n s = space halfm ++ s ++ space (m - halfm)
               where m     = n - length s
                     halfm = m `div` 2
ljustify n s = s ++ space (n - length s)
rjustify n s = space (n - length s) ++ s

space       :: Int -> String
space n      = copy n ' '

layn        :: [String] -> String
layn         = lay 1 where lay _ []     = []
                           lay n (x:xs) = rjustify 4 (show n) ++ ") "
                                           ++ x ++ "\n" ++ lay (n+1) xs
\end{verbatim}
\subsubsection*{Miscellaneous}
\begin{verbatim}
until                  :: (a -> Bool) -> (a -> a) -> a -> a
until p f x | p x       = x
            | otherwise = until p f (f x)

until'                 :: (a -> Bool) -> (a -> a) -> a -> [a]
until' p f              = takeUntil p . iterate f

error                  :: String -> a
error msg | False       = error msg

undefined              :: a
undefined | False       = undefined

asTypeOf               :: a -> a -> a
x `asTypeOf` _          = x
\end{verbatim}
\subsubsection*{A trimmed down version of the Haskell Text class}
\begin{verbatim}
type  ShowS   = String -> String

class Text a where 
    showsPrec      :: Int -> a -> ShowS
    showList       :: [a] -> ShowS

    showsPrec       = primPrint
    showList []     = showString "[]"
    showList (x:xs) = showChar '[' . shows x . showl xs
                      where showl []     = showChar ']'
                            showl (x:xs) = showChar ',' . shows x . showl xs

shows      :: Text a => a -> ShowS
shows       =  showsPrec 0

show       :: Text a => a -> String
show x      =  shows x ""

showChar   :: Char -> ShowS
showChar    =  (:)

showString :: String -> ShowS
showString  =  (++)

instance Text ()

instance Text Int

instance Text Char where
    showList cs = showChar '"' . showl cs
                  where showl ""       = showChar '"'
                        showl ('"':cs) = showString "\\\"" . showl cs
                        showl (c:cs)   = showChar c . showl cs
			-- Haskell has   showLitChar c . showl cs

instance Text a => Text [a]  where
    showsPrec p = showList

instance (Text a, Text b) => Text (a,b) where
    showsPrec p (x,y) = showChar '(' . shows x . showChar ',' .
                                       shows y . showChar ')'
\end{verbatim}
\subsubsection*{I/O functions and definitions}
\begin{verbatim}
stdin         =  "stdin"
stdout        =  "stdout"
stderr        =  "stderr"
stdecho       =  "stdecho"

data Request  =  -- file system requests:
                ReadFile      String         
              | WriteFile     String String
              | AppendFile    String String
                 -- channel system requests:
              | ReadChan      String 
              | AppendChan    String String
                 -- environment requests:
              | Echo          Bool

data Response = Success
              | Str String 
              | Failure IOError

data IOError  = WriteError   String
              | ReadError    String
              | SearchError  String
              | FormatError  String
              | OtherError   String

type Dialogue  =  [Response] -> [Request]
type SuccCont  =                Dialogue
type StrCont   =  String     -> Dialogue
type FailCont  =  IOError    -> Dialogue
 
done          ::                                                Dialogue
readFile      :: String ->           FailCont -> StrCont     -> Dialogue
writeFile     :: String -> String -> FailCont -> SuccCont    -> Dialogue
appendFile    :: String -> String -> FailCont -> SuccCont    -> Dialogue
readChan      :: String ->           FailCont -> StrCont     -> Dialogue
appendChan    :: String -> String -> FailCont -> SuccCont    -> Dialogue
echo          :: Bool ->             FailCont -> SuccCont    -> Dialogue

done resps    =  []
readFile name fail succ resps =
     (ReadFile name) : strDispatch fail succ resps
writeFile name contents fail succ resps =
    (WriteFile name contents) : succDispatch fail succ resps
appendFile name contents fail succ resps =
    (AppendFile name contents) : succDispatch fail succ resps
readChan name fail succ resps =
    (ReadChan name) : strDispatch fail succ resps
appendChan name contents fail succ resps =
    (AppendChan name contents) : succDispatch fail succ resps
echo bool fail succ resps =
    (Echo bool) : succDispatch fail succ resps

strDispatch fail succ (resp:resps) = 
            case resp of Str val     -> succ val resps
                         Failure msg -> fail msg resps

succDispatch fail succ (resp:resps) = 
            case resp of Success     -> succ resps
                         Failure msg -> fail msg resps

abort           :: FailCont
abort err        = done

exit            :: FailCont
exit err         = appendChan stdout msg abort done
                   where msg = case err of ReadError s   -> s
                                           WriteError s  -> s
                                           SearchError s -> s
                                           FormatError s -> s
                                           OtherError s  -> s

print           :: Text a => a -> Dialogue
print x          = appendChan stdout (show x) abort done

prints          :: Text a => a -> String -> Dialogue
prints x s       = appendChan stdout (shows x s) abort done

interact	:: (String -> String) -> Dialogue
interact f	 = readChan stdin abort
			    (\x -> appendChan stdout (f x) abort done)

run		:: (String -> String) -> Dialogue
run f		 = echo False abort (interact f)

\end{verbatim}

\chapter{Relationship with Haskell 1.1}

The language supported by Gofer is both syntactically and  semantically
similar to that of  the  functional  programming  language  Haskell  as
defined in the report  for  Haskell  version  1.1  [5].   This  section
details the differences between the two languages, outlined briefly  in
section 2.

\subsubsection*{Haskell features not included in Gofer:}
\BI
\IT  Modules

\IT  Arrays

\IT  Derived instances for standard classes -- the ability to construct
     instances of particular classes automatically.

\IT  Default mechanism for eliminating unresolved overloading involving
     numeric and standard classes.   Since  Gofer  is  an  experimental
     system, it can be  used  with  a  range  of  completely  different
     prelude files; there is no concept of `standard classes'.

\IT  Overloaded numeric constants.  In  the  absence  of  a  defaulting
     mechanism  as  mentioned  in  the  previous  item,  problems  with
     unresolved overloading make implicitly typed programming involving
     numeric constants impractical in an interpreter based system.

\IT  Full range of numeric types  and  classes.   Gofer  has  only  two
     primitive numeric types \verb"Int" and \verb"Float" 
     (the second of which is  not
     supported in the PC version).  Although is would  be  possible  to
     modify the standard prelude so that  Gofer  uses  the  same  class
     hierarchy as Haskell, this is unnecessarily sophisticated for  the
     intended uses of Gofer.

\IT  Datatype definitions in Haskell may involve class constraints such
     as:
\begin{verbatim}
    data  Ord a => Set a = Set [a]
\end{verbatim}
     It is  not  clear  how  such  constraints  should  be  interpreted
     (particularly in the light of the  extended  form  of  constraints
     used by Gofer) in such a way to  make them useful whilst  avoiding
     unwanted ambiguity problems.
\EI

\subsubsection*{Gofer features not supported in Haskell:}
\BI
\IT  Type classes may have multiple parameters.

\IT  Predicates  in  type  expressions  may  involve   arbitrary   type
     expressions, not just type variables as used in Haskell.

\IT  Instances of type classes can be defined at  non-overlapping,  but
     otherwise arbitrary types, as described in section 14.2.5.

\IT  List comprehensions  may include  local definitions,  specified by
     qualifiers of the form \verb"pat=expr" as described in section 10.2.

\IT  No restrictions are placed on the form of predicates  that  appear
     in the context for a class or instance declaration.   This  has  a
     number  of  consequences,  including  the  possibility  of   using
     (mutually)  recursive  groups  of  dictionaries,  but  means  that
     decidability of the predicate entailment  relation  may  be  lost.
     This is not a great problem  in  practice,  since  all  dictionary
     construction  is  performed  before  evaluation   and   supposedly
     non-terminating dictionary constructions will actually generate an
     error due to the limited amount of  space  available  for  holding
     dictionaries (see section 14.4.2).
\EI

\subsubsection*{Other differences:}
\BI
\IT  Whilst superficially similar the approach to type classes in Gofer
     is quite different from that used in Haskell.  In particular,  the
     approach used in Gofer ensures that all necessary dictionaries are
     constructed before the evaluation of an expression begins,  rather
     than being built (possibly several times) during the evaluation as
     is the case with Haskell.  See section 14 and reference  [11]  for
     further details.

\IT  Input/Output facilities - Gofer supports  only  a  subset  of  the
     requests available in Haskell.  In principle, it should not be too
     difficult to add most of the remaining forms of request (with  the
     exception of those associated with binary files)  to  Gofer.   The
     principal motivation for including the I/O facilities in Gofer was
     to  make  it  possible  to  experiment  with  simple   interactive
     programs.

\IT  In Gofer, unary minus has greater  precedence  than  any  operator
     symbol, but lower than that of function application.  In  Haskell,
     the precedence of unary minus is the same as  that  of  the  infix
     (subtraction) operator of the same name.

\IT  In Haskell, the character `\verb"-"'  can  only  be  used  as  the  first
     character of an operator symbol.  In  Gofer,  this  character  may
     appear  in  any  position  in  an  operator  (except  for  symbols
     beginning with \verb"--", which indicates the start of a comment).  The
     only problems that I am aware  of  with  this  is  that  a  lambda
     expression such as \verb"\-2->2" will be parsed as such  by  a  Haskell
     system, but cause a syntax error in Gofer.  This  form  of  lambda
     expression is sufficiently unusual that I do not believe this will
     cause any problems in practice; in any case, the  parsing  problem
     can be solved by inserting a space: \verb"\ -2->2".

\IT  Pattern bindings are not currently permitted in either instance or
     class declarations.  This restriction has  been  made  simply  for
     ease of implementation, is not an inherent problem with  the  type
     class system and is likely to be  relaxed  in  later  versions  of
     Gofer if appropriate.  I have yet to see any examples in which the
     lack of pattern bindings in class and instance declarations causes
     any kind of deficiency.

\IT  Qualified  type  signatures  are  not  permitted  for  the  member
     functions  in  Gofer  class  declarations.    Once   again,   this
     restriction was made for ease of implementation  rather  than  any
     pressing technical issues.  It is  likely  that  this  restriction
     will be relaxed in future versions of Gofer,  although  I  am  not
     convinced that proper use can be made  of  such  member  functions
     without some form of nested instance declarations (yuk!).

\IT  The definition of the class Text given  in  the  standard  prelude
     does not include the Haskell functions for reading/parsing  values
     from strings; the only reason for omitting these functions was  to
     try to  avoid unnecessary complexity in the standard prelude.  The
     standard prelude  can  be  modified  to  include  the  appropriate
     additional definitions if these are required.
\EI

\subsubsection*{Known problems in Gofer:}
\BI
\IT  The null escape sequence \verb="\&"= is not generated  in  the  printable
     representations of strings produced by both the primitive function
     primPrint (used to implement the \verb"show'" function) and  the  version
     of show defined in the standard prelude.  This means that  certain
     strings values are  not printed correctly  e.g.\  
     \verb=show' "\245\&123"=
     produces the string \verb="\245123"=.  This is unlikely to cause too many
     problems in practice.

\IT  Unification of a type variable a with a  type  expression  of  the
     form \verb"T a" where \verb"T" 
     is  a  synonym  name  whose  expansion  does  not
     involve a will fail.   It  is  not  entirely  clear  whether  this
     behaviour is correct or not.

\IT  Formfeeds \verb="\f"= and vertical 
     tabs \verb="\v"= are  not  treated  as  valid
     whitespace characters in the way suggested by the Haskell  report.

\IT  Inability to recover from program stack  overlow  errors  in  some
     situations.  This problem only affects the  PC  implementation  of
     Gofer.

\IT  Implementation of \verb"ReadFile" may lose referential transparency; the
     response to a particular \verb"ReadFile" request may  be  affected  by  a
     later \verb"WriteFile" or \verb"AppendFile" 
     request for the same  file.   Whilst
     this problem can be solved for Unix based implementations, I  have
     not yet found a portable solution suitable for all of the  systems
     on which Gofer can be used.
\EI

\subsubsection*{Areas for possible future improvement:}
\BI
\IT  Relaxing the restriction on type synonyms in predicates.

\IT  General  purpose  automatic  default  mechanism  for   eliminating
     certain forms of unresolved overloading.

\IT  Improved checking and use of superclass and  instance  constraints
     during static analysis and type checking.

\IT  Simple facility to force dictionary construction at load-time.

\IT  Provision for shell escapes :! etc within the Gofer interpreter.

\IT  Debugging  facilities,  including  breakpoints  and  tracing  from
     within interpreter.

\IT  Separate interpreter and compiler programs for creating standalone
     applications using Gofer.
\EI



\chapter{Using Gofer with Bird\&Wadler}

Bird and Wadler's textbook  [1]  gives  an  excellent  introduction  to
functional programming, providing an insight into both basic techniques
and matters of programming style as well as describing  the  underlying
mathematics and its use for program development and  derivation.   Most
of the programs in that book can be used with Gofer although there  are
a number of differences between the two notations.  Fortunately, it  is
not difficult to  translate  from  one  notation  to  the  other.   The
following points are particularly useful for this:
\BI
\IT  Type constructors in Gofer  begin with capital letters (e.g.\ \verb"Bool",
     \verb"Char" etc.) where lower case is used in  [1]  
     (e.g.\   \verb"bool",  \verb"char",
     etc.).  Note that Gofer has no general numeric type \verb"num" as used
     in [1];  Use  either  \verb"Int",  \verb"Float",  
     or  overloading  in  Gofer  as
     appropriate.

\IT  Datatype definitions in [1] are written in the form \verb"lhs::=constrs".
     The equivalent definition in Gofer is: \verb"data lhs = constrs".

     Similarly, a type synonym definition in [1] of the form \verb"lhs == rhs"
     can be written in Gofer as: \verb"type lhs = rhs".

\IT  The differences between the syntax used for  guarded equations  in
     Gofer compared with the notation used in  [1]  have  already  been
     discussed in section 9.2.  For example:

     ~~~~~Using the notation of [1]:~~~~~~~~~~~~~~~~~~~~~Using Gofer:
\begin{verbatim}
     filter p (x:xs)                  filter p (x:xs)
       = x : filter p xs, if p x         | p x       = x : filter p xs
       = filter p xs,     otherwise      | otherwise = filter p xs
\end{verbatim}
\IT  In Gofer,  list comprehension qualifiers  are separated by  commas
     rather than semicolons as used in [1].

\IT  A number of the  function names and types in the  standard prelude
     are different:
\BQ
\begin{tabular}{ll|ll}
  {[1]}          &    Gofer           & {[1]}            &  Gofer \\\hline
  \verb"(#)"     &    \verb"length"   & \verb"takewhile" &  \verb"takeWhile"\\
  \verb"(~)"     &    \verb"not"      & \verb"dropwhile" &  \verb"dropWhile"\\
  \verb"(/\)"    &    \verb"(&&)"     & \verb"zipwith"   &  \verb"zipWith"\\
  \verb"(\/)"    &    \verb"(||)"     & \verb"swap"      &  \verb"flip"\\
  \verb"(!)"     &    \verb"(!!)"     & \verb"in"        &  \verb"elem"\\
  \verb"(--)"    &    \verb"(\\)"     & \verb"scan"      &  \verb"scanl"\\
  \verb"hd"      &    \verb"head"     & \verb"some"      &  \verb"any"\\
  \verb"tl"      &    \verb"tail"     & \verb"listmin"   &  \verb"minimum"\\
  \verb"decode"  &    \verb"chr"      & \verb"listmax"   &  \verb"maximum"\\
  \verb"code"    &    \verb"ord"
\end{tabular}
\EQ
     See appendix B for a complete list of standard functions in Gofer.

     The version of \verb"foldl"  using  \verb"strict"  
     which  appears  in  [1]  is
     available in Gofer as the function \verb"foldl'".
     The role of \verb"zip" and \verb"zipwith" in [1] is 
     filled by the \verb"zip"  and
     \verb"zipWith" families of functions in Gofer.  An  expression  of  the
     form \verb"zip (xs,ys)" in [1] is equivalent to 
     \verb"zip xs ys"  in  Gofer
     etc.

\IT  Gofer does not enforce the condition assumed in [1] that the  left
     hand sides of each of the equations defining a  function  must  be
     disjoint.

\IT  The equality operator in Gofer is written as  \verb"=="  and the single
     equality character \verb"=" is a reserved symbol used to separate  left
     and right hand sides of equations.  Many  C  programmers  will  be
     familiar with this kind of notation (together with  the  kinds  of
     problems it can create!).

\IT  Some of the  identifiers used in  [1] are reserved words in Gofer.
     Examples that are particularly likely to occur  include  \verb"in"  and
     \verb"then".
\EI

\chapter{Primitives}

Warning: the features described in this appendix  are  typically  only
needed when alternative versions of the standard prelude  are  created.
These features should only be used by expert users; misuse may lead  to
failure and runtime errors in the Gofer interpreter.  It is not usually
a good idea to use primitive functions directly in your programs.

A number of primitive functions are builtin to the  Gofer  interpreter,
and may be bound to function symbols using a declaration of the form:
\begin{verbatim}
    primitive name1 code1, name2 code2, ...., namen coden :: type
\end{verbatim}
where each name is an identifier (or an  operator  symbol  enclosed  by
parentheses) and each code is a string literal  taken  from  the  table
below.  The type specified to the right of the  \verb"::"  symbol  must  be  a
valid type for the functions being defined -- {\em warning: gofer  does  not
attempt to check for suitability of the declared type}.   The  following
definition, taken from the standard prelude,  illustrates  the  use  of
this feature to bind  a  function  named  \verb"primPrint"  to  the  primitive
function with code name string \verb="primPrint"= and type 
\verb"Int -> a ->  String -> String":
\begin{verbatim}
    primitive primPrint "primPrint"  :: Int -> a -> String -> String
\end{verbatim}
The primitive functions currently available are:
\BI
\IT integer arithmetic:
\begin{verbatim}
    primPlusInt         Int -> Int -> Int 
    primMinusInt        Int -> Int -> Int
    primMulInt          Int -> Int -> Int
    primDivInt          Int -> Int -> Int
    primModInt          Int -> Int -> Int
    primRemInt          Int -> Int -> Int
    primNegInt          Int -> Int -> Int
\end{verbatim}
\IT floating point arithmetic:
\begin{verbatim}
    primPlusFloat       Float -> Float -> Float
    primMinusFloat      Float -> Float -> Float
    primMulFloat        Float -> Float -> Float
    primDivFloat        Float -> Float -> Float
    primNegFloat        Float -> Float -> Float
\end{verbatim}
\newpage
\IT coercion functions:
\begin{verbatim}
    primIntToChar       Int -> Char  -- chr in the standard prelude
    primCharToInt       Char -> Int  -- ord in the standard prelude
    primIntToFloat      Int -> Float -- implements fromInteger
\end{verbatim}
\IT equality and $\le$ primitives:
\begin{verbatim}
    primEqInt           Int -> Int -> Bool
    primLeInt           Int -> Int -> Bool
    primEqFloat         Float -> Float -> Bool
    primLeFloat         Float -> Float -> Bool
\end{verbatim}
\IT generic ordering primitives:
\begin{verbatim}
    primGenericEq       a -> a -> Bool
    primGenericNe       a -> a -> Bool
    primGenericGt       a -> a -> Bool
    primGenericLe       a -> a -> Bool
    primGenericGe       a -> a -> Bool
    primGenericLt       a -> a -> Bool
\end{verbatim}
    These functions implement the standard generic  (i.e.\  non
    overloaded) ordering primitives.  They are  not  currently
    used in the standard prelude.  A simplified prelude may be
    created by binding the  standard  operator  symbols  \verb"(==)",
    \verb"(/=)",  \verb"(>)",  \verb"(<=)",  \verb"(>=)"  
    and  \verb"(<)"  to   these   functions
    respectively.
\IT output:
\begin{verbatim}
    primPrint           Int -> a -> String -> String
\end{verbatim}
    This function is used to implement the \verb"show'"  function  in
    the standard prelude and is not usually used directly.

    \verb"primPrint d e s" produces a textual representation  of  the
    value of the expression \verb"e" as a  string,  followed  by  the
    string \verb"s".  The integer parameter \verb"d" is used as an indicator
    of the current precedence level.  The  \verb"primPrint"  function
    is the  standard  method  of  printing  the  value  of  an
    expression whose type is not equivalent to the type \verb"String"
    used by the top-level of the Gofer interpreter.
\IT sequencing:
\begin{verbatim}
    primStrict          (a -> b) -> a -> b
\end{verbatim}
    The \verb="primStrict"= function (bound to the identifier  \verb"strict"
    in the standard prelude)  forces  the  evaluation  of  its
    second argument before the function supplied as the  first
    argument is  applied  to  it.   See  section  9.4  for  an
    illustration.
\EI


%\chapter{Example programs}
%\input{examples.tex}
%

\chapter{Interpreter command summary}

\BI
\IT
\I{expr}     

             Analyse expression for errors, typecheck and evaluate.  If
             the expression has type \verb"Dialogue",  execute  as  a  program
             using the I/O facilities as described in section  12.   If
             the expression has type \verb"String", evaluate and print  result
             as a lazy list of characters.   In  any  other  case,  the
             standard  prelude  function  \verb"show'"  is  applied   to   the
             expression and used to print the value of  the  result  in
             the form of a string, as in the previous case.\\
\IT
\verb":t" \I{expr} \\
\verb":type" \I{expr} \\
\verb":T"

             Analyse expression for errors,  typecheck  and  print  the
             translation and inferred type of the term.
\IT
\verb":q"\\
\verb":quit"\\
\verb":Q"

             Exit Gofer interpreter.
\IT
\verb":?"\\
\verb":h"\\
\verb":H"

             Display summary of interpreter commands.
\IT
\verb":l" $f_1$ \dots $f_n$\\
\verb":load"\\
\verb":L"

             Removes any previously loaded  files  of  definitions  and
             attempts to load the contents of the files $f_1$ upto $f_n$  one
             after the other.
             If no filenames are provided,
             only
             those functions and values defined in the standard prelude
             will still be be available.
\IT
\verb":a" $f_1$ \dots $f_n$  \\
\verb":also"\\
\verb":A"

             Load the contents of the files $f_1$ upto $f_n$ in  addition  to
             any previously loaded files.   If  any  of  the  files  of
             definitions which  have  already  been  loaded  have  been
             modified since they were last  read  then  they  are
             automatically reloaded before any of the files $f_1$ upto  $f_n$
             are read.

             If successful, a command of  the  form  \verb":l f1 ... fn"  is
             equivalent to the sequence of commands:
             \verb":l", \verb":a f1" \dots \verb":a fn".
\IT
\verb":r"\\
\verb":reload"\\
\verb":R"

             Repeat the last load command,  attempting  to  reload  any
             files which have subsequently been modified.  Since  later
             files may depend on the definitions in earlier ones,  once
             one file has been reloaded, all subsequent files will also
             need to be reloaded.
\IT
\verb":e" \I{file}\\
\verb":edit"\\
\verb":E"

             Suspend current Gofer session and start an editor  program
             to modify or view the named file.  The Gofer session  will
             be resumed when the editor  program  terminates,  and  any
             script files that  have  been  changed  will  be  reloaded
             automatically.

             Note that a separate editor program is required  and  that
             Gofer must be properly installed to use this feature.  The
             default editor is usually \verb"vi" (Calvin version 2.0 is a good
             substitute for a PC), although this may have been  changed
             when your system was installed.   In  any  case,  you  can
             always substitute an editor of your choice by setting  the
             environment variable \verb"EDITOR" to the name of your  favourite
             editor program.

             There are a number  of  factors  which  will  affect  your
             choice of editor.  On a slow machine, with only a  limited
             amount of memory, you  will  probably  need  to  choose  a
             relatively small editor which  can  be  loaded  reasonably
             quickly and does not require too much memory.  On  a  more
             powerful system, you may find it more  convenient  to  use
             Gofer from a window based environment, running your editor
             in one window with Gofer in another.

             Using the \verb":e" command without specifying a particular  file
             to be edited starts up  an  editor  program  as  described
             above either for the file  of  definitions  most  recently
             loaded into Gofer or, if an error occurred whilst  loading
             a file of definitions, for  the  file  of  definitions  in
             which the error was last detected.

             With many editor programs, it is even  possible  to  start
             the editor at the  line  where  the  error  occurred.   As
             before, it is possible to change the default behaviour  of
             Gofer in this case by  setting  the  environment  variable
             \verb"EDITLINE" to a command string which can be  used  to  start
             the editor program with a given file at  a  specific  line
             number.  The positions in the string  at  which  the  file
             name and line number values should be inserted  should  be
             indicated by the strings \verb="%s"= 
             and \verb="%d"=  respectively,  and
             may appear in either order.  The default  command  string,
             which is used if \verb"EDITLINE" is not set is 
             \verb="vi +%d %s"=.
\EI


\chapter{Bibliography}

\begin{enumerate}
\item
     Richard  Bird  and  Philip Wadler, 
     {\em Introduction to functional programming}, 
     Prentice Hall International, 1989.

\item
     Simon  L.~Peyton Jones, 
     {The Implementation of functional programming languages},  
     Prentice Hall International, 1987.

\item
     Thomas  Johnsson,  
     {Lambda Lifting:  Transforming  Programs  to  Recursive  Equations},
     in  Lecture  Notes  in  Computer  Science  201,
     Springer Verlag, 1985.  
     [but try to get a copy of the  version  of
     this paper included in Johnsson's thesis which  benefits  from  an
     extended typeface and is a little easier to read!]

\item
     Philip  Wadler  and Stephen Blott, 
     {How to make ad-hoc polymorphism less  ad-hoc},  
     in the  proceedings  of  the
     16th ACM annual symposium on Principles of Programming  Languages,
     Austin, Texas, January 1989.

\item
     Paul Hudak,  Philip  Wadler  et al.,
     {Report on the programming language Haskell,  a  non-strict  purely
     functional language (Version 1.1)}, 
     Technical report Yale University/Glasgow University.  August, 1991.

\item
     Philip  Wadler  and  Quentin  Miller,
     {\em Introduction to Orwell 6.00}, 
     University of Oxford, 1990.

\item
     Lennart  Augustsson  and  Thomas  Johnsson,
     {\em Lazy ML user's manual}, 
     1990.

\item
     Mark  P.~Jones, 
     {\em Computing with lattices: An application of type classes},  
     Technical report PRG-TR-11-90, Programming Research  Group,
     Oxford University Computing Laboratory, June 1990.

\item
     Mark  P.~Jones,  
     {\em Towards a theory of qualified  types},  
     Technical report PRG-TR-6-91, 
     Programming Research Group, Oxford  University
     Computing Laboratory, April 1991.

\item
     Mark  P.~Jones,  
     {\em Type inference for  qualified  types},  
     Technical
     report PRG-TR-10-91, Programming Research Group, Oxford University
     Computing Laboratory, June 1991.

\item
     Mark  P.~Jones,  
     {\em A new approach to type classes},  
     distributed  to
     Haskell mailing list 1991.

\item
     Mark P.~Jones, 
     {\em Practical issues in the implementation of qualified types}, 
     Forthcoming 1991.
\end{enumerate}



\end{document}

