Skip to content

Commit ff77a3c

Browse files
authored
feat(chapter5): prove reverse equivalence (#64)
this solves one more exercise that was pending.
1 parent bd75856 commit ff77a3c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Fad/Chapter5-Ex.lean

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,25 @@ def expression₂ {α : Type} : List α → List α :=
166166
def reverse {α : Type} : List α → List α :=
167167
List.foldl (flip List.cons) []
168168

169-
example (xs : List Nat) : expression₁ xs = reverse xs := by
170-
induction xs with
169+
theorem expression₁_eq_reverse_aux (g : List α → List α) (xs ys : List α) :
170+
(List.foldl (λ f x => (x :: ·) ∘ f) g xs) ys = List.foldl (flip List.cons) (g ys) xs := by
171+
induction xs generalizing g with
171172
| nil => rfl
172-
| cons x xs ih =>
173-
simp [expression₁, reverse] at *
174-
simp [List.foldl_cons, flip] at *
175-
sorry
173+
| cons z zs ih =>
174+
exact ih ((fun x => z :: x) ∘ g)
176175

177-
example (xs : List Nat) : expression₂ xs = reverse xs := by
178-
induction xs with
176+
theorem expression₂_eq_reverse_aux (xs ys : List α) :
177+
(List.foldr (λ x f => f ∘ (x :: ·)) id xs) ys = List.foldl (flip List.cons) ys xs := by
178+
induction xs generalizing ys with
179179
| nil => rfl
180-
| cons x xs ih =>
181-
simp [expression₂, reverse] at *
182-
simp [List.foldr_cons, flip] at *
183-
sorry
180+
| cons z zs ih =>
181+
exact ih (z :: ys)
182+
183+
example (xs : List ℕ) : expression₁ xs = reverse xs := by
184+
exact expression₁_eq_reverse_aux id xs []
184185

186+
example (xs : List ℕ) : expression₂ xs = reverse xs := by
187+
exact expression₂_eq_reverse_aux xs []
185188

186189

187190
/-- # Exercise 5.11 -/

0 commit comments

Comments
 (0)