compilation-0.0.0.2: Control/Compilation/Environment.hs
----------------------------------------------------------------
--
-- | Compilation
-- Monad and combinators for quickly assembling simple
-- compilers.
--
-- @Control\/Compilation\/Environment.hs@
--
-- State extension class and combinators for implementations
-- of a state that support an environment (i.e., lookup table
-- or dictionary) data structure or structures.
--
----------------------------------------------------------------
--
{-# LANGUAGE MultiParamTypeClasses, ScopedTypeVariables #-}
module Control.Compilation.Environment
where
import Control.Compilation
----------------------------------------------------------------
-- | Type synonyms.
type Env a = [(String, a)]
----------------------------------------------------------------
-- | State extension class definition.
class StateExtension a => Environment a b where
project :: a -> Env b
inject :: Env b -> a -> a
addEnv :: String -> b -> Compilation a ()
addEnv v x =
do s :: a <- get
env :: Env b <- return $ project s
set $ inject ((v,x):env) s
popEnv :: Compilation a ()
popEnv =
do s :: a <- get
env :: Env b <- return $ project s
set $ inject (tail env) s
dropEnv :: Int -> Compilation a ()
dropEnv n =
do s :: a <- get
env :: Env b <- return $ project s
set $ inject (drop n env) s
lookupEnv :: String -> Compilation a (Maybe b)
lookupEnv v =
do s :: a <- get
env :: Env b <- return $ project s
return $ lookup v env
setEnv :: Env b -> Compilation a ()
setEnv env =
do s :: a <- get
set $ inject env s
getEnv :: Compilation a (Env b)
getEnv =
do s :: a <- get
env :: Env b <- return $ project s
return $ env
--eof