Zulip Chat Archive

Stream: new members

Topic: How to deal with a forIn step?


Philipp (Jan 09 2024 at 21:59):

There was a question recently about proving the equality of two functions that sum over a list:

def recSum : List Nat  Nat
| [] => 0
| a :: as => a + recSum as

def doSum (l : List Nat) : Nat := Id.run do
  let mut acc := 0
  for a in l do
    acc := acc + a
  return acc

I thought that could be an interesting exercise so I tried writing a proof

example (l : List Nat) : recSum l = doSum l := by
  unfold doSum Id.run
  simp
  induction' l with hd tail ih
  · apply Eq.refl
  · unfold recSum
    simp[ih]
    sorry

at this point the goal state is

hd: 
tail: List 
ih: recSum tail = forIn tail 0 fun a r => ForInStep.yield (r + a)
 (hd + forIn tail 0 fun a r => ForInStep.yield (r + a)) = forIn tail hd fun a r => ForInStep.yield (r + a)

I'm pretty lost. I guess I would want to somehow move the hd into the 0 (accumulator) on the left hand side? Like going a forIn step backwards?


Last updated: May 02 2025 at 03:31 UTC