[Haskell-cafe] Annotations in abstract syntax tree

Stefan Holdermans stefan at vectorfabrics.com
Fri Apr 27 14:40:04 CEST 2012


Romildo,

> How should an instance of (Functor ExprF) be defined? It is not shown in
> the thesis.

It's actually quite straigtforward:

  instance Functor ExprF where
    fmap _ (Num n)      = Num n
    fmap _ (Var x)      = Var x
    fmap f (Bin op l r) = Bin op (f l) (f r)

As expected you get:

  > cata exprEval (runExpr (1+2*3))
  7.0

HTH,

  Stefan



More information about the Haskell-Cafe mailing list