Skip to content

Instantly share code, notes, and snippets.

@blitzcode
Created July 4, 2014 14:01
Show Gist options
  • Save blitzcode/f8266b129500c511e2f6 to your computer and use it in GitHub Desktop.
Save blitzcode/f8266b129500c511e2f6 to your computer and use it in GitHub Desktop.
GADTs
data Exp a where
Lit :: Int -> Exp Int
Add :: Exp Int -> Exp Int -> Exp Int
Mul :: Exp Int -> Exp Int -> Exp Int
Cmp :: Eq a => Exp a -> Exp a -> Exp Bool
If :: Exp Bool -> Exp a -> Exp a -> Exp a
deriving instance Show (Exp a)
eval :: Exp a -> a
eval (Lit a) = a
eval (Add a b) = eval a + eval b
eval (Mul a b) = eval a * eval b
eval (Cmp a b) = eval a == eval b
eval (If cnd yes no) = eval $ if (eval cnd) then yes else no
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment