I have a numeric type Number for use in a dynamically typed language where
Exact numbers are considered a subset of Inexact numbers. For this reason, it
makes sense to call functions like truncate, round, etc. on Exact numbers. To
implement this, I made Number an instance of RealFrac, whose methods include
the above functions; note that all the methods are defined by default in terms
of one method, properFraction. properFraction has the following type:
> properFraction :: (Integral b) => a -> (b,a)
Here is the Number type I am using:
> data Number = Exact Int
> | Inexact Double
> deriving (Eq)
Here is the RealFrac instance:
> instance RealFrac Number where
> properFraction n@(Exact x) = (n, 1)
> properFraction (Inexact x) = (Exact (fst pf), Inexact (snd pf))
> where pf = properFraction x
I also have instances for the superclasses of RealFrac and an instance for
Integral (and Text). When I compile the module, I get this error from
GHC-0.26:
"Domains.lhs", line 212: Type signature mismatch:
Signature for class method `properFraction' doesn't match its inferred type.
Signature: Number -> (b._370, Number)
Inferred type: Number -> (Number, Number)
Now if b._370 is free, then I ought to be able to instantiate it with
Number, yet GHC considers this a type mismatch. Why? My type appears to be
too specific but as far as I can tell it is not inconsistent; if it were an
ambiguity, I would think to use an explicit signature in the function body or
use the function asTypeOf, but clearly there is no way to make a type more
general.
I have also looked through the Report a few times, checking for relevant
restrictions on classes and overloading instances, and although there is a lot
of stuff about ambiguities, nothing seems to apply to this problem. Also, as
far as I can see, this does not appear to be an instance of the monomorphism
restriction.
Has anyone encountered this problem before? Is it a compiler bug, an
oversight in the language or am I just doing something wrong? Comments and
workarounds would be appreciated.
Thanks.
Frank Christoph <[EMAIL PROTECTED]>