[Haskell-cafe] how to factorize propagation of a function over a data type

Daniel Trstenjak daniel.trstenjak at gmail.com
Mon Dec 2 13:48:20 UTC 2013


> class FooClass a where
>     foo1 :: a -> a
>     foo2 :: a -> a
> 
> instance FooClass Integer where
> 
>     foo1 v = 1
>     foo2 v = 2
> 
> data Bar = Bar1 Integer
>          | Exp1 Bar Bar
>          | Exp2 Bar Bar
>     deriving Show
> 
> instance FooClass Bar where
>     
>     foo1 b = case b of
>         Bar1 i     -> Bar1 (foo1 i)
>         Exp1 b1 b2 -> Exp1 (foo1 b1) (foo1 b2)
>         Exp2 b1 b2 -> Exp2 (foo1 b1) (foo1 b2)
> 
>     foo2 b = case b of
>         Bar1 i     -> Bar1 (foo2 i)
>         Exp1 b1 b2 -> Exp1 (foo2 b1) (foo2 b2)
>         Exp2 b1 b2 -> Exp2 (foo2 b1) (foo2 b2)


I think you're really asking for a generics library like 'uniplate':
http://community.haskell.org/~ndm/darcs/uniplate/uniplate.htm


Greetings,
Daniel


More information about the Haskell-Cafe mailing list