<story><title>Write You a Haskell: Building a modern functional compiler from first principles</title><url>http://dev.stephendiehl.com/fun/</url></story><parent_chain></parent_chain><comment><author>exDM69</author><text>Now this is very interesting!&lt;p&gt;There are plenty of compiler writing tutorials for conservative, imperative programming languages with a straightforward static type system (like C).&lt;p&gt;I did study a little bit of compilers for functional languages from Simon Peyton-Jones&amp;#x27; old book &amp;quot;The Implementation of Functional Programming Languages&amp;quot; [0]. It predates the Haskell programming language and uses a contemporary research language called Miranda as the target as well as the &amp;quot;host&amp;quot; language.&lt;p&gt;... which brings me to another topic: Monads. The SPJ book has a few chapters on implementing a Hindley-Milner -style type inference algorithm. It&amp;#x27;s written in Miranda without any Monads and uses a clever trick for coming up with unique temporary type names. There&amp;#x27;s an infinite list of integers [0..], which is &amp;quot;split&amp;quot; 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.&lt;p&gt;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).&lt;p&gt;I had a blast writing this algorithm, and I wanted to share it. It&amp;#x27;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.&lt;p&gt;[0] &lt;a href=&quot;http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;research.microsoft.com&amp;#x2F;en-us&amp;#x2F;um&amp;#x2F;people&amp;#x2F;simonpj&amp;#x2F;papers...&lt;/a&gt;
[1] &lt;a href=&quot;https://github.com/rikusalminen/funfun/blob/master/FunFun/TypeChecker.hs&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;rikusalminen&amp;#x2F;funfun&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;FunFun&amp;#x2F;Ty...&lt;/a&gt;</text></comment>
<story><title>Write You a Haskell: Building a modern functional compiler from first principles</title><url>http://dev.stephendiehl.com/fun/</url></story><parent_chain></parent_chain><comment><author>tenslisi</author><text>It looks like the author of this (Stephen Diehl) is looking for a job in Boston. Maybe HN can help him out?&lt;p&gt;&lt;a href=&quot;https://twitter.com/smdiehl/status/538494839189700608&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;twitter.com&amp;#x2F;smdiehl&amp;#x2F;status&amp;#x2F;538494839189700608&lt;/a&gt;</text></comment>