Skip to content

fix(env): do not share the normalisation cache with earlier environments - #1247

Open
strub wants to merge 1 commit into
mainfrom
fix-norm-cache
Open

strub wants to merge 1 commit into
mainfrom
fix-norm-cache

Conversation

@strub

@strub strub commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Fixes #1246.

NormMp.norm_mpath, norm_xfun and norm_xpv memoise their results
in env.env_norm, which is a ref in the otherwise persistent env.
An environment derived from another one shared this ref unless the
operation that derived it copied it, which only Mod.bind and
Mod.bind_local did. What was cached in the new environment, about
paths that it binds, then also reached the environment it came from.

When that environment is used again, a module defined later at the
same path is normalised as the old one. In the example of the issue,
the procedure M.q of the new M is normalised to N1.f, the
target of the old M.q, which is enough to prove false. This
happens:

  • after a command that fails once the body of a module has been
    typed, or at the end of a section that defines a local module: the
    body is typed in an environment made by enter;
  • after pragma reset or an undo, which go back to an environment
    that shares the cache with the one after end T or require T.

Entering a scope, and binding a theory, a module or a theory alias,
now give the new environment its own copy of the cache
(fork_norm), and so does EcEnv.copy. The other bindings
(operators, lemmas, ...) do not bring module paths and still share
it, so that the entries computed in proofs are kept.

Fixes #1246.

`NormMp.norm_mpath`, `norm_xfun` and `norm_xpv` memoise their results
in `env.env_norm`, which is a `ref` in the otherwise persistent `env`.
An environment derived from another one shared this `ref` unless the
operation that derived it copied it, which only `Mod.bind` and
`Mod.bind_local` did. What was cached in the new environment, about
paths that it binds, then also reached the environment it came from.

When that environment is used again, a module defined later at the
same path is normalised as the old one. In the example of the issue,
the procedure `M.q` of the new `M` is normalised to `N1.f`, the
target of the old `M.q`, which is enough to prove `false`. This
happens:

- after a command that fails once the body of a module has been
  typed, or at the end of a section that defines a local module: the
  body is typed in an environment made by `enter`;
- after `pragma reset` or an undo, which go back to an environment
  that shares the cache with the one after `end T` or `require T`.

Entering a scope, and binding a theory, a module or a theory alias,
now give the new environment its own copy of the cache
(`fork_norm`), and so does `EcEnv.copy`. The other bindings
(operators, lemmas, ...) do not bring module paths and still share
it, so that the entries computed in proofs are kept.
@strub strub changed the title fix(env): give each new scope its own normalisation cache fix(env): do not share the normalisation cache with earlier environments Nov 29, 2026
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.

Unsound: a stale normalisation cache lets a module path keep the meaning of an earlier definition

1 participant