Register user primitives on the term-encoding typechecker#945
Draft
oflatt-claude wants to merge 1 commit into
Draft
Register user primitives on the term-encoding typechecker#945oflatt-claude wants to merge 1 commit into
oflatt-claude wants to merge 1 commit into
Conversation
A primitive registered through the Rust API (`add_pure_primitive` / `add_*_primitive` / the `add_primitive!` macros) after e-graph construction was only registered on the running e-graph, not on the separate `original_typechecking` e-graph that term encoding / proofs use to typecheck the encoded program. The encoder then reported the primitive as an unbound function, so callers had to manually register it on `proof_state.original_typechecking` as well (a proof test did exactly that). `register_per_context` now walks the `original_typechecking` chain and registers the primitive on each e-graph. A typechecker only typechecks and never evaluates, so the wrapper's runtime state is irrelevant there, and both e-graphs register the same built-ins at construction, so a primitive added to both gets the same `ExternalFunctionId`. A sort's primitives already reach the typechecker through its own `add_arcsort` when the typechecker typechecks the sort command, so `add_arcsort` detaches the typechecker while a sort registers to avoid double-registering (which would make primitive resolution ambiguous). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
oflatt-claude
pushed a commit
to oflatt-claude/egglog-encoding
that referenced
this pull request
Jul 7, 2026
A primitive registered through the Rust API (`add_pure_primitive` / `add_*_primitive` / the `add_primitive!` macros) after e-graph construction was only registered on the running e-graph, not on the separate `original_typechecking` e-graph that term encoding / proofs use to typecheck the encoded program. The encoder then reported the primitive as an unbound function, so callers had to manually register it on `proof_state.original_typechecking` as well (a proof test did exactly that). `register_per_context` now walks the `original_typechecking` chain and registers the primitive on each e-graph. A typechecker only typechecks and never evaluates, so the wrapper's runtime state is irrelevant there, and both e-graphs register the same built-ins at construction, so a primitive added to both gets the same `ExternalFunctionId`. A sort's primitives already reach the typechecker through its own `add_arcsort` when the typechecker typechecks the sort command, so `add_arcsort` detaches the typechecker while a sort registers to avoid double-registering (which would make primitive resolution ambiguous). Upstreamed as egraphs-good/egglog#945. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #945 +/- ##
=======================================
Coverage 86.71% 86.71%
=======================================
Files 92 92
Lines 28609 28641 +32
=======================================
+ Hits 24807 24837 +30
- Misses 3802 3804 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Problem
A primitive registered through the Rust API (
EGraph::add_pure_primitive/add_read_primitive/add_write_primitive/add_full_primitive, or theadd_primitive!/add_literal_prim!macros) after the e-graph is constructed is only registered on the running e-graph. Under term encoding / proofs (new_with_term_encoding/new_with_proofs), typechecking is done by a separateproof_state.original_typecheckinge-graph, which never learns about the primitive — so the encoder reports it as an unbound function:Callers had to work around it by manually re-registering on
proof_state.original_typechecking(a proof test did exactly that).Fix
register_per_contextnow walks theoriginal_typecheckingchain and registers the primitive on each e-graph. A typechecker only typechecks and never evaluates, so the wrapper's runtime state (e.g. an action registry) is irrelevant there; and both e-graphs register the same built-ins at construction, so a primitive added to both gets the sameExternalFunctionId.A sort's primitives already reach the typechecker through its own
add_arcsortwhen the typechecker typechecks the sort command, soadd_arcsortdetaches the typechecker while a sort registers — otherwise container/sort primitives would be double-registered and primitive resolution would become ambiguous.Tests
prelude::tests::add_primitive_under_term_encodingregression test.original_typecheckingregistration from the container proof test.cargo test, incl. the proof/term-encoding tests).🤖 Generated with Claude Code