(*========================================================== ============================================================ TOPOLOGICAL FMAPS, HMAPS -  WITH TAGS AND EMBEDDINGS  PART 3:  EQUIVALENCES, CHARACTERISTICS, GENUS THEOREM, EULER_POINCARE FORMULA, PLANARITY CRITERIA  COMPLETE: OK!! ============================================================ ==========================================================*)
Require Export Jordan2.
(*========================================================== CONNECTIVITY IN HMAPS : ==========================================================*)
(* Connectivity: Definition in Prop "A� la" Warshall *)
Fixpoint eqc(m:fmap)(x y:dart){struct m}:Prop:=
match m with
V => False
|  I m0 x0 _ _ => x=x0 /\ y=x0 \/ eqc m0 x y
|  L m0 _ x0 y0 =>
eqc m0 x y
\/ eqc m0 x x0 /\ eqc m0 y0 y
\/ eqc m0 x y0 /\ eqc m0 x0 y
end.
(* Decidability of eqc: *)
(* Reflexivity of eqc: *)
(* Symmetry of eqc: *)
(* Transitivity of eqc: *)
(* OK: *)
(* OK: *)
(* OK: *)
(* OK: *)
(* OK: *)
(* OK: *)
(* OK: *)
Definition expf(m:fmap)(x y:dart):Prop:=
inv_hmap m /\ MF.expo m x y.
(*======================================================== NUMBERING AND CHARACTERISTICS: =========================================================*)
Open Scope Z_scope.
Fixpoint nd(m:fmap):Z :=
match m with
V => 0
| I m0 x _ _ => nd m0 + 1
| L m0 _ _ _ => nd m0
end.
(* Number of vertices: *)
Fixpoint nv(m:fmap):Z :=
match m with
V => 0
| I m0 x _ _ => nv m0 + 1
| L m0 zero x y => nv m0
| L m0 one x y => nv m0 - 1
end.
(* Number of edges: *)
Fixpoint ne(m:fmap):Z :=
match m with
V => 0
| I m0 x _ _ => ne m0 + 1
| L m0 zero x y => ne m0 - 1
| L m0 one x y => ne m0
end.
(* Number of faces: *)
Fixpoint nf(m:fmap):Z :=
match m with
V => 0
| I m0 x _ _ => nf m0 + 1
| L m0 zero x y =>
let x_1:= cA_1 m0 one x in
nf m0 +
if expf_dec m0 x_1 y then 1 else -1
| L m0 one x y =>
let y0 := cA m0 zero y in
nf m0 +
if expf_dec m0 x y0 then 1 else -1
end.
(* Number of connected components: *)
Fixpoint nc(m:fmap):Z :=
match m with
V => 0
| I m0 x _ _ => nc m0 + 1
| L m0 _ x y => nc m0 -
if eqc_dec m0 x y then 0 else 1
end.
(* Euler-Poincare characteristic: *)
Definition ec(m:fmap): Z:=
nv m + ne m + nf m - nd m.
(* The Euler-Poincare characteristic is even: OK.  *)
(* GENUS THEOREM: OK *)
Definition genus(m:fmap):= (nc m) - (ec m)/2.
(* OK: *)
Definition planar(m:fmap):= genus m = 0.
(* Rewriting of g = 0 : *)
(* ========================================================== PLANARITY CRITERIA (SUFFICIENT CONDITIONS) =============================================================*)
(* OK: *)
(* OK: *)
(* Definition of "Planarly formed" hypermap: *)
Fixpoint plf(m:fmap):Prop:=
match m with
V => True
| I m0 x _ _ => plf m0
| L m0 zero x y => plf m0 /\
(~eqc m0 x y \/ expf m0 (cA_1 m0 one x) y)
| L m0 one x y => plf m0 /\
(~eqc m0 x y \/ expf m0 x (cA m0 zero y))
end.
(* Constructive Sufficient Condition of planarity: *)
(* plf_EULER-POINCARE FORMULA: *)
(*============================================================== NECESSARY CONDITIONS AND FULL PLANARITY CRITERIA ===============================================================*)
(* OK!:*)
(* OK: weak rcp *)
(* OK: *)
(* OK: *)
(* OK: *)
(* FINALLY: FULL PLANARITY CRITERION: *)
(* OK: *)
(* FINALLY: *)

Theorem planarity_crit_0: forall (m:fmap)(x y:dart),
inv_hmap m -> prec_L m zero x y ->
(planar (L m zero x y) <->
(planar m /\ (~ eqc m x y \/ expf m (cA_1 m one x) y))).