
Hello Conal,
What you've done looks very much like the Regular datatype [1] in the
rewriting library [2]. The rewriting library, as its name indicates, is very
much targeted at rewriting. For a more complete library using a sum of
products view (and without type synonyms), try the new release of EMGM [3].
Cheers,
Pedro
[1]
http://hackage.haskell.org/packages/archive/rewriting/0.1/doc/html/Generics-...
[2] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rewriting
[3] http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/EMGM
2008/11/18 Conal Elliott
Is there a simple existing library that provides views of data types in terms of unit, product and sum?
Here's what I threw together for my own use. I used associated types, though functional dependencies would work as well.
class HasView t where type View t view :: t -> View t unview :: View t -> t
-- View instances
instance HasView (Maybe a) where type View (Maybe a) = Either () a
view Nothing = (Left ()) view (Just a) = (Right a)
unview (Left ()) = Nothing unview (Right a) = (Just a)
instance HasView [a] where type View [a] = Either () (a,[a])
view [] = (Left ()) view (a:as) = (Right (a,as))
unview (Left ()) = [] unview (Right (a,as)) = (a:as)
onView2 :: (HasView a, HasView b, HasView c) => (View a -> View b -> View c) -> (a -> b -> c) onView2 op a b = unview (view a `op` view b)
Thanks, - Conal
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe