[Haskell-cafe] Re: Morphing ASTs and scrapping boilerplate code

Joel Reymont joelr1 at gmail.com
Thu Apr 19 12:22:13 EDT 2007

Just to clarify, I really liked the SYB solution to my previous issue  
with stripping token locations. It looked like this:

strip :: (Data a) => a -> a
strip = everywhere (mkT f)
     where f (TokenPos a _) = a
           f x = x

In the general AST transformation case, my constructor name is the  
same and so is the number of arguments. So, yes, it's a different  
type but it's a derivative of Data and Typeable just like the  
original type.

Conceptually, I want to "lookup" the new type from a given module,  
using the same constructor name as the original type. Then I want to  
apply morph to every argument of the source constructor and give the  
resulting values to the new constructor.

Lastly, I would like to write pattern matches for special cases (see  
TokenPos above) and write out the transformation by hand. My ASTs are  
quite large and there aren't that many special cases.

	Thanks, Joel

On Apr 19, 2007, at 5:11 PM, Joel Reymont wrote:

> instance Morpher Type C.Type where
>     morph (TyList ty) = liftM C.TyList (morph ty)
>     morph (TyArray ty) = liftM C.TyArray (morph ty)
>     morph (TySeries ty) = liftM C.TySeries (morph ty)
>     morph (TyInput ty) = liftM C.TyProp (morph ty)
>     morph (TyRef ty) = liftM C.TyRef (morph ty)
>     morph TyUnit = return C.TyUnit
>     morph TyPrintDest = return C.TyPrintDest


More information about the Haskell-Cafe mailing list