[Haskell-cafe] Generic types
Cale Gibbard
cgibbard at gmail.com
Mon Jun 13 16:10:24 EDT 2005
Perhaps you want
data UnitValue u n =
(Unit u, Num n) => UnitValue {uUnit :: u, uValue :: n}
On 13/06/05, Adde <adde at trialcode.com> wrote:
> I'm writing a small unit library to excercise my barely existing
> Haskell skills.
> However I can't figure out how to make Haskell accept any type
> of a specific class.
>
> This is what I've got so far:
> <code>
> class Unit u where
> shortName :: u -> String
>
> data Meter = Meter
>
> instance Unit Meter where
> shortName u = "m"
>
> data UnitValue = UnitValue {uUnit :: Unit u => u, uValue :: Num n => n}
> </code>
>
> Meter is supposed to be a type in the Unit class.
> UnitValue is supposed to represent unit values regardless
> of the Unit type.
>
> Meter appears to be a subclass of Unit, so far so good:
>
> *Units> shortName Meter
> "m"
>
> But I can't get UnitValue to accept Meter as a Unit:
>
> *Units> UnitValue Meter 10
>
> <interactive>:1:10:
> Couldn't match the rigid variable `u' against `Meter'
> `u' is bound by the polymorphic type `forall u. (Unit u) => u'
> at <interactive>:1:0-17
> Expected type: u
> Inferred type: Meter
> In the first argument of `UnitValue', namely `Meter'
> In the definition of `it': it = UnitValue Meter 10
>
> I strongly suspect that I've got the declaration of UnitValue wrong
> but I've spent two hours searching the web and trying to get it to
> work without any success.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list