Skip to content

Instantly share code, notes, and snippets.

@bananu7
Last active October 28, 2015 00:35
Show Gist options
  • Save bananu7/233e3aae38e20afe75a1 to your computer and use it in GitHub Desktop.
Save bananu7/233e3aae38e20afe75a1 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Map as Map
import Control.Monad.Trans.State.Lazy
data Val = I Int | S String | B Bool deriving (Eq, Show)
type Table = Map.Map Val Val
-- m is the "root monad"
data ClosureM m a = RootClosure (StateT Table m a)
| NestedClosure (StateT Table (ClosureM m) a)
runClosure :: Table -> ClosureM m a -> m (a, Table)
--runClosure t (NestedClosure s) = runStateT s t
runClosure t (RootClosure s) = runStateT s t
instance (Functor m) => Functor (ClosureM m) where
fmap f (RootClosure a) = RootClosure $ fmap f a
fmap f (NestedClosure a) = NestedClosure $ fmap f a
instance (Monad m) => Monad (ClosureM m) where
return x = RootClosure . state $ \s -> (x,s)
(NestedClosure a) >>= b = NestedClosure . StateT $ \s -> do
(a', s') <- runStateT a s
runClosure s' (b a')
{-
closureGet :: (Monad m) => Val -> ClosureM m Val
closureGet k = ClosureM $ do
vals <- get
case lookup k vals of
Just v -> return v
Nothing -> lift $ closureGet
-}
main :: IO ()
main = print "ok"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment