Created
December 5, 2015 23:31
-
-
Save BekaValentine/a48c3ba0b82d1ff78ed9 to your computer and use it in GitHub Desktop.
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
| data Term | |
| = Lit Int | |
| | Pair Term Term | |
| | Fst Term | |
| | Snd Term | |
| | Lam (Term -> Term) | |
| | App Term Term | |
| instance Show Term where | |
| show (Lit i) = show i | |
| show (Pair x y) = "pair(" ++ show x ++ ";" ++ show y ++ ")" | |
| show (Fst p) = "fst(" ++ show p ++ ")" | |
| show (Snd p) = "snd(" ++ show p ++ ")" | |
| show (Lam b) = "lam(...)" | |
| show (App f x) = "app(" ++ show f ++ ";" ++ show x ++ ")" | |
| data Frame | |
| = InFst | |
| | InSnd | |
| | InAppFun Term | |
| type Stack = [Frame] | |
| (>>>) :: Stack -> Term -> Term | |
| s >>> Lit i = s <<< Lit i | |
| s >>> Pair x y = s <<< Pair x y | |
| s >>> Fst p = (InFst : s) >>> p | |
| s >>> Snd p = (InSnd : s) >>> p | |
| s >>> Lam b = s <<< Lam b | |
| s >>> (App f x) = (InAppFun x : s) >>> f | |
| (<<<) :: Stack -> Term -> Term | |
| [] <<< t = t | |
| (InFst : s) <<< Pair x _ = s >>> x | |
| (InSnd : s) <<< Pair _ y = s >>> y | |
| (InAppFun x : s) <<< Lam b = s >>> b x | |
| eval :: Term -> Term | |
| eval t = [] >>> t | |
| main :: IO () | |
| main = do print (eval ex0) | |
| print (eval ex1) | |
| print (eval ex2) | |
| where | |
| ex0 = Fst (Pair (Lit 0) (Lit 1)) | |
| -- fst(pair(0;1)) = 0 | |
| ex1 = App (Lam $ \x -> x) (Lit 2) | |
| -- app(lam(x.x) ; 2) = 2 | |
| ex2 = App (Lam $ \p -> Fst p) (Pair (Lit 0) (Lit 1)) | |
| -- app(lam(p.fst(p)) ; pair(0;1)) | |
| -- = fst(pair(0;1)) | |
| -- = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment