home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Docs / release.228 < prev    next >
Text File  |  1994-06-23  |  120KB  |  3,235 lines

  1.  
  2.  
  3.                                                                       
  4.  
  5.  
  6. -----------------------------------------------------------------------
  7.        __________   __________   __________   __________   ________
  8.       /  _______/  /  ____   /  /  _______/  /  _______/  /  ____  \
  9.      /  / _____   /  /   /  /  /  /______   /  /______   /  /___/  /
  10.     /  / /_   /  /  /   /  /  /  _______/  /  _______/  /  __   __/
  11.    /  /___/  /  /  /___/  /  /  /         /  /______   /  /  \  \ 
  12.   /_________/  /_________/  /__/         /_________/  /__/    \__\
  13.  
  14.   Functional programming environment, Version 2.28
  15.  
  16.   Copyright Mark P Jones 1993.
  17.  
  18.   Release notes
  19. -----------------------------------------------------------------------
  20.  
  21. This document is intended to be used as a supplement to the original
  22. user manual ``An introduction to Gofer version 2.20'' and release
  23. notes for Gofer 2.21 (previously supplied in a file called `update').
  24.  
  25. If you would like to be informed when bug-fixes or further versions
  26. become available, please contact me at jones-mark@cs.yale.edu (if you
  27. have not already done so) and I will add your name to the list.
  28.  
  29. Please contact me if you have any questions about the Gofer system, or
  30. if you need some advice or help to complete a port of Gofer to a new
  31. platform.
  32.  
  33.  
  34. ACKNOWLEDGMENTS:
  35. A lot of people have contributed to the development of Gofer 2.28 with
  36. their support, encouragement, suggestions, comments and bug reports.
  37. There are a lot of people to thank:
  38.  
  39.                  Ray Bellis            Brent Benson
  40.                David Bolton           Rodney Brown
  41.                 Dave Cattrall         Manuel Chakravarty
  42.                 Rami El Charif        Stuart Clayman
  43.                 Andy Duncan            Bernd Eckenfels
  44.              Stephen Eldridge         Jeroen Fokker
  45.                 Andy Gill             Annius Groenink
  46.             Dipankar Gupta           Guenter Huebel
  47.                  Jon Hallett           Kevin Hammond
  48.                Peter Hancock             Ian Holyer
  49.               Andrew Kennedy          Marnix Klooster
  50.                  Tom Lane           Hiroyuki Matsuda
  51.                Aiden McCaughey        Tobias Nipkow
  52.               Rainer Orth               Will Partain
  53.                Simon Peyton Jones        Ian Poole
  54.                 Mark Raemer             Dave Rushall
  55.               Julian Seward            Carol Tumey
  56.                Goran Uddeborg          Gavin Wraith
  57.                Bryan Scattergood     Matthew Smith
  58.              Bernard Sufrin           Philip Wadler
  59.  
  60. This list isn't complete, and I apologize in advance if I have
  61. inadvertently left your name out.
  62.  
  63.  
  64.                                       1
  65.  
  66.  
  67.  
  68.  
  69. Release Notes v2.28                 1. MINOR ENHANCEMENTS AND BUGFIXES
  70.  
  71.  
  72. 1. MINOR ENHANCEMENTS AND BUGFIXES
  73.  
  74. The following sections list the minor enhancements and bugfixes that
  75. have been made to Gofer since the release of Gofer version 2.23.  More
  76. significant changes are described in later sections.
  77.  
  78.  
  79. 1.1  Enhancements
  80. -----------------
  81.   o  For systems without the restrictions of older PCs, Gofer now uses
  82.      multiple hash tables to speed the lookup of globally defined
  83.      functions.  Loading large programs into Gofer is now much faster
  84.      as a result.  In one example, the time taken to load a 13,000 line
  85.      program spread across 40 individual script files was reduced by a
  86.      factor of five!
  87.  
  88.   o  For the most most part, internal errors (which shouldn't normally
  89.      appear anyway) no longer terminate the interpreter.
  90.  
  91.   o  Better handling for programs with objects whose type involves more
  92.      than 26 type variables (though whether anyone has real practical
  93.      applications for such beasts, I'm rather doubtful).
  94.  
  95.   o  The Gofer system now supports I/O requests GetProgName, GetArgs
  96.      and GetEnv.  The first two requests don't have any sensible
  97.      interpretation within the interpreter, so GetProgName always
  98.      returns "", while GetArgs returns [].  These I/O requests are most
  99.      useful when producing standalone applications with the Gofer
  100.      compiler where they do indeed give the name of the program and the
  101.      list of command line arguments as expected.
  102.  
  103.   o  Added primitives for direct comparison of characters.  The
  104.      original definitions of character equality and ordering in terms
  105.      of the equality and ordering on integers was elegant, but for some
  106.      examples, a substantial number of the total reductions in a given
  107.      program was taken up with calls to ord, an unnecessary
  108.      distraction.
  109.  
  110.   o  Small improvements in the speed of execution of the runtime machine,
  111.      particularly when Gofer is compiled using the GNU C compiler.
  112.  
  113.   o  Enabled the use of GNU C specific options to store frequently used
  114.      global variables in CPU registers.  This is perhaps most useful
  115.      for speeding up the performance of standalone applications
  116.      produced using the Gofer compiler.
  117.  
  118.   o  Changed definitions in standard preludes to provide overloaded
  119.      versions of sum, product, sums, products, abs, signum and (^).
  120.      Also added a genericLength function as in Haskell.  Finally,
  121.      added Text as a superclass of Num, again for Haskell compatibility.
  122.  
  123.   o  Added a new primitive function: openfile :: String -> String that
  124.      can be used to read the contents of a file (named by the argument
  125.      string) as a (lazy) stream of characters.  (The implementation is
  126.      in terms of a primitive which can also be used to implement the
  127.      hbc openFile function, provided that you also define the Either
  128.  
  129.  
  130.                                       2
  131.  
  132.  
  133.  
  134.  
  135. Release Notes v2.28                                  1.1  Enhancements
  136.  
  137.  
  138.      datatype used there.)
  139.  
  140.   o  Added support for a simple selection of operators for monadic I/O,
  141.      mutable variables etc. based on Lambda var (developed at Yale) and
  142.      the Glasgow I/O system.  I will provide more documentation on this
  143.      as soon as there is a better consensus on the names of the
  144.      datatypes and functions that should be included in systems like
  145.      this.
  146.  
  147.   o  The error function is now implemented using a primitive function.
  148.  
  149.   o  Added support for floating point primitives:
  150.  
  151.          pi          :: Float
  152.  
  153.          sin, asin,
  154.          cos, acos,
  155.          tan, atan,
  156.          log, log10,
  157.          exp, sqrt   :: Float -> Float
  158.  
  159.          atan2       :: Float -> Float -> Float
  160.          truncate    :: Float -> Int
  161.  
  162.   o  Added support for the use of GNU readline (or equivalent) library
  163.      to be used to enhance the user interface with command line
  164.      editing.  See the source makefile for instructions on how to use
  165.      this.
  166.  
  167.   o  Added floating point support to PC version of Gofer (even the
  168.      version for humble 8086 PCs will now support floating point).
  169.      Thanks to Jeroen Fokker for this!
  170.  
  171.   o  I/O datatype definitions and otherwise symbol are now builtin to
  172.      the Gofer system.
  173.  
  174.   o  Other minor tweaks and improvements.
  175.  
  176.  
  177. 1.2  Bug fixes
  178. --------------
  179. Nobody really likes to dwell on bugs, especially when they have been
  180. eliminated.  But for those of you who want to know, here is a summary of
  181. the bugs discovered and fixed in Gofer 2.28:
  182.  
  183.   o  End of file does not imply end of line (only significant on
  184.      certain systems ... I has made an assumption which happens to hold
  185.      under DOS and Unix, but was not true for other systems).
  186.  
  187.   o  Code generator produced incorrect code for some conditional
  188.      expressions involving local variables (fairly obscure).
  189.  
  190.   o  Some conditional expressions entered into the interpreter were
  191.      evaluated incorrectly, leading to unexpected evaluation errors.
  192.  
  193.   o  A small potential space leak concerned with saving the names of
  194.  
  195.  
  196.                                       3
  197.  
  198.  
  199.  
  200.  
  201. Release Notes v2.28                                     1.2  Bug fixes
  202.  
  203.  
  204.      files passed to the editor from within Gofer was eliminated.
  205.  
  206.   o  A subtle bug, which only occurred when a garbage collection
  207.      occurred in the middle of an attempt to update a cell with an
  208.      indirection has been fixed.
  209.  
  210.   o  Fixing the definitions of the div and quot operators to agree with
  211.      Haskell 1.2 (these had been changed in the transition from 1.1 to
  212.      1.2 without my noticing).
  213.  
  214.   o  Corrected bug in string matching code (part of the :names command)
  215.      which previously allowed "*e*p" to match with "negate"!
  216.  
  217.   o  Nested comments were not always handled correctly when they
  218.      occurred at the very end of a script file.
  219.  
  220.   o  Added new clauses to parser to improve and correct error messages
  221.      produced by some examples.
  222.  
  223.   o  Other miscellaneous tweaks and fixes.
  224.  
  225. There are no other currently known bugs in Gofer.  But someone is bound
  226. to find a new one within hours of the release of 2.28 if past
  227. experience is anything to go by.  If that someone is you, please let me
  228. know!
  229.  
  230.  
  231. 2. USER INTERFACE EXTENSIONS
  232.  
  233. The user interface of the previous release has been extended a little
  234. to support a range of new features, intended to make the Gofer
  235. environment more convenient for program development.  Further details
  236. are given in the following sections.
  237.  
  238. 2.1  Customizing the Gofer system
  239. ---------------------------------
  240. Often there will be several people using Gofer on the same system.  Not
  241. everyone will want to be using the system in the same way.  For example,
  242. some users may wish to use their own version of the prelude or start the
  243. interpreter with particular command line options.
  244.  
  245. It has always been possible to do this by installing Gofer in an
  246. appropriate manner.  But, having had more than a couple of enquiries
  247. about this, I wanted to take some time to spell the process out more
  248. clearly.  The following description will be biased towards those people
  249. using Gofer on Unix-like systems, but the same basic principles can be
  250. applied with other operating systems too.
  251.  
  252. The Gofer interpreter and prelude files will typically be installed in
  253. a given directory, accessible to all users on the system.  For the sake
  254. of this example, let's assume that this is /usr/local/lib/Gofer.  Each
  255. user could take a copy of the Gofer interpreter into their own file
  256. space, but a much better option is for each user to use a short script
  257. file stored somewhere on their path.  For example, the path on my Unix
  258. account includes a subdirectory called bin and I store the following
  259. script file `gofer' in this directory:
  260.  
  261.  
  262.                                       4
  263.  
  264.  
  265.  
  266.  
  267. Release Notes v2.28                  2.1  Customizing the Gofer system
  268.  
  269.  
  270.     #!/bin/sh
  271.     #
  272.     # A simple shell script to invoke the Gofer interpreter and set
  273.     # the path to the prelude file.  Ultimately, you might want to
  274.     # copy this file into your own bin directory so that you can record
  275.     # your favourite command line settings or use a different prelude
  276.     # file ...
  277.     #
  278.     GOFER=/usr/local/lib/Gofer/standard.prelude
  279.     export GOFER
  280.     exec /usr/local/lib/Gofer/gofer $*
  281.  
  282. I happen to use the standard prelude file and the default settings for
  283. all the command line options.  If, for example, I wanted to use a
  284. different prelude file, a smaller heap and omit the printing of
  285. statistics about the number of reductions and cells used in an
  286. evaluation, I can modify the script to reflect this:
  287.  
  288.     #!/bin/sh
  289.     #
  290.     # A modified version of the above script
  291.     #
  292.     GOFER=/usr/local/lib/Gofer/simple.prelude
  293.     export GOFER
  294.     exec /usr/local/lib/Gofer/gofer -h20000 -s $*
  295.  
  296. Of course, it is also possible to keep both of these short scripts in
  297. my bin directory, so that I have the choice of starting up Gofer in
  298. several different configurations, depending on the kind of work I'm
  299. going to be doing with it.
  300.  
  301.  
  302. 2.2  Command line options
  303. --------------------------
  304. Gofer 2.28 supports a number of options which can be set, either on the
  305. command line when Gofer interpreter is started, or using the :set
  306. command within in the interpreter.  Using the :set command without any
  307. arguments produces a list of all the command line options available:
  308.  
  309.    ? :set
  310.    TOGGLES: groups begin with +/- to turn options on/off resp.
  311.    s    Print no. reductions/cells after eval
  312.    t    Print type after evaluation
  313.    d    Show dictionary values in output exprs
  314.    f    Terminate evaluation on first error
  315.    g    Print no. cells recovered after gc
  316.    c    Test conformality for pattern bindings
  317.    l    Literate scripts as default
  318.    e    Warn about errors in literate scripts
  319.    i    Apply fromInteger to integer literals
  320.    o    Optimise use of (&&) and (||)
  321.    u    Catch ambiguously typed top-level vars
  322.    .    Print dots to show progress
  323.    w    Always show which files loaded
  324.    1    Overload singleton list notation
  325.    k    Show kind errors in full
  326.  
  327.  
  328.                                       5
  329.  
  330.  
  331.  
  332.  
  333. Release Notes v2.28                          2.2  Command line options
  334.  
  335.  
  336.    OTHER OPTIONS: (leading + or - makes no difference)
  337.    hnum Set heap size (cannot be changed within Gofer)
  338.    pstr Set prompt string to str
  339.    rstr Set repeat last expression string to str
  340.  
  341.    Current settings: +sfceow1 -tdgliu.k -h100000 -p? -r$$
  342.    ?
  343.  
  344. Most of these are the same as in the previous release of Gofer.  The
  345. following sections outline the few changes that have been made.  The
  346. `1' and `k' toggles are for use with constructor classes and will be
  347. described in Section 4.
  348.  
  349.  
  350. 2.2.1  Print dots to show progress
  351. ----------------------------------
  352. One of the first differences that you might notice when running the
  353. new version of Gofer is that the rows of dots printed when loading a
  354. script file:
  355.  
  356.     ? :l examples
  357.     Reading script file "examples":
  358.     Parsing....................................
  359.     Dependency analysis........................
  360.     Type checking..............................
  361.     Compiling..................................
  362.  
  363.     Gofer session for:
  364.     /usr/local/lib/Gofer/standard.prelude
  365.     examples
  366.     ?
  367.  
  368. are no longer printed while script files are loaded.  The rows of dots
  369. are useful for showing progress on slow machines (like the PC on which
  370. Gofer was originally developed) where it is reassuring to know that the
  371. system has not crashed, and is simply working its way through one
  372. particular phase of the system.  However, on a faster system, the dots
  373. are not necessary and printing them can impose a surprising overhead on
  374. the time it takes to load files.  As a default, Gofer now simply prints
  375. the names of each phase (Parsing, Dependency Analysis, Type checking
  376. and Compiling) and, when that phase is complete, backspaces over it to
  377. erase it from the screen.  If you are fortunate enough to be using a
  378. fast machine, you may not always see the individual words as they flash
  379. past.  After loading a file, your screen will typically look something
  380. like this:
  381.  
  382.     ? :l examples
  383.     Reading script file "examples":
  384.                    
  385.     Gofer session for:
  386.     /usr/local/lib/Gofer/standard.prelude
  387.     examples
  388.     ?
  389.  
  390. On some systems, the use of backspace characters to erase a line may
  391. not work properly.  One particular example of this occurs if you try to
  392.  
  393.  
  394.                                       6
  395.  
  396.  
  397.  
  398.  
  399. Release Notes v2.28                 2.2.1  Print dots to show progress
  400.  
  401.  
  402. run Gofer from within emacs.  In this case, you may prefer to use the
  403. original setting, printing the lines of dots by giving the command:
  404.  
  405.     :set +.
  406.  
  407. The default setting is (as illustrated above, :set -.).  In practice,
  408. you will probably want to include the appropriate setting for this
  409. option in your startup script (see Section 2.1).
  410.  
  411.  
  412. 2.2.2  Always show which files loaded
  413. -------------------------------------
  414. Some people may feel that the list of filenames printed by Gofer after
  415. successfully loading one or more script files is redundant.  This is
  416. particularly likely if you are using the (usually default) :set -.
  417. option since the list of files loaded will probably still be on the
  418. screen.  The list of filenames can be suppressed using the :set -w
  419. option as follows:
  420.  
  421.     ? :l examples
  422.     Reading script file "examples":
  423.                    
  424.     Gofer session for:
  425.     /usr/local/lib/Gofer/standard.prelude
  426.     examples
  427.     ? :set -w
  428.     ? :l examples
  429.     Reading script file "examples":
  430.     ?
  431.  
  432. The default setting can be recovered using a :set +w command.
  433.  
  434. Note that you can also use the :info command (without any arguments) as
  435. described in Section 2.3.2 to find out the list of files loaded into the
  436. current Gofer session.  This should be particularly useful if you choose
  437. the :set -w option.
  438.  
  439.  
  440. 2.2.3  Set repeat string
  441. ------------------------
  442. The previous expression entered into the Gofer system can be recalled
  443. as part of the next expression using the symbol $$:
  444.  
  445.     ? map (1+) [1..10]
  446.     [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  447.     (101 reductions, 189 cells)
  448.     ? filter even $$
  449.     [2, 4, 6, 8, 10]
  450.     (130 reductions, 215 cells)
  451.     ?
  452.  
  453. This feature was provided and documented in the previous release of
  454. Gofer.  However, it is possible that you may prefer to use a different
  455. character string.  This is the purpose of the -rstr option which sets
  456. the repeat string to str.  For example, user's of SML might be more
  457. comfortable using:
  458.  
  459.  
  460.                                       7
  461.  
  462.  
  463.  
  464.  
  465. Release Notes v2.28                           2.2.3  Set repeat string
  466.  
  467.  
  468.     ? :set -rit
  469.     ? 6*7
  470.     42
  471.     (3 reductions, 7 cells)
  472.     ? it + it
  473.     84
  474.     (4 reductions, 11 cells)
  475.     ?
  476.  
  477. Another reason for making this change might be that you have a program
  478. which uses the symbol $$ as an operator.  Each occurrence of the $$ symbol
  479. in a script file will be interpreted as the correct operator, whatever
  480. the value of the repeat string.  But, if the default :set -r$$ setting is
  481. used, any occurrence of $$ in an expression entered directly to the
  482. evaluator will be taken as a reference to the previous expression.
  483.  
  484. Note that the repeat string must be either a valid Haskell identifier or
  485. symbol, although it will always be parsed as an identifier.  If the
  486. repeat string is set to a value which is neither an identifier or symbol
  487. (for example, :set -r0) then the repeat last expression facility will be
  488. disabled altogether.
  489.  
  490.  
  491. 2.2.4  Other changes
  492. --------------------
  493. Comparing the list of command line options in Section 2.2 with the list
  494. produced by previous versions of Gofer will reveal some other small
  495. differences not already mentioned above.  The changes are as follows:
  496.  
  497.   o  The default setting for the d toggle (show dictionaries in output
  498.      expressions) has been changed to off (:set -d).  For a lot of
  499.      people, the appearance of dictionary values was rather confusing
  500.      and of little use.  If you still want to see how dictionary values
  501.      are used, you will need to do :set +d or add the +d argument to
  502.      your startup script.
  503.  
  504.   o  The default setting for the e toggle (warn about errors in
  505.      literate scripts) has been changed to :set +e for closer
  506.      compatibility with the literate script convention outline in the
  507.      Haskell report, version 1.2.  In addition, the setting of the l
  508.      toggle is now used only as a default if no particular type of
  509.      script file is specified by the file extension of a give script.
  510.      See Section 2.4 below for further details.
  511.  
  512.   o  The default setting for the f toggle (terminate evaluation on
  513.      first error) has been changed to :set +f.  The old setting of
  514.      :set -f is, in my opinion, better for debugging purposes, but
  515.      does not give the behaviour that those using Haskell might
  516.      expect.  This has caused a certain amount of confusion and was
  517.      the motivation for this change.
  518.  
  519.   o  The following three command line options, provided in previous
  520.      versions of Gofer, have now been removed:
  521.  
  522.          TOGGLES:
  523.          a    Use any evidence, not nec. best
  524.  
  525.  
  526.                                       8
  527.  
  528.  
  529.  
  530.  
  531. Release Notes v2.28                               2.2.4  Other changes
  532.  
  533.  
  534.          E    Fail silently if evidence not found
  535.  
  536.          OTHER OPTIONS:
  537.          xnum Set maximum depth for evidence search
  538.  
  539.      These options were only ever used for my own research and were
  540.      (intentionally) undocumented, so it seemed sensible to remove them
  541.      from the distributed system.  A quick patch to the source code and
  542.      a recompilation is all that is necessary to reinstate these
  543.      options; useful if somebody out there found out about these
  544.      options and actually uses them (if you do, I'd love to know
  545.      why!).
  546.  
  547.  
  548. 2.3  Commands
  549. -------------
  550. The full list of commands that can be used from within the Gofer
  551. interpreter are summarized using the command :? as follows:
  552.  
  553.     ? :?
  554.     LIST OF COMMANDS:  Any command may be abbreviated to :c where
  555.     c is the first character in the full name.
  556.  
  557.     :load <filenames>   load scripts from specified files
  558.     :load               clear all files except prelude
  559.     :also <filenames>   read additional script files
  560.     :reload             repeat last load command
  561.     :project <filename> use project file
  562.     :edit <filename>    edit file
  563.     :edit               edit last file
  564.     <expr>              evaluate expression
  565.     :type <expr>        print type of expression
  566.     :?                  display this list of commands
  567.     :set <options>      set command line options
  568.     :set                help on command line options
  569.     :names [pat]        list names currently in scope
  570.     :info <names>       describe named objects
  571.     :find <name>        edit file containing definition of name
  572.     :!command           shell escape
  573.     :cd dir             change directory
  574.     :quit               exit Gofer interpreter
  575.     ?
  576.  
  577. Almost all of these commands are the same as in the previous release.
  578. The only new features are listed in the following sections.
  579.  
  580.  
  581. 2.3.1  Shell escapes
  582. --------------------
  583. The shell escape command :! is used to enable you to run other programs
  584. from within the Gofer interpreter.  For example, on a Unix system, you
  585. can print a list of all the files in the current directory by typing:
  586.  
  587.     ? :!ls
  588.     <list of all files in current directory gets printed here>
  589.     ?
  590.  
  591.  
  592.                                       9
  593.  
  594.  
  595.  
  596.  
  597. Release Notes v2.28                               2.3.1  Shell escapes
  598.  
  599.  
  600. The same thing can be achieved on a PC running DOS by typing:
  601.  
  602.     ? :!dir
  603.     <list of all files in current directory gets printed here>
  604.     ?
  605.  
  606. This is the same as in previous releases of Gofer; the only difference
  607. is that there is no longer any need to type a space between the :!
  608. command and the shell command that follows it.  In fact, there is no
  609. longer any need to type the leading colon either.  Thus the two commands
  610. above could equally well have been entered as:
  611.  
  612.     !ls
  613.     !dir
  614.  
  615. To start a new shell from within Gofer, you can use the command :! or the
  616. abbreviated form ! -- in Unix and DOS you can return to the Gofer system
  617. by entering the shell command `exit'.  This is likely to be different if
  618. you use Gofer on other systems.
  619.  
  620.  
  621. 2.3.2  Information about named values
  622. -------------------------------------
  623. The :info command is a new feature which is useful for obtaining
  624. information about the values currently loaded into a Gofer session.  It
  625. can be used to display information about all kinds of different values
  626. including:
  627.  
  628.   o  Datatypes:  The name of the datatype and a list of its associated
  629.      constructor functions is printed:
  630.  
  631.          ? :info Request
  632.          -- type constructor
  633.          data Request
  634.  
  635.          -- constructors:
  636.          ReadFile :: String -> Request
  637.          WriteFile :: String -> String -> Request
  638.          AppendFile :: String -> String -> Request
  639.          ReadChan :: String -> Request
  640.          AppendChan :: String -> String -> Request
  641.          Echo :: Bool -> Request
  642.          GetArgs :: Request
  643.          GetProgName :: Request
  644.          GetEnv :: String -> Request
  645.  
  646.          ?
  647.  
  648.   o  Type synonyms:  Prints the name and expansion of the synonym:
  649.  
  650.          ? :info Dialogue
  651.          -- type constructor
  652.          type Dialogue = [Response] -> [Request]
  653.  
  654.          ?
  655.  
  656.  
  657.  
  658.                                       10
  659.  
  660.  
  661.  
  662.  
  663. Release Notes v2.28              2.3.2  Information about named values
  664.  
  665.  
  666.      If the type synonym is restricted (see Section 3.1) then the
  667.      expansion is not included in the output:
  668.  
  669.          ? :info Stack      
  670.          -- type constructor
  671.          type Stack a = <restricted>
  672.  
  673.          ?
  674.  
  675.   o  Type classes: Lists the type class name, superclasses, member
  676.      functions and instances:
  677.  
  678.          ? :info Eq
  679.          -- type class
  680.          class Eq a where
  681.              (==) :: Eq a => a -> a -> Bool
  682.              (/=) :: Eq a => a -> a -> Bool
  683.  
  684.          -- instances:
  685.          instance Eq ()
  686.          instance Eq Int
  687.          instance Eq Float
  688.          instance Eq Char
  689.          instance Eq a => Eq [a]
  690.          instance (Eq a, Eq b) => Eq (a,b)
  691.          instance Eq Bool
  692.  
  693.          ?
  694.  
  695.      Note that the member functions listed for the class include the
  696.      class predicate as part of the type; the output is not intended
  697.      to be thought of as a syntactically valid class declaration.
  698.  
  699.      Overlapping instance declarations (see Section 3.2) are listed in
  700.      increasing order of generality.
  701.  
  702.   o  Other values: for example, named functions and individual
  703.      constructor and member functions:
  704.  
  705.          ? :info map : <=
  706.          map :: (a -> b) -> [a] -> [b]
  707.  
  708.          (:) :: a -> [a] -> [a]   -- data constructor
  709.  
  710.          (<=) :: Ord a => a -> a -> Bool   -- class member
  711.  
  712.          ?
  713.  
  714. As the last example shows, the :info command can take several arguments
  715. and prints out information about each in turn.  A warning message is
  716. displayed if there are no known references to an argument:
  717.  
  718.     ? :info (:)
  719.     Unknown reference `(:)'
  720.     ?
  721.  
  722.  
  723.  
  724.                                       11
  725.  
  726.  
  727.  
  728.  
  729. Release Notes v2.28              2.3.2  Information about named values
  730.  
  731.  
  732. This illustrates that the arguments are treated as textual names for
  733. operators, not syntactic expressions (for example, identifiers). The
  734. type of the (:) operator can be obtained by giving the command :info :
  735. as above.  There is no provision for including wildcard characters of
  736. any form in the arguments of :info commands.
  737.  
  738. If a particular argument can be interpreted as, for example, a
  739. constructor function, or a type constructor depending on context, both
  740. possibilities are displayed.  For example, loading a program containing
  741. the definition:
  742.  
  743.     data Set a = Set [a]
  744.  
  745. We obtain:
  746.  
  747.     ? :info Set        
  748.     -- type constructor
  749.     data Set a
  750.  
  751.     -- constructors:
  752.     Set :: [a] -> Set a
  753.  
  754.     Set :: [a] -> Set a   -- data constructor
  755.  
  756.     ?
  757.  
  758. If no arguments are supplied to :info, a list of all the script files
  759. currently loaded into the interpreter will be displayed:
  760.  
  761.     ? :info
  762.  
  763.     Gofer session for:
  764.     /usr/local/lib/Gofer/standard.prelude
  765.     examples
  766.     ?
  767.  
  768.  
  769. 2.4  Literate scripts
  770. ---------------------
  771. Support for literate scripts -- files in which program lines begin with
  772. a `>' character and all other lines are treated as comments -- was
  773. provided in previous versions of Gofer.  The command line option
  774. :set +l was used to force Gofer to treat each input file as a literate
  775. script, while :set -l (the default) was used to treat each input file
  776. as a standard script of definitions.
  777.  
  778. In practice, this turned out to be somewhat inconvenient, particularly
  779. when loading combinations of files, some as literate scripts, some
  780. without.  For example, quite a few people kept two versions of the
  781. prelude, one as a literate script, one not, so that they wouldn't have
  782. to fiddle with the settings or using the :set commands to load files.
  783.  
  784. Gofer version 2.28 now uses a more sophisticated scheme to determine
  785. how an input script file should be treated, based on the use of file
  786. extensions.  More specifically, any script file with a name ending in
  787. one of the following suffixes:
  788.  
  789.  
  790.                                       12
  791.  
  792.  
  793.  
  794.  
  795. Release Notes v2.28                              2.4  Literate scripts
  796.  
  797.  
  798.     .hs     .has    .gs     .gof    .prelude
  799.  
  800. will always be loaded as a normal (i.e. non-literate) script file,
  801. regardless of the setting of the l command line option.  In a similar
  802. way, files with names ending in one of the following suffixes:
  803.  
  804.     .lgs    .lhs    .verb   .lit
  805.  
  806. will always be treated as literate scripts.  The command line option l
  807. is only used for files with names not ending in one of the above
  808. suffixes.
  809.  
  810. For example, the commands:
  811.  
  812.     :set -l
  813.     :load prog1.gs prog2 prog3.lgs
  814.  
  815. will load prog1.gs and prog2 as non-literate scripts, and then load
  816. prog3.lhs as a literate script.
  817.  
  818.  
  819. 2.5  Prelude files
  820. ------------------
  821. The Gofer system comes with a standard prelude, and a small number of
  822. alternative preludes.  These have always been there, but a lot of
  823. people don't seem to have noticed these, so I thought I'd say a few
  824. words about the different preludes included with Gofer: Remember that
  825. you can always change the prelude you are using by setting the GOFER
  826. environment variable or by modifying a startup script as described in
  827. Section 2.1:
  828.  
  829.     standard.prelude    The standard Gofer prelude, using type classes
  830.                         and providing the familiar range of operators
  831.                         and functions.
  832.  
  833.     nofloat.prelude     A simplified version of the standard.prelude
  834.                         which does not include any floating point
  835.                         operators.  This is likely to be of most use
  836.                         for those using Gofer on PCs where memory is
  837.                         at a premium; compiling a version of the
  838.                         interpreter (or compiler runtime library)
  839.                         without floating point support can give an
  840.                         important saving.
  841.  
  842.     simple.prelude      A prelude file based on the standard prelude
  843.                         but without type classes.  Let me emphasize
  844.                         that point: YOU CAN USE GOFER WITHOUT HAVING
  845.                         TO LEARN ABOUT TYPE CLASSES :-)  Some people
  846.                         seem to take to the use of type classes right
  847.                         from the beginning.  For those that have
  848.                         problems understanding the technical details
  849.                         or even the motivation, the simple.prelude
  850.                         can be used to get you familiar with the syntax
  851.                         of the language and the basic principles.
  852.                         Then you can move up to the standard.prelude
  853.                         when you're ready.  The principle differences
  854.  
  855.  
  856.                                       13
  857.  
  858.  
  859.  
  860.  
  861. Release Notes v2.28                                 2.5  Prelude files
  862.  
  863.  
  864.                         can be described by listing the types of
  865.                         commonly used operators in the simple.prelude:
  866.  
  867.                               (==) :: a -> a -> Bool
  868.                               (<=) :: a -> a -> Bool
  869.                               (<)  :: a -> a -> Bool
  870.                               (>=) :: a -> a -> Bool
  871.                               (>)  :: a -> a -> Bool
  872.                               (/=) :: a -> a -> Bool
  873.                               show :: a -> String
  874.                               (+)  :: Int -> Int -> Int
  875.                               (-)  :: Int -> Int -> Int
  876.                               (*)  :: Int -> Int -> Int
  877.                               (/)  :: Int -> Int -> Int
  878.  
  879.                         The resulting language is closer to the system
  880.                         in Bird and Wadler (and can be made closer
  881.                         still by editing the simple.prelude to use
  882.                         zipwith instead of zipWith etc...).
  883.  
  884.     cc.prelude          An extended version of the standard.prelude
  885.                         including support for a number of useful
  886.                         constructor classes.  Most of the examples
  887.                         and applications described in Section 4 are
  888.                         based on this prelude.
  889.  
  890.     min.prelude         A minimal prelude file.  If you really want to
  891.                         build a very small prelude for a particular
  892.                         application, start with this and add the extra
  893.                         things that you need.
  894.  
  895. As you can see, the standard extension for prelude files is .prelude
  896. and any file ending with this suffix will be read as a non-literate
  897. script (as described in Section 2.4).  Note that, even if you are using
  898. a computer where the full name of a prelude file is not stored (for
  899. example, on a DOS machine the standard.prelude file becomes
  900. STANDARD.PRE) you should still specify the prelude file by its full
  901. name to ensure that the Gofer system treats it correctly as a prelude
  902. file.
  903.  
  904. You are also free to construct your own prelude files, typically by
  905. modifying one of the supplied preludes described above.  Anyone who
  906. created prelude files for use with previous releases of Gofer will need
  907. to edit these files to ensure that they will work correctly.  Note in
  908. particular that there is no longer any need to include definitions of
  909. the I/O datatypes in programs.  Furthermore, the error function should
  910. now be bound to the primitive "primError" rather than using the old
  911. definition of error s | False = error s.
  912.  
  913.  
  914. 3. LANGUAGE DIFFERENCES
  915.  
  916. This section outlines a number of small differences and extensions to
  917. the language used by Gofer.  These features are not included in the
  918. definition of Haskell, so you shouldn't be thinking that programs
  919. written using these features can ultimately be used with a full Haskell
  920.  
  921.  
  922.                                       14
  923.  
  924.  
  925.  
  926.  
  927. Release Notes v2.28                            3. LANGUAGE DIFFERENCES
  928.  
  929.  
  930. system.  The use of constructor classes -- a more substantial change is
  931. described in Section 4.
  932.  
  933. 3.1  Restricted type synonyms
  934. -----------------------------
  935. Gofer 2.28 supports a form of restricted type synonym that can be used
  936. to restrict the expansion of the synonym to a particular set of
  937. functions.  Outside of the selected group of functions, the synonym
  938. constructor behaves like a standard datatype.  More precisely, a
  939. restricted type synonym definition is a top level declaration of the
  940. form:
  941.  
  942.     type T a1 ... am = rhs in f1, ..., fn
  943.  
  944. where T is the name of the restricted type synonym constructor and rhs
  945. is a type expression typically involving some of the (distinct) type
  946. variables a1, ..., am.  The same kind of restrictions that apply to
  947. normal type synonym declarations are also applied here.  The major
  948. difference is that the expansion of the type synonym can only be used
  949. within the binding group of one of the functions f1, ..., fn (all of
  950. which must be defined by top-level definitions in the file containing
  951. the restricted type synonym definition).  In the definition of any
  952. other function, the type constructor T is treated as if it had been
  953. introduced by a definition of the form:
  954.  
  955.     data T a1 ... am = ...
  956.  
  957. The original motivation for restricted type synonyms came from my work
  958. with constructor classes as described in Section 4 and you will several
  959. examples of this in the ccexamples.gs file in the demos/Ccexamples
  960. directory of the standard distribution.  For a simpler example,
  961. consider the following definition of a datatype of stacks in terms of
  962. the standard list type:
  963.  
  964.     type Stack a = [a] in emptyStack, push, pop, top, isEmpty
  965.  
  966. The definitions for the five functions named here are as follows:
  967.  
  968.     emptyStack :: Stack a
  969.     emptyStack  = []
  970.  
  971.     push       :: a -> Stack a -> Stack a
  972.     push        = (:)
  973.  
  974.     pop        :: Stack a -> Stack a
  975.     pop []      = error "pop: empty stack"
  976.     pop (_:xs)  = xs
  977.  
  978.     top        :: Stack a -> a
  979.     top []      = error "top: empty stack"
  980.     top (x:_)   = x
  981.  
  982.     isEmpty    :: Stack a -> Bool
  983.     isEmpty     = null
  984.  
  985. The type signatures here are particularly important.  For example,
  986.  
  987.  
  988.                                       15
  989.  
  990.  
  991.  
  992.  
  993. Release Notes v2.28                      3.1  Restricted type synonyms
  994.  
  995.  
  996. since emptyStack is mentioned in the definition of the restricted type
  997. synonym Stack, the definition of emptyStack is type correct.  The
  998. declared type for emptyStack is Stack a which can be expanded to [a],
  999. agreeing with the type for the empty list [].  However, in an expression
  1000. outside the binding group of these functions, the Stack a type is quite
  1001. distinct from the [a] type:
  1002.  
  1003.     ? emptyStack ++ [1]
  1004.     ERROR: Type error in application
  1005.     *** expression     : emptyStack ++ [1]
  1006.     *** term           : emptyStack
  1007.     *** type           : Stack a
  1008.     *** does not match : [Int]
  1009.  
  1010.     ?
  1011.  
  1012. The `binding group' of a value refers to the set of values whose
  1013. definitions are in the same mutually recursive group of bindings.  In
  1014. particular, this does not extend to the type class system so we can
  1015. define instances such as:
  1016.  
  1017.     instance Eq a => Eq (Stack a) where
  1018.         s1 == s2 | isEmpty s1 = isEmpty s2
  1019.                  | isEmpty s2 = isEmpty s1
  1020.                  | otherwise  = top s1 == top s2 && pop s1 == pop s2
  1021.  
  1022. As a convenience, Gofer allows the type signatures of functions
  1023. mentioned in the type synonym declaration to be specified within the
  1024. definition rather than in a different point in the script.  Thus the
  1025. example above could equally well have been written as:
  1026.  
  1027.     type Stack a = [a] in
  1028.         emptyStack :: Stack a,
  1029.         push       :: a -> Stack a -> Stack a,
  1030.         pop        :: Stack a -> Stack a,
  1031.         top        :: Stack a -> a,
  1032.         isEmpty    :: Stack a -> Bool
  1033.  
  1034.     emptyStack  = []
  1035.  
  1036.     push        = (:)
  1037.  
  1038.     pop []      = error "pop: empty stack"
  1039.     pop (_:xs)  = xs
  1040.  
  1041.     top []      = error "top: empty stack"
  1042.     top (x:_)   = x
  1043.  
  1044.     isEmpty     = null
  1045.  
  1046. However, the first form is necessary when you want to define two or
  1047. more restricted type synonyms simultaneously.  For example:
  1048.  
  1049.     type Pointer = Int in allocate, deref, assign
  1050.     type Heap a  = [a] in newHeap, allocate, deref, assign
  1051.     newHeap  :: Heap a
  1052.  
  1053.  
  1054.                                       16
  1055.  
  1056.  
  1057.  
  1058.  
  1059. Release Notes v2.28                      3.1  Restricted type synonyms
  1060.  
  1061.  
  1062.     allocate :: Heap a -> (Heap a, Pointer)
  1063.     deref    :: Heap a -> Pointer -> a
  1064.     assign   :: Heap a -> Pointer -> a -> Heap a
  1065.     etc ...
  1066.  
  1067. The use of restricted type synonyms doesn't quite provide proper
  1068. abstract data types.  For example, if you try:
  1069.  
  1070.     ? push 1 emptyStack
  1071.     [1]
  1072.     (5 reductions, 11 cells)
  1073.     ?
  1074.  
  1075. then the structure of the stack as a list of values is revealed by the
  1076. printing mechanism.  This happens because Gofer uses the show' function
  1077. to print out a value (in this case of type Stack Int) which looks inside
  1078. the structure of the object to see how it is represented.  This happens
  1079. to be most convenient for use in an interpreter as an aid to debugging.
  1080. For the purists (and the preservation of abstraction), Gofer could be
  1081. modified to apply the (overloaded) show function to printed values.
  1082. This would force the programmer to define the way in which stack values
  1083. are printed (distinct from lists) and preserve the abstraction.  Without
  1084. having set up this machinery, we get:
  1085.  
  1086.     ? show (push 1 emptyStack)
  1087.     ERROR: Cannot derive instance in expression
  1088.     *** Expression        : show (push 1 emptyStack)
  1089.     *** Required instance : Text (Stack Int)
  1090.  
  1091.     ?
  1092.  
  1093. The Gofer compiler described in Section 5 does not implement show' and
  1094. hence enforces the abstraction.
  1095.  
  1096.  
  1097. 3.2  Overlapping instance declarations
  1098. --------------------------------------
  1099. This section describes a somewhat technical extension, aimed at those
  1100. who work with type classes.  Many readers may prefer to skip to the
  1101. next section at this point.
  1102.  
  1103. The definition of Haskell and previous versions of Gofer insist that no
  1104. two instance declarations for a given class may contain overlapping
  1105. predicates.  Thus the declarations:
  1106.  
  1107.     class CX a where c :: a -> Int
  1108.  
  1109.     instance CX (a,Int) where c (x,y) = y
  1110.     instance CX (Int,a) where c (x,y) = x
  1111.  
  1112. are not allowed because the two predicates overlap:
  1113.  
  1114.     ERROR "misctest" (line 346): Overlapping instances for class "CX"
  1115.     *** This instance   : CX (Int,a)
  1116.     *** Overlaps with   : CX (a,Int)
  1117.     *** Common instance : CX (Int,Int)
  1118.  
  1119.  
  1120.                                       17
  1121.  
  1122.  
  1123.  
  1124.  
  1125. Release Notes v2.28             3.2  Overlapping instance declarations
  1126.  
  1127.  
  1128. As the error message indicates, given an expression c (1,2) it is not
  1129. clear whether we should use the first or the second instance
  1130. declarations to evaluate this, with potentially different results, 2 or
  1131. 1 respectively.
  1132.  
  1133. On the other hand, there are cases where this sort of thing might be
  1134. quite reasonable.  For example, the standard function show prints lists
  1135. of characters as strings, but any other kind of list is printed using
  1136. the [ ... ] notation with the items separated by commas:
  1137.  
  1138.     ? show "Hello"
  1139.     "Hello"
  1140.     ? show [True,False,True]
  1141.     [True,False,True]
  1142.     ? show [1..10]
  1143.     [1,2,3,4,5,6,7,8,9,10]
  1144.     ?
  1145.  
  1146. Haskell deals with this by an encoding using the showList function, but
  1147. a more obvious approach might be to define two instances:
  1148.  
  1149.     instance Text a => Text [a] where ... print using [ ... ] notation
  1150.     instance Text [Char] where ...        print as string
  1151.  
  1152. Other examples might include providing optimized versions of primitives
  1153. for particular frequently use operators, or providing a default
  1154. behaviour as in:
  1155.  
  1156.     class Eq a where (==) = error "no definition of equality specified"
  1157.  
  1158. Haskell requires the context of an overloaded function to be reduced to
  1159. a form where the only predicates that it contains are of the form C a.
  1160. This means that the inferred type of an object may be simplified before
  1161. the full type of that object is known.  For example, we might define a
  1162. function:
  1163.  
  1164.      f x = show [x,x]
  1165.  
  1166. The inferred type in Haskell is f :: Text a => a -> String and the
  1167. decision about which of the two instance declarations above should be
  1168. used has already been forced on us.  To see this, note that f 'a' would
  1169. evaluate to the string "['a', 'a']".  But if we allowed the second
  1170. instance declaration above to be used, show ['a', 'a'] would evaluate
  1171. to "aa".  This breaks a fundamental property of the language where we
  1172. expect to be able to replace one subexpression with another equal term
  1173. and obtain the same result.
  1174.  
  1175. In Gofer, the type system is a little different and the inferred type
  1176. is f :: Text [a] => a -> String.  The decision about which instance
  1177. declaration to use is postponed until the type assigned to 'a' is
  1178. known.  Thus both f 'a' and show ['a', 'a'] evaluate to "aa" without
  1179. any contradiction.
  1180.  
  1181. Although the type system in Gofer has always been able to support the
  1182. use of certain overlapping instance declarations, previous versions of
  1183. the system imposed stronger static restrictions which prohibited their
  1184.  
  1185.  
  1186.                                       18
  1187.  
  1188.  
  1189.  
  1190.  
  1191. Release Notes v2.28             3.2  Overlapping instance declarations
  1192.  
  1193.  
  1194. use.  Gofer 2.28 relaxes these restrictions by allowing a program to
  1195. contain overlapping instance declarations so long as:
  1196.  
  1197.   o  One of the instance predicates being declared is a substitution
  1198.      instance of the other.  Thus:
  1199.  
  1200.          instance Eq [Char] where ...    -- OK
  1201.          instance Eq a => Eq [a] where ...
  1202.  
  1203.      is permitted because the second predicate, Eq [a], is more general
  1204.      than the first, Eq [Char], which can be obtained by substituting
  1205.      Char for the type variable a.  However, the example at the
  1206.      beginning of this section:
  1207.  
  1208.          instance CX (a,Int) where ...   -- ILLEGAL
  1209.          instance CX (Int,a) where ...
  1210.  
  1211.      is not allowed since neither (a,Int) or (Int,a) is a substitution
  1212.      instance of the other (even though they have a common instance
  1213.      (Int,Int)).
  1214.  
  1215.   o  The two instances declared are not identical.  This rules out
  1216.      examples like:
  1217.  
  1218.          instance Eq Char where ...    -- ILLEGAL
  1219.          instance Eq Char where ...
  1220.  
  1221. The features described here are added principally for experimentation.
  1222. I have some particular applications that I want to try out (which is
  1223. why I actually implemented these ideas) but I would also be very
  1224. interested to hear from anyone else that makes use of this extension.
  1225.  
  1226.  
  1227. 3.3  Parsing Haskell syntax
  1228. ---------------------------
  1229. From correspondence that I have received, quite a few people use Gofer
  1230. to develop programs which, ultimately, will be compiled and executed
  1231. using a Haskell system.  Although the syntax of the two languages is
  1232. quite similar, it has been necessary to comment out module headers and
  1233. other constructs in Haskell programs before they could be used with
  1234. previous version of Gofer.
  1235.  
  1236. The new version of the Gofer system is now able to parse these
  1237. additional constructs (and will generate an error message if a syntax
  1238. error occurs).  However: NO ATTEMPT IS MADE TO INTERPRET OR USE THE
  1239. INFORMATION PROVIDED BY THESE ADDITIONAL CONSTRUCTS.  This feature is
  1240. provided purely for the convenience of those people using Gofer and
  1241. Haskell in the manner described above.  Gofer does not currently
  1242. support any notion of modules beyond the use of separate script files.
  1243.  
  1244. The following changes have been made:
  1245.  
  1246.   o  The identifiers:
  1247.  
  1248.          deriving     default      module      interface
  1249.          import       renaming     hiding      to
  1250.  
  1251.  
  1252.                                       19
  1253.  
  1254.  
  1255.  
  1256.  
  1257. Release Notes v2.28                        3.3  Parsing Haskell syntax
  1258.  
  1259.  
  1260.      are now reserved words in Gofer.  Any program that uses one of
  1261.      these as an identifier with an older version of Gofer will need
  1262.      to be modified to use a different name instead.
  1263.  
  1264.   o  Module headers and import declarations may be included in a Gofer
  1265.      program using the syntax set out in version 1.2 of the Haskell
  1266.      report.  Several modules may be included in a single file (but of
  1267.      course, Gofer makes no distinction between the sections of code
  1268.      appearing in different `modules').
  1269.  
  1270.   o  Datatype definitions may include deriving clauses such as:
  1271.  
  1272.          data Maybe a = Just a | Nothing deriving (Eq, Text)
  1273.  
  1274.      although no derived instances will actually be generated.
  1275.      If you need these facilities, you might consider writing out
  1276.      the instances of the type classes concerned yourself in a
  1277.      separate file which can be loaded when you run your program
  1278.      with Gofer, but which are omitted when you compile it with a
  1279.      proper Haskell system.
  1280.  
  1281.   o  Programs may include default declarations, although, once again,
  1282.      these are ignored; for example, there is no restriction on the
  1283.      forms of type that can be included in a default declaration, nor
  1284.      will an error occur if a single module includes multiple default
  1285.      declarations.
  1286.  
  1287.  
  1288. 3.4  Local definitions in comprehensions
  1289. ----------------------------------------
  1290. We all make mistakes.  The syntax for Gofer currently permits a local
  1291. definition to appear in a list comprehension (and indeed, in the monad
  1292. comprehensions described in the next section):
  1293.  
  1294.     [ (x,y) | x <- xs, y = f x, p y ]
  1295.  
  1296. This example is implemented by translating it to something equivalent
  1297. to:
  1298.  
  1299.     map h xs where h []     = []
  1300.                    h (x:xs) = let y = f x
  1301.                               in  if p y then (x,y) : h xs
  1302.                                          else h xs
  1303.  
  1304. It is cumbersome to rewrite this using list comprehensions without
  1305. local definitions:
  1306.  
  1307.     concat [ let y = f x in [ (x,y) | p y ] | x <- xs ]
  1308.  
  1309. so we might resort to the `hack' of writing:
  1310.  
  1311.     [ y | x <- xs, y <- [f x], p y ]
  1312.  
  1313. which works (but doesn't extend to recursive bindings, and is really an
  1314. inappropriate use for a list; a list is used to represent a sequence of
  1315. zero or more objects, so using a list when you know that there is
  1316.  
  1317.  
  1318.                                       20
  1319.  
  1320.  
  1321.  
  1322.  
  1323. Release Notes v2.28           3.4  Local definitions in comprehensions
  1324.  
  1325.  
  1326. always going to be exactly one element seems unnecessary).  So, to
  1327. summarize, I still think that local definitions can be useful in
  1328. comprehensions.
  1329.  
  1330. So where is the mistake I mentioned?  The problem is with the SYNTAX.
  1331. First, it is rather easy to confuse the comprehension above with the
  1332. comprehension:
  1333.  
  1334.     [ (x,y) | x <- xs, y == f x, p y ],
  1335.  
  1336. leading to errors which are hard to detect.  The second is that the
  1337. syntax is too restrictive; you can only give relatively simple local
  1338. declarations -- mutually recursive definitions and function bindings
  1339. are not permitted.
  1340.  
  1341. Gofer 2.28 now supports a new syntax for local definitions in
  1342. comprehensions.  The old syntax is still supported, for compatibility
  1343. with previous releases, but will be deleted in the next public release
  1344. (assuming I remember).  Local declarations can now be included in a
  1345. comprehension using a qualifier of the form let { decls }.  So the
  1346. comprehension at the beginning of this section can also be written:
  1347.  
  1348.     [ (x,y) | x <- xs, let {y = f x}, p y ]
  1349.  
  1350. Note that the braces cannot usually be omitted in Gofer due to an
  1351. undocumented extension to the syntax of Gofer function declarations.
  1352. The braces would not be needed if this syntax were added to a standard
  1353. Haskell system.
  1354.  
  1355. This extension means that it is now possible to write comprehensions
  1356. such as:
  1357.  
  1358.     [ (x,y,z) | x <- xs, let { y   = f x z;
  1359.                                z   = g x y;
  1360.                                f n = h n [] }, p x y z ]
  1361.      
  1362. Once again, this is still an experimental feature.  I suspect it will
  1363. be of most use to anyone making substantial use of monad comprehensions
  1364. as described in the next section.
  1365.  
  1366.  
  1367. 4. CONSTRUCTOR CLASSES
  1368.  
  1369. [This is a long section; if you are not interested in experimenting
  1370. with Gofer's system of constructor classes, you can skip straight ahead
  1371. to the next section without missing anything.  Of course, if you don't
  1372. know what a constructor class is, you might want to read at least some
  1373. of this section before you can make that decision.]
  1374.  
  1375. One of the biggest changes in Gofer version 2.28 is the provision of
  1376. support for constructor classes.  This section provides an overview of
  1377. constructor classes which should hopefully, in conjunction with the
  1378. example supplied with the full distribution, be enough to get you
  1379. started.  More technical details about constructor classes can be
  1380. obtained by contacting me.
  1381.  
  1382.  
  1383.  
  1384.                                       21
  1385.  
  1386.  
  1387.  
  1388.  
  1389. Release Notes v2.28                             4. CONSTRUCTOR CLASSES
  1390.  
  1391.  
  1392. Some of the following introduction here (particularly sections 4.1 and
  1393. 4.2) may seem somewhat familiar to those of you have already read one
  1394. of the papers that I have written on the subject although I have added
  1395. some more information about the Gofer implementation.
  1396.  
  1397. Others may find that this section of the documentation seems rather
  1398. technical; try not to be put off at first sight.  Looking through the
  1399. examples and the documentation, you may find it is easier to understand
  1400. than you expect!
  1401.  
  1402. A final comment before starting: there is, as yet, no strong consensus
  1403. on the names and syntax that would be best for monad operations,
  1404. comprehensions etc.  If you have any opinions, or proposals which
  1405. differ from what you see here, please let me know ... I'd be very
  1406. interested to hear other people's opinions on this.
  1407.  
  1408.  
  1409. 4.1  An overloaded map function
  1410. -------------------------------
  1411. Many functional programs use the map function to apply a function to
  1412. each of the elements in a given list.  The type and definition of this
  1413. function as given in the Gofer standard prelude are as follows:
  1414.  
  1415.     map          ::  (a -> b) -> ([a] -> [b])
  1416.     map f []      =  []
  1417.     map f (x:xs)  =  f x : map f xs
  1418.  
  1419. It is well known that the map function satisfies the familiar laws:
  1420.  
  1421.     map id         = id
  1422.     map f . map g  =  map (f . g)
  1423.  
  1424. A category theorist will recognize these observations as indicating
  1425. that there is a functor from types to types whose object part maps any
  1426. given type a to the list type [a] and whose arrow part maps each
  1427. function f::a -> b to the function map f :: [a] -> [b].  A functional
  1428. programmer will recognize that similar constructions are also used with
  1429. a wide range of other data types, as illustrated by the following
  1430. examples:
  1431.  
  1432.     data Tree a  =  Leaf a  |  Tree a :^: Tree a
  1433.  
  1434.     mapTree            :: (a -> b) -> (Tree a -> Tree b)
  1435.     mapTree f (Leaf x)  = Leaf (f x)
  1436.     mapTree f (l :^: r) = mapTree f l :^: mapTree f r
  1437.  
  1438.     data Maybe a =  Just a  |  Nothing
  1439.  
  1440.     mapMaybe           :: (a -> b) -> (Maybe a -> Maybe b)
  1441.     mapMaybe f (Just x) = Just (f x)
  1442.     mapMaybe f Nothing  = Nothing
  1443.  
  1444. Each of these functions has a similar type to that of the original map
  1445. and also satisfies the functor laws given above.  With this in mind, it
  1446. seems a shame that we have to use different names for each of these
  1447. variants.
  1448.  
  1449.  
  1450.                                       22
  1451.  
  1452.  
  1453.  
  1454.  
  1455. Release Notes v2.28                    4.1  An overloaded map function
  1456.  
  1457.  
  1458. A more attractive solution would allow the use of a single name map,
  1459. relying on the types of the objects involved to determine which
  1460. particular version of the map function is required in a given
  1461. situation.  For example, it is clear that map (1+) [1,2,3] should be
  1462. a list, calculated using the original map function on lists, while
  1463. map (1+) (Just 1) should evaluate to Just 2 using mapMaybe.
  1464.  
  1465. Unfortunately, in a language using standard Hindley/Milner type
  1466. inference, there is no way to assign a type to the map function that
  1467. would allow it to be used in this way.  Furthermore, even if typing
  1468. were not an issue, use of the map function would be rather limited
  1469. unless some additional mechanism was provided to allow the definition
  1470. to be extended to include new datatypes perhaps distributed across a
  1471. number of distinct program files.
  1472.  
  1473.  
  1474. 4.1.1  An attempt to define map using type classes
  1475. --------------------------------------------------
  1476. The ability to use a single function symbol with an interpretation that
  1477. depends on the type of its arguments is commonly known as overloading.
  1478. In Gofer, overloading is implemented using type classes -- which can be
  1479. thought of as sets of types.  For example, the Eq class defined by:
  1480.  
  1481.     class Eq a where
  1482.         (==), (/=) :: a -> a -> Bool
  1483.  
  1484. (together with an appropriate set of instance declarations) is used to
  1485. describe the set of types whose elements can be compared for equality.
  1486. The standard prelude for Gofer includes integers, floating point
  1487. numbers, characters, booleans, lists (in which the type of the members
  1488. is also in Eq) and so forth.  There is no need for all the definitions
  1489. of equality to be combined in a single script file; new definitions of
  1490. equality are typically included each time a new datatype is defined.
  1491.  
  1492. Functions such as nub, defined in the standard prelude as:
  1493.  
  1494.     nub       :: Eq a => [a] -> [a]   -- remove duplicates from list
  1495.     nub []     = []
  1496.     nub (x:xs) = x : nub (filter (x/=) xs)
  1497.  
  1498. can be used with any choice of type for the type variable a so long as
  1499. it is an instance of Eq.  Only a single definition of the nub function
  1500. is required.
  1501.  
  1502. Unfortunately, the system of type classes is not sufficiently powerful
  1503. to give a satisfactory treatment for the map function; to do so would
  1504. require a class Map and a type expression m(t) involving the type
  1505. variable t such that S = { m(t) | t is a member of Map } includes (at
  1506. least) the types:
  1507.  
  1508.     { (a -> b) -> ([a] -> [b]),
  1509.       (a -> b) -> (Tree a -> Tree b),
  1510.       (a -> b) -> (Maybe a -> Maybe b), ....
  1511.                                     | a and b arbitrary types }
  1512.  
  1513.  
  1514.  
  1515.  
  1516.                                       23
  1517.  
  1518.  
  1519.  
  1520.  
  1521. Release Notes v2.28 4.1.1  An attempt to define map using type classes
  1522.  
  1523.  
  1524. The only possibility is to take m(t) = t and choose Map as the set of
  1525. types S for which the map function is required:
  1526.  
  1527.     class Map t where map :: t
  1528.  
  1529.     instance Map ((a -> b) -> ([a] -> [b])) where ...
  1530.     instance Map ((a -> b) -> (Tree a -> Tree b)) where ...
  1531.     instance Map ((a -> b) -> (Maybe a -> Maybe b)) where ...
  1532.  
  1533. This syntax is permitted in Gofer (but not in Haskell) but it does not
  1534. give a sufficiently accurate characterization of the type of map to be
  1535. of much use.  For example, the principal type of \i j -> map j . map i
  1536. is:
  1537.  
  1538.     (Map (a -> c -> e), Map (b -> e -> d)) => a -> b -> c -> d
  1539.  
  1540. (a and b are the types of i and j respectively).  This is complicated
  1541. and does not enforce the condition that i and j have function types.
  1542. Furthermore, the type is ambiguous (the type variable e does not appear
  1543. to the right of the => symbol or in the assumptions).  Under these
  1544. conditions, we cannot guarantee a well-defined semantics for this
  1545. expression.  Other attempts to define the map function, for example
  1546. using multiple parameter type classes, have also failed for essentially
  1547. the same reasons.
  1548.  
  1549.  
  1550. 4.1.2  A solution using constructor classes
  1551. -------------------------------------------
  1552. A much better approach is to notice that each of the types for which
  1553. the map function is required is of the form:
  1554.  
  1555.     (a -> b) -> (f a -> f b).
  1556.  
  1557. The variables a and b here represent arbitrary types while f ranges
  1558. over the set of type constructors for which a suitable map function has
  1559. been defined.  In particular, we would expect to include the list
  1560. constructor (which we write as [] in Gofer), Tree and Maybe as elements
  1561. of this set.  Motivated by our earlier comments we will call this set
  1562. Functor.  With only a small extension to the Gofer syntax for type
  1563. classes this can be described by:
  1564.  
  1565.     class Functor f where
  1566.         map :: (a -> b) -> (f a -> f b)
  1567.  
  1568.     instance Functor [] where
  1569.         map f []        = []
  1570.         map f (x:xs)    = f x : map f xs
  1571.  
  1572.     instance Functor Tree where
  1573.         map f (Leaf a)  = Leaf (f a)
  1574.         map f (l :^: r) = map f l :^: map f r
  1575.  
  1576.     instance Functor Maybe where
  1577.         map f (Just x) = Just (f x)
  1578.         map f Nothing  = Nothing
  1579.  
  1580.  
  1581.  
  1582.                                       24
  1583.  
  1584.  
  1585.  
  1586.  
  1587. Release Notes v2.28        4.1.2  A solution using constructor classes
  1588.  
  1589.  
  1590. Functor is our first example of a constructor class.  The following
  1591. extract illustrates how the definitions for Functor work in practice:
  1592.  
  1593.     ? map (1+) [1,2,3]
  1594.     [2, 3, 4]
  1595.     (15 reductions, 44 cells)
  1596.     ? map (1+) (Leaf 1 :^: Leaf 2)
  1597.     Leaf 2 :^: Leaf 3
  1598.     (10 reductions, 46 cells)
  1599.     ? map (1+) (Just 1)
  1600.     Just 2
  1601.     (4 reductions, 17 cells)
  1602.     ?
  1603.  
  1604. Furthermore, by specifying the type of map function more precisely,
  1605. we avoid the ambiguity problems mentioned above.  For example, the
  1606. principal type of \i j -> map j . map i is simply:
  1607.  
  1608.     Functor f => (a -> b) -> (b -> c) -> f a -> f c
  1609.  
  1610. which is not ambiguous, and makes the types of i and j as (a -> b)
  1611. and (b -> c) respectively.
  1612.  
  1613. [You can try these examples yourself using the Gofer system.  The first
  1614. thing you need to do is start Gofer using the file cc.prelude instead
  1615. of the usual Gofer standard.prelude.  The cc.prelude includes the
  1616. definition of the functor class and the instance for Functor [].  The
  1617. remaining two instance declarations are included (along with lots of
  1618. other examples) in the file ccexamples.gs in the demos/Ccexamples
  1619. subdirectory of the standard distribution.]
  1620.  
  1621.  
  1622. 4.1.3  The kind system
  1623. ----------------------
  1624. Each instance of Functor can be thought of as a function from types to
  1625. types.  It would be nonsense to allow the type Int of integers to be an
  1626. instance of Functor, since the type (a -> b) ->(Int a -> Int b) is
  1627. obviously not well-formed.  To avoid unwanted cases like this, we have
  1628. to ensure that all of the elements in any given class are of the same
  1629. kind.
  1630.  
  1631. To do this, we formalize the notion of kind, writing * for the kind of
  1632. all types and k1 -> k2 for the kind of a constructor which takes
  1633. something of kind k1 and returns something of kind k2.  This notion
  1634. comes is motivated by some theoretical work by Henk Barendregt on the
  1635. subject of `Generalized type systems'; Do not confuse this with the use
  1636. of the symbol * in a certain well-known functional language where it
  1637. represents a type variable.  These things are completely different!
  1638.  
  1639. Rather than thinking only of types we work with constructors which
  1640. include types as a special case.  Constructors take the form:
  1641.  
  1642.     Constructor  ::=  ConstructorConstant
  1643.                   |   Constructor1 Constructor2
  1644.                   |   variable
  1645.  
  1646.  
  1647.  
  1648.                                       25
  1649.  
  1650.  
  1651.  
  1652.  
  1653. Release Notes v2.28                             4.1.3  The kind system
  1654.  
  1655.  
  1656. This corresponds very closely to the way that most type expressions
  1657. are already written in Gofer.  For example, Tree a is an application
  1658. of the constructor constant Tree to the variable a.  Gofer has some
  1659. special syntax for tuple, list and function types.  The corresponding
  1660. constructors can also be written directly in Gofer.  For example:
  1661.  
  1662.     a -> b   =  (->) a b
  1663.     [a]      =  [] a
  1664.     (a,b)    =  (,) a b
  1665.     (a,b,c)  =  (,,) a b c
  1666.     etc ...
  1667.  
  1668. Each constructor constant has a corresponding kind.  For example:
  1669.  
  1670.     Int, Float, ()    ::  *
  1671.     [], Tree, Maybe   ::  * -> *
  1672.     (->), (,)         ::  * -> * -> *
  1673.     (,,)              ::  * -> * -> * -> *
  1674.  
  1675. Applying one constructor C :: k1 -> k2 to a construct C' :: k1 gives
  1676. a constructor expression C C' with kind k2.  Notice that this is just
  1677. the same sort of thing you would expect from applying a function of
  1678. type a -> b to an value of type b; kinds really are very much like
  1679. `types for constructors'.
  1680.  
  1681. Instead of checking that type expressions contain the correct number of
  1682. arguments for each type constructor, we need to check that any type
  1683. expression has kind *.  In a similar way, all of the elements of a
  1684. constructor class must have the same kind; for example, a constructor
  1685. class constraint of the form Functor f is only valid if f is a
  1686. constructor expression of kind * -> *.  Note also that our system
  1687. includes Gofer/Haskell type classes as a special case; a type class is
  1688. simply a constructor class for which each instance has kind *.  Multiple
  1689. parameter classes can also be dealt with in the same way, using a tuple
  1690. of kinds (k1,...,kn) to indicate the kind of constructors required for
  1691. each argument.
  1692.  
  1693. The language of constructors is essentially a system of combinators
  1694. without any reduction rules.  As such, standard techniques can be
  1695. used to infer the kinds of constructor variables, constructor constants
  1696. introduced by new datatype definitions and the kind of the elements
  1697. held in any particular constructor class.  The important point is that
  1698. there is no need -- and indeed, in our current implementation, no
  1699. opportunity -- for the programmer to supply kind information
  1700. explicitly.  We regard this as a significant advantage since it means
  1701. that the programmer can avoid much of the complexity that might
  1702. otherwise result from the need to annotate type expressions with
  1703. kinds.
  1704.  
  1705.  
  1706. 4.2  Monads as an application of constructor classes
  1707. ----------------------------------------------------
  1708. Motivated by the work of Moggi and Spivey, Wadler has proposed a style
  1709. of functional programming based on the use of monads.  While the theory
  1710. of monads had already been widely studied in the context of abstract
  1711. category theory, Wadler introduced the idea that monads could be used
  1712.  
  1713.  
  1714.                                       26
  1715.  
  1716.  
  1717.  
  1718.  
  1719. Release Notes v2.28 4.2  Monads as an application of constructor classes
  1720.  
  1721.  
  1722. as a practical method for modeling so-called `impure' features in a
  1723. purely functional programming language.
  1724.  
  1725. The examples in this and following sections illustrate that the use of
  1726. constructor classes can be particularly convenient for programming in
  1727. this style.  You will also find a lot more examples prepared for use
  1728. with Gofer in the file ccexamples in the demos/Ccexamples subdirectory
  1729. of the standard distribution.
  1730.  
  1731.  
  1732. 4.2.1  A framework for programming with monads
  1733. ----------------------------------------------
  1734. The basic motivation for the use of monads is the need to distinguish
  1735. between computations and the values that they produce.  If m is a monad
  1736. then an object of type (m a) represents a computation which is expected
  1737. to produce a value of type a.  These types reflect the fact that the
  1738. use of particular programming language features in a given calculation
  1739. is a property of the computation itself and not of the result that it
  1740. produces.
  1741.  
  1742. Taking the approach outlined by Wadler in his paper `The Essence of
  1743. Functional Programming' (POPL '92), we introduce a constructor class of
  1744. monads using the definition:
  1745.  
  1746.     class Functor m => Monad m where
  1747.         result    :: a -> m a
  1748.         join      :: m (m a) -> m a
  1749.         bind      :: m a -> (a -> m b) -> m b
  1750.  
  1751.         join x     = bind x id
  1752.         x `bind` f = join (map f x)
  1753.  
  1754. The expression Functor m => Monad m defines Monad as a subclass of
  1755. Functor ensuring that, for any given monad, there will also be a
  1756. corresponding instance of the overloaded map function.  The use of a
  1757. hierarchy of classes enables us to capture the fact that not every
  1758. instance of Functor can be treated as an instance of Monad in any
  1759. natural way.
  1760.  
  1761. [If you are familiar with either my previous papers or Wadler's
  1762. writings on the use of monads, you might notice that the declaration
  1763. above uses the name `result' in place of `return' or `unit' that have
  1764. been previously used for the same thing.  The latter two choices have
  1765. been used elsewhere for rather different purposes, and there is
  1766. currently no clear picture of which names should be used.  The
  1767. identifier `result' is the latest in a long line of attempts to find a
  1768. name which both conveys the appropriate meaning and is not already in
  1769. use for other applications.]
  1770.  
  1771. By including default definitions for bind and join we only need to give
  1772. a definition for one of these (in addition to a definition for result)
  1773. to completely define an instance of Monad.  This is often quite
  1774. convenient.  On the other hand, it would be an error to omit
  1775. definitions for both operators since the default definitions are
  1776. clearly circular.  We should also mention that the member functions in
  1777. an instance of Monad are expected to satisfy a number of laws which are
  1778.  
  1779.  
  1780.                                       27
  1781.  
  1782.  
  1783.  
  1784.  
  1785. Release Notes v2.28     4.2.1  A framework for programming with monads
  1786.  
  1787.  
  1788. not reflected in the class definition above.
  1789.  
  1790. The following declaration defines the standard monad structure for the
  1791. list constructor [] which can be used to describe computations
  1792. producing multiple results, corresponding to a simple form of
  1793. non-determinism:
  1794.  
  1795.     instance Monad [] where
  1796.         result x        = [x]
  1797.         []     `bind` f = []
  1798.         (x:xs) `bind` f = f x ++ (xs `bind` f)
  1799.  
  1800. As a second example, the monad structure for the Maybe datatype, which
  1801. might be used to describe computations which fail to produce any value
  1802. at all if an error condition occurs, can be described by:
  1803.  
  1804.     instance Monad Maybe where
  1805.         result x         = Just x
  1806.         Just x  `bind` f = f x
  1807.         Nothing `bind` f = Nothing
  1808.  
  1809. Another interesting use of monads is to model programs that make use of
  1810. an internal state.  Computations of this kind can be represented by
  1811. functions of type s-> (a,s) (often referred to as state transformers)
  1812. mapping an initial state to a pair containing the result and final
  1813. state.  In order to get this into the appropriate form for the Gofer
  1814. system of constructor classes, we introduce a new datatype:
  1815.  
  1816.     data State s a = ST (s -> (a,s))
  1817.  
  1818. The functor and monad structures for state transformers are as follows:
  1819.  
  1820.     instance Functor (State s) where
  1821.         map f (ST st) = ST (\s -> let (x,s') = st s in (f x, s'))
  1822.  
  1823.     instance Monad (State s) where
  1824.         result x      = ST (\s -> (x,s))
  1825.         ST m `bind` f = ST (\s -> let (x,s') = m s
  1826.                                       ST f'  = f x
  1827.                                   in  f' s')
  1828.  
  1829. Notice that the State constructor has kind * -> * -> * and that the
  1830. declarations above define State s as a monad and functor for any state
  1831. type s (and hence State s has kind * -> * as required for an instance
  1832. of these classes).  There is no need to assume a fixed state type.
  1833.  
  1834. From a user's point of view, the most interesting properties of a monad
  1835. are described, not by the result, bind and join operators, but by the
  1836. additional operations that it supports.  The following examples are
  1837. often useful when working with state monads.  The first can be used to
  1838. `run' a program given an initial state and discarding the final state,
  1839. while the second might be used to implement an integer counter in a
  1840. State Int monad:
  1841.  
  1842.     startingWith          :: State s a -> s -> a
  1843.     ST m `startingWith` s0 = result where (result,_) = m s0
  1844.  
  1845.  
  1846.                                       28
  1847.  
  1848.  
  1849.  
  1850.  
  1851. Release Notes v2.28     4.2.1  A framework for programming with monads
  1852.  
  1853.  
  1854.     incr :: State Int Int
  1855.     incr  = ST (\s -> (s,s+1))
  1856.  
  1857. To illustrate the use of state monads, consider the task of labeling
  1858. each of the nodes in a binary tree with distinct integer values.  One
  1859. simple definition is:
  1860.  
  1861.     label     :: Tree a -> Tree (a,Int)
  1862.     label tree = fst (lab tree 0)
  1863.      where lab (Leaf n)  c  =  (Leaf (n,c), c+1)
  1864.            lab (l :^: r) c  =  (l' :^: r', c'')
  1865.                                where (l',c')  = lab l c
  1866.                                      (r',c'') = lab r c'
  1867.  
  1868. This uses an explicit counter (represented by the second parameter to
  1869. lab) and great care must be taken to ensure that the appropriate
  1870. counter value is used in each part of the program; simple errors, such
  1871. as writing c in place of c' in the last line, are easily made but can
  1872. be hard to detect.
  1873.  
  1874. An alternative definition, using a state monad and following the
  1875. layout suggested in Wadler's POPL paper, can be written as follows:
  1876.  
  1877.     label     :: Tree a -> Tree (a,Int)
  1878.     label tree = lab tree `startingWith` 0
  1879.      where lab (Leaf n)  = incr                  `bind` \c ->
  1880.                            result (Leaf (n,c))
  1881.            lab (l :^: r) = lab l                 `bind` \l' ->
  1882.                            lab r                 `bind` \r' ->
  1883.                            result (l' :^: r')
  1884.  
  1885. While this program is perhaps a little longer than the previous
  1886. version, the use of monad operations ensures that the correct counter
  1887. value is passed from one part of the program to the next.  There is no
  1888. need to mention explicitly that a state monad is required: The use of
  1889. startingWith and the initial value 0 (or indeed, the use of incr on its
  1890. own) are sufficient to determine the monad State Int needed for the
  1891. bind and result operators.  It is not necessary to distinguish between
  1892. different versions of the monad operators bind, result and join or to
  1893. rely on explicit type declarations.
  1894.  
  1895.  
  1896. 4.2.2  Monad comprehensions
  1897. ---------------------------
  1898. Several functional programming languages provide support for list
  1899. comprehensions, enabling some common forms of computation with lists to
  1900. be written in a concise form resembling the standard syntax for set
  1901. comprehensions in mathematics.  In his paper `Comprehending Monads'
  1902. (ACM Lisp and Functional Programming, 1990), Wadler made the
  1903. observation that the comprehension notation can be generalized to
  1904. arbitrary monads, of which the list constructor is just one special
  1905. case.
  1906.  
  1907. In Wadler's notation, a monad comprehension is written using the syntax
  1908. of a list comprehension but with a superscript to indicate the monad in
  1909. which the comprehension is to be interpreted.  This is a little awkward
  1910.  
  1911.  
  1912.                                       29
  1913.  
  1914.  
  1915.  
  1916.  
  1917. Release Notes v2.28                        4.2.2  Monad comprehensions
  1918.  
  1919.  
  1920. and makes the notation less powerful than might be hoped since each
  1921. comprehension is restricted to a particular monad.  Using the
  1922. overloaded operators described in the previous section, Gofer provides
  1923. a more flexible form of monad comprehension which relies on overloading
  1924. rather than superscripts.  At the time of writing, this is the only
  1925. concrete implementation of monad comprehensions known to us.
  1926.  
  1927. In our system, a monad comprehension is an expression of the form
  1928. [e | qs ] where e is an expression and gs is a list of generators of
  1929. the form p <- exp.  As a special case, if gs is empty then the
  1930. comprehension [ e | qs ] is written as [ e ].  The implementation of
  1931. monad comprehensions is based on the following translation of the
  1932. comprehension notation in terms of the result and bind operators
  1933. described in the previous section:
  1934.  
  1935.     [ e ]                = result e
  1936.     [ e | p <- exp, qs ] = exp `bind` \p -> [ e | qs ]
  1937.  
  1938. In this notation, the label function from the previous section can
  1939. be rewritten as:
  1940.  
  1941.     label     :: Tree a -> Tree (a,Int)
  1942.     label tree = lab tree `startingWith` 0
  1943.      where lab (Leaf n)  = [ Leaf (n,c) | c <- incr ]
  1944.            lab (l :^: r) = [  l :^: r   | l <- lab l, r <- lab r ]
  1945.  
  1946. Applying the translation rules for monad comprehensions to this
  1947. definition yields the previous definition in terms of result and bind.
  1948. The principal advantage of the comprehension syntax is that it is often
  1949. more concise and, in the author's opinion, sometimes more attractive.
  1950.  
  1951.  
  1952. 4.2.3  Monads with a zero
  1953. -------------------------
  1954. Assuming that you are familiar with Gofer's list comprehensions, you
  1955. will know that it is also possible to include boolean guards in
  1956. addition to generators in the definition of a list comprehension.  Once
  1957. again, Wadler showed that this was also possible in the more general
  1958. setting of monad comprehensions, so long as we restrict such
  1959. comprehensions to monads that include a special element zero satisfying
  1960. a small number of laws.  This can be dealt with in our framework by
  1961. defining a subclass of Monad:
  1962.  
  1963.     class Monad m => Monad0 m where
  1964.         zero   :: m a
  1965.  
  1966. For example, the List monad has the empty list as a zero element:
  1967.  
  1968.     instance Monad0 [] where zero = []
  1969.  
  1970. Note that not there are also some monads which do not have a zero
  1971. element and hence cannot be defined as instances of Monad0.  The
  1972. State s monads described in Section 4.2.1 are a simple example of
  1973. this.
  1974.  
  1975. Working in a monad with a zero, a comprehension involving a boolean
  1976.  
  1977.  
  1978.                                       30
  1979.  
  1980.  
  1981.  
  1982.  
  1983. Release Notes v2.28                          4.2.3  Monads with a zero
  1984.  
  1985.  
  1986. guard can be implemented using the translation:
  1987.  
  1988.     [ e | guard, qs ]  =  if guard then [ e | qs ] else zero
  1989.  
  1990. Notice that, as far as the type system is concerned, the use of
  1991. zero in the translation of a comprehension involving a guard automatically
  1992. captures the restriction to monads with a zero:
  1993.  
  1994.     ? :t \x p -> [ x | p x ]
  1995.     \x p -> [ x | p x ] :: Monad0 b => a -> (a -> Bool) -> b a
  1996.     ?
  1997.  
  1998. The inclusion of a zero element also allows a slightly different
  1999. translation for generators in comprehensions:
  2000.  
  2001.     [ e | p <- exp, qs ]  =  exp `bind` f
  2002.                              where  f p  =  [ e | qs ]
  2003.                                     f _  =  zero
  2004.  
  2005. This corresponds directly to the semantics of standard Gofer list
  2006. comprehensions, but only differs from the semantics of the translation
  2007. given in the previous section when p is an irrefutable pattern; i.e.
  2008. when p is a pattern which may not match the value (or values) generated
  2009. by exp.  You can see the difference by trying the following example
  2010. in Gofer:
  2011.  
  2012.     ? [ x | [x] <- [[1],[],[2]]]
  2013.     [1, 2]
  2014.     (9 reductions, 31 cells)
  2015.     ? map (\[x] -> x) [[1],[],[2]]
  2016.     [1, 
  2017.     Program error: {v157 []}
  2018.     (8 reductions, 66 cells)
  2019.  
  2020.     ?
  2021.  
  2022. In order to retain compatibility with the standard list comprehension
  2023. notation, Gofer always uses the second translation above for generators
  2024. if the pattern p is refutable.  This may sometimes give inferred types
  2025. which are more restrictive than you expect.  For example, tuples are
  2026. not irrefutable patterns in Gofer or Haskell, and so the function:
  2027.  
  2028.     ? :t \xs -> [ x | (x,y) <- xs ]
  2029.     \xs -> [ x | (x,y)<-xs ] :: Monad0 a => a (b,c) -> a b
  2030.     ?
  2031.  
  2032. is restricted to monads with a zero because the expanded translation
  2033. above is used.  You can always avoid this problem by using the lazy
  2034. pattern construct (i.e. the tilde operator, ~p) as in:
  2035.  
  2036.     ? :t \xs -> [ x | ~(x,y) <- xs ]
  2037.     \xs -> [ x | ~(x,y)<-xs ] :: Monad a => a (b,c) -> a b
  2038.     ?
  2039.  
  2040. [At one stage, I was using a different form of brackets to represent
  2041. monad comprehensions, implemented using the original translation to
  2042.  
  2043.  
  2044.                                       31
  2045.  
  2046.  
  2047.  
  2048.  
  2049. Release Notes v2.28                          4.2.3  Monads with a zero
  2050.  
  2051.  
  2052. avoid changing the semantics of list comprehensions.  But I finally
  2053. decided that it would be better to use standard comprehension notation
  2054. with lazy pattern annotations where necessary since this is less
  2055. cumbersome than writing \xs -> [| x | (x,y) <- xs |] in place of the
  2056. comprehension above.  Please let me know what you think!]
  2057.  
  2058.  
  2059. 4.2.4  Generic operations on monads
  2060. -----------------------------------
  2061. The combination of polymorphism and constructor classes in our system
  2062. makes it possible to define generic functions which can be used on a
  2063. wide range of different monads.  A simple example of this is the
  2064. `Kleisli composition' for an arbitrary monad, similar to the usual
  2065. composition of functions except that it also takes care of `side
  2066. effects'.  The general definition is as follows:
  2067.  
  2068.    (@@)   :: Monad m => (a -> m b) -> (c -> m a) -> (c -> m b)
  2069.    f @@ g  = join . map f . g
  2070.  
  2071. For example, in a monad of the form State s, the expression f @@ g
  2072. denotes a state transformer in which the final state of the computation
  2073. associated with g is used as the initial state for the computation
  2074. associated with f.  More precisely, for this particular kind of monad,
  2075. the general definition given above is equivalent to:
  2076.  
  2077.     (@@)  :: (b -> State s c) -> (a -> State s b) -> (a -> State s c)
  2078.     f @@ g = \a -> STM (\s0 -> let ST g'  = g a
  2079.                                    (b,s1) = g' s0
  2080.                                    ST f'  = f b
  2081.                                    (c,s2) = f' s1
  2082.                                in  (c,s2))
  2083.  
  2084. The biggest advantage of the generic definition is that there is no
  2085. need to construct new definitions of (@@) for every different monad.
  2086. On the other hand, if specific definitions were required for some
  2087. instances, perhaps in the interests of efficiency, we could simply
  2088. include (@@) as a member function of Monad and use the generic
  2089. definition as a default implementation.
  2090.  
  2091. Generic operations can also be defined using the comprehension
  2092. notation:
  2093.  
  2094.     mapl             :: Monad m => (a -> m b) -> ([a] -> m [b])
  2095.     mapl f []         = [ [] ]
  2096.     mapl f (x:xs)     = [ y:ys | y <- f x, ys <- mapl f xs ]
  2097.  
  2098. This is the same as mapping a function down the elements of a list
  2099. using the normal map function except that, in the presence of side
  2100. effects, the order in which the applications are carried out is
  2101. important.  For mapl, we start on the left (i.e. the front of the list)
  2102. and work towards the right.  There is a corresponding dual which works
  2103. in the reverse direction:
  2104.  
  2105.     mapr             :: Monad m => (a -> m b) -> ([a] -> m [b])
  2106.     mapr f []         = [ [] ]
  2107.     mapr f (x:xs)     = [ y:ys | ys <- mapr f xs, y <- f x ]
  2108.  
  2109.  
  2110.                                       32
  2111.  
  2112.  
  2113.  
  2114.  
  2115. Release Notes v2.28                4.2.4  Generic operations on monads
  2116.  
  2117.  
  2118. These general functions have applications in several kinds of monad
  2119. with examples involving state and output.
  2120.  
  2121. The comprehension notation can also be used to define a generalization
  2122. of Haskell's filter function which works in an arbitrary monad with a
  2123. zero:
  2124.  
  2125.     filter           :: Monad0 m => (a -> Bool) -> m a -> m a
  2126.     filter p xs       = [ x | x<-xs, p x ]
  2127.  
  2128. There are many other general purpose functions that can be defined
  2129. in the current framework and used in arbitrary monads.  To give you
  2130. some further examples, here are generalized versions of the foldl and
  2131. foldr functions which work in an arbitrary monad:
  2132.  
  2133.     mfoldl           :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
  2134.     mfoldl f a []     = result a
  2135.     mfoldl f a (x:xs) = f a x `bind` (\fax -> mfoldl f fax xs)
  2136.  
  2137.     mfoldr           :: Monad m => (a -> b -> m b) -> b -> [a] -> m b
  2138.     mfoldr f a []     = result a
  2139.     mfoldr f a (x:xs) = mfoldr f a xs `bind` (\y -> f x y)
  2140.  
  2141. [Generalizing these definitions (and those of mapl, mapr) to work with
  2142. a second arbitrary monad (in place of the list monad) is left as an
  2143. entertaining exercise for the reader :-)]
  2144.  
  2145. As a final example, here is a definition of a `while' loop for an
  2146. arbitrary monad:
  2147.  
  2148.     while    :: Monad m => m Bool -> m b -> m ()
  2149.     while c s = c  `bind` \b ->
  2150.                 if b then s         `bind` \x ->
  2151.                           while c s
  2152.                      else result ()
  2153.  
  2154.  
  2155. 4.2.5  A family of state monads
  2156. -------------------------------
  2157. We have already described the use of monads to model programs with
  2158. state using the State datatype in Section 4.2.1.  The essential
  2159. property of any such monad is the ability to update the state and we
  2160. might therefore consider a more general class of state monads given by:
  2161.  
  2162.     class Monad (m s) => StateMonad m s where
  2163.         update :: (s -> s) -> m s s
  2164.         set    :: s -> m s s
  2165.         fetch  :: m s s
  2166.         set new = update (\old -> new)
  2167.         fetch   = update id
  2168.  
  2169. An expression of the form update f denotes the computation which
  2170. updates the state using f and result the old state as its result.  For
  2171. example, the incr function described above can be defined as:
  2172.  
  2173.     incr :: StateMonad m Int => m Int Int
  2174.  
  2175.  
  2176.                                       33
  2177.  
  2178.  
  2179.  
  2180.  
  2181. Release Notes v2.28                    4.2.5  A family of state monads
  2182.  
  2183.  
  2184.     incr  = update (1+)
  2185.  
  2186. in this more general setting.  The class declaration above also
  2187. includes set and fetch functions which set the state to a particular
  2188. value or return its value.  These are easily defined in terms of the
  2189. update function as illustrated by the default definitions.
  2190.  
  2191. The StateMonad class has two parameters; the first should be a
  2192. constructor of kind (* -> * -> *) while the second gives the state
  2193. type (of kind *); both are needed to specify the type of update.
  2194. The implementation of update for a monad of the form State s is
  2195. straightforward and provides us with our first instance of the
  2196. StateMonad class:
  2197.  
  2198.     instance StateMonad State s where
  2199.         update f = ST (\s -> (s, f s))
  2200.  
  2201. A rather more interesting family of state monads can be described using
  2202. the following datatype definition:
  2203.  
  2204.     data STM m s a = STM (s -> m (a,s)) -- a more sophisticated example,
  2205.                                         -- where the state monad is
  2206.                                         -- parameterized by a second,
  2207.                                         -- arbitrary monad.
  2208.  
  2209. Note that the first parameter to StateM has kind (* -> *), a
  2210. significant extension from Haskell (and previous versions of Gofer)
  2211. where all of the arguments to a type constructor must be types.  This
  2212. is another benefit of the kind system.
  2213.  
  2214. The functor and monad structure of a StateM m s constructor are given
  2215. by:
  2216.  
  2217.     instance Monad m => Functor (STM m s) where
  2218.         map f (STM xs) = STM (\s -> [ (f x, s') | ~(x,s') <- xs s ])
  2219.  
  2220.     instance Monad m => Monad (STM m s) where
  2221.         result x        = STM (\s -> result (x,s))
  2222.         STM xs `bind` f = STM (\s -> xs s `bind` (\(x,s') ->
  2223.                                      let STM f' = f x
  2224.                                      in  f' s'))
  2225.  
  2226. Note the condition that m is an instance of Monad in each of these
  2227. definitions.  If we hadn't used the lazy pattern construct ~(x,s') in
  2228. the instance of Functor, it would have been necessary to strengthen
  2229. this further to instances of Monad0 -- i.e. monads with a zero.
  2230.  
  2231. The definition of StateM m as an instance of StateMonad is also
  2232. straightforward:
  2233.  
  2234.     instance StateMonad (STM m) s where
  2235.         update f = STM (\s -> result (s, f s))
  2236.  
  2237. The following two functions are also useful for work with STM m s
  2238. monads.  The first, protect, allows an arbitrary computation to be
  2239. embedded in a state based computation without access to the state.
  2240.  
  2241.  
  2242.                                       34
  2243.  
  2244.  
  2245.  
  2246.  
  2247. Release Notes v2.28                    4.2.5  A family of state monads
  2248.  
  2249.  
  2250. The second, execute, is similar to the startingWith function in
  2251. Section 4.2.1, running a state based computation with a given initial
  2252. state and returning a computation as the result.
  2253.  
  2254. protect          :: Monad m => m a -> STM m s a
  2255. protect m         = STM (\s -> [ (x,s) | x<-m ])
  2256.  
  2257. execute          :: Monad m => s -> STM m s a -> m a
  2258. execute s (STM f) = [ x | ~(x,s') <- f s ]
  2259.  
  2260. Support for monads like StateM m s seems to be an important step
  2261. towards solving the problem of constructing monads by combining
  2262. features from simpler monads, in this case combining the use of state
  2263. with the features of an arbitrary monad m.  I hope that the system of
  2264. constructor classes in Gofer will be a useful tool for people working
  2265. in this area.
  2266.  
  2267.  
  2268. 4.2.6  Monads and substitution
  2269. ------------------------------
  2270. The previous sections have concentrated on the use of monads to
  2271. describe computations.  Monads also have a useful interpretation as a
  2272. general approach to substitution.  This in turn provides another
  2273. application for constructor classes.
  2274.  
  2275. Taking a fairly general approach, a substitution can be considered as a
  2276. function s::v-> t w where the types v and w
  2277. represent sets of variables and the type t a represents a set
  2278. of terms, typically involving elements of type a.  If t is
  2279. a monad and x::t v, then x `bind` s gives the result of
  2280. applying the substitution s to the term x by replacing
  2281. each occurrence of a variable v in x with the corresponding
  2282. term s v in the result.  For example:
  2283.  
  2284.     instance Monad Tree where
  2285.         result             = Leaf
  2286.         Leaf x    `bind` f = f x
  2287.         (l :^: r) `bind` f = (l `bind` f) :^: (r `bind` f)
  2288.  
  2289. With this interpretation in mind, the Kleisli composition (@@) in
  2290. Section 4.2.4 is just the standard way of composing substitutions,
  2291. while the result function corresponds to a null substitution.  The fact
  2292. that (@@) is associative with result as both a left and right identity
  2293. follows from the standard algebraic properties of a monad.
  2294.  
  2295.  
  2296. 4.3  Constructor classes in Gofer
  2297. ---------------------------------
  2298. The previous two sections should have given you some ideas about the
  2299. motivation and use for constructor classes.  It remains to say a few
  2300. words about the way that constructor classes fit into the general Gofer
  2301. framework.  In practice, this means giving a more detailed description
  2302. of the way that the kind system works.
  2303.  
  2304.  
  2305.  
  2306.  
  2307.  
  2308.                                       35
  2309.  
  2310.  
  2311.  
  2312.  
  2313. Release Notes v2.28   4.3.1  Kind errors and the k command line option
  2314.  
  2315.  
  2316. 4.3.1  Kind errors and the k command line option
  2317. ------------------------------------------------
  2318. As has already been mentioned, Gofer 2.28 uses kind information to
  2319. check that type expressions are well-formed rather than simply checking
  2320. that each type constructor is applied to an appropriate number of
  2321. arguments.  For example, having defined a tree datatype:
  2322.  
  2323.     data Tree a = Leaf a | Tree a :^: Tree a
  2324.  
  2325. the following definition will be rejected as an error:
  2326.  
  2327.     type Example  =  Tree Int Bool
  2328.  
  2329. as follows:
  2330.  
  2331.     ERROR "file" (line 42): Illegal type "Tree Int Bool" in
  2332.                             constructor application
  2333.  
  2334. The problem here is that the Tree constructor has kind * -> * so that
  2335. it expects to take one argument (a type) and deliver a type as the
  2336. result.  On the other hand, in the definition of Example, the Tree
  2337. constructor is treated as having (at least) two arguments; i.e. as
  2338. having a kind of the form (* -> * -> k) for some kind k.  Rather than
  2339. confuse a user who is not familiar with the use of kinds, Gofer
  2340. normally just prints an error message like the one above for examples
  2341. like this.
  2342.  
  2343. If you would like Gofer to give a more detailed description of the
  2344. problem, you can use the :set +k command line option as follows:
  2345.  
  2346.     ? :set +k
  2347.     ? :r
  2348.     Reading script file "file":
  2349.        
  2350.     ERROR "file" (line 42): Kind error in constructor application
  2351.     *** expression     : Tree Int Bool
  2352.     *** constructor    : Tree
  2353.     *** kind           : * -> *
  2354.     *** does not match : * -> a -> b
  2355.  
  2356.     ? 
  2357.  
  2358. When the k command line option has been selected, the :info command
  2359. described in Section 2.3.2 also includes kind information about the
  2360. kinds of type constructors defined in a program.  For example, given
  2361. the definition of Tree above and the datatypes:
  2362.  
  2363.     data STM m s x = STM (s -> m (s, x))
  2364.     data Queue a   = Empty | a :< Queue a | Queue a :> a
  2365.  
  2366. The :info command gives the following kinds (editing the output to
  2367. remove details about constructor functions for each datatype):
  2368.  
  2369.     ? :info Tree STM Queue
  2370.     -- type constructor with kind * -> *
  2371.     data Tree a
  2372.  
  2373.  
  2374.                                       36
  2375.  
  2376.  
  2377.  
  2378.  
  2379. Release Notes v2.28   4.3.1  Kind errors and the k command line option
  2380.  
  2381.  
  2382.     -- type constructor with kind (* -> *) -> * -> * -> *
  2383.     data STM a b c
  2384.  
  2385.     -- type constructor with kind * -> *
  2386.     data Queue a
  2387.  
  2388.     ?
  2389.  
  2390. In addition to calculating a kind of each type constructor introduced
  2391. in a datatype declaration, Gofer also determines a kind for each
  2392. constructor defined by means of a type synonym.  For example, the
  2393. following definitions:
  2394.  
  2395.     type Subst m v = v -> m v
  2396.     type Compose f g x = f (g x)
  2397.     type Pointer a = Int
  2398.     type Apply f x = f x
  2399.     type Fusion f g x = f x (g x)
  2400.     type Const x y   = x
  2401.  
  2402. are treated as having kinds:
  2403.  
  2404.     ? :info Subst Compose Pointer Apply Fusion Const
  2405.     -- type constructor with kind (* -> *) -> * -> *
  2406.     type Subst a b = b -> a b
  2407.  
  2408.     -- type constructor with kind (* -> *) -> (* -> *) -> * -> *
  2409.     type Compose a b c = a (b c)
  2410.  
  2411.     -- type constructor with kind * -> *
  2412.     type Pointer a = Int
  2413.  
  2414.     -- type constructor with kind (* -> *) -> * -> *
  2415.     type Apply a b = a b
  2416.  
  2417.     -- type constructor with kind (* -> * -> *) -> (* -> *) -> * -> *
  2418.     type Fusion a b c = a c (b c)
  2419.  
  2420.     -- type constructor with kind * -> * -> *
  2421.     type Const a b = a
  2422.  
  2423.     ?
  2424.  
  2425. Note however type synonyms are only used as abbreviations for other
  2426. type expressions.  It is not permitted to use a type synonym
  2427. constructor in a type expression without giving the correct number of
  2428. arguments.
  2429.  
  2430.     ? undefined :: Const Int
  2431.  
  2432.     ERROR: Wrong number of arguments for type synonym "Const"
  2433.     ?
  2434.  
  2435. Assuming that you are familiar with polymorphic functions in Gofer, you
  2436. might be wondering why some of the kinds given for the type synonyms
  2437. above are not also polymorphic in some sense.  After all, the standard
  2438.  
  2439.  
  2440.                                       37
  2441.  
  2442.  
  2443.  
  2444.  
  2445. Release Notes v2.28   4.3.1  Kind errors and the k command line option
  2446.  
  2447.  
  2448. prelude function const, is defined by
  2449.  
  2450.     const x y = x
  2451.  
  2452. with type a -> b -> a, which looks very similar to the definition of
  2453. the Const type synonym above, except that the kinds of the two
  2454. arguments have both been fixed as *.  In fact, the right hand side of
  2455. a type synonym declaration is always required to have kind *, so this
  2456. would mean that the most general kind that could be assigned to the
  2457. Const constructor would be * -> a -> *.
  2458.  
  2459. Gofer does not currently support the use of polymorphic kinds (let's
  2460. call them polykinds from now on).  First of all, it is not clear what
  2461. practical applications polykinds might offer (I have yet to find an
  2462. example where they are useful).  Furthermore, some of the deeper
  2463. theoretical issues about type inference and related topics have not yet
  2464. been studied and I suspect that polykinds would introduce significant
  2465. complications without any significant benefits.
  2466.  
  2467. The current approach is to replace any unknown part of an inferred kind
  2468. with the kind *.  Any polymorphism in the kind of a constructor
  2469. corresponds much more closely to the idea of a value that is not
  2470. actually used at all than in the language of normal expressions and
  2471. their types so this is unlikely to cause any problems.  And of course,
  2472. in Haskell and previous versions of Gofer, any variable used in a type
  2473. expression was assumed to be a type variable with kind *, so all of the
  2474. kinds above are consistent with this interpretation.
  2475.  
  2476. The rest of this section is likely to get a bit hairy.  Read on at your
  2477. peril, or skip to the start of Section 4.3.2.  Only those with a strong
  2478. interest in the type theory and pragmatics of constructor classes will
  2479. miss anything.
  2480.  
  2481. The same approach is used to determine the kinds of constructor
  2482. variables in type expressions.  In theory, this can sometimes lead to
  2483. problems.  In practice, this only happens in very contrived examples
  2484. and I doubt that any problems will occur for serious applications.  The
  2485. following example illustrates the kind of `problem' that can occur.
  2486. Suppose that we use a script containing the definitions:
  2487.  
  2488.     undefined :: a             -- the `bottom' value
  2489.     undefined  = undefined
  2490.  
  2491.     strange   :: f Tree -> f a
  2492.     strange    = undefined
  2493.     
  2494. The type signature for the `strange' function is indeed very strange;
  2495. the constructor variables f and a have kinds (* -> *) -> * and (* -> *)
  2496. respectively.  What's more, the type is very restrictive.  Without
  2497. including additional primitive constructs in the language, I very much
  2498. doubt that you will be able to find an alternative definition for
  2499. strange which is not semantically equivalent to the definition above.
  2500. And of course, the definition above doesn't really have any practical
  2501. applications anyway.  [In case you don't get my point, I'm trying to
  2502. show that this really is a very contrived example.]  I would be very
  2503. surprised to see a genuine example of a polymorphic operator which
  2504.  
  2505.  
  2506.                                       38
  2507.  
  2508.  
  2509.  
  2510.  
  2511. Release Notes v2.28   4.3.1  Kind errors and the k command line option
  2512.  
  2513.  
  2514. involves constructor variables of higher kinds in a non-trivial way
  2515. that does not also include overloading constraints as part of the
  2516. type.  For example, it is not at all difficult to think of an
  2517. interesting value of type Monad m => a -> m a, but much harder to think
  2518. of something with type a -> m a (remember this means for all a and for
  2519. all m).
  2520.  
  2521. The definitions of undefined and strange above will be accepted by the
  2522. Gofer system as will the following definition:
  2523.  
  2524.     contrived = strange undefined
  2525.  
  2526. The type of contrived will now be  f a  where f :: (* -> *) -> * and
  2527. a :: (* -> *).  However, if we modify the definition of contrived to
  2528. include a type signature:
  2529.  
  2530.     contrived :: f a
  2531.     contrived  = strange undefined
  2532.  
  2533. then we get a type checking error:
  2534.  
  2535.     ? :l file
  2536.     Reading script file "file":
  2537.     Type checking      
  2538.     ERROR "file" (line 24): Type error in function binding
  2539.     *** term           : contrived
  2540.     *** type           : a b
  2541.     *** does not match : c d
  2542.     *** because        : constructor variable kinds do not match
  2543.  
  2544.     ?
  2545.  
  2546. The problem is that for the declared type signature, the variables f and
  2547. a are treated as having kinds (* -> *) and * respectively.  These do not
  2548. agree with the real kinds for these variables.
  2549.  
  2550. To summarize, what this all means is that it is possible to define
  2551. values whose principal types cannot be expressed within the language of
  2552. Gofer types in the current implementation.  The values defined can
  2553. actually be used within a program, but it would not, for example, be
  2554. possible to allow such values to be exported from a module in a Haskell
  2555. system unless kind annotations were added to the inferred types.
  2556.  
  2557.  
  2558. 4.3.2  The kind of values in a constructor class
  2559. ------------------------------------------------
  2560. The previous section indicated that, if the :set +k command line option
  2561. has been set, the :info command will include information about the
  2562. kinds of type constructor constants in its output.  This will also
  2563. cause the :info command to display information about the kinds of
  2564. classes and constructor classes.  Notice for example in the following
  2565. how the output distinguishes between Eq, a type class, and Functor, a
  2566. constructor class in which each instance has kind (* -> *):
  2567.  
  2568.     ? :info Eq Functor
  2569.     -- type class
  2570.  
  2571.  
  2572.                                       39
  2573.  
  2574.  
  2575.  
  2576.  
  2577. Release Notes v2.28   4.3.2  The kind of values in a constructor class
  2578.  
  2579.  
  2580.     class Eq a where
  2581.         (==) :: Eq a => a -> a -> Bool
  2582.         (/=) :: Eq a => a -> a -> Bool
  2583.  
  2584.     -- instances:
  2585.     instance Eq ()
  2586.     ...
  2587.  
  2588.     -- constructor class with arity (* -> *)
  2589.     class Functor a where
  2590.         map :: Functor a => (b -> c) -> a b -> a c
  2591.  
  2592.     -- instances:
  2593.     instance Functor []
  2594.     ...
  2595.  
  2596.     ?
  2597.  
  2598.  
  2599. 4.3.3  Implementation of list comprehensions
  2600. --------------------------------------------
  2601. The implementation of overloaded monad comprehensions is cute, but also
  2602. has a couple of potential disadvantages.  These are discussed in this
  2603. section.  As you will see, they really aren't very much to worry
  2604. about.
  2605.  
  2606. First of all, the decision to overload the notation for singleton lists
  2607. so that [ exp ] == result exp can sometimes cause a few surprises:
  2608.  
  2609.     ? map (1+) [1]
  2610.     ERROR: Unresolved overloading
  2611.     *** type        : Monad a => a Int
  2612.     *** translation : map (1 +) [ 1 ]
  2613.  
  2614.     ?
  2615.  
  2616. Note that this will only occur if you are actually using a prelude
  2617. which includes the definition of the Monad class given in Section 4.2
  2618. This can be solved using the command line toggle :set -1 which forces
  2619. any expression of the form [ exp ] to be treated as a singleton list
  2620. rather than being interpreted in an arbitrary monad.  You really
  2621. have to write `result' if you do want an arbitrary monad:
  2622.  
  2623.     ? :set -1
  2624.     ? map (1+) [1]
  2625.     [2]
  2626.     (7 reductions, 18 cells)
  2627.     ? map (1+) (result 1)
  2628.     ERROR: Unresolved overloading
  2629.     *** type        : Monad a => a Int
  2630.     *** translation : map (1 +) (result 1)
  2631.  
  2632.     ?
  2633.  
  2634. This should probably be the default setting, but I have left things as
  2635. they are for the time being, partly so that other people might get the
  2636.  
  2637.  
  2638.                                       40
  2639.  
  2640.  
  2641.  
  2642.  
  2643. Release Notes v2.28       4.3.3  Implementation of list comprehensions
  2644.  
  2645.  
  2646. chance to find out about this and decide what setting they think would
  2647. be best.  As usual, the default setting can be recovered using the
  2648. :set +1 command.
  2649.  
  2650. A second concern is that the implementation of list comprehensions may
  2651. be less efficient in the presence of monad comprehensions.  Gofer
  2652. usually uses Wadler's `optimal' translation for list comprehensions as
  2653. described in Simon Peyton Jones book.  In fact, this translation will
  2654. always be used if either the prelude being used does not include the
  2655. standard Monad class or the type system is able to guarantee that a
  2656. given monad comprehension is actually a list comprehension.
  2657.  
  2658. If you use a prelude containing the Monad class, you may notice some
  2659. small differences in performance in examples such as:
  2660.  
  2661.     ? [ x * x | x <- [1..10] ]
  2662.     [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  2663.     (98 reductions, 203 cells)
  2664.  
  2665.     ? f [1..10] where f xs = [ x * x | x <- xs ]
  2666.     [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  2667.     (139 reductions, 268 cells)
  2668.  
  2669.     ?
  2670.  
  2671. The second expression is a little more expensive since the local
  2672. definition of f is polymorphic with f :: (Num b, Monad a) => a b -> a b
  2673. and hence the implementation of the comprehension in f does not use the
  2674. standard translation for lists.  To be honest, the difference between
  2675. these two functions really isn't anything to worry about in the context
  2676. of an interpreter like Gofer.  And of course, if you really want to
  2677. avoid this problem, an explicit type signature will do the trick (as in
  2678. other cases where overloading is involved):
  2679.  
  2680.     ? f [1..10] where f   :: Num b => [b] -> [b];
  2681.                       f xs = [ x * x | x <- xs ]
  2682.     [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  2683.     (99 reductions, 205 cells)
  2684.  
  2685.     ? f [1..10] where f   :: [Int] -> [Int]
  2686.                       f xs = [ x * x | x <- xs ]      
  2687.     [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  2688.     (99 reductions, 203 cells)
  2689.  
  2690.     ?
  2691.  
  2692. As the last example shows, there is only one more reduction in this
  2693. case and that is the reduction step that deals with the application of
  2694. f to the argument list [1..10].
  2695.  
  2696.  
  2697.  
  2698.  
  2699.  
  2700.  
  2701.  
  2702.  
  2703.  
  2704.                                       41
  2705.  
  2706.  
  2707.  
  2708.  
  2709. Release Notes v2.28                        5. GOFC, THE GOFER COMPILER
  2710.  
  2711.  
  2712. 5. GOFC, THE GOFER COMPILER
  2713.  
  2714. This release of Gofer includes gofc, a `compiler' for Gofer programs
  2715. which translates a large class of Gofer programs into C code which can
  2716. then be compiled and executed as a standalone application.
  2717.  
  2718. Before anybody gets too excited, there are a couple of points which I
  2719. should mention straight away:
  2720.  
  2721.   o  To make use of gofc, you will need a C compiler.  This is why I
  2722.      do not intend to distribute any binary versions of gofc; if you
  2723.      have the C compiler needed to compile the output of gofc then
  2724.      you should also be able to compile gofc from the sources.
  2725.  
  2726.   o  First of all, the Gofer compiler was written by modifying the
  2727.      Gofer interpreter.  Most of the modifications and changes were
  2728.      made in just a few days.  The compiler and interpreter still
  2729.      share a large proportion of code.  As such, and in case it isn't
  2730.      obvious: PLEASE DO NOT expect to gain the same kind of performance
  2731.      out of gofc as you would from one of the serious Haskell
  2732.      projects.  A considerably greater amount of time and effort has
  2733.      gone into those systems.
  2734.  
  2735.   o  The compiler is actually over a year old, but this is the first
  2736.      time it has been released.  Although I have worked with it a bit
  2737.      myself, it hasn't had half the amount of testing that Gofer user's
  2738.      have given the interpreter over the last year and a half.  It may
  2739.      not be as reliable as the interpreter.  If you have problems with
  2740.      a compiled program, try running it with the interpreter too just
  2741.      to check that you haven't found a potential bug in gofc.
  2742.  
  2743. That having been said, I hope that the Gofer compiler will be useful to
  2744. many Gofer users.  One possible advantage is that the executables may
  2745. be smaller than with some other systems.  And of course, the fact that
  2746. gofc runs on some home computers may also be useful.  Finally, gofc
  2747. provides a simplified system for experimenting with the runtime details
  2748. of an implementation.  For example, the source code for the runtime
  2749. system is set up in such a way as to make it possible to experiment
  2750. with alternative garbage collection schemes.
  2751.  
  2752.  
  2753. 5.1  Using gofc
  2754. ---------------
  2755. Compiling a program with gofc is very much like starting up the Gofer
  2756. interpreter.  The compiler starts by reading the prelude and then
  2757. loads the script files specified by the command line.  These scripts
  2758. must contain a definition for the value main :: Dialogue which will be
  2759. the dialogue expression that is evaluated when the compiled program is
  2760. executed.
  2761.  
  2762. For example, if the file apr1.gs contains the simple program:
  2763.  
  2764.     main :: Dialogue
  2765.     main  = appendChan "stdout" "Hello, world\n" exit done
  2766.  
  2767. then this can be compiled as:
  2768.  
  2769.  
  2770.                                       42
  2771.  
  2772.  
  2773.  
  2774.  
  2775. Release Notes v2.28                                    5.1  Using gofc
  2776.  
  2777.  
  2778.     machine% gofc apr1.gs
  2779.     Gofer->C Version 1.01 (2.28)  Copyright (c) Mark P Jones 1992-1993
  2780.     
  2781.     Reading script file "/usr/local/lib/Gofer/standard.prelude":
  2782.     Reading script file "apr1.gs":
  2783.                    
  2784.     Writing C output file "apr1.c":
  2785.     [Leaving Gofer->C]
  2786.     machine% 
  2787.  
  2788. The output is written to the file apr1.c -- i.e. the name obtained by
  2789. removing the .gs suffix and replacing it with a .c suffix.  Other
  2790. filename suffixes that are treated in a similar way are:
  2791.  
  2792.     .prj    .gp                   for Gofer project files
  2793.  
  2794.     .prelude                      for Gofer prelude files
  2795.  
  2796.     .gof    .gs                   for Gofer scripts
  2797.  
  2798.     .has    .hs                   for Haskell scripts
  2799.  
  2800.     .lhs    .lit                  for literate scripts
  2801.     .lgs    .verb
  2802.  
  2803. If no recognized suffix is found then the name of the output file is
  2804. obtained simply by appending the .c suffix to the input name.
  2805.  
  2806. For the benefit of those using Unix systems, let me point out that this
  2807. could cause you problems if you are not careful; if you take an input
  2808. file called `prog' and compile it to `prog.c' using gofc, make sure
  2809. that you do not compile the C program in such a way that the output is
  2810. also called `prog' since this will overwrite your original source code!
  2811. For this reason, I would always suggest using file extensions such as
  2812. the .gs example above if you are using gofc.
  2813.  
  2814. If you run gofc with multiple script files, then the name of the output
  2815. file is based on the last script file to be loaded.  For example, the
  2816. command `gofc prog1.gs prog2.gs' produces an output file `prog2.c'.
  2817.  
  2818. Gofc also works with project files, using the name of the project file
  2819. to determine the name of the output file.  For example, the miniProlog
  2820. interpreter can be compiled using:
  2821.  
  2822.     machine% gofc + miniProlog
  2823.     Gofer->C Version 1.01 (2.28)  Copyright (c) Mark P Jones 1992-1993
  2824.  
  2825.     Reading script file "/usr/local/lib/Gofer/standard.prelude":
  2826.     Reading script file "Parse":
  2827.     Reading script file "Interact":
  2828.     Reading script file "PrologData":
  2829.     Reading script file "Subst":
  2830.     Reading script file "StackEngine":
  2831.     Reading script file "Main":
  2832.                    
  2833.     Writing C output file "miniProlog.c":
  2834.  
  2835.  
  2836.                                       43
  2837.  
  2838.  
  2839.  
  2840.  
  2841. Release Notes v2.28                                    5.1  Using gofc
  2842.  
  2843.  
  2844.     [Leaving Gofer->C]
  2845.     machine% 
  2846.  
  2847. This is another case where it might well have been sensible to have
  2848. used a .prj or .gp for the project file miniProlog since compiling the
  2849. C code in miniProlog.c to a file named `miniProlog' will overwrite the
  2850. project file!  Choose filenames with care!
  2851.  
  2852. You can also specify Gofer command line options as part of the command
  2853. line used to run gofc.  Think of it like this; use exactly the same
  2854. command line to start Gofc as you would have done to start Gofer (ok,
  2855. replacing the command `gofer' with `gofc') so that you could start your
  2856. program immediately by evaluating the main expression.  To summarize
  2857. what happens next:
  2858.  
  2859.   o  Gofc will load the prelude file.  Do not worry if the prelude
  2860.      (or indeed, later files) contain lots of definitions that your
  2861.      program will not actually use; only definitions which are actually
  2862.      required to evaluate the main expression will be included in the
  2863.      output file.
  2864.  
  2865.   o  Gofc will load the script files specified.  If an error is found
  2866.      then an error message will be printed and the compilation will be
  2867.      aborted.  You would probably be sensible to run your program
  2868.      through the interpreter first to tidy up any errors and avoid this
  2869.      problem.
  2870.  
  2871.   o  Gofc will look for a definition of `main' and check that it has
  2872.      type Dialogue.  You will get an error if an appropriate main
  2873.      value cannot be found.
  2874.  
  2875.   o  Gofc determines the appropriate name for the output file.
  2876.  
  2877.   o  Gofc checks to make sure that you haven't used a primitive
  2878.      function that is not supported by the runtime system (see
  2879.      Section 5.2 for more details).
  2880.  
  2881.   o  Gofc outputs a C version of the program in the output file.
  2882.  
  2883. Once you have compiled the Gofer program to C, you need to compile
  2884. the C code to build the executable application program.  This will
  2885. vary from one system to another and is documented elsewhere.
  2886.  
  2887.  
  2888. 5.2  Primitive operations
  2889. -------------------------
  2890. The Gofer compiler accepts the same source language as the
  2891. interpreter.  However, there is a small collection of Gofer primitives
  2892. which are only implemented in the interpreter.  The most likely
  2893. omission that you will notice is the primPrint function which is used
  2894. to define the show' function in the standard prelude.  Omitting this
  2895. function is not an indication of laziness on my part; it is impossible
  2896. to implement primPrint in the current runtime system because there is
  2897. insufficient type information available at program runtime.
  2898.  
  2899.  
  2900.  
  2901.  
  2902.                                       44
  2903.  
  2904.  
  2905.  
  2906.  
  2907. Release Notes v2.28                          5.2  Primitive operations
  2908.  
  2909.  
  2910. For example, if you try to compile the program:
  2911.  
  2912.     main :: Dialogue
  2913.     main  = appendChan "stdout" (show' 42) exit done
  2914.  
  2915. the compiler will respond with the error message:
  2916.  
  2917.     ERROR: Primitive function primPrint is not
  2918.            supported by the gofc runtime system
  2919.            (used in the definition of show')
  2920.     Aborting compilation
  2921.  
  2922. The solution is to use type classes.  This is one of the reasons for
  2923. including them in the language in the first place.  This example can
  2924. be compiled by changing the original program to:
  2925.  
  2926.     main :: Dialogue
  2927.     main  = appendChan "stdout" (show 42) exit done
  2928.  
  2929. (Remember that show is the overloaded function for converting values of
  2930. any type a that is an instance of the Text class to a string value.)
  2931.  
  2932.  
  2933. 5.3  Debugging output
  2934. ---------------------
  2935. Another potentially useful feature of gofc is it's ability to dump a
  2936. listing of all the supercombinator definitions that are created by
  2937. loading a particular combination of script files.  For the time being,
  2938. this is only useful for the purpose of debugging, but with only small
  2939. modifications, it might be possible to use this as input to an
  2940. alternative backend/code generator system (the format of the output
  2941. combinators already uses explicit layout characters to make the task of
  2942. parsing easier in an application like this).
  2943.  
  2944. To illustrate how this option might be used, suppose that we were working
  2945. on a program containing the definition:
  2946.  
  2947.     hidden xs = map (\[x] -> x) xs
  2948.  
  2949. and that somewhere during the execution of our program, this function is
  2950. applied to a list value [[1],[1,2]]:
  2951.  
  2952.     ? hidden [[1],[1,2]]
  2953.     [1, 
  2954.     Program error: {v132 [1, 2]}
  2955.     (13 reductions, 75 cells)
  2956.  
  2957.     ?
  2958.  
  2959. The variable v132 which appears here is the name used internally to
  2960. represent the lambda expression in the definition of hidden.  For this
  2961. particular example, it is fairly easy to work this out, but in general,
  2962. it may not be so straightforward.  Running the program through gofc and
  2963. using the +D toggle as follows produces an output file containing Gofer
  2964. SuperCombinators, hence the .gsc suffix:
  2965.  
  2966.  
  2967.  
  2968.                                       45
  2969.  
  2970.  
  2971.  
  2972.  
  2973. Release Notes v2.28                              5.3  Debugging output
  2974.  
  2975.  
  2976.     machine% gofc +D file
  2977.     Gofer->C Version 1.01 (2.28)  Copyright (c) Mark P Jones 1992-1993
  2978.  
  2979.     [Writing supercombinators to "file.gsc"]
  2980.     Reading script file "/usr/local/lib/Gofer/standard.prelude":
  2981.     Reading script file "file":
  2982.     [Leaving Gofer->C]
  2983.     machine% 
  2984.  
  2985. Note that there is no need in this situation for the files loaded to
  2986. contain a definition for main :: Dialogue, although the compiler must
  2987. be loaded using exactly the same prelude and order of files as in the
  2988. original Gofer session to ensure that the same names are used.  Scanning
  2989. the output file, we find that the only mention of v132 is in the
  2990. definitions:
  2991.  
  2992.     v132 o1 = case o1 of {
  2993.                 (:) o3 o2 -> case o2 of {
  2994.                                [] -> o3;
  2995.                              }
  2996.               }
  2997.  
  2998.     hidden o1 = map v132 o1;
  2999.  
  3000. This shows fairly clearly where the function v132 comes from.  Of
  3001. course, this is far from perfect, but it might help someone to track
  3002. down a bug that little bit faster one day.  It's better than nothing.
  3003.  
  3004. Of course, the debugging output might also be of interest to anyone
  3005. that wants to find out more about the implementation of Gofer and
  3006. examine the supercombinator definitions generated when list
  3007. comprehensions, overloading, local function definitions etc.  have all
  3008. been eliminated.  For example, the standard prelude definitions of map
  3009. and filter become:
  3010.  
  3011.     map o2 o1 = case o1 of {
  3012.                   [] -> [];
  3013.                   (:) o4 o3 -> o2 o4 : map o2 o3;
  3014.                 }
  3015.  
  3016.     filter o2 o1 = case o1 of {
  3017.                      [] -> [];
  3018.                      (:) o4 o3 -> let { o5 = filter o2 o3;
  3019.                                   } in  | o2 o4 -> o4 : o5;
  3020.                                         | otherwise -> o5;
  3021.                    }
  3022.  
  3023. This is one of the tools I'll be using if anyone ever reports another
  3024. bug in the code generator...
  3025.  
  3026.  
  3027.  
  3028.  
  3029.  
  3030.  
  3031.  
  3032.  
  3033.  
  3034.                                       46
  3035.  
  3036.  
  3037.  
  3038.  
  3039. Release Notes v2.28                                    6. SOME HISTORY
  3040.  
  3041.  
  3042. 6. SOME HISTORY
  3043.  
  3044. Ever since the first version of Gofer was released I've had requests
  3045. from Gofer users around the world asking how Gofer got its name and how
  3046. it came into being.  This section is an attempt to try and answer those
  3047. questions.
  3048.  
  3049. 6.1  Why Gofer?
  3050. ---------------
  3051. Everything has to have a name.  You may type in an `anonymous function'
  3052. as a lambda expression but Gofer will still go ahead and give it a
  3053. name.  To tell the truth, I always intended the name `Gofer' to be
  3054. applied to my particular implementation of a functional programming
  3055. environment, not to the language on which it is based.  I wanted that
  3056. to be an anonymous language.  But common usage has given it the same
  3057. name, Gofer.
  3058.  
  3059. If you take a look in a dictionary (as some puzzled Gofer users have)
  3060. you'll find that `gofer' means:
  3061.  
  3062.      ``an employee whose duties include running errands''
  3063.  
  3064. (although you'd better choose a dictionary printed since the 70s for
  3065. this).  I'd not thought about this when I chose the name (and I would
  3066. have used a lower case g instead of an upper case G if I had).  In
  3067. fact, Gofer was originally conceived as a system for machine assisted
  3068. equational reasoning.  One of the properties of functional languages
  3069. that I find particularly attractive is that they are:
  3070.  
  3071.     GOod For Equational Reasoning.
  3072.     ^^   ^   ^          ^
  3073. So now you know.  The fact that you can also tell someone who is having
  3074. a problem with their C program to ``Gofer it!'' (unsympathetic, I know)
  3075. is nothing more than a coincidence.  Fairly recently, somebody wrote to
  3076. ask if Gofer stood for ``GOod Functional programming EnviRonment''. I
  3077. was flattered; I wish I'd thought of that one.
  3078.  
  3079. Some people have asked me why I didn't choose a title including the
  3080. name `Haskell', a language on which Gofer is very strongly based.
  3081. There are two reasons for this.  To start with, the original version of
  3082. Gofer was based on a different syntax, Orwell + type classes.  The
  3083. Haskell influence only crept in when I started on version 2.xx.
  3084. Secondly, it's only right to point out that there is quite a large gap
  3085. between a system like Gofer and the full blown Haskell systems that
  3086. have been developed.  Using a name which doesn't involve `Haskell'
  3087. directly seemed the right thing to do.  Some people tell me that it was
  3088. a mistake.  One of the objectives of Haskell was to create a standard
  3089. language for non-strict functional programming.  Gofer isn't intended
  3090. as an alternative to Haskell and I hope it will continue to grow closer
  3091. as time passes.
  3092.  
  3093. While I'm on the subject of names, I should also talk about an
  3094. additional source of confusion that may sometimes crop up.  While Gofer
  3095. is a functional programming system, there is also a campus wide
  3096. information system called `Gopher' (sharing it's name with the North
  3097. American rodents).  I would guess that the latter has many more users
  3098.  
  3099.  
  3100.                                       47
  3101.  
  3102.  
  3103.  
  3104.  
  3105. Release Notes v2.28                                    6.1  Why Gofer?
  3106.  
  3107.  
  3108. than the former.  So please be careful to spell Gofer with an `f' not
  3109. a `ph' to try and minimize the confusion.
  3110.  
  3111. It has occurred to me that I should try and think of another name for
  3112. Gofer to avoid the confusion with Gopher.  I hope that won't be
  3113. necessary, but if you have a really good suggestion, let me know!  One
  3114. possibility might be to call it `Gordon'.  The younger generation of
  3115. brits might know what the connection is.  Others may need to ask their
  3116. children...
  3117.  
  3118. 6.2  The history of Gofer
  3119. -------------------------
  3120. Here is a summary of the way that I first learnt about functional
  3121. programming, and how it started me on the path to writing Gofer.
  3122. This, slightly sentimental review is mostly for my own entertainment.
  3123. If you're the sort of person that likes to read the acknowledgments
  3124. and bibliographic notes in a thesis: this is for you.  If not, you
  3125. can always stop reading :-)
  3126.  
  3127. My first exposure to lazy functional programming languages was using a
  3128. language called `Orwell' developed and used at the Programming Research
  3129. Group in Oxford.  I've been interested in using and implementing lazy
  3130. functional programming languages ever since.
  3131.  
  3132. One of the properties of programming in Orwell that appealed to me was
  3133. the ability to use equational reasoning -- a very simple style of
  3134. mathematical reasoning -- to establish properties of programs and prove
  3135. that they would behave in particular ways.  Even more interesting,
  3136. equational reasoning can be used to calculate efficient implementations
  3137. of programs from a formal specification of what was intended.
  3138.  
  3139. Probably the first non-trivial functional program that I wrote was a
  3140. simple Prolog interpreter.  (This was originally written in Orwell and
  3141. later transcribed to be compiled using the Chalmers Haskell B compiler,
  3142. hbc.  The remnants of this program live on in the mini Prolog
  3143. interpreter that is included with the Gofer distribution and, I
  3144. believe, with at least a couple of the big Haskell systems.) Using a
  3145. sequence of something like a dozen or so transformations (most of which
  3146. were fairly mundane), I discovered that I could turn a relatively
  3147. abstract specification of a Prolog inference engine into a program that
  3148. could be interpreted as the definition of a low level stack-based
  3149. machine for executing Prolog queries.  Indeed, I used the result as the
  3150. core of a C implementation of mini Prolog.
  3151.  
  3152. The transformations themselves were simple enough but managing the
  3153. complexity of the calculations was tough.  It was not uncommon to find
  3154. that some of the intermediate steps in a calculation would span more
  3155. than 200 characters.  Even with a relatively small number of
  3156. transformation steps, carrying out proofs like this was both tedious
  3157. and prone to mistakes.  A natural application for a computer!
  3158.  
  3159. Here's an outline of what happened next:
  3160.  
  3161.    eqr   1989.  Eqr was a crude tool for machine assisted equational
  3162.          reasoning.  It worked well enough for the job I had intended
  3163.          to use it for, but it also had a number of problems.  I
  3164.  
  3165.  
  3166.                                       48
  3167.  
  3168.  
  3169.  
  3170.  
  3171. Release Notes v2.28                          6.2  The history of Gofer
  3172.  
  3173.  
  3174.          particularly missed the ability to use and record type
  3175.          information as part of an automated derivation.
  3176.  
  3177.    1.xx  1990.  Gofer 1.xx was intended to be the next step forward
  3178.          providing machine support for *typed* equational reasoning.
  3179.          It was based on Orwell syntax and was later extended to
  3180.          support Haskell style type classes.  It had a lexer, parser,
  3181.          type checker and simple top-level interactive loop.  It
  3182.          couldn't run programs or construct derivations.
  3183.  
  3184.    2.xx  January 1991.  A complete rewrite.  I remember those early
  3185.          days, several months passed before I ever got compile some of
  3186.          the earliest code.  The emphasis switched to being able to run
  3187.          programs rather than derive them when I came up with a new
  3188.          implementation technique for type classes in February 1991.
  3189.          If I wanted to see it implemented, I was going to have to do
  3190.          it myself.  Around about May, I realized I had something that
  3191.          might be useful to other people.
  3192.  
  3193.    2.20  The first public release, announced in August 1991 and
  3194.          distributed shortly after that in September.
  3195.  
  3196.    2.21  November 1991, providing a more comprehensive user
  3197.      interface, access to command line options and fixing a
  3198.      small number of embarrassing bugs in the original release.
  3199.  
  3200.    2.23  August 1992, having been somewhat preoccupied with academic
  3201.      studies for some time, the main purpose of this release
  3202.      was to correct a number of minor bugs which had again been
  3203.      discovered, either by myself or by one or more of the many
  3204.      Gofer users out there.
  3205.  
  3206.    2.28  January 1993.  The most substantial update to Gofer since
  3207.      the original release.  I had been doing a lot of work and
  3208.      experimentation with Gofer during the time between the
  3209.      release of versions 2.21 and 2.23, but I didn't have the
  3210.      time to get these extensions suitable for public distribution.
  3211.      By the time I came to release version 2.23, I also had
  3212.      several other distinct versions of Gofer (each derived
  3213.      from the source for version 2.21) including a compiler
  3214.      and a prototype implementation of constructor classes
  3215.      which was called `ccgofer'.   Work on version 2.28 started
  3216.      with efforts to merge these developments back into a single
  3217.      system (I was tired of trying to maintain several different
  3218.      versions, even though I was the only one using them).
  3219.      The rough outline of changes was as follows (with the
  3220.      corresponding version numbers for those who wonder why
  3221.      2.28 follows 2.23):
  3222.  
  3223.             2.24   enhancements and bug fixes
  3224.             2.25   merging in support for the Gofer compiler
  3225.             2.26   a reimplementation of constructor classes
  3226.             2.27   reworked code generator and other minor fixes
  3227.             2.28   preparation for public release
  3228.  
  3229.  
  3230.  
  3231.  
  3232.                                       49
  3233.  
  3234.  
  3235.