[Haskell-cafe] simple generic / data view library?
conal at conal.net
Tue Nov 18 02:03:52 EST 2008
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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe