[Haskell-cafe] generalized newtype deriving allows the definition
of otherwise undefinable functions
g9ks157k at acme.softbase.org
Tue Mar 9 04:37:50 EST 2010
Am Dienstag, 9. März 2010 07:24:35 schrieb Steffen Schuldenzucker:
> On 03/08/2010 10:45 PM, Wolfgang Jeltsch wrote:
> > The point is, of course, that such conversions are not only possible for
> > binary operations but for arbitrary values and that these conversions are
> > done by a single generic function conv. I don’t think it would be
> > possible to implement conv without generalized newtype deriving.
> > Any thoughts?
> Hi Wolfgang,
> it's not exactly the same, but...
> > import Control.Applicative
> > newtype Wrapped a = Wrap a deriving Show
> > instance Functor Wrapped where
> > fmap f (Wrap x) = Wrap $ f x
> > instance Applicative Wrapped where
> > pure = Wrap
> > (Wrap f) <*> (Wrap x) = Wrap $ f x
> > convBinOp :: (a -> a -> a) -> (Wrapped a -> Wrapped a -> Wrapped a)
> > convBinOp op x y = pure op <*> x <*> y
I think this is fundamentally different. As I said above:
> The point is, of course, that such conversions are not only possible for
> binary operations but for arbitrary values and that these conversions are
> done by a single generic function conv.
Your applicative functor Wrapped allows conversions only for n-ary functions,
so, for example, John Meachem’s trick to break the invariant of Set doesn’t
work. On the other hand, you need a separate conversion function for each
arity (pure, fmap, liftA2, liftA3, …) whereas generalized newtype deriving
allows you to use the same conversion function for all arities.
More information about the Haskell-Cafe