Re: [Haskell] Compiler Construction course using Haskell?

On Wed, 2008-08-20 at 16:03 +0200, Johannes Waldmann wrote:
Hello.
I plan to give a course in compiler construction, using Haskell as the implementation language (not as source or target language).
Something along these lines: 1. combinator parsers (Parsec), 2. simple interpreter (arithmetical expressions) 3. add algebraic data types, functions 4. type checker 5. code generator. Ideally, 2..5 would be using the very same tree traversal code and just change the monad for evaluation.
Any comments appreciated. Have you given such a course? Taken?
[I've replied to Haskell-Cafe which is a better list for this discussion.] 2 & 3 are going to have different trees so using the same tree traversal code would require some finesse, though the code for 2 should only need extension not change. One thing you may want to look at is attribute grammars which can be cast into a monadic framework and gives a natural example of using the backward state monad and allows you to connect to other formalisms used for compiler construction. Another possibility is abstract interpretation. Both code generation and type checking can be viewed as examples of abstract interpretation (as well as, of course, actual interpretation.) Also many analyses fit into the model of abstract interpretation. I'd also recommend transforming the initial AST to some intermediate form before doing 2-5. Not only is this a virtually universal practice, but it will the later stages, particularly the interpreter and the code generator easier to write and, in the code generator's case, able to produce better output. Ultimately, trying to have the same code produce all of these is going to lead to some compromises. You are either going to have to forsake some quality in the output, or you are going to have unnatural or, at least, non-trivial implementations of some the functions. The recommendation to for the use of a intermediate language mitigates this somewhat.

On 08/20/08 11:43, Derek Elkins wrote:
On Wed, 2008-08-20 at 16:03 +0200, Johannes Waldmann wrote:
Hello.
I plan to give a course in compiler construction, using Haskell as the implementation language (not as source or target language).
Something along these lines: 1. combinator parsers (Parsec), 2. simple interpreter (arithmetical expressions) 3. add algebraic data types, functions 4. type checker 5. code generator. Ideally, 2..5 would be using the very same tree traversal code and just change the monad for evaluation.
Any comments appreciated. Have you given such a course? Taken?
[I've replied to Haskell-Cafe which is a better list for this discussion.]
2 & 3 are going to have different trees so using the same tree traversal code would require some finesse, though the code for 2 should only need extension not change.
One thing you may want to look at is attribute grammars which can be cast into a monadic framework and gives a natural example of using the backward state monad and allows you to connect to other formalisms used for compiler construction.
Could you give some examples or links or references?
Another possibility is abstract interpretation. Both code generation and type checking can be viewed as examples of abstract interpretation (as well as, of course, actual interpretation.) Also many analyses fit into the model of abstract interpretation.
Links or references? [snip] I'd also would like to see First and Follow sets: http://www.jambe.co.nz/UNI/FirstAndFollowSets.html calculated in haskell. I'd think it would be natural since, AFAICT, the calculation of the First sets is a homomorphism on the algebra of grammar expressions. IOW FIRST( x <|> y ) = set_union(FIRST(x),FIRST(y)) and I *think* maybe a similar definition can be made for the sequencing operator. Since I've seen haskell associated with algebras and expecially monads, I guess haskell would be especially adept at this. WARNING: I've not written a line of haskell code. -regards Larry

I plan to give a course in compiler construction, using Haskell as the implementation language (not as source or target language).
Something along these lines: 1. combinator parsers (Parsec), 2. simple interpreter (arithmetical expressions) 3. add algebraic data types, functions 4. type checker 5. code generator. Ideally, 2..5 would be using the very same tree traversal code and just change the monad for evaluation.
Any comments appreciated. Have you given such a course? Taken?
At Utrecht University, they teach excellent courses on exactly this subject, using Haskell. The course webpage [1] is probably a useful resource for you, as it shows exactly what we were thought (I participated in the course last year). We made heavy use of Utrecht's Attribute Grammar Compiler [2], which is a pre-processor for Haskell that allows you to very easily build programs using an attribute grammar. This proved to be a really nice way to do the tree transformations. I very much liked the idea of attribute grammars, but I personally don't like pre-processors very much. Also, we targeted Simple Stack Machine as a platform. This is an assembly-like language that has a graphical interpreter, so you can actually see your code, do single-stepping, see your stack, etc. It proved to be quite useful. As a student, I it added a lot of educational value, but a real language would also be cool (e.g. Harpy [4]). HTH, -chris [1]: http://www.cs.uu.nl/docs/vakken/ipt/ [2]: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/uuagc [3]: http://people.cs.uu.nl/atze/SSM/index.html [4]: http://uebb.cs.tu-berlin.de/harpy/

chris:
I plan to give a course in compiler construction, using Haskell as the implementation language (not as source or target language).
Something along these lines: 1. combinator parsers (Parsec), 2. simple interpreter (arithmetical expressions) 3. add algebraic data types, functions 4. type checker 5. code generator. Ideally, 2..5 would be using the very same tree traversal code and just change the monad for evaluation.
Any comments appreciated. Have you given such a course? Taken?
At Utrecht University, they teach excellent courses on exactly this subject, using Haskell. The course webpage [1] is probably a useful resource for you, as it shows exactly what we were thought (I participated in the course last year). We made heavy use of Utrecht's Attribute Grammar Compiler [2], which is a pre-processor for Haskell that allows you to very easily build programs using an attribute grammar. This proved to be a really nice way to do the tree transformations. I very much liked the idea of attribute grammars, but I personally don't like pre-processors very much.
Also, we targeted Simple Stack Machine as a platform. This is an assembly-like language that has a graphical interpreter, so you can actually see your code, do single-stepping, see your stack, etc. It proved to be quite useful. As a student, I it added a lot of educational value, but a real language would also be cool (e.g. Harpy [4]).
And Language.C http://hackage.haskell.org/cgi-bin/hackage-scripts/package/language-c
participants (4)
-
Chris Eidhof
-
Derek Elkins
-
Don Stewart
-
Larry Evans