Require Export Arith. (* required for le_plus_minus_r *)

(** Proof of Scott Theorem for fixpoint equations *)
Section Scott.

Variable A:Set.
Variable R:A->A->Prop.

(** order *)
Definition order :=
(forall x:A, (R x x))/\
(forall x y:A, (R x y) -> (R y x) -> x=y)/\
(forall x y z:A, (R x y) -> (R y z) -> (R x z)).

(** complete order *)
Definition monotonic_seq (u:nat->A) := 
  order -> forall n m:nat, n<=m -> (R (u n) (u m)).

Definition complete :=
  forall u:nat->A, monotonic_seq u -> exists a:A, (forall n:nat, (R (u n) a))
.
(** properties of f *)

Definition monotonic (f:A->A) := 
  order -> forall x y:A, (R x y) -> (R (f x) (f y)).

(** limits *)
Definition has_limit (u:nat->A):= complete/\(monotonic_seq u).

Variable lim : forall u:nat->A, has_limit u -> A.

Definition continuous (f:A->A) := 
  monotonic f/\
  (forall u:nat->A, forall hu:(has_limit u),
    forall hu':(has_limit (fun (n:nat)=>(f (u n)))),
      f (lim u hu) = lim (fun (n:nat)=>(f (u n))) hu').

Definition ex_min := exists a:A, forall x:A, (R a x).

Definition has_fixpoints (f:A->A) := exists x:A, f x = x.

Definition o (A B C:Set)(f:A->B)(g:B->C) := (fun (x:A) => g(f x)).
Implicit Arguments o [ A B C ].

Definition F := 
  fun f : A -> A =>
    fix F (n : nat) : A -> A :=
      match n with
	| O => fun x : A => x
	| S p => o f (F p)
      end.

Lemma f_F_f : forall f:A->A, 
  forall n:nat, forall x:A,f (F f n x)=(F f (S n) x).
intros f n;elim n.
simpl; unfold o;simpl; intros x; trivial.
intros p IH x.
simpl; unfold o; simpl.
rewrite IH.
simpl; unfold o; simpl.
trivial.
Qed.

Lemma monotonic_lemma : 
  forall f:A->A, (monotonic f) -> 
    order ->
    forall bottom:A, (forall x:A, (R bottom x)) ->
      forall i:nat, (R (F f i bottom) (F f (S i) bottom)).
intros f Hf Horder bottom Hb i; elim i.
simpl; unfold o; simpl.
apply Hb.
intros i0 IH0.
rewrite <- f_F_f.
rewrite <- f_F_f.
apply Hf.
apply Horder.
apply IH0.
Qed.

Lemma monotonic_lemma2 : 
  forall f:A->A, (monotonic f) -> 
    order ->
    forall bottom:A, (forall x:A, (R bottom x)) ->
      forall i:nat, forall j:nat, (R (F f i bottom) (F f (plus i j) bottom)).
intros f Hf (Hrefl,(Hantisym,Htrans)) bottom Hb i j; elim j.
rewrite <- plus_n_O.
apply Hrefl.
intros j0 IH0.
rewrite <- plus_n_Sm.
apply Htrans with (y:=(F f (i + j0) bottom)).
apply IH0.
apply monotonic_lemma; unfold order;intuition.
Qed.

Lemma monotonic_monotonic_seq_bottom : 
  forall (f:A->A), 
    (monotonic f) -> 
    order ->
    forall bottom:A, (forall x:A, (R bottom x)) ->
      (monotonic_seq (fun (n:nat) => (F f n bottom))).
intros f Hf Horder bottom Hb.
unfold monotonic_seq.
intros (Hrefl,(Hantisym,Htrans)).
intros n m Hnm.
rewrite <- (le_plus_minus_r n m Hnm).
apply monotonic_lemma2; try assumption.
Qed.

Lemma monotonic_monotonic_seq_bottom2 :
  forall f:A->A, monotonic f -> 
    forall u:nat->A, (monotonic_seq u) -> 
      (monotonic_seq (fun (n:nat) => (f (u n)))).
intros f Hf u Hu.
unfold monotonic_seq.
intros Horder.
intros n m Hnm.
apply Hf; intuition.
Qed.

Axiom ext : forall A B:Set, 
  forall f:A->B, forall g:A->B, (forall x:A, f x=g x) -> f=g.

Axiom lim_n_Sn : 
  forall f:A->A, forall x:A, 
    forall Hu:(has_limit (fun (n:nat) => (F f n) x)),
      forall Hu':(has_limit (fun (n:nat) => (F f (S n)) x)),
	(lim (fun (n:nat) => (F f n) x) Hu)=(lim (fun (n:nat) => (F f (S n)) x) Hu').

Lemma has_limit_Ffnbottom: forall f:A->A, forall bottom:A, 
  order/\complete/\(forall x:A, (R bottom x)) /\ (continuous f) ->
  (has_limit (fun (n:nat) => (F f n) bottom)).
intros f bottom (Horder,(Hcomplete,(Hbottom, Hcontinuous))).
unfold has_limit.
split; [trivial| idtac].
apply monotonic_monotonic_seq_bottom; unfold continuous in Hcontinuous;intuition.
Qed.

Lemma scott : 
  forall f:A->A, order/\complete/\ex_min/\(continuous f)->has_fixpoints f.
intros f (Horder,(Hcomplete,(Hex_min,Hcontinuous))).
elim Hex_min.
intros bottom.
intros Hbottom.
unfold has_fixpoints.
cut (has_limit (fun (n:nat) => (F f n) bottom)).
intros Hu'.
exists (lim (fun (n:nat) => (F f n) bottom) Hu').
unfold continuous in Hcontinuous; elim Hcontinuous.
intros Hmonotonic Hcontinuous2.
cut (has_limit (fun n : nat => (f (F f n bottom)))).
intros Hu.
rewrite (Hcontinuous2 (fun n : nat => F f n bottom) Hu' Hu). 
generalize Hu.
replace (fun n : nat => f (F f n bottom))
  with (fun n : nat => (F f (S n) bottom)).
clear Hu; intros Hu.
rewrite (lim_n_Sn f bottom Hu' Hu).
trivial.
apply ext.
intros x; rewrite f_F_f; trivial.
(* second cut *)
unfold has_limit.
split; [trivial| idtac].
apply (monotonic_monotonic_seq_bottom2 f Hmonotonic (fun (n:nat) => (F f n bottom))).
apply monotonic_monotonic_seq_bottom; intuition.
(* first cut *)
apply has_limit_Ffnbottom; intuition.
Qed.

Axiom to_limit :  
  forall u:nat->A, monotonic_seq u -> forall h:has_limit u,
    forall x:A, (forall n:nat,(R (u n) x)) -> (R (lim u h) x).

Lemma smallest_fixpoint : 
forall f:A->A, forall bottom:A, 
  order/\complete/\(forall x:A, (R bottom x)) /\ (continuous f) ->
    forall h:(has_limit (fun (n:nat) => (F f n bottom))), 
      forall x:A, f x = x -> (R (lim (fun (n:nat) =>(F f n bottom)) h) x).
intros f bottom (Horder,(Hcomplete,(Hb,Hf))).
intros h x Hfix.
apply (to_limit (fun n : nat => F f n bottom)).
elim Hf; intros Hf1 Hf2.
apply (monotonic_monotonic_seq_bottom f Hf1 Horder bottom Hb).
intros n; elim n.
simpl; apply Hb.
intros n0 IH0.
rewrite <- f_F_f.
rewrite <- Hfix.
elim Hf; intros Hf1 Hf2; apply Hf1;intuition.
Qed.

End Scott.


(* 
 Local Variables: 
 mode: coq 
 coq-prog-name: "/home/magaud/adk/coq-8.0pl2/bin/coqtop -q -emacs"
 compile-command: "make Scott-Tarki.vo"
 End:
*)
