[email protected]
[Top] [All Lists]

Re: [Haskell-cafe] Existential Types (I guess)

Subject: Re: [Haskell-cafe] Existential Types I guess
From: Tom Davie
Date: Fri, 22 Jan 2010 12:11:03 +0000
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 <[email protected]> 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
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe
<Prev in Thread] Current Thread [Next in Thread>