Repository navigation
Conversation
There was a problem hiding this comment.
Thanks for helping us out with the iterator API!
I noticed that the verification API for the reverse iterators is still missing (under Std/Data/Iterators/Lemmas/Producers/Monadic/Array.lean etc.). It would be very helpful to have the verification API already in this PR. Besides the benefit of proving that the iterator does what it should, I believe that writing it would have caught some of the things I remarked, e.g. the missing Finite and IteratorLoop instance
| · cases h' | ||
| · cases h | ||
|
|
||
| instance ArrayRevIterator.instFinite [Pure m] : Finite (ArrayIterator α) m := by |
There was a problem hiding this comment.
Typo: Should be ArrayRevIterator. toList, as in your tests, works, but you wouldn't be able to use it in proofs without the Finite instance. There's the same type in instIteratorLoop below.
| @[always_inline, inline, match_pattern] | ||
| def _root_.Array.iterRevFromIdxM {α : Type w} (array : Array α) (m : Type w → Type w') (pos : Nat) | ||
| [Pure m] : | ||
| IterM (α := ArrayRevIterator α) m α := | ||
| ⟨{ array := array, pos := (pos + 1).min array.size, upper_bound := Nat.min_le_right _ _ }⟩ |
There was a problem hiding this comment.
Something that will bite us in the lemmas (iterRevFromIdxM is the natural thing to induct over):
For non-empty arrays, iterRevFromIdxM cannot represent the exhausted state pos := 0.
I also don't think that the match_pattern will work here because of min; this is also something that would become apparent when writing the lemmas.
I think we could address both issues by instead providing an iterator iterRevFromRevIdxM, where the last element of the iterator gets position 0. Then the exhausted state becomes representable and we can get rid of the min. What do you think? Alternatively, we could provide both iterRevFromRevIdxM, the more well-behaved one, and iterRevFromIdxM, the more intuitive one.
There was a problem hiding this comment.
iterRevFromRevIdxM sounds like a good idea indeed. It should also help with formalizing the equivalence with the forward iterator too. That said, how would I actually write down the statement that "iterRevFromRevIdxM is equivalent to iterFromIdxM on the reversed array"?
There was a problem hiding this comment.
It would already be great to show that applying toList to both yields the same result. The more general, but also more difficult, theorem would be to show that these iterators are equivalent. Although it would be great to have such a lemma, I think the toList-based lemma would be sufficient for now (except, of course, if you are willing to prove the general equivalence, that would be great).
205259a to
72eb75d
Compare
72eb75d to
3dbaf7e
Compare
This PR adds reverse iterators for
ArrayandVector.This change was split out of #15233 into a separate PR, as per the comments there.