secret

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.