e3fa29d6e79b33bd166934333fb8851f9a600df1
[lambda.git] / juli8.mdwn
1 [[!toc]]
2
3 <!--
4 > Here are some OCaml libraries: [[Juli8|code/juli8.ml]] and [[Monad|code/monad.ml]]. We'll write up explanations of these soon. But quickly, before you `#use "juli8.ml"`, you have to run this in your OCaml session. (I have these lines in my `~/.ocamlinit` file, so they run every time OCaml starts up:
5
6 >     #load "str.cma";;
7 >     module Std = struct
8 >       include Pervasives
9 >       module List = List
10 >       module Random = Random
11 >       module String = String
12 >     end
13 -->
14
15
16 ## Using the Juli8 Libraries ##
17
18 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 last week (LINK) 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.
19
20 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.
21
22 First we'll give instructions on how to install Juli8 into your existing Haskell and/or OCaml setups. Then we'll give instructions on 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 here (LINK). At the end, we say a little bit about what Juli8 provides you if you're using Haskell.
23
24 ## Setting up OCaml in general, and for use with Juli8 ##
25
26 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`).
27
28         opam update
29         opam upgrade
30         opam install delimcc
31
32     We'll comment on the `delimcc` package in a moment.
33
34     We also recommend upgrading to one of the more recent versions of OCaml. This gets you the most recent stable release:
35
36         opam switch 4.02.1 && opam install --switch=4.02.1 delimcc && eval `opam config env`
37
38     This will get you a slightly older release, which however has "improved type error messages":
39
40         opam switch 4.02.0+improved-errors && opam install --switch=4.02.0+improved-errors delimcc && eval `opam config env`
41
42     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:
43
44         module type SOMETHING = Monad.OPTION
45
46     as I directed you elsewhere (LINK), you can instead just type:
47
48         #show Monad.OPTION
49
50 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:
51
52     > 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:
53
54     >     ./configure --prefix=$HOME && make && make install
55
56     > 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:
57
58     >     ./configure --prefix=$HOME && make CFLAGS="-g -O2 -I $HOME/include -L $HOME/lib" && make install
59
60     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.)
61
62     Some keycommands you can now use are:
63
64     * TODO
65
66     If you want to customize `rlwrap` or these 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.
67
68 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.
69
70     After any commands that are already there, add these lines:
71
72         #load "str.cma";;
73         module Std = struct
74           include Pervasives
75           module List = List
76           module Random = Random
77           module String = String
78         end
79
80         #directory "+../delimcc";;
81
82         #directory "/Users/jim/.juli8/ocaml";; (* you'll have to substitute your own $HOME directory in for `/Users/jim/` *)
83
84         #use "juli8.ml";;
85
86 4. Next create a folder in your `$HOME` directory named `.juli8`. Download the Juli8 code from [[here]] (LINK). 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.
87
88     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. -->
89
90
91 If you're only going to use Juli8 for OCaml and not for Haskell, you can skip ahead to later in this document...
92
93 ## Setting up Haskell for use with Juli8 ##
94
95 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.
96
97 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:
98
99         ln -s ~/Library/Haskell/logs/world ~/.cabal
100
101     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.)
102
103 3. Still assuming you have Cabal, type the following in a Terminal:
104
105         cabal update
106         cabal install --user ghc-paths semigroups hoogle
107
108 4. If you haven't already downloaded and installed the Juli8 libraries as described in Step 4 under the previous OCaml section, do that now. Also type the following lines in a Terminal:
109
110         ln -s ~/.juli8/haskell ~/.ghc/juli8
111
112 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`:
113
114     * $HOME/.ghci
115     * $HOME/.ghc/ghci.conf
116     * $HOME/"Application Data"/ghc/ghci.conf
117
118     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.
119
120 6. Add these lines to the `.ghci` or `ghci.conf` file identified in the previous step:
121
122         -- reads ghci commands from any file named in $GHCIRC
123         :cmd (System.Environment.getEnvironment >>= maybe (return "") readFile . lookup "GHCIRC")
124
125         -- special :commands from Juli8
126         :cmd do { dot_ghc <- System.Directory.getAppUserDataDirectory "ghc"; let { juli8 = dot_ghc ++ "/juli8"; cmds = juli8 ++ "/commands" }; juli8_exists <- System.Directory.doesDirectoryExist juli8; cmds_exists <- System.Directory.doesFileExist cmds; Control.Monad.when cmds_exists $ putStrLn "Loading juli8/commands ..."; return $ unlines $ if cmds_exists then [":set -i"++juli8, ":script "++cmds] else if juli8_exists then [":set -i"++juli8] else [] }
127
128         :def! url (\l->return $ ":!open "++l)
129         -- :set editor vim
130         -- :set +m -- for multiline input
131         -- :set +t -- to print types after each binding
132
133         :load Juli8
134
135     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:
136
137         :set editor emacs
138         :set editor nano
139         :set editor open -a TextEdit -- Mac-only
140         :set editor bbedit -- Mac-only, see http://www.barebones.com/support/bbedit/cmd-line-tools.html
141         :set editor mate -- Mac-only, see http://manual.macromates.com/en/using_textmate_from_terminal.html
142         -- for Windows, use one of https://wiki.haskell.org/Windows#Editors
143
144     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 diffeent way to do that, where you type like this:
145
146         :{
147         multiline
148           command
149         :}
150
151     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.
152
153 If everything works, then when you start up GHCi, you should see a prompt like this:
154
155     *Juli8>
156
157
158 ## Using the Juli8 libraries with OCaml ##
159
160 Will add here some guidance on using the non-monadic parts of the Juli8 library. For the monadic part, [[see here]] (LINK).
161