Created
April 14, 2015 21:00
-
-
Save cqfd/50d294ae4fb1cdda8ad6 to your computer and use it in GitHub Desktop.
Phantom type evaluator example
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
module Phantom ( | |
ei, eb, plus, iff, -- Smart constructors | |
eval -- Evaluation | |
) where | |
data Expr a = EI Int | |
| EB Bool | |
| Plus (Expr Int) (Expr Int) | |
| If (Expr Bool) (Expr a) (Expr a) | |
deriving Show | |
ei :: Int -> Expr Int | |
ei i = EI i | |
eb :: Bool -> Expr Bool | |
eb b = EB b | |
plus :: Expr Int -> Expr Int -> Expr Int | |
plus l r = Plus l r | |
iff :: Expr Bool -> Expr a -> Expr a -> Expr a | |
iff p t e = If p t e | |
data Val = VI Int | |
| VB Bool | |
deriving Show | |
example = iff (eb True) (ei 1) (ei 2) | |
eval :: Expr a -> Val | |
eval (EI i) = VI i | |
eval (EB b) = VB b | |
eval (Plus l r) = | |
let (VI i) = eval l | |
(VI j) = eval r | |
in VI (i + j) | |
eval (If p t e) = | |
let (VB b) = eval p | |
in if b then eval t else eval e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment