Last active
August 29, 2015 14:08
-
-
Save Decoherence/c5d5f55c4abf1e939246 to your computer and use it in GitHub Desktop.
Haskell: Generalized Algebraic Data Types (GADTs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# Language GADTs #-} | |
data Term a where | |
Lit :: a -> Term a | |
Succ :: Term Int -> Term Int | |
IsZero :: Term Int -> Term Bool | |
If :: Term Bool -> Term a -> Term a -> Term a | |
IsNeg :: Term Int -> Term Bool | |
eval :: Term a -> a | |
eval (Lit i) = i -- Term a | |
eval (Succ t) = 1 + eval t -- Term (a ~ Int) | |
eval (IsZero i) = eval i == 0 -- Term (a ~ Int) | |
eval (If b e1 e2) = if eval b then eval e1 else eval e2 -- Term (a ~ Bool) | |
eval (IsNeg t) = eval t < 0 | |
example :: Int | |
example = eval (Succ (Succ (Lit 3))) -- 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment