[Haskell-cafe] Is there already an abstraction for this?

Sebastian Fischer sebf at informatik.uni-kiel.de
Mon Sep 22 22:39:30 EDT 2008


Hi Jeremy,

There are some approaches that support such generic transformations.  
The simplest is probably Uniplate by Neil Mitchell:

   http://www-users.cs.york.ac.uk/~ndm/uniplate/

The function 'rewrite' is what you are looking for. If you change the  
definition of 'identity' to:

> identity (Sum (Lit 0) a)        = Just a
> identity (Sum a (Lit 0))        = Just a
> identity (Difference a (Lit 0)) = Just a
> identity (Product a (Lit 1))    = Just a
> identity (Product (Lit 1) a)    = Just a
> identity (Quotient a (Lit 1))   = Just a
> identity _                      = Nothing

then the function 'rewrite identity :: Expr -> Expr' does what you want.

Cheers,
Sebastian


More information about the Haskell-Cafe mailing list