Setting Up OCaml
To use the Core library and get familiar with OCaml, it is recommended that you install it on your computer or workstation instead of only using an online REPL (I’ve listed a couple in the resources section). Because CS51 uses Real World OCaml as its textbook, this section will set up your OCaml environment accordingly. Note that Real World OCaml on Windows is not supported by the examples in the textbook so installation on Windows is not covered. If you have OCaml set up, already know what OPAM is or don't care, feel free to move on.
Our Goal
Download OCaml, OPAM and related libraries that we'll be using in this resource and in CS51. Optional: Get utop and tuareg set up in emacs. (This is the preferred method for CS51, but I’ve linked to instructions for vim, etc.)
Other Information
Before diving into the instructions, I want to briefly touch on what each of the pieces are/do. This is in order to be thorough and for anyone’s edification. Skip to the next section for the links to installation instructions.
OCaml is the programming language of choice. It was originally known as Objective Caml and is derived from the ml family of languages. This resource dives further into some its features, such as a static type system, later on. Most importantly, at least pedagogically, to the course is that OCaml is said to bring functional, imperative, and object-oriented programming together. A relatively new language (1996), OCaml sees some use in industry.
OPAM is the OCaml package manager we’ll be using to install OCaml and related libraries. Coming from a Windows background, package managers were foreign to me. Package managers are simply a group of tools that take care of installing, updating, and deleting software packages on an OS. This is opposed to Windows machines, where installation requires downloading an .exe file. Package managers are included in most GNU/Linux OS. For instance, on the Ubuntu distribution of Linux, apt-get is the package manager that we’d use to install OPAM. Almost a dependency issue, OPAM is built using OCaml, which means that the instructions for Fedora distros of Linux, OCaml must be installed before installing OPAM. (Because I am writing and testing the code used in this resource using the first installation method listed below, if the code written here does not work because you do not have the same libraries I have installed, you can use OPAM to install the needed libraries.)
Core Library: The course assumes the installation of the Core library, a popular library (set of predefined functions) for OCaml. The Core library is an open source library built by the folks at Jane Street. The Core library is not compatible with the standard library released with OCaml, which means that some functions (e.g. List.fold_right) are defined and implemented differently. Instead, the Core library acts as a replacement for the standard library. Because this resource assumes the use of the Core library (to get you hired at Jane Street), anyone not using the Core Library will need to adjust the code to work with the standard library. We’ll also be using the core_extended, async, among other libraries.
utop is the interactive toplevel for OCaml. An interactive toplevel, or REPL (Read-evaluate-print loop), is a programming language environment that takes (reads) single commands, evaluates the commands and prints the result. A common example is the command line shell, where you enter commands and the shell executes them and prints results (if any). Using a REPL is different than compiling a program and then running it. It is recommended to use utop in conjunction with tuareg in emacs, that way we can test individual parts of code immediately, instead of needing to compile and test each time we want to run our code.
OCaml is the programming language of choice. It was originally known as Objective Caml and is derived from the ml family of languages. This resource dives further into some its features, such as a static type system, later on. Most importantly, at least pedagogically, to the course is that OCaml is said to bring functional, imperative, and object-oriented programming together. A relatively new language (1996), OCaml sees some use in industry.
OPAM is the OCaml package manager we’ll be using to install OCaml and related libraries. Coming from a Windows background, package managers were foreign to me. Package managers are simply a group of tools that take care of installing, updating, and deleting software packages on an OS. This is opposed to Windows machines, where installation requires downloading an .exe file. Package managers are included in most GNU/Linux OS. For instance, on the Ubuntu distribution of Linux, apt-get is the package manager that we’d use to install OPAM. Almost a dependency issue, OPAM is built using OCaml, which means that the instructions for Fedora distros of Linux, OCaml must be installed before installing OPAM. (Because I am writing and testing the code used in this resource using the first installation method listed below, if the code written here does not work because you do not have the same libraries I have installed, you can use OPAM to install the needed libraries.)
Core Library: The course assumes the installation of the Core library, a popular library (set of predefined functions) for OCaml. The Core library is an open source library built by the folks at Jane Street. The Core library is not compatible with the standard library released with OCaml, which means that some functions (e.g. List.fold_right) are defined and implemented differently. Instead, the Core library acts as a replacement for the standard library. Because this resource assumes the use of the Core library (to get you hired at Jane Street), anyone not using the Core Library will need to adjust the code to work with the standard library. We’ll also be using the core_extended, async, among other libraries.
utop is the interactive toplevel for OCaml. An interactive toplevel, or REPL (Read-evaluate-print loop), is a programming language environment that takes (reads) single commands, evaluates the commands and prints the result. A common example is the command line shell, where you enter commands and the shell executes them and prints results (if any). Using a REPL is different than compiling a program and then running it. It is recommended to use utop in conjunction with tuareg in emacs, that way we can test individual parts of code immediately, instead of needing to compile and test each time we want to run our code.
emacs is a popular text editor and can be customized to fit any user’s needs. Because of its extensibility and the ease of using utop and tuareg, it is the recommended text editor for CS51. Students used to gedit from CS50 will encounter numerous environment differences, such as the fact that Cntr+S no longer saves. But emacs has a number of awesome commands that, when learned, can turn you into an editing wizard. It goes without saying, but it’ll be said anyway, there is always a relevant xkcd.
|
tuareg is an emacs mode. An emacs mode is simply a lisp (the language emacs is built in, yay functional programming!) library that extends the functionality of emacs. emacs modes are part of the reason why people can use emacs so efficiently for everything from writing essays to sending emails. tuareg also us to run utop in emacs and provides a dedicated buffer for doing so. I had a fellow classmate program in gedit the entire semester and didn’t understand how he did so. More information on tuareg can be found here.
Instructions
Method 1 (Using the CS50 Appliance): For anyone without a Mac or Linux OS that they feel comfortable developing on, the easiest (in terms of the course) route to setting up a Linux virtual machine and the OCaml installation is to simply set up the CS50 linux distro (ver. 19). The CS50 appliance is the distro of Linux that is used to teach CS50, the first semester introductory programming course at Harvard. All that is required to get a functioning OS in which to learn OCaml is some disk space and some waiting time.
Here are CS50’s instructions for downloading Ver. 19 of the appliance. A quick rundown follows:
First, you will need to download a software hypervisor, a piece of software that lets you run a virtual machine on your computer. VMWare Fusion/Player and VirtualBox are two such tools.
Second, you will download the appliance itself.
Finally, after opening the appliance in your hypervisor, updating the appliance and getting it set up to your liking, run the following command at the command prompt (We are now following instructions found here):
Here are CS50’s instructions for downloading Ver. 19 of the appliance. A quick rundown follows:
First, you will need to download a software hypervisor, a piece of software that lets you run a virtual machine on your computer. VMWare Fusion/Player and VirtualBox are two such tools.
Second, you will download the appliance itself.
Finally, after opening the appliance in your hypervisor, updating the appliance and getting it set up to your liking, run the following command at the command prompt (We are now following instructions found here):
curl -s http://sites.fas.harvard.edu/~cs51/setup51.sh | sh
This will set up OPAM, OCaml, and the necessary libraries, fix up tuareg in emacs, etc. After it finishes, run the following command at the command prompt
source ~/.bashrc
Now you should be able to go into emacs, hit Ctrl+c and Ctrl+b (C-c C-b), and run an entire .ml file in utop in emacs. Ctrl-c Ctrl-e (C-c C-e) will run individual commands. We’ll review this later as well.
Method 2 (via Real World OCaml): This method is lengthier. As such, I'll just allow you to follow the instructions linked.
Method 3 (via OCaml.org): If you want to make it on your own and make your development environment yours, the most generic, complete list of installation instructions can be found thanks to our friends at OCaml.org.
Method 3 (via OCaml.org): If you want to make it on your own and make your development environment yours, the most generic, complete list of installation instructions can be found thanks to our friends at OCaml.org.