Skip to content

replace CryptolEnvStack with scopes within CryptolEnv#3242

Open
danmatichuk wants to merge 10 commits into
masterfrom
cryptol-scopes
Open

replace CryptolEnvStack with scopes within CryptolEnv#3242
danmatichuk wants to merge 10 commits into
masterfrom
cryptol-scopes

Conversation

@danmatichuk

@danmatichuk danmatichuk commented May 11, 2026

Copy link
Copy Markdown
Contributor

fixes #3167 and #2860

splits up the CryptolEnv datatype into GlobalCryptolEnv for globally-tracked Cryptol translation data, and CryptolEnv for data that's only relevant for scoping "extra" names (i.e. injected SAWScript values) needed for parsing Cryptol.

The GlobalCryptolEnv is stored as global metadata in the SharedContext, and only accessible indirectly via the functions in CryptolSAWCore.GlobalCryptolEnv. In the majority of cases, functions with the shape SharedContext -> CryptolEnv -> a -> IO (b, CryptolEnv) have been rewritten into SharedContext -> a -> IO b.

The previous CryptolEnvStack has been replaced by having CryptolEnv itself provide push/pop scope functions. Importantly, the data from GlobalCryptolEnv that was previously part of this stack is now unscoped. This is internally consistent because the names in the global environment are globally-unique (generated with unique nonces from the Cryptol module environment).

Although this reworks a large chunk of the Cryptol environment code, this change shouldn't affect any currently-working SAW scripts, but should resolve issues with SAWScript scopes capturing stale Cryptol environments.

@danmatichuk danmatichuk requested a review from sauclovian-g May 11, 2026 23:45
@danmatichuk danmatichuk force-pushed the cryptol-scopes branch 4 times, most recently from e1bde62 to 45e96ed Compare May 15, 2026 18:34
@sauclovian-g sauclovian-g changed the title replace CrytolEnvStack with scopes within CryptolEnv replace CryptolEnvStack with scopes within CryptolEnv May 15, 2026
@danmatichuk danmatichuk force-pushed the cryptol-scopes branch 2 times, most recently from dbf2a17 to dfc303c Compare May 22, 2026 23:27
@danmatichuk danmatichuk requested a review from brianhuffman June 1, 2026 21:00

@sauclovian-g sauclovian-g left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments from a first run, mostly cosmetic stuff.

The basic approach seems sound.

Comment on lines +109 to +111
-- This is intended to be a write-once record of any work done
-- during translation, analagous to the 'SharedContext' from
--- SAWCore. Rather than directly accessing this environment,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- This is intended to be a write-once record of any work done
-- during translation, analagous to the 'SharedContext' from
--- SAWCore. Rather than directly accessing this environment,
--
-- This is intended to be a write-once record of any work done
-- during translation, analogous to the 'SharedContext' from
-- SAWCore. Rather than directly accessing this environment,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and similarly below and elsewhere, please put empty lines between comment paragraphs and include the initial -- in them so it's all one big comment (I'm not sure offhand if Haddock will collect or ignore following comment fragments, but I think it will probably ignore them and that's not what we want...))

-- includes a scoped naming environment via 'CryptolScope'.


--

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to keep the rest of this? Hopefully it's now out of date

(also some of it, like "There is now one environment type", is now wrong)

}

-- | Initialize the global environment with the given 'ME.ModuleEnv',
-- and populate the 'geAllVars' accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- and populate the 'geAllVars' accordingly.
-- and populate the 'geAllVars' field accordingly.

Comment on lines +188 to +189
let newMEnv = geModuleEnv chk
chkMEnv = geModuleEnv now

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names seem backwards...?

let env1 = pushScope env0
(a, env2) <- f env1
unless (sameHeight env1 env2) $
fail "withFreshScope: mismatched push/pops"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a panic. There seems to be only one user, that doesn't abuse it.

Comment on lines +692 to +693
let _cryenv = cc ^. jccCryptolEnv
ety_tm <- liftIO $ Cryptol.translateType sc ety

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let _cryenv = cc ^. jccCryptolEnv
ety_tm <- liftIO $ Cryptol.translateType sc ety
ety_tm <- liftIO $ Cryptol.translateType sc ety

-- NOTE: implementation of loadCryptolModule, now uses this default:
-- defaultPrimitiveOptions = ImportPrimitiveOptions{allowUnknownPrimitives=True}
import_env <- refreshCryptolEnv env
let import_env = env

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this? The only thing that uses it downstream doesn't need it.

CryptolModule ->
IO (Either TranslationError [Rocq.Decl])
translateCryptolModule sc env configuration globalDecls (CryptolModule _ tm) =
translateCryptolModule sc _env configuration globalDecls (CryptolModule _ tm) =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this one? It only has the one call site

primEnviron :: Options -> BuiltinContext -> CryptolEnvStack -> Environ
primEnviron opts bic cryenvs =
primEnviron :: Options -> BuiltinContext -> CEnv.CryptolEnv -> Environ
primEnviron opts bic env =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
primEnviron opts bic env =
primEnviron opts bic cenv =

varenv = ScopedMap.push $ ScopedMap.seed $ primValueEnv opts bic
in
Environ varenv tyenv cryenvs
Environ varenv tyenv env

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Environ varenv tyenv env
Environ varenv tyenv cenv

There are too many things just "env" could be...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash passing Cryptol module handles to SAWScript functions

2 participants