type specs not making it in to functions
Hal Daume III
hdaume@ISI.EDU
Fri, 25 Jan 2002 14:00:08 -0800 (PST)
consider the following definition:
> class C a where foo :: a -> Int
> instance C Bool where foo _ = 5
I can then say:
> bar :: C a => a -> Int
> bar (x :: a) = foo (undefined :: a)
But not:
> bar :: C a => a -> Int
> bar x = foo (undefined :: a)
because it tries to use a new scope for the type variable a and doesn't
unify it with the one in the type spec. Why not?
On a more general note, suppose I have a class and there are "constant
members" of the class. For instance, something like:
> class D a where constMember :: Int
> instance D Int where constMember = 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 _ = 8
(which brings me to my use of "undefined::a"). But is there a
better/preferred way to do this?
- Hal
--
Hal Daume III
"Computer science is no more about computers | hdaume@isi.edu
than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume