Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created June 16, 2014 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/49bd8564404b56e794b0 to your computer and use it in GitHub Desktop.
Save patrickt/49bd8564404b56e794b0 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
type CGFloat = Double
data CGPoint = CGPoint { x :: CGFloat, y :: CGFloat } deriving (Eq, Show)
instance Num CGPoint where
(CGPoint x y) + (CGPoint x' y') = CGPoint { x = x+x', y = y+y' }
(CGPoint x y) * (CGPoint x' y') = CGPoint { x = x*x', y = y*y' }
(CGPoint x y) - (CGPoint x' y') = CGPoint { x = x-x', y = y-y' }
abs (CGPoint x y) = CGPoint { x = abs x, y = abs y}
signum = undefined
fromInteger = undefined
class (Show a, Num a) => IsPoint a where
zero :: a
distance :: a -> a -> CGFloat
instance IsPoint CGPoint where
zero = CGPoint 0 0
distance = undefined -- blah blah blah
newtype NaturalPoint = NaturalPoint CGPoint
deriving (Show, Eq, Num, IsPoint) -- we inherit the definitions of equality, numeric operations, and those defined in the IsPoint class
newtype UnscaledPoint = UnscaledPoint CGPoint
deriving (Show, Eq, Num, IsPoint)
newtype ScaledPoint = ScaledPoint CGPoint
deriving (Show, Eq, Num, IsPoint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment