[Haskell-beginners] instances of different kinds

Brandon S Allbery KF8NH allbery at ece.cmu.edu
Fri Aug 27 23:15:15 EDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 8/27/10 22:58 , Greg wrote:
> 
> I'm still having a hard time finding a way to make this work, even given the
> fine suggestions from Tobias and Jürgen.  I suspect there's some piece of
> information that the compiler can't make sense of that I'm just not seeing--
> a case of it insisting on doing what I say instead of what I mean...  =)
> 
> I guess the problem I'm having is finding a way to treat parametric and
> non-parametric types interchangeably.  The syntax doesn't seem to exist that
> will allow me to say:
> 
> div2pi :: (Floating a) => a -> a   -- for non parametric types (ie. Float)
> and
> div2pi :: (Floating b) => a b -> b  -- for parametric types (ie. Foo Float)
> 
> 
> In addition, I'm having a hard time understanding the errors I'm getting
> from constructs like this:
> 
> data Foo a = Foo a 
> 
> class TwoPi a where
>   div2pi :: (Floating b) => a -> b

You were told about this already:  because "b" is only mentioned in the
result of div2pi, it must be able to return *any* "b" that has a Floating
instance.

But then you say
> instance (Floating a) => TwoPi (Foo a)  where
>   div2pi (Foo x) = x / (2*pi)

An instance applies to a *specific* type; thus, this instance declaration
forces a *specific* type on the result of div2pi, when the class declaration
says it must be able to return *any* type.

Expanding the types, the class declaration says

> div2pi :: forall b. (Floating a, Floating b) => a -> b

but the instance declares

> div2pi :: (Floating a) => a -> a

The instance doesn't conform to the class definition; it includes a
constraint that the class does not, as the class insists that the type of
the result must be independent of the type of the argument, while the
instance insists that they must be identical.

Perhaps the correct question is "what exactly are you trying to do?"

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      allbery at kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkx4f0IACgkQIn7hlCsL25WexQCfTWxZxqchxvAB0Dm1wWwYQIrq
sBQAoLeaz8Lq6ydO5WJPu679WyNEu8w0
=BnBu
-----END PGP SIGNATURE-----


More information about the Beginners mailing list