Module SPWEqP := WEqPropertiesOn SetOfPairsOfPositiveOrderedType SP.
Definition pick_line (s : SS.elt) (sp : SP.t) :=
SP.exists_ (fun p => (S.mem (fstpp p) s) && (S.mem (sndpp p) s)) sp.
Definition exists_witness (f : SS.elt -> bool) (s : SS.t) : option SS.elt :=
SS.choose (SS.filter f s).
Definition pick_lines_aux (s1 : SS.elt)
(ss : SS.t)
(sp : SP.t)
: (option (SS.elt * SS.elt)) :=
match ((exists_witness (fun s2 => let i := S.inter s1 s2 in
pick_line i sp)) ss) with
| None => None
| Some s2 => Some(s1,s2)
end.
Definition pick_lines (ss : SS.t) (sp : SP.t)
: (option (SS.elt * SS.elt)) :=
match (exists_witness (fun s =>
match (pick_lines_aux s (SS.remove s ss) sp) with
| None => false
| _ => true
end) ss) with
| None => None
| Some s1 => pick_lines_aux s1 (SS.remove s1 ss) sp
end.
Definition eqop (p1 p2 : option SS.elt) :=
match p1,p2 with
| None, None => True
| Some s1, Some s2 => True
| _, _ => False
end.
Definition eqopp (p1 p2 : option (SS.elt * SS.elt)) :=
match p1,p2 with
| None, None => True
| Some s1, Some s2 => True
| _, _ => False
end.
Function identify_lines (ss : SS.t) (sp : SP.t) {measure SS.cardinal ss}
: SS.t :=
let lines := pick_lines ss sp in
match lines with
|None => ss
|Some (s1,s2) => let auxsetofsets := SS.remove s2 (SS.remove s1 ss) in
let auxset := S.union s1 s2 in
let newss := SS.add auxset auxsetofsets in
identify_lines newss sp
end.
Definition ss_ok (ss : SS.t) (interp: positive -> COLTpoint) :=
forall s, SS.mem s ss = true ->
forall p1 p2 p3, S.mem p1 s && S.mem p2 s && S.mem p3 s = true ->
CTCol (interp p1) (interp p2) (interp p3).
Definition sp_ok (sp : SP.t) (interp: positive -> COLTpoint) :=
forall p, SP.mem p sp = true -> interp (fstpp p) <> interp (sndpp p).

Lemma identify_lines_ok : forall ss sp interp,
ss_ok ss interp -> sp_ok sp interp ->
ss_ok (identify_lines ss sp) interp.