[Haskell-cafe] Generic types

Adde adde at trialcode.com
Mon Jun 13 15:23:41 EDT 2005

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:
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}

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

But I can't get UnitValue to accept Meter as a Unit:

*Units> UnitValue Meter 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.

More information about the Haskell-Cafe mailing list