@[inline]
Tail-recursive version of Nat.fold
.
Equations
- n.foldTR f init = Nat.foldTR.loop n f n ⋯ init
Instances For
@[specialize #[]]
Equations
- Nat.foldTR.loop n f 0 h x = x
- Nat.foldTR.loop n f m.succ h x = Nat.foldTR.loop n f m ⋯ (f (n - m.succ) ⋯ x)
Instances For
@[specialize #[]]
Nat.foldRev
evaluates f
on the numbers up to n
exclusive, in decreasing order:
Nat.foldRev f 3 init = f 0 <| f 1 <| f 2 <| init
Equations
- Nat.foldRev 0 f x✝ = x✝
- n.succ.foldRev f x✝ = n.foldRev (fun (i : Nat) (h : i < n) => f i ⋯) (f n ⋯ x✝)
Instances For
@[specialize #[]]
Equations
- Nat.anyTR.loop n f 0 h = false
- Nat.anyTR.loop n f m.succ h = (f (n - m.succ) ⋯ || Nat.anyTR.loop n f m ⋯)
Instances For
@[specialize #[]]
Equations
- Nat.allTR.loop n f 0 h = true
- Nat.allTR.loop n f m.succ h = (f (n - m.succ) ⋯ && Nat.allTR.loop n f m ⋯)
Instances For
csimp theorems #
theorem
Nat.foldTR_loop_congr
{α : Type u}
{n m : Nat}
(w : n = m)
(f : (i : Nat) → i < n → α → α)
(j : Nat)
(h : j ≤ n)
(init : α)
:
Nat.foldTR.loop n f j h init = Nat.foldTR.loop m (fun (i : Nat) (h : i < m) => f i ⋯) j ⋯ init
theorem
Nat.anyTR_loop_congr
{n m : Nat}
(w : n = m)
(f : (i : Nat) → i < n → Bool)
(j : Nat)
(h : j ≤ n)
:
Nat.anyTR.loop n f j h = Nat.anyTR.loop m (fun (i : Nat) (h : i < m) => f i ⋯) j ⋯
theorem
Nat.allTR_loop_congr
{n m : Nat}
(w : n = m)
(f : (i : Nat) → i < n → Bool)
(j : Nat)
(h : j ≤ n)
:
Nat.allTR.loop n f j h = Nat.allTR.loop m (fun (i : Nat) (h : i < m) => f i ⋯) j ⋯
theorem
Nat.fold_eq_finRange_foldl
{α : Type u}
(n : Nat)
(f : (i : Nat) → i < n → α → α)
(init : α)
:
n.fold f init = List.foldl
(fun (acc : α) (x : Fin n) =>
match x with
| ⟨i, h⟩ => f i h acc)
init (List.finRange n)
@[simp]
theorem
Nat.foldRev_zero
{α : Type u}
(f : (i : Nat) → i < 0 → α → α)
(init : α)
:
Nat.foldRev 0 f init = init
theorem
Nat.foldRev_eq_finRange_foldr
{α : Type u}
(n : Nat)
(f : (i : Nat) → i < n → α → α)
(init : α)
:
n.foldRev f init = List.foldr
(fun (x : Fin n) (acc : α) =>
match x with
| ⟨i, h⟩ => f i h acc)
init (List.finRange n)
theorem
Nat.any_eq_finRange_any
{n : Nat}
(f : (i : Nat) → i < n → Bool)
:
n.any f = (List.finRange n).any fun (x : Fin n) =>
match x with
| ⟨i, h⟩ => f i h
theorem
Nat.all_eq_finRange_all
{n : Nat}
(f : (i : Nat) → i < n → Bool)
:
n.all f = (List.finRange n).all fun (x : Fin n) =>
match x with
| ⟨i, h⟩ => f i h
@[inline]
def
Prod.foldI
{α : Type u}
(i : Nat × Nat)
(f : (j : Nat) → i.fst ≤ j → j < i.snd → α → α)
(a : α)
:
α
(start, stop).foldI f a
evaluates f
on all the numbers
from start
(inclusive) to stop
(exclusive) in increasing order:
(5, 8).foldI f init = init |> f 5 |> f 6 |> f 7