[Haskell-cafe] Why this doesn't type checked
Brent Yorgey
byorgey at seas.upenn.edu
Mon Jun 28 04:56:28 EDT 2010
On Mon, Jun 28, 2010 at 08:51:45AM +0400, Victor Nazarov wrote:
> >
> >> What we get with this instances is following code.
> >>
> >> > main =
> >> > do print (sizeof :: Sizeof Word16)
> >>
> >> Let's try it.
> >>
> >> $ runhaskell this.lhs
> >> this.lhs:78:14:
> >> Couldn't match expected type `Int'
> >> against inferred type `Sizeof sizeable'
> >> NB: `Sizeof' is a type function, and may not be injective
> >> In the first argument of `print', namely
> >> `(sizeof :: Sizeof Word16)'
> >> In the expression: print (sizeof :: Sizeof Word16)
> >> In the expression: do { print (sizeof :: Sizeof Word16) }
> >
> > Right. Since Sizeof Word8 is Int too, the type can't help determining the
> > value.
> >
>
> Then it should be ambiguous type-parameter error or something like
> this, why Int is expected?
It knows that
Sizeof Word16 = Int
which is why Int is expected. It also knows
sizeof :: Sizeable sizeable => Sizeof sizeable
so it tries to match 'Sizeof sizeable' with 'Int'. Unfortunately this
is not enough information to figure out what 'sizeable' is supposed to
be. There are quite a few options for sizeable that would make
'Sizeof sizeable = Int', and even if there were only one, type classes
are open so there could always be another one added in another module
somewhere.
-Brent
More information about the Haskell-Cafe
mailing list