Jump to the current lecture
This is a course on Functional Programming in OCaml [1]. It focuses on functional programming ideas, rather than pragmatic OCaml programming. Lectures 1 to 6 may appear overwhelming as they introduce many concepts. In fact, the algorithmic content of lectures 1–6 is for the most part quite simple, but I wanted them to force the students to think from scratch. Concepts from lectures 7, 8, 10 are more prevalent in Haskell, because OCaml programmers can judiciously use imperative features to avoid introducing complexity. In particular, monadic programming has better optimization support in Haskell than in OCaml. Lecture 9 (and lectures 12, 14 to come) address the pragmatics of OCaml programming directly. To be a well-rounded functional programmer, you should also complete a complementary course based on the book Pearls of Functional Algorithm Design by Richard Bird. Or you can intersperse reading Pearls of Functional Algorithm Design with following this course. (Perhaps I’ll do something about it.)
Initial lectures have examples translated to F# [2], but then I decided to focus on OCaml.
Useful links:
- New: Type OCaml [3] — impressive site teaching functional programming in OCaml.
- Awesome OCaml links.
- OCaml from the Very Beginning [4] by John Whitington (book and) a couple of video lectures for extreme beginners.
- OCaml-Top editor [5] suited for solving exercises.
- A community site for OCaml: The OCaml Programming Language [6].
- I recommend OCaml comprehensive tutorial [7]
- In particular, practice! (99 problems) [8]
- The original Prolog Problems,
- Ninety-Nine Haskell Problems [9],
- and new: S-99: Ninety-Nine Scala Problems [10].
- Our course focuses on functional programming intuitions rather than the wider practice of programming. But:
- Real World OCaml book.
- To follow-up on OCaml programming, try Developing Applications with OCaml [11].
- Or try Unix system programming in OCaml [12].
- OCaml manual [13]
- OCaml Batteries API [14]
- OCaml API search [15] only standard library and ExtLib (an old precursor of Batteries)
- F# operators and basic functions [16]
- OCaml Tutorial for functional programming beginners
Functional programming video lectures:
- Functional Programming Fundamentals [17] in Haskell, by Erik Meijer, based on Programming in Haskell [18] by Graham Hutton. (at Channel 9)
- Functional Programming with Ralf Laemmel [19], more advanced topics, in Haskell. (at Channel 9)
- Functional Programming Principles in Scala by Martin Odersky. (at Coursera)
- Introduction to Functional Programming [20] by Philip Wadler, in Haskell. (at Edinburgh)
Lectures and assigned material
The time given is how many lecture-hours a lecture actually took us, for reference: don’t be scared that lecture 6 appears as a single lecture. One meeting is two lecture-hours.
- Lecture 1: Logic (and Types). [2 hours]
- Examples: Attach:Lec1.ml, Attach:Lec1.fs.
- For further practice, you can do lessons 1–4 of the tutorial at http://try.ocamlpro.com/, and parts “Quick Language Overview” and “The Functional World” of the tutorial at http://www.tryfsharp.org/. Skip over the parts that you do not understand or that are too time-consuming. Unfortunately the F# tutorial wouldn’t work for me under Linux, but it works under Windows.
- Lecture 2: Algebra (and Algebraic Data Types) (needs some fixes) — Lecture 2 Figure. [2 hours]
- Lecture 3: Computation. [2 hours]
- Lecture 4: Functions (and Lambda Calculus). Also known as Alligator Calculus [21]. (Lecture requires OCaml.) [3 hours]
- Lecture 5: Polymorphism and Abstract Data Types. Type inference. Polymorphic recursion. Algebraic specifications. Maps. Red-black trees. [3 hours]
- Lecture 6: Generic functions, Lists. Generic programming with mapping and folding. Backtracking with lists. [5 hours]
- Lecture 7: Laziness and Streams. Lazy evaluation and stream processing. [3 hours]
- Lecture 8: Monads. List comprehensions. Monads. The module system. Probabilistic Programming. Lightweight cooperative threads. [7 hours]
- Examples: Attach:Lec8.ml.
- Exercises.
- Teaser for Probabilistic Programming paradigm [22] — from cognitive science perspective.
- Tutorial on the state monad [24] and on monad transformers [25].
- Monad syntax extension [26]
- Lecture 9: Compilation and Parsing. Working with OCaml projects. Compiling and runtime of FPLs: Garbage Collection and closures. Optimization. Writing parsers in Menhir.
- This lecture is OCaml-specific, and least functional-programming-ish in the course.
- Examples:
- Exercises.
- Reading the generated assembly [27] — compiling with
ocamlopt -S
.
- Writing Performance Sensitive OCaml Code [28]
- New: capnp-ocaml 2.0: The Road to Unembarrassing Performance [29] — a blog post about optimizing an OCaml serialization plugin.
- Lecture 10: FRP. Zippers. Adaptive Programming aka. Self-Adjusting Computation. Functional Reactive Programming i.e. temporal programming. GUIs.
- Lecture 11: the Expression Problem. Type system concepts for solving the Expression Problem with introduction to OCaml’s objects and classes. Monadic parsing and dynamic code loading.
- Examples: Attach:Lec11.zip.
- Exercises.
- Ralf Laemmel lectures on MSDN’s Channel 9: The Expression Problem [38], Haskell’s Type Classes [39].
- Developing Applications With Objective Caml. Chapter 15: Object-Oriented Programming [40], Chapter 16: Comparison of the Models of Organisation [41].
- Real World OCaml. Chapter 11: Objects, Chapter 12: Classes.
- Jacques Garrigue: Code Reuse Through Polymorphic Variants [42], Structural Types, Recursive Modules, and the Expression Problem [43].
- Graham Hutton and Erik Meijer: Monadic Parser Combinators.
- Lecture 12: Standard Libraries. Haskell’s type classes. OCaml’s alternatives Batteries and Core, and Haskell Hierarchical Libraries.
- Lecture 13: Category Theory for Functional Programmer. Invitation to Category Theory.
- Lecture 14: Parallelism. Sharing memory across processes, MPI, and more.
Contact me if you would like to receive our exam exercise sets. They are based in half on 99 problems [44].
Working in the browser:
- Tutorial OCaml toplevel [45] running on client-side in JavaScript (compiled from OCaml bytecode by js_of_ocaml).
- OCaml toplevel [46] with the same
js_of_ocaml
-based engine.
- OCaml toplevel [47] running on client-side in Java plugin (compiled from OCaml sources by ocamljava).
- http://codepad.org/ online compiler/interpreter for several languages, running on server-side.
Installing on a desktop:
The instructions below are outdated. Go to ocaml.org / Install OCaml [48] if you do not have OCaml working yet.
Installing on Windows:
- OCaml student-friendly: OCaml Installer [49]
- The installer will also install Emacs. We will have several ways to use OCaml:
- using the graphical toplevel
OCamlWin.exe
,
- using any editor, e.g.
OCamlBrowser
, copying to the toplevel ocaml.exe
and using the compiler ocamlc.exe
,
- using Emacs as we do under Linux.
- Save Attach:common.ml as
.ocamlinit
in a directory which OCaml will recognize (either “home” or where you start OCaml); or just # #use “path\to\common.ml”;;
in the toplevel (where path\to
is the directory in which you saved Attach:common.ml).
- OCaml developer-friendly: Wodi [50] (based on GODI [51])
- See also OCaml 4.0 Win readme [52].
- F#: Visual F# [53]
- InstallFSharp.msi on the page linked [54]
- (Right now I have problems using Visual Studio :p but F# Console works just fine.)
Installing on Debian-derived systems (e.g. Ubuntu):
- OCaml:
$ sudo apt-get install ocaml
$ sudo apt-get install tcl8.5-dev tk8.5-dev
(as suggested)
- We do not need Batteries, so instead, save Attach:common.ml as
.ocamlinit
in your home directory.
- But if you really want Batteries, do:
$ sudo apt-get install ocaml-batteries-included
- and save Attach:ocamlinit.ml as
.ocamlinit
in your home directory, for OCaml Batteries Included to start automatically in the toplevel.
$ ocaml
starts the toplevel.
- Instal other tools and libraries as needed, in similar manner.
- F#:
$ sudo apt-get install mono-devel mono-tools-devel libmono-winforms2.0-cil libmono-system-runtime2.0-cil
- Download fsharp2.0-1all.deb [55] (this is just one possibility…)
$ sudo dpkg -i fsharp2.0–1all.deb
$ fsharpi
starts the toplevel.
- If the toplevel fails, we try to install from sources:
$ sudo apt-get purge fsharp
$ git clone https://github.com/fsharp/fsharp.git
$ cd fsharp
$ autoreconf
$ ./configure —prefix=/usr
$ make
$ sudo make install
$ fsharpi
- (thanks to instructions at F# on Ubuntu 11.10 [56])
- I use Emacs with
fsharp-mode
, download from http://sourceforge.net/projects/fsharp-mode/ and follow instructions in the README
file.