Choosing the new Stan compiler's implementation language

I wouldn’t say I hate Haskell, but I did voice a preference against it for this particular project.

I use monads all the time to structure denotational semantics. I did a PhD in category theory so I don’t think I’m biased against monads. I just think they can add too much cognitive burden when doing simple things, particularly when you start mixing computational effects and need to use monad transformers. I’m not sure they’re the best programming abstraction for all purposes and I am hoping that they will be replaced by a better (particularly, more composable) mechanism for managing effects using the type system. In our particular case, I think they would create a much higher barrier to entry to working on the compiler.

Sure, you can write a lot of a compiler in a purely functional style, but ultimately it’s convenient to have some state around, for instance for the symbol table (convenient though not essential here: you can write everything in a pure state-passing-style with Maps rather than HashTables) and for the various compiler optimisations (really very convenient here, I think). This means our code would inevitably involve a bunch of monads.

The lazy evaluation is something I’m really not enthusiastic about in general. It results in unpredictable computational complexity - which for a compiler might not be super important - and can be very confusing for novice users. I think lazy evaluation is useful in some particular cases, but I’d rather not have it as a default.

Ultimately, my preference of OCaml over Haskell came from the fact that I believe that most people could understand a compiler written in OCaml in a couple of days, even if they’ve never seen an ML dialect before, while a compiler in Haskell would scare away most people who aren’t functional programming or category theory enthusiasts.

That said, there are a bunch of things I like a lot about Haskell: the type classes, the idea of having some mechanism in the type system for making users be explicit about the computational effects they want to use, the higher order polymorphism and generally the design of the syntax. Probably, the higher order polymorphism would be the only thing I’d really like from Haskell while writing a compiler, as I’ve experienced first hand that it can be useful for structuring some of the intermediate representations. Then again, even there I’d have mixed feelings as it really makes the learning curve for the project steeper for new developers.

@Kevin_Van_Horn, I’d love to understand what you’ve found useful though about Haskell over, say OCaml, when writing compilers. I’m far from experienced when it comes to writing compilers myself so I could well be overlooking important things here.

1 Like