Require Export XR_Rlt_0_1.
Require Export XR_Rmult_lt_reg_r.

Local Open Scope R_scope.

Lemma Rinv_0_lt_compat : forall r,
  R0 < r ->
  R0 < / r.
Proof.
  intros x h.
  apply Rmult_lt_reg_r with x.
  { exact h. }
  {
    rewrite Rmult_0_l.
    rewrite Rinv_l.
    { exact Rlt_0_1. }
    {
      apply not_eq_sym.
      apply Rlt_not_eq.
      exact h.
    }
  }
Qed.


Require Import Fiat.Common Coq.Arith.Arith Coq.Bool.Bool Coq.Sets.Ensembles.

Class DecideableEnsemble {A} (P : Ensemble A) :=
  { dec : A -> bool;
    dec_decides_P : forall a, dec a = true <-> P a}.

Lemma Decides_false {A} :
  forall (P : Ensemble A)
         (P_dec : DecideableEnsemble P) a,
    dec a = false <-> ~ (P a).
Proof.
  split; unfold not; intros.
  + apply dec_decides_P in H0; congruence.
  + case_eq (dec a); intros; eauto.
    apply dec_decides_P in H0; intuition.
Qed.

Program Instance DecideableEnsemble_gt {A} (f f' : A -> nat)
  : DecideableEnsemble (fun a => f a > f' a) :=
  {| dec a := if le_lt_dec (f a) (f' a) then false else true |}.
Next Obligation.
  intros; find_if_inside; intuition.
Defined.

Program Instance DecideableEnsemble_And
         {A : Type}
         {P P' : Ensemble A}
         {P_dec : DecideableEnsemble P}
         {P'_dec : DecideableEnsemble P'}
: DecideableEnsemble (fun a => P a /\ P' a) :=
  {| dec a := (@dec _ _ P_dec a) && (@dec _ _  P'_dec a) |}.
Next Obligation.
  intros; rewrite <- (@dec_decides_P _ P),
          <- (@dec_decides_P _ P').
  setoid_rewrite andb_true_iff; reflexivity.
Defined.

(* Class used to overload equality test notation (==) in queries. *)
Class Query_eq (A : Type) :=
      {A_eq_dec : forall a a' : A, {a = a'} + {a <> a'}}.

Infix "==" := (A_eq_dec) (at level 1).

Instance Query_eq_nat : Query_eq nat :=
  {| A_eq_dec := eq_nat_dec |}.

Instance Query_eq_bool : Query_eq bool :=
  {| A_eq_dec := bool_dec |}.

Instance Query_eq_list {A}
         (_ : Query_eq A)
  : Query_eq (list A) :=
  {| A_eq_dec := List.list_eq_dec A_eq_dec |}.

Program Instance DecideableEnsemble_NEqDec
         {A B : Type}
         {b_eq_dec : Query_eq B}
         (f f' : A -> B)
: DecideableEnsemble (fun a : A => f a <> f' a) :=
  {| dec a := if A_eq_dec (f a) (f' a) then false else true |}.
Next Obligation.
  intros; find_if_inside; intuition.
Defined.

Program Instance DecideableEnsemble_EqDec {A B : Type}
         (B_eq_dec : Query_eq B)
         (f f' : A -> B)
: DecideableEnsemble (fun a => eq (f a) (f' a)) :=
  {| dec a := if A_eq_dec (f a) (f' a) then true else false |}.
Next Obligation.
  intros; find_if_inside; split; congruence.
Defined.

Program Instance DecideableEnsemble_Not
         {A : Type}
         (P : Ensemble A)
         {P_dec : DecideableEnsemble P}
: DecideableEnsemble (fun a => ~ P a) :=
  {| dec a := negb (dec a) |}.
Next Obligation.
  intros; case_eq (dec a); intros; intuition eauto.
  - discriminate.
  - rewrite dec_decides_P in H; intuition.
  - rewrite <- dec_decides_P in H1; congruence.
Defined.

Program Instance DecideableEnsemble_True
         {A : Type}
: DecideableEnsemble (fun a : A => True) :=
  {| dec a := true |}.
Next Obligation.
  intros; intuition eauto.
Defined.

Program Instance DecideableEnsemble_False
         {A : Type}
: DecideableEnsemble (fun a : A => False) :=
  {| dec a := false |}.
Next Obligation.
  intros; intuition eauto; discriminate.
Defined.

Program Instance DecideableEnsemble_Or
         {A : Type}
         (P P' : Ensemble A)
         {P_dec : DecideableEnsemble P}
         {P'_dec : DecideableEnsemble P'}
: DecideableEnsemble (fun a : A => P a \/ P' a) :=
  {| dec a := if dec (P := P) a then true else dec (P := P') a |}.
Next Obligation.
  intros; case_eq (dec (P := P) a);
  [ rewrite dec_decides_P | rewrite Decides_false];
  intuition.
  setoid_rewrite dec_decides_P in H0; intuition.
  setoid_rewrite dec_decides_P; intuition.
Defined.


Load LFindLoad.
From lfind Require Import LFind.
Unset Printing Notations.
Set Printing Implicit.

From QuickChick Require Import QuickChick.
Inductive natural : Type := Succ : natural -> natural |  Zero : natural.
Derive Show for natural. Derive Arbitrary for natural.  Instance Dec_Eq_natural : Dec_Eq natural. Proof. dec_eq. Qed.
Inductive lst : Type := Cons : natural -> lst -> lst |  Nil : lst.

Inductive tree : Type := Node : natural -> tree -> tree -> tree |  Leaf : tree.

Inductive Pair : Type := mkpair : natural -> natural -> Pair
with Zlst : Type := zcons : Pair -> Zlst -> Zlst |  znil : Zlst.

Fixpoint append (append_arg0 : lst) (append_arg1 : lst) : lst
           := match append_arg0, append_arg1 with
              | Nil, x => x
              | Cons x y, z => Cons x (append y z)
              end.

Fixpoint rev (rev_arg0 : lst) : lst
           := match rev_arg0 with
              | Nil => Nil
              | Cons x y => append (rev y) (Cons x Nil)
              end.

Fixpoint qreva (qreva_arg0 : lst) (qreva_arg1 : lst) : lst
           := match qreva_arg0, qreva_arg1 with
              | Nil, x => x
              | Cons z x, y => qreva x (Cons z y)
              end.

Theorem append_nil: forall (l: lst), append l Nil = l.
Proof.
   induction l.
   { simpl. f_equal. assumption. }
   { simpl. reflexivity. }
Qed.

Theorem append_assoc:
   forall (l1 l2 l3: lst), append l1 (append l2 l3) = append (append l1 l2) l3.
Proof.
   induction l1; induction l2; induction l3; try (simpl; reflexivity).
   { simpl. rewrite <- IHl1. f_equal. }
   { simpl. lfind.  reflexivity.  }
Admitted.

Theorem qreva_append : forall (x y : lst), (qreva x y) = (append (rev x) y).
Proof.
   induction x; induction y; simpl; try reflexivity.
   { rewrite IHx.
   rewrite <- append_assoc.
   f_equal. }
   { rewrite IHx.
   rewrite append_nil.
   reflexivity. }
Qed.

Theorem theorem0 : forall (x : lst), eq (rev x) (qreva x Nil).
Proof.
   intros.
   rewrite qreva_append.
   rewrite append_nil.
   reflexivity.
Qed.


Require Import Arith.

Set Primitive Projections.
Set Universe Polymorphism.
Set Polymorphic Inductive Cumulativity.

Inductive seq {A : Type} (x : A) : A -> SProp := srefl : seq x x.

Notation "x ≡ y" := (seq x y) (at level 70).

(*
Axiom J_seq : forall (A : Type) (x : A) (P : forall y, x ≡ y -> Type),
  P x (srefl _) -> forall y e, P y e.
*)
Lemma J_seq : forall (A : Type) (x : A) (P : forall y, x ≡ y -> Type),
  P x (srefl _) -> forall y e, P y e.
Proof.
intros A x P p y e.
refine (match e in _ ≡ z as e return P _ e with srefl _ => p end).
Defined.

Lemma transp_seq {A : Type} {x : A} (P : A -> Type)
  (a : P x) {y : A} (e : x ≡ y) : P y.
Proof.
refine (match e in _ ≡ z as e return P _ with srefl _ => a end).
Defined.

Lemma J_seqs : forall (A : Type) (x : A) (P : forall y, x ≡ y -> SProp),
  P x (srefl _) -> forall y e, P y e.
Proof.
intros A x P p y e.
refine (match e in _ ≡ z as e return P _ e with srefl _ => p end).
Defined.

Lemma transp_seqs {A : Type} {x : A} (P : A -> SProp)
  (a : P x) {y : A} (e : x ≡ y) : P y.
Proof.
refine (match e in _ ≡ z as e return P _ with srefl _ => a end).
Defined.

Definition ssym {A} {x y : A} (e : x ≡ y) : (y ≡ x).
Proof.
  destruct e. exact (srefl _).
Defined.

Inductive sFalse : SProp :=.
Inductive sTrue : SProp := sI.

(* Proof-irrelevant inequalities *)

Inductive sle (n : nat) : nat -> SProp :=
| sle_n : sle n n
| sle_S : forall m : nat, sle n m -> sle n (S m).

Definition le_to_sle {m n : nat} :
  le m n -> sle m n.
Proof.
intro H. induction H as [| m' n' IH].
- exact (sle_n m).
- exact (sle_S m m' IH).
Defined.

Definition sle_to_le {m n : nat} :
  sle m n -> le m n.
Proof.
intro H.
destruct (dec_le m n) as [Hd | Hd].
- exact Hd.
- assert (sFalse) as Hf.
  { induction H as [| m' n' IHsle].
    - destruct (Hd (le_n m)).
    - apply IHsle. intro H'. exact (Hd (le_S m m' H')). }
  destruct Hf.
Defined.

Definition slt (n m : nat) : SProp := sle (S n) m.

(* Finite sets *)

Record finset (n : nat) : Set := mkFinset {
    val : nat ;
    eps_val : slt val n ;
}.

Definition finzero {p : nat} : finset (S p).
Proof.
unshelve refine (mkFinset _ _ _).
+ exact 0.
+ apply le_to_sle. apply le_n_S. exact (le_0_n p).
Defined.

Definition finsucc {p : nat} (n : finset p) : finset (S p).
Proof.
destruct n as [n nε].
unshelve refine (mkFinset _ _ _).
+ exact (S n).
+ apply le_to_sle. apply le_n_S. apply sle_to_le. exact nε.
Defined.

Definition finmatch {m : nat} (P : finset (S m) -> Type)
  (P0 : P finzero) (PS : forall n, P (finsucc n)) : forall n, P n.
Proof.
refine (fun n => _). destruct n as [v ε]. revert ε.
destruct v.
- refine (fun ε => P0).
- refine (fun ε => PS {| val := v ; eps_val := le_to_sle (le_S_n _ _ (sle_to_le ε)) |}).
Defined.

Definition sfinmatch {m : nat} (P : finset (S m) -> SProp)
  (P0 : P finzero) (PS : forall n, P (finsucc n)) : forall n, P n.
Proof.
refine (fun n => _). destruct n as [v ε]. revert ε.
destruct v.
- refine (fun ε => P0).
- refine (fun ε => PS {| val := v ; eps_val := le_to_sle (le_S_n _ _ (sle_to_le ε)) |}).
Defined.

Arguments val {_}.
Arguments eps_val {_}.

(* Arrows *)

Definition cube (n : nat) : Set := finset n -> bool.

Definition le_cube {n : nat} (p q : cube n) : SProp :=
  forall m : finset n, if p m then (if q m then sTrue else sFalse) else sTrue.

Definition increasing {m n : nat} (f : cube m -> cube n) : SProp :=
  forall x y : cube m, le_cube x y -> le_cube (f x) (f y).

Record cube_arr (m n : nat) : Set := mkCubeArr {
    arr : cube m -> cube n ;
    eps_arr : increasing arr ;
}.

Arguments arr {_ _}.
Arguments eps_arr {_ _}.

Definition seq_cube_arr {p q : nat} {α β : cube_arr p q} :
    α.(arr) ≡ β.(arr) -> α ≡ β.
Proof.
intro H.
destruct α as [α αε]. destruct β as [β βε].
simpl in H. destruct H. refine (srefl _).
Defined.

(* Composition *)

Definition comp_increasing {m n n' : nat} {f : cube m -> cube n} {g : cube n -> cube n'} :
  increasing f -> increasing g -> increasing (fun x => g (f x)).
Proof.
unshelve refine (fun Hf Hg x y Hxy => _).
unshelve refine (Hg (f x) (f y) _).
unshelve refine (Hf x y Hxy).
Defined.

Definition comp {m n n' : nat} (f : cube_arr m n) (g : cube_arr n n') : cube_arr m n'.
Proof.
unshelve refine (mkCubeArr _ _ _ _).
+ unshelve refine (fun x => g.(arr) (f.(arr) x)).
+ unshelve refine (comp_increasing f.(eps_arr) g.(eps_arr)).
Defined.

(* Identity *)

Definition id_cube (n : nat) : cube_arr n n.
Proof.
unshelve refine (mkCubeArr _ _ _ _).
+ unshelve refine (fun x => x).
+ unshelve refine (fun x y Hxy => Hxy).
Defined.

(* Definitional category *)
(* equations hold by reflexivity *)

Lemma comp_assoc (m m' n n' : nat) (f : cube_arr m m') (g : cube_arr m' n) (h : cube_arr n n') :
  comp (comp f g) h = comp f (comp g h).
Proof.
reflexivity.
Qed.

Lemma comp_id_left (m n : nat) (f : cube_arr m n) :
  comp f (id_cube n) = f.
Proof.
reflexivity.
Qed.

Lemma comp_id_right (m n : nat) (f : cube_arr m n) :
  comp (id_cube m) f = f.
Proof.
reflexivity.
Qed.

(* Notations *)

Definition ℙ@{} : Set := nat.

Definition le@{} p q := cube_arr p q.

Definition le_id {p} : le p p := id_cube p.
Definition le_cmp {p q r} (α : le p q) (β : le q r) : le p r := comp α β.

Notation "p ≤ q" := (le p q) (at level 70, no associativity).
Notation "'!'" := (@le_id _).
Notation "α ∘ β" := (@le_cmp _ _ _ β α) (at level 35).

Notation "α · x" := (fun r β => x r (α ∘ β)) (at level 40).

(* useful arrows *)

Definition side_0 {p : ℙ} : p ≤ S p.
Proof.
unshelve econstructor.
- unshelve refine (fun c => _).
  refine (finmatch (fun _ => bool) _ _).
  + exact false.
  + refine (fun n => c n).
- intros c d Hcd.
  refine (sfinmatch _ _ _) ; simpl.
  + exact sI.
  + refine (fun n => _). refine (Hcd _).
Defined.

Definition side_1 {p : ℙ} : p ≤ S p.
Proof.
unshelve econstructor.
- unshelve refine (fun c => _).
  refine (finmatch (fun _ => bool) _ _).
  + exact true.
  + refine (fun n => c n).
- intros c d Hcd.
  refine (sfinmatch _ _ _) ; simpl.
  + exact sI.
  + refine (fun n => _). refine (Hcd _).
Defined.

Definition squish {p : ℙ} : S p ≤ p.
Proof.
unshelve refine (mkCubeArr _ _ _ _).
- refine (fun c n => c (finsucc n)).
- intros c d Hcd n.
  exact (Hcd (finsucc n)).
Defined.

Definition antisquish {p : ℙ} : S p ≤ 1.
Proof.
unshelve econstructor.
- refine (fun c n => c finzero).
- intros c d Hcd n.
  exact (Hcd finzero).
Defined.

Definition promote {p q : ℙ} (α : q ≤ p) : S q ≤ S p.
Proof.
unshelve refine (mkCubeArr _ _ _ _).
- unshelve refine (fun c => _).
  refine (finmatch (fun _ => bool) _ _).
  + exact (c finzero).
  + apply (arr α).
    refine (fun n => c (finsucc n)).
- intros c d Hcd.
  refine (sfinmatch _ _ _) ; simpl.
  + eapply Hcd.
  + eapply (eps_arr α).
    refine (fun m => _). eapply Hcd.
Defined.

Definition merge {p q : ℙ} (α : q ≤ p) (β : q ≤ 1) : q ≤ S p.
Proof.
unshelve econstructor.
- refine (fun c => _).
  refine (finmatch (fun _ => bool) _ _).
  + refine (β.(arr) c finzero).
  + refine (fun n => α.(arr) c n).
- intros c d Hcd.
  refine (sfinmatch _ _ _) ; simpl.
  + exact (β.(eps_arr) c d Hcd finzero).
  + refine (fun n => α.(eps_arr) c d Hcd n).
Defined.

Definition vee {p : ℙ} : S (S p) ≤ S p.
Proof.
unshelve econstructor.
- refine (fun c => _).
  refine (finmatch (fun _ => bool) _ _).
  + exact (orb (c finzero) (c (finsucc finzero))).
  + refine (fun n' => _).
    exact (c (finsucc (finsucc n'))).
- intros c d Hcd.
  refine (sfinmatch _ _ _) ; simpl.
  + pose proof (Hcd finzero) as H0. pose proof (Hcd (finsucc finzero)) as H1.
    revert H0 H1. destruct (c finzero) ; destruct (c (finsucc finzero)) ; destruct (d finzero) ; destruct (d (finsucc finzero)) ; simpl ; easy.
  + refine (fun n => _). eapply Hcd.
Defined.

Definition wedge {p : ℙ} : S (S p) ≤ S p.
Proof.
unshelve econstructor.
- refine (fun c => _).
  refine (finmatch (fun _ => bool) _ _).
  + exact (andb (c finzero) (c (finsucc finzero))).
  + refine (fun n' => _).
    exact (c (finsucc (finsucc n'))).
- intros c d Hcd.
  refine (sfinmatch _ _ _) ; simpl.
  + pose proof (Hcd finzero) as H0. pose proof (Hcd (finsucc finzero)) as H1.
    revert H0 H1. destruct (c finzero) ; destruct (c (finsucc finzero)) ; destruct (d finzero) ; destruct (d (finsucc finzero)) ; simpl ; easy.
  + refine (fun n => _). eapply Hcd.
Defined.

(* All of the following hold definitionally *)

Lemma arrow_eq_0 {p : ℙ} : (@squish p) ∘ side_0 = !.
Proof.
reflexivity.
Defined.

Lemma arrow_eq_1 {p : ℙ} : (@squish p) ∘ side_1 = !.
Proof.
reflexivity.
Defined.

Lemma arrow_eq_2 {p q : ℙ} (α : q ≤ p) :
  promote α ∘ side_0 = side_0 ∘ α.
Proof.
reflexivity.
Defined.

Lemma arrow_eq_3 {p q : ℙ} (α : q ≤ p) :
  promote α ∘ side_1 = side_1 ∘ α.
Proof.
reflexivity.
Defined.

Lemma arrow_eq_4 {p q : ℙ} (α : q ≤ p) :
  squish ∘ promote α = α ∘ squish.
Proof.
reflexivity.
Defined.

Lemma arrow_eq_5 {p q : ℙ} (α : q ≤ p) (β : q ≤ 1) :
  squish ∘ (merge α β) ≡ α.
Proof.
reflexivity.
Defined.

Lemma arrow_eq_6 {p q r : ℙ} (α : q ≤ p) (β : q ≤ 1) (γ : r ≤ q) :
  (merge α β) ∘ γ ≡ merge (α ∘ γ) (β ∘ γ).
Proof.
reflexivity.
Defined.

Lemma arrow_eq_7 {p : ℙ} : squish ∘ squish ≡ squish ∘ (@wedge p).
Proof.
reflexivity.
Defined.

Lemma arrow_eq_8 {p : ℙ} : squish ∘ squish ≡ squish ∘ (@vee p).
Proof.
reflexivity.
Defined.

(* these ones, however, requires η for nat.
  without it, we can only get an extensional equality. tough luck *)

Lemma arrow_eq_fail1 {p q : ℙ} (α : q ≤ S p) :
  forall c n, (merge (squish ∘ α) (antisquish ∘ α)).(arr) c n ≡ α.(arr) c n.
Proof.
refine (fun c n => _). revert n.
refine (sfinmatch _ _ _) ; reflexivity.
Defined.

Lemma arrow_eq_fail2 {p : ℙ} :
  forall c n, (wedge ∘ (@side_1 (S p))).(arr) c n ≡ !.(arr) c n.
Proof.
refine (fun c n => _). revert n.
refine (sfinmatch _ _ _) ; reflexivity.
Defined.

Lemma arrow_eq_fail3 {p : ℙ} :
  forall c n, (wedge ∘ (@side_0 (S p))).(arr) c n ≡ (side_0 ∘ squish).(arr) c n.
Proof.
refine (fun c n => _). revert n.
refine (sfinmatch _ _ _) ; reflexivity.
Defined.

Lemma arrow_eq_fail4 {p : ℙ} :
  forall c n, (wedge ∘ (promote (@side_1 p))).(arr) c n ≡ !.(arr) c n.
Proof.
refine (fun c n => _). revert n.
refine (sfinmatch _ _ _).
- unfold vee ; unfold promote ; unfold side_1 ; simpl.
  destruct (c finzero) ; easy.
- easy.
Defined.

Lemma arrow_eq_fail5 {p : ℙ} :
  forall c n, (wedge ∘ (promote (@side_0 p))).(arr) c n ≡ (side_0 ∘ squish).(arr) c n.
Proof.
refine (fun c n => _). revert n.
refine (sfinmatch _ _ _).
- unfold vee ; unfold promote ; unfold side_0 ; simpl.
  destruct (c finzero) ; easy.
- easy.
Defined.

(* Axioms for interval-types, à la CubicalTT *)

Definition i0 {p} : p ≤ 1.
Proof.
unshelve econstructor.
- refine (fun c n => false).
- refine (fun c d Hcd n => _). easy.
Defined.

Definition i1 {p} : p ≤ 1.
Proof.
unshelve econstructor.
- refine (fun c n => true).
- refine (fun c d Hcd n => _). easy.
Defined.

Definition itype (p : ℙ) (A : Type) (x y : A) : Type.
Admitted.

Definition itype_in {p} {A : Type} (z : p ≤ 1 -> A) :
  itype p A (z i0) (z i1).
Admitted.

Definition itype_out {p} {A : Type} {x y : A} :
  itype p A x y -> p ≤ 1 -> A.
Admitted.

Definition itype_inout :
  (fun p A (x : p ≤ 1 -> A) y => itype_out (itype_in x) y) ≡ fun p A x y => x y.
Admitted.

Definition itype_out0 :
  (fun p (X : Type) (y z : X) (x : itype p X y z) => itype_out x i0) ≡ (fun p X y z x => y).
Admitted.

Definition itype_out1 :
  (fun p (X : Type) (y z : X) (x : itype p X y z) => itype_out x i1) ≡ (fun p X y z x => z).
Admitted.


Require Import Setoid.

(* forall_e Hypothese t: introduit une nouvelle hypothèse qui est ce
   qu'on obtient quand on applique Hypothese de la forme
   forall x:T, ...
   au terme t:T   *)
Ltac forall_e H t := generalize (H t); intro.

(* Alternativement:
   specialize Hypothese with t
   specialize Hypotheses with (x:=t)
   specialize Hypothese with t as Ht
   specialize Hypothese with (x:=t) as Ht
   les deux premières formes remplacent l'hypothèse;
   specifier la variable à lier avec (x:=t) permet de sélectionner
   la variable x s'il y a plusieurs quantificateurs consécutifs,
   forall z y x, ...  *)

Section Vehicules.

(* Prenez le temps de vous familiariser avec les hypothèses, et de les
   interpréter mentalement: cela vous aidera pour vos preuves *)

  Variable vehicule: Type.

  Variable voiture: vehicule -> Prop.
  Variable velo: vehicule -> Prop.
  Variable rouge: vehicule -> Prop.
  Variable noir: vehicule -> Prop.
  Variable blanc: vehicule -> Prop.
  Variable ford: vehicule -> Prop.
  Variables totoche T: vehicule.

  Hypothesis Htot: rouge totoche.
  Hypothesis HT: ford T.
  Hypothesis Hford: forall v, ford v -> noir v.

  Hypothesis Hcouleur: forall v, rouge v \/ noir v \/ blanc v.
  Hypothesis Hrn: forall v, ~(rouge v /\ noir v).
  Hypothesis Hnb: forall v, ~(noir v /\ blanc v).
  Hypothesis Hrb: forall v, ~(rouge v /\ blanc v).

  Hypothesis Htype: forall v, voiture v \/ velo v.
  Hypothesis Hvv: forall v, ~(voiture v /\ velo v).

  Variable propulsion: Type.
  Variable categorie: vehicule -> propulsion -> Prop.
  Variable essence : propulsion.
  Variable electrique: propulsion.
  Variable pedale: propulsion.

  Hypothesis Hve: ~(exists v, velo v /\ categorie v essence).

(* Un exemple *)

  Lemma Exemple: noir T.
  Proof.
    forall_e Hford T.
    apply H; assumption.
  Qed.

(* Un second exemple *)
  Lemma Exemple_bis: forall v:vehicule, ~voiture v -> velo v.
  Proof.
    intros.
    forall_e Htype v.
    destruct H0.
    - contradiction.
    - assumption.
  Qed.

(* Les exercices commencent ici *)

(* Dans chaque Lemme, remplacer "Admitted" par votre propre script de preuve.
   N'oubliez pas de terminer chaque preuve par "Qed". *)

  Lemma Exercice1: ~ ford totoche.
  Proof.
  Admitted.

  Lemma Exercice2: forall v:vehicule, noir v -> ~(rouge v \/ blanc v).
  Proof.
  Admitted.

  Lemma Exercice3: (forall v, velo v-> rouge v) -> (forall w, ford w -> voiture w).
  Proof.
  Admitted.

  Lemma Exercice4: ~(totoche=T).
  Proof.
  Admitted.

  Lemma Exercice5: (exists v, blanc v /\ voiture v) -> exists v, voiture v /\ ~ford v.
  Proof.
  Admitted.

  Lemma Exercice6 : forall v,  categorie v essence -> voiture v.
  Proof.
  Admitted.

End Vehicules.

(* Fin des exercices *)


Require Setoid.
Require Import PeanoNat Le Gt Minus Bool Lt List.
Require Import Omega.

Section ReDun.

  Variable A : Type.

  Variable decA : forall (a b : A), {a = b}+{a <> b}.

  Theorem NoDup_count_occ' l:
    NoDup l <-> (forall x:A, In x l -> count_occ decA l x = 1).
  Proof.
    rewrite (NoDup_count_occ decA).
    setoid_rewrite (count_occ_In decA) at 1.
    unfold gt, lt in *.
    split; intros H x; specialize (H x);
    set (n := count_occ decA l x) in *; clearbody n; omega.
  Qed.
End ReDun.


Require Import List.

Require Import Defns.

(**
   * Standard equality
 **)

(** decidability of equality for nameless syntax **)

Lemma eq_eTy_dec : forall A B : eTy, {A = B} + {A <> B}
  with eq_ete_dec : forall M N : ete, {M = N} + {M <> N}.
Proof.
  (* lemma 1 *)
  (intros **).
  decide equality.
  (apply eq_tcon).
  decide equality.
  decide equality.
  (* lemma 2 *)
  (intros **).
  decide equality.
  (apply eq_con).
  decide equality.
  decide equality.
  decide equality.
Defined.

Lemma eq_sTy_dec : forall tau1 tau2 : sTy, {tau1 = tau2} + {tau1 <> tau2}.
Proof.
  (intros **).
  decide equality.
  (apply eq_tcon).
Defined.

Lemma eq_eK : forall K L : eK, {K = L} + {K <> L}.
Proof.
  decide equality.
  (apply eq_eTy_dec).
  decide equality.
  decide equality.
Defined.

Lemma eq_sK :
  forall kappa1 kappa2 : sK, {kappa1 = kappa2} + {kappa1 <> kappa2}.
Proof.
  decide equality.
  (apply eq_sTy_dec).
Defined.


Require Import Coq.Structures.Equalities.
Require Import Coq.Structures.OrderedType.

Require Import CoLoR.Util.Vector.VecUtil.

Require Import MathClasses.interfaces.canonical_names.
Require Import MathClasses.interfaces.abstract_algebra.
Require Import MathClasses.theory.rings.
Require Import MathClasses.theory.abs.
Require Import MathClasses.interfaces.orders.
Require Import MathClasses.orders.minmax.

Require Import Helix.Util.Misc.

Parameter CarrierA : Type.

Notation avector n := (vector CarrierA n) (only parsing).

Set Implicit Arguments.
Class CarrierDefs : Type :=
  {
  (* Relations *)
  CarrierAe    :> Equiv CarrierA
  ; CarrierAle :> Le CarrierA
  ; CarrierAlt :> Lt CarrierA
  (* Decidability *)
  ; CarrierAltdec     :> forall x y: CarrierA, Decision (x < y)
  ; CarrierAequivdec :> forall x y: CarrierA, Decision (x = y)
  (* Constants *)
  ; CarrierAz :> Zero CarrierA
  ; CarrierA1 :> One CarrierA
  (* operations *)
  ; CarrierAplus :> Plus CarrierA
  ; CarrierAmult :> Mult CarrierA
  ; CarrierAneg  :> Negate CarrierA
  ; CarrierAabs  :> @Abs CarrierA CarrierAe CarrierAle CarrierAz CarrierAneg
  }.

Class CarrierProperties `{CADEFS: CarrierDefs}: Prop :=
  {
  (* Equality *)
  CarrierAsetoid :> @Setoid CarrierA CarrierAe
  (* Ring *)
  ; CarrierAr :> Ring CarrierA
  (* Orders *)
  ; CarrierAto  :> @TotalOrder CarrierA CarrierAe CarrierAle
  ; CarrierASSO :> @StrictSetoidOrder CarrierA CarrierAe CarrierAlt
  ; CarrierFPSO :> @FullPseudoOrder CarrierA CarrierAe (@strong_setoids.default_apart CarrierA CarrierAe) CarrierAle CarrierAlt
  }.

Section CarrierAExtraProperties.
  Context `{CAPROPS: @CarrierProperties CADEFS}.

  Global Instance CarrierAledec: forall x y: CarrierA, Decision (x ≤ y).
  Proof.
    (* This proofs requires FullPseudoOrder *)
    intros x y.
    unfold Decision.
    destruct (CarrierAltdec x y), (CarrierAequivdec x y).
    - left. apply orders.eq_le. assumption.
    - left. apply orders.not_lt_le_flip, orders.lt_flip. assumption.
    - left. apply orders.eq_le. assumption.
    - right. apply orders.lt_not_le_flip.
      apply orders.not_lt_apart_lt_flip; auto.
  Qed.

  Global Instance CarrierA_min_proper: Proper((=) ==> (=) ==> (=)) (@min CarrierA CarrierAle CarrierAledec).
  Proof. typeclasses eauto. Qed.

  Global Instance CarrierA_max_proper: Proper((=) ==> (=) ==> (=)) (@max CarrierA CarrierAle CarrierAledec).
  Proof. typeclasses eauto. Qed.

  (* Zero and One are two distrinct values *)
  Axiom CarrierA_Z_neq_One: CarrierAz ≠ CarrierA1.

  Add Ring RingA: (stdlib_ring_theory CarrierA).

  Global Instance CarrierAPlus_proper:
    Proper ((=) ==> (=) ==> (=)) CarrierAplus.
  Proof.
    solve_proper.
  Qed.

  Global Instance CarrierAmult_proper:
    Proper((=) ==> (=) ==> (=)) CarrierAmult.
  Proof.
    solve_proper.
  Qed.

  Global Instance CarrierAabs_proper : Proper ((=) ==> (=)) abs.
  Proof.
    typeclasses eauto.
  Qed.

  Global Instance CommutativeMonoid_plus_zero:
    @MathClasses.interfaces.abstract_algebra.CommutativeMonoid CarrierA _ plus zero.
  Proof.
    typeclasses eauto.
  Qed.

  (* Poor man's minus *)
  Definition sub {T:Type}
             `{Plus T}
             `{Negate T}
    : T → T → T := plus∘negate.

  (* The following is not strictly necessary as it follows from "properness" of composition, negation, and addition operations. Unfortunately Coq 8.4 class resolution could not find these automatically so we hint it by adding implicit instance. *)
  Global Instance CarrierA_sub_proper:
    Proper ((=) ==> (=) ==> (=)) sub.
  Proof.
    intros a b Ha x y Hx .
    unfold sub, compose.
    rewrite Hx, Ha.
    reflexivity.
  Qed.

  Definition Zless (a b: CarrierA): CarrierA
    := if CarrierAltdec a b then one else zero.

  Global Instance Zless_proper:
    Proper ((=) ==> (=) ==> (=)) Zless.
  Proof.
    unfold Proper.
    intros a a' aE z z' zE.
    unfold Zless.
    destruct (CarrierAltdec a z), (CarrierAltdec a' z'); auto.
    rewrite aE, zE in l; contradiction.
    rewrite <- aE, <- zE in l; contradiction.
  Qed.

  Definition CarrierA_beq (a b: CarrierA) : bool := bool_decide (a=b).

  Lemma CarrierA_eqb_equiv : forall x y : CarrierA, CarrierA_beq x y ≡ true ↔ equiv x y.
  Proof.
    intros x y.
    split.
    -
      intros H.
      unfold CarrierA_beq in H.
      apply bool_decide_true, H.
    -
      intros H.
      unfold CarrierA_beq.
      apply bool_decide_true, H.
  Qed.

End CarrierAExtraProperties.

Ltac decide_CarrierA_equality E NE :=
  let E' := fresh E in
  let NE' := fresh NE in
  match goal with
  | [ |- @equiv CarrierA CarrierAe ?A ?B ] => destruct (CarrierAequivdec A B) as [E'|NE']
  end.


(* External import(s). *)
Require Import Coq.Bool.Bool.
Require Import Coq.Lists.List.
Import         ListNotations.
(* Internal import(s). *)
Require Import Basics.Identifier.
Require Import Basics.Aux.
Require Import Basics.Map.
Require Import Languages.WHILE.Syntax.

Definition isval (e:exp): bool :=
  match e with
    | ENat  _ => true
    | EBool _ => true
    | _       => false
  end.

Definition runStep (config:cfg): option cfg :=
  match config with
  | [EmptyStack | env] => None

  | [SKIP ~> s | env]  => Some $ [s | env]

    (* Assignments *)
  | [x ::= e ~> s | env] =>
      if (isval e) then
        match e with
        | ENat i => Some $ [s | x |-> i IN env]
        | _      => None
        end
      else
        Some $ [e ~> x ::= EHole ~> s | env]
  | [ENat i ~> x ::= EHole ~> s | env] =>
      Some $ [x ::= i ~> s | env]

  (* Identifier lookup. *)
  | [EId k ~> s | env] =>
      Some $ [find k env ~> s | env]

  (* Addition. *)
  | [e1 + e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (ENat i1, ENat i2) => Some $ [ENat (plus i1 i2) ~> s | env]
        | _                  => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | ENat _  => Some $ [e2 ~> e1 + EHole ~> s | env]
        | _       => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole + e2 ~> s | env]
  | [ENat i1 ~> EHole + e2 ~> s | env] =>
      Some $ [i1 + e2 ~> s | env]
  | [ENat i2 ~> e1 + EHole ~> s | env] =>
      Some $ [e1 + i2 ~> s | env]

  (* Subtraction. *)
  | [e1 - e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (ENat i1, ENat i2) => Some $ [ENat (minus i1 i2) ~> s | env]
        | _                  => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | ENat _  => Some $ [e2 ~> e1 - EHole ~> s | env]
        | _       => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole - e2 ~> s | env]
  | [ENat i1 ~> EHole - e2 ~> s | env] =>
      Some $ [i1 - e2 ~> s | env]
  | [ENat i2 ~> e1 - EHole ~> s | env] =>
      Some $ [e1 - i2 ~> s | env]

  (* Multiplication. *)
  | [e1 * e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (ENat i1, ENat i2) => Some $ [ENat (mult i1 i2) ~> s | env]
        | _                  => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | ENat i1 => Some $ [e2 ~> e1 * EHole ~> s | env]
        | _       => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole * e2 ~> s | env]
  | [ENat i1 ~> EHole * e2 ~> s | env] =>
      Some $ [i1 * e2 ~> s | env]
  | [ENat i2 ~> e1 * EHole ~> s | env] =>
      Some $ [e1 * i2 ~> s | env]

  (* Division. *)
  | [e1 / e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (ENat i1, ENat i2) => Some $ [ENat (Nat.div i1 i2) ~> s | env]
        | _                  => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | ENat _ => Some $ [e2 ~> e1 / EHole ~> s | env]
        | _      => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole / e2 ~> s | env]
  | [ENat i1 ~> EHole / e2 ~> s | env] =>
      Some $ [i1 / e2 ~> s | env]
  | [ENat i2 ~> e1 / EHole ~> s | env] =>
      Some $ [e1 / i2 ~> s | env]

  (* Logical negation. *)
  | [!e1 ~> s | env] =>
      if (isval e1) then
        match e1 with
        | EBool b1 => Some $ [EBool (negb b1) ~> s | env]
        | _        => Some $ [s | env]
        end
      else
        Some $ [e1 ~> !EHole ~> s | env]
  | [EBool e1 ~> !EHole ~> s | env] =>
      Some $ [!e1 ~> s | env]

  (* Equality. *)
  | [e1 ==? e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (ENat i1, ENat i2) => Some $ [EBool (Nat.eqb i1 i2) ~> s | env]
        | _                  => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | ENat _ => Some $ [e2 ~> e1 ==? EHole ~> s | env]
        | _      => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole ==? e2 ~> s | env]
  | [ENat e1 ~> EHole ==? e2 ~> s | env] =>
      Some $ [e1 ==? e2 ~> s | env]
  | [ENat e2 ~> e1 ==? EHole ~> s | env] =>
      Some $ [e1 ==? e2 ~> s | env]

  (* Less-than. *)
  | [e1 <? e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (ENat i1, ENat i2) => Some $ [EBool (Nat.ltb i1 i2) ~> s | env]
        | _                  => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | ENat _  => Some $ [e2 ~> e1 <? EHole ~> s | env]
        | _       => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole <? e2 ~> s | env]
  | [ENat e1 ~> EHole <? e2 ~> s | env] =>
      Some $ [e1 <? e2 ~> s | env]
  | [ENat e2 ~> e1 <? EHole ~> s | env] =>
      Some $ [e1 <? e2 ~> s | env]

  (* Logical and. *)
  | [e1 & e2 ~> s | env] =>
      if (isval e1 && isval e2) then
        match (e1, e2) with
        | (EBool b1, EBool b2) => Some $ [EBool (andb b1 b2) ~> s | env]
        | _                    => Some $ [s | env]
        end
      else if (isval e1) then
        match e1 with
        | EBool _ => Some $ [e2 ~> e1 & EHole ~> s | env]
        | _       => Some $ [s | env]
        end
      else
        Some $ [e1 ~> EHole & e2 ~> s | env]
  | [EBool b1 ~> EHole & e2 ~> s | env] =>
      Some $ [(EBool b1) & e2 ~> s | env]
  | [EBool b2 ~> e1 & EHole ~> s | env] =>
      Some $ [e1 & (EBool b2) ~> s | env]

  (* If-then-else. *)
  | [ITE e1 THEN e2 ELSE e3 ETI ~> s | env] =>
      if (isval e1) then
        match e1 with
        | EBool true  => Some $ [e2 ~> s | env]
        | EBool false => Some $ [e3 ~> s | env]
        | _           => Some $ [s | env]
        end
      else
        Some $ [e1 ~> ITE EHole THEN e2 ELSE e3 ETI ~> s | env]
  | [EBool b1 ~> ITE EHole THEN e2 ELSE e3 ETI ~> s | env] =>
      Some $ [ITE (EBool b1) THEN e2 ELSE e3 ETI ~> s | env]

  (* While loop. *)
  | [WHILE e1 DO e2 END ~> s | env] =>
      Some $ [ITE e1 THEN (e2; WHILE e1 DO e2 END) ELSE SKIP ETI ~> s | env]

  (* Sequence. *)
  | [e1;e2 ~> s | env] =>
      if (isval e1) then
        Some $ [e2 ~> s | env]
      else
         match e1 with
         | EHole => Some $ [e2 ~> s | env]
         | _     => Some $ [e1 ~> EHole;e2 ~> s | env]
         end
  | [ENat e1 ~> EHole;e2 ~> s | env] =>
      Some $ [(ENat e1);e2 ~> s | env]
  | [EBool b1 ~> EHole;e2 ~> s | env] =>
      Some $ [(EBool b1);e2 ~> s | env]

  | [EHole ~> s | env] => Some $ [s | env]

  | cfg => None
  end.

Fixpoint runNSteps (n:nat) (config:cfg): option cfg :=
  match n with
  | O    => ret config
  | S n' => runStep config >>=
      fun config' => runNSteps n' config'
  end.


Require Import Coq.Arith.PeanoNat.
Require Import Coq.Logic.Eqdep Coq.Logic.Eqdep_dec Coq.Program.Equality.

(** * Equalities on dependent types *)

Theorem eq_rect_nat_double : forall T (a b c : nat) x ab bc,
  eq_rect b T (eq_rect a T x b ab) c bc = eq_rect a T x c (eq_trans ab bc).
Proof.
  intros.
  destruct ab.
  destruct bc.
  rewrite (UIP_dec Nat.eq_dec (eq_trans eq_refl eq_refl) eq_refl).
  simpl.
  auto.
Qed.

#[global] Hint Rewrite eq_rect_nat_double.
#[global] Hint Rewrite <- (eq_rect_eq_dec Nat.eq_dec).

Ltac generalize_proof :=
    match goal with
    | [ |- context[eq_rect _ _ _ _ ?H ] ] => generalize H
    end.

Ltac eq_rect_simpl :=
  unfold eq_rec_r, eq_rec;
  repeat rewrite eq_rect_nat_double;
  repeat rewrite <- (eq_rect_eq_dec Nat.eq_dec).

Ltac destruct_existT :=
  repeat match goal with
           | [H: existT _ _ _ = existT _ _ _ |- _] =>
             (apply Eqdep.EqdepTheory.inj_pair2 in H; subst)
         end.

Lemma eq_rect_word_offset_helper : forall a b c,
  a = b -> c + a = c + b.
Proof.
  intros; congruence.
Qed.

Lemma eq_rect_word_mult_helper : forall a b c,
  a = b -> a * c = b * c.
Proof.
  intros; congruence.
Qed.

Lemma existT_eq_rect:
  forall (X: Type) (P: X -> Type) (x1 x2: X) (H1: P x1) (Hx: x1 = x2),
    existT P x2 (eq_rect x1 P H1 x2 Hx) =
    existT P x1 H1.
Proof.
  intros; subst; reflexivity.
Qed.

Lemma existT_eq_rect_eq:
  forall (X: Type) (P: X -> Type) (x1 x2: X)
         (H1: P x1) (H2: P x2) (Hx: x1 = x2),
    H2 = eq_rect _ P H1 _ Hx ->
    existT P x1 H1 = existT P x2 H2.
Proof.
  intros; subst; reflexivity.
Qed.

Lemma eq_rect_existT_eq:
  forall (X: Type) (P: X -> Type) (x1 x2: X)
         (H1: P x1) (H2: P x2) (Hx: x1 = x2)
         (Hex: existT P x1 H1 = existT P x2 H2),
    H2 = eq_rect _ P H1 _ Hx.
Proof.
  intros; subst.
  subst; destruct_existT.
  reflexivity.
Qed.


(*******************************************************************)
(*  This is part of RelationAlgebra, it is distributed under the   *)
(*    terms of the GNU Lesser General Public License version 3     *)
(*              (see file LICENSE for more details)                *)
(*                                                                 *)
(*  Copyright 2012: Damien Pous. (CNRS, LIP - ENS Lyon, UMR 5668)  *)
(*******************************************************************)

(** * sums: finite sums, a la ssreflect *)

(** this tiny module extends [sups] to the setting of monoids (it is
   a separate module just to avoid [sups] to depend on [monoid]) *)

Require Import monoid.
Require Export sups.

(** in the setting of monoids, we prefer the "sum" notation *)

Notation "\sum_ ( i \in l ) f" := (@sup (mor _ _) _ (fun i => f) l)
  (at level 41, f at level 41, i, l at level 50,
    format "'[' \sum_ ( i \in  l ) '/  '  f ']'"): ra_terms.

Notation "\sum_ ( i < n ) f" := (\sum_(i \in seq n) f)
  (at level 41, f at level 41, i, n at level 50,
    format "'[' \sum_ ( i < n ) '/  '  f ']'"): ra_terms.

(** [dot] distributes over sums *)
Lemma dotxsum `{laws} `{BSL<<l} I J n m p (f: I -> X m n) (x: X p m):
  x * (\sum_(i\in J) f i) == \sum_(i\in J) (x * f i).
Proof. apply f_sup_weq. apply dotx0. intros; apply dotxpls. Qed.

Lemma dotsumx `{laws} `{BSL<<l} I J n m p (f: I -> X n m) (x: X m p):
  (\sum_(i\in J) f i) * x == \sum_(i\in J) (f i * x).
Proof. dual @dotxsum. Qed.

(** converse commutes with sums *)
Lemma cnvsum `{laws} `{BSL+CNV<<l} I J n m (f: I -> X n m):
  (\sum_(i\in J) f i)` == \sum_(i\in J) (f i`).
Proof. apply f_sup_weq. apply cnv0. apply cnvpls. Qed.


Require Import Core.Basics.
Require Import Coq.Program.Equality.
Require Import Coq.Logic.ProofIrrelevance.

Record Tagged T := {
  tagged_data :> Type ;
  get_tag :> tagged_data -> T ;
}.

Arguments tagged_data [T].
Arguments get_tag [T _].

Lemma tagged_eq {T : Type} {X Y : Tagged T}
  (p : tagged_data X = tagged_data Y)
  (q : ∀ i : X, get_tag i = get_tag (rew [idmap] p in i))
  : X = Y.
Proof.
  destruct X, Y; cbn in *.
  destruct p. f_equal.
  funext i. rewrite q. reflexivity.
Defined.

Definition tagged_mkeq {T} (X Y : Tagged T) :=
  { p : tagged_data X = tagged_data Y | ∀ i : X, get_tag i = get_tag (rew [idmap] p in i) }.

Lemma tagged_eq_iff {T} (X Y : Tagged T) :
  tagged_mkeq X Y <-> X = Y.
Proof.
  split; intros.
  - destruct H. refine (tagged_eq _ _). apply e.
  - subst. now exists eq_refl.
Qed.

Definition tagged_morphism
    [A B : Type] (fᵢ : A -> B) (X : Tagged A) (Y : Tagged B) :=
  { f : X -> Y | ∀ x : X, get_tag (f x) = fᵢ (get_tag x) }.

Definition tagged_morphism_map
  [A B : Type] (fᵢ : A -> B) (X : Tagged A) (Y : Tagged B)
  : tagged_morphism fᵢ X Y -> (X -> Y) :=
@proj1_sig _ _.

Coercion tagged_morphism_map : tagged_morphism >-> Funclass.

Definition tagged_morphism_commutes
    [A B : Type] [fᵢ : A -> B] [X Y]
    : ∀ (f : tagged_morphism fᵢ X Y) (i : X), Y (f i) = fᵢ (X i) :=
  @proj2_sig _ _.

Lemma tagged_morphism_eq {T1 T2} {X : Tagged T1} {Y : Tagged T2} {t : T1 -> T2}
  (f g : tagged_morphism t X Y)
  : proj1_sig f = proj1_sig g -> f = g.
Proof.
  intros. destruct f, g.
  apply subset_eq_compat.
  apply H.
Qed.

Definition tagged_morphism_compose
  [A B C : Type] [gᵢ : B -> C] [fᵢ : A -> B] [X Y Z]
  (g : tagged_morphism gᵢ Y Z) (f : tagged_morphism fᵢ X Y)
  : tagged_morphism (gᵢ ∘ fᵢ) X Z.
Proof.
  exists (g ∘ f).
  intros. refine (eq_trans _ _).
  * apply tagged_morphism_commutes.
  * apply f_equal. apply tagged_morphism_commutes.
Defined.

Definition tagged_sum [A] (T1 T2 : Tagged A) : Tagged A := {|
  tagged_data := T1 + T2 ;
  get_tag x :=
    match x with
    | inl x => T1 x
    | inr x => T2 x
    end ;
|}.

Definition tagged_empty A : Tagged A :=
  Build_Tagged A Empty_set void.

(* Lemma oaknbsdokasnd [A] (f : A -> A) (X : Tagged A) (extid : ∀ x, f x = x) :
  tagged_morphism f X X = tagged_morphism idmap X X.
Proof.
  unfold tagged_morphism. setoid_rewrite extid.
Qed. *)


(* Copyright (c) 2006, 2011-2012, Adam Chlipala
 *
 * This work is licensed under a
 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
 * Unported License.
 * The license text is available at:
 *   http://creativecommons.org/licenses/by-nc-nd/3.0/
 *)

(* begin hide *)
Require Import Coq.Lists.List.
Require Import Arith List.

Require Import CpdtTactics Coinductive.

Set Implicit Arguments.
(* end hide *)

(** %\chapter{General Recursion}% *)

(** Termination of all programs is a crucial property of Gallina.  Non-terminating programs introduce logical inconsistency, where any theorem can be proved with an infinite loop.  Coq uses a small set of conservative, syntactic criteria to check termination of all recursive definitions.  These criteria are insufficient to support the natural encodings of a variety of important programming idioms.  Further, since Coq makes it so convenient to encode mathematics computationally, with functional programs, we may find ourselves wanting to employ more complicated recursion in mathematical definitions.

   What exactly are the conservative criteria that we run up against?  For _recursive_ definitions, recursive calls are only allowed on _syntactic subterms_ of the original primary argument, a restriction known as%\index{primitive recursion}% _primitive recursion_.  In fact, Coq's handling of reflexive inductive types (those defined in terms of functions returning the same type) gives a bit more flexibility than in traditional primitive recursion, but the term is still applied commonly.  In Chapter 5, we saw how _co-recursive_ definitions are checked against a syntactic guardedness condition that guarantees productivity.

   Many natural recursion patterns satisfy neither condition.  For instance, there is our simple running example in this chapter, merge sort.  We will study three different approaches to more flexible recursion, and the latter two of the approaches will even support definitions that may fail to terminate on certain inputs, without any up-front characterization of which inputs those may be.

   Before proceeding, it is important to note that the problem here is not as fundamental as it may appear.  The final example of Chapter 5 demonstrated what is called a%\index{deep embedding}% _deep embedding_ of the syntax and semantics of a programming language.  That is, we gave a mathematical definition of a language of programs and their meanings.  This language clearly admitted non-termination, and we could think of writing all our sophisticated recursive functions with such explicit syntax types.  However, in doing so, we forfeit our chance to take advantage of Coq's very good built-in support for reasoning about Gallina programs.  We would rather use a%\index{shallow embedding}% _shallow embedding_, where we model informal constructs by encoding them as normal Gallina programs.  Each of the three techniques of this chapter follows that style. *)

(** * Well-Founded Recursion *)

(** The essence of terminating recursion is that there are no infinite chains of nested recursive calls.  This intuition is commonly mapped to the mathematical idea of a%\index{well-founded relation}% _well-founded relation_, and the associated standard technique in Coq is%\index{well-founded recursion}% _well-founded recursion_.  The syntactic-subterm relation that Coq applies by default is well-founded, but many cases demand alternate well-founded relations.  To demonstrate, let us see where we get stuck on attempting a standard merge sort implementation. *)

Section mergeSort.
  Variable A : Type.
  Variable le : A -> A -> bool.

  (** We have a set equipped with some "less-than-or-equal-to" test. *)

  (** A standard function inserts an element into a sorted list, preserving sortedness. *)

  Fixpoint insert (x : A) (ls : list A) : list A :=
    match ls with
      | nil => x :: nil
      | h :: ls' =>
	if le x h
	  then x :: ls
	  else h :: insert x ls'
    end.

  (** We will also need a function to merge two sorted lists.  (We use a less efficient implementation than usual, because the more efficient implementation already forces us to think about well-founded recursion, while here we are only interested in setting up the example of merge sort.) *)

  Fixpoint merge (ls1 ls2 : list A) : list A :=
    match ls1 with
      | nil => ls2
      | h :: ls' => insert h (merge ls' ls2)
    end.

  (** The last helper function for classic merge sort is the one that follows, to split a list arbitrarily into two pieces of approximately equal length. *)

  Fixpoint split (ls : list A) : list A * list A :=
    match ls with
      | nil => (nil, nil)
      | h :: nil => (h :: nil, nil)
      | h1 :: h2 :: ls' =>
	let (ls1, ls2) := split ls' in
	  (h1 :: ls1, h2 :: ls2)
    end.

  (** Now, let us try to write the final sorting function, using a natural number "[<=]" test [leb] from the standard library.
[[
  Fixpoint mergeSort (ls : list A) : list A :=
    if leb (length ls) 1
      then ls
      else let lss := split ls in
	merge (mergeSort (fst lss)) (mergeSort (snd lss)).
]]

<<
Recursive call to mergeSort has principal argument equal to
"fst (split ls)" instead of a subterm of "ls".
>>

The definition is rejected for not following the simple primitive recursion criterion.  In particular, it is not apparent that recursive calls to [mergeSort] are syntactic subterms of the original argument [ls]; indeed, they are not, yet we know this is a well-founded recursive definition.

To produce an acceptable definition, we need to choose a well-founded relation and prove that [mergeSort] respects it.  A good starting point is an examination of how well-foundedness is formalized in the Coq standard library. *)

  Print well_founded.
  (** %\vspace{-.15in}% [[
well_founded =
fun (A : Type) (R : A -> A -> Prop) => forall a : A, Acc R a
]]

The bulk of the definitional work devolves to the%\index{accessibility relation}\index{Gallina terms!Acc}% _accessibility_ relation [Acc], whose definition we may also examine. *)

(* begin hide *)
(* begin thide *)
Definition Acc_intro' := Acc_intro.
(* end thide *)
(* end hide *)

  Print Acc.
(** %\vspace{-.15in}% [[
Inductive Acc (A : Type) (R : A -> A -> Prop) (x : A) : Prop :=
    Acc_intro : (forall y : A, R y x -> Acc R y) -> Acc R x
]]

In prose, an element [x] is accessible for a relation [R] if every element "less than" [x] according to [R] is also accessible.  Since [Acc] is defined inductively, we know that any accessibility proof involves a finite chain of invocations, in a certain sense that we can make formal.  Building on Chapter 5's examples, let us define a co-inductive relation that is closer to the usual informal notion of "absence of infinite decreasing chains." *)

  CoInductive infiniteDecreasingChain A (R : A -> A -> Prop) : stream A -> Prop :=
  | ChainCons : forall x y s, infiniteDecreasingChain R (Cons y s)
    -> R y x
    -> infiniteDecreasingChain R (Cons x (Cons y s)).

(** We can now prove that any accessible element cannot be the beginning of any infinite decreasing chain. *)

(* begin thide *)
  Lemma noBadChains' : forall A (R : A -> A -> Prop) x, Acc R x
    -> forall s, ~infiniteDecreasingChain R (Cons x s).
    induction 1; crush;
      match goal with
        | [ H : infiniteDecreasingChain _ _ |- _ ] => inversion H; eauto
      end.
  Qed.

(** From here, the absence of infinite decreasing chains in well-founded sets is immediate. *)

  Theorem noBadChains : forall A (R : A -> A -> Prop), well_founded R
    -> forall s, ~infiniteDecreasingChain R s.
    destruct s; apply noBadChains'; auto.
  Qed.
(* end thide *)

(** Absence of infinite decreasing chains implies absence of infinitely nested recursive calls, for any recursive definition that respects the well-founded relation.  The [Fix] combinator from the standard library formalizes that intuition: *)

  Check Fix.
(** %\vspace{-.15in}%[[
Fix
     : forall (A : Type) (R : A -> A -> Prop),
       well_founded R ->
       forall P : A -> Type,
       (forall x : A, (forall y : A, R y x -> P y) -> P x) ->
       forall x : A, P x
]]

A call to %\index{Gallina terms!Fix}%[Fix] must present a relation [R] and a proof of its well-foundedness.  The next argument, [P], is the possibly dependent range type of the function we build; the domain [A] of [R] is the function's domain.  The following argument has this type:
[[
       forall x : A, (forall y : A, R y x -> P y) -> P x
]]

This is an encoding of the function body.  The input [x] stands for the function argument, and the next input stands for the function we are defining.  Recursive calls are encoded as calls to the second argument, whose type tells us it expects a value [y] and a proof that [y] is "less than" [x], according to [R].  In this way, we enforce the well-foundedness restriction on recursive calls.

The rest of [Fix]'s type tells us that it returns a function of exactly the type we expect, so we are now ready to use it to implement [mergeSort].  Careful readers may have noticed that [Fix] has a dependent type of the sort we met in the previous chapter.

Before writing [mergeSort], we need to settle on a well-founded relation.  The right one for this example is based on lengths of lists. *)

  Definition lengthOrder (ls1 ls2 : list A) :=
    length ls1 < length ls2.

  (** We must prove that the relation is truly well-founded.  To save some space in the rest of this chapter, we skip right to nice, automated proof scripts, though we postpone introducing the principles behind such scripts to Part III of the book.  Curious readers may still replace semicolons with periods and newlines to step through these scripts interactively. *)

  Hint Constructors Acc.

  Lemma lengthOrder_wf' : forall len, forall ls, length ls <= len -> Acc lengthOrder ls.
    unfold lengthOrder; induction len; crush.
  Defined.

  Theorem lengthOrder_wf : well_founded lengthOrder.
    red; intro; eapply lengthOrder_wf'; eauto.
  Defined.

  (** Notice that we end these proofs with %\index{Vernacular commands!Defined}%[Defined], not [Qed].  Recall that [Defined] marks the theorems as %\emph{%#<i>#transparent#</i>#%}%, so that the details of their proofs may be used during program execution.  Why could such details possibly matter for computation?  It turns out that [Fix] satisfies the primitive recursion restriction by declaring itself as _recursive in the structure of [Acc] proofs_.  This is possible because [Acc] proofs follow a predictable inductive structure.  We must do work, as in the last theorem's proof, to establish that all elements of a type belong to [Acc], but the automatic unwinding of those proofs during recursion is straightforward.  If we ended the proof with [Qed], the proof details would be hidden from computation, in which case the unwinding process would get stuck.

     To justify our two recursive [mergeSort] calls, we will also need to prove that [split] respects the [lengthOrder] relation.  These proofs, too, must be kept transparent, to avoid stuckness of [Fix] evaluation.  We use the syntax [@foo] to reference identifier [foo] with its implicit argument behavior turned off.  (The proof details below use Ltac features not introduced yet, and they are safe to skip for now.) *)

  Lemma split_wf : forall len ls, 2 <= length ls <= len
    -> let (ls1, ls2) := split ls in
      lengthOrder ls1 ls /\ lengthOrder ls2 ls.
    unfold lengthOrder; induction len; crush; do 2 (destruct ls; crush);
      destruct (le_lt_dec 2 (length ls));
        repeat (match goal with
                  | [ _ : length ?E < 2 |- _ ] => destruct E
                  | [ _ : S (length ?E) < 2 |- _ ] => destruct E
                  | [ IH : _ |- context[split ?L] ] =>
                    specialize (IH L); destruct (split L); destruct IH
                end; crush).
  Defined.

  Ltac split_wf := intros ls ?; intros; generalize (@split_wf (length ls) ls);
    destruct (split ls); destruct 1; crush.

  Lemma split_wf1 : forall ls, 2 <= length ls
    -> lengthOrder (fst (split ls)) ls.
    split_wf.
  Defined.

  Lemma split_wf2 : forall ls, 2 <= length ls
    -> lengthOrder (snd (split ls)) ls.
    split_wf.
  Defined.

  Hint Resolve split_wf1 split_wf2.

  (** To write the function definition itself, we use the %\index{tactics!refine}%[refine] tactic as a convenient way to write a program that needs to manipulate proofs, without writing out those proofs manually.  We also use a replacement [le_lt_dec] for [leb] that has a more interesting dependent type.  (Note that we would not be able to complete the definition without this change, since [refine] will generate subgoals for the [if] branches based only on the _type_ of the test expression, not its _value_.) *)

  Definition mergeSort : list A -> list A.
(* begin thide *)
    refine (Fix lengthOrder_wf (fun _ => list A)
      (fun (ls : list A)
        (mergeSort : forall ls' : list A, lengthOrder ls' ls -> list A) =>
        if le_lt_dec 2 (length ls)
	  then let lss := split ls in
            merge (mergeSort (fst lss) _) (mergeSort (snd lss) _)
	  else ls)); subst lss; eauto.
  Defined.
(* end thide *)
End mergeSort.

(** The important thing is that it is now easy to evaluate calls to [mergeSort]. *)

Eval compute in mergeSort leb (1 :: 2 :: 36 :: 8 :: 19 :: nil).
(** [= 1 :: 2 :: 8 :: 19 :: 36 :: nil] *)

(** %\smallskip{}%Since the subject of this chapter is merely how to define functions with unusual recursion structure, we will not prove any further correctness theorems about [mergeSort]. Instead, we stop at proving that [mergeSort] has the expected computational behavior, for all inputs, not merely the one we just tested. *)

(* begin thide *)
Theorem mergeSort_eq : forall A (le : A -> A -> bool) ls,
  mergeSort le ls = if le_lt_dec 2 (length ls)
    then let lss := split ls in
      merge le (mergeSort le (fst lss)) (mergeSort le (snd lss))
    else ls.
  intros; apply (Fix_eq (@lengthOrder_wf A) (fun _ => list A)); intros.

  (** The library theorem [Fix_eq] imposes one more strange subgoal upon us.  We must prove that the function body is unable to distinguish between "self" arguments that map equal inputs to equal outputs.  One might think this should be true of any Gallina code, but in fact this general%\index{extensionality}% _function extensionality_ property is neither provable nor disprovable within Coq.  The type of [Fix_eq] makes clear what we must show manually: *)

  Check Fix_eq.
(** %\vspace{-.15in}%[[
Fix_eq
     : forall (A : Type) (R : A -> A -> Prop) (Rwf : well_founded R)
         (P : A -> Type)
         (F : forall x : A, (forall y : A, R y x -> P y) -> P x),
       (forall (x : A) (f g : forall y : A, R y x -> P y),
        (forall (y : A) (p : R y x), f y p = g y p) -> F x f = F x g) ->
       forall x : A,
       Fix Rwf P F x = F x (fun (y : A) (_ : R y x) => Fix Rwf P F y)
]]

  Most such obligations are dischargeable with straightforward proof automation, and this example is no exception. *)

  match goal with
    | [ |- context[match ?E with left _ => _ | right _ => _ end] ] => destruct E
  end; simpl; f_equal; auto.
Qed.
(* end thide *)

(** As a final test of our definition's suitability, we can extract to OCaml. *)

Extraction mergeSort.

(** <<
let rec mergeSort le x =
  match le_lt_dec (S (S O)) (length x) with
  | Left ->
    let lss = split x in
    merge le (mergeSort le (fst lss)) (mergeSort le (snd lss))
  | Right -> x
>>

  We see almost precisely the same definition we would have written manually in OCaml!  It might be a good exercise for the reader to use the commands we saw in the previous chapter to clean up some remaining differences from idiomatic OCaml.

  One more piece of the full picture is missing.  To go on and prove correctness of [mergeSort], we would need more than a way of unfolding its definition.  We also need an appropriate induction principle matched to the well-founded relation.  Such a principle is available in the standard library, though we will say no more about its details here. *)

Check well_founded_induction.
(** %\vspace{-.15in}%[[
well_founded_induction
     : forall (A : Type) (R : A -> A -> Prop),
       well_founded R ->
       forall P : A -> Set,
       (forall x : A, (forall y : A, R y x -> P y) -> P x) ->
       forall a : A, P a
]]

  Some more recent Coq features provide more convenient syntax for defining recursive functions.  Interested readers can consult the Coq manual about the commands %\index{Function}%[Function] and %\index{Program Fixpoint}%[Program Fixpoint]. *)

(** * A Non-Termination Monad Inspired by Domain Theory *)

(** The key insights of %\index{domain theory}%domain theory%~\cite{WinskelDomains}% inspire the next approach to modeling non-termination.  Domain theory is based on _information orders_ that relate values representing computation results, according to how much information these values convey.  For instance, a simple domain might include values "the program does not terminate" and "the program terminates with the answer 5."  The former is considered to be an _approximation_ of the latter, while the latter is _not_ an approximation of "the program terminates with the answer 6."  The details of domain theory will not be important in what follows; we merely borrow the notion of an approximation ordering on computation results.

   Consider this definition of a type of computations. *)

Section computation.
  Variable A : Type.
  (** The type [A] describes the result a computation will yield, if it terminates.

     We give a rich dependent type to computations themselves: *)

  Definition computation :=
    {f : nat -> option A
      | forall (n : nat) (v : A),
	f n = Some v
	-> forall (n' : nat), n' >= n
	  -> f n' = Some v}.

  (** A computation is fundamentally a function [f] from an _approximation level_ [n] to an optional result.  Intuitively, higher [n] values enable termination in more cases than lower values.  A call to [f] may return [None] to indicate that [n] was not high enough to run the computation to completion; higher [n] values may yield [Some].  Further, the proof obligation within the subset type asserts that [f] is _monotone_ in an appropriate sense: when some [n] is sufficient to produce termination, so are all higher [n] values, and they all yield the same program result [v].

  It is easy to define a relation characterizing when a computation runs to a particular result at a particular approximation level. *)

  Definition runTo (m : computation) (n : nat) (v : A) :=
    proj1_sig m n = Some v.

  (** On top of [runTo], we also define [run], which is the most abstract notion of when a computation runs to a value. *)

  Definition run (m : computation) (v : A) :=
    exists n, runTo m n v.
End computation.

(** The book source code contains at this point some tactics, lemma proofs, and hint commands, to be used in proving facts about computations.  Since their details are orthogonal to the message of this chapter, I have omitted them in the rendered version. *)
(* begin hide *)

Hint Unfold runTo.

Ltac run' := unfold run, runTo in *; try red; crush;
  repeat (match goal with
            | [ _ : proj1_sig ?E _ = _ |- _ ] =>
              match goal with
                | [ x : _ |- _ ] =>
                  match x with
                    | E => destruct E
                  end
              end
            | [ |- context[match ?M with exist _ _ => _ end] ] => let Heq := fresh "Heq" in
              case_eq M; intros ? ? Heq; try rewrite Heq in *; try subst
            | [ _ : context[match ?M with exist _ _ => _ end] |- _ ] => let Heq := fresh "Heq" in
              case_eq M; intros ? ? Heq; try rewrite Heq in *; subst
            | [ H : forall n v, ?E n = Some v -> _,
                _ : context[match ?E ?N with Some _ => _ | None => _ end] |- _ ] =>
              specialize (H N); destruct (E N); try rewrite (H _ (eq_refl _)) by auto; try discriminate
            | [ H : forall n v, ?E n = Some v -> _, H' : ?E _ = Some _ |- _ ] => rewrite (H _ _ H') by auto
          end; simpl in *); eauto 7.

Ltac run := run'; repeat (match goal with
                            | [ H : forall n v, ?E n = Some v -> _
                                |- context[match ?E ?N with Some _ => _ | None => _ end] ] =>
                              specialize (H N); destruct (E N); try rewrite (H _ (eq_refl _)) by auto; try discriminate
                          end; run').

Lemma ex_irrelevant : forall P : Prop, P -> exists n : nat, P.
  exists 0; auto.
Qed.

Hint Resolve ex_irrelevant.

Require Import Max.

Theorem max_spec_le : forall n m, n <= m /\ max n m = m \/ m <= n /\ max n m = n.
  induction n; destruct m; simpl; intuition;
    specialize (IHn m); intuition.
Qed.

Ltac max := intros n m; generalize (max_spec_le n m); crush.

Lemma max_1 : forall n m, max n m >= n.
  max.
Qed.

Lemma max_2 : forall n m, max n m >= m.
  max.
Qed.

Hint Resolve max_1 max_2.

Lemma ge_refl : forall n, n >= n.
  crush.
Qed.

Hint Resolve ge_refl.

Hint Extern 1 => match goal with
                   | [ H : _ = exist _ _ _ |- _ ] => rewrite H
                 end.
(* end hide *)
(** remove printing exists *)

(** Now, as a simple first example of a computation, we can define [Bottom], which corresponds to an infinite loop.  For any approximation level, it fails to terminate (returns [None]).  Note the use of [abstract] to create a new opaque lemma for the proof found by the #<tt>#%\coqdocvar{%run%}%#</tt># tactic.  In contrast to the previous section, opaque proofs are fine here, since the proof components of computations do not influence evaluation behavior.  It is generally preferable to make proofs opaque when possible, as this enforces a kind of modularity in the code to follow, preventing it from depending on any details of the proof. *)

Section Bottom.
  Variable A : Type.

  Definition Bottom : computation A.
    exists (fun _ : nat => @None A); abstract run.
  Defined.

  Theorem run_Bottom : forall v, ~run Bottom v.
    run.
  Qed.
End Bottom.

(** A slightly more complicated example is [Return], which gives the same terminating answer at every approximation level. *)

Section Return.
  Variable A : Type.
  Variable v : A.

  Definition Return : computation A.
    intros; exists (fun _ : nat => Some v); abstract run.
  Defined.

  Theorem run_Return : run Return v.
    run.
  Qed.
End Return.

(** The name [Return] was meant to be suggestive of the standard operations of %\index{monad}%monads%~\cite{Monads}%.  The other standard operation is [Bind], which lets us run one computation and, if it terminates, pass its result off to another computation.  We implement bind using the notation [let (x, y) := e1 in e2], for pulling apart the value [e1] which may be thought of as a pair.  The second component of a [computation] is a proof, which we do not need to mention directly in the definition of [Bind]. *)

Section Bind.
  Variables A B : Type.
  Variable m1 : computation A.
  Variable m2 : A -> computation B.

  Definition Bind : computation B.
    exists (fun n =>
      let (f1, _) := m1 in
      match f1 n with
	| None => None
	| Some v =>
	  let (f2, _) := m2 v in
	    f2 n
      end); abstract run.
  Defined.

  Theorem run_Bind : forall (v1 : A) (v2 : B),
    run m1 v1
    -> run (m2 v1) v2
    -> run Bind v2.
    run; match goal with
           | [ x : nat, y : nat |- _ ] => exists (max x y)
         end; run.
  Qed.
End Bind.

(** A simple notation lets us write [Bind] calls the way they appear in Haskell. *)

Notation "x <- m1 ; m2" :=
  (Bind m1 (fun x => m2)) (right associativity, at level 70).

(** We can verify that we have indeed defined a monad, by proving the standard monad laws.  Part of the exercise is choosing an appropriate notion of equality between computations.  We use "equality at all approximation levels." *)

Definition meq A (m1 m2 : computation A) := forall n, proj1_sig m1 n = proj1_sig m2 n.

Theorem left_identity : forall A B (a : A) (f : A -> computation B),
  meq (Bind (Return a) f) (f a).
  run.
Qed.

Theorem right_identity : forall A (m : computation A),
  meq (Bind m (@Return _)) m.
  run.
Qed.

Theorem associativity : forall A B C (m : computation A)
  (f : A -> computation B) (g : B -> computation C),
  meq (Bind (Bind m f) g) (Bind m (fun x => Bind (f x) g)).
  run.
Qed.

(** Now we come to the piece most directly inspired by domain theory.  We want to support general recursive function definitions, but domain theory tells us that not all definitions are reasonable; some fail to be _continuous_ and thus represent unrealizable computations.  To formalize an analogous notion of continuity for our non-termination monad, we write down the approximation relation on computation results that we have had in mind all along. *)

Section lattice.
  Variable A : Type.

  Definition leq (x y : option A) :=
    forall v, x = Some v -> y = Some v.
End lattice.

(** We now have the tools we need to define a new [Fix] combinator that, unlike the one we saw in the prior section, does not require a termination proof, and in fact admits recursive definition of functions that fail to terminate on some or all inputs. *)

Section Fix.

  (** First, we have the function domain and range types. *)

  Variables A B : Type.

  (** Next comes the function body, which is written as though it can be parameterized over itself, for recursive calls. *)

  Variable f : (A -> computation B) -> (A -> computation B).

  (** Finally, we impose an obligation to prove that the body [f] is continuous.  That is, when [f] terminates according to one recursive version of itself, it also terminates with the same result at the same approximation level when passed a recursive version that refines the original, according to [leq]. *)

  Hypothesis f_continuous : forall n v v1 x,
    runTo (f v1 x) n v
    -> forall (v2 : A -> computation B),
      (forall x, leq (proj1_sig (v1 x) n) (proj1_sig (v2 x) n))
      -> runTo (f v2 x) n v.

  (** The computational part of the [Fix] combinator is easy to define.  At approximation level 0, we diverge; at higher levels, we run the body with a functional argument drawn from the next lower level. *)

  Fixpoint Fix' (n : nat) (x : A) : computation B :=
    match n with
      | O => Bottom _
      | S n' => f (Fix' n') x
    end.

  (** Now it is straightforward to package [Fix'] as a computation combinator [Fix]. *)

  Hint Extern 1 (_ >= _) => omega.
  Hint Unfold leq.

  Lemma Fix'_ok : forall steps n x v, proj1_sig (Fix' n x) steps = Some v
    -> forall n', n' >= n
      -> proj1_sig (Fix' n' x) steps = Some v.
    unfold runTo in *; induction n; crush;
      match goal with
        | [ H : _ >= _ |- _ ] => inversion H; crush; eauto
      end.
  Qed.

  Hint Resolve Fix'_ok.

  Hint Extern 1 (proj1_sig _ _ = _) => simpl;
    match goal with
      | [ |- proj1_sig ?E _ = _ ] => eapply (proj2_sig E)
    end.

  Definition Fix : A -> computation B.
    intro x; exists (fun n => proj1_sig (Fix' n x) n); abstract run.
  Defined.

  (** Finally, we can prove that [Fix] obeys the expected computation rule. *)

  Theorem run_Fix : forall x v,
    run (f Fix x) v
    -> run (Fix x) v.
    run; match goal with
           | [ n : nat |- _ ] => exists (S n); eauto
         end.
  Qed.
End Fix.

(* begin hide *)
Lemma leq_Some : forall A (x y : A), leq (Some x) (Some y)
  -> x = y.
  intros ? ? ? H; generalize (H _ (eq_refl _)); crush.
Qed.

Lemma leq_None : forall A (x y : A), leq (Some x) None
  -> False.
  intros ? ? ? H; generalize (H _ (eq_refl _)); crush.
Qed.

Ltac mergeSort' := run;
  repeat (match goal with
            | [ |- context[match ?E with O => _ | S _ => _ end] ] => destruct E
          end; run);
  repeat match goal with
           | [ H : forall x, leq (proj1_sig (?f x) _) (proj1_sig (?g x) _) |- _ ] =>
             match goal with
               | [ H1 : f ?arg = _, H2 : g ?arg = _ |- _ ] =>
                 generalize (H arg); rewrite H1; rewrite H2; clear H1 H2; simpl; intro
             end
         end; run; repeat match goal with
                            | [ H : _ |- _ ] => (apply leq_None in H; tauto) || (apply leq_Some in H; subst)
                          end; auto.
(* end hide *)

(** After all that work, it is now fairly painless to define a version of [mergeSort] that requires no proof of termination.  We appeal to a program-specific tactic whose definition is hidden here but present in the book source. *)

Definition mergeSort' : forall A, (A -> A -> bool) -> list A -> computation (list A).
  refine (fun A le => Fix
    (fun (mergeSort : list A -> computation (list A))
      (ls : list A) =>
      if le_lt_dec 2 (length ls)
	then let lss := split ls in
          ls1 <- mergeSort (fst lss);
          ls2 <- mergeSort (snd lss);
          Return (merge le ls1 ls2)
	else Return ls) _); abstract mergeSort'.
Defined.

(** Furthermore, "running" [mergeSort'] on concrete inputs is as easy as choosing a sufficiently high approximation level and letting Coq's computation rules do the rest.  Contrast this with the proof work that goes into deriving an evaluation fact for a deeply embedded language, with one explicit proof rule application per execution step. *)

Lemma test_mergeSort' : run (mergeSort' leb (1 :: 2 :: 36 :: 8 :: 19 :: nil))
  (1 :: 2 :: 8 :: 19 :: 36 :: nil).
  exists 4; reflexivity.
Qed.

(** There is another benefit of our new [Fix] compared with the one we used in the previous section: we can now write recursive functions that sometimes fail to terminate, without losing easy reasoning principles for the terminating cases.  Consider this simple example, which appeals to another tactic whose definition we elide here. *)

(* begin hide *)
Ltac looper := unfold leq in *; run;
  repeat match goal with
           | [ x : unit |- _ ] => destruct x
           | [ x : bool |- _ ] => destruct x
         end; auto.
(* end hide *)

Definition looper : bool -> computation unit.
  refine (Fix (fun looper (b : bool) =>
    if b then Return tt else looper b) _); abstract looper.
Defined.

Lemma test_looper : run (looper true) tt.
  exists 1; reflexivity.
Qed.

(** As before, proving outputs for specific inputs is as easy as demonstrating a high enough approximation level.

   There are other theorems that are important to prove about combinators like [Return], [Bind], and [Fix].  In general, for a computation [c], we sometimes have a hypothesis proving [run c v] for some [v], and we want to perform inversion to deduce what [v] must be.  Each combinator should ideally have a theorem of that kind, for [c] built directly from that combinator.  We have omitted such theorems here, but they are not hard to prove.  In general, the domain theory-inspired approach avoids the type-theoretic "gotchas" that tend to show up in approaches that try to mix normal Coq computation with explicit syntax types.  The next section of this chapter demonstrates two alternate approaches of that sort.  In the final section of the chapter, we review the pros and cons of the different choices, coming to the conclusion that none of them is obviously better than any one of the others for all situations. *)

(** * Co-Inductive Non-Termination Monads *)

(** There are two key downsides to both of the previous approaches: both require unusual syntax based on explicit calls to fixpoint combinators, and both generate immediate proof obligations about the bodies of recursive definitions.  In Chapter 5, we have already seen how co-inductive types support recursive definitions that exhibit certain well-behaved varieties of non-termination.  It turns out that we can leverage that co-induction support for encoding of general recursive definitions, by adding layers of co-inductive syntax.  In effect, we mix elements of shallow and deep embeddings.

   Our first example of this kind, proposed by Capretta%~\cite{Capretta}%, defines a silly-looking type of thunks; that is, computations that may be forced to yield results, if they terminate. *)

CoInductive thunk (A : Type) : Type :=
| Answer : A -> thunk A
| Think : thunk A -> thunk A.

(** A computation is either an immediate [Answer] or another computation wrapped inside [Think].  Since [thunk] is co-inductive, every [thunk] type is inhabited by an infinite nesting of [Think]s, standing for non-termination.  Terminating results are [Answer] wrapped inside some finite number of [Think]s.

   Why bother to write such a strange definition?  The definition of [thunk] is motivated by the ability it gives us to define a "bind" operation, similar to the one we defined in the previous section. *)

CoFixpoint TBind A B (m1 : thunk A) (m2 : A -> thunk B) : thunk B :=
  match m1 with
    | Answer x => m2 x
    | Think m1' => Think (TBind m1' m2)
  end.

(** Note that the definition would violate the co-recursion guardedness restriction if we left out the seemingly superfluous [Think] on the righthand side of the second [match] branch.

   We can prove that [Answer] and [TBind] form a monad for [thunk].  The proof is omitted here but present in the book source.  As usual for this sort of proof, a key element is choosing an appropriate notion of equality for [thunk]s. *)

(* begin hide *)
CoInductive thunk_eq A : thunk A -> thunk A -> Prop :=
| EqAnswer : forall x, thunk_eq (Answer x) (Answer x)
| EqThinkL : forall m1 m2, thunk_eq m1 m2 -> thunk_eq (Think m1) m2
| EqThinkR : forall m1 m2, thunk_eq m1 m2 -> thunk_eq m1 (Think m2).

Section thunk_eq_coind.
  Variable A : Type.
  Variable P : thunk A -> thunk A -> Prop.

  Hypothesis H : forall m1 m2, P m1 m2
    -> match m1, m2 with
         | Answer x1, Answer x2 => x1 = x2
         | Think m1', Think m2' => P m1' m2'
         | Think m1', _ => P m1' m2
         | _, Think m2' => P m1 m2'
       end.

  Theorem thunk_eq_coind : forall m1 m2, P m1 m2 -> thunk_eq m1 m2.
    cofix; intros;
      match goal with
        | [ H' : P _ _ |- _ ] => specialize (H H'); clear H'
      end; destruct m1; destruct m2; subst; repeat constructor; auto.
  Qed.
End thunk_eq_coind.
(* end hide *)

(** In the proofs to follow, we will need a function similar to one we saw in Chapter 5, to pull apart and reassemble a [thunk] in a way that provokes reduction of co-recursive calls. *)

Definition frob A (m : thunk A) : thunk A :=
  match m with
    | Answer x => Answer x
    | Think m' => Think m'
  end.

Theorem frob_eq : forall A (m : thunk A), frob m = m.
  destruct m; reflexivity.
Qed.

(* begin hide *)
Theorem thunk_eq_frob : forall A (m1 m2 : thunk A),
  thunk_eq (frob m1) (frob m2)
  -> thunk_eq m1 m2.
  intros; repeat rewrite frob_eq in *; auto.
Qed.

Ltac findDestr := match goal with
                    | [ |- context[match ?E with Answer _ => _ | Think _ => _ end] ] =>
                      match E with
                        | context[match _ with Answer _ => _ | Think _ => _ end] => fail 1
                        | _ => destruct E
                      end
                  end.

Theorem thunk_eq_refl : forall A (m : thunk A), thunk_eq m m.
  intros; apply (thunk_eq_coind (fun m1 m2 => m1 = m2)); crush; findDestr; reflexivity.
Qed.

Hint Resolve thunk_eq_refl.

Theorem tleft_identity : forall A B (a : A) (f : A -> thunk B),
  thunk_eq (TBind (Answer a) f) (f a).
  intros; apply thunk_eq_frob; crush.
Qed.

Theorem tright_identity : forall A (m : thunk A),
  thunk_eq (TBind m (@Answer _)) m.
  intros; apply (thunk_eq_coind (fun m1 m2 => m1 = TBind m2 (@Answer _))); crush;
    findDestr; reflexivity.
Qed.

Lemma TBind_Answer : forall (A B : Type) (v : A) (m2 : A -> thunk B),
  TBind (Answer v) m2 = m2 v.
  intros; rewrite <- (frob_eq (TBind (Answer v) m2));
    simpl; findDestr; reflexivity.
Qed.

Hint Rewrite TBind_Answer.

(** printing exists $\exists$ *)

Theorem tassociativity : forall A B C (m : thunk A) (f : A -> thunk B) (g : B -> thunk C),
  thunk_eq (TBind (TBind m f) g) (TBind m (fun x => TBind (f x) g)).
  intros; apply (thunk_eq_coind (fun m1 m2 => (exists m,
    m1 = TBind (TBind m f) g
    /\ m2 = TBind m (fun x => TBind (f x) g))
  \/ m1 = m2)); crush; eauto; repeat (findDestr; crush; eauto).
Qed.
(* end hide *)

(** As a simple example, here is how we might define a tail-recursive factorial function. *)

CoFixpoint fact (n acc : nat) : thunk nat :=
  match n with
    | O => Answer acc
    | S n' => Think (fact n' (S n' * acc))
  end.

(** To test our definition, we need an evaluation relation that characterizes results of evaluating [thunk]s. *)

Inductive eval A : thunk A -> A -> Prop :=
| EvalAnswer : forall x, eval (Answer x) x
| EvalThink : forall m x, eval m x -> eval (Think m) x.

Hint Rewrite frob_eq.

Lemma eval_frob : forall A (c : thunk A) x,
  eval (frob c) x
  -> eval c x.
  crush.
Qed.

Theorem eval_fact : eval (fact 5 1) 120.
  repeat (apply eval_frob; simpl; constructor).
Qed.

(** We need to apply constructors of [eval] explicitly, but the process is easy to automate completely for concrete input programs.

   Now consider another very similar definition, this time of a Fibonacci number function. *)

Notation "x <- m1 ; m2" :=
  (TBind m1 (fun x => m2)) (right associativity, at level 70).

(* begin hide *)
(* begin thide *)
Definition fib := pred.
(* end thide *)
(* end hide *)

(** %\vspace{-.3in}%[[
CoFixpoint fib (n : nat) : thunk nat :=
  match n with
    | 0 => Answer 1
    | 1 => Answer 1
    | _ => n1 <- fib (pred n);
      n2 <- fib (pred (pred n));
      Answer (n1 + n2)
  end.
]]

Coq complains that the guardedness condition is violated.  The two recursive calls are immediate arguments to [TBind], but [TBind] is not a constructor of [thunk].  Rather, it is a defined function.  This example shows a very serious limitation of [thunk] for traditional functional programming: it is not, in general, possible to make recursive calls and then make further recursive calls, depending on the first call's result.  The [fact] example succeeded because it was already tail recursive, meaning no further computation is needed after a recursive call.

%\medskip%

I know no easy fix for this problem of [thunk], but we can define an alternate co-inductive monad that avoids the problem, based on a proposal by Megacz%~\cite{Megacz}%.  We ran into trouble because [TBind] was not a constructor of [thunk], so let us define a new type family where "bind" is a constructor. *)

CoInductive comp (A : Type) : Type :=
| Ret : A -> comp A
| Bnd : forall B, comp B -> (B -> comp A) -> comp A.

(** This example shows off Coq's support for%\index{recursively non-uniform parameters}% _recursively non-uniform parameters_, as in the case of the parameter [A] declared above, where each constructor's type ends in [comp A], but there is a recursive use of [comp] with a different parameter [B].  Beside that technical wrinkle, we see the simplest possible definition of a monad, via a type whose two constructors are precisely the monad operators.

   It is easy to define the semantics of terminating [comp] computations. *)

Inductive exec A : comp A -> A -> Prop :=
| ExecRet : forall x, exec (Ret x) x
| ExecBnd : forall B (c : comp B) (f : B -> comp A) x1 x2, exec (A := B) c x1
  -> exec (f x1) x2
  -> exec (Bnd c f) x2.

(** We can also prove that [Ret] and [Bnd] form a monad according to a notion of [comp] equality based on [exec], but we omit details here; they are in the book source at this point. *)

(* begin hide *)
Hint Constructors exec.

Definition comp_eq A (c1 c2 : comp A) := forall r, exec c1 r <-> exec c2 r.

Ltac inverter := repeat match goal with
                          | [ H : exec _ _ |- _ ] => inversion H; []; crush
                        end.

Theorem cleft_identity : forall A B (a : A) (f : A -> comp B),
  comp_eq (Bnd (Ret a) f) (f a).
  red; crush; inverter; eauto.
Qed.

Theorem cright_identity : forall A (m : comp A),
  comp_eq (Bnd m (@Ret _)) m.
  red; crush; inverter; eauto.
Qed.

Lemma cassociativity1 : forall A B C (f : A -> comp B) (g : B -> comp C) r c,
  exec c r
  -> forall m, c = Bnd (Bnd m f) g
   -> exec (Bnd m (fun x => Bnd (f x) g)) r.
  induction 1; crush.
  match goal with
    | [ H : Bnd _ _ = Bnd _ _ |- _ ] => injection H; clear H; intros; try subst
  end.
  move H3 after A.
  generalize dependent B0.
  do 2 intro.
  subst.
  crush.
  inversion H; clear H; crush.
  eauto.
Qed.

Lemma cassociativity2 : forall A B C (f : A -> comp B) (g : B -> comp C) r c,
  exec c r
  -> forall m, c = Bnd m (fun x => Bnd (f x) g)
   -> exec (Bnd (Bnd m f) g) r.
  induction 1; crush.
  match goal with
    | [ H : Bnd _ _ = Bnd _ _ |- _ ] => injection H; clear H; intros; try subst
  end.
  move H3 after B.
  generalize dependent B0.
  do 2 intro.
  subst.
  crush.
  inversion H0; clear H0; crush.
  eauto.
Qed.

Hint Resolve cassociativity1 cassociativity2.

Theorem cassociativity : forall A B C (m : comp A) (f : A -> comp B) (g : B -> comp C),
  comp_eq (Bnd (Bnd m f) g) (Bnd m (fun x => Bnd (f x) g)).
  red; crush; eauto.
Qed.
(* end hide *)

(** Not only can we define the Fibonacci function with the new monad, but even our running example of merge sort becomes definable.  By shadowing our previous notation for "bind," we can write almost exactly the same code as in our previous [mergeSort'] definition, but with less syntactic clutter. *)

Notation "x <- m1 ; m2" := (Bnd m1 (fun x => m2)).

CoFixpoint mergeSort'' A (le : A -> A -> bool) (ls : list A) : comp (list A) :=
  if le_lt_dec 2 (length ls)
    then let lss := split ls in
      ls1 <- mergeSort'' le (fst lss);
      ls2 <- mergeSort'' le (snd lss);
      Ret (merge le ls1 ls2)
    else Ret ls.

(** To execute this function, we go through the usual exercise of writing a function to catalyze evaluation of co-recursive calls. *)

Definition frob' A (c : comp A) :=
  match c with
    | Ret x => Ret x
    | Bnd _ c' f => Bnd c' f
  end.

Lemma exec_frob : forall A (c : comp A) x,
  exec (frob' c) x
  -> exec c x.
  destruct c; crush.
Qed.

(** Now the same sort of proof script that we applied for testing [thunk]s will get the job done. *)

Lemma test_mergeSort'' : exec (mergeSort'' leb (1 :: 2 :: 36 :: 8 :: 19 :: nil))
  (1 :: 2 :: 8 :: 19 :: 36 :: nil).
  repeat (apply exec_frob; simpl; econstructor).
Qed.

(** Have we finally reached the ideal solution for encoding general recursive definitions, with minimal hassle in syntax and proof obligations?  Unfortunately, we have not, as [comp] has a serious expressivity weakness.  Consider the following definition of a curried addition function: *)

Definition curriedAdd (n : nat) := Ret (fun m : nat => Ret (n + m)).

(** This definition works fine, but we run into trouble when we try to apply it in a trivial way.
[[
Definition testCurriedAdd := Bnd (curriedAdd 2) (fun f => f 3).
]]

<<
Error: Universe inconsistency.
>>

The problem has to do with rules for inductive definitions that we will study in more detail in Chapter 12.  Briefly, recall that the type of the constructor [Bnd] quantifies over a type [B].  To make [testCurriedAdd] work, we would need to instantiate [B] as [nat -> comp nat].  However, Coq enforces a %\emph{predicativity restriction}% that (roughly) no quantifier in an inductive or co-inductive type's definition may ever be instantiated with a term that contains the type being defined.  Chapter 12 presents the exact mechanism by which this restriction is enforced, but for now our conclusion is that [comp] is fatally flawed as a way of encoding interesting higher-order functional programs that use general recursion. *)

(** * Comparing the Alternatives *)

(** We have seen four different approaches to encoding general recursive definitions in Coq.  Among them there is no clear champion that dominates the others in every important way.  Instead, we close the chapter by comparing the techniques along a number of dimensions.  Every technique allows recursive definitions with termination arguments that go beyond Coq's built-in termination checking, so we must turn to subtler points to highlight differences.

   One useful property is automatic integration with normal Coq programming.  That is, we would like the type of a function to be the same, whether or not that function is defined using an interesting recursion pattern.  Only the first of the four techniques, well-founded recursion, meets this criterion.  It is also the only one of the four to meet the related criterion that evaluation of function calls can take place entirely inside Coq's built-in computation machinery.  The monad inspired by domain theory occupies some middle ground in this dimension, since generally standard computation is enough to evaluate a term once a high enough approximation level is provided.

   Another useful property is that a function and its termination argument may be developed separately.  We may even want to define functions that fail to terminate on some or all inputs.  The well-founded recursion technique does not have this property, but the other three do.

   One minor plus is the ability to write recursive definitions in natural syntax, rather than with calls to higher-order combinators.  This downside of the first two techniques is actually rather easy to get around using Coq's notation mechanism, though we leave the details as an exercise for the reader.  (For this and other details of notations, see Chapter 12 of the Coq 8.4 manual.)

   The first two techniques impose proof obligations that are more basic than termination arguments, where well-founded recursion requires a proof of extensionality and domain-theoretic recursion requires a proof of continuity.  A function may not be defined, and thus may not be computed with, until these obligations are proved.  The co-inductive techniques avoid this problem, as recursive definitions may be made without any proof obligations.

   We can also consider support for common idioms in functional programming.  For instance, the [thunk] monad effectively only supports recursion that is tail recursion, while the others allow arbitrary recursion schemes.

   On the other hand, the [comp] monad does not support the effective mixing of higher-order functions and general recursion, while all the other techniques do.  For instance, we can finish the failed [curriedAdd] example in the domain-theoretic monad. *)

Definition curriedAdd' (n : nat) := Return (fun m : nat => Return (n + m)).

Definition testCurriedAdd := Bind (curriedAdd' 2) (fun f => f 3).

(** The same techniques also apply to more interesting higher-order functions like list map, and, as in all four techniques, we can mix primitive and general recursion, preferring the former when possible to avoid proof obligations. *)

Fixpoint map A B (f : A -> computation B) (ls : list A) : computation (list B) :=
  match ls with
    | nil => Return nil
    | x :: ls' => Bind (f x) (fun x' =>
      Bind (map f ls') (fun ls'' =>
        Return (x' :: ls'')))
  end.

(** remove printing exists *)
Theorem test_map : run (map (fun x => Return (S x)) (1 :: 2 :: 3 :: nil))
  (2 :: 3 :: 4 :: nil).
  exists 1; reflexivity.
Qed.

(** One further disadvantage of [comp] is that we cannot prove an inversion lemma for executions of [Bind] without appealing to an _axiom_, a logical complication that we discuss at more length in Chapter 12.  The other three techniques allow proof of all the important theorems within the normal logic of Coq.

Perhaps one theme of our comparison is that one must trade off between, on one hand, functional programming expressiveness and compatibility with normal Coq types and computation; and, on the other hand, the level of proof obligations one is willing to handle at function definition time. *)


Require Import A1_Plan A4_Droite A6_Intersection A7_Tactics.

Section INTERSECTION_LINES_PROPERTIES.

Definition InterLinesPoint (l1 l2 : Line) (H : SecantLines l1 l2) :=
let (M, _) := PointDef (fun M : Point => OnLine l1 M /\ OnLine l2 M) (InterLines l1 l2 H) in M.

Lemma OnLine1InterLinesPoint : forall (l1 l2 : Line) (H : SecantLines l1 l2),
	OnLine l1 (InterLinesPoint l1 l2 H).
Proof.
	intros; unfold InterLinesPoint in |- *.
	destruct (PointDef (fun M : Point => OnLine l1 M /\ OnLine l2 M) (InterLines l1 l2 H)) as (M, ((H1, _), _)).
 	exact H1.
Qed.

Lemma OnLine2InterLinesPoint : forall (l1 l2 : Line) (H : SecantLines l1 l2),
	OnLine l2 (InterLinesPoint l1 l2 H).
Proof.
	intros; unfold InterLinesPoint in |- *.
	destruct (PointDef (fun M : Point => OnLine l1 M /\ OnLine l2 M) (InterLines l1 l2 H)) as (M, ((_, H1), _)).
 	exact H1.
Qed.

Lemma UniqueInterLinesPoint : forall (l1 l2 : Line) (H : SecantLines l1 l2), forall M : Point,
	OnLine l1 M -> OnLine l2 M ->
	M = InterLinesPoint l1 l2 H.
Proof.
	intros; unfold InterLinesPoint in |- *.
	destruct (PointDef (fun M : Point => OnLine l1 M /\ OnLine l2 M) (InterLines l1 l2 H)) as (N, (_, H2)).
	 apply sym_eq; apply (H2 M); auto.
Qed.

Lemma EqPointsInterLines : forall (l1 l2 : Line) (H : SecantLines l1 l2), forall M N : Point,
	OnLine l1 M -> OnLine l2 M -> OnLine l1 N -> OnLine l2 N ->
	M = N.
Proof.
	intros; apply trans_eq with (y := InterLinesPoint l1 l2 H).
	 apply UniqueInterLinesPoint; trivial.
	 apply sym_eq; apply UniqueInterLinesPoint; trivial.
Qed.

Lemma SecantLinesSym : forall l1 l2 : Line,
	SecantLines l1 l2 -> SecantLines l2 l1.
Proof.
	unfold SecantLines, ParallelLines in |- *; canonize1.
Qed.

End INTERSECTION_LINES_PROPERTIES.


(*
コンピュータの数学 6.6

Matiyasevich の補題

マティヤセビッチの補題の一部をCoqで形式化してみる。

GKP p.268 の 13行目の式を証明する。

F_(k*n)   = k * F_n * (F_(n+1))^k-1 mod (F_n)^2
かつ
F_(k*n+1) = (F_(n+1))^k             mod (F_n)^2
 *)

From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
From common Require Import ssromega.

Import GRing.Theory.         (* mulrA などを使えるようにする。 *)
Import Num.Theory.           (* unitf_gt0 などを使えるようにする。 *)
Import intZmod.              (* addz など *)
Import intRing.              (* mulz など *)
Open Scope ring_scope.       (* 環の四則演算を使えるようにする。 *)

Fixpoint fibn (n : nat) : nat :=
    match n with
    | 0 => 0
    | 1 => 1
    | (m.+1 as pn).+1 => fibn m + fibn pn (* fibn n.-2 + fibn n.-1 *)
    end.

Definition fibz (n : nat) : int := fibn n.

Definition mati_1 k n :=
  (fibz (k * n) = k%:Z * fibz n * fibz n.+1 ^ k.-1 %[mod (fibz n)^2])%Z.

Definition mati_2 k n :=
  (fibz (k * n).+1 = fibz n.+1 ^ k %[mod (fibz n)^2])%Z.

Lemma fibn_2 n : fibz n + fibz n.+1 = fibz n.+2.
Proof.
  done.
Qed.

Lemma transpos (a b c : int) : a = c + b -> a - b = c.
Proof.
  move/eqP => H.
  apply/eqP.
  by rewrite subr_eq.
Qed.

Lemma fibz_1 n : (0 < n)%N -> fibz n.-1 = fibz n.+1 - fibz n.
Proof.
  case: n => // n Hn.
  rewrite -pred_Sn -fibn_2.
  apply: esym.
  by apply: transpos.
Qed.

Lemma m_addz m n p q d  :
  (m = n %[mod d])%Z -> (p = q %[mod d])%Z -> (m + p = n + q %[mod d])%Z.
Proof.
Admitted.                                   (* see. gkp_4_6_modulo.v *)

Lemma m_mulz m n p q d  :
  (m = n %[mod d])%Z -> (p = q %[mod d])%Z -> (m * p = n * q %[mod d])%Z.
Proof.
Admitted.                                   (* see. gkp_4_6_modulo.v *)

(* 加法定理 *)
Lemma fib_addition n m :
  (0 < n)%N -> fibz (n + m) = fibz n * fibz m.+1 + fibz n.-1 * fibz m.
Proof.
Admitted.                                   (* see. ssr_fib_2.v *)

Check modzMDl : forall p m d : int, (p * d + m = m %[mod d])%Z.
Lemma modzMDr : forall p m d : int, (m + p * d = m %[mod d])%Z.
Proof.
  move=> p m d.
  rewrite [m + p * d]addrC.
  by rewrite modzMDl.
Qed.

Lemma modzMDr' : forall p m d : int, (m - p * d = m %[mod d])%Z.
Proof.
  move=> p m d.
  rewrite [m - p * d]addrC -mulNr.
  by rewrite modzMDl.
Qed.

Lemma l_mati k n : (1 < n)%N -> mati_1 k n /\ mati_2 k n.
Proof.
  move=> Hn.
  elim: k => /= [| k [IHk1 IHk2]].
  - by split.
  - rewrite /mati_1 in IHk1.
    rewrite /mati_2 in IHk2.
    rewrite /mati_1 /mati_2.
    split.

    (* ゴールが mati_1 の場合 *)
    + rewrite -pred_Sn.
      have -> : (k.+1 * n = n + k * n)%N by done.

      (* 左辺に加法定理を使う。 fib (n + kn) *)
      rewrite fib_addition; last ssromega.
      rewrite fibz_1; last ssromega.

      (* 左辺を IHk1 と IHk2 で書き換える。 *)
      have -> : (fibz n * fibz (k * n).+1 + (fibz n.+1 - fibz n) * fibz (k * n)
      = fibz n * (fibz n.+1 ^ k) + (fibz n.+1 - fibz n) * (k%:Z * fibz n * fibz n.+1 ^ k.-1)
       %[mod fibz n ^ 2])%Z
        by apply: m_addz; apply: m_mulz.

      (* 左辺を展開する。 *)
      have -> : (fibz n.+1 - fibz n) * (k%:Z * fibz n * fibz n.+1 ^ k.-1)
      = (k%:Z * fibz n * fibz n.+1 ^ k) - (k%:Z * fibz n ^ 2 * fibz n.+1 ^ k.-1).
      {
        rewrite mulrBl mulrCA -exprSz.
        case: k IHk1 IHk2 => [| k IHk1 IHk2].
        * by rewrite !mulr0 !mul0r.
        * by rewrite !mulrA [fibz n * k.+1]mulrC.
      }.
      have -> : k%:Z * fibz n ^ 2 * fibz n.+1 ^ k.-1
                = k%:Z * fibz n.+1 ^ k.-1 * fibz n ^ 2
        by rewrite mulrAC.
      rewrite addrA.

      (* 左辺の fib n ^ 2 を消す。 *)
      have -> : (fibz n * (fibz n.+1 ^ k) + k%:Z * fibz n * fibz n.+1 ^ k
      - k%:Z * fibz n.+1 ^ k.-1 * fibz n ^ 2
                 = fibz n * (fibz n.+1 ^ k) + k%:Z * fibz n * fibz n.+1 ^ k
      %[mod fibz n ^ 2])%Z
        by rewrite modzMDr'.

      (* 左辺を整理する。 *)
      have -> : fibz n * (fibz n.+1 ^ k) + k%:Z * fibz n * fibz n.+1 ^ k
                = k.+1%:Z * fibz n * fibz n.+1 ^ k
        by rewrite -mulrDl.
      done.

    (* ゴールが mati_2 の場合 *)
    + have -> : ((k.+1 * n).+1 = (k * n).+1 + n)%N
        by ssromega.

      (* 左辺に加法定理を使う。 fib (kn+1 + n) *)
      rewrite fib_addition; last ssromega.

      (* 左辺を IHk1 と IHk2 で書き換える。 *)
      have -> : (fibz (k * n).+1 * fibz n.+1 + fibz (k * n) * fibz n
      = (fibz n.+1 ^ k) * fibz n.+1 + (k%:Z * fibz n * fibz n.+1 ^ k.-1) * fibz n
      %[mod fibz n ^ 2])%Z
        by apply: m_addz; apply: m_mulz.

      (* 左辺を整理する。 *)
      have -> : fibz n.+1 ^ k * fibz n.+1 + k%:Z * fibz n * fibz n.+1 ^ k.-1 * fibz n
                = fibz n.+1 ^ k.+1 + k%:Z * fibz n.+1 ^ k.-1 * fibz n ^ 2.
      {
        rewrite -exprSr; congr (_ + _).
        rewrite -!mulrA; congr (_ * _).
        by rewrite mulrCA.
      }

      (* 左辺の fib n ^ 2 を消す。 *)
      have -> : (fibz n.+1 ^ k.+1 + k%:Z * fibz n.+1 ^ k.-1 * fibz n ^ 2
                 = fibz n.+1 ^ k.+1 %[mod fibz n ^ 2])%Z
        by rewrite modzMDr.
      done.
Qed.

(* END *)