Write You a Haskell: Building a modern functional compiler from first principles
dev.stephendiehl.com
dev.stephendiehl.com
There are plenty of compiler writing tutorials for conservative, imperative programming languages with a straightforward static type system (like C).
I did study a little bit of compilers for functional languages from Simon Peyton-Jones' old book "The Implementation of Functional Programming Languages" [0]. It predates the Haskell programming language and uses a contemporary research language called Miranda as the target as well as the "host" language.
... which brings me to another topic: Monads. The SPJ book has a few chapters on implementing a Hindley-Milner -style type inference algorithm. It's written in Miranda without any Monads and uses a clever trick for coming up with unique temporary type names. There's an infinite list of integers [0..], which is "split" when traversing the syntax tree so that the left branch of the recursion tree gets the even numbers and the right branch gets the odd numbers.
The method works fine and is indeed very clever but try comparing that code to the same algorithm re-implemented (by me) with Monads (State and Error monads + transformer) [1]. The original algorithm is rather long and hard to follow (because lists of unique integers are passed to all functions).
I had a blast writing this algorithm, and I wanted to share it. It's been a few years since I last worked with it. It was my first non-trivial use of monads and I really like how it turned out in the end.
[0] http://research.microsoft.com/en-us/um/people/simonpj/papers... [1] https://github.com/rikusalminen/funfun/blob/master/FunFun/Ty...
[0]: http://research.microsoft.com/en-us/um/people/simonpj/Papers...
So they're missing for a reason.
1. http://www.cs.cmu.edu/afs/cs/user/crary/www/819-f09/Moggi91....
My sole intent was to emphasize, using a practical example, how neat and elegant monads can be at best.
http://research.microsoft.com/en-us/um/people/simonpj/Papers...
*I wish I could put that in sarcastic quotes.
http://matt.might.net/articles/cps-conversion/
http://lucacardelli.name/Papers/BasicTypechecking.pdf
http://library.readscheme.org/page1.html
https://research.microsoft.com/pubs/67083/spineless-tagless-...
http://research.microsoft.com/pubs/64044/compilingwithcontin...
I haven't read it, that's why I'm asking this.
It simplifies some things (for example tiling while producing the final asm).
I jest, but this is an issue I've thought about idly for a while. Maybe it's possible to write a two-step parser: one that turns chars into tokens (and cheat a little bit by stripping spaces in some contexts to make it context free), and then you have a nice CFG that you can do easily.
I asked whether I could use bash, and consider gcc a feature of the language.
I was informed that would be cheating.
https://github.com/purescript/purescript/blob/master/src/Lan...
The state is defined in https://github.com/purescript/purescript/blob/master/src/Lan...
(its just the column number). I believe the whitespace rules for Haskell are somewhat subtle, but shouldn't be much harder.