[Haskell-cafe] Existential Types (I guess)

Ozgur Akgun ozgurakgun at gmail.com
Fri Jan 22 10:46:02 EST 2010


OK, I wasn't storing a simple (Num a) in my holder data structure, it was
more of a thing implementing one of my type classes. So it knows how to
quack, and I can use it now, thanks to Neil's suggestion.

I have a (somewhat) heterogeneus list of values, belonging to the same type
class, and I just wanted to map a function to that list.

Now it works.

BTW Tom, Thanks for your suggestion, but it simply doesn't fit my needs this
time. But I'll keep your suggestion in mind.

Cheers guys!



2010/1/22 Tom Davie <tom.davie at gmail.com>

> 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
>>
>>
>


-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100122/34a885b5/attachment.html


More information about the Haskell-Cafe mailing list