[Haskell-cafe] Uniplate and rewriting with different types

Neil Mitchell ndmitchell at gmail.com
Tue Jan 29 22:00:13 CET 2013

Hi Chris,

> With the following type, and transformation functions:
>     data Odd = OddOne Even | OddZero Even          deriving (Data,Typeable,Show)
>     data Even = EvenOne Odd | EvenZero Odd | Nil   deriving (Data,Typeable,Show)
>     t1,t2,t3 :: Even -> Maybe Even

> But if one of the transformations has a different type, you can't do
> it this way.  For instance, redefine t2 to have a different type:
>     t2 :: Odd -> Maybe Odd
>     t2 (OddZero (EvenOne x)) = Just $ OddZero (EvenZero x)
>     t2 x                     = Nothing
> and you are stuck because the functions of different types can't be
> combined into a single transformation.
> My question is: is there a good way to combine the transformation
> functions if they have different types?

Currently, no. Although there is something definitely related, with
transformBis: http://hackage.haskell.org/packages/archive/uniplate/1.6.10/doc/html/Data-Generics-Uniplate-Data.html#v:transformBis

That takes a list of transformation functions of different types and
acts as though you did transform on each one in turn. You could
certainly imagine adding rewriteBis in the same style, and with your
version you almost have. The transformBis function is particularly
efficient because it "knows" which traversals or parts of traversals
can be fused without changing the semantics. rewriteBis could
certainly do the same trick.

If you provided a patch for rewriteBis I'd certainly apply it!

Thanks, Neil

More information about the Haskell-Cafe mailing list