Skip to content

Add the universe level functions of rulean-core - #27

Merged
tamnd merged 2 commits into
mainfrom
m2/levels
Nov 21, 2026
Merged

tamnd merged 2 commits into
mainfrom
m2/levels

Conversation

@tamnd

@tamnd tamnd commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Refers to #3.

This PR adds crates/rulean-core/src/level.rs. It is the first part of engine R. The functions are a transcription of level.cpp of the official kernel v4.35.0 (spec 05.6).

  • mk_max and mk_imax keep the simplifications of the official code.
  • normalize flattens max, sorts by is_norm_lt, keeps the largest offset for each base and drops a covered explicit level. The result is cached in a LevelCache with one slot for each level handle.
  • is_equivalent compares normal forms. It is incomplete by design, as in the official kernel.
  • is_geq is the structural algorithm on normal forms.
  • instantiate makes a changed max or imax node again with mk_max or mk_imax, as the official update_max does.
  • name_cmp is the name order of the kernel. A number component is less than a string component.

The code is in the Aeneas subset. The sort is an insertion sort, because the sort methods are outside the modelled functions. Equal levels have equal handles, so the result does not depend on the sort algorithm.

Tests: crates/rulean-core/tests/level.rs has 9 tests for the simplifications, the normal form, equivalence, is_geq, the zero tests, instantiation and the name order.

@tamnd tamnd added this to the M2 Engine R milestone Nov 21, 2026
@tamnd tamnd added area/core The type checker: engines F and R, declarations, profiles type/feature New function labels Nov 21, 2026
The functions are a transcription of level.cpp of the official kernel v4.35.0. They keep the order of the cases and the simplifications of mk_max and mk_imax, because the shape of a level can change the result of is_geq.
@tamnd
tamnd merged commit 667dc7e into main Nov 21, 2026
17 checks passed
@tamnd
tamnd deleted the m2/levels branch November 21, 2026 16:33
@tamnd tamnd mentioned this pull request Nov 21, 2026
2 of 20 tasks
tamnd added a commit that referenced this pull request Nov 21, 2026
Refers to #3.

This PR comes after #27. It adds `crates/rulean-core/src/expr.rs` and
`crates/rulean-core/src/memo.rs`.

The expression operations are a transcription of `instantiate.cpp`,
`abstract.cpp`, the loose bound variable part of `expr.cpp` and
`expr_eq_fn.cpp` of the official kernel v4.35.0:

- `instantiate`, `instantiate_from`, `instantiate1` and
`instantiate_rev`. A substituted value is lifted under the binders, as
in the official code.
- `lift_loose_bvars`, `lower_loose_bvars`, `has_loose_bvar` and
`abstract_fvars`.
- `instantiate_lparams`. A changed level is made again with the
simplifications of `level::instantiate`.
- `app_fn`, `app_args`, `app_num_args` and `mk_rev_app`.
- `apply_beta`, `head_beta` and `cheap_beta_reduce`.
- `expr_equal`, the `==` of the official kernel. It ignores binder names
and binder info, and it uses the pair cache of `expr_eq_fn`.

The official functions use `replace` with a cache for shared nodes. Here
each operation has its own recursion and a `Memo` keyed by the node and
the binder offset. `Memo` is a small map on the verified `Table` with
epochs, so a clear is O(1). A free variable is an atom, so the
operations do not go into its type.

**Store change.** The hash of a `lam`, `pi` or `let` node no longer
mixes the binder name or the binder info. This is the method of Lean.
Alpha-equivalent terms now have equal hashes, so `expr_equal` can reject
on a hash mismatch. The interning table still compares the full node, so
handles stay exact.

Tests: `crates/rulean-core/tests/expr.rs` has 9 tests. The store tests
pass without a change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core The type checker: engines F and R, declarations, profiles type/feature New function

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant