Folding over Fin #
This file defines the prime versions of Fin.(d)fold{l/r} which has better definitional equalities
(i.e. automatically unfold for 0 and for n.succ). We then show equivalences between the prime
and non-prime versions, so that when compiling, the non-prime versions (which are more efficient)
are used.
We also prove that the existing mathlib function finSumFinEquiv : ∑ i, Fin (n i) ≃ Fin (∑ i, n i)
is equivalent to a version defined via Fin.dfoldl'.
theorem
Fin.dfoldl_congr
{n : ℕ}
{α α' : Fin (n + 1) → Type u}
{f : (i : Fin n) → α i.castSucc → α i.succ}
{f' : (i : Fin n) → α' i.castSucc → α' i.succ}
{init : α 0}
{init' : α' 0}
(hα : ∀ (i : Fin (n + 1)), α i = α' i)
(hf : ∀ (i : Fin n) (a : α i.castSucc), f i a = cast ⋯ (f' i (cast ⋯ a)))
(hinit : init = cast ⋯ init')
:
Congruence for dfoldl
theorem
Fin.dfoldl_congr_dcast
{n : ℕ}
{ι : Type v}
{α α' : Fin (n + 1) → ι}
{β : ι → Type u}
[DCast ι β]
{f : (i : Fin n) → β (α i.castSucc) → β (α i.succ)}
{f' : (i : Fin n) → β (α' i.castSucc) → β (α' i.succ)}
{init : β (α 0)}
{init' : β (α' 0)}
(hα : ∀ (i : Fin (n + 1)), α i = α' i)
(hf : ∀ (i : Fin n) (a : β (α i.castSucc)), f i a = dcast ⋯ (f' i (dcast ⋯ a)))
(hinit : init = dcast ⋯ init')
:
Congruence for dfoldl whose type vectors are indexed by ι and have a DCast instance
Note that we put cast (instead of dcast) in the theorem statement for easier matching,
but dcast inside the hypotheses for downstream proving.