Instructions/suggestions to work comfortably with Ocaml
Beware, several of the following commands require confirmation with a prompt like:
> Do you want to continue? [Y/n]You need to answer "y" (yes) to confirm or "n" (no) to abort, then press the "Enter" key. The uppercase letter indicates the default choice if you simply press "Enter". Therefore, launch the commands below one by one and check if questions are asked!
Before using opam for the first time, it needs to be initialized. To do this, execute the following commands:
opam init -y --shell-setup eval $(opam env)Then, you can install opam packages with the command opam install. Install packages that will be useful for the course:
CHECK_IF_PREINSTALLED=false opam install -y ocamlbuild opam install -y menhir merlin graphics dune ocamlformat tuareg ocp-index user-setup ocaml-lsp-server opam user-setup install eval $(opam env)To use Down (an improved toplevel; does not work on Windows):
opam install down touch ~/.ocamlinit && echo '#use "down.top";;' >> ~/.ocamlinitNow you can comfortably work with OCaml, especially with an editor like Emacs or VIM (see below). If you want to use Visual Studio Code, follow the instructions below.
curl -sSL "https://code.visualstudio.com/sha/download?build=stable&os=linux-x64" -o vscode-client-linux-x64.tar.gz tar zxf vscode-client-linux-x64.tar.gz rm vscode-client-linux-x64.tar.gz echo "export PATH=\$HOME/VSCode-linux-x64/bin/:\$PATH" >> $HOME/.profile
Now, click on and search for extension
"OCaml Platform" by typing "OCaml". Install it. Now you can use it.
Click on "Open folder" to choose a folder to work on. Add a file in that folder, e.g. insertion.ml. The file should be recognized by the VSC as an OCAml file (a small orange icon with a camel. It will by type-checked on a fly dynamically and the types/errors will be shown. See more details, in particular for using VSC OCaml on windows, at the Details description of the Ocaml Platform extension.
To run an OCaml program, open a new terminal (Terminal menu) then run a command like:
ocamlopt insertion.ml -o insertion && ./insertion(or make if we use a Makefile, or dune runtest if using dune, etc.)
It is also possible to send the whole file to a toplevel OCaml with Ctrl-A (select all) then Shift-Enter.
It's in the File menu, then Preferences, and Settings, or simply Ctrl-,. There is a search bar.
For OCaml, it is essential to modify the default behavior of VSCode's automatic completion, which chooses the first possible completion when the Enter key is pressed. Without this, the keyword in at the end of the line is automatically replaced by in_channel_length! Look for the option called Editor: Accept Suggestion On Enter and set it to off.
To use the Cygwin environment within Visual Studio Code (and thus have access to OCaml from VSCode), you need to launch it with ocaml-env. You can create a shortcut (a .lnk file) to automate this (you then need to launch VSCode with this shortcut), with a command similar to "C:\Ocaml64\usr\local\bin\ocaml-env-win.exe" -- "C:\\Program Files\\Path To\\VisualStudioCode.exe". To find the location of the VSCode executable on your machine, you can refer to this StackOverflow link.
Alternative: If the above installation does not work, you can use a Linux virtual machine running on Windows (not ideal, a bit slow, but should still work). Download the course Debian live CD and install VirtualBox. Launch VirtualBox, create a new Debian 32-bit installation, start it and choose to boot from the downloaded image. Then, proceed as described below for a Debian system.
brew install opam emacs
apt-get install opam emacs
Above, we explained how to configure Visual Studio Code for OCaml.
Emacs configuration is done in a .emacs file located at the root of your account. The above installation will have added some lines to this file. In the end, it may look like something like this.
The Merlin mode compiles your program every time you save (to report errors) but does not build an executable.
To build an executable, you can call the OCaml compiler directly from Emacs with the Meta-x compile command (Meta is obtained with the Alt or Esc key, as preferred). You can rerun the same command with Meta-x recompile. If the OCaml compiler reports an error, you can navigate directly to the corresponding position with Meta-x next-error. You can simplify the keyboard shortcuts for these three commands by inserting, for example, the following lines into your ~/.emacs file:
(global-set-key [f5] 'compile) (global-set-key [f6] 'recompile) (global-set-key [f7] 'next-error)This is a very effective way to work. (And you can use these shortcuts for anything, not just OCaml programming.)
In the source code, you can access the type inferred by the compiler with Ctrl-c Ctrl-t and use completion with M-x completion-at-point. Refer to the documentation for more details.
If you prefer Ctrl-C, Ctrl-V, Ctrl-X shortcuts for copy-pasting in Emacs, you can add the line
(custom-set-variables '(cua-mode t nil (cua-base)))to your .emacs file to activate Cua Mode for Emacs. This is done in the proposed .emacs file.
let g:opamshare = substitute(system('opam config var share'),'\n$','','''') execute "set rtp+=" . g:opamshare . "/merlin/vim" filetype plugin on filetype indent off syntax enableThe first two lines allow Vim to find the location of Merlin files.
You can initiate a compilation with :make (if the compilation is handled by a Makefile). In case of an error, the cursor will be placed at the corresponding location.
In the source code, you can access the type inferred by the compiler with \ t and use completion with Ctrl-x Ctrl-o. Refer to the documentation.
back to the course page