Section Set_of_tuple_of_positive.
Context {Ar : Arity}.
Fixpoint eqList (l1 l2 : list positive) :=
match l1, l2 with
| nil, nil => True
| (hd1 :: tl1), (hd2 :: tl2) => (Pos.eq hd1 hd2) /\ (eqList tl1 tl2)
| _, _ => False
end.
Definition tST := cartesianPower positive (S (S n)).
Definition eqST (cp1 cp2 : tST) :=
eqList (PosSort.sort (CPToList cp1)) (PosSort.sort (CPToList cp2)).
Fixpoint eqbList (l1 l2 : list positive) :=
match l1, l2 with
| nil         , nil          => true
| (hd1 :: tl1), (hd2 :: tl2) => (Pos.eqb hd1 hd2) && (eqbList tl1 tl2)
| _           , _            => false
end.
Definition eqbST (cp1 cp2 : tST) :=
eqbList (PosSort.sort (CPToList cp1)) (PosSort.sort (CPToList cp2)).
Definition ltST (cp1 cp2 : tST) :=
ltList (PosSort.sort (CPToList cp1)) (PosSort.sort (CPToList cp2)).
Fixpoint compareList (l1 l2 : list positive) :=
match l1, l2 with
| nil, nil => Eq
| (hd1 :: tl1), (hd2 :: tl2) => match Pos.compare hd1 hd2 with
| Lt => Lt
| Eq => compareList tl1 tl2
| Gt => Gt
end
| nil, _ => Lt
| _, nil => Gt
end.
Definition compareST (cp1 cp2 : tST) :=
compareList (PosSort.sort (CPToList cp1)) (PosSort.sort (CPToList cp2)).
(* TODO: try to see if using sorted lists would not make the tactic faster. *)
Definition STelt := tST.
Definition STt := list tST.
Definition STempty : STt := nil.
Definition STadd (x : STelt) (s : STt) := cons x s.
Fixpoint STexists_ (f : STelt -> bool) (s : STt) :=
match s with
| nil      => false
| hd :: tl => f hd || STexists_ f tl
end.
Fixpoint STmem elt l :=
match l with
| nil      => false
| hd :: tl => if eqST_dec hd elt then true else STmem elt tl
end.

Lemma STadd_iff : forall (s : STt) (x y : STelt),
STmem y (STadd x s) = true <-> (eqST x y \/ STmem y s = true).