Created
July 22, 2018 17:52
-
-
Save ekmett/f081b5e36bac3fed1ea6b21eb25327c6 to your computer and use it in GitHub Desktop.
Simple CEK Machine
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 StrictData #-} | |
module CEK where | |
-- C -- Control | |
-- E -- Environment | |
-- (S) -- Store | |
-- K -- Continuation | |
data Exp | |
= Var String | |
| Lam String Exp | |
| Ap Exp Exp | |
deriving Show | |
newtype Env = Env { (!) :: String -> Value } | |
instance Show Env where | |
show _ = "Env" | |
data Value = Closure String Exp Env | |
data Kont | |
= Top | |
| Arg Exp Env Kont | |
| Fun String Exp Env Kont | |
deriving Show | |
data State = State Exp Env Kont | |
deriving Show | |
start :: Exp -> State | |
start c = State c (Env $ const undefined) Top | |
id_ = Lam "x" $ Var "x" | |
const_ = Lam "x" $ Lam "y" $ Var "x" | |
-- small-step semantics step | |
step :: State -> State | |
step s@(State c e k) = case c of | |
Var v -> case e ! v of | |
Closure v' b e' -> State (Lam v' b) e' k | |
Ap cf cx -> State cf e (Arg cx e k) | |
Lam v b -> case k of | |
Top -> s | |
Arg cx e' k' -> State cx e' (Fun v b e k') | |
Fun v' b' e' k' -> State b' (extend v' (Closure v b e) e') k' | |
extend :: String -> Value -> Env -> Env | |
extend i v f = Env $ \j -> if i == j then v else f ! j | |
final :: State -> Bool | |
final (State Lam{} _ Top) = True | |
final _ = False | |
-- until :: (a -> Bool) -> (a -> a) -> a -> a | |
eval :: State -> State | |
eval = until final step |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Ed,
Thanks for this. Could you please add the following notes for people looking at how you tested it? (And add anything I've missed).
Cheers
Julian