Windfall Architecture — Compilers(Part 3/5). Of OCaml and Reason.

Recently, I have been writing about Windfall Architecture. If you haven’t read Part 1 + Part 2 of this series, I would highly recommend doing so.

In this article, I will be discussing why I chose OCaml as my go to language for building this particular compiler. I will be touching on the use of a functional language for aforementioned purpose. In addition, why I specifically chose OCaml, as well as how it bridges to the Front End space as well.

Mareo — Mario in Reason + Bucklescript

Functional Vs. OOP — Compiler Edition

The following are three reasons as to why a functional language makes sense to create a compiler:

  1. Compiler source code is generally parsed into a syntax tree. Which is then parsed into another tree etc. This equates to recursion and pattern matching, which functional languages tend to specialize in.
  2. Compilers are synonymous with the very nature of functional languages. That is, the transformation of one state to another.
  3. Emphasis in functional language design is on operation. For instance, monads, maps, composition etc. That is vs. the nature of OOP languages which is type specific(Factory, Singleton, Prototype etc.). Compilers tend to be very abstract, and therefore perfect with operation heavy development.

The next question is, why OCaml specifically?!

Why OCaml Specifically?

The following is a terrific article explaining why OCaml is a great language for writing compilers. The following really spoke to me:

  1. Exception Handling
  2. Terrific community support
  3. Module system + functors
  4. Library support
  5. Garbage collection and quick.
  6. Type inference, with option to specify type.

However, Haskell does seem to have a lot of the same benefits as above, so why choose OCaml over Haskell?

Haskell V. OCaml — Front End Showdown

This part of the conversation I found really interesting. I should note, that I have spoken with quite a few people on this topic(in person). In conversation, many people in the Front End Compiler community seemed to recommend OCaml. The greatest argument in these conversations for OCaml was the fact that there was a terrific community around ReasonML. For Haskell, it was the scientific community, in particular machine learning. Both recognized the benefit of both, and how they would both be adequate for the open source at hand. In the end, I decided on OCaml vs. Haskell for the following reasons:

  1. More native Javascript feel [e.g. let, match, modules, ->, pipes for matching]
  2. Reason, the really terrific community around it, and the progressive projects that exist. I do know Purescript does exist. I will be writing about Reason vs. Purescript in another article.
  3. Bucklescript, and the really passionate people dedicated to the project.
  4. OCaml has many singular libraries that the community has seemed to agree on.
  5. The community was the most receptive, and the most engaging. I feel like that is a terrific indicator of potential success.

The above would be what has led me to my decision on choosing OCaml. Other considerations were made such as Typescript. Ultimately, I like the idea of layering on a toolchain to a native language, that has been battle tested for the use of compilers. In addition, I really agree with the philosophy of “framework compilation”(e.g. Elm). I really feel that the community is headed towards the direction of ReasonML and Purescript. Something I will write about in another series.

Thanks for reading, and more amazing content is right around the corner! Salute, and that’s the scoop!!!

P.S. Time and effort went into the above article. If you are interested in writing your own compiler I highly reccomend:

If you would like to follow the github project in real time, it can be found here. Thank you.