Require Export SpecSyntax.
Set Implicit Arguments.

(******************************************************************************)
(* Shifting                                                                   *)
(******************************************************************************)

Fixpoint tshiftIndex (c : hnat) (i : nat) : nat :=
  match c , i with
    | HO    , _   => S i
    | HSm c , _   => tshiftIndex c i
    | HSy c , O   => O
    | HSy c , S i => S (tshiftIndex c i)
  end.

Fixpoint tshiftTy (c : hnat) (T : Ty) : Ty :=
  match T with
    | tvar X     => tvar (tshiftIndex c X)
    | tarr T1 T2 => tarr (tshiftTy c T1) (tshiftTy c T2)
    | tall T     => tall (tshiftTy (HSy c) T)
    | texist T   => texist (tshiftTy (HSy c) T)
    | tprod T1 T2 => tprod (tshiftTy c T1) (tshiftTy c T2)
  end.

Fixpoint tshiftTm (c : hnat) (t : Tm) : Tm :=
  match t with
  | var x         => var x
  | abs T1 t2     => abs (tshiftTy c T1) (tshiftTm (HSm c) t2)
  | app t1 t2     => app (tshiftTm c t1) (tshiftTm c t2)
  | tabs t2       => tabs (tshiftTm (HSy c) t2)
  | tapp t1 T2    => tapp (tshiftTm c t1) (tshiftTy c T2)
  | pack T1 t T2  => pack (tshiftTy c T1) (tshiftTm c t) (tshiftTy c T2)
  | unpack t1 t2  => unpack (tshiftTm c t1) (tshiftTm (HSm (HSy c)) t2)
  | prod t1 t2    => prod (tshiftTm c t1) (tshiftTm c t2)
  | case t1 p t2  => case (tshiftTm c t1) p (tshiftTm (c + bindPat p) t2)
  end.

Fixpoint shiftIndex (c : hnat) (i : nat) : nat :=
  match c , i  with
    | HO    , _   => S i
    | HSm c , O   => O
    | HSm c , S i => S (shiftIndex c i)
    | HSy c , _   => shiftIndex c i
  end.

Fixpoint shiftTm (c : hnat) (t : Tm) : Tm :=
  match t with
  | var x        => var (shiftIndex c x)
  | abs T1 t2    => abs T1 (shiftTm (HSm c) t2)
  | app t1 t2    => app (shiftTm c t1) (shiftTm c t2)
  | tabs t2      => tabs (shiftTm (HSy c) t2)
  | tapp t1 T2   => tapp (shiftTm c t1) T2
  | pack T1 t T2  => pack T1 (shiftTm c t) T2
  | unpack t1 t2  => unpack (shiftTm c t1) (shiftTm (HSm (HSy c)) t2)
  | prod t1 t2   => prod (shiftTm c t1) (shiftTm c t2)
  | case t1 p t2 => case (shiftTm c t1) p (shiftTm (hplus c (bindPat p)) t2)
  end.

Fixpoint weakenTm (t : Tm) (k : hnat) : Tm :=
  match k with
    | HO    => t
    | HSm k => shiftTm HO (weakenTm t k)
    | HSy k => tshiftTm HO (weakenTm t k)
  end.

Fixpoint weakenTy (T : Ty) (k : hnat) : Ty :=
  match k with
    | HO    => T
    | HSm k => weakenTy T k
    | HSy k => tshiftTy HO (weakenTy T k)
  end.

(******************************************************************************)
(* Type substitution.                                                         *)
(******************************************************************************)

Fixpoint tsubstIndex (X : hnat) (T' : Ty) (Y : nat) : Ty :=
  match X , Y with
    | HO    , O   => T'
    | HO    , S Y => tvar Y
    | HSm X , Y   => tsubstIndex X T' Y
    | HSy X , O   => tvar O
    | HSy X , S Y => tshiftTy HO (tsubstIndex X T' Y)
  end.

Fixpoint tsubstTy (X : hnat) (T' : Ty) (T : Ty) : Ty :=
  match T with
    | tvar Y      => tsubstIndex X T' Y
    | tarr T1 T2  => tarr (tsubstTy X T' T1) (tsubstTy X T' T2)
    | tall T      => tall (tsubstTy (HSy X) T' T)
    | texist T    => texist (tsubstTy (HSy X) T' T)
    | tprod T1 T2 => tprod (tsubstTy X T' T1) (tsubstTy X T' T2)
  end.

Fixpoint tsubstTm (X : hnat) (T' : Ty) (t : Tm) : Tm :=
  match t with
  | var x         => var x
  | abs T1 t2     => abs  (tsubstTy X T' T1) (tsubstTm (HSm X) T' t2)
  | app t1 t2     => app  (tsubstTm X T' t1) (tsubstTm X T' t2)
  | tabs t2       => tabs (tsubstTm (HSy X) T' t2)
  | tapp t1 T2    => tapp (tsubstTm X T' t1) (tsubstTy X T' T2)
  | pack T1 t T2  => pack (tsubstTy X T' T1) (tsubstTm X T' t) (tsubstTy X T' T2)
  | unpack t1 t2  => unpack (tsubstTm X T' t1) (tsubstTm (HSm (HSy X)) T' t2)
  | prod t1 t2    => prod (tsubstTm X T' t1) (tsubstTm X T' t2)
  | case t1 p t2  => case (tsubstTm X T' t1) p
                       (tsubstTm (hplus X (bindPat p)) T' t2)
  end.

(******************************************************************************)
(* Term substitutions.                                                        *)
(******************************************************************************)

Fixpoint substIndex (x : hnat) (t : Tm) (y : nat) : Tm :=
  match x , y with
    | HO    , O   => t
    | HO    , S y => var y
    | HSm x , O   => var O
    | HSm x , S y => shiftTm HO (substIndex x t y)
    | HSy x , y   => tshiftTm HO (substIndex x t y)
  end.

Fixpoint substTm (x : hnat) (t' : Tm) (t : Tm) : Tm :=
  match t with
    | var y        => substIndex x t' y
    | abs T1 t2    => abs T1 (substTm (HSm x) t' t2)
    | app t1 t2    => app (substTm x t' t1) (substTm x t' t2)
    | tabs t2      => tabs (substTm (HSy x) t' t2)
    | tapp t1 T2   => tapp (substTm x t' t1) T2
    | pack T1 t T2 => pack T1 (substTm x t' t) T2
    | unpack t1 t2 => unpack (substTm x t' t1) (substTm (HSm (HSy x)) t' t2)
    | prod t1 t2   => prod (substTm x t' t1) (substTm x t' t2)
    | case t1 p t2 => case (substTm x t' t1) p
                        (substTm (hplus x (bindPat p)) t' t2)
  end.

(******************************************************************************)
(* Context extensions.                                                        *)
(******************************************************************************)

Fixpoint append (Δ1 Δ2 : Env) : Env :=
  match Δ2 with
    | empty     => Δ1
    | evar Δ2 T => evar (append Δ1 Δ2) T
    | etvar Δ2  => etvar (append Δ1 Δ2)
  end.

Fixpoint dom (Δ : Env) : hnat :=
  match Δ with
    | empty    => HO
    | evar Δ _ => HSm (dom Δ)
    | etvar Δ  => HSy (dom Δ)
  end.

Fixpoint tshiftEnv (c : hnat) (Δ : Env) : Env :=
  match Δ with
    | empty     => empty
    | evar Δ T  => evar (tshiftEnv c Δ) (tshiftTy (c + dom Δ) T)
    | etvar Δ   => etvar (tshiftEnv c Δ)
  end.

Fixpoint tsubstEnv (X : hnat) (T' : Ty) (Δ : Env) : Env :=
  match Δ with
    | empty    => empty
    | evar Δ T => evar (tsubstEnv X T' Δ) (tsubstTy (X + dom Δ) T' T)
    | etvar Δ  => etvar (tsubstEnv X T' Δ)
  end.

(******************************************************************************)
(* Context lookups.                                                           *)
(******************************************************************************)

Inductive lookup_etvar : Env → nat → Prop :=
  | lookup_etvar_here {Γ} :
      lookup_etvar (etvar Γ) O
  | lookup_etvar_there_evar {Γ T X} :
      lookup_etvar Γ X →
      lookup_etvar (evar Γ T) X
  | lookup_etvar_there_etvar {Γ X} :
      lookup_etvar Γ X →
      lookup_etvar (etvar Γ) (S X).
Hint Constructors lookup_etvar.

Inductive lookup_evar : Env → nat → Ty → Prop :=
  | lookup_evar_here {Γ T} :
      lookup_evar (evar Γ T) O T
  | lookup_evar_there_evar {Γ T T' X} :
      lookup_evar Γ X T →
      lookup_evar (evar Γ T') (S X) T
  | lookup_evar_there_etvar {Γ T X} :
      lookup_evar Γ X T →
      lookup_evar (etvar Γ) X (tshiftTy HO T).
Hint Constructors lookup_evar.

(******************************************************************************)
(* Well-formed types.                                                         *)
(******************************************************************************)

Inductive wfTy : Env → Ty → Prop :=
  | wf_tvar {Γ X} :
      lookup_etvar Γ X → wfTy Γ (tvar X)
  | wf_tarr {Γ T1 T2} :
      wfTy Γ T1 → wfTy Γ T2 → wfTy Γ (tarr T1 T2)
  | wf_tall {Γ T} :
      wfTy (etvar Γ) T → wfTy Γ (tall T)
  | wf_texist {Γ T} :
      wfTy (etvar Γ) T → wfTy Γ (texist T).
Hint Constructors wfTy.


From Coq Require Import QArith Qround.

Require Arith2.

Lemma Q_floor_via_Z (a: Z) (b: positive):
  Qfloor (a # b) = (a / Zpos b)%Z.
Proof.
rewrite Zdiv_Qdiv.
rewrite Qmake_Qdiv.
trivial.
Qed.

Lemma Q_ceiling_via_Z (a: Z) (b: positive):
  Qceiling (a # b) = (- ((- a) / Zpos b))%Z.
Proof.
unfold Qround.Qceiling.
rewrite Qmake_Qdiv.
rewrite Zdiv_Qdiv.
rewrite inject_Z_opp.
f_equal. f_equal.
unfold Qdiv. unfold Qmult. cbn.
repeat rewrite Z.mul_1_r.
trivial.
Qed.

Lemma Q_ceiling_via_Z_floor (a: Z) (b: positive):
  Qceiling (a # b) = ((a + Z.pos b - 1) / Zpos b)%Z.
Proof.
rewrite Q_ceiling_via_Z.
apply Arith2.Z_ceiling_via_floor.
rewrite<- Z.leb_le.
trivial.
Qed.


From stars Require Import definitions.

Ltac lift_distr H := intros x y z; symmetry; apply H.
Ltac auto_resolve := repeat split; c.

(*** Arithmetic of Boolean logic. *)
Section Boolean.

Global Instance : Equiv bool := eq.
Global Instance : Zero bool := false.
Global Instance : One bool := true.
Global Instance : Add bool := orb.
Global Instance : Mul bool := andb.
Global Instance : Star bool := λ _, 1.

Global Instance : Kleene_Algebra bool.
Proof. repeat split; repeat intros []; cbn; done. Qed.

End Boolean.

(*** Arithmetic of Peano numbers. *)
Section Natural.

Global Instance : Equiv nat := eq.
Global Instance : Zero nat := 0%nat.
Global Instance : One nat := 1%nat.
Global Instance : Join nat := Nat.max.
Global Instance : Meet nat := Nat.min.
Global Instance : Add nat := Nat.add.
Global Instance : Mul nat := Nat.mul.

Global Instance : Assoc (=) max := Nat.max_assoc.
Global Instance : Assoc (=) min := Nat.min_assoc.
Global Instance : Assoc (=) add := Nat.add_assoc.
Global Instance : Assoc (=) mul := Nat.mul_assoc.

Global Instance : Comm (=) max := Nat.max_comm.
Global Instance : Comm (=) min := Nat.min_comm.
Global Instance : Comm (=) add := Nat.add_comm.
Global Instance : Comm (=) mul := Nat.mul_comm.

Global Instance : IdemP (=) max := Nat.max_id.
Global Instance : IdemP (=) min := Nat.min_id.
Global Instance : Absorb (=) max min := Nat.min_max_absorption.
Global Instance : Absorb (=) min max := Nat.max_min_absorption.

Global Instance : LeftId (=) 0 max := Nat.max_0_l.
Global Instance : RightId (=) 0 max := Nat.max_0_r.
Global Instance : LeftId (=) 0 add := Nat.add_0_l.
Global Instance : RightId (=) 0 add := Nat.add_0_r.
Global Instance : LeftId (=) 1 mul := Nat.mul_1_l.
Global Instance : RightId (=) 1 mul := Nat.mul_1_r.

Global Instance : LeftAbsorb (=) 0 min := Nat.min_0_l.
Global Instance : RightAbsorb (=) 0 min := Nat.min_0_r.
Global Instance : LeftAbsorb (=) 0 mul := Nat.mul_0_l.
Global Instance : RightAbsorb (=) 0 mul := Nat.mul_0_r.

Global Instance : LeftDistr (=) mul add := Nat.mul_add_distr_l.
Global Instance : RightDistr (=) mul add := Nat.mul_add_distr_r.
Global Instance : LeftDistr (=) add min. lift_distr Nat.add_min_distr_l. Qed.
Global Instance : RightDistr (=) add min. lift_distr Nat.add_min_distr_r. Qed.
Global Instance : LeftDistr (=) add min. lift_distr Nat.add_min_distr_l. Qed.
Global Instance : RightDistr (=) add min. lift_distr Nat.add_min_distr_r. Qed.

Global Instance : Lattice nat. auto_resolve. Qed.
Global Instance : Semiring nat. auto_resolve. Qed.

End Natural.

(*** Arithmetic of binary numbers. *)
Section Positive.

Global Instance : Equiv N := eq.
Global Instance : Zero N := 0%N.
Global Instance : One N := 1%N.
Global Instance : Join N := N.max.
Global Instance : Meet N := N.min.
Global Instance : Add N := N.add.
Global Instance : Mul N := N.mul.

Global Instance : Assoc (=) join := N.max_assoc.
Global Instance : Assoc (=) meet := N.min_assoc.
Global Instance : Assoc (=) add := N.add_assoc.
Global Instance : Assoc (=) mul := N.mul_assoc.

Global Instance : Comm (=) join := N.max_comm.
Global Instance : Comm (=) meet := N.min_comm.
Global Instance : Comm (=) add := N.add_comm.
Global Instance : Comm (=) mul := N.mul_comm.

Global Instance : IdemP (=) join := N.max_id.
Global Instance : IdemP (=) meet := N.min_id.
Global Instance : Absorb (=) join meet := N.min_max_absorption.
Global Instance : Absorb (=) meet join := N.max_min_absorption.

Global Instance : LeftId (=) 0 join := N.max_0_l.
Global Instance : RightId (=) 0 join := N.max_0_r.
Global Instance : LeftId (=) 0 add := N.add_0_l.
Global Instance : RightId (=) 0 add := N.add_0_r.
Global Instance : LeftId (=) 1 mul := N.mul_1_l.
Global Instance : RightId (=) 1 mul := N.mul_1_r.

Global Instance : LeftAbsorb (=) 0 meet := N.min_0_l.
Global Instance : RightAbsorb (=) 0 meet := N.min_0_r.
Global Instance : LeftAbsorb (=) 0 mul := N.mul_0_l.
Global Instance : RightAbsorb (=) 0 mul := N.mul_0_r.

Global Instance : LeftDistr (=) mul add := N.mul_add_distr_l.
Global Instance : RightDistr (=) mul add := N.mul_add_distr_r.
Global Instance : LeftDistr (=) add meet. lift_distr N.add_min_distr_l. Qed.
Global Instance : RightDistr (=) add meet. lift_distr N.add_min_distr_r. Qed.
Global Instance : LeftDistr (=) add meet. lift_distr N.add_min_distr_l. Qed.
Global Instance : RightDistr (=) add meet. lift_distr N.add_min_distr_r. Qed.

Global Instance : Lattice N. auto_resolve. Qed.
Global Instance : Semiring N. auto_resolve. Qed.

End Positive.

(*** Arithmetic of rational numbers. *)
Section Rational.

Coercion inject_Z : Z >-> Q.

Global Instance : Equiv Q := Qeq.
Global Instance : Zero Q := 0%Z.
Global Instance : One Q := 1%Z.
Global Instance : Add Q := Qplus.
Global Instance : Mul Q := Qmult.

Global Instance : RelDecision Qeq := Qeq_dec.
Global Instance : Assoc (≡) add := Qplus_assoc.
Global Instance : Assoc (≡) mul := Qmult_assoc.
Global Instance : Comm (≡) add := Qplus_comm.
Global Instance : Comm (≡) mul := Qmult_comm.
Global Instance : LeftId (≡) 0 add := Qplus_0_l.
Global Instance : RightId (≡) 0 add := Qplus_0_r.
Global Instance : LeftId (≡) 1 mul := Qmult_1_l.
Global Instance : RightId (≡) 1 mul := Qmult_1_r.
Global Instance : LeftAbsorb (≡) 0 mul := Qmult_0_l.
Global Instance : RightAbsorb (≡) 0 mul := Qmult_0_r.
Global Instance : LeftDistr (≡) mul add := Qmult_plus_distr_r.
Global Instance : RightDistr (≡) mul add := Qmult_plus_distr_l.
Global Instance : Semiring Q. auto_resolve. Qed.

End Rational.

(*** Arithmetic of minimum and addition. *)
(* Tropical is a reference to the climate of Brazil, where Imre Simon lived. *)
(* Imre Simon (1943-2009) founded the topic of tropical mathematics. *)
Section Tropical.

Variable X : Type.
Context `{Equiv X, Equivalence X (≡), Meet X, Add X, Zero X}.
Context `{Semilattice X (≡) meet, Monoid X (≡) add 0}.
Context `{LeftAbsorb X (≡) 0 meet, RightAbsorb X (≡) 0 meet}.
Context `{LeftDistr X (≡) add meet, RightDistr X (≡) add meet}.

Inductive trop := Tropical (x : X) | TInfinity.

Global Instance : Equiv trop := λ a b,
  match a, b with
  | Tropical x, Tropical y => x ≡ y
  | TInfinity, TInfinity => True
  | _, _ => False
  end.

Global Instance : Infinity trop := TInfinity.
Global Instance : Zero trop := TInfinity.
Global Instance : One trop := Tropical 0.

Global Instance : Add trop :=
  λ a b, match a, b with
  | TInfinity, _ => b
  | _, TInfinity => a
  | Tropical x, Tropical y => Tropical (x ∧ y)
  end.

Global Instance : Mul trop :=
  λ a b, match a, b with
  | TInfinity, _ => TInfinity
  | _, TInfinity => TInfinity
  | Tropical x, Tropical y => Tropical (x + y)
  end.

Global Instance trop_star : Star trop :=
  λ _, 1.

Global Instance : Kleene_Algebra trop.
Proof.
repeat split.
4,9: intros [] [] A [] [] B; cbn in *; try done; rewrite A, B; done.
all: repeat intros []; cbn; try done; f_equal. apply Equivalence_Transitive.
apply (assoc meet). apply (comm meet). apply (assoc add).
apply (left_id 0 add). apply (right_id 0 add). apply (idemp meet).
1,2: rewrite (left_id 0 add); intros; apply (idemp meet).
1,2: rewrite (right_id 0 add); intros; apply (idemp meet).
Qed.

End Tropical.

Arguments Tropical {_}.
Arguments TInfinity {_}.

(*** One-point compactification of the rational numbers. *)
Section Compact.

Inductive frac :=
  | Frac (q : Q)
  | Inf.

Coercion Frac : Q >-> frac.

Global Instance : Equiv frac := λ x y,
  match x, y with
  | Frac p, Frac q => p == q
  | Inf, Inf => True
  | _, _ => False
  end.

Global Instance : Infinity frac := Inf.
Global Instance : Zero frac := 0%Z.
Global Instance : One frac := 1%Z.

Definition frac_simplify (x : frac) :=
  match x with
  | Inf => Inf
  | Frac p => Frac (Qred p)
  end.

Definition frac_add (x y : frac) : frac :=
  match x, y with
  | Inf, _ => Inf
  | _, Inf => Inf
  | Frac p, Frac q => (p + q)%Q
  end.

Definition frac_mul (x y : frac) : frac :=
  match x, y with
  | Frac (0 # _), Inf => 0
  | Inf, Frac (0 # _) => 0
  | Inf, _ => Inf
  | _, Inf => Inf
  | Frac p, Frac q => (p * q)%Q
  end.

Global Instance add_frac : Add frac := λ x y, frac_simplify (frac_add x y).
Global Instance mul_frac : Mul frac := λ x y, frac_simplify (frac_mul x y).

Global Instance : Star frac := λ x,
  match x with
  | Frac ((Z.pos m # n) as p) =>
    if Pos.eqb m n then Inf else Qred (/(1 - p))
  | Frac p => Qred (/(1 - p))
  | Inf => Inf
  end.

Global Instance : Equivalence (≡).
Proof.
split.
- intros []; done.
- intros [] [] H; done.
- intros [] [] [] H H'; try done; cbn; trans q0; done.
Qed.

Global Instance : RelDecision (≡).
Proof.
intros [p|] [q|]. apply (decide (p ≡ q)).
1,2: right; intro H; done. left; done.
Qed.

(***
The general proof strategy is as follows:
1. [_intros] Destruct frac values (Inf, 0, positive and negative rationals).
2. [_unwrap] Unfold add and mul, and remove frac_simplify.
3. [_reduce, _simpl] Rewrite with Inf reductions, or evaluate the term.
*)

Ltac _intro := let i := fresh "i" in intros [[[] i]|].
Ltac _intros := repeat (_intro || let H := fresh "H" in intro H).

Local Lemma zero_frac i : 0 # i ≡ zero.
Proof. apply Qreduce_zero. Qed.

Local Lemma frac_simplify_id x : frac_simplify x ≡ x.
Proof. destruct x. apply Qred_correct. done. Qed.

Local Lemma _red_0 x : frac_add Inf x = Inf. done. Qed.
Local Lemma _red_1 x : frac_add x Inf = Inf. revert x; _intro; done. Qed.
Local Lemma _red_2 i j : frac_mul Inf (Z.pos i # j) = Inf. done. Qed.
Local Lemma _red_3 i j : frac_mul Inf (Z.neg i # j) = Inf. done. Qed.
Local Lemma _red_4 i j : frac_mul (Z.pos i # j) Inf = Inf. done. Qed.
Local Lemma _red_5 i j : frac_mul (Z.neg i # j) Inf = Inf. done. Qed.

Ltac _unwrap := unfold add, add_frac, mul, mul_frac; rewrite ?frac_simplify_id.
Ltac _red_step := rewrite ?_red_0, ?_red_1, ?_red_2, ?_red_3, ?_red_4, ?_red_5.
Ltac _reduce := rewrite ?zero_frac; repeat _red_step.
Ltac _simpl := cbn in *; try done.

Section Morphisms.

Lemma frac_add_hom p q : frac_add (Frac p) (Frac q) ≡ Frac (p + q)%Q.
Proof. done. Qed.

Lemma frac_mul_hom p q : frac_mul (Frac p) (Frac q) ≡ Frac (p * q)%Q.
Proof. destruct p as [[] i], q as [[] j]; done. Qed.

Lemma add_frac_hom p q : Frac p + Frac q ≡ Frac (p + q)%Q.
Proof. _unwrap; apply frac_add_hom. Qed.

Lemma mul_frac_hom p q : Frac p * Frac q ≡ Frac (p * q)%Q.
Proof. _unwrap; apply frac_mul_hom. Qed.

Global Instance : Proper ((≡) ==> (≡)) Frac.
Proof. intros x y H; done. Qed.

Global Instance proper_frac_simplify : Proper ((≡) ==> (≡)) frac_simplify.
Proof. intros x y H; rewrite ?frac_simplify_id; done. Qed.

Global Instance proper_frac_add : Proper ((≡) ==> (≡) ==> (≡)) frac_add.
Proof. _intros; _simpl; apply Qplus_comp; done. Qed.

Global Instance proper_frac_mul : Proper ((≡) ==> (≡) ==> (≡)) frac_mul.
Proof. _intros; _simpl; apply Qmult_comp; done. Qed.

Ltac lift_proper H := repeat intros ?; apply proper_frac_simplify, H; done.

Global Instance : Proper ((≡) ==> (≡) ==> (≡)) add.
Proof. lift_proper proper_frac_add. Qed.

Global Instance : Proper ((≡) ==> (≡) ==> (≡)) mul.
Proof. lift_proper proper_frac_mul. Qed.

Global Instance : Proper ((≡) ==> (≡)) star.
Proof.
_intros; _simpl; try (rewrite H; done).
destruct (p =? i)%positive eqn:E.
- apply Pos.eqb_eq in E; subst. unfold Qeq in H; cbn in H.
  rewrite Z.mul_comm in H; apply Z.mul_cancel_r, Zpos_eq_iff in H; subst.
  rewrite Pos.eqb_refl; done. done.
- apply Pos.eqb_neq in E; destruct (p0 =? i0)%positive eqn:E0.
  apply Pos.eqb_eq in E0; subst. unfold Qeq in H; cbn in H.
  rewrite Z.mul_comm in H; apply Z.mul_cancel_l, Zpos_eq_iff in H; subst; done.
  cbn; rewrite H; done.
Qed.

End Morphisms.

Ltac _transfer := repeat (rewrite ?frac_add_hom, ?frac_mul_hom).
Ltac lift_Qplus H := repeat intros []; _unwrap; _reduce; try done; apply H.
Ltac lift_Qmult H := _intros; _unwrap; _reduce; try done; apply H.

Global Instance : Assoc (≡) add. lift_Qplus Qplus_assoc. Qed.
Global Instance : Comm (≡) add. lift_Qplus Qplus_comm. Qed.
Global Instance : Comm (≡) mul. lift_Qmult Qmult_comm. Qed.
Global Instance : LeftId (≡) 0 add. lift_Qplus Qplus_0_l. Qed.
Global Instance : RightId (≡) 0 add. lift_Qplus Qplus_0_r. Qed.
Global Instance : LeftId (≡) 1 mul. lift_Qmult Qmult_1_l. Qed.
Global Instance : RightId (≡) 1 mul. lift_Qmult Qmult_1_r. Qed.
Global Instance : LeftAbsorb (≡) 0 mul. lift_Qmult Qmult_0_l. Qed.
Global Instance : RightAbsorb (≡) 0 mul. lift_Qmult Qmult_0_r. Qed.

Global Instance : Assoc (≡) mul.
Proof.
intros x y z; _unwrap; revert x y z.
_intros; _reduce; try done; apply Qmult_assoc.
Qed.

Global Instance : LeftDistr (≡) mul add.
Proof.
intros x y z; _unwrap; revert x y z.
_intros; _reduce; _transfer; try done; try apply Qmult_plus_distr_r.
(* If x = ∞, y = 1, z = -1, then the equality fails. *)
Admitted.

Global Instance : RightDistr (≡) mul add.
Proof.
intros x y z; _unwrap; revert x y z.
_intros; _reduce; _transfer; try done; try apply Qmult_plus_distr_l.
(* If x = 1, y = -1, z = ∞, then the equality fails. *)
Admitted.

Lemma star_frac_neq_1 q :
  Frac q ≢ 1 -> (Frac q){*} ≡ /(1 - q).
Proof.
destruct q as [[] i]; intros; _simpl; try apply Qred_correct.
destruct (p =? i)%positive eqn:E. 2: apply Qred_correct.
exfalso; apply H; apply Pos.eqb_eq in E; subst.
unfold Qeq; cbn; apply Z.mul_comm.
Qed.

Lemma expand_star_frac q :
  ¬ q == 1 -> / (1 - q) == 1 + q * / (1 - q).
Proof.
intros Hq; rewrite <-Qmult_inv_r with (x:=(1 - q)%Q) at 2.
- rewrite <-Qmult_plus_distr_l; unfold Qminus at 2.
  rewrite <-Qplus_assoc, (Qplus_comm _ q).
  rewrite Qplus_opp_r, Qplus_0_r, Qmult_1_l; done.
- intros Hq'; apply Hq; unfold Qminus in Hq'.
  symmetry; rewrite <-Qplus_0_r, <-Qplus_opp_r with (q:=q).
  rewrite (Qplus_comm q), Qplus_assoc, Hq', Qplus_0_l; done.
Qed.

Global Instance : Star_Semiring frac.
Proof.
repeat split; try c.
all: intros []. 2,4: done.
all: destruct (decide (Frac q ≡ 1)) as [->|Hq]. 1,3: done.
all: rewrite (star_frac_neq_1 _ Hq). 2: rewrite (comm mul).
all: rewrite (expand_star_frac _ Hq) at 1.
all: rewrite <-add_frac_hom, <-mul_frac_hom; done.
Qed.

End Compact.


(* Copyright (c) 2006, 2011-2012, 2015, 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 Arith List Lia.

Require Import Cpdt.CpdtTactics Cpdt.Coinductive.

Require Extraction.

Set Implicit Arguments.
Set Asymmetric Patterns.
(* 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.
  Proof.
    Admitted.
    (* 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 (_ >= _) => lia.
  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 thunk_eq_coind; 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. *)

Set Universe Polymorphism.

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.
  try subst B. (* This line expected to fail in Coq 8.4 and succeed in Coq 8.6. *)
  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.
  try subst A. (* Same as above *)
  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. *)


(** Introduction to Coq exercises for the Bindel group meeting 09/23/2022 **)

(** Note: In the web-based environment for Coq, you can move down through proof
   scripts using Alt+p and up through proof scripts using Alt+n . **)

(** Example Set 1 **)
Theorem id_test :
forall P : Prop, P -> P.
Proof.
(* apply the inference rule of implication elimination, using the intros tactic *)
intros P.
(* The Show Proof command displays the proof object that's been built so far. Incomplete
  parts of the proof -- holes -- are denoted with a ? prefix. *)
Show Proof.
(* apply the inference rule of implication elimination, using the intros tactic *)
intros X.
Show Proof.
(* TODO *)
Admitted.

(* We can see id_test's proof object and type using the Print command. *)

Print id_test.

(* We can provide the proof object directly as an alternative proof. *)

Theorem id_test_alt:
forall P : Prop, P -> P.
Proof.
apply (fun P => fun X => X).
Qed.

Print id_test_alt.

Theorem app_test1 :
  forall A B , (A -> B) -> A -> B.
Proof.
(* Function application binds tighter than abstraction. *)
apply (fun A B => fun HAB => fun HA => HAB HA).
Qed.

Theorem app_test2 :
  forall A B C, (A -> B) -> (B -> C) -> A -> C.
Proof.
apply (fun A B C => fun HAB HBC HA => HBC (HAB HA)).
Qed.

Theorem app_test3 :
  forall A B C, (A -> B -> C) -> A -> B -> C.
Proof.
(* Function application is left associative. *)
apply (fun A B C => fun HABC A B => HABC A B).
Qed.

(** Example Set 2 : Inductive Definitions **)
Module BindelGroupIntro.
(** NOTE : Module System **)
(** All declarations between Module X and End X markers are referred to by names like X.foo
in the remainder of the file (instead of just foo). Use this feature to limit the scope of
definitions, so that you can reuse names. **)

(** From Software Foundations: "The set of built-in features in Coq is extremely small.
For example, instead of providing the usual palette of atomic data types
(booleans, integers, strings, etc.), Coq offers a powerful mechanism for
defining new data types from scratch, with all these familiar types as instances.
Naturally, the Coq distribution comes with an extensive standard library providing
definitions of booleans, numbers, and many common data structures like lists and hash tables.
But there is nothing magic or primitive about these library definitions". **)

(** We can define a new type inductively, called day, whose members are monday, tuesday, etc. **)

Inductive day : Type :=
  | monday
  | tuesday
  | wednesday
  | thursday
  | friday
  | saturday
  | sunday.

(** An Inductive definition does two things:
(#1.) It defines a set of new constructors. monday is a constructor.
(#2.) It groups them into a new named type. day is a new named type. **)

(** day is the type of the constructor monday **)
Check monday.
(** Set is the type of day. **)
Check day.
(** Type is the type of Set. *)
Check Set.

(** Having defined day, we can write functions that operate on days. **)

Definition next_weekday (d:day) : day :=
  match d with
  | monday => tuesday
  | tuesday => wednesday
  | wednesday => thursday
  | thursday => friday
  | _ => monday
  end.

End BindelGroupIntro.

(** Every nat is either built with O or with S. **)
Print nat.
(** What's the type of S ? **)
Check S.
(* If a nat is built with S we can extract the underlying natural number. *)
Check S O.

Definition minus_one (n : nat) : nat :=
  match n with
    | 0 => 0
    | S n' => n'
  end.

Check minus_one.

(** Observe that minus_one and S appear to have the same type.
 In fact S is more than just a function nat -> nat: we can pattern
 match on S but we can't pattern match on minus_one. **)

(*
Definition minus_two (n: nat) : nat :=
  match n with
    | O => O
    | minus_one n' => minus_one n'
  end.
*)

(** TODO : define minus_two **)

(** Coq provides several evaluation mechanisms. **)
Eval compute in (minus_one (S ( S 0))).

(** There are different ways to define objects in
 Coq. We can construct a definition of
 "minus_one" in "proof mode." **)
Definition minus_one_transparent (n : nat) : nat.
exact (n - 1). Defined.

Print minus_one.
Print minus_one_transparent.

(** Defined marks a definition as transparent,
allowing it to be unfolded; Qed marks a definition as
opaque, preventing unfolding. *)
Eval compute in (minus_one_transparent 3).

Definition minus_one_opaque (n : nat) : nat.
exact (n - 1). Qed.

Eval compute in (minus_one_opaque 3).

Lemma equal_defs n :
minus_one n = minus_one_transparent n.
Proof.
unfold minus_one_transparent, minus_one.
(* Perform case analysis by generating a subgoal
 for each constructor of the inductive type using the
 destruct tactic *)
destruct n.
- (* bullets allow us to focus on a subgoal *)
(* how do we finish this proof? *)
(* we can look at available lemmas *)
Search (0 - _)%nat.
Abort.

(** a standard prelude module provides the standard logic connectives,
and a few arithmetic notions. If you want to load and open other modules
from the library, you have to use the Require command. **)

Require Import Arith.

(* we can look at available lemmas *)
Search ( 0 - _)%nat.

Lemma equal_defs n :
minus_one n = minus_one_transparent n.
Proof.
unfold minus_one_transparent, minus_one.
destruct n.
-
rewrite Nat.sub_0_l.
reflexivity.
-
rewrite Nat.sub_1_r.
rewrite Nat.pred_succ.
reflexivity.
Qed.

(** Example Set 3: Recursive Functions & Inductively Defined Properties **)

(** Recursive functions are defined with the keyword Fixpoint instead of Definition.**)
Print fact.

Eval compute in (fact 4).

(** We can express the concept "m is n!" as an inductively defined property of
  natural numbers. **)

Inductive fact_rel : nat -> nat -> Prop :=
  | zero : fact_rel 0 1 (* "1 is 0!" *)
  | plus_one : forall n m, fact_rel n m -> fact_rel (n + 1) (( n + 1) * m)
    (* "if m is n! then (n+1) * m is (n + 1)!" *)
    .

Check zero.

(* import some tactics for integers *)
Require Import Lia.

Theorem fact_correct1 :
  forall (n m : nat), fact_rel n (fact n).
Proof.
intros; induction n.
-
apply zero.
(* can also use the tactic "constructor" *)
-
unfold fact.
replace (S n) with (n + 1)%nat by lia.
constructor.
fold fact.
exact IHn.
Qed.

(** Example Set 4: Real Analysis **)

(** The literature: "Coquelicot: A User-Friendly Library of Real Analysis for Coq"
  by Sylvie Boldo and co-authors. **)
Require Import Reals.
(** "The formalization of real numbers from the standard library is axiomatic
rather than definitional. Instead of building reals as Cauchy sequences or
Dedekind cuts of rational numbers and proving their properties, Coq developers
have assumed the existence of a set with the usual properties of the real line." **)

Require Import Coquelicot.Coquelicot.


Require Import ZArith.

Require Import List.

Set Implicit Arguments.

Open Scope Z_scope.
Inductive Sorted : list Z -> Prop :=
  | sorted0 : Sorted nil
  | sorted1 : forall z:Z, Sorted (z :: nil)
  | sorted2 : forall (z1 z2:Z) (l:list Z),
        z1 <= z2 -> Sorted (z2 :: l) -> Sorted (z1 :: z2 :: l).
Close Scope Z_scope.

Fixpoint count (z:Z) (l:list Z) {struct l} : nat :=
  match l with
  | nil => 0%nat     (* %nat to force the interpretation in nat, since have we open Z_scope *)
  | (z' :: l') =>
      match Z.eq_dec z z' with
      | left _ => S (count z l')
      | right _ => count z l'
      end
  end.

Definition Perm (l1 l2:list Z) : Prop :=
                                 forall z, count z l1 = count z l2.

Lemma Perm_cons : forall a l1 l2, Perm l1 l2 -> Perm (a::l1) (a::l2).
Proof.
  intros.
  unfold Perm. intro. simpl. elim (Z.eq_dec z a).
  - intros. rewrite Nat.succ_inj_wd. unfold Perm in H. apply H.
  - intros. unfold Perm in H; apply H.
Qed.

Lemma Perm_cons_cons : forall x y l, Perm (x::y::l) (y::x::l).
Proof.
  intros.
  unfold Perm.
  intros. simpl. elim (Z.eq_dec z x).
  - intros. elim (Z.eq_dec z y).
    + intros; reflexivity.
    + intros; reflexivity.
  - intros; elim (Z.eq_dec z y).
    + intros; reflexivity.
    + intros; reflexivity.
Qed.

(* ------------ *)

Require Import Recdef.

Function merge (p:list Z*list Z)
{measure (fun p=>(length (fst p))+(length (snd p)))} : list Z :=
  match p with
  | (nil,l) => l
  | (l,nil) => l
  | (x::xs,y::ys) => if Z_lt_ge_dec x y
                     then x::(merge (xs,y::ys))
                     else y::(merge (x::xs,ys))
  end.
  intros.
  simpl; auto with arith.
  intros. simpl. omega.
  Qed.

Theorem merge_correct : forall (p:list Z*list Z), Perm (merge p) ((fst p) ++ (snd p)) /\ Sorted (merge p).
Proof.
  Admitted.

(* Garante que o resultado é igual ao comprimento da lista *)
Definition comprimentoLista (A : Type) (l : list A) (res : nat) : Prop :=
            forall n:nat, n < res <-> exists a:A, nth_error l n = Some a.

Theorem comprimentoLista_correct : forall (A:Type) (l : list A), { n:nat | comprimentoLista l n }.
Proof.
  intros. unfold comprimentoLista. induction l.
  - exists 0. split.
    + intros. contradict H. omega.
    + intros. destruct H. contradict H. induction n.
      * simpl. discriminate.
      * simpl; discriminate.
  - destruct IHl. exists (S x). intros. induction n.
    + simpl. split.
      * intros. exists a; reflexivity.
      * intros. omega.
    + simpl; split.
      * intros. destruct i with n. apply H0. omega.
      * intros. destruct i with n. Search (S _ < S _). apply lt_n_S. exact (H1 H).
Qed.

Definition soma (A:Type) (l : list (A*nat)) (res : nat) : Prop :=
            res = fold_right (fun x y:nat => x+y) 0 (map (fun p => snd p) l).

Theorem soma_correct : forall (A:Type) (l : list(A*nat)), { n: nat | soma l n}.
Proof.
  unfold soma. intros. induction l.
  - simpl. exists 0. reflexivity.
  - simpl. destruct IHl. exists ((snd a) + x). subst. reflexivity.
Qed.

(* Set Extraction AccessOpaque.*)
Require Extraction.

Extraction Language Haskell.

Recursive Extraction comprimentoLista_correct.

Recursive Extraction soma_correct.