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'
.
Heterogeneous left fold over Fin n
in a monad, prime version with better defeq.
Automatically unfolds for 0
and n.succ
.
Equations
- Fin.dfoldlM' 0 α_2 f_2 init_2 = pure init_2
- Fin.dfoldlM' n_2.succ α_2 f_2 init_2 = do let x ← Fin.dfoldlM' n_2 (α_2 ∘ Fin.castSucc) (fun (i : Fin n_2) => f_2 i.castSucc) init_2 f_2 (Fin.last n_2) x
Instances For
Heterogeneous left fold over Fin n
, prime version with better defeq.
Automatically unfolds for 0
and n.succ
.
Equations
- Fin.dfoldl' n α f init = Fin.dfoldlM' n α f init
Instances For
Left fold over Fin n
, prime version with better defeq.
Automatically unfolds for 0
and n.succ
.
Equations
- Fin.foldl' n f init = Fin.dfoldl' n (fun (x : Fin (n + 1)) => α) f init
Instances For
Heterogeneous right fold over Fin n
in a monad, prime version with better defeq.
Automatically unfolds for 0
and n.succ
.
Equations
- Fin.dfoldrM' 0 α_2 f_2 init_2 = pure init_2
- Fin.dfoldrM' n_2.succ α_2 f_2 init_2 = do let x ← Fin.dfoldrM' n_2 (α_2 ∘ Fin.succ) (fun (i : Fin n_2) => f_2 i.succ) init_2 f_2 0 x
Instances For
Heterogeneous right fold over Fin n
, prime version with better defeq.
Automatically unfolds for 0
and n.succ
.
Equations
- Fin.dfoldr' n α f init = Fin.dfoldrM' n α f init
Instances For
Right fold over Fin n
, prime version with better defeq.
Automatically unfolds for 0
and n.succ
.
Equations
- Fin.foldr' n f init = Fin.dfoldr' n (fun (x : Fin (n + 1)) => α) f init
Instances For
Equations
- Fin.IterType n = Fin.dfoldl' n (fun (x : Fin (n + 1)) => Type) (fun (x : Fin n) (T : Type) => T × T) ℕ
Instances For
Equations
- Fin.PowerType n = Fin.dfoldr' n (fun (x : Fin (n + 1)) => Type) (fun (x : Fin n) (T : Type) => T → T) ℕ
Instances For
Equations
- Fin.NestedList n = Fin.dfoldr' n (fun (x : Fin (n + 1)) => Type) (fun (x : Fin n) (T : Type) => List T) ℕ
Instances For
Congruence for dfoldl
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.