Repository navigation
Kernel.process: messages, inherit_env, header handling, unescaped names, cwd-independent lean_prefix; Float by value - #16
Draft
kiranandcode wants to merge 3 commits into
Conversation
…kipped when loaded (else an error naming the modules); names unescaped; lean_prefix independent of the cwd Kernel.process kept its string result, now with the header handled and names unescaped. New: leanpy_kernel_frontend_process_json and Kernel.elaborate -> ProcessResult (commands' constants and messages, header messages, error, ok/errors). lean_prefix: LEANPY_LEAN_PREFIX, then a pinned toolchain (set_toolchain, LEANPY_TOOLCHAIN, ELAN_TOOLCHAIN), then lean.py's own lean-toolchain; from_lake pins the project's. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…T set to the loaded runtime's prefix, so init_search's findSysroot does not depend on the cwd Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…), not boxed; test Found exporting a binary64 mirror from materia: a @[python] function taking a Float failed with 'argument N: must be real number, not LP_lean_object'. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #15 (
codex/lean-4.35-newton). These fix four problems found while driving the kernel bridge from materia, where a translator elaborates generated Lean declarations against a compiled Mathlib project.(a)
Kernel.processdropped Lean's messages and the environmentNew
@[python "leanpy_kernel_frontend_process_json"] frontendProcessJson (source : String) (inheritEnv : Bool) : IO String, wrapped as:ProcessResulthas these fields:commands: list[Command], where aCommandis (line,constants,messages);header: list[Message];error: str | None. This is set only when nothing could be processed: no environment is loaded, or an imported module isn't loaded.It also has the properties
messages,errors,constantsandok(noerrorand no error-severity message).A
Messagehasseverity("error","warning"or"information"),line(1-based),column(0-based),end_line,end_columnandtext, all relative to the processed source.With
inherit_env=True,envRefbecomes the environment after the source. Laterelaborate,process,decl_type,decl_axioms,goal_createand other calls then see the new declarations, so fragments can be elaborated and checked one at a time. If an exception occurs, the environment is unchanged. A declaration that fails to elaborate is still added (withsorryAx) and reported; callers checkok/errors.Kernel.processkeeps its string result, so existing callers and tests are unaffected.(b) A
module/importheader gave empty output silentlycreateContextStateFromFile (env? := some env)started parsing at byte 0, so a header failed to parse as commands, and the error was dropped. Both entry points, andcollect_sorrys, now parse the header withParser.parseHeader:modulekeyword is accepted, and the body is elaborated as ordinary commands.elaboratereturnserrornaming the missing modules, andprocessreturns<error: …>.(c)
lean_prefixdepended on the cwd'slean-toolchainutils.lean_prefix()resolves the first of these that is set:LEANPY_LEAN_PREFIX;utils.set_toolchain(name | lean-toolchain file | dir),LEANPY_TOOLCHAINorELAN_TOOLCHAIN, resolved bylean --print-prefixwithELAN_TOOLCHAINset;lean-toolchain, when it runs from a checkout;LeanLibrary.from_lakepins the project'slean-toolchainunless the caller already pinned one._parse.find_lean_headernow uses the same resolution instead of its ownlean --print-prefix.The in-process Lean has the same problem:
Kernel.init_searchcallsLean.findSysroot, which ranlean --print-prefixin the cwd. From a directory without a toolchain it failed with "no default toolchain configured".LeanLibrarynow setsLEAN_SYSROOT, if unset, to the resolved prefix when it loads the runtime, andfindSysrootreads that first.(d) Names came back wrapped in «»
processandelaborateconstants,all_decls,catalog,search,module_ofanddecl_axiomsnow useName.toString (escape := false). Goal and metavariable names are unchanged, since they are round-tripped intoresumeand similar calls. The ops that take a declaration name (decl_exists,decl_type,decl_value,decl_axioms,module_of,is_internal_name) accept it unescaped or escaped (parseName), so returned names read back.String.toNamealone returnsanonymousfora b. A component that itself contains a dot needs the escaped form.(e) A top-level
Floatargument or result was boxedLeanLibrary's wrapper passed aFloatparameter throughto_lean, which boxes it withlean_box_float. That is right inside anArray Floator a structure, but at the C ABI a top-levelFloatis a rawdouble. So any@[python]function taking aFloatfailed with "argument N: must be real number, not LP_lean_object", and aFloatresult was unboxed from a double. Both now go by value, the same way the wrapper already unboxes a top-levelBool/enum.tests/lean/TestLib.leangainspy_float_affine (x : Float) (k : Nat) (ys : Array Float) : Float, andtest_marshal.pygains a test for it. This was found while exporting a binary64 mirror from materia (BasisResearch/materia#24).Tests
The new
tests/test_kernel_process.pyhas 14 tests:inherit_env, and persisting with it (also after an error);moduleheader skipped, and an unloaded import refused by name;init_searchfrom a cwd with no toolchain;lean_prefixignoring a boguslean-toolchainin the cwd, honouringLEANPY_LEAN_PREFIX, andset_toolchainfrom a file.Full suite on Linux x86 (AWS, Lean 4.35.0): 1330 passed, 7 skipped, 3 failed. The 3 failures are the
test_packagingbundle and wheel tests, which needpatchelf. It isn't installed on this box, and they fail the same way on the base branch.ruff checkandruff format --checkare clean.examples/kernel/lean(KernelBridge) andtests/leanbuild.🤖 Generated with Claude Code