Writing a compiler in Hakell

Hi everyone. I am designing my own programming language. I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction? I know about Happy. Is that a good tool to use? The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows. Are there any good books that you can recommend on compiler construction in general and specific to haskell? On another note, how is the operator + implemented in haskell? is there a primitve (say #+) that is wrapped by the haskell operator +? Maybe something like: (+) :: a -> a -> a v1 + v2 = #+ v1 v2 Thanks in advance Rouan.

2009/5/6 Rouan van Dalen
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows.
Hi, This seems an interesting project. Can you tell us a bit more about your language (functional, dynamic, lazy, does it support call/cc, ...) ? What about the target language (C, C--, x86 assembly, some virtual machine) ? What about the syntax ? Depending of the kind of answers you might provide, specific directions can probably be given. Also, you say "maybe competing with compilers written in C", do you really talk about compiler performance or about generated code performance ? Once those questions are resolved, you can go to hackage, see a list of packages. http://hackage.haskell.org/packages/archive/pkg-list.html For x86 machine code: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/harpy For LLVM: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/llvm For C: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/language-c Cheers, Thu

On Wed, May 6, 2009 at 12:07 AM, Rouan van Dalen
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
I don't like Happy. Using a parser generator is like admitting defeat, when we have no defeat to admit. I would recommend a parser combinator library like Parsec (or if you like building things from the ground up, ReadP).
The compiler is intended for serious use and I would like it to be very efficient, maybe competing
with compilers written in C. It should also be very easy to extend as the
languoge grows.
I agree with minh thu; there are too many questions to consider before giving too much specific advice. But in almost all cases, plan on the production version of your compiler being written in the language it is compiling. This has many advantages.
Are there any good books that you can recommend on compiler construction in general and specific to haskell?
On another note, how is the operator + implemented in haskell?
is there a primitve (say #+) that is wrapped by the haskell operator +? Maybe something like:
(+) :: a -> a -> a v1 + v2 = #+ v1 v2
It varies from compiler to compiler, but in general, yes. And in GHC I think it is called +#, actually, but you need to do some digging to find it, and your code won't be portable once you do. Luke

On 06/05/2009 07:31, Luke Palmer wrote:
On Wed, May 6, 2009 at 12:07 AM, Rouan van Dalen
mailto:rvdalen@yahoo.co.uk> wrote: Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
I don't like Happy. Using a parser generator is like admitting defeat, when we have no defeat to admit. I would recommend a parser combinator library like Parsec (or if you like building things from the ground up, ReadP).
There are distinct advantages to using a parser generator over parsing combinators. For example, you get a static guarantee that your language is unambiguous and doesn't need backtracking. (and we like static guarantees!) I seem to recall there are parsing combinators that generate LR parsing tables at runtime, which might be a good compromise though. Cheers, Simon

Hello Rouan
My bible : The dragon book of Aho, Sethi & Ullman
http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools
Regards
J-C
On Wed, May 6, 2009 at 8:07 AM, Rouan van Dalen
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows.
Are there any good books that you can recommend on compiler construction in general and specific to haskell?
On another note, how is the operator + implemented in haskell?
is there a primitve (say #+) that is wrapped by the haskell operator +? Maybe something like:
(+) :: a -> a -> a v1 + v2 = #+ v1 v2
Thanks in advance
Rouan.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, May 5, 2009 at 11:07 PM, Rouan van Dalen
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I think GHC itself uses Alex and Happy for tokenizing and parsing respectively. Parsec is a nice parser combinator library. Another tool you could employ is Higher Order Abstract Syntax (more of an idiom), and there are papers about it if you google for them. It's a way of representing the program you want to compile and doing transformations on it.
I know about Happy. Is that a good tool to use?
My understanding is that GHC uses it. I think they are happy with happy, but maybe a GHC dev could comment.
The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows.
I think one of the biggest slow downs for compilers is the complexity of parsing and the complexity of the compilation. For example, If you pick a grammar that parses efficiently and do less sophisticated analyses involving a single pass and so on it will be fast to compile. The draw back is that the compiled programs may execute more slowly than if you did lots of analysis or optimization. As an example of what not to do, C++ has a notoriously complex to parse grammar along with some other issues that make it a slow compiling language :)
Are there any good books that you can recommend on compiler construction in general and specific to haskell?
I like "Modern Compiler Design" myself: http://www.amazon.com/Modern-Compiler-Design-D-Grune/dp/0471976970 I would also recommend reading the haskell report to get an idea of what a friendly language specification looks like: http://www.haskell.org/onlinereport/ One point of advice I would give is defining your language as an embedded language in Haskell first. You can start with the data type that holds your intermediate representation or abstract syntax. Then you can work on either code generation or evaluation. As you start to settle on your data structures and the language primitives you can begin working on a parser. In a sense, the parser is the last part you need to finish. I've found this approach to be quite accommodating and to also save me a lot of time. If you google for it, there is a fair bit of literature about domain specific and embedded domain specific languages in Haskell. It's quite a good language for prototyping this way. Once you write some code you'll discover that some things are taking too long. In that case, you'll want to learn how to make proper use of the GHC profiler, assuming you use GHC as your haskell compiler. It's quite powerful and by using it correctly you can zoom in on the slow spots and dramatically improve the performance of your compiler. I found this paper to be a nice way to get started: http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf One of the brilliant bits in that paper is that it teaches you to use an existing compiler (gcc) to generate code and then learn how to implement things by example! I highly recommend reading it. It's written about a scheme compiler, but it applies equally well to other languages. Good luck! Jason

Rouan van Dalen wrote:
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows.
Be sure to take a look at attribute grammars (AG). From what I hear it varies whether people like using them, but they're worth a try at least. The Monad Reader Issue 4 has an introductory article on AGs [1] by Wouter Swierstra, explaining what they do and how they are useful. I hear Happy has an AG system [2]. Probably the most mature standalone AG is the UUAG [3], with which the Utrecht Haskell Compiler [4] was built. HTH, Martijn. [1] http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Gra... [2] http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html [3] http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem [4] http://www.cs.uu.nl/wiki/UHC

2009/5/6 Rouan van Dalen
I know about Happy. Is that a good tool to use?
Alex and Happy are used in (at least) these two packages: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/language-python http://hackage.haskell.org/cgi-bin/hackage-scripts/package/language-c You should be able to find lots of useful code to start with in both of those. You can get good performance out of Alex and Happy. Of course there are more considerations than just performance, and you will get different opinions about those other aspects. I wrote the python parser above and I was quite pleased with Alex and Happy from a usability perspective. LLVM is a nice framework for building the backend of a compiler, and the Haskell bindings are quite useful: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/llvm The amount of work you have to do depends on your runtime system (GC, threads, exceptions etc). If your language has fairly conventional semantics (a typical imperative language) then you can reuse a lot of the existing infrastructure that LLVM provides.
On another note, how is the operator + implemented in haskell?
It is good to differentiate Haskell "the language specification" from compilers (and other tools) which implement it. The language specification does not say exactly how the primitive operations should be implemented; that is a detail which is up to the compiler writer to decide. GHC's implementation is the best documented, and there are many research papers which describe aspects of it (some outdated). For numerical primitives, you could start by looking at this paper: "Unboxed values as first class citizens in a non-strict functional language" Peyton Jones and Launchbury. http://research.microsoft.com/en-us/um/people/simonpj/papers/unboxed-values.... Cheers, Bernie.

On May 6, 2009, at 8:07 AM, Rouan van Dalen wrote:
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows.
Are there any good books that you can recommend on compiler construction in general and specific to haskell?
What is your goal? Writing a compiler, for fun and educational purposes, or to implement a new language? When the actual language is the main purpose and your language will be a functional one you can try to piggyback on existing compilers. For example, translate your language to the GHC core or UHC[1] core and you get a cross platform highly optimizing compiler for free. You can now freely focus on the semantics of your language and let an existing code generator do the dirty work for you. When, in the end when your languages is finished, the existing compiler is not fast enough, improve it and everyone benefits!
On another note, how is the operator + implemented in haskell?
is there a primitve (say #+) that is wrapped by the haskell operator +? Maybe something like:
(+) :: a -> a -> a v1 + v2 = #+ v1 v2
Thanks in advance
Rouan.
[1] https://svn.cs.uu.nl:12443/repos/EHC/trunk/EHC/src/ehc/Core/AbsSyn.cag -- Sebastiaan Visser

Dear Rouan, on http://www.cs.uu.nl/wiki/HUT/WebHome you will find a collection of tools which may help you to construct a compiler. As an example you will find a Tiger compiler constructed with the uulib tools and the uuagc attribute grammar system. Tiger is the language used in the book series by Andrew Apple. Not that Tiger is a great language, but the compiler contains an instance of all the things that have to be done when writing a compiler. Once you like these tools you may take a look at the UHC compiler, which actually is a series of compilers, starting from a small language, which is than gradually extended, both with new language concepts and with new aspects, such as code generation, new forms of types etc. Here you will also see that writing a compiler for a language like Haskell is not a small endeavour. You said you wanted to write a compiler in Haskell, but you did not say what language you want to compile? This sounds a bit strange: I have chosen my tool, but now I am going to decide what to use it for? With respect to your question about the + operator. There are many answers to this question; but it depends on what language you are compiling, with which goal in mind etc. It makes a big difference whether you are compiling a language like Perl into some byte-code language which is to be interpreted in which case your aproach looks reasaonable, or a special purpose language describing finite state machines into code for an embedded system in which case your approach does not work out. Doaitse Swierstra On 6 mei 2009, at 08:07, Rouan van Dalen wrote:
Hi everyone.
I am designing my own programming language.
I would like to know what is the best way to go about writing my compiler in haskell. What are the tools available in haskell that can help with compiler construction?
I know about Happy. Is that a good tool to use?
The compiler is intended for serious use and I would like it to be very efficient, maybe competing with compilers written in C. It should also be very easy to extend as the languoge grows.
Are there any good books that you can recommend on compiler construction in general and specific to haskell?
On another note, how is the operator + implemented in haskell?
is there a primitve (say #+) that is wrapped by the haskell operator +? Maybe something like:
(+) :: a -> a -> a v1 + v2 = #+ v1 v2
Thanks in advance
Rouan.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Doaitse Doaitse Swierstra wrote:
Dear Rouan,
on
http://www.cs.uu.nl/wiki/HUT/WebHome
you will find a collection of tools which may help you to construct a compiler. As an example you will find a Tiger compiler constructed with the uulib tools and the uuagc attribute grammar system. Tiger is the language used in the book series by Andrew Apple. Not that Tiger is a great language, but the compiler contains an instance of all the things that have to be done when writing a compiler.
Once you like these tools you may take a look at the UHC compiler, which actually is a series of compilers, starting from a small language, which is than gradually extended, both with new language concepts and with new aspects, such as code generation, new forms of types etc. Here you will also see that writing a compiler for a language like Haskell is not a small endeavour.
I tried the uu-parsinglib and must admit, that I got a bit frustrated. In my rant below I may sound very negative, but that is not my intention. My intention is to give you my initial impression of the rough spots when trying uu-parsinglib, which you may (or may not) use if you want to make uu-parsinglib more newcomer friendly. First the 55 page document "Combinator Parsing: A Short Tutorial" has, in my mind, some shortcoming: * A 55 page document should have a table of contents. It helps people understand the structure, and makes the document easier to navigate. * It is not really a tutorial! Or at least, not a tutorial that gets you quickly writing parses using uu-parselib. It describes alternative implementation, why something is implemented in a particular way, how something is implemented, ... These are all good things, but not something that helps me get started. They belong in an advanced section. Or maybe it would be better with two documents, with different target audiences. Furthermore, the Tiger example is good. But please provide type signatures for all functions. The signatures may be obvious to you, but for the newcomer they may not be. When I look at the uu-parselib interface [1] it seems very sparse. Compared to Parsec, I miss a lot of the standard combinators [2]. It seems like you have to implement those combinators yourself. Finally, there is no Haddock documentation in uu-parselib. The lack of entry-level documentation and few predefined parsing combinators in uu-parselib do make for a steep learning curve. Kind regards, Mads Lindstrøm [1] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/uu-parsinglib [2] http://hackage.haskell.org/packages/archive/parsec/3.0.0/doc/html/Text-Parse...
participants (11)
-
Bernie Pope
-
Jason Dagit
-
jean-christophe mincke
-
Luke Palmer
-
Mads Lindstrøm
-
Martijn van Steenbergen
-
minh thu
-
Rouan van Dalen
-
S. Doaitse Swierstra
-
Sebastiaan Visser
-
Simon Marlow