29 Jan 2013 00:38
Uniplate and rewriting with different types
Chris Mears <chris <at> cmears.id.au>
2013-01-28 23:38:55 GMT
2013-01-28 23:38:55 GMT
Hi all,
I have a question about the Uniplate library, regarding rewriting with
transformations that have different types.
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
t1 (EvenOne (OddOne x)) = Just $ EvenOne (OddZero x)
t1 x = Nothing
t2 (EvenOne (OddZero x)) = Just $ EvenZero (OddOne x)
t2 x = Nothing
t3 (EvenZero (OddOne x)) = Just $ EvenZero (OddZero x)
t3 x = Nothing
it is easy to combine the transformations into a single
transformation, because they all have the same type. The result can
then be passed to the Uniplate's "rewriteBi" function:
allts x = t1 x `mplus` t2 x `mplus` t3 x
example = OddOne (EvenOne (OddOne (EvenOne (OddOne Nil))))
go = rewriteBi allts example
But if one of the transformations has a different type, you can't do
(Continue reading)
RSS Feed