[Haskell-beginners] typeclass confusion

Greg greglists at me.com
Mon Aug 23 22:33:21 EDT 2010


Hi--

I'm back to trying to teach myself Haskell, and I've already got myself into a muddle again.  I've begun playing a bit with the type system, and typeclasses and figured I'd build a type to hold angular values since it would be nice to force the type system to check whether I'm using degrees or radians:

data Angle a = Radians a
             | Degrees a
             deriving (Eq, Show)

Then I decided I wanted to create a (gratuitous) type class to abstract out the radian/degree conversions:

class Angular a where
  rad :: a -> a
  rad x = pi * (deg x) / 180
  deg :: a -> a
  deg x = 180 * (rad x) / pi


Then tried to make Angle an instance of Angular (such as Float in an instance of Floating):

instance Angular (Angle a) where
    rad (Radians x) = x
    deg (Degrees x) = x 


I've removed all the context clauses, rather than pick one of many equally broken implementations to share, because that seems to be a large part of where my problems are.

Is this just an issue of syntax, or am I doing something fundamentally wrong?

Thanks--
 Greg

-------------- next part --------------
Skipped content of type multipart/related


More information about the Beginners mailing list