Materials on Compiling Functional Languages.

Hi Cafe, I'm looking for materials on compiling functional languages. The materials that I was able to gather from the web were kind of all over the place, so I'm hoping someone could help me out with something more in order for a novice. Starting from something specific and simple so I could have something small and concrete to implement, play and ponder about. And then going forward with more advanced materials about different optimization strategies for eager and lazy languages or what could be achieved when limiting yourself only to a total language, etc. Cheers, Max.

Maksymilian Owsianny
Hi Cafe,
I'm looking for materials on compiling functional languages. The materials that I was able to gather from the web were kind of all over the place, so I'm hoping someone could help me out with something more in order for a novice.
Starting from something specific and simple so I could have something small and concrete to implement, play and ponder about. And then going forward with more advanced materials about different optimization strategies for eager and lazy languages or what could be achieved when limiting yourself only to a total language, etc.
Cheers, Max.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe <at> haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Hi Maksymilian, Here you'll find a (perhaps not so) short list I've accumulated: http://pastemarkdown.com/XUEvs and here it is in markdown format: - [Implementing a JIT Compiled Language with Haskell and LLVM](http://www.stephendiehl.com/llvm/) - [Write You a Haskell](http://dev.stephendiehl.com/fun/index.html) - [Theory and Implementation of a Functional Programming Language](http://www.rose-hulman.edu/mathjournal/archives/2000/vol1-n2/paper3/v1n2-3pd...) - [Implementing Functional Programming Languages](http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/...) - SPJ - [Implementing Functional Langauges: a tutorial](http://research.microsoft.com/en-us/um/people/simonpj/Papers/pj-lester-book/) - SPJ - [Practical Implementation of a Dependently Typed Functional Programming Language | Brady](http://compsoc.dur.ac.uk/~ecb/proofread/thesis.pdf) - [PLAI](http://cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04...) - [STG Machine](http://www.dcc.fc.up.pt/~pbv/aulas/linguagens/peytonjones92implementing.pdf) - [Typing Haskell in Haskell](http://web.cecs.pdx.edu/~mpj/thih/TypingHaskellInHaskell.html - [Practical type inference for arbitrary-rank types / SPJ et al.](http://research.microsoft.com/en-us/um/people/simonpj/papers/higher-rank/put...) - Compiling with Continuations - Appel - [PureScript Compiler](https://github.com/purescript/purescript) Cheers and good luck, Gil

I would start simple by creating a Lisp compiler or low level interpreter, and am personally a great fan of https://github.com/openube/zozotez, but making a compiler for Haskell is of course way more difficult, productive and awesome. Gil came with some good links to help you with that.

There's "write you a Haskell" but it's still in progress
http://dev.stephendiehl.com/fun/index.html
On Thursday, March 10, 2016, Jonne Ransijn
I would start simple by creating a Lisp compiler or low level interpreter, and am personally a great fan of https://github.com/openube/zozotez, but making a compiler for Haskell is of course way more difficult, productive and awesome. Gil came with some good links to help you with that.

@Jonne Maybe I should have been a little more precise. I'm specifically interested in a compilation step, that is turning a AST of some functional language say untyped lambda calculus into a sequence of imperative instructions. And I'm looking for a somewhat structured materials on the subject. If I were to give an analogy it would be how you would teach someone about sorting algorithms. You would start with simple solutions that most people come up on their own, like insert or bubble sort. Then you go on about explaining problems with complexity of that solutions and you present slightly more complex but optimal algorithms like quick or merge sort. Then maybe you would give a proof that n log n is best you can do for comparison based solutions. Then you could ask what could be done if you remove that restriction, and so on... That way I could get a more complete understanding of the field. Where all I was able to fish from google were papers about (to continue the analogy) theoretical complexity of a sorting algorithm on a quantum computer or constant time improvements of a heap sort given architecture with SIMD instruction set. And of course given enough time I would be able to piece all that knowledge together, but I'm hoping someone could spare me that time. @Gil Thanks for suggestions, I was able to locate previously these series by Stephan Diehl and they are truly quality material, the only problem is that the first one deals with compiling an imperative language. So it is of great use as practical solution to llvm generation but doesn't really answers any of my theoretical questions about functional language compilation. The second one on the other hand deals with haskell so that's a bit of a jump already but also is still a work in progress and crucially doesn't yet have the chapters regarding compilation. As for others most of them are new to me so I guess I'll have to take a deeper look. Ultimately I wasn't really expecting to get all that knowledge on a silver platter. I guess I'll have to dig into all that literature until I'll come out enlightened, so really anything useful you can throw at me I'll be grateful for. Thanks, Max.

Try to find "Implementing Functional Languages: A Tutorial" by Simon
Peyton-Jones and David Lester.
On Thu, Mar 10, 2016 at 12:42 PM, Maksymilian Owsianny
@Jonne
Maybe I should have been a little more precise. I'm specifically interested in a compilation step, that is turning a AST of some functional language say untyped lambda calculus into a sequence of imperative instructions.
And I'm looking for a somewhat structured materials on the subject.
If I were to give an analogy it would be how you would teach someone about sorting algorithms. You would start with simple solutions that most people come up on their own, like insert or bubble sort. Then you go on about explaining problems with complexity of that solutions and you present slightly more complex but optimal algorithms like quick or merge sort. Then maybe you would give a proof that n log n is best you can do for comparison based solutions. Then you could ask what could be done if you remove that restriction, and so on...
That way I could get a more complete understanding of the field.
Where all I was able to fish from google were papers about (to continue the analogy) theoretical complexity of a sorting algorithm on a quantum computer or constant time improvements of a heap sort given architecture with SIMD instruction set.
And of course given enough time I would be able to piece all that knowledge together, but I'm hoping someone could spare me that time.
@Gil
Thanks for suggestions, I was able to locate previously these series by Stephan Diehl and they are truly quality material, the only problem is that the first one deals with compiling an imperative language. So it is of great use as practical solution to llvm generation but doesn't really answers any of my theoretical questions about functional language compilation. The second one on the other hand deals with haskell so that's a bit of a jump already but also is still a work in progress and crucially doesn't yet have the chapters regarding compilation. As for others most of them are new to me so I guess I'll have to take a deeper look.
Ultimately I wasn't really expecting to get all that knowledge on a silver platter. I guess I'll have to dig into all that literature until I'll come out enlightened, so really anything useful you can throw at me I'll be grateful for.
Thanks, Max.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

@Matthew: Gil already provided a link for that, which appears to be the first result on Google aswell: http://research.microsoft.com/en-us/um/people/simonpj/Papers/pj-lester-book/ @Max: Haskell compilation is not really my area, but if a Lisp compiler seems too easy (And I have to say, it _is_ quite easy, and shouldn't take more than a couple of hours of coding), I would recommend starting by looking into the Haskell Core to find out what Haskell boils down to. From there you might be able to figure out how Haskell actually gets turned into iterative code. The links Gil provided really help with this aswell. This stackoverflow page contains some useful links and a compact explaination of the Haskell Core, and is a great introduction to the Haskell core. (Although it might be focused more around optimalization than it is about the core itself): http://stackoverflow.com/questions/6121146/reading-ghc-core Explaination from haskell.org: https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/CoreSynType PDF (As found on the above page): https://downloads.haskell.org/~ghc/6.12.2/docs/core.pdf Sorry if these links are not helping. I am still trying to figure out the Haskell Core myself. On Thu, Mar 10, 2016 at 2:28 PM, Matthew Pickering < matthewtpickering@gmail.com> wrote:
Try to find "Implementing Functional Languages: A Tutorial" by Simon Peyton-Jones and David Lester.
On Thu, Mar 10, 2016 at 12:42 PM, Maksymilian Owsianny
wrote: @Jonne
Maybe I should have been a little more precise. I'm specifically interested in a compilation step, that is turning a AST of some functional language say untyped lambda calculus into a sequence of imperative instructions.
And I'm looking for a somewhat structured materials on the subject.
If I were to give an analogy it would be how you would teach someone about sorting algorithms. You would start with simple solutions that most people come up on their own, like insert or bubble sort. Then you go on about explaining problems with complexity of that solutions and you present slightly more complex but optimal algorithms like quick or merge sort. Then maybe you would give a proof that n log n is best you can do for comparison based solutions. Then you could ask what could be done if you remove that restriction, and so on...
That way I could get a more complete understanding of the field.
Where all I was able to fish from google were papers about (to continue the analogy) theoretical complexity of a sorting algorithm on a quantum computer or constant time improvements of a heap sort given architecture with SIMD instruction set.
And of course given enough time I would be able to piece all that knowledge together, but I'm hoping someone could spare me that time.
@Gil
Thanks for suggestions, I was able to locate previously these series by Stephan Diehl and they are truly quality material, the only problem is that the first one deals with compiling an imperative language. So it is of great use as practical solution to llvm generation but doesn't really answers any of my theoretical questions about functional language compilation. The second one on the other hand deals with haskell so that's a bit of a jump already but also is still a work in progress and crucially doesn't yet have the chapters regarding compilation. As for others most of them are new to me so I guess I'll have to take a deeper look.
Ultimately I wasn't really expecting to get all that knowledge on a silver platter. I guess I'll have to dig into all that literature until I'll come out enlightened, so really anything useful you can throw at me I'll be grateful for.
Thanks, Max.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On 11/03/16 3:17 am, Jonne Ransijn wrote:
if a Lisp compiler seems too easy (And I have to say, it _is_ quite easy, and shouldn't take more than a couple of hours of coding),
Since you know so much about writing Lisp compilers, perhaps you can point me to something that explains how to implement Common Lisp function calls efficiently? There are &optional, &rest, and &key parameters to deal with, and when the compiler sees a call site, in general it hasn't a clue what the target is expecting (this is unlike optional and keyword parameters in Swift, for example). If it were just required, &optional, and &rest parameters, I could see how to do it, but throwing &key in makes it much harder.

There is part II of this set of slides by Xavier Leroy:
http://gallium.inria.fr/~xleroy/mpri/2-4/.
Paul
On Thu, Mar 10, 2016 at 10:27 PM Richard A. O'Keefe
On 11/03/16 3:17 am, Jonne Ransijn wrote:
if a Lisp compiler seems too easy (And I have to say, it _is_ quite easy, and shouldn't take more than a couple of hours of coding),
Since you know so much about writing Lisp compilers, perhaps you can point me to something that explains how to implement Common Lisp function calls efficiently? There are &optional, &rest, and &key parameters to deal with, and when the compiler sees a call site, in general it hasn't a clue what the target is expecting (this is unlike optional and keyword parameters in Swift, for example). If it were just required, &optional, and &rest parameters, I could see how to do it, but throwing &key in makes it much harder.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Am 10.03.2016 um 13:42 schrieb Maksymilian Owsianny:
I'm specifically interested in a compilation step, that is turning a AST of some functional language say untyped lambda calculus into a sequence of imperative instructions.
I was asking myself the same question. I can't offer structured material, but an insight I gained recently (old news to most people here I'm sure). The insight was that you don't compile code for the "this function is not being evaluated" case - that would be pointless because that case is handled by generic graph construction and you don't need code that's specific for the function. Instead, you generate code for the case that the function participates in being evaluated ("forced"). At least for me, that was one of those "a-ha" moments that told my why generating imperative code is even relevant for a language like Haskell. The standard case is where the top-level element of the function is a pattern match: You generate code for the match expressions, freely inline-expanding code from functions called from the match expressions. Same goes for the condition if the top-level element is an if expression. The right-hand sides of the pattern matches remain unevaluated, you generate code to construct graphs, because the compiler does not know which pattern will match. I'm pretty sure I'm missing a big part of the picture and that this model is just the starting point, but anyway, here it is :-)

Max,
I had similar questions several years ago and was able to do a term
project to answer them. I wrote a really simple compiler for a
Haskell-like language called "habit". The code doesn't work anymore,
but you can read my report here:
https://github.com/m4dc4p/mil/blob/master/caffeine/report/report.pdf
It covers translating an AST into x86, closure representation, among
other topics.
Under that directory you'll also find the source code to the compiler
and a presentation I gave on it.
On Thu, Mar 10, 2016 at 4:42 AM, Maksymilian Owsianny
@Jonne
Maybe I should have been a little more precise. I'm specifically interested in a compilation step, that is turning a AST of some functional language say untyped lambda calculus into a sequence of imperative instructions.
And I'm looking for a somewhat structured materials on the subject.
If I were to give an analogy it would be how you would teach someone about sorting algorithms. You would start with simple solutions that most people come up on their own, like insert or bubble sort. Then you go on about explaining problems with complexity of that solutions and you present slightly more complex but optimal algorithms like quick or merge sort. Then maybe you would give a proof that n log n is best you can do for comparison based solutions. Then you could ask what could be done if you remove that restriction, and so on...
That way I could get a more complete understanding of the field.
Where all I was able to fish from google were papers about (to continue the analogy) theoretical complexity of a sorting algorithm on a quantum computer or constant time improvements of a heap sort given architecture with SIMD instruction set.
And of course given enough time I would be able to piece all that knowledge together, but I'm hoping someone could spare me that time.
@Gil
Thanks for suggestions, I was able to locate previously these series by Stephan Diehl and they are truly quality material, the only problem is that the first one deals with compiling an imperative language. So it is of great use as practical solution to llvm generation but doesn't really answers any of my theoretical questions about functional language compilation. The second one on the other hand deals with haskell so that's a bit of a jump already but also is still a work in progress and crucially doesn't yet have the chapters regarding compilation. As for others most of them are new to me so I guess I'll have to take a deeper look.
Ultimately I wasn't really expecting to get all that knowledge on a silver platter. I guess I'll have to dig into all that literature until I'll come out enlightened, so really anything useful you can throw at me I'll be grateful for.
Thanks, Max.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (9)
-
Gil Mizrahi
-
Joachim Durchholz
-
Jonne Ransijn
-
Justin Bailey
-
Maksymilian Owsianny
-
Matthew Pickering
-
Patrick Redmond
-
Paul Brauner
-
Richard A. O'Keefe