type specs not making it in to functions

Ashley Yakeley ashley@semantic.org
Fri, 25 Jan 2002 14:22:05 -0800


At 2002-01-25 14:00, Hal Daume III wrote:

>> class D a where constMember :: Int
>> instance D Int where constMember =3D 8
>
>It seems ehre that there's no way to extract constMember for a
>/particular/ class, since you can't tell it what "a" is supposed to
>be.  So, instead, I do:
>
>> class D a where constMember :: a -> Int -- should be independent of arg
>> instance D Int where constMember _ =3D 8
>
>(which brings me to my use of "undefined::a").  But is there a
>better/preferred way to do this?

Unfortunately this seems to be the preferred way to do this. For 
instance, from the Prelude:

  class=A0=A0(RealFrac=A0a,=A0Floating=A0a)=A0=3D>=A0RealFloat=A0a=A0=A0whe=
re
=A0=A0=A0=A0floatRadix=A0=A0=A0=A0=A0=A0=A0::=A0a=A0->=A0Integer
=A0=A0=A0=A0floatDigits=A0=A0=A0=A0=A0=A0::=A0a=A0->=A0Int

It's not the _best_ way to do it, in my opinion. I do this:

  data Type a =3D Type

  class D a where
    constMember :: Type a -> Int

  instance D Int where
    constMember Type =3D 8

  intConst =3D constMember (Type :: Type Int)

See 
<http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/jvm-bridge/sourc
e/Haskell/Type.hs?rev=3DHEAD&content-type=3Dtext/plain>.

Why is this better?

1. It's absolutely clear to the user of the class that constMember 
depends only on the type.

2. It prevents the user from putting in an inappropriate "back channel" 
in the member for some instance of the class.

3. It avoids use of 'undefined', which is just plain ugly. After all, 
intuitively everything is defined.

-- 
Ashley Yakeley, Seattle WA
Almost empty page: <http://semantic.org/>