Zulip Chat Archive

Stream: new members

Topic: Iterating over indexed types


Vivek Rajesh Joshi (Nov 15 2024 at 11:18):

import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fin.Tuple.Reflection

variable (F : Type) [Field F] [DecidableEq F]

inductive RowEchelonForm : (m n : Nat)  Type where
| nil : RowEchelonForm m 0
| pad : RowEchelonForm m n  RowEchelonForm m (n+1)
| extend : RowEchelonForm m n  (Fin n  F)  RowEchelonForm (m+1) (n+1)
deriving Repr

def R0 : RowEchelonForm Rat m n := RowEchelonForm.pad^[n] RowEchelonForm.nil

I want to iterate the pad function n times, but that isn't possible with Nat.iterate since it requires the function to have the same domain and codomain. Is there a version of Nat.iterate that can do this?

Vivek Rajesh Joshi (Nov 15 2024 at 11:55):

I have written a custom function for doing this. However, I'm running into someproblems while trying to prove theorems with it:

import Mathlib.Logic.Function.Iterate

@[simp]
def Nat.iterate_ind {α :   Type} (f : {i : }  α i  α (i+1)) (n : ) (a : α 0) : α n :=
  match n with
  | 0 => a
  | succ k => iterate_ind f k (f a)

theorem Nat.iterate_ind_succ {α :   Sort u} (f : {i : }  α i  α (i+1)) (n : ) :
  Nat.iterate_ind f (n+1) = (Nat.iterate_ind f n)  f := sorry

Why is the type of the LHS a (0+1) -> a (n+1+1) instead of being a 0 -> a (n+1)?

Riccardo Brasca (Nov 15 2024 at 12:02):

Lean is confused by α, that is implicit

Riccardo Brasca (Nov 15 2024 at 12:02):

theorem Nat.iterate_ind_succ {α :   Sort*} (f : {i : }  α i  α (i+1)) (n : ) :
  Nat.iterate_ind (α := α) f (n+1) = (Nat.iterate_ind f n)  f := sorry

works

Riccardo Brasca (Nov 15 2024 at 12:07):

Note that #check Nat.iterate_ind (α := fun i ↦ α (i + 37)) f typecheks, so Lean has no real way of guessing α from f, and in your example is guessing a different one.

Vivek Rajesh Joshi (Nov 15 2024 at 12:10):

Okay, I see now. Thanks a lot!

Riccardo Brasca (Nov 15 2024 at 12:13):

In your original example there is also the problem that Nat.iterate_ind is defined for an α taking values in Type and you apply with α taking value in any Sort. If you fix it using Sort* everywhere then rfl proves the theorem.


Last updated: May 02 2025 at 03:31 UTC