Zulip Chat Archive

Stream: lean4

Topic: proof of for loop idx bound


Quinn (Dec 02 2024 at 22:32):

I can write

Id.run do for i in [0:10] do ...

is there a way to name the fact, which lean presumably knows, that 0<=i<10?

I can explore the ForIn construct, but I thought I'd check if anyone has some code lying around that does this. I'm accessing and updating an array inside the for loop, and trying to eliminate !

Kim Morrison (Dec 02 2024 at 23:09):

for h : i in [0:10] do

Quinn (Dec 02 2024 at 23:29):

oh that's so cool

Quinn (Dec 03 2024 at 00:09):

having a bunch of trouble converting the context which says that col : Std.Range := { start, stop, step } and i \in col into the Fin constructor.

Kim Morrison (Dec 03 2024 at 00:11):

Note sure what you are saying. Could you post a #mwe?

Quinn (Dec 03 2024 at 00:20):

yes, sry

Quinn (Dec 03 2024 at 00:20):

def f := Id.run do
  let mut result := Array.mkArray 10 0
  for h : i in [0:10] do
    have ifin : i < result.size := by
      sorry
    let fini : Fin result.size := i, ifin
    result := result.set fini i

Quinn (Dec 03 2024 at 00:20):

the sorry

Quinn (Dec 03 2024 at 00:21):

my usual have trick for getting col out of grey'd out dagger notation isn't working

Quinn (Dec 03 2024 at 00:25):

rather, I do

      have start, stop, step := col
      have result : Array Nat := mkArray 10 0

but result is grey'd out / dagger so I can't show that stop = result.size

Kim Morrison (Dec 03 2024 at 03:31):

The problem here is that as soon as you use Array.set, you need a lemma to know the size is the same as before.

Kim Morrison (Dec 03 2024 at 03:32):

Consider using Vector here. (You may need to be on a nightly release, or otherwise import Batteries.)


Last updated: May 02 2025 at 03:31 UTC