@@ -37,7 +37,23 @@ protected theorem le_antisymm {x y : Fin n} (h1 : x ≤ y) (h2 : y ≤ x) : x =
37
37
theorem list_succ (n) : list (n+1 ) = 0 :: (list n).map Fin.succ := by
38
38
apply List.ext_get; simp; intro i; cases i <;> simp
39
39
40
- /-! ### foldlM -/
40
+ theorem list_succ_last (n) : list (n+1 ) = (list n).map castSucc ++ [last n] := by
41
+ rw [list_succ]
42
+ induction n with
43
+ | zero => rfl
44
+ | succ n ih =>
45
+ rw [list_succ, List.map_cons castSucc, ih]
46
+ simp [Function.comp_def, succ_castSucc]
47
+
48
+ theorem list_reverse (n) : (list n).reverse = (list n).map rev := by
49
+ induction n with
50
+ | zero => rfl
51
+ | succ n ih =>
52
+ conv => lhs; rw [list_succ_last]
53
+ conv => rhs; rw [list_succ]
54
+ simp [List.reverse_map, ih, Function.comp_def, rev_succ]
55
+
56
+ /-! ### foldl -/
41
57
42
58
theorem foldlM_loop_lt [Monad m] (f : α → Fin n → m α) (x) (h : i < n) :
43
59
foldlM.loop n f x i = f x ⟨i, h⟩ >>= (foldlM.loop n f . (i+1 )) := by
@@ -75,14 +91,22 @@ theorem foldlM_eq_foldlM_list [Monad m] (f : α → Fin n → m α) (x) :
75
91
76
92
theorem foldl_eq_foldlM : foldl n f init = foldlM (m:=Id) n f init := rfl
77
93
78
- theorem foldl_zero (f : α → Fin 0 → α) (x) : foldl 0 f x = x := rfl
94
+ @[simp] theorem foldl_zero (f : α → Fin 0 → α) (x) : foldl 0 f x = x := rfl
79
95
80
96
theorem foldl_succ (f : α → Fin (n+1 ) → α) (x) :
81
97
foldl (n+1 ) f x = foldl n (fun x j => f x j.succ) (f x 0 ) := foldlM_succ ..
82
98
83
- theorem foldl_eq_foldl_list (f : α → Fin n → α) (x) :
84
- foldl n f x = (list n).foldl f x :=
85
- by simp only [foldl_eq_foldlM, foldlM_eq_foldlM_list, List.foldl_eq_foldlM]
99
+ theorem foldl_succ_last (f : α → Fin (n+1 ) → α) (x) :
100
+ foldl (n+1 ) f x = f (foldl n (f · ·.castSucc) x) (last n) := by
101
+ rw [foldl_succ]
102
+ induction n generalizing x with
103
+ | zero => rfl
104
+ | succ n ih => rw [foldl_succ, ih (f · ·.succ), foldl_succ]; simp [succ_castSucc]
105
+
106
+ theorem foldl_eq_foldl_list (f : α → Fin n → α) (x) : foldl n f x = (list n).foldl f x := by
107
+ induction n generalizing x with
108
+ | zero => rfl
109
+ | succ n ih => rw [foldl_succ, ih, list_succ, List.foldl_cons, List.foldl_map]
86
110
87
111
/-! ### foldrM -/
88
112
@@ -104,24 +128,33 @@ theorem foldrM_loop [Monad m] [LawfulMonad m] (f : Fin (n+1) → α → m α) (x
104
128
rw [foldrM_loop_succ, foldrM_loop_succ, bind_assoc]
105
129
congr; funext; exact ih ..
106
130
107
- theorem foldrM_zero [Monad m] (f : Fin 0 → α → m α) (x) : foldrM 0 f x = pure x := rfl
131
+ @[simp] theorem foldr_zero (f : Fin 0 → α → α) (x) : foldr 0 f x = x := rfl
108
132
109
- theorem foldrM_succ [Monad m] [LawfulMonad m] (f : Fin (n+1 ) → α → m α) (x) :
110
- foldrM (n+1 ) f x = foldrM n (fun i => f i.succ) x >>= f 0 := foldrM_loop ..
133
+ theorem foldr_succ (f : Fin (n+1 ) → α → α) (x) :
134
+ foldr (n+1 ) f x = f 0 (foldr n (fun i => f i.succ) x) := foldr_loop ..
111
135
112
- theorem foldrM_eq_foldrM_list [Monad m] [LawfulMonad m] (f : Fin n → α → m α) (x) :
113
- foldrM n f x = (list n).foldrM f x := by
114
- induction n with
136
+ theorem foldr_succ_last (f : Fin (n+1 ) → α → α) (x) :
137
+ foldr (n+1 ) f x = foldr n (f ·.castSucc) (f (last n) x) := by
138
+ rw [foldr_succ]
139
+ induction n generalizing x with
115
140
| zero => rfl
116
- | succ n ih => rw [foldrM_succ , ih, list_succ, List.foldrM_cons, List.foldrM_map ]
141
+ | succ n ih => rw [foldr_succ , ih (f ·.succ), foldr_succ]; simp [succ_castSucc ]
117
142
118
- theorem foldr_eq_foldrM (f : Fin n → α → α) (init) : foldr n f init = foldrM (m:=Id) n f init := rfl
143
+ theorem foldr_eq_foldr_list (f : Fin n → α → α) (x) : foldr n f x = (list n).foldr f x := by
144
+ induction n with
145
+ | zero => rfl
146
+ | succ n ih => rw [foldr_succ, ih, list_succ, List.foldr_cons, List.foldr_map]
119
147
120
- theorem foldr_zero (f : Fin 0 → α → α) (x) : foldr 0 f x = x := rfl
148
+ /-! ### foldl/ foldr -/
121
149
122
- theorem foldr_succ (f : Fin (n+1 ) → α → α) (x) :
123
- foldr (n+1 ) f x = f 0 (foldr n (fun i => f i.succ) x) := foldrM_loop ..
150
+ theorem foldl_rev (f : Fin n → α → α) (x) :
151
+ foldl n (fun x i => f i.rev x) x = foldr n f x := by
152
+ induction n generalizing x with
153
+ | zero => rfl
154
+ | succ n ih => rw [foldl_succ, foldr_succ_last, ← ih]; simp [rev_succ]
124
155
125
- theorem foldr_eq_foldr_list (f : Fin n → α → α) (x) :
126
- foldr n f x = (list n).foldr f x := by
127
- simp only [foldr_eq_foldrM, foldrM_eq_foldrM_list, List.foldr_eq_foldrM]
156
+ theorem foldr_rev (f : α → Fin n → α) (x) :
157
+ foldr n (fun i x => f x i.rev) x = foldl n f x := by
158
+ induction n generalizing x with
159
+ | zero => rfl
160
+ | succ n ih => rw [foldl_succ_last, foldr_succ, ← ih]; simp [rev_succ]
0 commit comments