@@ -755,26 +755,28 @@ type VarEnv = ScopedMap SS.Name (SS.Pos, SS.PrimitiveLifecycle,
755755-- builtin types that aren't special cases in the AST appear)
756756type TyEnv = ScopedMap SS. Name (SS. PrimitiveLifecycle , SS. NamedType )
757757
758+ -- | The Cryptol environment. We maintain a stack of Cryptol
759+ -- environments and push/pop them as we enter and leave scopes;
760+ -- otherwise the Cryptol environment doesn't track SAWScript scopes
761+ -- and horribly confusing wrong things happen.
762+ newtype CryptolScopeStack = CryptolScopeStack (NonEmpty CEnv. CryptolEnv )
763+
758764-- | Type for the ordinary interpreter environment.
759765--
760766-- There's one environment that maps variable names to values, and
761- -- one that maps type names to types. Both get closed in with
762- -- lambdas and do-blocks at the appropriate times.
767+ -- one that maps type names to types. A third handles the Cryptol
768+ -- domain. All three get closed in with lambdas and do-blocks at the
769+ -- appropriate times.
763770--
764771-- Note that rebindable variables are sold separately. This is so
765772-- they don't get closed in; references to rebindable variables
766773-- always retrieve the most recent version.
767774data Environ = Environ {
768775 eVarEnv :: VarEnv ,
769- eTyEnv :: TyEnv
776+ eTyEnv :: TyEnv ,
777+ eCryptol :: CryptolScopeStack
770778}
771779
772- -- | The Cryptol environment. We maintain a stack of Cryptol
773- -- environments and push/pop them as we enter and leave scopes;
774- -- otherwise the Cryptol environment doesn't track SAWScript scopes
775- -- and horribly confusing wrong things happen.
776- newtype CryptolScopeStack = CryptolScopeStack (NonEmpty CEnv. CryptolEnv )
777-
778780-- | The extra environment for rebindable globals.
779781--
780782-- Note: because no builtins are rebindable, there are no lifecycle
@@ -785,28 +787,27 @@ type RebindableEnv = Map SS.Name (SS.Pos, SS.Schema, Value)
785787-- | Enter a scope.
786788pushScope :: TopLevel ()
787789pushScope = do
788- Environ varenv tyenv <- gets rwEnviron
789- cryenv <- gets rwCryptol
790+ Environ varenv tyenv cryenv <- gets rwEnviron
790791 let varenv' = ScopedMap. push varenv
791792 tyenv' = ScopedMap. push tyenv
792793 cryenv' = cryptolPush cryenv
793- modifyTopLevelRW (\ rw -> rw { rwEnviron = Environ varenv' tyenv', rwCryptol = cryenv' })
794+ modifyTopLevelRW (\ rw -> rw { rwEnviron = Environ varenv' tyenv' cryenv' })
794795
795796-- | Leave a scope. This will panic if you try to leave the last scope;
796797-- pushes and pops should be matched.
797798popScope :: TopLevel ()
798799popScope = do
799- Environ varenv tyenv <- gets rwEnviron
800- cryenv <- gets rwCryptol
800+ Environ varenv tyenv cryenv <- gets rwEnviron
801801 let varenv' = ScopedMap. pop varenv
802802 tyenv' = ScopedMap. pop tyenv
803803 cryenv' = cryptolPop cryenv
804- modifyTopLevelRW (\ rw -> rw { rwEnviron = Environ varenv' tyenv', rwCryptol = cryenv' })
804+ modifyTopLevelRW (\ rw -> rw { rwEnviron = Environ varenv' tyenv' cryenv' })
805805
806806
807807getCryptolEnv :: TopLevel CEnv. CryptolEnv
808808getCryptolEnv = do
809- CryptolScopeStack (e :| _) <- gets rwCryptol
809+ Environ _varenv _tyenv cryenv <- gets rwEnviron
810+ let CryptolScopeStack (e :| _) = cryenv
810811 return e
811812
812813cryptolPush :: CryptolScopeStack -> CryptolScopeStack
@@ -858,9 +859,6 @@ data TopLevelRW =
858859 rwEnviron :: Environ
859860 , rwRebindables :: RebindableEnv
860861
861- -- | The Cryptol naming environment.
862- , rwCryptol :: CryptolScopeStack
863-
864862 -- | The current execution position. This is only valid when the
865863 -- interpreter is calling out into saw-central to execute a
866864 -- builtin. Within the interpreter, the current position is
@@ -1177,7 +1175,7 @@ extendEnv pos name rb ty doc v = do
11771175 modname = T. packModName [name]
11781176
11791177 -- Update the SAWScript environment.
1180- Environ varenv tyenv <- gets rwEnviron
1178+ Environ varenv tyenv cryenv <- gets rwEnviron
11811179 rbenv <- gets rwRebindables
11821180 let (varenv', rbenv') = case rb of
11831181 SS. ReadOnlyVar ->
@@ -1196,7 +1194,7 @@ extendEnv pos name rb ty doc v = do
11961194 (varenv, re')
11971195
11981196 -- Mirror the value into the Cryptol environment if appropriate.
1199- CryptolScopeStack (ce :| ces) <- gets rwCryptol
1197+ let CryptolScopeStack (ce :| ces) = cryenv
12001198 ce' <-
12011199 case v of
12021200 VTerm t ->
@@ -1215,19 +1213,19 @@ extendEnv pos name rb ty doc v = do
12151213 pure $ CEnv. bindTypedTerm (ident, tt) ce
12161214 _ ->
12171215 pure ce
1216+ let cryenv' = CryptolScopeStack (ce' :| ces)
12181217
12191218 -- Drop the new bits into place.
12201219 modify (\ rw -> rw {
1221- rwCryptol = CryptolScopeStack (ce' :| ces),
1222- rwEnviron = Environ varenv' tyenv,
1220+ rwEnviron = Environ varenv' tyenv cryenv',
12231221 rwRebindables = rbenv'
12241222 })
12251223
12261224extendEnvMulti :: [(SS. Pos , SS. Name , SS. Rebindable , SS. Schema , Maybe [Text ], Environ -> Value )] -> TopLevel ()
12271225extendEnvMulti bindings = do
12281226
12291227 -- Update the SAWScript environment.
1230- Environ varenv tyenv <- gets rwEnviron
1228+ Environ varenv tyenv cryenv <- gets rwEnviron
12311229
12321230 -- Insert all the bindings at once, and feed the final resulting
12331231 -- interpreter environment into each value. This circular
@@ -1260,7 +1258,7 @@ extendEnvMulti bindings = do
12601258 in
12611259 ScopedMap. insert name (pos, SS. Current , ty, v'', doc) tmpenv
12621260 varenv' = foldr insert varenv bindings
1263- environ' = Environ varenv' tyenv
1261+ environ' = Environ varenv' tyenv cryenv
12641262
12651263 -- Drop the new bits into place.
12661264 modify (\ rw -> rw { rwEnviron = environ' })
@@ -1270,10 +1268,10 @@ extendEnvMulti bindings = do
12701268addTypedef :: SS. Name -> SS. Type -> TopLevel ()
12711269addTypedef name ty = do
12721270 avail <- gets rwPrimsAvail
1273- Environ varenv tyenv <- gets rwEnviron
1271+ Environ varenv tyenv cryenv <- gets rwEnviron
12741272 let ty' = SS. substituteTyVars avail tyenv ty
12751273 tyenv' = ScopedMap. insert name (SS. Current , SS. ConcreteType ty') tyenv
1276- modify (\ rw -> rw { rwEnviron = Environ varenv tyenv' })
1274+ modify (\ rw -> rw { rwEnviron = Environ varenv tyenv' cryenv })
12771275
12781276typedTermOfString :: SharedContext -> String -> IO TypedTerm
12791277typedTermOfString sc str =
0 commit comments