OCaml programming language
Created:
OCaml
for the impatient. The web’s quickest and dirtiest OCaml getting
started guide
This is the most up to date setup using Opam,
jbuilder, Merlin, VSCode etc.,(. August 2017). (ed note,
2023-10-10: Since then the OCaml system has made huge strides in
developer productivity.)
My notes
emacs
+ utop
+ merlin
+
typerex
= Excellent IDE.
2014-11-25: Run ocamlbrowser
in a shell to see a
tk
based documentaiton browser.
** OCamlEditor **
Fortunately, you can install it using
opam install ocamleditor
… unfortunately you are dealing
with various idiosyncrasis of gtk on mac… ugh.. To keep it short,
lablgtk
installation by opam might fail with
pkg-config
errors. The fix is:
https://github.com/Homebrew/homebrew/issues/14123#issuecomment-37896791
… before running opam install ocamleditor
. Solution via
Note: looks butt-ugly on mac, as expected.
opam
allows you create “sandboxes” and install packages
in them. See the -A --alias-of
option.
See Setting
up an OCaml environment for Programming. Use ocamlfind
,
ocamlbuild
and make
.
Apr 2016. See ogen – a tool for creating new OCaml projects with OPAM, Oasis, and Merlin.
Compiling an ocaml file:
That has no dependecies
ocamlopt foo.ml -o foo
that has a dependency (say csv)
ocamlfind ocamlopt -o test -linkpkg -package csv test.ml
ocamlfind
ocamlfind
is a program that finds all the ocaml packages that are installed on your system (or opam instance, for that matter).ocamlfind -h
to see list of commands available.ocamlfind printconf
shows the path configs etc.,ocamlfind list
will list all the packages installed.
ocamldep
typical usage: ocamldep *.ml
, which will dumps
dependency lines.
** Different file types **
.cmo
–.cmi
–.mli
–
Ocaml has good study materials – textbooks, lecture notes. There is also the advantage of being able to roll-over to F# for “enterprise” stuff.
Language
Concurrency
Learning OCaml
Courseware
- Cornell’s 3110 – Data Structures and Functional Programming course (2013); 2016 2018; 2020
- Cornell’s Functional programming in OCaml, CSCI 49201.
- Harvard’s CS51 – Introduction to Computer Science II: Abstraction and Design
- Functional Programming – Cornell, 2016 by Michael Clarkson
- Computer Laboratory – Course pages 2016–17: Types
- Computer Laboratory – Course pages 2016–17: Advanced Functional Programming
- CS
3110 Tournament History (CS.Cornell)
- 2017 fall
- Functional Programming in OCaml – Fall 2018 Edition Cornell’s cs3110, compiled into a gitbook.
- Functional Data Structures by PLRagde at Waterloo
- Functional Programming in OCaml - Spring 2019 Edition
- Introductory Data Structures and Algorithms — YSC2229 2021 This course on classic imperative data structures and algorithms has been offered by Ilya Sergey at Yale-NUS College in 2019-2021.
Tutorials
- Introduction to OCaml
- OCaml for the skeptical
- Using, Understanding, and Unraveling The OCaml Language by Didier Remy [2002].
- An overview of OCaml [2015].
- OCaml and Emily In a Walnut – Custom Designed For C++ and Java Programmers
- Early OCaml workflow pains
- ocamllabs/ocaml-effects-tutorial: Concurrent Programming with Effect Handlers (CUFP’17)
- A Tutorial to OCaml -ppx Language Extensions | Victor Alexandru Darvariu
- dmbaturin’s blog - ocaml and specifically - introduction to OCaml
- Getting Started with OCaml in 2021 · Perpetually Curious Blog
Best practices
Books
- Developing applications with OCaml – online book by INRIA.
- Unix System Programming in OCaml
- How to Think like Computer Scientist (OCaml)
- Fun of Programming – a work in progress.
- Real World OCaml – expected in Fall 2012(!)
Textbooks
- uds-psl/Prog – Introduction to Functional Programming and the Structure of Programming Languages using OCaml by Gert Smolka
- OCaml Programming: Correct + Efficient + Beautiful — OCaml Programming: Correct + Efficient + Beautiful
Articles
- How OCaml Type Checker works. In general Olegs <http://okmij.org/ftp/> is a treasure trove of CS material.
- Memory Layout
in Ocaml – comment by
avsm
- Why OCaml [2002]. Also has many links to OCaml pages. - OCaml programming with emacs.
- Another Why OCaml
- OCaml briefly – a brief tour of OCaml.
- What is the appeal of OCaml today? Why use it over something like Haskell or Java (reddit); 2018-07-11.
Blogs
- Danny Willems - Blog - You have ideas, I have skills.
- let author = “Rudi Grinberg” — Rudi Grinberg documentation
- Shayne Fletcher
- dmbaturin’s blog - ocaml tagged articles – has a series on “Introduction to OCaml”. Also notable - If you think ReasonML compiles to JS, you are wrong
- Jane Street Tech Blog
Resources
- Learn OCaml v.0.14.1 – François Pottier designed a series of advanced interactive OCaml exercises where students implement: - The nondeterminism monad - Parser combinators - An algebraic pretty-printer & this is an in-browser IDE!
- TryOCaml - This site is a demo for the programming environment used during the first session of the OCaml MOOC.
- Ocaml Reddit
- OCaml for the Skeptical: OCaml in a Nutshell
Papers
- Polymorphism, subtyping and type inference in MLsub; online editor of the inference enginer
- PhD Thesis: Tierless Web programming in ML | Drup’s thingies
Package managers
- opam
- GODI
- YYPkg
- Using jbuilder/topkg – “Platforms, Packaging, Progress”; [Aug 2017]
- ~lthms: Building Static Binaries for OCaml Projects; Dec 2023.
OCaml Languages
Reason: a syntax for OCaml, so native development or melange development to compile to JS
ReScript: a syntax similar to Reason, but the entire toolchain. Closer to JavaScript, can’t compile to native.
Talks
Yaron Minsky:
- OCaml for the masses in ACMQueue.
- Effective ML talk at Harvard and “Caml Trading” talk at CMU.
- More Effective ML
Miscellaneous
- OCaml Skeleton a jumpstart environment for to get code up-and-running. An example application with unit tests is a nice sandbox for language discovery.
- Try Ocaml a web based OCaml REPL.
- OCaml style guide
- another OCaml style guide from UPenn, which is based on,
- SML style guide
OCaml as a replacement language
- Replacing Python with OCaml. Thread and thread2
- Felix is offered an alternate to OCaml
- OCaml vs Haskell discussion on Quora. YMMV.
- 8 months of OCaml after 8 years of Haskell in production - DEV Community – has a table comparing OCaml and haskell
- OCamlers’ critique of Haskell
Writing languages using OCaml
- Developing Applications with Ocaml - Chapter 6 implements a BASIC interpreter.
- The programming language zoo by Andrej Bauer has multiple examples of simple languages built using OCaml.
- Toy APL implementation by Kragen Sitaker.
- CS 421/521: Compilers and Interpreters
- Lua-ML pure OCaml implementation of Lua 2.5 by Normal Ramsey.
- ML to C++ compiler
Tools, ecosystem and libraries
- Setting Up an OCaml Development Environment in Debian - The Always Learning Marty ; Aug 2018
- OCaml Lint detects common errors in OCaml code.
- IOCaml, OCaml kernel for iPython.
- Domical = OCaml + Iodide - Iodide; Sep 2018.
- Regular expressions in OCaml
- Opam-Android –An OCaml 4.02 toolchain for Android and several useful libraries
- OCaml Scientific Computing — Owl Numerical Library
Js_of_Ocaml
- js-skeleton
– sample project showing how to build a Javascript web-app using
js_of_ocaml
,TyXML
andReact
- H.261 Video Decoding in OCaml. – This notebook will demonstrate a simple H.261 Video Decoder written in OCaml and compiled to javascript with js_of_ocaml.
OCaml project tools
Build tools:
- OMake
- OCamlbuild
- OASIS
- opam-publish – A tool to ease contributions to opam repositories
Some opinions on the merits of using these tools:
- What is the preferred
way to structure and build OCaml projects?
- recco: use
ocamlbuild
butoasis
may be better now. ocamlbuild
comes with the distribution.oasis
buildsmyocamlbuild.ml
file automatically for you.oasis
gives you access tooasis-db
, a CPAN like system.- to manage libraries, use
ocamlfind
. opam
really doesn’t care about build systems.- MLState is using
ocamlbuild
for buildingOpa lang
.
- recco: use
- Another thread recommending OASIS
Code bits
Explicit Typing of a polymorphic variant
type download_result =
[ `aborted_by_userof string
| `network_failure of filepath ] | `success
Information
- What is good about OCaml’s module system?
- What good is First class Modules?
- “Using an error monad in a language like ocaml is superior to exceptions in almost every way.” via
Sample code / Read Source
- github.com/chrismamo1/zipper – a CLI tool in the imperative style.
- collaborative text editor using Eliom
- vbmithr (Vincent Bernardoff) / Repositories
- bcpierce00/unison: Unison file synchronizer
- Gopiandcode/ocamlot: An Activitypub server in OCaml!
Multicore OCaml
- Presentation by Dolan, White and Madhavapeddy 2014.
- Multicore OCaml - What’s coming in 2021 - Speaker Deck; Anil and KCSRK
- Practical OCaml, Multicore Edition by Yawar Amin
Object oriened OCaml
- The O in OCaml – a reddit discussion, with strong answers from ‘gasche’.
- Experiences with OCaml objects - Thomas Leonard’s blog
Hindley-Milner type system
- Show HN: Hindley-Milner Type Inference Algorithm in OCaml | Hacker News see links within.
Semantics
Web programming with OCaml
PPX
Comparison
- OCaml vs haskell vs go
- Thread by @wilfredh: I’m a lisper who has spend a little over six months getting up to speed in OCaml, and it’s been really interesting…
- How does OCaml compare with haskell?; HN thread.
min-caml
esumii/min-caml: a complete optimizing native-code compiler for a reasonably usable subset of OCaml, in 2000 lines of OCaml. Paper.
Running log
PSA: Tired of inscrutable type errors from OCaml? If you use jbuilder/dune, just add this to your jbuild build stanza:
(flags (:standard -short-paths))
and your type errors and inferred types from Merlin will magically improve!
NixOS
Data Science and AI
- OCaml Scientific Computing: Functional Programming in Data Science and Artificial Intelligence (Undergraduate Topics in Computer Science): Wang, Liang, Zhao, Jianxin, Mortier, Richard: 9783030976446: Amazon.com: Books (Book); Springer link