[Haskell-cafe] Re: Tiger compiler in Haskell: annotating abstract syntax tree

oleg at okmij.org oleg at okmij.org
Tue Jul 20 05:06:38 EDT 2010


Jose' Romildo Malaquias wrote:

> I am writing here to ask suggestions on how to annotate an ast with
> types (or any other information that would be relevant in a compiler
> phase) in Haskell.

There is also a general way of annotating AST post factum, described in
	http://okmij.org/ftp/Algorithms.html#tree-annot

The method lets one attach arbitrarily many annotations to an already
built AST, *without* the need to change the definition of the
datatype. One does not even have to anticipate annotations! The method
would work with your AST

>   data Exp
>     = IntExp Integer
>     | VarExp Symbol
>     | AssignExp Symbol Exp

as _it is_, without any modifications -- neither to the data type
definition, nor to the tree.

The method was demonstrated when writing a compiler of sorts:
annotating an AST with an inferred type for each node. If the type
inference fails, we can still print out the inferred types for the
good subexpressions.



More information about the Haskell-Cafe mailing list