Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. {-# Language StrictData #-}
  2. {-# Language GADTs #-}
  3. {-# Language DeriveTraversable #-}
  4. {-# Language LambdaCase #-}
  5. module CEK where
  6.  
  7. import Control.Monad (ap)
  8. import Data.Maybe
  9. import Data.Void
  10.  
  11. -- C -- Control
  12. -- E -- Environment
  13. -- (S) -- Store
  14. -- K -- Continuation
  15.  
  16. data Exp a
  17. = Var a
  18. | Lam (Exp (Maybe a))
  19. | Ap (Exp a) (Exp a)
  20. deriving (Show, Functor, Foldable, Traversable)
  21.  
  22. instance Applicative Exp where
  23. pure = Var
  24. (<*>) = ap
  25.  
  26. instance Monad Exp where
  27. return = Var
  28. Var a >>= f = f a
  29. Ap l r >>= f = Ap (l >>= f) (r >>= f)
  30. Lam b >>= f = Lam $ b >>= \case
  31. Nothing -> Var Nothing
  32. Just a -> Just <$> f a
  33.  
  34. abstract :: (Functor f, Eq a) => a -> f a -> f (Maybe a)
  35. abstract a = fmap go where
  36. go b
  37. | a == b = Nothing
  38. | otherwise = Just b
  39.  
  40. lam :: Eq a => a -> Exp a -> Exp a
  41. lam a b = Lam (abstract a b)
  42.  
  43. closed :: Exp a -> Exp b
  44. closed = fromJust . traverse (const Nothing)
  45.  
  46. newtype Env a = Env { (!) :: a -> Value }
  47. -- instance Contravariant Env
  48.  
  49. instance Show (Env a) where
  50. show _ = "Env"
  51.  
  52. data Value where
  53. Closure :: Show a => Exp (Maybe a) -> Env a -> Value
  54.  
  55. data Kont where
  56. Top :: Kont
  57. Arg :: Show a => Exp a -> Env a -> Kont -> Kont
  58. Fun :: Show a => Exp (Maybe a) -> Env a -> Kont -> Kont
  59.  
  60. instance Show Kont where
  61. showsPrec d Top = showString "Top"
  62. showsPrec d (Arg c e k) = showParen (d > 10) $
  63. showString "Arg " . showsPrec 11 c . showChar ' ' . showsPrec 11 e . showChar ' ' . showsPrec 11 k
  64. showsPrec d (Fun b e k) = showParen (d > 10) $
  65. showString "Fun " . showsPrec 11 b . showChar ' ' . showsPrec 11 e . showChar ' ' . showsPrec 11 k
  66.  
  67. data State where
  68. State :: Show a => Exp a -> Env a -> Kont -> State
  69.  
  70. instance Show State where
  71. showsPrec d (State c e k) = showParen (d > 10) $
  72. showString "State " . showsPrec 11 c . showChar ' ' . showsPrec 11 e . showChar ' ' . showsPrec 11 k
  73.  
  74. start :: Exp Void -> State
  75. start c = State c (Env absurd) Top
  76.  
  77. id_ :: Exp Void
  78. id_ = closed $ lam "x" $ Var "x"
  79.  
  80. const_ :: Exp Void
  81. const_ = closed $ lam "x" $ lam "y" $ Var "x"
  82.  
  83. -- small-step semantics step
  84. step :: State -> State
  85. step s@(State c e k) = case c of
  86. Var v -> case e ! v of
  87. Closure b e' -> State (Lam b) e' k
  88. Ap cf cx -> State cf e (Arg cx e k)
  89. Lam b -> case k of
  90. Top -> s
  91. Arg cx e' k' -> State cx e' (Fun b e k')
  92. Fun b' e' k' -> State b' (extend (Closure b e) e') k'
  93.  
  94. extend :: Value -> Env a -> Env (Maybe a)
  95. extend v (Env f) = Env $ maybe v f
  96.  
  97. final :: State -> Bool
  98. final (State Lam{} _ Top) = True
  99. final _ = False
  100.  
  101. -- until :: (a -> Bool) -> (a -> a) -> a -> a
  102.  
  103. eval :: State -> State
  104. eval = until final step
  105.  
  106. -- big-step semantics
  107.  
  108. big :: Show a => Exp a -> Env a -> Kont -> State
  109. big c e k = case c of
  110. Var v -> case e ! v of
  111. Closure b e' -> big (Lam b) e' k
  112. Ap cf cx -> big cf e (Arg cx e k)
  113. Lam b -> case k of
  114. Top -> State c e k
  115. Arg cx e' k' -> big cx e' (Fun b e k')
  116. Fun b' e' k' -> big b' (extend (Closure b e) e') k'
Add Comment
Please, Sign In to add comment