Power
Installing OPAM and OCaml
Follow these instructions in Michael Clarkson's textbook, OCaml Programming: Correct + Efficient + Beautiful. In short: you need to have,
- a Unix development environment, which could be native Linux, native MacOS, or (on Windows 10/11) a WSL2 virtual machine running Linux.
- The opam package manager running under Unix.
- The OCaml compiler installed through opam.
- The VS Code editor, running native. (Under Windows your VS Code will not run inside WSL2, but it will connect to OCaml running inside WSL2.)
Optional: instead of naming your opam switch cs3110-2024sp, which has a "far above Cayuga's waters" flavor, you could name it cos326-2024fa. But it won't make much practical difference.
Text Editors
You don't have to use VS Code. You could use Emacs, or some other text editor. See our page on text editors to install here
Using utop
The lecture notes (slides) for Lecture 1 show (very briefly) the use of utop, the interactive OCaml top level. You can fire up utop and type in OCaml top-level definitions, and it will read the definition, (compile and) evaluate the definition, and print the result. (Hence, this is sometimes called a read-evaluate-print loop, or REPL).This can be very handy for exploratory experiments in how your functions work on test inputs, especially little auxiliary functions you write that help with the main tasks of the (various) assignments. For homework 1, you might do the utop command,
# #use "a1.ml";; # (* . . . now, call some functions defined in a1.ml, applied to arguments *)(note that the first # is the prompt, but you actually type the hash-sign in #use).
For assignments 2 and onward, instead of the #use command, you want to load in all the dune-compiled files, and call functions from those files. You can do that with the following command:
# #use_output "dune ocaml top";;(as the reference manual says). Now (unless you had compile errors) everything is loaded in. For the next step, I will use Assignment 2 (Boxoffice) as the example. utop shows a list of modules across the bottom:
|A2lib|A2lib__Io|A2lib__Part1|A2lib__Query|Arg|Array|....If you have implemented, for example, the perm function, then you could do:
# A2lib__Part1.perm [1;2;3];;and (if your implementation is working) utop would respond,
- : int list list = [[1; 2; 3]; [2; 1; 3]; [2; 3; 1]; [1; 3; 2]; [3; 1; 2]; [3; 2; 1]]You would probably find it more convenient to do,
# open A2lib__Part1;;and then you can use the name perm without the module-qualifier A2lib__Part1 in front of it. That is, the open statement in OCaml is like the import statement in Java.