(** This file is part of the Coquelicot formalization of real analysis in Coq: http://coquelicot.saclay.inria.fr/  Copyright (C) 2011-2015 Sylvie Boldo #<br /># Copyright (C) 2011-2015 Catherine Lelay #<br /># Copyright (C) 2011-2015 Guillaume Melquiond  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the COPYING file for more details. *)
(** This file describes iterators on lists. This is mainly used for Riemannn sums. *)
Section Iter.
Context {I T : Type}.
Context (op : T -> T -> T).
Context (x0 : T).
Context (neutral_l : forall x, op x0 x = x).
Context (neutral_r : forall x, op x x0 = x).
Context (assoc : forall x y z, op x (op y z) = op (op x y) z).
Fixpoint iter (l : list I) (f : I -> T) :=
match l with
| nil => x0
| cons h l' => op (f h) (iter l' f)
end.
Definition iter' (l : list I) (f : I -> T) :=
fold_right (fun i acc => op (f i) acc) x0 l.

(* Specifications: *)
Lemma iter_iter' l f :
iter l f = iter' l f.
Lemma iter_cat l1 l2 f :
iter (l1 ++ l2) f = op (iter l1 f) (iter l2 f).
Lemma iter_ext l f1 f2 :
(forall x, In x l -> f1 x = f2 x) ->
iter l f1 = iter l f2.
Lemma iter_comp (l : list I) f (g : I -> I) :
iter l (fun x => f (g x)) = iter (map g l) f.