-
-
Save bananu7/233e3aae38e20afe75a1 to your computer and use it in GitHub Desktop.
This file contains 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 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