[Haskell-cafe] Existential Types (I guess)
Tom Davie
tom.davie at gmail.com
Fri Jan 22 07:11:03 EST 2010
Aside from Neil's point about rank-2 polymorphism, you can of course just
parameterise your NumHolder type...
data Num a => NumHolder a = NumHolder a
instance Show a => Show NumHolder a where
show (NumHolder x) = show x
instance Functor NumHolder where
fmap f (NumHolder a) = NumHolder (f a)
It depends what you want to do with your NumHolder though. What is the
purpose of this type?
Bob
On Fri, Jan 22, 2010 at 11:31 AM, Ozgur Akgun <ozgurakgun at gmail.com> wrote:
> Dear Cafe,
>
> I can write and use the following,
>
> data IntHolder = IntHolder Integer
>
> instance Show IntHolder where
> show (IntHolder n) = show n
>
> liftInt :: (Integer -> Integer) -> IntHolder -> IntHolder
> liftInt f (IntHolder c) = IntHolder (f c)
>
> But I cannot generalise it to *Num:*
>
> data NumHolder = forall a. Num a => NumHolder a
>
> instance Show NumHolder where
> show (NumHolder n) = show n
>
> liftNum :: (Num a) => (a -> a) -> NumHolder -> NumHolder
> liftNum f (NumHolder c) = NumHolder (f c)
>
> The error message I get is the following:
>
> Couldn't match expected type `a' against inferred type `a1'
> `a' is a rigid type variable bound by
> the type signature for `liftNum' at Lifts.hs:54:16
> `a1' is a rigid type variable bound by
> the constructor `NumHolder' at Lifts.hs:55:11
> In the first argument of `f', namely `c'
> In the first argument of `NumHolder', namely `(f c)'
> In the expression: NumHolder (f c)
>
>
> Regards,
>
>
> --
> Ozgur Akgun
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100122/bdd968f6/attachment.html
More information about the Haskell-Cafe
mailing list