Windfall Architecture — Compilers(Part 3/5). Of OCaml and Reason.
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.
Functional Vs. OOP — Compiler Edition
The following are three reasons as to why a functional language makes sense to create a compiler:
- 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.
- Compilers are synonymous with the very nature of functional languages. That is, the transformation of one state to another.
- 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:
- Exception Handling
- Terrific community support
- Module system + functors
- Library support
- Garbage collection and quick.
- 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:
- 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.
- Bucklescript, and the really passionate people dedicated to the project.
- OCaml has many singular libraries that the community has seemed to agree on.
- 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:
- OCaml from the Very Beginning
- Real World OCaml
- Compiler Construction: Principles and Practice
- Compilers: Principles, Techniques, and Tools
- A Nanopass Framework for Compiler Education
- Let’s Build a Compiler
- Using Machine Learning to Automate Compiler Optimisation
- Engineering a Compiler, Second Edition
If you would like to follow the github project in real time, it can be found here. Thank you.