(* Copyright (c) 2008-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 List.

Require Import Cpdt.CpdtTactics.

Set Implicit Arguments.
Set Asymmetric Patterns.
(* end hide *)

(** %\part{Basic Programming and Proving}

\chapter{Introducing Inductive Types}% *)

(** The logical foundation of Coq is the Calculus of Inductive Constructions, or CIC.  In a sense, CIC is built from just two relatively straightforward features: function types and inductive types.  From this modest foundation, we can prove essentially all of the theorems of math and carry out effectively all program verifications, with enough effort expended.  This chapter introduces induction and recursion for functional programming in Coq.  Most of our examples reproduce functionality from the Coq standard library, and I have tried to copy the standard library's choices of identifiers, where possible, so many of the definitions here are already available in the default Coq environment.

The last chapter took a deep dive into some of the more advanced Coq features, to highlight the unusual approach that I advocate in this book.  However, from this point on, we will rewind and go back to basics, presenting the relevant features of Coq in a more bottom-up manner.  A useful first step is a discussion of the differences and relationships between proofs and programs in Coq. *)

(** * Proof Terms *)

(** Mainstream presentations of mathematics treat proofs as objects that exist outside of the universe of mathematical objects.  However, for a variety of reasoning tasks, it is convenient to encode proofs, traditional mathematical objects, and programs within a single formal language.  Validity checks on mathematical objects are useful in any setting, to catch typos and other uninteresting errors.  The benefits of static typing for programs are widely recognized, and Coq brings those benefits to both mathematical objects and programs via a uniform mechanism.  In fact, from this point on, we will not bother to distinguish between programs and mathematical objects.  Many mathematical formalisms are most easily encoded in terms of programs.

Proofs are fundamentally different from programs, because any two proofs of a theorem are considered equivalent, from a formal standpoint if not from an engineering standpoint.  However, we can use the same type-checking technology to check proofs as we use to validate our programs.  This is the%\index{Curry-Howard correspondence}% _Curry-Howard correspondence_ %\cite{Curry,Howard}%, an approach for relating proofs and programs.  We represent mathematical theorems as types, such that a theorem's proofs are exactly those programs that type-check at the corresponding type.

The last chapter's example already snuck in an instance of Curry-Howard.  We used the token [->] to stand for both function types and logical implications.  One reasonable conclusion upon seeing this might be that some fancy overloading of notations is at work.  In fact, functions and implications are precisely identical according to Curry-Howard!  That is, they are just two ways of describing the same computational phenomenon.

A short demonstration should explain how this can be.  The identity function over the natural numbers is certainly not a controversial program. *)

Check (fun x : nat => x).
(** [: nat -> nat] *)

(** %\smallskip{}%Consider this alternate program, which is almost identical to the last one. *)

Check (fun x : True => x).
(** [: True -> True] *)

(** %\smallskip{}%The identity program is interpreted as a proof that %\index{Gallina terms!True}%[True], the always-true proposition, implies itself!  What we see is that Curry-Howard interprets implications as functions, where an input is a proposition being assumed and an output is a proposition being deduced.  This intuition is not too far from a common one for informal theorem proving, where we might already think of an implication proof as a process for transforming a hypothesis into a conclusion.

There are also more primitive proof forms available.  For instance, the term %\index{Gallina terms!I}%[I] is the single proof of [True], applicable in any context. *)

Check I.
(** [: True] *)

(** %\smallskip{}%With [I], we can prove another simple propositional theorem. *)

Check (fun _ : False => I).
(** [: False -> True] *)

(** %\smallskip{}%No proofs of %\index{Gallina terms!False}%[False] exist in the top-level context, but the implication-as-function analogy gives us an easy way to, for example, show that [False] implies itself. *)

Check (fun x : False => x).
(** [: False -> False] *)

(** %\smallskip{}%Every one of these example programs whose type looks like a logical formula is a%\index{proof term}% _proof term_.  We use that name for any Gallina term of a logical type, and we will elaborate shortly on what makes a type logical.

In the rest of this chapter, we will introduce different ways of defining types.  Every example type can be interpreted alternatively as a type of programs or proofs.

One of the first types we introduce will be [bool], with constructors [true] and [false].  Newcomers to Coq often wonder about the distinction between [True] and [true] and the distinction between [False] and [false].  One glib answer is that [True] and [False] are types, but [true] and [false] are not.  A more useful answer is that Coq's metatheory guarantees that any term of type [bool] _evaluates_ to either [true] or [false].  This means that we have an _algorithm_ for answering any question phrased as an expression of type [bool].  Conversely, most propositions do not evaluate to [True] or [False]; the language of inductively defined propositions is much richer than that.  We ought to be glad that we have no algorithm for deciding our formalized version of mathematical truth, since otherwise it would be clear that we could not formalize undecidable properties, like almost any interesting property of general-purpose programs. *)

(** * Enumerations *)

(** Coq inductive types generalize the %\index{algebraic datatypes}%algebraic datatypes found in %\index{Haskell}%Haskell and %\index{ML}%ML.  Confusingly enough, inductive types also generalize %\index{generalized algebraic datatypes}%generalized algebraic datatypes (GADTs), by adding the possibility for type dependency.  Even so, it is worth backing up from the examples of the last chapter and going over basic, algebraic-datatype uses of inductive datatypes, because the chance to prove things about the values of these types adds new wrinkles beyond usual practice in Haskell and ML.

The singleton type [unit] is an inductive type:%\index{Gallina terms!unit}\index{Gallina terms!tt}% *)

Inductive unit : Set :=
  | tt.

(** This vernacular command defines a new inductive type [unit] whose only value is [tt].  We can verify the types of the two identifiers we introduce: *)

Check unit.
(** [unit : Set] *)

Check tt.
(** [tt : unit] *)

(** %\smallskip{}%We can prove that [unit] is a genuine singleton type. *)

Theorem unit_singleton : forall x : unit, x = tt.

(** The important thing about an inductive type is, unsurprisingly, that you can do induction over its values, and induction is the key to proving this theorem.  We ask to proceed by induction on the variable [x].%\index{tactics!induction}% *)

(* begin thide *)
  induction x.

(** The goal changes to:
[[
 tt = tt
]]
*)

  (** %\noindent{}%...which we can discharge trivially. *)

  reflexivity.
Qed.
(* end thide *)

(** It seems kind of odd to write a proof by induction with no inductive hypotheses.  We could have arrived at the same result by beginning the proof with:%\index{tactics!destruct}% [[
  destruct x.
]]

%\noindent%...which corresponds to "proof by case analysis" in classical math.  For non-recursive inductive types, the two tactics will always have identical behavior.  Often case analysis is sufficient, even in proofs about recursive types, and it is nice to avoid introducing unneeded induction hypotheses.

What exactly _is_ the %\index{induction principles}%induction principle for [unit]?  We can ask Coq: *)

Check unit_ind.
(** [unit_ind : forall P : unit -> Prop, P tt -> forall u : unit, P u] *)

(** %\smallskip{}%Every [Inductive] command defining a type [T] also defines an induction principle named [T_ind].  Recall from the last section that our type, operations over it, and principles for reasoning about it all live in the same language and are described by the same type system.  The key to telling what is a program and what is a proof lies in the distinction between the type %\index{Gallina terms!Prop}%[Prop], which appears in our induction principle; and the type %\index{Gallina terms!Set}%[Set], which we have seen a few times already.

The convention goes like this: [Set] is the type of normal types used in programming, and the values of such types are programs.  [Prop] is the type of logical propositions, and the values of such types are proofs.  Thus, an induction principle has a type that shows us that it is a function for building proofs.

Specifically, [unit_ind] quantifies over a predicate [P] over [unit] values.  If we can present a proof that [P] holds of [tt], then we are rewarded with a proof that [P] holds for any value [u] of type [unit].  In our last proof, the predicate was [(fun u : unit => u = tt)].

The definition of [unit] places the type in [Set].  By replacing [Set] with [Prop], [unit] with [True], and [tt] with [I], we arrive at precisely the definition of [True] that the Coq standard library employs!  The program type [unit] is the Curry-Howard equivalent of the proposition [True].  We might make the tongue-in-cheek claim that, while philosophers have expended much ink on the nature of truth, we have now determined that truth is the [unit] type of functional programming.

%\medskip%

We can define an inductive type even simpler than [unit]:%\index{Gallina terms!Empty\_set}% *)

Inductive Empty_set : Set := .

(** [Empty_set] has no elements.  We can prove fun theorems about it: *)

Theorem the_sky_is_falling : forall x : Empty_set, 2 + 2 = 5.
(* begin thide *)
  destruct 1.
Qed.
(* end thide *)

(** Because [Empty_set] has no elements, the fact of having an element of this type implies anything.  We use [destruct 1] instead of [destruct x] in the proof because unused quantified variables are relegated to being referred to by number.  (There is a good reason for this, related to the unity of quantifiers and implication.  At least within Coq's logical foundation of %\index{constructive logic}%constructive logic, which we elaborate on more in the next chapter, an implication is just a quantification over a proof, where the quantified variable is never used.  It generally makes more sense to refer to implication hypotheses by number than by name, and Coq treats our quantifier over an unused variable as an implication in determining the proper behavior.)

We can see the induction principle that made this proof so easy: *)

Check Empty_set_ind.
(** [Empty_set_ind : forall (P : Empty_set -> Prop) (e : Empty_set), P e] *)

(** %\smallskip{}%In other words, any predicate over values from the empty set holds vacuously of every such element.  In the last proof, we chose the predicate [(fun _ : Empty_set => 2 + 2 = 5)].

We can also apply this get-out-of-jail-free card programmatically.  Here is a lazy way of converting values of [Empty_set] to values of [unit]: *)

Definition e2u (e : Empty_set) : unit := match e with end.

(** We employ [match] pattern matching as in the last chapter.  Since we match on a value whose type has no constructors, there is no need to provide any branches.  It turns out that [Empty_set] is the Curry-Howard equivalent of [False].  As for why [Empty_set] starts with a capital letter and not a lowercase letter like [unit] does, we must refer the reader to the authors of the Coq standard library, to which we try to be faithful.

%\medskip%

Moving up the ladder of complexity, we can define the Booleans:%\index{Gallina terms!bool}\index{Gallina terms!true}\index{Gallina terms!false}% *)

Inductive bool : Set :=
| true
| false.

(** We can use less vacuous pattern matching to define Boolean negation.%\index{Gallina terms!negb}% *)

Definition negb (b : bool) : bool :=
  match b with
    | true => false
    | false => true
  end.

(** An alternative definition desugars to the above, thanks to an %\index{Gallina terms!if}%[if] notation overloaded to work with any inductive type that has exactly two constructors: *)

Definition negb' (b : bool) : bool :=
  if b then false else true.

(** We might want to prove that [negb] is its own inverse operation. *)

Theorem negb_inverse : forall b : bool, negb (negb b) = b.
(* begin thide *)
  destruct b.

  (** After we case-analyze on [b], we are left with one subgoal for each constructor of [bool].
[[
  2 subgoals

  ============================
   negb (negb true) = true

subgoal 2 is

 negb (negb false) = false
]]

The first subgoal follows by Coq's rules of computation, so we can dispatch it easily: *)

  reflexivity.

(** Likewise for the second subgoal, so we can restart the proof and give a very compact justification.%\index{Vernacular commands!Restart}% *)

Restart.

  destruct b; reflexivity.
Qed.
(* end thide *)

(** Another theorem about Booleans illustrates another useful tactic.%\index{tactics!discriminate}% *)

Theorem negb_ineq : forall b : bool, negb b <> b.
(* begin thide *)
  destruct b; discriminate.
Qed.
(* end thide *)

(** The [discriminate] tactic is used to prove that two values of an inductive type are not equal, whenever the values are formed with different constructors.  In this case, the different constructors are [true] and [false].

At this point, it is probably not hard to guess what the underlying induction principle for [bool] is. *)

Check bool_ind.
(** [bool_ind : forall P : bool -> Prop, P true -> P false -> forall b : bool, P b] *)

(** %\smallskip{}%That is, to prove that a property describes all [bool]s, prove that it describes both [true] and [false].

There is no interesting Curry-Howard analogue of [bool].  Of course, we can define such a type by replacing [Set] by [Prop] above, but the proposition we arrive at is not very useful.  It is logically equivalent to [True], but it provides two indistinguishable primitive proofs, [true] and [false].   In the rest of the chapter, we will skip commenting on Curry-Howard versions of inductive definitions where such versions are not interesting. *)

(** * Simple Recursive Types *)

(** The natural numbers are the simplest common example of an inductive type that actually deserves the name.%\index{Gallina terms!nat}\index{Gallina terms!O}\index{Gallina terms!S}% *)

Inductive nat : Set :=
| O : nat
| S : nat -> nat.

(** The constructor [O] is zero, and [S] is the successor function, so that [0] is syntactic sugar for [O], [1] for [S O], [2] for [S (S O)], and so on.

Pattern matching works as we demonstrated in the last chapter:%\index{Gallina terms!pred}% *)

Definition isZero (n : nat) : bool :=
  match n with
    | O => true
    | S _ => false
  end.

Definition pred (n : nat) : nat :=
  match n with
    | O => O
    | S n' => n'
  end.

(** We can prove theorems by case analysis with [destruct] as for simpler inductive types, but we can also now get into genuine inductive theorems.  First, we will need a recursive function, to make things interesting.%\index{Gallina terms!plus}% *)

Fixpoint plus (n m : nat) : nat :=
  match n with
    | O => m
    | S n' => S (plus n' m)
  end.

(** Recall that [Fixpoint] is Coq's mechanism for recursive function definitions.  Some theorems about [plus] can be proved without induction. *)

Theorem O_plus_n : forall n : nat, plus O n = n.
(* begin thide *)
  intro; reflexivity.
Qed.
(* end thide *)

(** Coq's computation rules automatically simplify the application of [plus], because unfolding the definition of [plus] gives us a [match] expression where the branch to be taken is obvious from syntax alone.  If we just reverse the order of the arguments, though, this no longer works, and we need induction. *)

Theorem n_plus_O : forall n : nat, plus n O = n.
(* begin thide *)
  induction n.

(** Our first subgoal is [plus O O = O], which _is_ trivial by computation. *)

  reflexivity.

(** Our second subgoal requires more work and also demonstrates our first inductive hypothesis.

[[
  n : nat
  IHn : plus n O = n
  ============================
   plus (S n) O = S n

]]

We can start out by using computation to simplify the goal as far as we can.%\index{tactics!simpl}% *)

  simpl.

(** Now the conclusion is [S (plus n O) = S n].  Using our inductive hypothesis: *)

  rewrite IHn.

(** %\noindent{}%...we get a trivial conclusion [S n = S n]. *)

  reflexivity.

(** Not much really went on in this proof, so the [crush] tactic from the [CpdtTactics] module can prove this theorem automatically. *)

Restart.

  induction n; crush.
Qed.
(* end thide *)

(** We can check out the induction principle at work here: *)

Check nat_ind.
(** %\vspace{-.15in}% [[
  nat_ind : forall P : nat -> Prop,
            P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
  ]]

Each of the two cases of our last proof came from the type of one of the arguments to [nat_ind].  We chose [P] to be [(fun n : nat => plus n O = n)].  The first proof case corresponded to [P O] and the second case to [(forall n : nat, P n -> P (S n))].  The free variable [n] and inductive hypothesis [IHn] came from the argument types given here.

Since [nat] has a constructor that takes an argument, we may sometimes need to know that that constructor is injective.%\index{tactics!injection}\index{tactics!trivial}% *)

Theorem S_inj : forall n m : nat, S n = S m -> n = m.
(* begin thide *)
  injection 1; trivial.
Qed.
(* end thide *)

(** The [injection] tactic refers to a premise by number, adding new equalities between the corresponding arguments of equated terms that are formed with the same constructor.  We end up needing to prove [n = m -> n = m], so it is unsurprising that a tactic named [trivial] is able to finish the proof.  This tactic attempts a variety of single proof steps, drawn from a user-specified database that we will later see how to extend.

There is also a very useful tactic called %\index{tactics!congruence}%[congruence] that can prove this theorem immediately.  The [congruence] tactic generalizes [discriminate] and [injection], and it also adds reasoning about the general properties of equality, such as that a function returns equal results on equal arguments.  That is, [congruence] is a%\index{theory of equality and uninterpreted functions}% _complete decision procedure for the theory of equality and uninterpreted functions_, plus some smarts about inductive types.

%\medskip%

We can define a type of lists of natural numbers. *)

Inductive nat_list : Set :=
| NNil : nat_list
| NCons : nat -> nat_list -> nat_list.

(** Recursive definitions over [nat_list] are straightforward extensions of what we have seen before. *)

Fixpoint nlength (ls : nat_list) : nat :=
  match ls with
    | NNil => O
    | NCons _ ls' => S (nlength ls')
  end.

Fixpoint napp (ls1 ls2 : nat_list) : nat_list :=
  match ls1 with
    | NNil => ls2
    | NCons n ls1' => NCons n (napp ls1' ls2)
  end.

(** Inductive theorem proving can again be automated quite effectively. *)

Theorem nlength_napp : forall ls1 ls2 : nat_list, nlength (napp ls1 ls2)
  = plus (nlength ls1) (nlength ls2).
(* begin thide *)
  induction ls1; crush.
Qed.
(* end thide *)

Check nat_list_ind.
(** %\vspace{-.15in}% [[
  nat_list_ind
     : forall P : nat_list -> Prop,
       P NNil ->
       (forall (n : nat) (n0 : nat_list), P n0 -> P (NCons n n0)) ->
       forall n : nat_list, P n
]]

%\medskip%

In general, we can implement any "tree" type as an inductive type.  For example, here are binary trees of naturals. *)

Inductive nat_btree : Set :=
| NLeaf : nat_btree
| NNode : nat_btree -> nat -> nat_btree -> nat_btree.

(** Here are two functions whose intuitive explanations are not so important.  The first one computes the size of a tree, and the second performs some sort of splicing of one tree into the leftmost available leaf node of another. *)

Fixpoint nsize (tr : nat_btree) : nat :=
  match tr with
    | NLeaf => S O
    | NNode tr1 _ tr2 => plus (nsize tr1) (nsize tr2)
  end.

Fixpoint nsplice (tr1 tr2 : nat_btree) : nat_btree :=
  match tr1 with
    | NLeaf => NNode tr2 O NLeaf
    | NNode tr1' n tr2' => NNode (nsplice tr1' tr2) n tr2'
  end.

Theorem plus_assoc : forall n1 n2 n3 : nat, plus (plus n1 n2) n3 = plus n1 (plus n2 n3).
(* begin thide *)
  induction n1; crush.
Qed.
(* end thide *)

Hint Rewrite n_plus_O plus_assoc.

Theorem nsize_nsplice : forall tr1 tr2 : nat_btree, nsize (nsplice tr1 tr2)
  = plus (nsize tr2) (nsize tr1).
(* begin thide *)
  induction tr1; crush.
Qed.
(* end thide *)

(** It is convenient that these proofs go through so easily, but it is still useful to look into the details of what happened, by checking the statement of the tree induction principle. *)

Check nat_btree_ind.
(** %\vspace{-.15in}% [[
  nat_btree_ind
     : forall P : nat_btree -> Prop,
       P NLeaf ->
       (forall n : nat_btree,
        P n -> forall (n0 : nat) (n1 : nat_btree), P n1 -> P (NNode n n0 n1)) ->
       forall n : nat_btree, P n
]]

We have the usual two cases, one for each constructor of [nat_btree]. *)

(** * Parameterized Types *)

(** We can also define %\index{polymorphism}%polymorphic inductive types, as with algebraic datatypes in Haskell and ML.%\index{Gallina terms!list}\index{Gallina terms!Nil}\index{Gallina terms!Cons}\index{Gallina terms!length}\index{Gallina terms!app}% *)

Inductive list (T : Set) : Set :=
| Nil : list T
| Cons : T -> list T -> list T.

Fixpoint length T (ls : list T) : nat :=
  match ls with
    | Nil => O
    | Cons _ ls' => S (length ls')
  end.

Fixpoint app T (ls1 ls2 : list T) : list T :=
  match ls1 with
    | Nil => ls2
    | Cons x ls1' => Cons x (app ls1' ls2)
  end.

Theorem length_app : forall T (ls1 ls2 : list T), length (app ls1 ls2)
  = plus (length ls1) (length ls2).
(* begin thide *)
  induction ls1; crush.
Qed.
(* end thide *)

(** There is a useful shorthand for writing many definitions that share the same parameter, based on Coq's%\index{sections}\index{Vernacular commands!Section}\index{Vernacular commands!Variable}% _section_ mechanism.  The following block of code is equivalent to the above: *)

(* begin hide *)
Reset list.
(* end hide *)

Section list.
  Variable T : Set.

  Inductive list : Set :=
  | Nil : list
  | Cons : T -> list -> list.

  Fixpoint length (ls : list) : nat :=
    match ls with
      | Nil => O
      | Cons _ ls' => S (length ls')
    end.

  Fixpoint app (ls1 ls2 : list) : list :=
    match ls1 with
      | Nil => ls2
      | Cons x ls1' => Cons x (app ls1' ls2)
    end.

  Theorem length_app : forall ls1 ls2 : list, length (app ls1 ls2)
    = plus (length ls1) (length ls2).
(* begin thide *)
    induction ls1; crush.
  Qed.
(* end thide *)
End list.

Implicit Arguments Nil [T].

(** After we end the section, the [Variable]s we used are added as extra function parameters for each defined identifier, as needed.  With an [Implicit Arguments]%~\index{Vernacular commands!Implicit Arguments}% command, we ask that [T] be inferred when we use [Nil]; Coq's heuristics already decided to apply a similar policy to [Cons], because of the [Set Implicit Arguments]%~\index{Vernacular commands!Set Implicit Arguments}% command elided at the beginning of this chapter.  We verify that our definitions have been saved properly using the [Print] command, a cousin of [Check] which shows the definition of a symbol, rather than just its type. *)

Print list.
(** %\vspace{-.15in}% [[
  Inductive list (T : Set) : Set :=
    Nil : list T | Cons : T -> list T -> list T
]]

The final definition is the same as what we wrote manually before.  The other elements of the section are altered similarly, turning out exactly as they were before, though we managed to write their definitions more succinctly. *)

Check length.
(** %\vspace{-.15in}% [[
  length
     : forall T : Set, list T -> nat
]]

The parameter [T] is treated as a new argument to the induction principle, too. *)

Check list_ind.
(** %\vspace{-.15in}% [[
  list_ind
     : forall (T : Set) (P : list T -> Prop),
       P (Nil T) ->
       (forall (t : T) (l : list T), P l -> P (Cons t l)) ->
       forall l : list T, P l
]]

Thus, despite a very real sense in which the type [T] is an argument to the constructor [Cons], the inductive case in the type of [list_ind] (i.e., the third line of the type) includes no quantifier for [T], even though all of the other arguments are quantified explicitly.  Parameters in other inductive definitions are treated similarly in stating induction principles. *)

(** * Mutually Inductive Types *)

(** We can define inductive types that refer to each other: *)

Inductive even_list : Set :=
| ENil : even_list
| ECons : nat -> odd_list -> even_list

with odd_list : Set :=
| OCons : nat -> even_list -> odd_list.

Fixpoint elength (el : even_list) : nat :=
  match el with
    | ENil => O
    | ECons _ ol => S (olength ol)
  end

with olength (ol : odd_list) : nat :=
  match ol with
    | OCons _ el => S (elength el)
  end.

Fixpoint eapp (el1 el2 : even_list) : even_list :=
  match el1 with
    | ENil => el2
    | ECons n ol => ECons n (oapp ol el2)
  end

with oapp (ol : odd_list) (el : even_list) : odd_list :=
  match ol with
    | OCons n el' => OCons n (eapp el' el)
  end.

(** Everything is going roughly the same as in past examples, until we try to prove a theorem similar to those that came before. *)

Theorem elength_eapp : forall el1 el2 : even_list,
  elength (eapp el1 el2) = plus (elength el1) (elength el2).
(* begin thide *)
  induction el1; crush.

  (** One goal remains: [[

  n : nat
  o : odd_list
  el2 : even_list
  ============================
   S (olength (oapp o el2)) = S (plus (olength o) (elength el2))
   ]]

   We have no induction hypothesis, so we cannot prove this goal without starting another induction, which would reach a similar point, sending us into a futile infinite chain of inductions.  The problem is that Coq's generation of [T_ind] principles is incomplete.  We only get non-mutual induction principles generated by default. *)

Abort.
Check even_list_ind.
(** %\vspace{-.15in}% [[
  even_list_ind
     : forall P : even_list -> Prop,
       P ENil ->
       (forall (n : nat) (o : odd_list), P (ECons n o)) ->
       forall e : even_list, P e
]]

We see that no inductive hypotheses are included anywhere in the type.  To get them, we must ask for mutual principles as we need them, using the %\index{Vernacular commands!Scheme}%[Scheme] command. *)

Scheme even_list_mut := Induction for even_list Sort Prop
with odd_list_mut := Induction for odd_list Sort Prop.

(** This invocation of [Scheme] asks for the creation of induction principles [even_list_mut] for the type [even_list] and [odd_list_mut] for the type [odd_list].  The [Induction] keyword says we want standard induction schemes, since [Scheme] supports more exotic choices.  Finally, [Sort Prop] establishes that we really want induction schemes, not recursion schemes, which are the same according to Curry-Howard, save for the [Prop]/[Set] distinction. *)

Check even_list_mut.
(** %\vspace{-.15in}% [[
  even_list_mut
     : forall (P : even_list -> Prop) (P0 : odd_list -> Prop),
       P ENil ->
       (forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) ->
       (forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) ->
       forall e : even_list, P e
]]

This is the principle we wanted in the first place.

The [Scheme] command is for asking Coq to generate particular induction schemes that are mutual among a set of inductive types (possibly only one such type, in which case we get a normal induction principle).  In a sense, it generalizes the induction scheme generation that goes on automatically for each inductive definition.  Future Coq versions might make that automatic generation smarter, so that [Scheme] is needed in fewer places.  In a few sections, we will see how induction principles are derived theorems in Coq, so that there is not actually any need to build in _any_ automatic scheme generation.

There is one more wrinkle left in using the [even_list_mut] induction principle: the [induction] tactic will not apply it for us automatically.  It will be helpful to look at how to prove one of our past examples without using [induction], so that we can then generalize the technique to mutual inductive types.%\index{tactics!apply}% *)

Theorem n_plus_O' : forall n : nat, plus n O = n.
  apply nat_ind.
  (** Here we use [apply], which is one of the most essential basic tactics.  When we are trying to prove fact [P], and when [thm] is a theorem whose conclusion can be made to match [P] by proper choice of quantified variable values, the invocation [apply thm] will replace the current goal with one new goal for each premise of [thm].

     This use of [apply] may seem a bit _too_ magical.  To better see what is going on, we use a variant where we partially apply the theorem [nat_ind] to give an explicit value for the predicate that gives our induction hypothesis. *)

  Undo.
  apply (nat_ind (fun n => plus n O = n)); crush.
Qed.

(** From this example, we can see that [induction] is not magic.  It only does some bookkeeping for us to make it easy to apply a theorem, which we can do directly with the [apply] tactic.

This technique generalizes to our mutual example: *)

Theorem elength_eapp : forall el1 el2 : even_list,
  elength (eapp el1 el2) = plus (elength el1) (elength el2).

  apply (even_list_mut
    (fun el1 : even_list => forall el2 : even_list,
      elength (eapp el1 el2) = plus (elength el1) (elength el2))
    (fun ol : odd_list => forall el : even_list,
      olength (oapp ol el) = plus (olength ol) (elength el))); crush.
Qed.
(* end thide *)

(** We simply need to specify two predicates, one for each of the mutually inductive types.  In general, it is not a good idea to assume that a proof assistant can infer extra predicates, so this way of applying mutual induction is about as straightforward as we may hope for. *)

(** * Reflexive Types *)

(** A kind of inductive type called a _reflexive type_ includes at least one constructor that takes as an argument _a function returning the same type we are defining_.  One very useful class of examples is in modeling variable binders.  Our example will be an encoding of the syntax of first-order logic.  Since the idea of syntactic encodings of logic may require a bit of acclimation, let us first consider a simpler formula type for a subset of propositional logic.  We are not yet using a reflexive type, but later we will extend the example reflexively. *)

Inductive pformula : Set :=
| Truth : pformula
| Falsehood : pformula
| Conjunction : pformula -> pformula -> pformula.

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

(** A key distinction here is between, for instance, the _syntax_ [Truth] and its _semantics_ [True].  We can make the semantics explicit with a recursive function.  This function uses the infix operator %\index{Gallina operators!/\textbackslash}%[/\], which desugars to instances of the type family %\index{Gallina terms!and}%[and] from the standard library.  The family [and] implements conjunction, the [Prop] Curry-Howard analogue of the usual pair type from functional programming (which is the type family %\index{Gallina terms!prod}%[prod] in Coq's standard library). *)

Fixpoint pformulaDenote (f : pformula) : Prop :=
  match f with
    | Truth => True
    | Falsehood => False
    | Conjunction f1 f2 => pformulaDenote f1 /\ pformulaDenote f2
  end.

(** This is just a warm-up that does not use reflexive types, the new feature we mean to introduce.  When we set our sights on first-order logic instead, it becomes very handy to give constructors recursive arguments that are functions. *)

Inductive formula : Set :=
| Eq : nat -> nat -> formula
| And : formula -> formula -> formula
| Forall : (nat -> formula) -> formula.

(** Our kinds of formulas are equalities between naturals, conjunction, and universal quantification over natural numbers.  We avoid needing to include a notion of "variables" in our type, by using Coq functions to encode the syntax of quantification.  For instance, here is the encoding of [forall x : nat, x = x]:%\index{Vernacular commands!Example}% *)

Definition forall_refl : formula := Forall (fun x => Eq x x).

(** We can write recursive functions over reflexive types quite naturally.  Here is one translating our formulas into native Coq propositions. *)

Fixpoint formulaDenote (f : formula) : Prop :=
  match f with
    | Eq n1 n2 => n1 = n2
    | And f1 f2 => formulaDenote f1 /\ formulaDenote f2
    | Forall f' => forall n : nat, formulaDenote (f' n)
  end.

(** We can also encode a trivial formula transformation that swaps the order of equality and conjunction operands. *)

Fixpoint swapper (f : formula) : formula :=
  match f with
    | Eq n1 n2 => Eq n2 n1
    | And f1 f2 => And (swapper f2) (swapper f1)
    | Forall f' => Forall (fun n => swapper (f' n))
  end.

(** It is helpful to prove that this transformation does not make true formulas false. *)

Theorem swapper_preserves_truth : forall f, formulaDenote f -> formulaDenote (swapper f).
(* begin thide *)
  induction f; crush.
Qed.
(* end thide *)

(** We can take a look at the induction principle behind this proof. *)

Check formula_ind.
(** %\vspace{-.15in}% [[
  formula_ind
     : forall P : formula -> Prop,
       (forall n n0 : nat, P (Eq n n0)) ->
       (forall f0 : formula,
        P f0 -> forall f1 : formula, P f1 -> P (And f0 f1)) ->
       (forall f1 : nat -> formula,
        (forall n : nat, P (f1 n)) -> P (Forall f1)) ->
       forall f2 : formula, P f2
]]

Focusing on the [Forall] case, which comes third, we see that we are allowed to assume that the theorem holds _for any application of the argument function [f1]_.  That is, Coq induction principles do not follow a simple rule that the textual representations of induction variables must get shorter in appeals to induction hypotheses.  Luckily for us, the people behind the metatheory of Coq have verified that this flexibility does not introduce unsoundness.

%\medskip%

Up to this point, we have seen how to encode in Coq more and more of what is possible with algebraic datatypes in %\index{Haskell}%Haskell and %\index{ML}%ML.  This may have given the inaccurate impression that inductive types are a strict extension of algebraic datatypes.  In fact, Coq must rule out some types allowed by Haskell and ML, for reasons of soundness.  Reflexive types provide our first good example of such a case; only some of them are legal.

Given our last example of an inductive type, many readers are probably eager to try encoding the syntax of %\index{lambda calculus}%lambda calculus.  Indeed, the function-based representation technique that we just used, called%\index{higher-order abstract syntax}\index{HOAS|see{higher-order abstract syntax}}% _higher-order abstract syntax_ (HOAS)%~\cite{HOAS}%, is the representation of choice for lambda calculi in %\index{Twelf}%Twelf and in many applications implemented in Haskell and ML.  Let us try to import that choice to Coq: *)
(* begin hide *)
(* begin thide *)
Inductive term : Set := App | Abs.
Reset term.
Definition uhoh := O.
(* end thide *)
(* end hide *)
(** [[
Inductive term : Set :=
| App : term -> term -> term
| Abs : (term -> term) -> term.
]]

<<
Error: Non strictly positive occurrence of "term" in "(term -> term) -> term"
>>

We have run afoul of the%\index{strict positivity requirement}\index{positivity requirement}% _strict positivity requirement_ for inductive definitions, which says that the type being defined may not occur to the left of an arrow in the type of a constructor argument.  It is important that the type of a constructor is viewed in terms of a series of arguments and a result, since obviously we need recursive occurrences to the lefts of the outermost arrows if we are to have recursive occurrences at all.  Our candidate definition above violates the positivity requirement because it involves an argument of type [term -> term], where the type [term] that we are defining appears to the left of an arrow.  The candidate type of [App] is fine, however, since every occurrence of [term] is either a constructor argument or the final result type.

Why must Coq enforce this restriction?  Imagine that our last definition had been accepted, allowing us to write this function:

%\vspace{-.15in}%[[
Definition uhoh (t : term) : term :=
  match t with
    | Abs f => f t
    | _ => t
  end.
]]

Using an informal idea of Coq's semantics, it is easy to verify that the application [uhoh (Abs uhoh)] will run forever.  This would be a mere curiosity in OCaml and Haskell, where non-termination is commonplace, though the fact that we have a non-terminating program without explicit recursive function definitions is unusual.

%\index{termination checking}%For Coq, however, this would be a disaster.  The possibility of writing such a function would destroy all our confidence that proving a theorem means anything.  Since Coq combines programs and proofs in one language, we would be able to prove every theorem with an infinite loop.

Nonetheless, the basic insight of HOAS is a very useful one, and there are ways to realize most benefits of HOAS in Coq.  We will study a particular technique of this kind in the final chapter, on programming language syntax and semantics. *)

(** * An Interlude on Induction Principles *)

(** As we have emphasized a few times already, Coq proofs are actually programs, written in the same language we have been using in our examples all along.  We can get a first sense of what this means by taking a look at the definitions of some of the %\index{induction principles}%induction principles we have used.  A close look at the details here will help us construct induction principles manually, which we will see is necessary for some more advanced inductive definitions. *)

Print nat_ind.
(** %\vspace{-.15in}%[[
nat_ind =
fun P : nat -> Prop => nat_rect P
     : forall P : nat -> Prop,
       P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
]]

We see that this induction principle is defined in terms of a more general principle, [nat_rect].  The <<rec>> stands for "recursion principle," and the <<t>> at the end stands for [Type]. *)

Check nat_rect.
(** %\vspace{-.15in}% [[
nat_rect
     : forall P : nat -> Type,
       P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
]]

The principle [nat_rect] gives [P] type [nat -> Type] instead of [nat -> Prop].  This [Type] is another universe, like [Set] and [Prop].  In fact, it is a common supertype of both.  Later on, we will discuss exactly what the significances of the different universes are.  For now, it is just important that we can use [Type] as a sort of meta-universe that may turn out to be either [Set] or [Prop].  We can see the symmetry inherent in the subtyping relationship by printing the definition of another principle that was generated for [nat] automatically: *)

Print nat_rec.
(** %\vspace{-.15in}%[[
nat_rec =
fun P : nat -> Set => nat_rect P
     : forall P : nat -> Set,
       P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
]]

This is identical to the definition for [nat_ind], except that we have substituted [Set] for [Prop].  For most inductive types [T], then, we get not just induction principles [T_ind], but also %\index{recursion principles}%recursion principles [T_rec].  We can use [T_rec] to write recursive definitions without explicit [Fixpoint] recursion.  For instance, the following two definitions are equivalent: *)

Fixpoint plus_recursive (n : nat) : nat -> nat :=
  match n with
    | O => fun m => m
    | S n' => fun m => S (plus_recursive n' m)
  end.

Definition plus_rec : nat -> nat -> nat :=
  nat_rec (fun _ : nat => nat -> nat) (fun m => m) (fun _ r m => S (r m)).

Theorem plus_equivalent : plus_recursive = plus_rec.
  reflexivity.
Qed.

(** Going even further down the rabbit hole, [nat_rect] itself is not even a primitive.  It is a functional program that we can write manually. *)

Print nat_rect.
(** %\vspace{-.15in}%[[
nat_rect =
fun (P : nat -> Type) (f : P O) (f0 : forall n : nat, P n -> P (S n)) =>
fix F (n : nat) : P n :=
  match n as n0 return (P n0) with
  | O => f
  | S n0 => f0 n0 (F n0)
  end
     : forall P : nat -> Type,
       P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
]]

The only new wrinkles here are, first, an anonymous recursive function definition, using the %\index{Gallina terms!fix}%[fix] keyword of Gallina (which is like [fun] with recursion supported); and, second, the annotations on the [match] expression.  This is a%\index{dependent pattern matching}% _dependently typed_ pattern match, because the _type_ of the expression depends on the _value_ being matched on.  We will meet more involved examples later, especially in Part II of the book.

%\index{type inference}%Type inference for dependent pattern matching is undecidable, which can be proved by reduction from %\index{higher-order unification}%higher-order unification%~\cite{HOU}%.  Thus, we often find ourselves needing to annotate our programs in a way that explains dependencies to the type checker.  In the example of [nat_rect], we have an %\index{Gallina terms!as}%[as] clause, which binds a name for the discriminee; and a %\index{Gallina terms!return}%[return] clause, which gives a way to compute the [match] result type as a function of the discriminee.

To prove that [nat_rect] is nothing special, we can reimplement it manually. *)

Fixpoint nat_rect' (P : nat -> Type)
  (HO : P O)
  (HS : forall n, P n -> P (S n)) (n : nat) :=
  match n return P n with
    | O => HO
    | S n' => HS n' (nat_rect' P HO HS n')
  end.

(** We can understand the definition of [nat_rect] better by reimplementing [nat_ind] using sections. *)

Section nat_ind'.
   (** First, we have the property of natural numbers that we aim to prove. *)

  Variable P : nat -> Prop.

   (** Then we require a proof of the [O] case, which we declare with the command %\index{Vernacular commands!Hypothesis}%[Hypothesis], which is a synonym for [Variable] that, by convention, is used for variables whose types are propositions. *)

  Hypothesis O_case : P O.

   (** Next is a proof of the [S] case, which may assume an inductive hypothesis. *)

  Hypothesis S_case : forall n : nat, P n -> P (S n).

   (** Finally, we define a recursive function to tie the pieces together. *)

  Fixpoint nat_ind' (n : nat) : P n :=
    match n with
      | O => O_case
      | S n' => S_case (nat_ind' n')
    end.
End nat_ind'.

(** Closing the section adds the [Variable]s and [Hypothesis]es as new [fun]-bound arguments to [nat_ind'], and, modulo the use of [Prop] instead of [Type], we end up with the exact same definition that was generated automatically for [nat_rect].

%\medskip%

We can also examine the definition of [even_list_mut], which we generated with [Scheme] for a mutually recursive type. *)

Print even_list_mut.
(** %\vspace{-.15in}%[[
  even_list_mut =
  fun (P : even_list -> Prop) (P0 : odd_list -> Prop)
    (f : P ENil) (f0 : forall (n : nat) (o : odd_list), P0 o -> P (ECons n o))
    (f1 : forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) =>
  fix F (e : even_list) : P e :=
    match e as e0 return (P e0) with
    | ENil => f
    | ECons n o => f0 n o (F0 o)
    end
  with F0 (o : odd_list) : P0 o :=
    match o as o0 return (P0 o0) with
    | OCons n e => f1 n e (F e)
    end
  for F
     : forall (P : even_list -> Prop) (P0 : odd_list -> Prop),
       P ENil ->
       (forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) ->
       (forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) ->
       forall e : even_list, P e
]]

We see a mutually recursive [fix], with the different functions separated by %\index{Gallina terms!with}%[with] in the same way that they would be separated by <<and>> in ML.  A final %\index{Gallina terms!for}%[for] clause identifies which of the mutually recursive functions should be the final value of the [fix] expression.  Using this definition as a template, we can reimplement [even_list_mut] directly. *)

Section even_list_mut'.
  (** First, we need the properties that we are proving. *)

  Variable Peven : even_list -> Prop.
  Variable Podd : odd_list -> Prop.

  (** Next, we need proofs of the three cases. *)

  Hypothesis ENil_case : Peven ENil.
  Hypothesis ECons_case : forall (n : nat) (o : odd_list), Podd o -> Peven (ECons n o).
  Hypothesis OCons_case : forall (n : nat) (e : even_list), Peven e -> Podd (OCons n e).

  (** Finally, we define the recursive functions. *)

  Fixpoint even_list_mut' (e : even_list) : Peven e :=
    match e with
      | ENil => ENil_case
      | ECons n o => ECons_case n (odd_list_mut' o)
    end
  with odd_list_mut' (o : odd_list) : Podd o :=
    match o with
      | OCons n e => OCons_case n (even_list_mut' e)
    end.
End even_list_mut'.

(** Even induction principles for reflexive types are easy to implement directly.  For our [formula] type, we can use a recursive definition much like those we wrote above. *)

Section formula_ind'.
  Variable P : formula -> Prop.
  Hypothesis Eq_case : forall n1 n2 : nat, P (Eq n1 n2).
  Hypothesis And_case : forall f1 f2 : formula,
    P f1 -> P f2 -> P (And f1 f2).
  Hypothesis Forall_case : forall f : nat -> formula,
    (forall n : nat, P (f n)) -> P (Forall f).

  Fixpoint formula_ind' (f : formula) : P f :=
    match f with
      | Eq n1 n2 => Eq_case n1 n2
      | And f1 f2 => And_case (formula_ind' f1) (formula_ind' f2)
      | Forall f' => Forall_case f' (fun n => formula_ind' (f' n))
    end.
End formula_ind'.

(** It is apparent that induction principle implementations involve some tedium but not terribly much creativity. *)

(** * Nested Inductive Types *)

(** Suppose we want to extend our earlier type of binary trees to trees with arbitrary finite branching.  We can use lists to give a simple definition. *)

Inductive nat_tree : Set :=
| NNode' : nat -> list nat_tree -> nat_tree.

(** This is an example of a%\index{nested inductive type}% _nested_ inductive type definition, because we use the type we are defining as an argument to a parameterized type family.  Coq will not allow all such definitions; it effectively pretends that we are defining [nat_tree] mutually with a version of [list] specialized to [nat_tree], checking that the resulting expanded definition satisfies the usual rules.  For instance, if we replaced [list] with a type family that used its parameter as a function argument, then the definition would be rejected as violating the positivity restriction.

As we encountered with mutual inductive types, we find that the automatically generated induction principle for [nat_tree] is too weak. *)

(* begin hide *)
(* begin thide *)
Check Forall.
(* end thide *)
(* end hide *)

Check nat_tree_ind.
(** %\vspace{-.15in}% [[
  nat_tree_ind
     : forall P : nat_tree -> Prop,
       (forall (n : nat) (l : list nat_tree), P (NNode' n l)) ->
       forall n : nat_tree, P n
]]

There is no command like [Scheme] that will implement an improved principle for us.  In general, it takes creativity to figure out _good_ ways to incorporate nested uses of different type families.  Now that we know how to implement induction principles manually, we are in a position to apply just such creativity to this problem.

Many induction principles for types with nested uses of [list] could benefit from a unified predicate capturing the idea that some property holds of every element in a list.  By defining this generic predicate once, we facilitate reuse of library theorems about it.  (Here, we are actually duplicating the standard library's [Forall] predicate, with a different implementation, for didactic purposes.) *)

Section All.
  Variable T : Set.
  Variable P : T -> Prop.

  Fixpoint All (ls : list T) : Prop :=
    match ls with
      | Nil => True
      | Cons h t => P h /\ All t
    end.
End All.

(** It will be useful to review the definitions of [True] and [/\], since we will want to write manual proofs of them below. *)

Print True.
(** %\vspace{-.15in}%[[
  Inductive True : Prop :=  I : True
  ]]

That is, [True] is a proposition with exactly one proof, [I], which we may always supply trivially.

Finding the definition of [/\] takes a little more work.  Coq supports user registration of arbitrary parsing rules, and it is such a rule that is letting us write [/\] instead of an application of some inductive type family.  We can find the underlying inductive type with the %\index{Vernacular commands!Locate}%[Locate] command, whose argument may be a parsing token.%\index{Gallina terms!and}% *)

Locate "/\".
(** %\vspace{-.15in}%[[
  "A /\ B" := and A B  : type_scope (default interpretation)
]]
*)

Print and.
(** %\vspace{-.15in}%[[
  Inductive and (A : Prop) (B : Prop) : Prop :=  conj : A -> B -> A /\ B
]]
%\vspace{-.1in}%
<<
  For conj: Arguments A, B are implicit
>>

In addition to the definition of [and] itself, we get information on %\index{implicit arguments}%implicit arguments (and some other information that we omit here).  The implicit argument information tells us that we build a proof of a conjunction by calling the constructor [conj] on proofs of the conjuncts, with no need to include the types of those proofs as explicit arguments.

%\medskip%

Now we create a section for our induction principle, following the same basic plan as in the previous section of this chapter. *)

Section nat_tree_ind'.
  Variable P : nat_tree -> Prop.

  Hypothesis NNode'_case : forall (n : nat) (ls : list nat_tree),
    All P ls -> P (NNode' n ls).

  (* begin hide *)
  (* begin thide *)
  Definition list_nat_tree_ind := O.
  (* end thide *)
  (* end hide *)

  (** A first attempt at writing the induction principle itself follows the intuition that nested inductive type definitions are expanded into mutual inductive definitions.

  %\vspace{-.15in}%[[
  Fixpoint nat_tree_ind' (tr : nat_tree) : P tr :=
    match tr with
      | NNode' n ls => NNode'_case n ls (list_nat_tree_ind ls)
    end

  with list_nat_tree_ind (ls : list nat_tree) : All P ls :=
    match ls with
      | Nil => I
      | Cons tr rest => conj (nat_tree_ind' tr) (list_nat_tree_ind rest)
    end.
  ]]

  Coq rejects this definition, saying
<<
  Recursive call to nat_tree_ind' has principal argument equal to "tr"
  instead of rest.
>>

  There is no deep theoretical reason why this program should be rejected; Coq applies incomplete termination-checking heuristics, and it is necessary to learn a few of the most important rules.  The term "nested inductive type" hints at the solution to this particular problem.  Just as mutually inductive types require mutually recursive induction principles, nested types require nested recursion. *)

  Fixpoint nat_tree_ind' (tr : nat_tree) : P tr :=
    match tr with
      | NNode' n ls => NNode'_case n ls
        ((fix list_nat_tree_ind (ls : list nat_tree) : All P ls :=
          match ls with
            | Nil => I
            | Cons tr' rest => conj (nat_tree_ind' tr') (list_nat_tree_ind rest)
          end) ls)
    end.

  (** We include an anonymous [fix] version of [list_nat_tree_ind] that is literally _nested_ inside the definition of the recursive function corresponding to the inductive definition that had the nested use of [list].   *)

End nat_tree_ind'.

(** We can try our induction principle out by defining some recursive functions on [nat_tree] and proving a theorem about them.  First, we define some helper functions that operate on lists. *)

Section map.
  Variables T T' : Set.
  Variable F : T -> T'.

  Fixpoint map (ls : list T) : list T' :=
    match ls with
      | Nil => Nil
      | Cons h t => Cons (F h) (map t)
    end.
End map.

Fixpoint sum (ls : list nat) : nat :=
  match ls with
    | Nil => O
    | Cons h t => plus h (sum t)
  end.

(** Now we can define a size function over our trees. *)

Fixpoint ntsize (tr : nat_tree) : nat :=
  match tr with
    | NNode' _ trs => S (sum (map ntsize trs))
  end.

(** Notice that Coq was smart enough to expand the definition of [map] to verify that we are using proper nested recursion, even through a use of a higher-order function. *)

Fixpoint ntsplice (tr1 tr2 : nat_tree) : nat_tree :=
  match tr1 with
    | NNode' n Nil => NNode' n (Cons tr2 Nil)
    | NNode' n (Cons tr trs) => NNode' n (Cons (ntsplice tr tr2) trs)
  end.

(** We have defined another arbitrary notion of tree splicing, similar to before, and we can prove an analogous theorem about its relationship with tree size.  We start with a useful lemma about addition. *)

(* begin thide *)
Lemma plus_S : forall n1 n2 : nat,
  plus n1 (S n2) = S (plus n1 n2).
  induction n1; crush.
Qed.
(* end thide *)

(** Now we begin the proof of the theorem, adding the lemma [plus_S] as a hint. *)

Hint Rewrite plus_S.

Theorem ntsize_ntsplice : forall tr1 tr2 : nat_tree, ntsize (ntsplice tr1 tr2)
  = plus (ntsize tr2) (ntsize tr1).
(* begin thide *)
  (** We know that the standard induction principle is insufficient for the task, so we need to provide a %\index{tactics!using}%[using] clause for the [induction] tactic to specify our alternate principle. *)

  induction tr1 using nat_tree_ind'; crush.

  (** One subgoal remains: [[
  n : nat
  ls : list nat_tree
  H : All
        (fun tr1 : nat_tree =>
         forall tr2 : nat_tree,
         ntsize (ntsplice tr1 tr2) = plus (ntsize tr2) (ntsize tr1)) ls
  tr2 : nat_tree
  ============================
   ntsize
     match ls with
     | Nil => NNode' n (Cons tr2 Nil)
     | Cons tr trs => NNode' n (Cons (ntsplice tr tr2) trs)
     end = S (plus (ntsize tr2) (sum (map ntsize ls)))

     ]]

     After a few moments of squinting at this goal, it becomes apparent that we need to do a case analysis on the structure of [ls].  The rest is routine. *)

  destruct ls; crush.

  (** We can go further in automating the proof by exploiting the hint mechanism.%\index{Vernacular commands!Hint Extern}% *)

  Restart.

  Hint Extern 1 (ntsize (match ?LS with Nil => _ | Cons _ _ => _ end) = _) =>
    destruct LS; crush.
  induction tr1 using nat_tree_ind'; crush.
Qed.
(* end thide *)

(** We will go into great detail on hints in a later chapter, but the only important thing to note here is that we register a pattern that describes a conclusion we expect to encounter during the proof.  The pattern may contain unification variables, whose names are prefixed with question marks, and we may refer to those bound variables in a tactic that we ask to have run whenever the pattern matches.

The advantage of using the hint is not very clear here, because the original proof was so short.  However, the hint has fundamentally improved the readability of our proof.  Before, the proof referred to the local variable [ls], which has an automatically generated name.  To a human reading the proof script without stepping through it interactively, it was not clear where [ls] came from.  The hint explains to the reader the process for choosing which variables to case analyze, and the hint can continue working even if the rest of the proof structure changes significantly. *)

(** * Manual Proofs About Constructors *)

(** It can be useful to understand how tactics like %\index{tactics!discriminate}%[discriminate] and %\index{tactics!injection}%[injection] work, so it is worth stepping through a manual proof of each kind.  We will start with a proof fit for [discriminate]. *)

Theorem true_neq_false : true <> false.

(* begin thide *)
(** We begin with the tactic %\index{tactics!red}%[red], which is short for "one step of reduction," to unfold the definition of logical negation. *)

  red.
(** %\vspace{-.15in}%[[
  ============================
   true = false -> False
]]

The negation is replaced with an implication of falsehood.  We use the tactic %\index{tactics!intro}%[intro H] to change the assumption of the implication into a hypothesis named [H]. *)

  intro H.
(** %\vspace{-.15in}%[[
  H : true = false
  ============================
   False
]]

This is the point in the proof where we apply some creativity.  We define a function whose utility will become clear soon. *)

  Definition toProp (b : bool) := if b then True else False.

(** It is worth recalling the difference between the lowercase and uppercase versions of truth and falsehood: [True] and [False] are logical propositions, while [true] and [false] are Boolean values that we can case-analyze.  We have defined [toProp] such that our conclusion of [False] is computationally equivalent to [toProp false].  Thus, the %\index{tactics!change}%[change] tactic will let us change the conclusion to [toProp false].  The general form [change e] replaces the conclusion with [e], whenever Coq's built-in computation rules suffice to establish the equivalence of [e] with the original conclusion. *)

  change (toProp false).
(** %\vspace{-.15in}%[[
  H : true = false
  ============================
   toProp false
]]

Now the righthand side of [H]'s equality appears in the conclusion, so we can rewrite, using the notation [<-] to request to replace the righthand side of the equality with the lefthand side.%\index{tactics!rewrite}% *)

  rewrite <- H.
(** %\vspace{-.15in}%[[
  H : true = false
  ============================
   toProp true
]]

We are almost done.  Just how close we are to done is revealed by computational simplification. *)

  simpl.
(** %\vspace{-.15in}%[[
  H : true = false
  ============================
   True
]]
*)

  trivial.
Qed.
(* end thide *)

(** I have no trivial automated version of this proof to suggest, beyond using [discriminate] or [congruence] in the first place.

%\medskip%

We can perform a similar manual proof of injectivity of the constructor [S].  I leave a walk-through of the details to curious readers who want to run the proof script interactively. *)

Theorem S_inj' : forall n m : nat, S n = S m -> n = m.
(* begin thide *)
  intros n m H.
  change (pred (S n) = pred (S m)).
  rewrite H.
  reflexivity.
Qed.
(* end thide *)

(** The key piece of creativity in this theorem comes in the use of the natural number predecessor function [pred].  Embodied in the implementation of [injection] is a generic recipe for writing such type-specific functions.

The examples in this section illustrate an important aspect of the design philosophy behind Coq.  We could certainly design a Gallina replacement that built in rules for constructor discrimination and injectivity, but a simpler alternative is to include a few carefully chosen rules that enable the desired reasoning patterns and many others.  A key benefit of this philosophy is that the complexity of proof checking is minimized, which bolsters our confidence that proved theorems are really true. *)


(* Inductive False := . *)

Module MLCoq.

SearchAbout (_ + _).
Print bool.
Print conj.
Print and_ind.
Print nat_ind.
Print plus_n_Sm.
Print plus_Sn_m.

Set Printing All.

Print IF_then_else.

Inductive and' (A B : Prop) : Prop :=
    conj' : forall (_ : A) (_ : B), and' A B .
Definition and'_ind_verbose : forall (A B P : Prop) (_ : forall (_ : A) (_ : B), P)(_ : and' A B), P :=
fun (A B P : Prop) (f : forall (_ : A) (_ : B), P) (a : and' A B) =>
match a return P with
| conj' _ _ x x0 => f x x0
end .
Definition and'_rec_verbose : forall (A B : Prop) (P : Set) (_ : forall (_ : A) (_ : B), P)(_ : and' A B), P :=
fun (A B : Prop) (P : Set) => and'_rect A B P .
Definition and'_rect_verbose : forall (A B : Prop) (P : Type) (_ : forall (_ : A) (_ : B), P)(_ : and' A B), P :=
fun (A B : Prop) (P : Type) (f : forall (_ : A) (_ : B), P)
  (a : and' A B) =>
match a return P with
| conj' _ _ x x0 => f x x0
end .

Print and'_ind.
Inductive ex1 (A : Type) (P : forall _ : A, Prop) : Prop :=
    ex_intro1 : forall (x : A) (_ : P x), ex1 A P .
Definition ex1_ind_verbose : forall (A : Type) (P : forall _ : A, Prop)(P0 : Prop) (_ : forall (x : A) (_ : P x), P0)(_ : ex1 A P), P0 :=
fun (A : Type) (P : forall _ : A, Prop) (P0 : Prop)
  (f : forall (x : A) (_ : P x), P0) (e : ex1 A P) =>
match e return P0 with
| ex_intro1 _ _ x x0 => f x x0
end .

Set Implicit Arguments.

Inductive ex2 (A : Type) (P : forall _ : A, Prop) : Prop :=
    ex_intro2 : forall (x : A) (_ : P x), @ex2 A P .
Definition ex2_ind_verbose : forall (A : Type) (P : forall _ : A, Prop)(P0 : Prop) (_ : forall (x : A) (_ : P x), P0)(_ : @ex2 A P), P0 :=
fun (A : Type) (P : forall _ : A, Prop) (P0 : Prop)
  (f : forall (x : A) (_ : P x), P0) (e : @ex2 A P) =>
match e return P0 with
| ex_intro2 _ x x0 => f x x0
end .

Inductive ex3 (A : Type) (P : forall _ : A, Prop) : Prop :=
    ex_intro3 : forall (x : A) (_ : P x), @ex3 A P .
Definition ex3_ind_verbose : forall (A : Type) (P : forall _ : A, Prop)(P0 : Prop) (_ : forall (x : A) (_ : P x), P0)(_ : @ex3 A P), P0 :=
fun (A : Type) (P : forall _ : A, Prop) (P0 : Prop)
  (f : forall (x : A) (_ : P x), P0) (e : @ex3 A P) =>
match e return P0 with
| ex_intro3 _ x x0 => f x x0
end .

Inductive ex4 (A : Type) (P : forall _ : A, Prop) : Prop :=
    ex_intro4 : forall (x : A) (_ : P x), @ex4 A P .
Definition ex4_ind_verbose : forall (A : Type) (P : forall _ : A, Prop)(P0 : Prop) (_ : forall (x : A) (_ : P x), P0)(_ : @ex4 A P), P0 :=
fun (A : Type) (P : forall _ : A, Prop) (P0 : Prop)
  (f : forall (x : A) (_ : P x), P0) (e : @ex4 A P) =>
match e return P0 with
| ex_intro4 _ x x0 => f x x0
end .

Check conj.
Check conj'.

Print conj.

Print nat.
Print nat_ind.

Print False_ind.
Print Nat.add.
Print and_ind.

Print True.
Print True_ind.

Definition False_ind : forall (P : Prop) (_ : False), P :=
fun (P : Prop) (f : False) => match f return P with
                              end .

Definition add_ind' : forall (_ : nat) (_ : nat), nat :=
fix add (n m : nat) {struct n} : nat :=
  match n return nat with
  | O => m
  | S p => S (add p m)
  end .

Print nat.
Definition nat_ind' : forall (P : forall _ : nat, Prop) (_ : P O)(_ : forall (n : nat) (_ : P n), P (S n))(n : nat), P n :=
fun (P : forall _ : nat, Prop) (f : P O)
  (f0 : forall (n : nat) (_ : P n), P (S n)) =>
fix F (n : nat) : P n :=
  match n as n0 return (P n0) with
  | O => f
  | S n0 => f0 n0 (F n0)
  end .

Print eq.
Print eq_ind.

Definition AddZero : forall n : nat, @eq nat (Nat.add n O) n :=
fun n : nat =>
nat_ind (fun n0 : nat => @eq nat (Nat.add n0 O) n0)
  (@eq_refl nat O : @eq nat (Nat.add O O) O)
  (fun (n0 : nat) (IHn : @eq nat (Nat.add n0 O) n0) =>
   @eq_ind_r nat n0 (fun n1 : nat => @eq nat (S n1) (S n0))
     (@eq_refl nat (S n0)) (Nat.add n0 O) IHn
   :
   @eq nat (Nat.add (S n0) O) (S n0)) n .

Set Printing All.
Print AddZero.
Check AddZero.

Print eq_refl.
Print eq_ind.

Definition AndComm : forall (A B : Prop) (_ : and A B), and B A :=
fun (A B : Prop) (H : and A B) =>
match H return (and B A) with
| conj H0 H1 => @conj B A H1 H0
end .

Set Printing All.
Print AndComm.
Print not.

Definition AndComm' : forall (A B : Prop) (_ : and A B), and B A :=
fun (A B : Prop) (H : and A B) =>
match H return (and B A) with
| conj H0 H1 => @conj B A H1 H0
end .

(* ask brandon if way to get term out of proof *)

Definition AndComm'' : forall (A B : Prop) (_ : and A B), and B A :=
AndComm' .

Print AndComm'.

Print nat_ind.
Print or.

Definition test : forall _ : Prop, Prop := fun _ : Prop => True .

Print test.

Definition and_rect' : Type :=
forall (A B : Prop) (P : Type) (_ : forall (_ : A) (_ : B), P) (_ : and A B),
P .
Definition and_ind : Prop :=
forall (A B P : Prop) (_ : forall (_ : A) (_ : B), P) (_ : and A B), P .
Definition and_rec : Type :=
forall (A B : Prop) (P : Set) (_ : forall (_ : A) (_ : B), P) (_ : and A B),
P .

Definition AndCom : forall (A B : Prop) (_ : and A B), and B A :=
fun (A B : Prop) (H : and A B) =>
match H return (and B A) with
| conj H0 H1 => @conj B A H1 H0
end .

Print AndCom.

Definition AndCom' : forall (A B : Prop) (_ : and A B), and B A :=
fun (A B : Prop) (H : and A B) =>
match H return (and B A) with
| conj H0 H1 => @conj B A H1 H0
end .

Print All.

End MLCoq.

(* Require Import dpdgraph.dpdgraph. *)
(* Print FileDependGraph MLCoq. *)

(* Print MLCoq.AndCom'. *)


(* Author: Masaki Hara, 2014 *)
(* Linear Logic Toy for Coq *)

(*************************************************)
(*      Environment for Linear Logic             *)
(*************************************************)
Class LEnv := {
  LWeight : Type;
  LWeightPlus : LWeight -> LWeight -> LWeight;
  LWeightZero : LWeight;
  LWeightPlusAssoc a b c :
    LWeightPlus a (LWeightPlus b c) = LWeightPlus (LWeightPlus a b) c;
  LWeightZeroL a : LWeightPlus a LWeightZero = a;
  LWeightZeroR a : LWeightPlus LWeightZero a = a;
  LWeightPlusComm a b : LWeightPlus a b = LWeightPlus b a;
  LPole : Type;
  lpoleweight : LPole -> LWeight
}.

Delimit Scope LWeight_scope with LWeight.
Arguments LWeightPlus [_] _%LWeight _%LWeight.
Notation "a + b" := (LWeightPlus a b) : LWeight_scope.
Notation "0" := LWeightZero : LWeight_scope.

(*************************************************)
(* Auto-derivation rules for proposition weights *)
(*************************************************)
Class LWeightMinus{E:LEnv} (W V X:LWeight) := {
  lweightminus_eqn : (V + X = W)%LWeight
}.
Arguments LWeightMinus [E] W%LWeight V%LWeight X%LWeight.
Arguments Build_LWeightMinus [E] [W] [V] [X] _.
Arguments lweightminus_eqn [E] [W] [V] [X] [_].
Class LWeightMinus1{E:LEnv} (W V X:LWeight) := {
  lweightminus1_eqn : (V + X = W)%LWeight
}.
Arguments LWeightMinus1 [E] W%LWeight V%LWeight X%LWeight.
Arguments Build_LWeightMinus1 [E] [W] [V] [X] _.
Arguments lweightminus1_eqn [E] [W] [V] [X] [_].
Class LWeightAnnihil{E:LEnv} (W:LWeight) := {
  lweightannihil_eqn : (W = 0)%LWeight
}.
Arguments LWeightAnnihil [E] W%LWeight.
Arguments Build_LWeightAnnihil [E] [W] _.
Arguments lweightannihil_eqn [E] [W] [_].

Class LWeightCastPlus{E:LEnv} (W V X:LWeight) := {
  lweightcastplus_eqn : (V + X = W)%LWeight
}.
Arguments LWeightCastPlus [E] W%LWeight V%LWeight X%LWeight.
Arguments Build_LWeightCastPlus [E] [W] [V] [X] _.
Arguments lweightcastplus_eqn [E] [W] [V] [X] [_].
Class LWeightCastSelf{E:LEnv} (W V:LWeight) := {
  lweightcastself_eqn : (V = W)%LWeight
}.
Arguments LWeightCastSelf [E] W%LWeight V%LWeight.
Arguments Build_LWeightCastSelf [E] [W] [V] _.
Arguments lweightcastself_eqn [E] [W] [V] [_].
Class LWeightCastZero{E:LEnv} (W:LWeight) := {
  lweightcastzero_eqn : (0 = W)%LWeight
}.
Arguments LWeightCastZero [E] W%LWeight.
Arguments Build_LWeightCastZero [E] [W] _.
Arguments lweightcastzero_eqn [E] [W] [_].

Instance LWeightCastPlus_self{E:LEnv} (V X:LWeight)
    : LWeightCastPlus (V + X)%LWeight V X := {|
  lweightcastplus_eqn := eq_refl
|}.
Instance LWeightCastZero_self{E:LEnv}
    : LWeightCastZero 0%LWeight := {|
  lweightcastzero_eqn := eq_refl
|}.

Instance LWeightMinus_VPlus{E:LEnv} {W W' X V V0 V1:LWeight}
    {H:LWeightCastPlus V V0 V1}
    {H0:LWeightMinus W V0 W'} {H1:LWeightMinus W' V1 X}
    : LWeightMinus W V X | 0.
Proof.
  rewrite <-(@lweightcastplus_eqn _ _ _ _ H).
  exists.
  rewrite <-LWeightPlusAssoc.
  rewrite (@lweightminus_eqn _ _ _ _ H1).
  apply (@lweightminus_eqn _ _ _ _ H0).
Defined.
Instance LWeightMinus_VSelf{E:LEnv} {W X V V0:LWeight}
    {H:LWeightCastSelf V V0}
    {H0:LWeightMinus W V0 X}
    : LWeightMinus W V X | 2.
Proof.
  rewrite <-(@lweightcastself_eqn _ _ _ H).
  exact H0.
Defined.
Instance LWeightMinus_VZero{E:LEnv} {W V:LWeight}
    {H:LWeightCastZero V}
    : LWeightMinus W V W | 0.
Proof.
  rewrite <-(@lweightcastzero_eqn _ _ H).
  exists.
  apply LWeightZeroR.
Defined.
Instance LWeightMinus_Promote{E:LEnv} {W V X:LWeight}
    {H:LWeightMinus1 W V X}
    : LWeightMinus W V X | 1.
Proof.
  exists.
  apply lweightminus1_eqn.
Defined.

Instance LWeightMinus1_WPlusL{E:LEnv} {W V X W0 W1:LWeight}
    {H:LWeightCastPlus W W0 W1}
    {H0:LWeightMinus1 W0 V X}
    : LWeightMinus1 W V (X + W1) | 0.
Proof.
  rewrite <-(@lweightcastplus_eqn _ _ _ _ H).
  exists.
  rewrite LWeightPlusAssoc.
  f_equal.
  apply (@lweightminus1_eqn _ _ _ _ H0).
Defined.
Instance LWeightMinus1_WPlusR{E:LEnv} {W V X W0 W1:LWeight}
    {H:LWeightCastPlus W W0 W1}
    {H1:LWeightMinus1 W1 V X}
    : LWeightMinus1 W V (W0 + X) | 0.
Proof.
  rewrite <-(@lweightcastplus_eqn _ _ _ _ H).
  exists.
  rewrite (LWeightPlusComm V).
  rewrite <-LWeightPlusAssoc.
  f_equal.
  rewrite LWeightPlusComm.
  apply (@lweightminus1_eqn _ _ _ _ H1).
Defined.
Instance LWeightMinus1_WSelf{E:LEnv} {W V X W0:LWeight}
    {H:LWeightCastSelf W W0}
    {H0:LWeightMinus1 W0 V X}
    : LWeightMinus1 W V X | 2.
Proof.
  rewrite <-(@lweightcastself_eqn _ _ _ H).
  exact H0.
Defined.
Instance LWeightMinus1_self{E:LEnv} {W:LWeight}
    : LWeightMinus1 W W 0 | 0.
Proof.
  exists.
  apply LWeightZeroL.
Defined.

Instance LWeightAnnihil_plus{E:LEnv} {W W0 W1:LWeight}
    {H:LWeightCastPlus W W0 W1}
    {H0:LWeightAnnihil W0}
    {H1:LWeightAnnihil W1}
    : LWeightAnnihil W | 0.
Proof.
  rewrite <-lweightcastplus_eqn.
  exists.
  rewrite (@lweightannihil_eqn _ _ H0).
  rewrite (@lweightannihil_eqn _ _ H1).
  apply LWeightZeroL.
Defined.
Instance LWeightAnnihil_self{E:LEnv} {W:LWeight}
    {H:LWeightCastZero W}
    : LWeightAnnihil W | 0.
Proof.
  exists.
  symmetry; apply lweightcastzero_eqn.
Defined.

Class LWeightEquation{E:LEnv} (W V:LWeight) := {
  lweight_eqn : W = V
}.
Arguments LWeightEquation [E] W V.
Arguments Build_LWeightEquation [E] [W] [V] _.
Arguments lweight_eqn [E] [W] [V] _.

Instance LWeightEquationAuto{E:LEnv} {W V X:LWeight}
    {mn:LWeightMinus W V X} {annihil:LWeightAnnihil X}
    : LWeightEquation W V.
Proof.
  exists.
  rewrite <-lweightminus_eqn.
  rewrite (@lweightannihil_eqn _ _ annihil).
  apply LWeightZeroL.
Defined.

Definition lweight_hole{E:LEnv} (W:LWeight) := W.
Lemma lweight_hole_unfold{E:LEnv} (W:LWeight)
  : lweight_hole W = W.
Proof.
  reflexivity.
Qed.
Opaque lweight_hole.


Require
  MathClasses.theory.naturals.
Require Import
  Coq.setoid_ring.Ring MathClasses.interfaces.abstract_algebra MathClasses.interfaces.naturals MathClasses.interfaces.orders MathClasses.orders.rings.
Require Export
  MathClasses.orders.nat_int.

Section naturals_order.
Context `{Naturals N} `{Apart N} `{!TrivialApart N} `{!FullPseudoSemiRingOrder Nle Nlt}.

Instance nat_nonneg x : PropHolds (0 ≤ x).
Proof. now apply (to_semiring_nonneg (f:=id)). Qed.

Lemma nat_le_plus {x y: N}: x ≤ y ↔ ∃ z, y = x + z.
Proof.
  split; intros E.
   destruct (decompose_le E) as [z [Ez1 Ez2]]. now exists z.
  destruct E as [z Ez].
  apply compose_le with z; [solve_propholds | easy].
Qed.

Lemma nat_not_neg x : ¬x < 0.
Proof. apply le_not_lt_flip, nat_nonneg. Qed.

Lemma nat_0_or_pos x : x = 0 ∨ 0 < x.
Proof.
  destruct (trichotomy (<) 0 x) as [?|[?|?]]; intuition.
  now destruct (nat_not_neg x).
Qed.

Lemma nat_0_or_ge_1 x : x = 0 ∨ 1 ≤ x.
Proof. rewrite <-pos_ge_1. apply nat_0_or_pos. Qed.

Lemma nat_ne_0_pos x : x ≠ 0 ↔ 0 < x.
Proof.
  split.
   destruct (nat_0_or_pos x); intuition.
  intros E1 E2. rewrite E2 in E1. now destruct (irreflexivity (<) 0).
Qed.

Lemma nat_ne_0_ge_1 x : x ≠ 0 ↔ 1 ≤ x.
Proof. rewrite <-pos_ge_1. now apply nat_ne_0_pos. Qed.

Global Instance: ∀ (z : N), PropHolds (z ≠ 0) → OrderReflecting (z *.).
Proof.
   intros z ?.
   repeat (split; try apply _). apply (order_reflecting_pos (.*.) z).
   now apply nat_ne_0_pos.
Qed.

Global Program Instance slow_nat_le_dec: ∀ x y: N, Decision (x ≤ y) | 10 := λ x y,
  match decide (naturals_to_semiring _ nat x ≤ naturals_to_semiring _ nat y) with
  | left E => left _
  | right E => right _
  end.
Next Obligation. now apply (order_reflecting (naturals_to_semiring N nat)). Qed.

Section another_ring.
  Context `{Ring R} `{Apart R} `{!FullPseudoSemiRingOrder (A:=R) Rle Rlt}
    `{!SemiRing_Morphism (f : N → R)}.

  Lemma negate_to_ring_nonpos n : -f n ≤ 0.
  Proof. apply flip_nonneg_negate. now apply to_semiring_nonneg. Qed.

  Lemma between_to_ring n : -f n ≤ f n.
  Proof. apply between_nonneg. now apply to_semiring_nonneg. Qed.
End another_ring.
End naturals_order.

#[global]
Hint Extern 20 (PropHolds (_ ≤ _)) => eapply @nat_nonneg : typeclass_instances.

(* A default order on the naturals *)
#[global]
Instance nat_le `{Naturals N} : Le N | 10 :=  λ x y, ∃ z, x + z = y.
#[global]
Instance nat_lt  `{Naturals N} : Lt N | 10 := dec_lt.

Section default_order.
Context `{Naturals N} `{Apart N} `{!TrivialApart N}.
Add Ring N2 : (rings.stdlib_semiring_theory N).

Instance: Proper ((=) ==> (=) ==> iff) nat_le.
Proof.
  intros x1 y1 E1 x2 y2 E2.
  split; intros [z p]; exists z.
   now rewrite <-E1, <-E2.
  now rewrite E1, E2.
Qed.

Instance: PartialOrder nat_le.
Proof.
  repeat (split; try apply _).
    intros x. exists 0. ring.
   intros x y z [a A] [b B]. exists (a + b). now rewrite associativity, A, B.
   intros x y [a A] [b B].
  destruct (naturals.zero_sum a b) as [E1 E2].
   apply (left_cancellation (+) x).
   rewrite associativity, A, B. ring.
  rewrite <-A, <-B, E1, E2. ring.
Qed.

Instance: SemiRingOrder nat_le.
Proof.
  repeat (split; try apply _).
     intros x y [a A]. now exists a.
    intros x y [a A]. exists a. rewrite <-A. ring.
   intros x y [a A]. exists a.
   apply (left_cancellation (+) z). rewrite <-A. ring.
  intros x y _ _. exists (x * y). ring.
Qed.

Notation n_to_sr := (naturals_to_semiring N nat).

Instance: TotalRelation nat_le.
Proof.
  assert (∀ x y, n_to_sr x ≤ n_to_sr y → x ≤ y) as P.
   intros x y E.
   destruct (decompose_le E) as [a [_ A]].
   exists (naturals_to_semiring nat N a).
   apply (injective n_to_sr).
   rewrite rings.preserves_plus. now rewrite (naturals.to_semiring_involutive _ _).
  intros x y.
  destruct (total (≤) (n_to_sr x) (n_to_sr y)); [left | right]; now apply P.
Qed.

Global Instance: FullPseudoSemiRingOrder nat_le nat_lt.
Proof. now apply dec_full_pseudo_srorder. Qed.
End default_order.


Require Import NatMisc.
Require Import NotationsMisc.
From Equations Require Import Equations.
Set Equations Transparent.
Unset Equations WithK.

(* transport is just eq_rect with adapted parameter ordering and implicity *)

Definition transport {A : Type} {x y : A}
                     (eq : x = y) (B : A -> Type) (bx : B x) : B y :=
  eq_rect x B bx y eq.

Notation "eq '*^' B" := (transport eq B) (at level 1).

(* paths "over" paths *)

Definition eq_over {A : Type} {x y : A} (eq : x = y)
                   (B : A -> Type) (ox : B x) (oy : B y) : Type :=
  eq *^ B ox = oy.

(* TODO think about better notation *)

Notation "ox '=^' B '=(' eq ')=' oy" := (eq_over eq B ox oy) (at level 5).

(* equality in sigma type induces equality in the base. *)

Equations sigmaEq1 {A : Type} {B : A -> Type}
                   {x y : { a : A & B a}} (eq : x = y) : x.1 = y.1 :=
sigmaEq1 eq_refl := eq_refl.

Notation "p '=.1'" := (sigmaEq1 p) (at level 1).

(* and equality in the fibers "over" the base equality *)

Equations sigmaEq2 {A : Type} {B : A -> Type}
                   {x y : { a : A & B a}} (eq : x = y) :
                   x.2 =^ B =( eq=.1 )=  y.2 :=
sigmaEq2 eq_refl := eq_refl.

Notation "p '=.2'" := (sigmaEq2 p) (at level 1).

(* Since "eq_refl*^B" is the identity for any B, "=^ B =( eq_refl )="  is just "=".
   If the base has UIP, and eq : {| a ; x |} = {| a ; y |}, we get x = y.
   We need this for base nat:  *)

Equations sigmaNat {B : nat -> Type} {n : nat} {x y : B n}
                   (eq : {| n ; x |} = {| n ; y |}) : x = y :=
sigmaNat eq := _.
Next Obligation.
  pose (eq=.2) as eq'; simpl in eq'.
  assert (eq=.1 = eq_refl) as eqEqRefl by (apply uipNat).
  rewrite eqEqRefl in eq'.
  exact eq'.
Defined.

(* and for base (nat,nat)  (in curried form...) *)

Equations sigmaNat2 {B : nat -> nat -> Type} {n c : nat} {x y : B n c}
                    (eq : (existT (fun n => { c : nat & B n c }) n {| c ; x |}) =
                          (existT (fun n => { c : nat & B n c }) n {| c ; y |})) : x = y :=
sigmaNat2 eq := _.
Next Obligation.
  apply sigmaNat.
  apply (@sigmaNat (fun n => { c : nat & B n c})).
  exact eq.
Defined.

(* ... *)

Equations sigmaEqCompute {A : Type} {B : A -> Type}
                         (x : {a : A & B a}) : {| x.1 ; x.2 |} = x :=
  sigmaEqCompute {| x1 ; x2 |} := eq_refl.


Require Import ClassicalFacts.
Require Import Coq.Logic.Classical_Prop.
Require Import Coq.micromega.Psatz.
Require Import Coq.Classes.Morphisms.
Require Import Coq.Classes.Morphisms_Prop.
Require Import Coq.Classes.Equivalence.
Require Import Coq.Classes.RelationPairs.
Require Import VCF.LiftConstructors.
Require Import VCF.PairConstructors.

Class AntiSymmetric_Setoid {A:Type} (R: A-> A-> Prop) (R':A->A->Prop): Prop :=
  antisymmetricity_setoid: forall a b, R a b -> R b a -> R' a b.
(*反对称性*)

Class PartialOrder_Setoid {A:Type} (R: A-> A-> Prop)(R':A->A->Prop): Prop :=
{
  PO_AntiSymmetric_Setoid: AntiSymmetric_Setoid R R';
  PO_Reflexive_Setoid: forall x y, R' x y -> R x y;
  PO_Transitive: Transitive R
}.
(*偏序关系*)

Existing Instances PO_AntiSymmetric_Setoid PO_Transitive.

Instance PartialOrder_Setoid_Reflexive:forall A:Type, forall R R':A->A->Prop,
  Equivalence R' -> PartialOrder_Setoid R R' -> Reflexive R.
Proof.
  intros.
  hnf; intros.
  apply PO_Reflexive_Setoid.
  reflexivity.
Qed.

Instance PartialOrder_Setoid_Proper {A:Type} (R R': A -> A -> Prop) {equ: Equivalence R'} {PO: PartialOrder_Setoid R R'}: Proper (R' ==> R' ==> iff) R.
Proof.
  unfold Proper, respectful.
  intros.
  split; intros.
  + transitivity x0; [| apply PO_Reflexive_Setoid; auto].
    transitivity x; [apply PO_Reflexive_Setoid; symmetry; auto |].
    auto.
  + transitivity y0; [| apply PO_Reflexive_Setoid; symmetry; auto].
    transitivity y; [apply PO_Reflexive_Setoid; auto |].
    auto.
Qed.

Definition upper_bound {A:Type} (R:A->A->Prop) (T: A -> Prop) (bnd:A):Prop :=
forall a, T a -> R a bnd.
(*检查bnd是否是一个集合的上界*)

Definition least_upper_bound {A:Type} (R:A->A->Prop) (T: A -> Prop) (bnd:A): Prop :=
  upper_bound R T bnd /\
  forall bnd', upper_bound R T bnd' -> R bnd bnd'.
(*一个集合的上确界*)

Definition lower_bound {A:Type} (R:A->A->Prop) (T: A -> Prop) (bnd:A):Prop :=
forall a, T a -> R bnd a.
(*检查bnd是否是一个集合的下界*)

Definition greatest_lower_bound {A:Type} (R:A->A->Prop) (T: A -> Prop) (bnd:A): Prop :=
  lower_bound R T bnd /\
  forall bnd', lower_bound R T bnd' -> R bnd' bnd.
(*一个集合的下确界*)

Class CompleteLattice_Setoid {A:Type} (R:A->A->Prop) (R': A-> A-> Prop) (lub: (A -> Prop) -> A): Prop :=
{
  CL_PartialOrder: PartialOrder_Setoid R R';
  CL_Complete_lub: forall T, least_upper_bound R T (lub T);
  lub_congr: forall T1 T2, (forall a, T1 a <-> T2 a) -> R' (lub T1) (lub T2);
}.

Existing Instance CL_PartialOrder.

Definition fixpoint {A:Type} (f:A->A) (a:A)(R':A->A->Prop): Prop:=
R' a (f a).
(* 函数f的不动点, R' 为等价关系*)

Definition least_fixpoint {A:Type} (f:A->A) (R:A->A->Prop) (R':A->A->Prop)(a:A): Prop:=
fixpoint f a R' /\ forall a', fixpoint f a' R'-> R a a'.
(* 最小不动点 *)

Definition greatest_fixpoint {A:Type} (f:A->A) (R:A->A->Prop) (R':A->A->Prop)(a:A): Prop:=
fixpoint f a R' /\ forall a', fixpoint f a' R'-> R a' a.
(* 最大不动点 *)

Definition lower_than_fp {A:Type} (F:A->A) (R:A->A->Prop): A -> Prop :=
  fun x => R x (F x).

Definition greater_than_fp {A:Type} (F:A->A) (R:A->A->Prop): A -> Prop :=
  fun x => R (F x) x.

Definition glb_of_lub {A:Type} (R:A->A->Prop) (lub: (A -> Prop) -> A): (A -> Prop) -> A :=
  fun X => lub (lower_bound R X).

Definition KT_fix_g {A: Type} {R R': A -> A -> Prop} {lub} {CL: CompleteLattice_Setoid R R' lub} (f: A -> A): A :=
  lub (lower_than_fp f R).

Definition KT_fix_l {A: Type} {R R': A -> A -> Prop} {lub} {CL: CompleteLattice_Setoid R R' lub} (f: A -> A): A :=
  glb_of_lub R lub (greater_than_fp f R).

Lemma lub_less_than {A: Type} {R R': A -> A -> Prop} {lub} {CL: CompleteLattice_Setoid R R' lub}: forall (X: A -> Prop) y,
  (forall x, X x -> R x y) ->
  R (lub X) y.
Proof.
  intros.
  pose proof CL_Complete_lub X as [? ?].
  apply H1.
  auto.
Qed.

Lemma less_than_lub {A: Type} {R R': A -> A -> Prop} {lub} {CL: CompleteLattice_Setoid R R' lub}: forall x y (Y: A -> Prop),
  Y y ->
  R x y ->
  R x (lub Y).
Proof.
  intros.
  pose proof CL_Complete_lub Y as [? ?].
  rewrite H0.
  apply H1.
  auto.
Qed.

Lemma element_less_than_lub {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall x (X: A -> Prop),
  X x ->
  R x (lub X).
Proof.
  intros.
  apply less_than_lub with x; [| reflexivity].
  auto.
Qed.

Lemma CL_Complete_glb {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall T, greatest_lower_bound R T (glb_of_lub R lub T).
Proof.
  intros.
  hnf; intros.
  split.
  + hnf; intros.
    unfold glb_of_lub.
    apply lub_less_than.
    intros.
    auto.
  + intros.
    unfold glb_of_lub.
    apply element_less_than_lub; auto.
Qed.

Lemma glb_congr {A: Type} {R R': A -> A -> Prop} {lub} {CL: CompleteLattice_Setoid R R' lub}: forall T1 T2, (forall a, T1 a <-> T2 a) -> R' (glb_of_lub R lub T1) (glb_of_lub R lub T2).
Proof.
  intros.
  unfold glb_of_lub.
  apply lub_congr.
  intros.
  unfold lower_bound.
  apply all_iff_morphism; intros ?.
  rewrite H; tauto.
Qed.

Lemma less_than_glb {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall (X: A -> Prop) y,
  (forall x, X x -> R y x) ->
  R y (glb_of_lub R lub X).
Proof.
  intros.
  pose proof CL_Complete_glb X as [? ?].
  apply H1.
  auto.
Qed.

Lemma glb_less_than {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall x y (Y: A -> Prop),
  Y y ->
  R y x ->
  R (glb_of_lub R lub Y) x.
Proof.
  intros.
  pose proof CL_Complete_glb Y as [? ?].
  rewrite <- H0.
  apply H1.
  auto.
Qed.

Lemma glb_less_than_element {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall x (X: A -> Prop),
  X x ->
  R (glb_of_lub R lub X) x.
Proof.
  intros.
  apply glb_less_than with x; [| reflexivity].
  auto.
Qed.

Lemma KnasterTarski_fixpoint_theorem_g {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall f,
  Proper (R ==> R) f ->
  R' (KT_fix_g f) (f (KT_fix_g f)).
Proof.
  intros.
  assert (R (KT_fix_g f) (f (KT_fix_g f))).
  {
    unfold KT_fix_g.
    apply lub_less_than.
    intros.
    hnf in H0.
    rewrite H0.
    apply H.
    apply element_less_than_lub; auto.
  }
  apply antisymmetricity_setoid; auto.
  pose proof H _ _ H0: lower_than_fp f R ((f (KT_fix_g f))).
  apply element_less_than_lub in H1.
  auto.
Qed.

Lemma KnasterTarski_fixpoint_greatest_fixpoint {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall f x,
  Proper (R ==> R) f ->
  R' x (f x) ->
  R x (KT_fix_g f).
Proof.
  intros.
  pose proof CL_Complete_lub (lower_than_fp f R) as [? ?].
  apply H1.
  hnf.
  rewrite <- H0.
  reflexivity.
Qed.

Lemma KnasterTarski_fixpoint_theorem_l {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall f,
  Proper (R ==> R) f ->
  R' (KT_fix_l f) (f (KT_fix_l f)).
Proof.
  intros.
  assert (R (f (KT_fix_l f)) (KT_fix_l f)).
  {
    unfold KT_fix_l.
    apply less_than_glb.
    intros.
    hnf in H0.
    rewrite <- H0.
    apply H.
    apply glb_less_than_element; auto.
  }
  apply antisymmetricity_setoid; auto.
  pose proof H _ _ H0: greater_than_fp f R ((f (KT_fix_l f))).
  apply glb_less_than_element in H1.
  auto.
Qed.

Lemma KnasterTarski_fixpoint_least_fixpoint {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall f x,
  Proper (R ==> R) f ->
  R' x (f x) ->
  R (KT_fix_l f) x.
Proof.
  intros.
  pose proof CL_Complete_glb (greater_than_fp f R) as [? ?].
  apply H1.
  hnf.
  rewrite <- H0.
  reflexivity.
Qed.

Lemma KnasterTarski_fixpoint_least_prefixpoint {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}: forall f x,
  Proper (R ==> R) f ->
  R (f x) x ->
  R (KT_fix_l f) x.
Proof.
  intros.
  pose proof CL_Complete_glb (greater_than_fp f R) as [? ?].
  apply H1.
  hnf.
  auto.
Qed.

Lemma least_fixpoint_mono {A: Type} {R R': A -> A -> Prop} {lub} {Equ: Equivalence R'} {CL: CompleteLattice_Setoid R R' lub}:
  forall f g,
    Proper (R ==> R) f ->
    Proper (R ==> R) g ->
    lift_rel2 R f g ->
    R (KT_fix_l f) (KT_fix_l g).
Proof.
  intros.
  apply KnasterTarski_fixpoint_least_prefixpoint; auto.
  rewrite (KnasterTarski_fixpoint_theorem_l g) at 2 by auto.
  apply H1.
Qed.

Class CL_Domain (A:Type): Type :=
{
  Domain_R: A -> A -> Prop;
  Domain_R': A -> A -> Prop;
  Domain_lub: (A -> Prop) -> A;
  Domain_Equiv: Equivalence Domain_R';
  Domain_CompleteLattice: CompleteLattice_Setoid Domain_R Domain_R' Domain_lub;
}.

Existing Instances Domain_Equiv Domain_CompleteLattice.

Lemma lift_complete_lattice {X A R R' lub} (CL: @CompleteLattice_Setoid A R R' lub):
  @CompleteLattice_Setoid (X -> A) (lift_rel2 R) (lift_rel2 R') (lift_set_summary lub).
Proof.
  constructor.
  + constructor; hnf; intros.
    - intros ?; apply antisymmetricity_setoid; auto.
    - intros ?; apply PO_Reflexive_Setoid; auto.
    - intros ?; eapply transitivity; eauto.
  + intros.
    split.
    - hnf; intros.
      intros ?.
      refine (proj1 (CL_Complete_lub _) _ _).
      eauto.
    - intros.
      intros ?.
      refine (proj2 (CL_Complete_lub _) _ _).
      hnf; intros.
      destruct H0 as [? [? ?]]; subst.
      apply (H x); auto.
  + intros.
    intros ?.
    unfold lift_set_summary.
    apply lub_congr.
    intros. apply ex_iff_morphism.
    intros ?.
    specialize (H a1); tauto.
Qed.

Instance Lift_CL_Domain (A B: Type) {CL: CL_Domain B}: CL_Domain (A -> B) :=
    Build_CL_Domain (A -> B) (lift_rel2 Domain_R) (lift_rel2 Domain_R') (lift_set_summary Domain_lub) ltac:(apply pointwise_equivalence; typeclasses eauto) (lift_complete_lattice Domain_CompleteLattice).

Lemma pair_CL
        {A B AB: Type}
        (projA: AB -> A)
        (projB: AB -> B)
        {RA RA': A -> A -> Prop}
        {RB RB': B -> B -> Prop}
        (RAB RAB': AB -> AB -> Prop)
        {lubA lubB} lubAB
        (CLA: CompleteLattice_Setoid RA RA' lubA)
        (CLB: CompleteLattice_Setoid RB RB' lubB):
  (forall c1 c2, RAB c1 c2 <-> RA (projA c1) (projA c2) /\ RB (projB c1) (projB c2)) ->
  (forall c1 c2, RAB' c1 c2 <-> RA' (projA c1) (projA c2) /\ RB' (projB c1) (projB c2)) ->
  (forall cs, projA (lubAB cs) = lubA (fun a => exists c, cs c /\ projA c = a) /\
              projB (lubAB cs) = lubB (fun b => exists c, cs c /\ projB c = b)) ->
  (forall c1 c2, projA c1 = projA c2 -> projB c1 = projB c2 -> c1 = c2) ->
  @CompleteLattice_Setoid AB RAB RAB' lubAB.
Proof.
  intros HR HR' Hlub Hproj.
  constructor.
  + constructor.
    - hnf; intros.
      rewrite HR in H, H0; rewrite HR'.
      destruct H, H0.
      split; apply antisymmetricity_setoid; auto.
    - intros.
      rewrite HR' in H; rewrite HR.
      destruct H.
      split; apply PO_Reflexive_Setoid; auto.
    - hnf; intros.
      rewrite HR in H, H0 |- *.
      destruct H, H0.
      split; etransitivity; eauto.
  + intros.
    hnf; intros.
    split.
    - hnf; intros.
      rewrite HR.
      rewrite (proj1 (Hlub _)), (proj2 (Hlub _)).
      split; apply (proj1 (CL_Complete_lub _)); eauto.
    - intros.
      rewrite HR.
      rewrite (proj1 (Hlub _)), (proj2 (Hlub _)).
      split; apply (proj2 (CL_Complete_lub _)).
      * hnf.
        intros a [c [? ?]]; subst.
        specialize (H _ H0).
        rewrite HR in H; tauto.
      * hnf.
        intros a [c [? ?]]; subst.
        specialize (H _ H0).
        rewrite HR in H; tauto.
  + intros.
    rewrite HR'.
    rewrite !(proj1 (Hlub _)), !(proj2 (Hlub _)).
    split; apply lub_congr; intros; apply ex_iff_morphism; intros ?.
    - rewrite H; tauto.
    - rewrite H; tauto.
Qed.

Lemma prod_pair_CL
        {A B: Type}
        {RA RA': A -> A -> Prop}
        {RB RB': B -> B -> Prop}
        {lubA lubB}
        {CLA: CompleteLattice_Setoid RA RA' lubA}
        {CLB: CompleteLattice_Setoid RB RB' lubB}:
  @CompleteLattice_Setoid
    (A * B)
    (pair_rel2 RA RB)
    (pair_rel2 RA' RB')
    (pair_set_summary lubA lubB).
Proof.
  apply (pair_CL fst snd _ _ _ CLA CLB).
  + intros; reflexivity.
  + intros; reflexivity.
  + intros; split; reflexivity.
  + intros [? ?] [? ?]; simpl; intros; subst; reflexivity.
Qed.

Instance Pair_CL_Domain (A B: Type) {CLA: CL_Domain A} {CLB: CL_Domain B}: CL_Domain (A * B) :=
  Build_CL_Domain
    (A * B)
    (pair_rel2 Domain_R Domain_R)
    (pair_rel2 Domain_R' Domain_R')
    (pair_set_summary Domain_lub Domain_lub)
    ltac:(refine (RelProd_Equivalence _ _ _ _); typeclasses eauto)
    (prod_pair_CL).

Definition KT_fix2_l
             {A B}
             {RA RA': A -> A -> Prop}
             {RB RB': B -> B -> Prop}
             {lubA lubB}
             {CLA: CompleteLattice_Setoid RA RA' lubA}
             {CLB: CompleteLattice_Setoid RB RB' lubB}
             (f: A -> B -> A) (g: A -> B -> B): A * B :=
  @KT_fix_l
    (A * B) _ _ _ prod_pair_CL
    (fun c => (f (fst c) (snd c), g (fst c) (snd c))).

Corollary two_dimensional_fix_coincide:
  forall {A B: Type}
         {RA RA': A -> A -> Prop}
         {RB RB': B -> B -> Prop}
         {lubA lubB}
         {equA: Equivalence RA'}
         {equB: Equivalence RB'}
         {CLA: CompleteLattice_Setoid RA RA' lubA}
         {CLB: CompleteLattice_Setoid RB RB' lubB},
  forall (f: A -> B -> A) (g: A -> B -> B)
    (Hf_ProperR: Proper (RA ==> RB ==> RA) f)
    (Hg_ProperR: Proper (RA ==> RB ==> RB) g),
    (pair_rel2 RA' RB')
      (KT_fix2_l f g)
      (KT_fix2_l (fun _ b => KT_fix_l (fun a => f a b)) (fun a _ => KT_fix_l (fun b => g a b))).
Proof.
  intros.
  assert (Proper (RA' ==> RB' ==> RA') f) as Hf_ProperR'.
  {
    hnf; intros; hnf; intros.
    apply antisymmetricity_setoid.
    + apply Hf_ProperR; [rewrite H | rewrite H0]; reflexivity.
    + apply Hf_ProperR; [rewrite H | rewrite H0]; reflexivity.
  }
  assert (Proper (RA' ==> RB' ==> RB') g) as Hg_ProperR'.
  {
    hnf; intros; hnf; intros.
    apply antisymmetricity_setoid.
    + apply Hg_ProperR; [rewrite H | rewrite H0]; reflexivity.
    + apply Hg_ProperR; [rewrite H | rewrite H0]; reflexivity.
  }
  assert (forall b, Proper (RA ==> RA) (fun a => f a b)) as Hf_mono.
  {
    intros; hnf; intros.
    apply Hf_ProperR; auto.
    reflexivity.
  }
  assert (forall a, Proper (RB ==> RB) (fun b => g a b)) as Hg_mono.
  {
    intros; hnf; intros.
    apply Hg_ProperR; auto.
    reflexivity.
  }
  assert (Equivalence (pair_rel2 RA' RB')) as equAB by (apply RelProd_Equivalence; auto).
  set (h := fun c => (f (fst c) (snd c), g (fst c) (snd c))).
  set (h' := fun c => (KT_fix_l (fun a => f a (snd c)), KT_fix_l (fun b => g (fst c) b))).
  assert (Proper (pair_rel2 RA RB ==> pair_rel2 RA RB) h) as Hh_ProperR.
  {
    hnf; intros [? ?] [? ?] [? ?].
    split; [apply Hf_ProperR | apply Hg_ProperR]; auto.
  }
  assert (Proper (pair_rel2 RA RB ==> pair_rel2 RA RB) h') as Hh'_ProperR.
  {
    hnf; intros [? ?] [? ?] [? ?].
    split; simpl.
    + apply least_fixpoint_mono; auto.
      hnf; intros; simpl in H0 |- *.
      rewrite H0; reflexivity.
    + apply least_fixpoint_mono; auto.
      hnf; intros; simpl in H |- *.
      rewrite H; reflexivity.
  }
  assert (pair_rel2 RA' RB'
            (@KT_fix_l _ _ _ _ (prod_pair_CL) h')
            (h (@KT_fix_l _ _ _ _ (prod_pair_CL) h'))) as Hfix1.
  {
    pose proof (@KnasterTarski_fixpoint_theorem_l _ _ _ _ _ (prod_pair_CL) h') _ as [? ?].
    split.
    + unfold h' at 2 in H.
      unfold fst at 2 in H.
      pose proof (@KnasterTarski_fixpoint_theorem_l _ _ _ _ _ CLA (fun a => f a (snd (@KT_fix_l _ _ _ _ (prod_pair_CL) h'))) _).
      cbv beta in H1.
      rewrite <- H in H1.
      exact H1.
    + unfold h' at 2 in H0.
      unfold snd at 2 in H0.
      pose proof (@KnasterTarski_fixpoint_theorem_l _ _ _ _ _ CLB (fun b => g (fst (@KT_fix_l _ _ _ _ (prod_pair_CL) h')) b) _).
      cbv beta in H1.
      rewrite <- H0 in H1.
      exact H1.
  }
  apply (@KnasterTarski_fixpoint_least_fixpoint _ _ _ _ _ (prod_pair_CL)) in Hfix1; auto.
  assert (pair_rel2 RA RB
            (h' (@KT_fix_l _ _ _ _ (prod_pair_CL) h))
            (@KT_fix_l _ _ _ _ (prod_pair_CL) h)) as Hfix2.
  {
    pose proof (@KnasterTarski_fixpoint_theorem_l _ _ _ _ _ (prod_pair_CL) h) _ as [? ?].
    split.
    + unfold h'.
      unfold fst at 1.
      apply KnasterTarski_fixpoint_least_fixpoint; auto.
    + unfold h'.
      unfold snd at 1.
      apply KnasterTarski_fixpoint_least_fixpoint; auto.
  }
  apply (@KnasterTarski_fixpoint_least_prefixpoint _ _ _ _ _ (prod_pair_CL)) in Hfix2; auto.
  pose proof (@prod_pair_CL A B _ _ _ _ _ _ _ _).
  apply antisymmetricity_setoid; auto.
Qed.