Skip to content

Commit 05ab0d9

Browse files
committed
refactor: move registration of namespaces on kernel add into elaborator
1 parent 08b0ac7 commit 05ab0d9

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

src/Lean/AddDecl.lean

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,43 @@ import Lean.CoreM
88

99
namespace Lean
1010

11+
@[deprecated "use `Lean.addAndCompile` instead"]
1112
def Environment.addAndCompile (env : Environment) (opts : Options) (decl : Declaration)
1213
(cancelTk? : Option IO.CancelToken := none) : Except Kernel.Exception Environment := do
1314
let env ← addDecl env opts decl cancelTk?
1415
compileDecl env opts decl
1516

17+
private def isNamespaceName : Name → Bool
18+
| .str .anonymous _ => true
19+
| .str p _ => isNamespaceName p
20+
| _ => false
21+
22+
private def registerNamePrefixes (env : Environment) (name : Name) : Environment :=
23+
match name with
24+
| .str _ s =>
25+
if s.get 0 == '_' then
26+
-- Do not register namespaces that only contain internal declarations.
27+
env
28+
else
29+
go env name
30+
| _ => env
31+
where go env
32+
| .str p _ => if isNamespaceName p then go (env.registerNamespace p) p else env
33+
| _ => env
34+
1635
def addDecl (decl : Declaration) : CoreM Unit := do
1736
profileitM Exception "type checking" (← getOptions) do
18-
withTraceNode `Kernel (fun _ => return m!"typechecking declaration") do
37+
let mut env ← withTraceNode `Kernel (fun _ => return m!"typechecking declaration") do
1938
if !(← MonadLog.hasErrors) && decl.hasSorry then
2039
logWarning "declaration uses 'sorry'"
21-
match (← getEnv).addDecl (← getOptions) decl (← read).cancelTk? with
22-
| .ok env => setEnv env
23-
| .error ex => throwKernelException ex
40+
(← getEnv).addDecl (← getOptions) decl (← read).cancelTk? |> ofExceptKernelException
41+
42+
-- register namespaces for newly added constants; this used to be done by the kernel itself
43+
-- but that is incompatible with moving it to a separate task
44+
env := decl.getNames.foldl registerNamePrefixes env
45+
if let .inductDecl _ _ types _ := decl then
46+
env := types.foldl (registerNamePrefixes · <| ·.name ++ `rec) env
47+
setEnv env
2448

2549
def addAndCompile (decl : Declaration) : CoreM Unit := do
2650
addDecl decl

src/Lean/Environment.lean

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ opaque addDeclCore (env : Environment) (maxHeartbeats : USize) (decl : @& Declar
320320
@[inherit_doc Kernel.Environment.addDeclWithoutChecking, extern "lean_elab_add_decl_without_checking"]
321321
opaque addDeclWithoutChecking (env : Environment) (decl : @& Declaration) : Except Kernel.Exception Environment
322322

323+
@[deprecated "use `Lean.addDecl` instead"]
323324
def addDecl (env : Environment) (opts : Options) (decl : Declaration)
324325
(cancelTk? : Option IO.CancelToken := none) : Except Kernel.Exception Environment :=
325326
if debug.skipKernelTC.get opts then
@@ -1103,34 +1104,9 @@ def isNamespace (env : Environment) (n : Name) : Bool :=
11031104
def getNamespaceSet (env : Environment) : NameSSet :=
11041105
namespacesExt.getState env
11051106

1106-
private def isNamespaceName : Name → Bool
1107-
| .str .anonymous _ => true
1108-
| .str p _ => isNamespaceName p
1109-
| _ => false
1110-
1111-
private def registerNamePrefixes (env : Environment) (name : Name) : Environment :=
1112-
match name with
1113-
| .str _ s =>
1114-
if s.get 0 == '_' then
1115-
-- Do not register namespaces that only contain internal declarations.
1116-
env
1117-
else
1118-
go env name
1119-
| _ => env
1120-
where go env
1121-
| .str p _ => if isNamespaceName p then go (registerNamespace env p) p else env
1122-
| _ => env
1123-
11241107
@[export lean_elab_environment_update_base_after_kernel_add]
1125-
private def updateBaseAfterKernelAdd (env : Environment) (added : Declaration) (base : Kernel.Environment) : Environment := Id.run do
1126-
let mut env := { env with base }
1127-
env := added.getNames.foldl registerNamePrefixes env
1128-
if let .inductDecl _ _ types _ := added then
1129-
-- also register inductive type names as namespaces; this used to be done by the kernel itself
1130-
-- when adding e.g. the recursor but now that the environment extension API exists only for
1131-
-- `Lean.Environment`, we have to do it here
1132-
env := types.foldl (registerNamePrefixes · <| ·.name ++ `rec) env
1133-
env
1108+
private def updateBaseAfterKernelAdd (env : Environment) (base : Kernel.Environment) : Environment :=
1109+
{ env with base }
11341110

11351111
@[export lean_display_stats]
11361112
def displayStats (env : Environment) : IO Unit := do

src/library/elab_environment.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ Authors: Leonardo de Moura, Sebastian Ullrich
1111
#include "library/compiler/ir_interpreter.h"
1212

1313
namespace lean {
14-
/* updateBaseAfterKernelAdd (env : Environment) (added : Declaration) (base : Kernel.Environment) :
15-
Environment
14+
/* updateBaseAfterKernelAdd (env : Environment) (base : Kernel.Environment) : Environment
1615
17-
Updates an elab environment with a given kernel environment after the declaration `d` has been
18-
added to it. `d` is used to adjust further elab env data such as registering new namespaces.
16+
Updates an elab environment with a given kernel environment.
1917
2018
NOTE: Ideally this language switching would not be necessary and we could do all this in Lean
2119
only but the old code generator and `mk_projections` still need a C++ `elab_environment::add`. */
22-
extern "C" obj_res lean_elab_environment_update_base_after_kernel_add(obj_arg env, obj_arg d, obj_arg kenv);
20+
extern "C" obj_res lean_elab_environment_update_base_after_kernel_add(obj_arg env, obj_arg kenv);
2321

2422
elab_environment elab_environment::add(declaration const & d, bool check) const {
2523
environment kenv = to_kernel_env().add(d, check);
26-
return elab_environment(lean_elab_environment_update_base_after_kernel_add(this->to_obj_arg(), d.to_obj_arg(), kenv.to_obj_arg()));
24+
return elab_environment(lean_elab_environment_update_base_after_kernel_add(this->to_obj_arg(), kenv.to_obj_arg()));
2725
}
2826

2927
extern "C" LEAN_EXPORT object * lean_elab_add_decl(object * env, size_t max_heartbeat, object * decl,

0 commit comments

Comments
 (0)