tweaks
[lambda.git] / juli8.mdwn
index e3fa29d..a87464b 100644 (file)
@@ -1,25 +1,12 @@
 [[!toc]]
 
-<!--
-> 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:
+## What are the Juli8 Libraries? ##
 
->     #load "str.cma";;
->     module Std = struct
->       include Pervasives
->       module List = List
->       module Random = Random
->       module String = String
->     end
--->
-
-
-## Using the Juli8 Libraries ##
-
-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.
+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.
 
 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.
 
-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.
+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.
 
 ## Setting up OCaml in general, and for use with Juli8 ##
 
@@ -43,7 +30,7 @@ First we'll give instructions on how to install Juli8 into your existing Haskell
 
         module type SOMETHING = Monad.OPTION
 
-    as I directed you elsewhere (LINK), you can instead just type:
+    as [[I directed you elsewhere|/topics/week9_using_the_monad_library]], you can instead just type:
 
         #show Monad.OPTION
 
@@ -61,9 +48,28 @@ First we'll give instructions on how to install Juli8 into your existing Haskell
 
     Some keycommands you can now use are:
 
-    * TODO
-
-    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.
+    * `^H` (that is, Control+`H`) will delete one char to the left of the cursor position (also `backspace`)
+    * `^D` will delete one char to the right of the cursor position (but if you type it on a blank line it will quit the program you're running)
+    * `^W` will delete one word to the left of the cursor position (also `esc` followed by `backspace`, after releasing `esc`)
+    * `esc` + `D` will delete one word to the right of the cursor position
+    * `^U` will delete from the cursor position leftwards all the way to the start of the line
+    * `^K` will delete from the cursor position rightwards all the way to the end of the line
+    * Control+`-` will undo one editing command or sequence of typed characters
+    * `esc` + `R` will (usually) erase the whole line (undoing everything)
+    * `^A` moves the cursor to the start of the line
+    * `^E` moves the cursor to the end of the line
+    * `^B` and `^F` work like left and right arrows
+    * `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)
+    * Control-`]`+`x` will move the cursor rightwards to the next `x` character on the line (similarly for other characters in place of `x`). You can do the same leftwards by typing `esc`+Control-`]`+`x`. You can move to the second-next `x` character by typing `esc`+`2`+Control-`]`+`x`. This is all pretty cumbersome.
+    * `^L` clears the screen
+    * `up`/`down` arrows let you scroll through previous lines you typed (also `^P` / `^N`)
+    * `^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. Type `^G` to exit the search with a blank command line.
+
+    If you create a file named `$HOME/.inputrc` with the contents:
+
+        set blink-matching-paren on
+
+    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.
 
 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.
 
@@ -83,37 +89,44 @@ First we'll give instructions on how to install Juli8 into your existing Haskell
 
         #use "juli8.ml";;
 
-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.
+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.
 
     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. -->
 
 
-If you're only going to use Juli8 for OCaml and not for Haskell, you can skip ahead to later in this document...
+## Using the Juli8 libraries with OCaml ##
+
+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]].
+
+TODO
+
 
 ## Setting up Haskell for use with Juli8 ##
 
 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.
 
-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:
+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 logs them in the file `~/Library/Haskell/logs/world`. Now what I did was to type the following command in a Terminal:
 
-        ln -s ~/Library/Haskell/logs/world ~/.cabal
+        ln -s ~/Library/Haskell/logs/world ~/.cabal/
 
-    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.)
+    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.)
 
 3. Still assuming you have Cabal, type the following in a Terminal:
 
         cabal update
-        cabal install --user ghc-paths semigroups hoogle
+        cabal install --user cabal-install ghc-paths semigroups hoogle
+
+    <!-- also haskell-docs for :whatis command, but I'm not sure yet this is worth it. -->
 
-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:
+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:
 
         ln -s ~/.juli8/haskell ~/.ghc/juli8
 
 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`:
 
-    * $HOME/.ghci
-    * $HOME/.ghc/ghci.conf
-    * $HOME/"Application Data"/ghc/ghci.conf
+    * `$HOME/.ghci`
+    * `$HOME/.ghc/ghci.conf`
+    * `$HOME/"Application Data"/ghc/ghci.conf`
 
     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.
 
@@ -123,7 +136,15 @@ If you're only going to use Juli8 for OCaml and not for Haskell, you can skip ah
         :cmd (System.Environment.getEnvironment >>= maybe (return "") readFile . lookup "GHCIRC")
 
         -- special :commands from Juli8
-        :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 [] }
+        :{
+        :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 [] }
+        :}
 
         :def! url (\l->return $ ":!open "++l)
         -- :set editor vim
@@ -131,6 +152,7 @@ If you're only going to use Juli8 for OCaml and not for Haskell, you can skip ah
         -- :set +t -- to print types after each binding
 
         :load Juli8
+        :mod Juli8
 
     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:
 
@@ -141,7 +163,7 @@ If you're only going to use Juli8 for OCaml and not for Haskell, you can skip ah
         :set editor mate -- Mac-only, see http://manual.macromates.com/en/using_textmate_from_terminal.html
         -- for Windows, use one of https://wiki.haskell.org/Windows#Editors
 
-    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:
+    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:
 
         :{
         multiline
@@ -152,10 +174,20 @@ If you're only going to use Juli8 for OCaml and not for Haskell, you can skip ah
 
 If everything works, then when you start up GHCi, you should see a prompt like this:
 
-    *Juli8>
+    Prelude Juli8>
 
 
-## Using the Juli8 libraries with OCaml ##
+## What do I get from Juli8 for Haskell? ##
+
+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.
+
+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.
+
+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.
+
+The command `:help` will give you the old, official help text, that doesn't show the extra commands installed by Juli8.
+
+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.)
 
-Will add here some guidance on using the non-monadic parts of the Juli8 library. For the monadic part, [[see here]] (LINK).
+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.