Skip to content

Commit

Permalink
chore: remove functions for compiling decls from Environment (#6600)
Browse files Browse the repository at this point in the history
This PR removes functions from compiling decls from Environment, and
moves all users to functions on CoreM. This is required for supporting
the new code generator, since its implementation uses CoreM.
  • Loading branch information
zwarich authored Jan 13, 2025
1 parent e6a6437 commit 85560da
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 43 deletions.
5 changes: 0 additions & 5 deletions src/Lean/AddDecl.lean
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ def Environment.addDecl (env : Environment) (opts : Options) (decl : Declaration
else
addDeclCore env (Core.getMaxHeartbeats opts).toUSize decl cancelTk?

def Environment.addAndCompile (env : Environment) (opts : Options) (decl : Declaration)
(cancelTk? : Option IO.CancelToken := none) : Except KernelException Environment := do
let env ← addDecl env opts decl cancelTk?
compileDecl env opts decl

def addDecl (decl : Declaration) : CoreM Unit := do
profileitM Exception "type checking" (← getOptions) do
withTraceNode `Kernel (fun _ => return m!"typechecking declaration") do
Expand Down
8 changes: 2 additions & 6 deletions src/Lean/Compiler/InitAttr.lean
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ def declareBuiltin (forDecl : Name) (value : Expr) : CoreM Unit := do
let type := mkApp (mkConst `IO) (mkConst `Unit)
let decl := Declaration.defnDecl { name, levelParams := [], type, value, hints := ReducibilityHints.opaque,
safety := DefinitionSafety.safe }
match (← getEnv).addAndCompile {} decl with
-- TODO: pretty print error
| Except.error e => do
let msg ← (e.toMessageData {}).toString
throwError "failed to emit registration code for builtin '{forDecl}': {msg}"
| Except.ok env => IO.ofExcept (setBuiltinInitAttr env name) >>= setEnv
addAndCompile decl
IO.ofExcept (setBuiltinInitAttr (← getEnv) name) >>= setEnv

end Lean
15 changes: 0 additions & 15 deletions src/Lean/Compiler/Old.lean
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,3 @@ def isUnsafeRecName? : Name → Option Name
| _ => none

end Compiler

namespace Environment

/--
Compile the given block of mutual declarations.
Assumes the declarations have already been added to the environment using `addDecl`.
-/
@[extern "lean_compile_decls"]
opaque compileDecls (env : Environment) (opt : @& Options) (decls : @& List Name) : Except KernelException Environment

/-- Compile the given declaration, it assumes the declaration has already been added to the environment using `addDecl`. -/
def compileDecl (env : Environment) (opt : @& Options) (decl : @& Declaration) : Except KernelException Environment :=
compileDecls env opt (Compiler.getDeclNamesForCodeGen decl)

end Environment
7 changes: 5 additions & 2 deletions src/Lean/CoreM.lean
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,16 @@ register_builtin_option compiler.enableNew : Bool := {
@[extern "lean_lcnf_compile_decls"]
opaque compileDeclsNew (declNames : List Name) : CoreM Unit

@[extern "lean_compile_decls"]
opaque compileDeclsOld (env : Environment) (opt : @& Options) (decls : @& List Name) : Except KernelException Environment

def compileDecl (decl : Declaration) : CoreM Unit := do
let opts ← getOptions
let decls := Compiler.getDeclNamesForCodeGen decl
if compiler.enableNew.get opts then
compileDeclsNew decls
let res ← withTraceNode `compiler (fun _ => return m!"compiling old: {decls}") do
return (← getEnv).compileDecl opts decl
return compileDeclsOld (← getEnv) opts decls
match res with
| Except.ok env => setEnv env
| Except.error (KernelException.other msg) =>
Expand All @@ -533,7 +536,7 @@ def compileDecls (decls : List Name) : CoreM Unit := do
let opts ← getOptions
if compiler.enableNew.get opts then
compileDeclsNew decls
match (← getEnv).compileDecls opts decls with
match compileDeclsOld (← getEnv) opts decls with
| Except.ok env => setEnv env
| Except.error (KernelException.other msg) =>
throwError msg
Expand Down
7 changes: 2 additions & 5 deletions src/Lean/ParserCompiler.lean
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,8 @@ partial def compileParserExpr (e : Expr) : MetaM Expr := do
name := c', levelParams := []
type := ty, value := value, hints := ReducibilityHints.opaque, safety := DefinitionSafety.safe
}
let env ← getEnv
let env ← match env.addAndCompile {} decl with
| Except.ok env => pure env
| Except.error kex => do throwError (← (kex.toMessageData {}).toString)
setEnv <| ctx.combinatorAttr.setDeclFor env c c'
addAndCompile decl
modifyEnv (ctx.combinatorAttr.setDeclFor · c c')
if cinfo.type.isConst then
if let some kind ← parserNodeKind? cinfo.value! then
-- If the parser is parameter-less and produces a node of kind `kind`,
Expand Down
21 changes: 11 additions & 10 deletions tests/lean/run/toExpr.lean
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ let decl := Declaration.defnDecl {
safety := DefinitionSafety.safe
};
IO.println (toExpr a);
(match env.addAndCompile {} decl with
| Except.error _ => throwError "addDecl failed"
| Except.ok env =>
match env.evalConst α {} auxName with
| Except.error ex => throwError ex
| Except.ok b => do
IO.println b;
unless a == b do
throwError "toExpr failed";
pure ())
let oldEnv ← getEnv;
addAndCompile decl;
let newEnv ← getEnv
setEnv oldEnv
match newEnv.evalConst α {} auxName with
| Except.error ex => throwError ex
| Except.ok b => do
IO.println b;
unless a == b do
throwError "toExpr failed";
pure ()

#eval test #[(1, 2), (3, 4)]
#eval test ['a', 'b', 'c']
Expand Down

0 comments on commit 85560da

Please sign in to comment.