OCaml

We will use the OCaml programming language throughout the course. Here are some useful OCaml resources:

x86-64

Our compiler targets the x86-64 architecture. We’ve prepared a reference (to be updated throughout the semester) for the x86-64 features we’ll be using: x86-64 reference

VM Setup (Vagrant/VirtualBox)

Below are instructions for installing the cs1260 virtual machine on your computer in order to have a standardized development environment. In order to install the necessary software we will be referring to the wonderful guide put together by the cs131 course staff. Please refer to the following sections in the guide with any changes or updates noted.

Virtual Machine Setup

  1. Software

    • Vagrant now supports up to version 6.1.x of VirtualBox.

    • Download Vagrant

    • Download VirtualBox

  2. Vagrant Setup

    • You should use our Vagrantfile instead of the one from cs131. The cs126 Vagrantfile is as follows:

       Vagrant.configure("2") do |config|
           config.vm.box = "brown-cs1260/ocaml-dev-base"
           config.vm.box_version = "1.1.4"
           config.vm.provider "virtualbox" do |vb|
               vb.customize [ "modifyvm", :id, "--uart1", "0x3F8", "4" ]
               vb.customize [ "modifyvm", :id, "--uartmode1", "file", File.join(Dir.pwd, "ubuntu-bionic-18.04-cloudimg-console.log") ]
      
               vb.memory = "2048"
           end
       end
      
  3. Using the VM

  4. Shared Folders

Git

  1. Learning Git

  2. Github Classroom setup

    • The cs1260 Github classroom links will be provided on a per-assignment basis.

Department machine setup

If the VM does not work for you, you can use the CS department machines. We recommend connecting via FastX. The tools you need to build and run the course assignments are pre-installed; you just need to configure your environment in order to locate them. This configuration will depend on your shell; instructions for common shells are listed below. Once you’ve completed these steps, continue to the next section to see how to setup OCaml support in VSCode on department machines

Bash (the default)

Add the following lines at the end of your .bashrc file. You can open this file by running nano .bashrc.

source /course/cs1260/opam/opam-init/variables.sh
dune() {
  cmd=$1
  shift
  if [ -f "dune-project" ]; then
      /course/cs1260/opam/default/bin/dune $cmd --root . $@
  else
      echo "Make sure to run dune from a project root directory (i.e. one that contains a 'dune-project' file)"
  fi
}

Zsh

Add the following lines at the end of your .zshrc file. You can open this file by running nano .zshrc.

source /course/cs1260/opam/opam-init/variables.sh
dune() {
  cmd=$1
  shift
  if [ -f "dune-project" ]; then
      /course/cs1260/opam/default/bin/dune $cmd --root . $@
  else
      echo "Make sure to run dune from a project root directory (i.e. one that contains a 'dune-project' file)"
  fi
}

Fish

Add the following lines at the end of your ~/.config/fish/config.fish file. You can open this file by running nano ~/.config/fish/config.fish.

source /course/cs1260/opam/opam-init/variables.fish
function dune
    if test -f "dune-project"
        /course/cs1260/opam/default/bin/dune $argv[1] --root . $argv[2..-1]
    else
        echo "Make sure to run dune from a project root directory (i.e. one that contains a 'dune-project' file)"
    end
end

Editor setup

Writing OCaml in VSCode in the Virtual Machine

Below are instructions for using VSCode with Vagrant to develop OCaml.

If you have another editor of choice (e.g. emacs or vim) you are free to use it. Note that your virtual machine already has the merlin and ocamlformat packages which may be of use should you choose to use a different text editor.

First, install VSCode by following these instructions. Once VSCode is installed, go navigate to View > Extensions and search for and install the Remote - SSH extension from Microsoft.

Now, open a terminal and go to the folder where you have your Vagrantfile (but do not enter your virtual machine). Make sure it is running by typing vagrant up. Then, type vagrant ssh-config --host csci1260. Copy the output to your clipboard.

Return to VSCode. Open the command palette by navigating to View > Command Palette and type Remote-SSH: Open Configuration File. In the now open file, paste the information that you copied earlier after running vagrant ssh-config --host csci1260.

You are now ready to connect to your Vagrant virtual machine with VSCode! open the command palette again and type Remote-SSH: Connect to Host. Pick csci1260 from the resulting options. VSCode will open a new window (there may be some first time setup which will take a moment to complete).

From here, you can open open the explorer by navigating to View > Explorer. Then, type /vagrant and press OK to see a view of the files in your virtual machine’s shared folder. Double click to open any file.

The virtual machine comes with OCaml support pre-configured for VSCode, which gives you many useful features like type-hints above functions and formatting-on-save.

Writing OCaml in VSCode on Department Machines

If using department machines, you’ll ned to add OCaml support to your editor of choice yourself. We recommend VSCode, and instructions for setting up OCaml in VSCode are below.

Start by opening VSCode over FastX. Navigate to the extensions tab and install the following extensions:

  • OCaml and Reason IDE

To format your code, run

dune build @fmt --auto-promote

Conclusion

You are now all set up to write OCaml! If you have issues with any of this, post on Piazza and course staff can help. Have fun coding!