Martijn van Steenbergen has a good blog post that describes the method I generally use:<br><a href="http://martijn.van.steenbergen.nl/journal/2010/06/24/generically-adding-position-information-to-a-datatype/">http://martijn.van.steenbergen.nl/journal/2010/06/24/generically-adding-position-information-to-a-datatype/</a><br>
<br>In his example he annotates the expression tree with position information, but you can use the same method to add type annotations, or whatever you want.<br><br>- Job<br><br><br><div class="gmail_quote">2010/7/19 José Romildo Malaquias <span dir="ltr">&lt;<a href="mailto:j.romildo@gmail.com">j.romildo@gmail.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hello.<br>
<br>
In his book &quot;Modern Compilder Implementation in ML&quot;, Appel presents a<br>
compiler project for the Tiger programming language where type checking<br>
and intermediate code generation are intrinsically coupled.<br>
<br>
There is a function<br>
<br>
  transExp :: Absyn.Exp -&gt; (Tree.Exp,Types.Type)<br>
<br>
that do semantic analysis, translating an expression to the Tree<br>
intermediate representation language and also do type checking,<br>
calculating the type of the expression.<br>
<br>
Maybe the compiler can be made more didatic if these phases are separate<br>
phases of compilation.<br>
<br>
The type checker would annotate the abstract syntax tree (ast) with type<br>
annotations, that could be used later by the translater to intermediate<br>
representation.<br>
<br>
In an imperative language probably each relevant ast node would have a<br>
field for the type annotation, and the type checker would assign the<br>
type of the node to this field after computing it.<br>
<br>
I am writing here to ask suggestions on how to annotate an ast with<br>
types (or any other information that would be relevant in a compiler<br>
phase) in Haskell.<br>
<br>
As an example, consider the simplified ast types:<br>
<br>
  data Exp<br>
    = IntExp Integer<br>
    | VarExp Symbol<br>
    | AssignExp Symbol Exp<br>
    | IfExp Exp Exp (Maybe Exp)<br>
    | CallExp Symbol [Exp]<br>
    | LetExp [Dec] Exp<br>
<br>
  data Dec<br>
     = TypeDec Symbol Ty<br>
     | FunctionDec Symbol [(Symbol,Symbol)] (Mybe Symbol) Exp<br>
     | VarDec Symbol (Maybe Symbol) Exp<br>
<br>
Expressions can have type annotations, but declarations can not.<br>
<br>
Comments?<br>
<br>
<br>
Regards,<br>
<br>
Romildo<br>
<font color="#888888">--<br>
Computer Science Department<br>
Universidade Federal de Ouro Preto, Brasil<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</font></blockquote></div><br>