[Haskell-cafe] generalized newtype deriving allows the definition of otherwise undefinable functions

Steffen Schuldenzucker sschuldenzucker at uni-bonn.de
Tue Mar 9 01:24:35 EST 2010


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

Best regards,

Steffen


More information about the Haskell-Cafe mailing list