[Haskell-cafe] simple generic / data view library?
José Pedro Magalhães
jpm at cs.uu.nl
Tue Nov 18 02:11:30 EST 2008
What you've done looks very much like the Regular datatype  in the
rewriting library . 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 .
2008/11/18 Conal Elliott <conal at conal.net>
> 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 at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe