Last Updated: 2018-11-21 Wed 11:02

CSCI 2041 Ocaml Setup Guide

CHANGELOG: Empty

1 Access to CSE Computing Resources

All CS students have access to the College of Science and Engineering (CSE) Computing resources. There are a variety of ways to access these. Acquaint yourself with all of them so that you can complete course work effectively.

1.1 Remote Graphical Access through Vole

The University provides graphical login to virtual Linux machines on its Vole systems via the following steps.

  1. Open the following address in a web browser: https://vole.cse.umn.edu
  2. Select a standard session (no 3D needed)
  3. Enter you University (X.500) login credentials
  4. Start a new session (XFCE Desktop)
  5. You will now be shown a Linux desktop which is reasonably fully functional.
  6. Files you save in this system will be available on any lab machine.
  7. The "window" button in the bottom toolbar opens a terminal which will be crucial for our course.
  8. The other menus provide access to many useful tools such as code editors, browsers, etc.

1.2 Remote Login in Via SSH

Graphical logins are nice but can slow down considerably if network bandwith is limited producing graphics requires transmitting much more information than just text.

A massively useful tool in any computer scientist's utility belt is the ability to remotely log into a machine on a command line terminal.

Secure Shell is protocol for doing this and many programs implement it.

  • Linux / Unix almost always comes with a suite of programs such as the ssh command by default.
  • Mac OSX has Unix underpinnings so the ssh command is available in its terminal
  • Windows users may have access to the ssh command in the PowerShell, perhaps by enabling it (alternate instructions). One can always install a program that does this such as the venerable Putty or the newer Bitvise Client.

CSE IT maintains a list of machines that are accessible via SSH here: https://cseit.umn.edu/computer-classrooms/cse-labs-unix-machine-listings

Common machines to log into include

  • apollo.cselabs.umn.edu (machine room)
  • csel-kh1250-01.cselabs.umn.edu to
  • csel-kh1250-37.cselabs.umn.edu (classroom machines)

1.3 Physical Access

The following site lists all computer classrooms and labs available for physical login: https://cseit.umn.edu/computer-classrooms

  • The most common lab is Keller Hall 4-250

2 Ocaml on UMN CSE Lab Machines

We will primarily use Ocaml in CSCI 2041 so ensure that it is available on the machine you are using. Instructions on this for CSE lab machines are below.

2.1 Ensuring Ocaml is Available

All CSE Linux machines have Ocaml installed. In most cases one simply needs to open a terminal run a suitable command such as ocaml or ocamlc as shown below.

> which ocaml                   # check where ocaml is stored
/usr/bin/ocaml

> ocaml -version                # see which version is default
The OCaml toplevel, version 4.02.3

> ocaml                         # start the ocaml top-level loop
        OCaml version 4.02.3
# 1+1;;                         # crunch some numbers
- : int = 2                     # result reported by ocaml
# #quit;;                       # exit the top level ocaml loop
>

2.2 Alternate Versions of Ocaml

If the ocaml program is not found or if one desires to run a more recent version, make use of the following module commands to determine which versions of Ocaml are available on CSE Lab machines and change to a different version. Any 4.x version will work for the course unless noted otherwise.

> module avail soft/ocaml       # determine which ocaml versions are available
---------------------- /usr/local/modules-tcl/modulefiles ----------------------
soft/ocaml/3.11  soft/ocaml/4.01  soft/ocaml/4.03(default)  soft/ocaml/4.06.0  

> module load soft/ocaml/4.06.0 # load version 4.06 for this terminal session

> which ocaml                   # ask where ocaml executable is located
/soft/ocaml-4.06.0/Linux_x86_64/bin/ocaml

> ocaml -version                # determine which version of ocaml will run
The OCaml toplevel, version 4.06.0

> ocaml                         # start ocaml top level loop
        OCaml version 4.06.0
                                # the ocaml prompt starts with a #
# 1+1;;                         # crunch some numbers
- : int = 2                     # result reported by ocaml
# #quit;;                       # exit the top level ocaml loop
>

# The following command will add loading of ocaml version 4.06 to the
# shell/terminal initialization file so that every time a terminal is
# started, version 4.06 is the default WITHOUT needing to type in
# 'module load' ever again.
> echo 'module load soft/ocaml/4.06.0' >> ~/.bashrc

3 Getting Ocaml Tools on Home Machines

It is highly liberating and expected that you have access to Ocaml on your home machine. Below are steps to do so.

3.1 Windows 10

The suggested method is to make use of the Windows Subsystem for Linux on Windows 10

  • Install a flavor of Linux such as Ubuntu (Video Tutorial)
  • Start a linux command line via the bash program
  • Follow the Linux installation instructions appropriate to the distro (see Linux/Unix instructions)

3.2 Older Windows Versions

The Ocaml web site provides some methods for native Windows install which may apply but most of these are likely dated. The easiest path to victory is to upgrade to Windows 10 or make use of lab machines.

Installing Linux/Unix virtual machine via virtualization software like Virtualbox is another possibility. It is also a great way to get experience with Unix machines without the risk of a native install.

3.3 Linux/Unix

All major distributions of Linux with package managers will have Ocaml packages. A listing of common installation commands for Linux/Unix are given here: https://ocaml.org/docs/install.html#FreeBSD

Using a package manager, installing Ocaml is as easy as

$ sudo apt install ocaml   # debian or ubuntu linux
or
$ sudo pacman -S ocaml     # arch linux 

3.4 Mac OSX

The easiest method to install Ocaml is via a third-party Unix-style package manager such as MacPorts. Installing this software allows a similar command line install a la

$ port install ocaml

4 Editing Ocaml Code

We will be reading and writing a significant number of Ocaml programs for 2041. To that end, it is worthwhile to ensure the programs are reasonably pretty to look at. Since Ocaml has not achieved the popularity of Java or C, there is somewhat less support for it in code editors. However, below are instructions on several viable candidates for use in the class.

4.1 Atom

A "hot new" editor that is relatively easy for beginners.

  • Homepage: https://atom.io/
  • Installed by default on CSE Labe machines: find it in the applications menu or just type atom on the command line though it might take some time to start up.
  • Available on a variety of platforms including Linux, Windows, Mac
  • Configure Ocaml code support using the following steps.
    1. Open Atom. Select the Edit -> Preferences menu option.

      atom-config1.png

    2. Navigate to the Install Option and type "ocaml" into the filter. The first result should be the add-on "ocaml-language". Select Install

      atom-config2.png

    3. Open any Ocaml file which has the .ml extension. One should proper syntax highlighting of the program.

      atom-config3.png

  • You may wish to examine other tools associated with Ocaml in Atom to see if they will help you work but most of the action will happen on the command line.

4.2 Emacs

The 100-year editor, not easy for beginners but favored heavily by the Ocaml community and other enlightened folk.

  • Homepage: https://www.gnu.org/software/emacs/
  • Installed by default on CSE Labe machines: find it in the applications menu or just type emacs on the command line.
  • Available on a variety of platforms including Linux, Windows, Mac
  • CSE Lab machines have the Ocaml editing mode tuareg installed by default.
  • If installing Emacs at home, install the tuareg package to get indentation and syntax highlighting support. For instructions on how to install emacs packages, examine this page.

4.3 VI and VIM

These venerable editors are highly likely to have support for Ocaml but you will need to research this on your own if you have been seduced by the dark side.

5 Compiling and Running Ocaml Programs

Copy and paste the following into a file called hello.ml

(* hello.ml : hello world program in Ocaml *)
print_endline "Hello world!";;

Open a Unix terminal and change to the directory where hello.ml is stored and follow the steps in the session below.

# Lines that start or trail with # are comments
# Lines that start with > are typed into the terminal. 
# Other lines are feedback from terminal commands.

> pwd                           # where am I?
/home/kauffman                  # in my home folder

# I saved the file in the folder "test-programs" so change to that directory
> cd test-programs
> pwd
/home/kauffman/test-programs    # now in the test-programs folder

> ls hello.ml                   # check that 
hello.ml

> ocamlc hello.ml               # compile program to bytecode 
> ls                            # show what's in the directory now
a.out  hello.cmi  hello.cmo  hello.ml

> file *                        # show what kind of data is in each file
a.out:     a /usr/bin/ocamlrun script executable (binary data)
hello.cmi: OCaml interface file (.cmi) (Version 023)
hello.cmo: OCaml object file (.cmo) (Version 023)
hello.ml:  ASCII text

# The compiled program is called "a.out": this is the traditional
# default name for compiled executables

> ./a.out                       # run the a.out program (./ for current directory)
Hello world!

> ocamlc -o say_hello hello.ml  # re-compile but name the program "say_hello"

> ls                            # show what's in the directory now
a.out  hello.cmi  hello.cmo  hello.ml  say_hello

> file say_hello                # what kind of thing is "say_hello"
say_hello: a /usr/bin/ocamlrun script executable (binary data)

> ./say_hello                   # run the program
Hello world!

Author: Chris Kauffman (kauffman@umn.edu)
Date: 2018-11-21 Wed 11:02