Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created May 4, 2010 12:36
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 sjoerdvisscher/389359 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/389359 to your computer and use it in GitHub Desktop.
{-# LANGUAGE NoMonomorphismRestriction, TypeSynonymInstances, FlexibleInstances, MultiParamTypeClasses #-}
class ExpSYM repr where
lit :: Int -> repr
neg :: repr -> repr
add :: repr -> repr -> repr
class Ann repr e where
a :: e -> repr -> repr
instance ExpSYM Int where
lit n = n
neg e = - e
add e1 e2 = e1 + e2
instance Ann Int e where
a e = id
instance ExpSYM String where
lit n = show n
neg e = "(-" ++ e ++ ")"
add e1 e2 = "(" ++ e1 ++ " + " ++ e2 ++ ")"
instance Show a => Ann String a where
a e s = "@" ++ show e ++ " " ++ s
data P = P Int Int
instance Show P where show (P x y) = "(" ++ show x ++ "," ++ show y ++ ")"
tf1 = add (lit 8) (neg (add (lit 1) (lit 2)))
tf2 = a (P 1 1) $ add (a (P 2 1) $ lit 8) (a (P 3 1) $ neg (a (P 4 1) $ add (a (P 5 1) $ lit 1) (a (P 6 1) $ lit 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment