{-# LANGUAGE Safe #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
module Cryptol.Eval.Env where
import Cryptol.Eval.Backend
import Cryptol.Eval.Monad( PPOpts )
import Cryptol.Eval.Type
import Cryptol.Eval.Value
import Cryptol.ModuleSystem.Name
import Cryptol.TypeCheck.AST
import Cryptol.TypeCheck.Solver.InfNat
import Cryptol.Utils.PP
import qualified Data.Map.Strict as Map
import Data.Semigroup
import GHC.Generics (Generic)
import Prelude ()
import Prelude.Compat
data GenEvalEnv sym = EvalEnv
{ envVars :: !(Map.Map Name (SEval sym (GenValue sym)))
, envTypes :: !TypeEnv
} deriving Generic
instance Semigroup (GenEvalEnv sym) where
l <> r = EvalEnv
{ envVars = Map.union (envVars l) (envVars r)
, envTypes = Map.union (envTypes l) (envTypes r)
}
instance Monoid (GenEvalEnv sym) where
mempty = EvalEnv
{ envVars = Map.empty
, envTypes = Map.empty
}
mappend l r = l <> r
ppEnv :: Backend sym => sym -> PPOpts -> GenEvalEnv sym -> SEval sym Doc
ppEnv sym opts env = brackets . fsep <$> mapM bind (Map.toList (envVars env))
where
bind (k,v) = do vdoc <- ppValue sym opts =<< v
return (pp k <+> text "->" <+> vdoc)
emptyEnv :: GenEvalEnv sym
emptyEnv = mempty
bindVar ::
Backend sym =>
sym ->
Name ->
SEval sym (GenValue sym) ->
GenEvalEnv sym ->
SEval sym (GenEvalEnv sym)
bindVar sym n val env = do
let nm = show $ ppLocName n
val' <- sDelay sym (Just nm) val
return $ env{ envVars = Map.insert n val' (envVars env) }
bindVarDirect ::
Backend sym =>
Name ->
GenValue sym ->
GenEvalEnv sym ->
GenEvalEnv sym
bindVarDirect n val env = do
env{ envVars = Map.insert n (pure val) (envVars env) }
{-# INLINE lookupVar #-}
lookupVar :: Name -> GenEvalEnv sym -> Maybe (SEval sym (GenValue sym))
lookupVar n env = Map.lookup n (envVars env)
{-# INLINE bindType #-}
bindType :: TVar -> Either Nat' TValue -> GenEvalEnv sym -> GenEvalEnv sym
bindType p ty env = env { envTypes = Map.insert p ty (envTypes env) }
{-# INLINE lookupType #-}
lookupType :: TVar -> GenEvalEnv sym -> Maybe (Either Nat' TValue)
lookupType p env = Map.lookup p (envTypes env)