Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
Created August 29, 2012 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanHowell/f8e5d1ecf20ea09a8b36 to your computer and use it in GitHub Desktop.
Save NathanHowell/f8e5d1ecf20ea09a8b36 to your computer and use it in GitHub Desktop.
import Control.Monad.State
import Control.Applicative
data Foo = Foo0
| Foo1 Char
| Foo2 Bool Char
| Foo3 Char Bool Char
deriving Show
type Env a = State (String, [Bool]) a
class InEnv a where
envGet :: Int -> Env a
instance InEnv Char where
envGet i = do
(s, _) <- get
return $! s !! i
instance InEnv Bool where
envGet i = do
(_, b) <- get
return $! b !! i
cid :: Env Char
cid = envGet 1
bid :: Env Bool
bid = envGet 2
env :: (String, [Bool])
env = ("xy", map (==1) [0,0,1])
runEnv :: Env a -> a
runEnv f = evalState f env
test0 :: Foo
test0 = Foo0
test1 :: Foo
test1 = runEnv $ Foo1 <$> cid
test2 :: Foo
test2 = runEnv $ Foo2 <$> bid <*> cid
test3 :: Foo
test3 = runEnv $ Foo3 <$> cid <*> bid <*> cid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment