Why ML/OCaml are good for writing compilers (1998)
The problem with this article is that it&#x27;s missing an answer to why one would choose ML&#x2F;OCaml over Haskell. Haskell has many more features, a more advanced type system, arguably superior syntax, and much better library support. However, I believe that OCaml&#x2F;SML are often a better choice for a number of reasons.<p>First of all, OCaml&#x2F;SML are the best choice in terms of example code for compilers. They&#x27;re historically the choice of many compiler&#x2F;interpreter&#x2F;type theory texts (Types and Programming Languages, Modern Compiler Implementation in ML, and an ML is even used as a language to interpret in Essentials of Programming Languages). Andrej Bauer&#x27;s PLZOO is also written in OCaml. Equally important is the fact that there are a variety of ML implementations, all of which are much more approachable than GHC. The OCaml compiler&#x27;s codebase is a reasonable size that an individual could get a good idea of how it works in a few weeks or so. SMLNJ, MLKit, MLton, CakeML are all open source and on Github, and all seem to be fairly approachable in comparison to the monolith that is GHC. And that&#x27;s not even mentioning other compiler in ML (Haxe, Rust&#x27;s bootstrap compiler, Facebook&#x27;s Hack compiler, etc.). The fact that there are real-world compilers with perfectly approachable code bases (even without great familiarity with the language; compilers in Haskell might require an in-depth understanding of many of the core type classes and language extensions available) that are open source is highly attractive to novice compiler writers.<p>Additionally, the feature set in MLs is a good choice for compilers. While they lack some of the cooler features of Haskell, MLs make up for it in simplicity; lots of the features in GHC&#x27;s type system (especially with language extensions) mean very little for 90% of compiler writers, and getting rid of them from the get-go helps keep the code small and easy to reason about (even if you won&#x27;t have as much type safety in the compiler itself). This also means that there are a lot less ways to do a single thing, which can be nice when you&#x27;re not sure exactly how you&#x27;re going to implement a certain feature. However, one thing I really find incredibly useful is OCaml&#x27;s polymorphic variants. These are pretty much perfect for statically enforcing a nanopass-like system in your compiler and are a great way of designing your AST&#x2F;data types in your compiler. I feel like this gets passed up a ton (as far as I know I&#x27;m the first person who&#x27;s used them to create nanopasses), but it&#x27;s quite convenient and makes OCaml a good competitor for Scheme in this regard.