OCaml Lecture Information
- lecture.ml, the file we work through interactively in class.
- For the separate compilation topic we cover this example (zipped)
Our ML DialectWe are going to use OCaml, version 4.07.1. Earlier/later versions will work perfectly fine for your own coding, but the binary distibutions of the FbDK are very picky on which version they are run under and so by that point in the course you really need to have 4.07.1 running.
Installing OCaml 4.07.1 on Your MachineWe require that you use the OPAM packaging system for OCaml. OPAM will allow you to easily install some necessary OCaml extensions.
- For Linux or Mac see The OPAM install page for instructions. Depending on which method you use you may then need to run some terminal commands to set up the basics:
opam initto initialize OPAM;
opam switch create 4.07.1to build OCaml version 4.07.1;
eval `opam config env`to let the shell know where the OPAM files are; and
alsoadd the very same line,
eval `opam config env`, to your
.bashrcshell init file so you will not have to keep doing that over and over.
- The OCaml toolchain is not good in straight Windows.
- If you are running Windows 10, try the new MSFT Bash on Windows mode which once you have set up will allow you to follow the Linux Ubuntu install instructions to get opam. Windows 10 only for this unfortunately and it may have some incompatibilities. here is a step-by-step guide for installing OCaml under Bash on Windows; this document is a bit out of date so issue the opam commands as above/below once you have opam properly installed.
- Otherwise, you will need to set up a Linux VM on your Windows box, and then set up a Linux install of OCaml within the VM. There are many good tutorials on how to build a Linux VM, here is one of them. Once your virtual Linux box is set up, you can follow the Linux install instructions.
OCaml 4.07.1 should be available on all department Linux machines via the shell command ocaml.
Required OPAM Standard packagesOnce you have the basics installed, run the following command to install additional necessary packages for the class:
opam install ocamlbuild merlin ocp-indent user-setup tuareg menhirFor the course we will be implicitly assuming you installed all of these OPAM packages; some things will not work if these are not installed.
OCaml development environments
You should definitely use one of Atom, VSCode, Emacs, or Vim since they have OCaml-specific features such as syntax highliting, auto-indent, and lint analysis to make the coding process much smoother. If you are using a VM under Windows, you should run one of these editors within the VM to take advantage of the OCaml modes.
- Install Atom, read a tutorial if you are not so familiar with it, and then install the atom and apm shell commands (see the Atom..Install Shell Commands menu option on Macs, or type shift-command-p(⇧⌘P) and then in the box type command Window: Install Shell Commands). With those commands installed, type into a terminal
apm install language-ocaml linter ocaml-indent ocaml-merlin ocaml-replto install the relevant OCaml packages. Here is some documentation on the different extensions: merlin (syntax highlighting, auto-completion with control-space, etc); Repl (run an OCaml top-loop within Atom; you should install Atom package ocaml-repl instead of repl to patch some bugs in Repl for OCaml use) ). Here are some handy Atom keymaps for common operations these extensions support -- add this to your .atom/keymap.cson file:
'atom-text-editor[data-grammar="source ocaml"]': 'ctrl-shift-t': 'ocaml-merlin:show-type' 'alt-shift-r': 'ocaml-merlin:rename-variable' 'ctrl-shift-l': 'linter:lint' 'ctrl-alt-f': 'ocaml-indent:file'linter:lint above is particularly useful if you are using the FbDK, it will refresh the lint data based on the latest compiled version of your code. In addition, control-space should auto-complete, control-y control-o should start up an OCaml top-loop, and once you have the top loop started you can type control-y control-s to one-key copy/paste selected code into the top loop.
- Visual Studio Code
- VSCode now has very good OCaml support. Install the OCaml and Reason IDE extension to get syntax highlighting, type information, etc: from the View menu select Extensions, then type in OCaml and this extension will show up; install it. You can also easily run an ocaml shell from within VSCode, just open it up from the Terminal menu and type ocaml into the terminal.
- To link Tuareg (syntax highlighting) and Merlin (type display and error finding) to emacs (make sure you first have those OPAM packages installed as above), type opam user-setup install which will patch your emacs startup files - restart emacs and Tuareg and Merlin should work.
In emacs, every file that ends in .ml will automatically load Tuareg mode. The tuareg-mode distribution includes no documentation but is easy to use; control-h m from within emacs gets you the command list. The main useful commands (C-c means control-C, etc) are
C-c C-e evaluate the phrase the cursor is sitting in the middle of C-c C-r evaluate the whole selected region C-c C-s show the shell window; will automatically start an ocaml shell if not running C-c ` go to exact error location in the source
Merlin mode is useful because it can also display types of expressions and take you to a definition of a variable from its use. See here for more information on Merlin. Useful keys available include
C-c C-l from a variable use jump to its definition. C-c & merlin-pop-stack (go back to where you were before above) C-c C-t display the type of the enclosing expression.
- If you like vim, just type
opam user-setup installto set up syntax highlighting, tab completion, displaying types, etc. See here for some dense documentation.
OCaml with the Book's FbDK
See the FbDK README for information on how to use the FbDK.
utop: A better top-loop
- There is a newer top loop which you can install easily with OPAM: just type
opam install utop.
- With utop you can scroll up and down your OCaml input history, do simple editing on previous inputs, etc.
- See the utop README for more information on what it can do for you.
The OCaml Manual
The OCaml manual (download your local copy from the links at the top). We only need a subset of the manual, below is the Cliff Notes version of the table of contents.
- Part I is the tutorial introduction; Part I Chapter 1 is a brief but readable introduction to the core language. This will be our main reference. The only other chapter in Part I you may need to reference is the module system.
- Part II is the language reference and you can probably skip it.
- Part III documents ocamlc, the batch compiler, and other command-line tools; we only use it in simple ways but look here if you want to know more.
- Part IV describes the libraries; you probably only need to reference the first two chapters here.
- The Core Library chapter describes standard types and exceptions built into OCaml, and the Pervasives module referenced there includes all basic int/bool/float/string/etc operations such as <, mod, etc which are always available.
- The Standard Library chapter includes standard data structures such as hashtables and stacks as well as library operations for lists, arrays, strings, printing, etc.
Additional OCaml Information
- This is the home of OCaml for finding downloads, documentation, etc. The tutorials are also very good and there is a page of books.
- OCaml Youtube Tutorial
- If you like watching videos to learn things here is a complete collection for OCaml.
- UPenn OCaml Style Guide
- A guide to proper formatting and other style concerns for OCaml.