# OCaml programming language

- Page: https://shared.btbytes.com/p/ocaml
- Date: 2020-02-25
- Tags: ocaml, programming-language

[OCaml for the impatient ](https://adambard.com/blog/getting-started-with-ocaml/). <q>The web's quickest and dirtiest OCaml getting started guide</q> 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.)

[xvw.lol - Why I chose OCaml as my primary language](https://xvw.lol/en/articles/why-ocaml.html), an *opinion* piece by a programmer that uses OCaml professionally at `tarides`. Is a good intro the ecosystem in 2025; [via](https://news.ycombinator.com/item?id=44891759)


##  Language

* [OCSF – The OCaml Software Foundation](https://ocaml-sf.github.io/index.html)

## Concurrency

- [Automated Trading and OCaml with Yaron Minsky - Software Engineering Daily](http://softwareengineeringdaily.com/2015/11/09/automated-trading-and-ocaml-with-yaron-minsky/)


## Learning OCaml

### Courseware

- [Cornell's 3110 -- Data Structures and Functional Programming course (2013)](http://www.cs.cornell.edu/courses/cs3110/2013sp/); [2016](http://www.cs.cornell.edu/courses/cs3110/2016fa/) [2018](https://www.cs.cornell.edu/courses/cs3110/2018fa/textbook/); [2020](https://www.cs.cornell.edu/courses/cs3110/2020sp/textbook/)
- [Cornell's Functional programming in OCaml, CSCI 49201](https://a-nikolaev.github.io/fp/).
- [Harvard's CS51 -- Introduction to Computer Science II: Abstraction and Design](http://cs51.seas.harvard.edu/index.html)
- [Functional Programming](http://web.archive.org/web/20170426120954/http://www.cs.cornell.edu/%7Eclarkson/courses/sjtu/2016su/) -- Cornell, 2016 by Michael Clarkson
- [Computer Laboratory – Course pages 2016–17: Types](http://www.cl.cam.ac.uk/teaching/1617/Types/)
- [Computer Laboratory – Course pages 2016–17: Advanced Functional Programming](https://www.cl.cam.ac.uk/teaching/1617/L28/)
- [CS 3110 Tournament History](http://www.cs.cornell.edu/courses/cs3110/archive/tournaments.html) (CS.Cornell)
  - [2017 fall](https://www.cs.cornell.edu/courses/cs3110/2017fa/)
  - [Functional Programming in OCaml -- Fall 2018 Edition](https://www.cs.cornell.edu/courses/cs3110/2018fa/textbook/) Cornell's cs3110, compiled into a gitbook.
- [Functional Data Structures](https://cs.uwaterloo.ca/~plragde/flaneries/FDS/) by PLRagde at Waterloo
- [Functional Programming in OCaml - Spring 2019 Edition](https://www.cs.cornell.edu/courses/cs3110/2019sp/textbook/)
- [Introductory Data Structures and Algorithms — YSC2229 2021](https://ilyasergey.net/YSC2229/) This course on classic imperative data structures and algorithms has been offered by [Ilya Sergey](https://ilyasergey.net/) at Yale-NUS College in 2019-2021.


### Tutorials

- [Introduction to OCaml](http://web.archive.org/web/20041213110411/http://www.cs.jhu.edu/~scott/pl/lectures/caml-intro.html)
- [OCaml for the skeptical](http://www2.lib.uchicago.edu/keith/ocaml-class/)
- [Using, Understanding, and Unraveling The OCaml Language](http://cristal.inria.fr/~remy/cours/appsem/) by Didier Remy [2002].
- [An overview of OCaml](http://web.archive.org/web/20150719013415/http://simongrondin.name/?p=330) [2015].
- [OCaml and Emily In a Walnut -- Custom Designed For C++ and Java Programmers](http://www.skyhunter.com/marcs/emilyWalnut.html)
- [Early OCaml workflow pains](https://www.youtube.com/watch?v=rDFocxR6Mpw)
- [ocamllabs/ocaml-effects-tutorial: Concurrent Programming with Effect Handlers (CUFP'17)](https://github.com/ocamllabs/ocaml-effects-tutorial#1-algebraic-effects-and-handlers)
-  [A Tutorial to OCaml -ppx Language Extensions | Victor Alexandru Darvariu](https://www.victor.darvariu.me/jekyll/update/2018/06/19/ppx-tutorial.html)
- [dmbaturin's blog - ocaml](http://web.archive.org/web/20191021110124/https://blog.baturin.org/tag/ocaml.html) and specifically - [introduction to OCaml](http://web.archive.org/web/20200620090301/https://ocaml-book.baturin.org/)
- [Getting Started with OCaml in 2021 · Perpetually Curious Blog](https://lambdafoo.com/posts/2021-10-29-getting-started-with-ocaml.html)

### Best practices

* Our current OCaml best practices. --  [part 1](https://engineering.issuu.com/2018/11/20/our-current-ocaml-best-practices-part-1) and [part 2](https://engineering.issuu.com/2018/12/10/our-current-ocaml-best-practices-part-2) by Issuu Engineering; Dec 2018.


### Books

-   [Developing applications with
    OCaml](http://caml.inria.fr/pub/docs/oreilly-book/html/index.html)
    -- online book by INRIA.
-   [Unix System Programming in
    OCaml](http://ocamlunix.forge.ocamlcore.org/)
-   [How to Think like Computer Scientist
    (OCaml)](http://www.thinkocaml.com/)
-   [Fun of Programming](https://github.com/bobzhang/ocaml-book) -- a
    work in progress.
-   [Real World OCaml](http://realworldocaml.org/) -- expected in Fall
    2012(!)

### Textbooks

- [uds-psl/Prog](https://github.com/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](https://cs3110.github.io/textbook/cover.html)

### Articles

- [How OCaml Type Checker works](http://okmij.org/ftp/ML/generalization.html). In general Olegs [<http://okmij.org/ftp/>](http://okmij.org/ftp/) is a treasure trove of CS material.
-   [Memory Layout in Ocaml](https://news.ycombinator.com/item?id=3641535) -- comment by `avsm`
- [Why OCaml in 2024](https://kcsrk.info/slides/why_ocaml_in_2024.pdf) by `kcsrk`
- [Why OCaml](http://www.cs.ubc.ca/~murphyk/Software/Ocaml/why_ocaml.html) [2002]. Also has many links to OCaml pages. - [OCaml programming with
emacs](http://chaudhuri.info/misc/osetup/).
- Another [Why  OCaml](http://www2.lib.uchicago.edu/keith/ocaml-class/why.html)
- [OCaml briefly](http://mads379.github.io/ocaml/2014/11/13/ocaml-briefly.html) -- a brief tour of OCaml.
- [What is the appeal of OCaml today? Why use it over something like Haskell or Java](https://www.reddit.com/r/programming/comments/8xy6mb/ocaml_4070_released/e27ahr2) (reddit); 2018-07-11.

###  Blogs

- [Danny Willems - Blog - You have ideas, I have skills.](https://blog.danny-willems.be/en/)
    + [OCaml + Cordova = more secured, typed and hybrid mobile applications. - Danny Willems - Blog](https://blog.danny-willems.be/en/ocaml-cordova-secured-typed-hybrid-mobile-applications/)
- [let author = “Rudi Grinberg” — Rudi Grinberg documentation](http://rgrinberg.com/)
	+ [Introducing Opium](http://rgrinberg.com/posts/introducing-opium/), [Free Monads in the Wild](http://rgrinberg.com/posts/free-monads-in-the-wild-ocaml/), [Publishing an OPAM Package - a Checklist](http://rgrinberg.com/posts/opam-package-checklist/)
- [Shayne Fletcher](https://blog.shaynefletcher.org/)
* [dmbaturin's blog - ocaml tagged articles](http://web.archive.org/web/20191021110124/https://blog.baturin.org/tag/ocaml.html) -- has a series on "Introduction to OCaml". Also notable - [If you think ReasonML compiles to JS, you are wrong](https://blog.baturin.org/if-you-think-reasonml-compiles-to-js-you-are-wrong.html)
* [Jane Street Tech Blog](https://blog.janestreet.com/what-the-interns-have-wrought-2023/)

### Resources

- [Learn OCaml v.0.14.1](https://ocaml-sf.org/learn-ocaml-public/) -- 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](https://try.ocamlpro.com/fun-demo/) - This site is a demo for the programming environment used during the first session of the OCaml MOOC.
- [Ocaml Reddit](http://reddit.com/r/ocaml)
- [OCaml for the Skeptical: OCaml in a Nutshell](http://www2.lib.uchicago.edu/keith/ocaml-class/class-01.html)

### Papers

- [Polymorphism, subtyping and type inference in MLsub](http://www.cl.cam.ac.uk/~sd601/papers/mlsub-preprint.pdf); [online editor of the inference enginer](http://www.cl.cam.ac.uk/~sd601/mlsub/)
- [PhD Thesis: Tierless Web programming in ML | Drup's thingies](https://drup.github.io/2017/11/15/phdthesis/)


## Package managers

-   [opam](http://opam.ocamlpro.com/)
-   GODI
-   [YYPkg](http://yypkg.forge.ocamlcore.org/)
- Using jbuilder/topkg -- "[Platforms, Packaging, Progress](http://mort.io/blog/2017/08/28/past-present-future/)"; [Aug 2017]
- [~lthms: Building Static Binaries for OCaml Projects](https://soap.coffee/~lthms/posts/OCamlStaticBinaries.html); Dec 2023.

## OCaml Languages

[Reason](https://reasonml.github.io): a syntax for OCaml, so native development or [melange](https://shared.btbytes.com/p/melange) development to compile to JS

[ReScript](https://rescript-lang.org): a syntax similar to Reason, but the entire toolchain. Closer to JavaScript, **can't compile to native.**


## Talks

Yaron Minsky:

-   [OCaml for the
    masses](http://queue.acm.org/detail.cfm?id=2038036&ref=fullrss) in
    ACMQueue.
-   [Effective ML](http://ocaml.janestreet.com/?q=node/82) talk at
    Harvard and "Caml Trading" talk at CMU.
-   [More Effective ML](http://ocaml.janestreet.com/?q=node/85)

## Miscellaneous

- [OCaml Skeleton](https://github.com/wickedchicken/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](http://try.ocamlpro.com) a web based OCaml REPL.
- [OCaml style
    guide](http://caml.inria.fr/resources/doc/guides/guidelines.en.html)
- another [OCaml style
  guide](http://www.seas.upenn.edu/~cis500/cis500-f06/resources/programming_style.html)
  from UPenn, which is based on,
- [SML style guide](http://www.cs.cornell.edu/Courses/cs312/2001sp/style.html)
    -   [SML style
    guide](http://www.cs.cornell.edu/Courses/cs312/2001sp/style.html)


### OCaml as a replacement language

- [Replacing Python with OCaml](http://roscidus.com/blog/blog/2014/02/13/ocaml-what-you-gain/). [Thread](http://redd.it/1xtohg) and [thread2](https://news.ycombinator.com/item?id=7234855)
- [Felix](http://web.archive.org/web/20080415185225/http://felix-lang.org/) is offered an alternate to
  [OCaml](https://news.ycombinator.com/item?id=7235716)
- [OCaml vs Haskell](https://www.quora.com/Functional-Programming/Which-of-Haskell-and-OCaml-is-more-practical) discussion on Quora. YMMV.
- [8 months of OCaml after 8 years of Haskell in production - DEV Community](https://dev.to/chshersh/8-months-of-ocaml-after-8-years-of-haskell-in-production-h96) -- has a table comparing OCaml and [haskell](https://shared.btbytes.com/p/haskell)
- [OCamlers' critique of Haskell](https://redd.it/3ifwe9)

## Writing languages using OCaml

- [Developing Applications with Ocaml - Chapter 6](http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora058.html)  implements a BASIC interpreter.
- [The programming language zoo](http://andrej.com/plzoo/) by [Andrej Bauer](http://andrej.com/) has multiple examples of simple languages
  built using OCaml.
- [Toy APL implementation](http://lists.canonical.org/pipermail/kragen-hacks/2007-April/000456.html)
  by Kragen Sitaker.
- [CS 421/521: Compilers and Interpreters](http://flint.cs.yale.edu/cs421/)
- [Lua-ML](http://www.reddit.com/r/ocaml/comments/1xlylk/luaml_a_pure_ocaml_implementation_of_lua_25_by/)
 pure OCaml implementation of Lua 2.5 by Normal Ramsey.
- [ML to C++ compiler](https://github.com/akabe/evilml)


## Tools, ecosystem and libraries

- [Setting Up an OCaml Development Environment in Debian - The Always Learning Marty](https://thealmarty.com/2018/08/02/setting-up-an-ocaml-development-environment-in-debian/) ; Aug 2018
- [OCaml Lint](https://github.com/cryptosense/ocamllint) detects common errors in OCaml code.
- [IOCaml](https://github.com/andrewray/iocaml), OCaml kernel for iPython.
- [Domical = OCaml + Iodide - Iodide](https://louisabraham.github.io/domical/); Sep 2018.
- [Regular expressions in OCaml](http://rgrinberg.com/blog/2014/05/23/humane-re-intro/)
- [Opam-Android](https://github.com/whitequark/opam-android) --An OCaml 4.02 toolchain for Android and several useful libraries
- [OCaml Scientific Computing — Owl Numerical Library ](http://ocaml.xyz/)


### Js_of_Ocaml

- [js-skeleton](https://github.com/talex5/js-skeleton) -- sample
  project showing how to build a Javascript web-app using
  `js_of_ocaml`, `TyXML` and `React`
- [H.261 Video Decoding in OCaml.](https://andrewray.github.io/iocamljs/oh261.html#) -- 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](http://omake.metaprl.org/index.html)
- [OCamlbuild](http://brion.inria.fr/gallium/index.php/Ocamlbuild)
- [OASIS](http://oasis.forge.ocamlcore.org/)
- [opam-publish](https://github.com/AltGr/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?](http://stackoverflow.com/q/5956317)
    - recco: use `ocamlbuild` but `oasis` may be better now.
    - `ocamlbuild` comes with the distribution.
    - `oasis` builds `myocamlbuild.ml` file automatically for you.
    - `oasis` gives you access to `oasis-db`, a CPAN like system.
    - to manage libraries, use `ocamlfind`.
    - `opam` really doesn't care about build systems.
    - MLState is using `ocamlbuild` for building `Opa lang`.

- [Another thread recommending
  OASIS](http://stackoverflow.com/q/19350590)



## Code bits

### Explicit Typing of a polymorphic variant

~~~{.ocaml}
type download_result =
  [ `aborted_by_user
  | `network_failure of string
  | `success of filepath ]
~~~

[via](http://redd.it/1xtohg)


## Information

- [OCaml's string type becomes immutable in 4.02](http://redd.it/24e99g)
- [Which of OCaml's standard libraries to use?](http://redd.it/2d1h42)

## QQ

- What is good about [OCaml's module system?](http://www.cs.cornell.edu/courses/cs3110/2016fa/l/07-modules/notes.html)
- What good is First class Modules?
- "Using an error monad in a language like ocaml is superior to
  exceptions in almost every way."
  [via](https://lobste.rs/s/5ovrc2/what_s_your_most_controversial_technical_opinion/comments/pw0ane#c_pw0ane)


## Sample code / Read Source

- [github.com/chrismamo1/zipper](https://github.com/chrismamo1/zipper) -- a CLI tool in the imperative style.
- [collaborative text editor using Eliom](https://gitlab.com/adrien-n/prose)
- [vbmithr (Vincent Bernardoff) / Repositories](https://github.com/vbmithr?tab=repositories)
- [bcpierce00/unison: Unison file synchronizer](https://github.com/bcpierce00/unison)
- [Gopiandcode/ocamlot: An Activitypub server in OCaml!](https://github.com/Gopiandcode/ocamlot/tree/master)


## Multicore OCaml

- [Presentation by Dolan, White and Madhavapeddy](http://www.cl.cam.ac.uk/~sd601/papers/multicore_slides.pdf) 2014.
* [Multicore OCaml - What's coming in 2021 - Speaker Deck](https://speakerdeck.com/kayceesrk/multicore-ocaml-whats-coming-in-2021?slide=72); Anil and KCSRK
* [Practical OCaml, Multicore Edition](https://dev.to/yawaramin/practical-ocaml-multicore-edition-3gf2) by Yawar Amin

## Object oriened OCaml

- [The O in OCaml](https://redd.it/4ngcb7) -- a reddit discussion, with strong answers from 'gasche'.
- [Experiences with OCaml objects - Thomas Leonard's blog](http://roscidus.com/blog/blog/2013/09/28/ocaml-objects/)

## Hindley-Milner type system

- [Show HN: Hindley-Milner Type Inference Algorithm in OCaml | Hacker News](https://news.ycombinator.com/item?id=11577585) see links within.


## Semantics

- [Lev Walkin → ASN.1 Exposed → On FaceBook's Thrift semantics, code generation, and OCaml](http://lionet.info/asn1c/blog/2010/07/18/thrift-semantics/)

## Web programming with OCaml

- [How to deploy OCaml on Heroku](https://medium.com/@emillon/how-to-deploy-ocaml-on-heroku-9903548aafa5#.mp4k4nc04) ; Aug 2016.
- [Dream — Tidy, feature-complete web framework](http://web.archive.org/web/20210404201051/https://aantron.github.io/dream/)

## PPX

- [A Tutorial to OCaml -ppx Language Extensions | Victor Alexandru Darvariu](https://www.victor.darvariu.me/jekyll/update/2018/06/19/ppx-tutorial.html)

## Comparison

- [OCaml vs haskell vs go](https://pl-rants.net/posts/haskell-vs-go-vs-ocaml-vs/)
- [Thread by &#64;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...](https://threadreaderapp.com/thread/1115387665850015744.html)
* [How does OCaml compare with haskell?; HN thread](https://news.ycombinator.com/item?id=22411760).


## min-caml

[esumii/min-caml](https://github.com/esumii/min-caml):  a complete optimizing native-code compiler for a reasonably usable subset of OCaml, in 2000 lines of OCaml. [Paper](https://esumii.github.io/min-caml/paper.pdf).

## 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!

- [OCaml+PostgreSQL with Caqti](https://medium.com/@bobbypriambodo/interfacing-ocaml-and-postgresql-with-caqti-a92515bdaa11)

- [Natively run OCaml from Rust](https://blog.s-m.ac/ocaml-rust-ffi/)

## NixOS

- [Nix-powered development with OCaml - Dimitrije's Website](https://dimitrije.website/posts/2023-03-04-nix-ocaml.html); 2023/3/6
- [Bye Opam, Hello Nix | Emil Privér](https://priver.dev/blog/ocaml/bye-opam-hello-nix/); May 2024.


## 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](https://www.amazon.com/OCaml-Scientific-Computing-Intelligence-Undergraduate/dp/3030976440/) (Book); [Springer link](https://link.springer.com/book/10.1007/978-3-030-97645-3)

## OCaml Garbage Collection

- [Garbage Collection · OCaml Tutorials](https://ocaml.org/docs/garbage-collection)
- [An in-depth Look at OCaml’s new “Best-fit” Garbage Collector Strategy | OCamlPro](https://ocamlpro.com/blog/2020_03_23_in_depth_look_at_best_fit_gc/)
- [Lecture 26 of Cornell CS3110](https://www.cs.cornell.edu/courses/cs3110/2012sp/lectures/lec26-gc/lec26.html)

## 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 idiosyncrasies of GTK on mac... ugh.. To keep it short, `lablgtk`
installation by opam might fail with `pkg-config` errors. The fix is:

```bash
https://github.com/Homebrew/homebrew/issues/14123#issuecomment-37896791
```
... before running `opam install ocamleditor`. Solution
[via](https://github.com/Homebrew/homebrew/issues/14123#issuecomment-37896791)

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](https://cs.umd.edu/class/summer2015/cmsc330/setting-up-ocaml/). Use `ocamlfind`, `ocamlbuild` and `make`.

Apr 2016. See [ogen](https://github.com/nv-vn/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.
