2 module Vectorise.Monad.Local (
15 import Vectorise.Monad.Base
26 -- Local Environment ----------------------------------------------------------
27 -- | Project something from the local environment.
28 readLEnv :: (LocalEnv -> a) -> VM a
29 readLEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f lenv))
32 -- | Set the local environment.
33 setLEnv :: LocalEnv -> VM ()
34 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
37 -- | Update the enviroment using the provided function.
38 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
39 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
42 -- | Perform a computation in its own local environment.
43 -- This does not alter the environment of the current state.
44 localV :: VM a -> VM a
46 = do env <- readLEnv id
52 -- | Perform a computation in an empty local environment.
53 closedV :: VM a -> VM a
55 = do env <- readLEnv id
56 setLEnv (emptyLocalEnv { local_bind_name = local_bind_name env })
62 -- | Get the name of the local binding currently being vectorised.
63 getBindName :: VM FastString
64 getBindName = readLEnv local_bind_name
67 -- | Run a vectorisation computation in a local environment,
68 -- with this id set as the current binding.
69 inBind :: Id -> VM a -> VM a
71 = do updLEnv $ \env -> env { local_bind_name = occNameFS (getOccName id) }
75 -- | Lookup a PA tyvars from the local environment.
76 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
78 = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
81 -- | Add a tyvar to the local environment.
82 defLocalTyVar :: TyVar -> VM ()
83 defLocalTyVar tv = updLEnv $ \env ->
84 env { local_tyvars = tv : local_tyvars env
85 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
88 -- | Add mapping between a tyvar and pa dictionary to the local environment.
89 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
90 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
91 env { local_tyvars = tv : local_tyvars env
92 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
96 -- | Get the set of tyvars from the local environment.
97 localTyVars :: VM [TyVar]
98 localTyVars = readLEnv (reverse . local_tyvars)