[Haskell-cafe] Question related to Multi Param Type Classes
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Mon May 5 14:03:30 EDT 2008
On May 5, 2008, at 11:52 , Sai Hemanth K wrote:
> when I loaded it on ghci ( invoked with -XMultiParamTypeClasses ),
> I got an essay in greek (or is it latin?), which started something
> like below:
> Could not deduce (MyString m c) from the context (MyString m c4)
> arising from a use of `zLength' at GenericZAlgo.lhs:42:21-31
Other people gave you the "how"; here's the why.
The problem is that, with the information available to ghc from your
definitions, there is no way to fix a type for "c". It can't use the
instances it knows about because type classes are open; that is,
someone could add another instance in a different source file (or
even later in the existing one) and suddenly "c" would not be
determinable. This is considered a bad thing.
What this means in practice is that when GHC tries to type
"compareStr", because "c" is never used in the function, it can fix
"m" but it can't fix "MyString m c". Therefore it can't prove that
the same "MyString m c" applies to both arguments that refer to "m"
--- which is what that error quoted above means.
The functional dependency "MyString m c | m -> c" tells GHC that any
specific "m" determines a specific "c". It doesn't matter what the
type is here, since it's not used in the definition of compareStr;
but it must be possible to know that the same "c" is being used in
both arguments. Given this, it can fix "MyString m c" knowing only
"m" and as a result the function typechecks.
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
More information about the Haskell-Cafe