Skip to content

Commit ac2a7e7

Browse files
authored
fix: make replay output a kernel environment (#14391)
This PR refactors `Lean.Environment.replay` to `Lean.Kernel.Environment.replay`, so that environment replays work on `Kernel.Environment` instead of `Environment`, avoiding using the unstable `Environment.ofKernelEnv`. See #13783 for more context. Closes #13783 --------- Co-authored-by: Thomas Zhu <thomaszhu@bytedance.com>
1 parent be66a44 commit ac2a7e7

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/Lean/Replay.lean

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public import Lean.AddDecl
1111
import Lean.Util.FoldConsts
1212

1313
/-!
14-
# `Lean.Environment.replay`
14+
# `Lean.Kernel.Environment.replay`
1515
1616
`replay env constantMap` will "replay" all the constants in `constantMap : HashMap Name ConstantInfo` into `env`,
1717
sending each declaration to the kernel for checking.
@@ -21,12 +21,12 @@ but rather checks that they are identical to constructors or recursors generated
2121
after replaying any inductive definitions occurring in `constantMap`.
2222
2323
`replay` can be used either as:
24-
* a verifier for an `Environment`, by sending everything to the kernel, or
25-
* a mechanism to safely transfer constants from one `Environment` to another.
24+
* a verifier for a `Kernel.Environment`, by sending everything to the kernel, or
25+
* a mechanism to safely transfer constants from one `Kernel.Environment` to another.
2626
2727
-/
2828

29-
namespace Lean.Environment
29+
namespace Lean.Kernel.Environment
3030

3131
namespace Replay
3232

@@ -168,22 +168,30 @@ end Replay
168168
open Replay
169169

170170
/--
171-
"Replay" some constants into an `Environment`, sending them to the kernel for checking.
171+
"Replay" some constants into a `Kernel.Environment`, sending them to the kernel for checking.
172172
173173
Throws a `IO.userError` if the kernel rejects a constant,
174174
or if there are malformed recursors or constructors for inductive types.
175175
-/
176-
public def replay (newConstants : Std.HashMap Name ConstantInfo) (env : Environment) : IO Environment := do
176+
public def replay (newConstants : Std.HashMap Name ConstantInfo) (env : Kernel.Environment) :
177+
IO Kernel.Environment := do
177178
let mut remaining : NameSet := ∅
178179
for (n, ci) in newConstants.toList do
179180
-- We skip unsafe constants, and also partial constants.
180181
-- Later we may want to handle partial constants.
181182
if !ci.isUnsafe && !ci.isPartial then
182183
remaining := remaining.insert n
183-
let (_, s) ← StateRefT'.run (s := { env := env.toKernelEnv, remaining }) do
184+
let (_, s) ← StateRefT'.run (s := { env, remaining }) do
184185
ReaderT.run (r := { newConstants }) do
185186
for n in remaining do
186187
replayConstant n
187188
checkPostponedConstructors
188189
checkPostponedRecursors
189-
return .ofKernelEnv s.env
190+
return s.env
191+
192+
end Kernel.Environment
193+
194+
@[deprecated Kernel.Environment.replay (since := "2026-07-14")]
195+
public def Environment.replay (newConstants : Std.HashMap Name ConstantInfo) (env : Environment) :
196+
IO Environment := do
197+
return .ofKernelEnv (← env.toKernelEnv.replay newConstants)

src/LeanChecker.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ unsafe def replayFromImports (module : Name) : IO Unit := do
3030
-- Collect constants from last ("most private") part, which subsumes all prior ones
3131
for name in parts[parts.size-1].1.constNames, ci in parts[parts.size-1].1.constants do
3232
newConstants := newConstants.insert name ci
33-
let env' ← env.replay newConstants
34-
env'.freeRegions
33+
discard <| env.toKernelEnv.replay newConstants
34+
env.freeRegions
3535

3636
unsafe def replayFromFresh (module : Name) : IO Unit := do
3737
Lean.withImportModules #[{module}] {} fun env => do
38-
discard <| (← mkEmptyEnvironment).replay env.constants.map₁
38+
discard <| (← mkEmptyEnvironment).toKernelEnv.replay env.constants.map₁
3939

4040
/-- Read the name of the main module from the `lake-manifest`. -/
4141
-- This has been copied from `ImportGraph.getCurrentModule` in the

tests/elab/issue12819.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ run_meta show MetaM Unit from do
1111
consts.insert n info
1212
let some importEnv := env.importEnv?
1313
| unreachable!
14-
let _ ← importEnv.replay consts
14+
let _ ← importEnv.toKernelEnv.replay consts

tests/pkg/leanchecker/LeanCheckerTests/QuotEq.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ open Lean
2828
-- Replay from a fresh environment - this would fail before the fix
2929
-- if Quot was processed before Eq
3030
let freshEnv ← mkEmptyEnvironment
31-
let _ ← freshEnv.replay constants
31+
let _ ← freshEnv.toKernelEnv.replay constants
3232

3333
IO.println "Replay succeeded!"

0 commit comments

Comments
 (0)