[Haskell-cafe] Re: Generic types
Cale Gibbard
cgibbard at gmail.com
Tue Jun 14 04:06:37 EDT 2005
On 14/06/05, Adde <adde at trialcode.com> wrote:
> Cale Gibbard <cgibbard <at> gmail.com> writes:
>
> >
> > Perhaps you want
> > data UnitValue u n =
> > (Unit u, Num n) => UnitValue {uUnit :: u, uValue :: n}
> >
>
> I tried adding UnitValue as an instance of class Show, but I can't figure out
> how to tell the compiler that u is a Unit and m is a Num (shouldn't it be able
> to figure that out from the above declaration?):
>
> instance Show (UnitValue u m) where
> show (UnitValue u m) = show m ++ (shortName u)
>
You indicate the class restrictions just before the name of the class
and type, with the usual implication arrow:
instance (Num m, Unit u) => Show (UnitValue u m) where
show (UnitValue u m) = show m ++ (shortName u)
> This is the error I'm getting:
>
> Could not deduce (Show m) from the context (Show (UnitValue u m))
> arising from use of `show' at /Users/adde/Projects/Haskell/units.hs:18:26-29
> Probable fix: add (Show m) to the class or instance method `show'
> In the first argument of `(++)', namely `show m'
> In the definition of `show': show (UnitValue u m) = (show m) ++ (shortName u)
> In the definition for method `show'
>
> Could not deduce (Unit u) from the context (Show (UnitValue u m))
> arising from use of `shortName' at
> /Users/adde/Projects/Haskell/units.hs:18:37-45
> Probable fix: add (Unit u) to the class or instance method `show'
> In the second argument of `(++)', namely `(shortName u)'
> In the definition of `show': show (UnitValue u m) = (show m) ++ (shortName u)
> In the definition for method `show'
>
When you get errors like this, there are actually two probable fixes:
the one mentioned, to add the class restraints to the method (if
you're allowed to, in this case you can't/don't want to), or to add
the class restraints to the instance, which is what you want to do.
- Cale
More information about the Haskell-Cafe
mailing list