Fwd: [Haskell-beginners] instances of different kinds
Tobias Brandt
tob.brandt at googlemail.com
Fri Aug 27 05:15:47 EDT 2010
Forgot to reply to the list, sorry.
---------- Forwarded message ----------
From: Tobias Brandt <tob.brandt at googlemail.com>
Date: 27 August 2010 11:14
Subject: Re: [Haskell-beginners] instances of different kinds
To: Greg <greglists at me.com>
On 27 August 2010 10:58, Greg <greglists at me.com> wrote:
> data Foo a = Foo a
>
> class TwoPi a where
> div2pi :: (Floating b) => a -> b
>
div2pi is polymorphic in a AND b. They are completely independent.
> instance (Floating a) => TwoPi (Foo a) where
> div2pi (Foo a) = a / (2*pi)
>
but here, div2pi has type Floating a => Foo a -> a.
>
> data Foo a = Foo a
>
> class TwoPi a where
> div2pi :: a -> Float
>
> instance (Floating b) => TwoPi (Foo b) where
> div2pi (Foo b) = b / (2*pi)
>
now div2pi has type Floating a :: Foo a -> a, but should
have Floating a :: Foo a -> Float
There are two possible solutions (I can think of).
1. make div2pi less polymorphic:
class TwoPi a where div2pi :: a -> a
then your first instance works
2. use associated types:
class TwoPi a where
type TwoPiRes a
div2pi :: a -> TwoPiRes a
instance Floating a => TwoPi (Foo a) where
type TwoPiRes (Foo a) = a
div2pi (Foo a) = a /(2*pi)
More information about the Beginners
mailing list