(* Copyright (C) 2012  G. Gonthier, B. Ziliani, A. Nanevski, D. Dreyer  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  This program 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 GNU General Public License for more details.  You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. *)
From mathcomp
From LemmaOverloading
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
(******************************************************************************)
(*       Module for searching and inserting elements in a list                *)
(******************************************************************************)
Section XFind.
Variable A : Type.
Definition invariant s r i (e : A) := onth r i = Some e /\ prefix s r.
(* Tagging for controlling the search of instances *)
Structure xtagged := XTag {xuntag :> A}.
Definition extend_tag := XTag.
Definition recurse_tag := extend_tag.
Canonical Structure found_tag x := recurse_tag x.
(* Main structure s : input sequence r : output sequence. If elem_of is in the sequence, then it's equal to s, otherwise it's equal to (elem_of :: s) i : output index of elem_of in r *)
Structure xfind (s r : seq A) (i : nat) := XFind {
elem_of :> xtagged;
_ : invariant s r i elem_of}.
Arguments XFind : clear implicits.

(* Specifications: *)
Lemma found_pf x t : invariant (x :: t) (x :: t) 0 x.
Example test A (x1 x2 x3 : A) : onth [:: x1; x2; x3] 2 = Some x3.