Section Elt.
Variable elt : Type.
Notation eqk := (eqk (elt:=elt)).
Notation eqke := (eqke (elt:=elt)).
Notation MapsTo := (MapsTo (elt:=elt)).
Notation In := (In (elt:=elt)).
Notation NoDupA := (NoDupA eqk).
(** * [empty] *)
Definition empty : t elt := nil.
Definition Empty m := forall (a : key)(e:elt), ~ MapsTo a e m.
Hint Resolve empty_1 : core.
(** * [is_empty] *)
Definition is_empty (l : t elt) : bool := if l then true else false.
(** * [mem] *)
Function mem (k : key) (s : t elt) {struct s} : bool :=
match s with
| nil => false
| (k',_) :: l => if X.eq_dec k k' then true else mem k l
end.
(** * [find] *)
Function find (k:key) (s: t elt) {struct s} : option elt :=
match s with
| nil => None
| (k',x)::s' => if X.eq_dec k k' then Some x else find k s'
end.
(* Not part of the exported specifications, used later for [combine]. *)
(** * [add] *)
Function add (k : key) (x : elt) (s : t elt) {struct s} : t elt :=
match s with
| nil => (k,x) :: nil
| (k',y) :: l => if X.eq_dec k k' then (k,x)::l else (k',y)::add k x l
end.
(* Not part of the exported specifications, used later for [combine]. *)
(** * [remove] *)
Function remove (k : key) (s : t elt) {struct s} : t elt :=
match s with
| nil => nil
| (k',x) :: l => if X.eq_dec k k' then l else (k',x) :: remove k l
end.
(** * [elements] *)
Definition elements (m: t elt) := m.
(** * [fold] *)
Function fold (A:Type)(f:key->elt->A->A)(m:t elt) (acc : A) {struct m} :  A :=
match m with
| nil => acc
| (k,e)::m' => fold f m' (f k e acc)
end.
(** * [equal] *)
Definition check (cmp : elt -> elt -> bool)(k:key)(e:elt)(m': t elt) :=
match find k m' with
| None => false
| Some e' => cmp e e'
end.
Definition submap (cmp : elt -> elt -> bool)(m m' : t elt) : bool :=
fold (fun k e b => andb (check cmp k e m') b) m true.
Definition equal (cmp : elt -> elt -> bool)(m m' : t elt) : bool :=
andb (submap cmp m m') (submap (fun e' e => cmp e e') m' m).
Definition Submap cmp m m' :=
(forall k, In k m -> In k m') /\
(forall k e e', MapsTo k e m -> MapsTo k e' m' -> cmp e e' = true).
Definition Equivb cmp m m' :=
(forall k, In k m <-> In k m') /\
(forall k e e', MapsTo k e m -> MapsTo k e' m' -> cmp e e' = true).
(** Specification of [equal] *)

Lemma equal_1 : forall m (Hm:NoDupA m) m' (Hm': NoDupA m') cmp,
Equivb cmp m m' -> equal cmp m m' = true.