-
-
Save NathanHowell/f8e5d1ecf20ea09a8b36 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
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