Skip to content

Instantly share code, notes, and snippets.

@ion1
Created July 10, 2012 09:58
Show Gist options
  • Save ion1/3082411 to your computer and use it in GitHub Desktop.
Save ion1/3082411 to your computer and use it in GitHub Desktop.
Typeclass desugaring

The class

class Num a where
  (+) :: a -> a -> a
  negate :: a -> a

desugars to something like

data Num a = Num { (+) :: a -> a -> a
                 , negate :: a -> a
                 }

The instance

instance Num Integer where
  (+) = integerPlus
  negate = integerNegate

desugars to something like

numInteger :: Num Integer
numInteger = Num { (+) = integerPlus
                 , negate = integerNegate
                 }

The function

inc :: Num a => a -> a
inc n = (+) n 1

desugars to something like

inc :: Num a -> a -> a
inc num n = (+) num n 1

The action

main = print (inc (42 :: Integer) == 1 + 42)

desugars to something like

main = print (inc numInteger 42 == (+) numInteger 1 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment