Section gfp.
(* We develop the theory of Kleene's greatest fixpoint for binary relations and establish the fact that the gfp is an equivalence (under suitable hyps), reached after lia many steps (under lia continuity) and reached after finitely many steps (under finiteness of the domain and preservation of decidability *)
Variable (M : Type).
Implicit Type (R T : M -> M -> Prop).
Notation "R ⊆ T" := (forall x y, R x y -> T x y).
Notation "R 'o' T" := (fun x z => exists y, R x y /\ T y z) (at level 58).
Let incl_trans R S T : R ⊆ S -> S ⊆ T -> R ⊆ T.
Let i_decr n m : n <= m -> i m ⊆ i n.
Let gfp_refl : I ⊆ gfp.
Let gfp_sym : sym gfp ⊆ gfp.
Let gfp_trans : gfp o gfp ⊆ gfp.
Fact gfp_equiv : equiv _ gfp.
Fact gfp_greatest R : R ⊆ F R -> R ⊆ gfp.
Let gfp_fix1 : F gfp ⊆ gfp.
(* This is ω-continuity *)
Definition gfp_continuous := forall (s : nat -> M -> M -> Prop),
(forall n m, n <= m -> s m ⊆ s n)
-> (fun x y => forall n, F (s n) x y) ⊆ F (fun x y => forall n, s n x y).
Variable HF4 : gfp_continuous.
Let gfp_fix0 : gfp ⊆ F gfp.
Fact gfp_fix x y : F gfp x y <-> gfp x y.
(* For the decidability of gfp, we need the finiteness so that gfp = i n for a sufficiently large n *)
(* A good pair for i (ie n < m and i n ⊆ i m) means gfp is reached  at n *)
Let i_dup n m : n < m -> i n ⊆ i m -> forall k, n <= k -> forall x y, gfp x y <-> i k x y.
(* If there is a good pair below b, then gfp = i b *)
Let gfp_reached b : (exists n m, n < m <= b /\ i n ⊆ i m) -> (forall x y, gfp x y <-> i b x y).
Variable HF6 : finite_t M.     (* Finiteness of the domain *)
(* When M is finite, there is a list [T1;...;Tk] of relations of type M -> M -> Prop which contains every weakly decidable relations upto equivalence.  Hence, by the a generalized version of the PHP (proved w/o assuming discreteness), for n greater than the length of the list for M, one can find a duplicate in the list [i 0; ...;i n] ie a < b <= n such that i a ~ Tu ~ Tv ~ i b  Then one can deduce i n ~ gfp *)
(* As a consequence of been reached after a finite number of steps, gfp is one of the i n (for some computable n) and thus decidable *)

Theorem gfp_decidable : dec gfp.