f06ff2dbe16d82a7abdc50b2eff69acb15627a0e
[lambda.git] / juli8.mdwn
1 [[!toc]]
2
3 ## What are the Juli8 Libraries? ##
4
5 In addition to the fine programming language Kapulet, which doesn't yet exist in a form you can actually execute --- though the full-featured interpreter we provided [[a week or so ago|/topics/week7_untyped_evaluator]] is a good start --- I decided it would be useful to have a collection of basic libraries for our teaching (and other) purposes, that brought OCaml, Haskell, and the Scheme implementations we recommend, more onto even footing. Of course there are fundamental differences between these languages, such as the lack of types in Scheme (though both Racket and Chicken have some facility for working with types in extensions), or the default lazy evaluation strategy in Haskell. But there are also many simply accidental differences between the languages too, in that this one provides a library function doing so-and-so, but the other one doesn't, or calls it by a different name. The Juli8 collection of libraries is aimed to reduce these differences, to make it easier to move back and forth between the languages, and also to just make some of the languages generally easier to use (from my perspective). Juli8 will eventually have components that you can install into each of Haskell, OCaml, Racket, and Chicken. For the moment, OCaml is the most developed of these, and Haskell a bit, with the Scheme components deferred for another time.
6
7 Juli8 tries to give OCaml more-or-less the same functionality that you have in Haskell's Maybe, List, and Monad libraries, as well as a few others. It doesn't try for an exact match, and it doesn't strictly use the same names as Haskell does. I've aimed to at least stay close to the existing OCaml naming patterns, and also the Scheme naming patterns when they are salient to me.
8
9 Below, we'll give instructions on how to install Juli8 into your existing OCaml and/or Haskell setups. We'll also discuss how to use the non-monadic parts of the Juli8 libraries for OCaml. We discuss how to use the monadic parts of the Juli8 libraries for OCaml [[elsewhere|/topics/week9_using_the_monad_library]]. We'll also say a little bit about the little bit that Juli8 provides for you if you're using Haskell.
10
11 ## Setting up OCaml in general, and for use with Juli8 ##
12
13 1. In our instructions for installing OCaml, some of the strategies involved installing the OPAM package manager. (This doesn't appear to be available for Windows.) If you did install OPAM, then we recommend typing the following commands inside a terminal session (that is, *not* inside an OCaml session, but inside the prompt where you'd start OCaml by typing `ocaml`).
14
15         opam update
16         opam upgrade
17         opam install delimcc
18
19     We'll comment on the `delimcc` package in a moment.
20
21     We also recommend upgrading to one of the more recent versions of OCaml. This gets you the most recent stable release:
22
23         opam switch 4.02.1 && opam install --switch=4.02.1 delimcc && eval `opam config env`
24
25     This will get you a slightly older release, which however has "improved type error messages":
26
27         opam switch 4.02.0+improved-errors && opam install --switch=4.02.0+improved-errors delimcc && eval `opam config env`
28
29     Installing the `delimcc` library gives you Oleg's Delimited Continuation library for OCaml, which we will encourage you to play around with later in the term. It's not essential to have it, though. There are some advantages to using one of the 4.02.x versions of OCaml, though, rather than the 4.01.0 version I think most of us ended up installing. One advantage is that now you can use the special <code>#show <i>symbol</i></code> command in OCaml sessions, which works like Haskell's special <code>:info <i>symbol</i></code> inside a GHCi session. Then for example, instead of having to type:
30
31         module type SOMETHING = Monad.OPTION
32
33     as [[I directed you elsewhere|/topics/week9_using_the_monad_library]], you can instead just type:
34
35         #show Monad.OPTION
36
37 2. The program that starts up when you type `ocaml` is OCaml's Standard "Toplevel" Interactive Interpreter. There's an alternative interactive interpreter that you can try out, which some people like. It's called **utop** and [you can read about it here](https://github.com/diml/utop) or [here](https://opam.ocaml.org/blog/about-utop). To install it, you can just type `opam install utop`. I'm not so crazy about it myself. But I prefer to use *some* kind of helper program with OCaml's Standard Toplevel, because the Standard Toplevel itself doesn't let you scroll back through commands you typed previously, has only very rudimentary facilities for editing a line if you made a mistake and so on. One virtue of utop is that it does those things better, but there are also other ways to do them better. What I use is a wrapper program called **rlwrap**. Here are instructions for how to install that:
38
39     > First, I had to upgrade the version of the "GNU readline" library on my computer. My Mac with System 10.9.5 has a version of that library, but it's too old to use with recent versions of `rlwrap`. So I downloaded [the source code for GNU readline](http://ftp.gnu.org/gnu/readline/readline-6.3.tar.gz). Double click the downloaded archive to expand it, if your browser doesn't do that automatically. Then go inside the `readline-6.3` folder in a Terminal. On a Mac, you can click on the folder in the Finder and do a Copy (or cmd-C). Then open a Terminal and type `cd` followed by a space then do a Paste (cmd-V). Then press <code><i>return</i></code>. Once you're inside the `readline-6.3` folder, type this command in the Terminal:
40
41     >     ./configure --prefix=$HOME && make && make install
42
43     > That should build and install the readline library in your local user directories. It will take a couple of minutes. Next, download [the source code for rlwrap](http://utopia.knoware.nl/~hlub/rlwrap/rlwrap-0.42.tar.gz). Double-click to expand and go inside the `rlwrap-0.42` folder in a Terminal, as before. Then type this command in the Terminal:
44
45     >     ./configure --prefix=$HOME && make CFLAGS="-g -O2 -I $HOME/include -L $HOME/lib" && make install
46
47     If that all completes without errors, then you have gotten `rlwrap` installed. Congratulations. To use it, you just will now type `rlwrap ocaml ...` wherever before you would ordinarily type `ocaml ...` This is just to make interactive OCaml sessions nicer. To compile code with `ocamlc ...` and so on, you don't use `rlwrap` for that. (If you see error messages of the form `-bash: rlwrap: command not found`, then you should make sure that you have a `.bash_profile` or `.bashrc` in your `$HOME` directory which has a line containing something like `export PATH="$HOME/bin:$HOME/Library/Haskell/bin:$PATH"`. Then maybe start up a new Terminal.)
48
49     Some keycommands you can now use are:
50
51     * `^H` (that is, Control+`H`) will delete one char to the left of the cursor position (also `backspace`)
52     * `^D` will delete one char to the right of the cursor position
53     * `^W` will delete one word to the left of the cursor position (also `esc` followed by `backspace`, after releasing `esc`)
54     * `esc` + `D` will delete one word to the right of the cursor position
55     * `^U` will delete from the cursor position leftwards all the way to the start of the line
56     * `^K` will delete from the cursor position rightwards all the way to the end of the line
57     * `^A` moves the cursor to the start of the line
58     * `^E` moves the cursor to the end of the line
59     * `^B` and `^F` work like left and right arrows
60     * `esc` + `B` and `esc` + `F` move left and right a whole word at a time (on the Mac, Option-`left` and Option-`right` do the same)
61     * `^L` clears the screen
62     * `up`/`down` arrows let you scroll through previous lines you typed (also `^P` / `^N`)
63     * `^R` lets you search through previous lines you typed for a matching substring; to refine the search keep making the substring longer, or type `^R` again to find an earlier match. Type `esc` to exit the search and edit the currently showing command line.
64
65     If you create a file named `$HOME/.inputrc` with the contents:
66
67         set blink-matching-paren on
68
69     then you will get the further nice feature that when you type a close parenthesis, rlwrap will temporarily make the cursor jump to the matching open parenthesis, so that you can see how many close parentheses you need to type. If you want to further customize `rlwrap` or the above keycommands, you can read the documentation at `man rlwrap` and `man readline`, and also look into the Preferences of your Terminal program. (On the Mac, look under Terminal menu/Preferences/Keyboard tab.) This gets complicated quickly, but those are the places to start looking if you want to experiment. You should of course also read around on the web, rather than just changing these blindly.
70
71 3. Whether you use `utop` or `rlwrap ocaml` or just plain `ocaml`, you will need to set up some initialization commands in order to use the Juli8 libraries. You'll want to edit or create an file called `.ocamlinit` in your `$HOME` directory. The file may already be there and have some commands in it if you used OPAM.
72
73     After any commands that are already there, add these lines:
74
75         #load "str.cma";;
76         module Std = struct
77           include Pervasives
78           module List = List
79           module Random = Random
80           module String = String
81         end
82
83         #directory "+../delimcc";;
84
85         #directory "/Users/jim/.juli8/ocaml";; (* you'll have to substitute your own $HOME directory in for `/Users/jim/` *)
86
87         #use "juli8.ml";;
88
89 4. Next create a folder in your `$HOME` directory named `.juli8`. Download the Juli8 code from [[here|/code/Juli8-v1.2.tgz]]. That link will no doubt be updated frequently in April and May 2015. The current version is: 1.2, posted 4 April 2015. Copy the contents of the `Juli8` folder that you downloaded into the `$HOME/.juli8` folder that you created.
90
91     Now whenever you start up OCaml, the Juli8 OCaml libraries (including their monad components, which we'll be making extensive use of) will automatically be loaded. <!-- If later you want to load Oleg's Delimcc library, type `#load "delimcc.cma";;` then use the `Delimcc` module. -->
92
93
94 ## Using the Juli8 libraries with OCaml ##
95
96 Will add here some guidance on using the non-monadic parts of the Juli8 library. For the monadic part, [[see here|/topics/week9_using_the_monad_library]].
97
98 TODO
99
100
101 ## Setting up Haskell for use with Juli8 ##
102
103 1. When (If) you installed Haskell, we hope you did it via a method that gave you the Haskell Platform. This will give you a recent version of the Glasgow Haskell Compiler (GHC), which comes with the interactive Haskell session program `ghci`, along with the **Cabal** package manager (the analogue of OCaml's OPAM) and also with a collection of the most widely-used libraries in the Haskell community, that don't come along with GHC itself.
104
105 2. Assuming you do have Cabal, we recommend you do the following. First, find out where Cabal installs logs of its activity. On my Mac, it puts them in the folder `~/Library/Haskell/logs/world`. Now what I did was to type the following command in a Terminal:
106
107         ln -s ~/Library/Haskell/logs/world ~/.cabal
108
109     Now I can find a link to what Cabal has done inside cabal's own folder, without needing to remember or hunt down where the hell on my disk that information has been stored. (Alright, to be honest, you can skip this whole step if you want. But I recommend doing it.)
110
111 3. Still assuming you have Cabal, type the following in a Terminal:
112
113         cabal update
114         cabal install --user ghc-paths semigroups hoogle
115
116 4. If you haven't already downloaded and installed the Juli8 libraries as described in Step 4 under the earlier OCaml section, do that now. Also type the following lines in a Terminal:
117
118         ln -s ~/.juli8/haskell ~/.ghc/juli8
119
120 5. Check to see whether any of the following files exist on your system. `$HOME` will be some directory like `/Users/jim` or `/home/jim` or `C:/Documents\ and\ Settings/jim`:
121
122     * `$HOME/.ghci`
123     * `$HOME/.ghc/ghci.conf`
124     * `$HOME/"Application Data"/ghc/ghci.conf`
125
126     If you find such a file, you will add lines to it in the next step. If you don't find such a file, create one.
127
128 6. Add these lines to the `.ghci` or `ghci.conf` file identified in the previous step:
129
130         -- reads ghci commands from any file named in $GHCIRC
131         :cmd (System.Environment.getEnvironment >>= maybe (return "") readFile . lookup "GHCIRC")
132
133         -- special :commands from Juli8
134         :{
135         :cmd do { dot_ghc <- System.Directory.getAppUserDataDirectory "ghc";
136                   let { juli8 = dot_ghc ++ "/juli8";
137                         cmds = juli8 ++ "/commands" };
138                   juli8_exists <- System.Directory.doesDirectoryExist juli8;
139                   cmds_exists <- System.Directory.doesFileExist cmds;
140                   Control.Monad.when cmds_exists $ putStrLn "Loading juli8/commands ...";
141                   return $ unlines $ if cmds_exists then [":set -i"++juli8, ":script "++cmds] else if juli8_exists then [":set -i"++juli8] else [] }
142         :}
143
144         :def! url (\l->return $ ":!open "++l)
145         -- :set editor vim
146         -- :set +m -- for multiline input
147         -- :set +t -- to print types after each binding
148
149         :load Juli8
150         :mod Juli8
151
152     You may want to uncomment the `:set editor vim` line, but only if you know how to use the text editor `vim`. Other text editors you may be familiar with, and can use here are:
153
154         :set editor emacs
155         :set editor nano
156         :set editor open -a TextEdit -- Mac-only
157         :set editor bbedit -- Mac-only, see http://www.barebones.com/support/bbedit/cmd-line-tools.html
158         :set editor mate -- Mac-only, see http://manual.macromates.com/en/using_textmate_from_terminal.html
159         -- for Windows, use one of https://wiki.haskell.org/Windows#Editors
160
161     You may want to uncomment the `:set +m` line. What this does is let you type multi-line commands in the `ghci` sessions. There is a different way to do that, where you type like this:
162
163         :{
164         multiline
165           command
166         :}
167
168     but that's pretty cumbersome. The downside of having `:set +m` on is that sometimes you'll have to type an extra blank line before `ghci` will respond to your input.
169
170 If everything works, then when you start up GHCi, you should see a prompt like this:
171
172     Prelude Juli8>
173
174
175 ## What do I get from Juli8 for Haskell? ##
176
177 There are two or three benefits that Juli8 provides for Haskell, and they're not a big deal. If you're already a seasoned Haskell user, you may or may not find them helpful.
178
179 First, Juli8 comes with a bunch of extra `:commands` to use at the GHCi prompt. You can see a list of what it installs by typing `:?`. Some of the commands listed in `:?` were already present before Juli8 arrived, and are just here collected and explained in a way I find more helpful. Others are provided by Juli8 itself. Many of these are based on commands already published elsewhere on Haskell wikis and so on, so you may have installed some versions of them already yourself. I'll leave it to you to pick and choose whether anything that comes with Juli8 suits your further needs.
180
181 I developed these `:commands` on a Mac, and expect that some of the assumptions I made won't work on other systems. As the library matures, we'll try to make it work for a broader range of systems, or give specific instructions about how to customize it.
182
183 The command `:help` will give you the old, official help text, that doesn't show the extra commands installed by Juli8.
184
185 Second, Juli8 comes with a module/library that collects together a number of elements from scattered other locations in the Haskell libraries. These include the Semigroup libraries you installed in Step 3 of the above instructions, which you should use in place of Haskell's standard Data.Monoid libraries. Note that the Semigroup library provides `First a` and `Last a` types that differ from the types of the same name in Data.Monoid. Juli8 also provides `OptFirst a` and `OptLast a` types that behave more like the `First a` and `Last a` types from Data.Monoid. It also provides analogous types `OptMax a` and `OptMin a`. (If you don't know what any of this means, don't worry about it.)
186
187 Third, Juli8 comes with a module/library `IOPlus` that isn't loaded by default, but which you can load manually by saying `:add IOPlus`. This provides instances to make `IO a` a Monoid when `a` is, and to make `IO` act like an instance of the Alternative and MonadPlus typeclass. This has some limitations, and can't be done perfectly, which is why it isn't done in the standard libraries. There's also an `IOFirst` and an `IOLast`. This is more experimental than the rest of the stuff in Juli8 and may well change or be removed. I'll explain it and refine it another time.
188