| 1 |
From Coq Require Import List. |
| 2 |
|
| 3 |
Module List. |
| 4 |
Section FoldMap. (* map over a list in the state monad *) |
| 5 |
Context {A B S} (f : A -> S -> B * S). |
| 6 |
Fixpoint foldmap (l : list A) (s : S) : list B * S := |
| 7 |
match l with |
| 8 |
| nil => (nil, s) |
| 9 |
| cons a l => |
| 10 |
let bs_s := foldmap l s in |
| 11 |
let b_s := f a (snd bs_s) in |
| 12 |
(cons (fst b_s) (fst bs_s), snd b_s) |
| 13 |
end. |
| 14 |
Lemma foldmap_ind |
| 15 |
l s |
| 16 |
(P : list A -> list B -> S -> Prop) |
| 17 |
(Hnil : P nil nil s) |
| 18 |
(Hcons : forall (l : list A) (l' : list B) (s : S), |
| 19 |
P l l' s -> forall a, P (cons a l) (cons (fst (f a s)) l') (snd (f a s))) : P l (fst (foldmap l s)) (snd (foldmap l s)). |
| 20 |
Proof using Type. induction l; eauto; []; eapply Hcons; trivial. Qed. |
| 21 |
End FoldMap. |
| 22 |
End List. |