TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why ML/OCaml are good for writing compilers (1998)

241 pointsby monssoenabout 8 years ago

11 comments

hongbo_zhangabout 8 years ago
For web developers who are looking for an industrial strength functional language instead of JS, OCaml probably has the best story here.<p>Actually it has two OCaml-&gt;JS compilers of very high quality The first one, js_of_ocaml, could bootstrap the whole compiler several years ago(probably the first one there).<p>The recent one, <a href="https:&#x2F;&#x2F;github.com&#x2F;bloomberg&#x2F;bucklescript" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bloomberg&#x2F;bucklescript</a>, push the JS compilation into next level, it generates fairly readable code, good FFI story, and its compilation is <i>extremely</i> fast, check out the compiler in JS version(<a href="http:&#x2F;&#x2F;bloomberg.github.io&#x2F;bucklescript&#x2F;js-demo&#x2F;" rel="nofollow">http:&#x2F;&#x2F;bloomberg.github.io&#x2F;bucklescript&#x2F;js-demo&#x2F;</a>), and imagine how fast it would be for the compiler in native version. BuckleScript has a good story for Windows, and generates fairly efficient code, see benchmark here: <a href="https:&#x2F;&#x2F;github.com&#x2F;neonsquare&#x2F;bucklescript-benchmark" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;neonsquare&#x2F;bucklescript-benchmark</a> BuckleScript is already used in production by big companies: for example Facebook messenger.com 25% is powered by BuckleScript, the new WebAssembly spec interpreter by Google is also partly cross compiled into JS by BuckleScript.<p>Disclaimer: I am one of the authors of BuckleScript
评论 #14123698 未加载
评论 #14124342 未加载
评论 #14124985 未加载
评论 #14124414 未加载
评论 #14126671 未加载
Dangerangerabout 8 years ago
After learning Elm I wanted to understand ML&#x2F;OCaml a bit more, so I worked through some documentation from the OCaml site and walked away pleasantly surprised.<p>After using it for a couple of weeks I am confused why ML&#x2F;OCaml aren&#x27;t more popular. They are safe, functional, stable, fast, and have great tooling. They seem poised to take over the functional domain.<p>While the syntax took a little getting used to ( emphasis on little) once you are used to it, it&#x27;s very natural. Union types are wonderful, and the implicit type safety within programs was nice.
评论 #14123253 未加载
评论 #14123335 未加载
评论 #14123251 未加载
评论 #14123408 未加载
评论 #14124005 未加载
评论 #14123843 未加载
评论 #14123988 未加载
评论 #14123366 未加载
评论 #14123339 未加载
rbehrendsabout 8 years ago
I&#x27;ll note that some of the aspects don&#x27;t necessarily work out like that in practice:<p>1. The GC part is true, but one has to remember that this was written at a time when GC was still a bit of an unusual feature in mainstream languages.<p>2. Tail recursion doesn&#x27;t really make much of a difference for walking trees, which is recursive, but (mostly) not tail recursive.<p>3. OCaml in particular uses 63&#x2F;31-bit ints due to implementation details, which isn&#x27;t a good fit for 64&#x2F;32-bit integers. The strings and bignum part is mostly right, though.<p>4. ADTs can be good or bad for describing ASTs. Once you enrich ASTs with semantics shared by all variants (such as source coordinates), inheritance can become a better fit than ADTs.<p>8. Type inference doesn&#x27;t really extend to module signatures, which you have to write out explicitly (though tooling such as `ocamlc -i` allows you to let the compiler help you write them). I also generally find it better to explicitly annotate functions with types. Not only does it make the code more readable later in its life, but you get fewer truly impenetrable type error messages because you forgot parentheses or a semicolon somewhere.<p>That said, there are several good points still.
评论 #14125138 未加载
评论 #14123454 未加载
评论 #14124866 未加载
评论 #14123635 未加载
nv-vnabout 8 years ago
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.
评论 #14123319 未加载
评论 #14123320 未加载
评论 #14123536 未加载
评论 #14125626 未加载
评论 #14123294 未加载
vmastoabout 8 years ago
For anyone interested and isn&#x27;t aware yet Facebook is developing Reason, a layer over OCaml. I&#x27;ve been fiddling with it for the past couple of weeks and coming from JavaScript I personally found the experience generally enjoyable.<p><a href="https:&#x2F;&#x2F;facebook.github.io&#x2F;reason&#x2F;" rel="nofollow">https:&#x2F;&#x2F;facebook.github.io&#x2F;reason&#x2F;</a>
评论 #14123775 未加载
评论 #14123398 未加载
StrykerKKDabout 8 years ago
I agree that Ocaml is just extremely well suited for making new programig languages. If you are interested in Ocaml+programming languages check out the plzoo: <a href="http:&#x2F;&#x2F;plzoo.andrej.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;plzoo.andrej.com&#x2F;</a><p>I personally think that Ocaml is really good at this, because I started converting the Scheme examples from the PLAI book to Ocaml and it&#x27;s just felt right(maybe because I&#x27;m not fan of the scheme syntax).
chairmanwowabout 8 years ago
Currently taking a compilers course that uses SML&#x2F;NJ and it has been an absolute delight. The functional paradigm is a little strange to get used to at first, but after a while its strong suits make themselves known. The trivial type inferences and pattern matching capabilities make it easy to efficiently describe complicated and precise program situations.
kornakiewiczabout 8 years ago
I&#x27;m a relatively young developer (three years older than Java) and don&#x27;t fully get the thing about exceptions (point 7). It sounds very familiar to the solution I know from Java, and it does not make safety - or even feeling about it - any better. If you can still write code that can throw exception and an explicit assurance about it is the only way to prevent the crash, it doesn&#x27;t change anything, actually.<p>P.S. I&#x27;m not really familiar to ML&#x2F;OCaml, but have decent experience with large code bases in languages that are not very keen to protect you from yourself.
评论 #14123359 未加载
agumonkeyabout 8 years ago
FP is based on recursion, and recursive types, inductive algorithms are essential for linguistic processing and transformation.
hyperpalliumabout 8 years ago
How are simple parsers written in ML (or ocaml)?<p>You can&#x27;t use the coding style used for recursive descent in the Dragon compiler book, without using mutable variables.<p>Do you have to use parser combinators, which have their own limitations?
评论 #14124923 未加载
评论 #14125041 未加载
评论 #14123867 未加载
评论 #14124064 未加载
jackmottabout 8 years ago
OCaml has the potential to be great for almost everything with some work and a bigger ecosystems. F# as well (very similar). i can only imagine how great the world would be if a standard ml had accidentally become the web browser language and all that mind share had gone into evolving it and optimizing it and its tools.