Require Import ZArith.
Require Import String.
Require Import Basics.
Require Import Program.Combinators.

Require Import Algae.Group.
Require Import Algae.Monoid.
Require Import Algae.Semigroup.

(* And here we play with the machinery because it's fun! *)

(* This is useless since N can't form a group. *)
Instance magma_nat : Magma nat plus.
Proof. reflexivity. Qed.

Instance semigroup_nat : Semigroup magma_nat.
Proof.
  split.
  intros.
  apply Plus.plus_assoc.
Qed.

Instance monoid_nat : Monoid semigroup_nat 0.
Proof.
  split.
  intros.
  apply Plus.plus_0_l.
  apply Plus.plus_0_r.
Qed.

Require Export Ascii.
Open Scope string_scope.

(* This is useless for us too - The free monoid over strings + string concat. *)
(* I couldn't find a built-in exported theorem for associativity of append. *)
Instance magma_str_concat : Magma string append.
Proof. reflexivity. Qed.

Lemma string_assoc :
  (forall a x y, (String a x) <+> y = String a (x <+> y)).
Proof. reflexivity. Qed.

Instance semigroup_str_concat : Semigroup magma_str_concat.
Proof.
  split.
  intros.
  induction x.
  reflexivity.
  rewrite string_assoc.
  rewrite IHx.
  reflexivity.
Qed.

Instance monoid_str_concat : Monoid semigroup_str_concat EmptyString.
Proof.
  split.
  intros.
  trivial.
  intros.
  induction x.
  trivial.
  rewrite string_assoc.
  rewrite IHx.
  reflexivity.
Qed.

(* Functions form a monoid under composition. *)
Variable A : Type.
Instance magma_functions  : Magma (A -> A) compose.
Proof. reflexivity. Qed.

Instance semigroup_functions : Semigroup magma_functions.
Proof.
  split.
  reflexivity.
Qed.

Definition id x : A := x.

Instance monoid_functions : Monoid semigroup_functions id.
Proof.
  split.
  intros.
  reflexivity.
  reflexivity.
Qed.

(* They only form a group if they are all isomorphisms in the respective *)
(* category (bijections in Set). So we can't say much here. *)

Instance magma_ints_add : Magma Z Z.add.
Proof. reflexivity. Qed.

Instance semigroup_ints_add : Semigroup magma_ints_add.
Proof.
  split.
  intros.
  rewrite Z.add_assoc.
  reflexivity.
Qed.

Instance monoid_ints_add : Monoid semigroup_ints_add Z0.
Proof.
  split.
  apply Z.add_0_l.
  apply Z.add_0_r.
Qed.

Instance group_ints_add : Group monoid_ints_add Z.opp.
Proof.
  split.
  intros.
  rewrite Z.add_comm.
  rewrite Z.add_opp_diag_r.
  reflexivity.
  intros.
  rewrite Z.add_comm.
  rewrite Z.add_opp_diag_l.
  reflexivity.
Qed.

Require Export Ascii.
Open Scope string_scope.
Eval compute in "aa" <+> "bb".


From Coq Require Import Reals.
From Coq Require Import QArith.
Require Import Field.

Open Scope R_scope.

Definition C : Type := R * R.

Definition RtoC (r : R) : C := (r, 0).
Coercion RtoC : R >-> C.

Declare Scope C_scope.
Bind Scope C_scope with C.
Open Scope C_scope.

Notation i := (0,1).
Notation "0" := (RtoC 0) : C_scope.
Notation "1" := (RtoC 1) : C_scope.

Definition Cadd (c1 c2 : C) : C := (fst c1 + fst c2, snd c1 + snd c2).
Definition Csub (c1 c2 : C) : C := (fst c1 - fst c2, snd c1 - snd c2).
Definition Cmul (c1 c2 : C) : C := (fst c1 * fst c2 - snd c1 * snd c2, fst c1 * snd c2 + snd c1 * fst c2).
Definition Copp (c : C) : C := (- fst c, - snd c).
Definition Cinv (c : C) : C :=
  ( fst c / (fst c * fst c + snd c * snd c), - snd c / (fst c * fst c + snd c * snd c) ).
Definition Cdiv (c1 c2 : C) : C := Cmul c1 (Cinv c2).

Lemma c_proj_eq : forall (c1 c2 : C),
  fst c1 = fst c2 -> snd c1 = snd c2 -> c1 = c2.
Proof.
  intros. destruct c1, c2. simpl in *. subst. reflexivity.
Qed.

Lemma Cadd_0_l: forall (c: C),
  Cadd 0 c = c.
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Cadd_comm: forall (c1 c2: C),
  Cadd c1 c2 = Cadd c2 c1.
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Cadd_assoc: forall c1 c2 c3: C,
  Cadd c1 (Cadd c2 c3) = Cadd (Cadd c1 c2) c3.
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Cmul_1_l: forall c : C,
  Cmul 1 c = c.
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Cmul_comm: forall c1 c2 : C,
  Cmul c1 c2 = Cmul c2 c1.
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Cmul_assoc: forall c1 c2 c3 : C,
  ( Cmul c1 (Cmul c2 c3) ) = ( Cmul (Cmul c1 c2) c3 ).
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Cdistr_l: forall c1 c2 c3: C,
  Cmul (Cadd c1 c2) c3 = Cadd (Cmul c1 c3) (Cmul c2 c3).
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma Copp_def : forall c : C,
  Cadd c (Copp c) = 0.
Proof.
  intros. apply c_proj_eq; simpl; ring.
Qed.

Lemma C1_neq_C0 : 1 <> 0.
Proof.
Admitted.

Lemma Cinv_l : forall c: C,
  c <> 0 -> Cmul (Cinv c) c = 1.
Proof.
  Admitted.

Lemma C_Field_Theory : @field_theory C 0 1 Cadd Cmul Csub Copp Cdiv Cinv eq.
Proof.
  constructor. constructor.
  - apply Cadd_0_l.
  - apply Cadd_comm.
  - apply Cadd_assoc.
  - apply Cmul_1_l.
  - apply Cmul_comm.
  - apply Cmul_assoc.
  - apply Cdistr_l.
  - reflexivity.
  - apply Copp_def.
  - apply C1_neq_C0.
  - reflexivity.
  - apply Cinv_l.
Defined.
Add Field CField : C_Field_Theory.


(*
Verificación Formal - 2020-II
Archivo de definiciones - Números por paridad
*)

(*
------------------------------------####------------------------------------
------------------------------------####------------------------------------
                Inicio del Fragmento de código visto en clase.
------------------------------------####------------------------------------
------------------------------------####------------------------------------
*)

(*
Definición inductiva para los números por paridad
*)
Inductive BN :=
  | Z : BN
  | U : BN -> BN
  | D : BN -> BN.

Check BN_ind.
Check BN_rec.
Check BN_rect.

(*
Functió sucesor
*)
Fixpoint sucBN (b:BN) : BN :=
match b with
  | Z => U Z
  | U x => D x           (*S(U x) = S(2x + 1) = 2x + 2 = D x*)
  | D x => U (sucBN x)   (*S(D x)= S(2x + 2) = S(S(2x + 1)) = S(2x + 1) + 1  *)
                         (* 2(S(x)) + 1 = 2(x+1) + 1 = (2x + 2) + 1 = S(2x + 1) + 1*)
end.

(*
Se asume la existencia de un elemento indefinido en BN
*)
Parameter (undefBN: BN).

Fixpoint predBN (b:BN): BN :=
match b with
  | Z => undefBN
  | U Z => Z
  | U x => D (predBN x)
  | D x => U x
end.

(*
Funciones para convertir entre BN y nat
*)
Fixpoint toN (b:BN) : nat :=
match b with
  | Z => 0
  | U x => 2*(toN x) + 1
  | D x => 2*(toN x) + 2
end.

Fixpoint toBN (n: nat) : BN :=
match n with
  | 0 => Z
  | S x => sucBN (toBN x)
end.

Eval compute in (toN (predBN (toBN 47))).
Eval compute in toN(D(U(U Z))).
Eval compute in toN(sucBN(D(U(U Z)))).
Eval compute in toBN 16.

(*
Definición de la suma para BN
*)
Fixpoint plusBN (a b : BN) : BN :=
match a,b with
  | Z, b => b
  | a, Z  => a
  | U x, U y => D(plusBN x y)
  | D x, U y => U(sucBN (plusBN x y))
  | U x, D y => U(sucBN (plusBN x y))
  | D x, D y => D(sucBN (plusBN x y))
end.
Notation "a ⊞ b" := (plusBN a b) (at level 60).

(*
Definición del orden estricto para BN
*)
Inductive ltBN : BN -> BN -> Prop :=
  | ltBNZU : forall (a:BN), ltBN Z (U a)
  | ltBNZD : forall (a:BN), ltBN Z (D a)
  | ltBNUU : forall (a b:BN), ltBN a b -> ltBN (U a) (U b)
  | ltBNUDeq : forall (a :BN), ltBN (U a) (D a)
  | ltBNUD : forall (a b:BN), ltBN a b -> ltBN (U a) (D b)
  | ltBNDU : forall (a b:BN), ltBN a b -> ltBN (D a) (U b)
  | ltBNDD : forall (a b:BN), ltBN a b -> ltBN (D a) (D b).

(*
Definición del orden suave para BN
*)
Inductive lteqBN: BN -> BN -> Prop :=
  | lteqBNref: forall (a:BN), lteqBN a a
  | lteqBNl: forall (a b: BN), ltBN a b -> lteqBN a b.
Notation "a <BN b" := (ltBN a b) (at level 70).
Notation "a <BN b <BN c" := (ltBN a b /\ ltBN b c) (at level 70, b at next level).
Notation "a ≤BN b" := (lteqBN a b) (at level 70).

(*
------------------------------------####------------------------------------
------------------------------------####------------------------------------
                   Fin del fragmento de código visto en clase.
------------------------------------####------------------------------------
------------------------------------####------------------------------------
*)


(** * Logic: Logic in Coq *)

(** Zhaoguo Wang **)

Require Export MoreProp.

(** Coq's built-in logic is very small: the only primitives are
    [Inductive] definitions, universal quantification ([forall]), and
    implication ([->]), while all the other familiar logical
    connectives -- conjunction, disjunction, negation, existential
    quantification, even equality -- can be encoded using just these.

    This chapter explains the encodings and shows how the tactics
    we've seen can be used to carry out standard forms of logical
    reasoning involving these connectives. *)

(* ########################################################### *)
(** * Conjunction *)

(** The logical conjunction of propositions [P] and [Q] can be
    represented using an [Inductive] definition with one
    constructor. *)

Inductive and (P Q : Prop) : Prop :=
  conj : P -> Q -> (and P Q).

(** Note that, like the definition of [ev] in the previous
    chapter, this definition is parameterized; however, in this case,
    the parameters are themselves propositions, rather than numbers. *)

(** The intuition behind this definition is simple: to
    construct evidence for [and P Q], we must provide evidence
    for [P] and evidence for [Q].  More precisely:

    - [conj p q] can be taken as evidence for [and P Q] if [p]
      is evidence for [P] and [q] is evidence for [Q]; and

    - this is the _only_ way to give evidence for [and P Q] --
      that is, if someone gives us evidence for [and P Q], we
      know it must have the form [conj p q], where [p] is
      evidence for [P] and [q] is evidence for [Q].

   Since we'll be using conjunction a lot, let's introduce a more
   familiar-looking infix notation for it. *)

Notation "P /\ Q" := (and P Q) : type_scope.

(** (The [type_scope] annotation tells Coq that this notation
    will be appearing in propositions, not values.) *)

(** Consider the "type" of the constructor [conj]: *)

Check conj.
(* ===>  forall P Q : Prop, P -> Q -> P /\ Q *)

(** Notice that it takes 4 inputs -- namely the propositions [P]
    and [Q] and evidence for [P] and [Q] -- and returns as output the
    evidence of [P /\ Q]. *)

(** Besides the elegance of building everything up from a tiny
    foundation, what's nice about defining conjunction this way is
    that we can prove statements involving conjunction using the
    tactics that we already know.  For example, if the goal statement
    is a conjuction, we can prove it by applying the single
    constructor [conj], which (as can be seen from the type of [conj])
    solves the current goal and leaves the two parts of the
    conjunction as subgoals to be proved separately. *)

Theorem and_example :
  (beautiful 0) /\ (beautiful 3).
Proof.
  apply conj.
  (* Case "left". *) apply b_0.
  (* Case "right". *) apply b_3.  Qed.

(** Let's take a look at the proof object for the above theorem. *)

Print and_example.
(* ===>  conj (beautiful 0) (beautiful 3) b_0 b_3
            : beautiful 0 /\ beautiful 3 *)

(** Note that the proof is of the form
    conj (beautiful 0) (beautiful 3)
         (...pf of beautiful 3...) (...pf of beautiful 3...)
    as you'd expect, given the type of [conj]. *)

(** Just for convenience, we can use the tactic [split] as a shorthand for
    [apply conj]. *)

Theorem and_example' :
  (ev 0) /\ (ev 4).
Proof.
  split.
    Case "left". apply ev_0.
    Case "right". apply ev_SS. apply ev_SS. apply ev_0.  Qed.

(** Conversely, the [inversion] tactic can be used to take a
    conjunction hypothesis in the context, calculate what evidence
    must have been used to build it, and add variables representing
    this evidence to the proof context. *)

Theorem proj1 : forall P Q : Prop,
  P /\ Q -> P.
Proof.
  intros P Q H.
  inversion H as [HP HQ].
  apply HP.  Qed.

(** **** Exercise: 1 star, optional (proj2) *)
Theorem proj2 : forall P Q : Prop,
  P /\ Q -> Q.
Proof.
  intros P Q H.
  inversion H as [ HP HQ ].
  apply HQ.
Qed.

Theorem and_commut : forall P Q : Prop,
  P /\ Q -> Q /\ P.
Proof.
  (* WORKED IN CLASS *)
  intros P Q H.
  inversion H as [HP HQ].
  split.
    (* Case "left". *) apply HQ.
    (* Case "right".*) apply HP.  Qed.

(** Once again, we have commented out the [Case] tactics to make the
    proof object for this theorem easy to understand.  Examining it
    shows that all that is really happening is taking apart a record
    containing evidence for [P] and [Q] and rebuilding it in the
    opposite order: *)

Print and_commut.
(* ===>
   and_commut =
     fun (P Q : Prop) (H : P /\ Q) =>
     let H0 := match H with
               | conj HP HQ => conj Q P HQ HP
               end
     in H0
     : forall P Q : Prop, P /\ Q -> Q /\ P *)

(** **** Exercise: 2 stars (and_assoc) *)
(** In the following proof, notice how the _nested pattern_ in the
    [inversion] breaks the hypothesis [H : P /\ (Q /\ R)] down into
    [HP: P], [HQ : Q], and [HR : R].  Finish the proof from there: *)

Theorem and_assoc : forall P Q R : Prop,
  P /\ (Q /\ R) -> (P /\ Q) /\ R.
Proof.
  intros P Q R H.
  inversion H as [HP [HQ HR]].
  split.
  split.
  Case "P". apply HP.
  Case "Q". apply HQ.
  Case "R". apply HR.
Qed.

(** **** Exercise: 2 stars (even__ev) *)
(** Now we can prove the other direction of the equivalence of [even]
   and [ev], which we left hanging in chapter [Prop].  Notice that the
   left-hand conjunct here is the statement we are actually interested
   in; the right-hand conjunct is needed in order to make the
   induction hypothesis strong enough that we can carry out the
   reasoning in the inductive step.  (To see why this is needed, try
   proving the left conjunct by itself and observe where things get
   stuck.) *)

Theorem even__ev : forall n : nat,
  (even n -> ev n) /\ (even (S n) -> ev (S n)).
Proof.
  induction n as [ | n' ].
  split.

  Case "n = O".
  intro H.
  apply ev_0.

  Case "n = 1".
  intro H.
  inversion H.

  Case "n = S n'".
  split.

  SCase "Left".
  intro H.
  destruct IHn' as [ Hn' HSn' ].
  apply HSn' in H.
  apply H.

  SCase "Right".
  intro H.
  destruct IHn' as [ Hn' HSn' ].
  apply ev_SS in Hn'.
  apply Hn'.
  inversion H as [ H' ].
  apply H'.
Qed.

(** **** Exercise: 2 stars, optional (conj_fact) *)
(** Construct a proof object demonstrating the following proposition. *)

Definition conj_fact : forall P Q R, P /\ Q -> Q /\ R -> P /\ R :=
fun P Q R (H1:(P /\ Q)) ( H2:( Q /\ R))
=> conj P R  (proj1 P Q H1) (proj2 Q R H2).

(* ###################################################### *)
(** ** Iff *)

(** The handy "if and only if" connective is just the conjunction of
    two implications. *)

Definition iff (P Q : Prop) := (P -> Q) /\ (Q -> P).

Notation "P <-> Q" := (iff P Q)
                      (at level 95, no associativity)
                      : type_scope.

Theorem iff_implies : forall P Q : Prop,
  (P <-> Q) -> P -> Q.
Proof.
  intros P Q H.
  inversion H as [HAB HBA]. apply HAB.  Qed.

Theorem iff_sym : forall P Q : Prop,
  (P <-> Q) -> (Q <-> P).
Proof.
  (* WORKED IN CLASS *)
  intros P Q H.
  inversion H as [HAB HBA].
  split.
    Case "->". apply HBA.
    Case "<-". apply HAB.  Qed.

(** **** Exercise: 1 star, optional (iff_properties) *)
(** Using the above proof that [<->] is symmetric ([iff_sym]) as
    a guide, prove that it is also reflexive and transitive. *)

Theorem iff_refl : forall P : Prop,
  P <-> P.
Proof.
  intros P.
  split.
  Case "->". intro H_P. apply H_P.
  Case "<-". intro H_P. apply H_P.
Qed.

Theorem iff_trans : forall P Q R : Prop,
  (P <-> Q) -> (Q <-> R) -> (P <-> R).
Proof.
  intros P Q R H_P_iff_Q H_Q_iff_R.
  inversion H_P_iff_Q as [ H_P__Q H_Q__P ].
  inversion H_Q_iff_R as [ H_Q__R H_R__Q ].
  split.

  Case "->".
  intro H_P.
  apply H_Q__R.
  apply H_P__Q.
  apply H_P.

  Case "<-".
  intro H_R.
  apply H_Q__P.
  apply H_R__Q.
  apply H_R.
Qed.

(** Hint: If you have an iff hypothesis in the context, you can use
    [inversion] to break it into two separate implications.  (Think
    about why this works.) *)
(** [] *)

(** **** Exercise: 2 stars, advanced (beautiful_iff_gorgeous) *)

(** We have seen that the families of propositions [beautiful] and
    [gorgeous] actually characterize the same set of numbers.
    Prove that [beautiful n <-> gorgeous n] for all [n].  Just for
    fun, write your proof as an explicit proof object, rather than
    using tactics. (_Hint_: if you make use of previously defined
    theorems, you should only need a single line!) *)

Definition beautiful_iff_gorgeous :
  forall n, beautiful n <-> gorgeous n :=
fun n => conj (beautiful n -> gorgeous n) (gorgeous n -> beautiful n)
(beautiful__gorgeous n) (gorgeous__beautiful n).

(** Some of Coq's tactics treat [iff] statements specially, thus
    avoiding the need for some low-level manipulation when reasoning
    with them.  In particular, [rewrite] can be used with [iff]
    statements, not just equalities. *)

(* ############################################################ *)
(** * Disjunction *)

(** Disjunction ("logical or") can also be defined as an
    inductive proposition. *)

Inductive or (P Q : Prop) : Prop :=
  | or_introl : P -> or P Q
  | or_intror : Q -> or P Q.

Notation "P \/ Q" := (or P Q) : type_scope.

(** Consider the "type" of the constructor [or_introl]: *)

Check or_introl.
(* ===>  forall P Q : Prop, P -> P \/ Q *)

(** It takes 3 inputs, namely the propositions [P], [Q] and
    evidence of [P], and returns, as output, the evidence of [P \/ Q].
    Next, look at the type of [or_intror]: *)

Check or_intror.
(* ===>  forall P Q : Prop, Q -> P \/ Q *)

(** It is like [or_introl] but it requires evidence of [Q]
    instead of evidence of [P]. *)

(** Intuitively, there are two ways of giving evidence for [P \/ Q]:

    - give evidence for [P] (and say that it is [P] you are giving
      evidence for -- this is the function of the [or_introl]
      constructor), or

    - give evidence for [Q], tagged with the [or_intror]
      constructor. *)

(** Since [P \/ Q] has two constructors, doing [inversion] on a
    hypothesis of type [P \/ Q] yields two subgoals. *)

Theorem or_commut : forall P Q : Prop,
  P \/ Q  -> Q \/ P.
Proof.
  intros P Q H.
  inversion H as [HP | HQ].
    Case "left". apply or_intror. apply HP.
    Case "right". apply or_introl. apply HQ.  Qed.

(** From here on, we'll use the shorthand tactics [left] and [right]
    in place of [apply or_introl] and [apply or_intror]. *)

Theorem or_commut' : forall P Q : Prop,
  P \/ Q  -> Q \/ P.
Proof.
  intros P Q H.
  inversion H as [HP | HQ].
    Case "left". right. apply HP.
    Case "right". left. apply HQ.  Qed.

(** **** Exercise: 2 stars, optional (or_commut'') *)
(** Try to write down an explicit proof object for [or_commut] (without
    using [Print] to peek at the ones we already defined!). *)

Definition or_commut'' : forall P Q, P \/ Q -> Q \/ P :=
  fun P Q H =>
  match H with
    | or_introl EP => or_intror Q P EP
    | or_intror EQ => or_introl Q P EQ
  end.

Theorem or_distributes_over_and_1 : forall P Q R : Prop,
  P \/ (Q /\ R) -> (P \/ Q) /\ (P \/ R).
Proof.
  intros P Q R. intros H. inversion H as [HP | [HQ HR]].
    Case "left". split.
      SCase "left". left. apply HP.
      SCase "right". left. apply HP.
    Case "right". split.
      SCase "left". right. apply HQ.
      SCase "right". right. apply HR.  Qed.

(** **** Exercise: 2 stars (or_distributes_over_and_2) *)
Theorem or_distributes_over_and_2 : forall P Q R : Prop,
  (P \/ Q) /\ (P \/ R) -> P \/ (Q /\ R).
Proof.
  intros P Q R.
  intro H.
  inversion H as [[HP | HQ] [HP' | HR]].
  left. Case "left". apply HP.
  left. Case "left". apply HP.
  left. Case "left". apply HP'.
  right. Case "left". split.
  SCase "left". apply HQ.
  SCase "right". apply HR.
Qed.

(** **** Exercise: 1 star, optional (or_distributes_over_and) *)
Theorem or_distributes_over_and : forall P Q R : Prop,
  P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).
Proof.
  intros P Q R.
  split.
  apply or_distributes_over_and_1.
  apply or_distributes_over_and_2.
Qed.

(* ################################################### *)
(** ** Relating [/\] and [\/] with [andb] and [orb] (advanced) *)

(** We've already seen several places where analogous structures
    can be found in Coq's computational ([Type]) and logical ([Prop])
    worlds.  Here is one more: the boolean operators [andb] and [orb]
    are clearly analogs of the logical connectives [/\] and [\/].
    This analogy can be made more precise by the following theorems,
    which show how to translate knowledge about [andb] and [orb]'s
    behaviors on certain inputs into propositional facts about those
    inputs. *)

Theorem andb_true__and : forall b c,
  andb b c = true -> b = true /\ c = true.
Proof.
  (* WORKED IN CLASS *)
  intros b c H.
  destruct b.
    Case "b = true". destruct c.
      SCase "c = true". apply conj. reflexivity. reflexivity.
      SCase "c = false". inversion H.
    Case "b = false". inversion H.  Qed.

Theorem and__andb_true : forall b c,
  b = true /\ c = true -> andb b c = true.
Proof.
  (* WORKED IN CLASS *)
  intros b c H.
  inversion H.
  rewrite H0. rewrite H1. reflexivity. Qed.

(** **** Exercise: 2 stars, optional (bool_prop) *)
Theorem andb_false : forall b c,
  andb b c = false -> b = false \/ c = false.
Proof.
  intros b c.
  destruct b.
  Case "b = true".
  destruct c.
  SCase "c = true".
  intro H.
  inversion H.
  SCase "c = false".
  intro H.
  right.
  reflexivity.
  Case "b = false".
  intro H.
  left.
  reflexivity.
Qed.

Theorem orb_true : forall b c,
  orb b c = true -> b = true \/ c = true.
Proof.
  intros b c.
  destruct b.
  Case "b = true".
  intro H.
  left.
  reflexivity.
  Case "b = false".
  intro H.
  right.
  rewrite <- H.
  unfold orb.
  reflexivity.
Qed.

Theorem orb_false : forall b c,
  orb b c = false -> b = false /\ c = false.
Proof.
  intros b c.
  destruct b.
  Case "b = true".
  intro H.
  inversion H.
  Case "b = false".
  intro H.
  split.
  reflexivity.
  rewrite <- H.
  unfold orb.
  reflexivity.
Qed.

(* ################################################### *)
(** * Falsehood *)

(** Logical falsehood can be represented in Coq as an inductively
    defined proposition with no constructors. *)

Inductive False : Prop := .

(** Intuition: [False] is a proposition for which there is no way
    to give evidence. *)

(** Since [False] has no constructors, inverting an assumption
    of type [False] always yields zero subgoals, allowing us to
    immediately prove any goal. *)

Theorem False_implies_nonsense :
  False -> 2 + 2 = 5.
Proof.
  intros contra.
  inversion contra.  Qed.

(** How does this work? The [inversion] tactic breaks [contra] into
    each of its possible cases, and yields a subgoal for each case.
    As [contra] is evidence for [False], it has _no_ possible cases,
    hence, there are no possible subgoals and the proof is done. *)

(** Conversely, the only way to prove [False] is if there is already
    something nonsensical or contradictory in the context: *)

Theorem nonsense_implies_False :
  2 + 2 = 5 -> False.
Proof.
  intros contra.
  inversion contra.  Qed.

(** Actually, since the proof of [False_implies_nonsense]
    doesn't actually have anything to do with the specific nonsensical
    thing being proved; it can easily be generalized to work for an
    arbitrary [P]: *)

Theorem ex_falso_quodlibet : forall (P:Prop),
  False -> P.
Proof.
  (* WORKED IN CLASS *)
  intros P contra.
  inversion contra.  Qed.

(** The Latin _ex falso quodlibet_ means, literally, "from
    falsehood follows whatever you please."  This theorem is also
    known as the _principle of explosion_. *)

(* #################################################### *)
(** ** Truth *)

(** Since we have defined falsehood in Coq, one might wonder whether
    it is possible to define truth in the same way.  We can. *)

(** **** Exercise: 2 stars, advanced (True) *)
(** Define [True] as another inductively defined proposition.  (The
    intution is that [True] should be a proposition for which it is
    trivial to give evidence.) *)

Inductive True : Prop := T : True.

(** However, unlike [False], which we'll use extensively, [True] is
    just a theoretical curiosity: it is trivial (and therefore
    uninteresting) to prove as a goal, and it carries no useful
    information as a hypothesis. *)

(* #################################################### *)
(** * Negation *)

(** The logical complement of a proposition [P] is written [not
    P] or, for shorthand, [~P]: *)

Definition not (P:Prop) := P -> False.

(** The intuition is that, if [P] is not true, then anything at
    all (even [False]) follows from assuming [P]. *)

Notation "~ x" := (not x) : type_scope.

Check not.
(* ===> Prop -> Prop *)

(** It takes a little practice to get used to working with
    negation in Coq.  Even though you can see perfectly well why
    something is true, it can be a little hard at first to get things
    into the right configuration so that Coq can see it!  Here are
    proofs of a few familiar facts about negation to get you warmed
    up. *)

Theorem not_False :
  ~ False.
Proof.
  unfold not. intros H. inversion H.  Qed.

Theorem contradiction_implies_anything : forall P Q : Prop,
  (P /\ ~P) -> Q.
Proof.
  (* WORKED IN CLASS *)
  intros P Q H. inversion H as [HP HNA]. unfold not in HNA.
  apply HNA in HP. inversion HP.  Qed.

Theorem double_neg : forall P : Prop,
  P -> ~~P.
Proof.
  (* WORKED IN CLASS *)
  intros P H. unfold not. intros G. apply G. apply H.  Qed.

(** **** Exercise: 2 stars, advanced (double_neg_inf) *)
(** Write an informal proof of [double_neg]:

   _Theorem_: [P] implies [~~P], for any proposition [P].

   _Proof_:
(* FILL IN HERE *)
   [Introduce P and H; unfold the definition
of not to a nested expression. Then we can get G and apply G to the original proposition
. Lastly, apply the hypothesis to prove it ]
*)

(** **** Exercise: 2 stars (contrapositive) *)
Theorem contrapositive : forall P Q : Prop,
  (P -> Q) -> (~Q -> ~P).
Proof.
  intros P Q H_P__Q.
  unfold not.
  intros H_not_Q H_P.
  apply H_not_Q.
  apply H_P__Q.
  apply H_P.
Qed.

(** **** Exercise: 1 star (not_both_true_and_false) *)
Theorem not_both_true_and_false : forall P : Prop,
  ~ (P /\ ~P).
Proof.
  intros P.
  unfold not.
  intros [ HP H_not_P ].
  apply H_not_P.
  apply HP.
Qed.

(** **** Exercise: 1 star, advanced (informal_not_PNP) *)
(** Write an informal proof (in English) of the proposition [forall P
    : Prop, ~(P /\ ~P)]. *)

(* Introduct P first and then unfold not using its definition. Then
introduct hypotheises HP and H_not_P. Apply both sequentially to get proof*)

Theorem five_not_even :
  ~ ev 5.
Proof.
  (* WORKED IN CLASS *)
  unfold not. intros Hev5. inversion Hev5 as [|n Hev3 Heqn].
  inversion Hev3 as [|n' Hev1 Heqn']. inversion Hev1.  Qed.

(** **** Exercise: 1 star (ev_not_ev_S) *)
(** Theorem [five_not_even] confirms the unsurprising fact that five
    is not an even number.  Prove this more interesting fact: *)

Theorem ev_not_ev_S : forall n,
  ev n -> ~ ev (S n).
Proof.
  unfold not. intros n H. induction H. (* not n! *)

  Case "~ ev 1".
  intro contra.
  inversion contra.
  Case "~ ev (S (S (S n)))".
  intro H_SSSn.
  apply IHev.
  inversion H_SSSn.
  apply H1.
Qed.

(** Note that some theorems that are true in classical logic are _not_
    provable in Coq's (constructive) logic.  E.g., let's look at how
    this proof gets stuck... *)

Theorem classic_double_neg : forall P : Prop,
  ~~P -> P.
Proof.
  (* WORKED IN CLASS *)
  intros P H. unfold not in H.
  (* But now what? There is no way to "invent" evidence for [~P]
     from evidence for [P]. *)
  Admitted.

(** **** Exercise: 5 stars, advanced, optional (classical_axioms) *)
(** For those who like a challenge, here is an exercise
    taken from the Coq'Art book (p. 123).  The following five
    statements are often considered as characterizations of
    classical logic (as opposed to constructive logic, which is
    what is "built in" to Coq).  We can't prove them in Coq, but
    we can consistently add any one of them as an unproven axiom
    if we wish to work in classical logic.  Prove that these five
    propositions are equivalent. *)

Definition peirce := forall P Q: Prop,
  ((P->Q)->P)->P.
Definition classic := forall P:Prop,
  ~~P -> P.
Definition excluded_middle := forall P:Prop,
  P \/ ~P.
Definition de_morgan_not_and_not := forall P Q:Prop,
  ~(~P/\~Q) -> P\/Q.
Definition implies_to_or := forall P Q:Prop,
  (P->Q) -> (~P\/Q).

(* FILL IN HERE *)
(** [] *)

(* ########################################################## *)
(** ** Inequality *)

(** Saying [x <> y] is just the same as saying [~(x = y)]. *)

Notation "x <> y" := (~ (x = y)) : type_scope.

(** Since inequality involves a negation, it again requires
    a little practice to be able to work with it fluently.  Here
    is one very useful trick.  If you are trying to prove a goal
    that is nonsensical (e.g., the goal state is [false = true]),
    apply the lemma [ex_falso_quodlibet] to change the goal to
    [False].  This makes it easier to use assumptions of the form
    [~P] that are available in the context -- in particular,
    assumptions of the form [x<>y]. *)

Theorem not_false_then_true : forall b : bool,
  b <> false -> b = true.
Proof.
  intros b H. destruct b.
  Case "b = true". reflexivity.
  Case "b = false".
    unfold not in H.
    apply ex_falso_quodlibet.
    apply H. reflexivity.   Qed.

(** **** Exercise: 2 stars (not_eq_beq_false) *)
Theorem not_eq_beq_false : forall n n' : nat,
     n <> n' ->
     beq_nat n n' = false.
Proof.
  induction n.
  destruct n'.
  intros H.
  unfold not in H.
  apply ex_falso_quodlibet.
  apply H.
  reflexivity.
  intros H.
  simpl.
  reflexivity.
  destruct n'.
  intros H.
  simpl.
  reflexivity.
  intros H.
  simpl.
  apply IHn.
  unfold not.
  unfold not in H.
  intros H2.
  apply H.
  rewrite H2.
  reflexivity.
Qed.

(** **** Exercise: 2 stars, optional (beq_false_not_eq) *)
Theorem beq_false_not_eq : forall n m,
  false = beq_nat n m -> n <> m.
Proof.
  induction n.
  destruct m.
  intros H.
  inversion H.
  simpl.
  intros H.
  unfold not.
  intros H2.
  inversion H2.
  destruct m.
  simpl.
  intros H1.
  unfold not.
  intros H2.
  inversion H2.
  simpl.
  intros H.
  apply IHn in H.
  unfold not in H.
  unfold not.
  intros H2.
  inversion H2.
  apply H.
  rewrite H1.
  reflexivity.
Qed.

(* ############################################################ *)
(** * Existential Quantification *)

(** Another critical logical connective is _existential
    quantification_.  We can express it with the following
    definition: *)

Inductive ex (X:Type) (P : X->Prop) : Prop :=
  ex_intro : forall (witness:X), P witness -> ex X P.

(** That is, [ex] is a family of propositions indexed by a type [X]
    and a property [P] over [X].  In order to give evidence for the
    assertion "there exists an [x] for which the property [P] holds"
    we must actually name a _witness_ -- a specific value [x] -- and
    then give evidence for [P x], i.e., evidence that [x] has the
    property [P].

    For example, consider this existentially quantified proposition: *)

Definition some_nat_is_even : Prop :=
  ex nat ev.

(** To prove this proposition, we need to choose a particular number
    as witness -- say, 4 -- and give some evidence that that number is
    even. *)

Definition snie : some_nat_is_even :=
  ex_intro _ ev 4 (ev_SS 2 (ev_SS 0 ev_0)).

(** Coq's [Notation] facility can be used to introduce more
    familiar notation for writing existentially quantified
    propositions, exactly parallel to the built-in syntax for
    universally quantified propositions.  Instead of writing [ex nat
    ev] to express the proposition that there exists some number that
    is even, for example, we can write [exists x:nat, ev x].  (It is
    not necessary to understand exactly how the [Notation] definition
    works.) *)

Notation "'exists' x , p" := (ex _ (fun x => p))
  (at level 200, x ident, right associativity) : type_scope.
Notation "'exists' x : X , p" := (ex _ (fun x:X => p))
  (at level 200, x ident, right associativity) : type_scope.

(** We can use the usual set of tactics for
    manipulating existentials.  For example, to prove an
    existential, we can [apply] the constructor [ex_intro].  Since the
    premise of [ex_intro] involves a variable ([witness]) that does
    not appear in its conclusion, we need to explicitly give its value
    when we use [apply]. *)

Example exists_example_1 : exists n, n + (n * n) = 6.
Proof.
  apply ex_intro with (witness:=2).
  reflexivity.  Qed.

(** Note, again, that we have to explicitly give the witness. *)

(** Or, instead of writing [apply ex_intro with (witness:=e)] all the
    time, we can use the convenient shorthand [exists e], which means
    the same thing. *)

Example exists_example_1' : exists n, n + (n * n) = 6.
Proof.
  exists 2.
  reflexivity.  Qed.

(** Conversely, if we have an existential hypothesis in the
    context, we can eliminate it with [inversion].  Note the use
    of the [as...] pattern to name the variable that Coq
    introduces to name the witness value and get evidence that
    the hypothesis holds for the witness.  (If we don't
    explicitly choose one, Coq will just call it [witness], which
    makes proofs confusing.) *)

Theorem exists_example_2 : forall n,
  (exists m, n = 4 + m) ->
  (exists o, n = 2 + o).
Proof.
  intros n H.
  inversion H as [m Hm].
  exists (2 + m).
  apply Hm.  Qed.

(** **** Exercise: 1 star, optional (english_exists) *)
(** In English, what does the proposition
      ex nat (fun n => beautiful (S n))
]]
    mean? *)

(* For natural number, (fun n => beautiful (S n) holds  *)

(** Complete the definition of the following proof object: *)

Definition p : ex nat (fun n => beautiful (S n)) :=
ex_intro _ (fun n => beautiful (S n)) 2 b_3.

(** **** Exercise: 1 star (dist_not_exists) *)
(** Prove that "[P] holds for all [x]" and "there is no [x] for
    which [P] does not hold" are equivalent assertions. *)

Theorem dist_not_exists : forall (X:Type) (P : X -> Prop),
  (forall x, P x) -> ~ (exists x, ~ P x).
Proof.
  intros X P H.
  unfold not.
  intros H1.
  inversion H1.
  apply H0. apply H.
Qed.

(** **** Exercise: 3 stars, optional (not_exists_dist) *)
(** (The other direction of this theorem requires the classical "law
    of the excluded middle".) *)

Theorem not_exists_dist :
  excluded_middle ->
  forall (X:Type) (P : X -> Prop),
    ~ (exists x, ~ P x) -> (forall x, P x).
Proof.
  (* FILL IN HERE *) Admitted.
(** [] *)

(** **** Exercise: 2 stars (dist_exists_or) *)
(** Prove that existential quantification distributes over
    disjunction. *)

Theorem dist_exists_or : forall (X:Type) (P Q : X -> Prop),
  (exists x, P x \/ Q x) <-> (exists x, P x) \/ (exists x, Q x).
Proof.
  intros X P Q.
  unfold iff.
  split.
  intros H.
  inversion H.
  inversion H0.
  left.
  exists witness.
  apply H1.

  right.
  exists witness.
  apply H1.

  intros H.
  inversion H.
  inversion H0.
  exists witness.
  left.
  apply H1.

  inversion H0.
  exists witness.
  right.
  apply H1.
Qed.
(* Print dist_exists_or. *)

(* ###################################################### *)
(** * Equality *)

(** Even Coq's equality relation is not built in.  It has (roughly)
    the following inductive definition. *)

(* (We enclose the definition in a module to avoid confusion with the
    standard library equality, which we have used extensively
    already.) *)

Module MyEquality.

Inductive eq (X:Type) : X -> X -> Prop :=
  refl_equal : forall x, eq X x x.

(** Standard infix notation: *)

Notation "x = y" := (eq _ x y)
                    (at level 70, no associativity)
                    : type_scope.

(** The definition of [=] is a bit subtle.  The way to think about it
    is that, given a set [X], it defines a _family_ of propositions
    "[x] is equal to [y]," indexed by pairs of values ([x] and [y])
    from [X].  There is just one way of constructing evidence for
    members of this family: applying the constructor [refl_equal] to a
    type [X] and a value [x : X] yields evidence that [x] is equal to
    [x]. *)

(** **** Exercise: 2 stars (leibniz_equality) *)
(** The inductive definitions of equality corresponds to _Leibniz equality_:
   what we mean when we say "[x] and [y] are equal" is that every
   property on [P] that is true of [x] is also true of [y].  *)

Lemma leibniz_equality : forall (X : Type) (x y: X),
 x = y -> forall P : X -> Prop, P x -> P y.
Proof.
intros X x y H P.
inversion H.
trivial.
Qed.

(** We can use
    [refl_equal] to construct evidence that, for example, [2 = 2].
    Can we also use it to construct evidence that [1 + 1 = 2]?  Yes:
    indeed, it is the very same piece of evidence!  The reason is that
    Coq treats as "the same" any two terms that are _convertible_
    according to a simple set of computation rules.  These rules,
    which are similar to those used by [Eval simpl], include
    evaluation of function application, inlining of definitions, and
    simplification of [match]es.

    In tactic-based proofs of equality, the conversion rules are
    normally hidden in uses of [simpl] (either explicit or implicit in
    other tactics such as [reflexivity]).  But you can see them
    directly at work in the following explicit proof objects: *)

Definition four : 2 + 2 = 1 + 3 :=
  refl_equal nat 4.

Definition singleton : forall (X:Set) (x:X), []++[x] = x::[]  :=
  fun (X:Set) (x:X) => refl_equal (list X) [x].

End MyEquality.

(* ####################################################### *)
(** ** Inversion, Again (Advanced) *)

(** We've seen [inversion] used with both equality hypotheses and
    hypotheses about inductively defined propositions.  Now that we've
    seen that these are actually the same thing, we're in a position
    to take a closer look at how [inversion] behaves...

    In general, the [inversion] tactic

    - takes a hypothesis [H] whose type [P] is inductively defined,
      and

    - for each constructor [C] in [P]'s definition,

      - generates a new subgoal in which we assume [H] was
        built with [C],

      - adds the arguments (premises) of [C] to the context of
        the subgoal as extra hypotheses,

      - matches the conclusion (result type) of [C] against the
        current goal and calculates a set of equalities that must
        hold in order for [C] to be applicable,

      - adds these equalities to the context (and, for convenience,
        rewrites them in the goal), and

      - if the equalities are not satisfiable (e.g., they involve
        things like [S n = O]), immediately solves the subgoal. *)

(** _Example_: If we invert a hypothesis built with [or], there are two
   constructors, so two subgoals get generated.  The
   conclusion (result type) of the constructor ([P \/ Q]) doesn't
   place any restrictions on the form of [P] or [Q], so we don't get
   any extra equalities in the context of the subgoal.

   _Example_: If we invert a hypothesis built with [and], there is
   only one constructor, so only one subgoal gets generated.  Again,
   the conclusion (result type) of the constructor ([P /\ Q]) doesn't
   place any restrictions on the form of [P] or [Q], so we don't get
   any extra equalities in the context of the subgoal.  The
   constructor does have two arguments, though, and these can be seen
   in the context in the subgoal.

   _Example_: If we invert a hypothesis built with [eq], there is
   again only one constructor, so only one subgoal gets generated.
   Now, though, the form of the [refl_equal] constructor does give us
   some extra information: it tells us that the two arguments to [eq]
   must be the same!  The [inversion] tactic adds this fact to the
   context.  *)

(** **** Exercise: 1 star, optional (dist_and_or_eq_implies_and) *)
Lemma dist_and_or_eq_implies_and : forall P Q R,
P /\ (Q \/ R) /\ Q = R -> P/\Q.
Proof.
 intros P Q R H.
  inversion H.
  split.
  apply H0.
  inversion H1.
  inversion H2.
  apply H4.
  rewrite H3.
  apply H4.
Qed.

(* ########################################################### *)
(** * Quantification and Implication *)

(** In fact, the built-in logic is even smaller than it appears, since
    [->] and [forall] are actually the _same_ primitive!

    The [->] notation is actually just a shorthand for a degenerate
    use of [forall]. *)

(** For example, consider this proposition: *)

Definition funny_prop1 :=
  forall n, forall (E : beautiful n), beautiful (n+3).

(** A proof term inhabiting this proposition would be a function
    with two arguments: a number [n] and some evidence [E] that [n] is
    beautiful.  But the name [E] for this evidence is not used in the
    rest of the statement of [funny_prop1], so it's a bit silly to
    bother making up a name for it.  We could write it like this
    instead, using the dummy identifier [_] in place of a real
    name: *)

Definition funny_prop1' :=
  forall n, forall (_ : beautiful n), beautiful (n+3).

(** Or, equivalently, we can write it in more familiar notation: *)

Definition funny_prop1'' :=
  forall n, beautiful n -> beautiful (n+3).

(** In general, "[P -> Q]" is just syntactic sugar for
    "[forall (_:P), Q]". *)

(* ####################################################### *)
(** * Relations *)

(** A proposition parameterized by a number (such as [ev] or
    [beautiful]) can be thought of as a _property_ -- i.e., it defines
    a subset of [nat], namely those numbers for which the proposition
    is provable.  In the same way, a two-argument proposition can be
    thought of as a _relation_ -- i.e., it defines a set of pairs for
    which the proposition is provable. *)

Module LeModule.

(** We've seen an inductive definition of one fundamental relation:
    equality.  Another useful one is the "less than or equal to"
    relation on numbers: *)

(** The following definition should be fairly intuitive.  It
    says that there are two ways to give evidence that one number is
    less than or equal to another: either observe that they are the
    same number, or give evidence that the first is less than or equal
    to the predecessor of the second. *)

Inductive le : nat -> nat -> Prop :=
  | le_n : forall n, le n n
  | le_S : forall n m, (le n m) -> (le n (S m)).

Notation "m <= n" := (le m n).

(** Proofs of facts about [<=] using the constructors [le_n] and
    [le_S] follow the same patterns as proofs about properties, like
    [ev] in chapter [Prop].  We can [apply] the constructors to prove [<=]
    goals (e.g., to show that [3<=3] or [3<=6]), and we can use
    tactics like [inversion] to extract information from [<=]
    hypotheses in the context (e.g., to prove that [~(2 <= 1)].) *)

(** Here are some sanity checks on the definition.  (Notice that,
    although these are the same kind of simple "unit tests" as we gave
    for the testing functions we wrote in the first few lectures, we
    must construct their proofs explicitly -- [simpl] and
    [reflexivity] don't do the job, because the proofs aren't just a
    matter of simplifying computations.) *)

Theorem test_le1 :
  3 <= 3.
Proof.
  (* WORKED IN CLASS *)
  apply le_n.  Qed.

Theorem test_le2 :
  3 <= 6.
Proof.
  (* WORKED IN CLASS *)
  apply le_S. apply le_S. apply le_S. apply le_n.  Qed.

Theorem test_le3 :
  ~ (2 <= 1).
Proof.
  (* WORKED IN CLASS *)
  intros H. inversion H. inversion H2.  Qed.

(** The "strictly less than" relation [n < m] can now be defined
    in terms of [le]. *)

End LeModule.

Definition lt (n m:nat) := le (S n) m.

Notation "m < n" := (lt m n).

(** Here are a few more simple relations on numbers: *)

Inductive square_of : nat -> nat -> Prop :=
  sq : forall n:nat, square_of n (n * n).

Inductive next_nat (n:nat) : nat -> Prop :=
  | nn : next_nat n (S n).

Inductive next_even (n:nat) : nat -> Prop :=
  | ne_1 : ev (S n) -> next_even n (S n)
  | ne_2 : ev (S (S n)) -> next_even n (S (S n)).

(** **** Exercise: 2 stars (total_relation) *)
(** Define an inductive binary relation [total_relation] that holds
    between every pair of natural numbers. *)

Inductive total_relation : nat -> nat -> Prop :=
	total_relation1 : forall (n m:nat), total_relation n m.

(** **** Exercise: 2 stars (empty_relation) *)
(** Define an inductive binary relation [empty_relation] (on numbers)
    that never holds. *)

Inductive empty_relation : nat -> nat -> Prop :=
empty_relation1 : forall n m : nat, False -> empty_relation n m.

(** **** Exercise: 3 stars (R_provability) *)
Module R.
(** We can define three-place relations, four-place relations,
    etc., in just the same way as binary relations.  For example,
    consider the following three-place relation on numbers: *)

Inductive R : nat -> nat -> nat -> Prop :=
   | c1 : R 0 0 0
   | c2 : forall m n o, R m n o -> R (S m) n (S o)
   | c3 : forall m n o, R m n o -> R m (S n) (S o)
   | c4 : forall m n o, R (S m) (S n) (S (S o)) -> R m n o
   | c5 : forall m n o, R m n o -> R n m o.

(** - Which of the following propositions are provable?
      - [R 1 1 2]
      - [R 2 2 6]

    - If we dropped constructor [c5] from the definition of [R],
      would the set of provable propositions change?  Briefly (1
      sentence) explain your answer.

    - If we dropped constructor [c4] from the definition of [R],
      would the set of provable propositions change?  Briefly (1
      sentence) explain your answer.

(* FILL IN HERE *)
[]
*)

(** **** Exercise: 3 stars, optional (R_fact) *)
(** State and prove an equivalent characterization of the relation
    [R].  That is, if [R m n o] is true, what can we say about [m],
    [n], and [o], and vice versa?
*)

(* FILL IN HERE *)
(** [] *)

End R.

(* ####################################################### *)
(** * Additional Exercises *)

(** **** Exercise: 3 stars (all_forallb) *)
(** Inductively define a property [all] of lists, parameterized by a
    type [X] and a property [P : X -> Prop], such that [all X P l]
    asserts that [P] is true for every element of the list [l]. *)

Inductive all (X : Type) (P : X -> Prop) : list X -> Prop :=
  (* FILL IN HERE *)
.

(** Recall the function [forallb], from the exercise
    [forall_exists_challenge] in chapter [Poly]: *)

Fixpoint forallb {X : Type} (test : X -> bool) (l : list X) : bool :=
  match l with
    | [] => true
    | x :: l' => andb (test x) (forallb test l')
  end.

(** Using the property [all], write down a specification for [forallb],
    and prove that it satisfies the specification. Try to make your
    specification as precise as possible.

    Are there any important properties of the function [forallb] which
    are not captured by your specification? *)

(* FILL IN HERE *)
(** [] *)

(** **** Exercise: 4 stars, advanced (filter_challenge) *)
(** One of the main purposes of Coq is to prove that programs match
    their specifications.  To this end, let's prove that our
    definition of [filter] matches a specification.  Here is the
    specification, written out informally in English.

    Suppose we have a set [X], a function [test: X->bool], and a list
    [l] of type [list X].  Suppose further that [l] is an "in-order
    merge" of two lists, [l1] and [l2], such that every item in [l1]
    satisfies [test] and no item in [l2] satisfies test.  Then [filter
    test l = l1].

    A list [l] is an "in-order merge" of [l1] and [l2] if it contains
    all the same elements as [l1] and [l2], in the same order as [l1]
    and [l2], but possibly interleaved.  For example,
    [1,4,6,2,3]
    is an in-order merge of
    [1,6,2]
    and
    [4,3].
    Your job is to translate this specification into a Coq theorem and
    prove it.  (Hint: You'll need to begin by defining what it means
    for one list to be a merge of two others.  Do this with an
    inductive relation, not a [Fixpoint].)  *)

(* FILL IN HERE *)
(** [] *)

(** **** Exercise: 5 stars, advanced, optional (filter_challenge_2) *)
(** A different way to formally characterize the behavior of [filter]
    goes like this: Among all subsequences of [l] with the property
    that [test] evaluates to [true] on all their members, [filter test
    l] is the longest.  Express this claim formally and prove it. *)

(* FILL IN HERE *)
(** [] *)

(** **** Exercise: 4 stars, advanced (no_repeats) *)
(** The following inductively defined proposition... *)

Inductive appears_in {X:Type} (a:X) : list X -> Prop :=
  | ai_here : forall l, appears_in a (a::l)
  | ai_later : forall b l, appears_in a l -> appears_in a (b::l).

(** ...gives us a precise way of saying that a value [a] appears at
    least once as a member of a list [l].

    Here's a pair of warm-ups about [appears_in].
*)

Lemma appears_in_app : forall {X:Type} (xs ys : list X) (x:X),
     appears_in x (xs ++ ys) -> appears_in x xs \/ appears_in x ys.
Proof.
  (* FILL IN HERE *) Admitted.

Lemma app_appears_in : forall {X:Type} (xs ys : list X) (x:X),
     appears_in x xs \/ appears_in x ys -> appears_in x (xs ++ ys).
Proof.
  (* FILL IN HERE *) Admitted.

(** Now use [appears_in] to define a proposition [disjoint X l1 l2],
    which should be provable exactly when [l1] and [l2] are
    lists (with elements of type X) that have no elements in common. *)

(* FILL IN HERE *)

(** Next, use [appears_in] to define an inductive proposition
    [no_repeats X l], which should be provable exactly when [l] is a
    list (with elements of type [X]) where every member is different
    from every other.  For example, [no_repeats nat [1,2,3,4]] and
    [no_repeats bool []] should be provable, while [no_repeats nat
    [1,2,1]] and [no_repeats bool [true,true]] should not be.  *)

(* FILL IN HERE *)

(** Finally, state and prove one or more interesting theorems relating
    [disjoint], [no_repeats] and [++] (list append).  *)

(* FILL IN HERE *)
(** [] *)

(** **** Exercise: 2 stars, optional (le_exercises) *)
(** Here are a number of facts about the [<=] and [<] relations that
    we are going to need later in the course.  The proofs make good
    practice exercises. *)

Theorem O_le_n : forall n,
  0 <= n.
Proof.
  induction n as [| n'] ;
  apply le_n || apply le_S ;
  apply IHn'.
Qed.

Theorem n_le_m__Sn_le_Sm : forall n m,
  n <= m -> S n <= S m.
Proof.
  intros n m.
  destruct n as [| n'].
  Case "n = 0".
  induction m as [| m'].
  SCase "m = 0".
  intros H. apply le_n.
  SCase "m = S m'".
  intros H.
  apply le_S.
  apply IHm'.
  apply O_le_n.
  Case "n = S n'".
  induction m as [| m'].
  intros H.
  SCase "m = 0".
  inversion H.
  SCase "m = S m'".
  intros H.
  inversion H ; subst.
  apply le_n. apply le_S.
  apply IHm'. apply H1.
Qed.

Theorem Sn_le_Sm__n_le_m : forall n m,
  S n <= S m -> n <= m.
Proof.
  intros n m.  generalize dependent n.  induction m.
  Case "m = 0".
    intros n.
    destruct n as [| n'].
    SCase "n = 0".
     intros H. apply le_n.
    SCase "n = S n'".
     intros H. inversion H. inversion H1.
  Case "m = S m'".
    intros n H.
    inversion H ; subst.
    apply le_n.
    apply le_S. apply IHm.
    assumption.
Qed.

Theorem le_plus_l : forall a b,
  a <= a + b.
Proof.
  intros a.
  induction a as [| a'].
  Case "a = 0".
    simpl.
    apply O_le_n.
  Case "a = S a'".
    simpl. intros b.
    apply n_le_m__Sn_le_Sm.
    apply IHa'.
Qed.

Remark O_lt_Sn : forall n, 0 < S n.
Proof.
  intros n.
  unfold lt. apply n_le_m__Sn_le_Sm.
  apply O_le_n.
Qed.

Remark Sn_le_m__n_le_m : forall n m, S n <= m -> n <= m.
Proof.
  induction n as [| n'].
  Case "n = 0".
    intros m. intros H. apply O_le_n.
  Case "n = S n'".
    intros m H.
    destruct m as [| m'].
    SCase "m = 0".
      inversion H.
    SCase "m = S m'".
      apply Sn_le_Sm__n_le_m in H.
      apply IHn' in H.
      apply n_le_m__Sn_le_Sm.
      assumption.
Qed.

Theorem plus_lt : forall n1 n2 m,
  n1 + n2 < m ->
  n1 < m /\ n2 < m.
Proof.
   induction n1 as [| n1'].
  Case "n1 = 0".
    intros n2 m H.
    simpl in *.
    split.
    destruct m as [| m'].
    SCase "m = 0".
      inversion H.
    SCase "m = S m'".
      apply O_lt_Sn.
    assumption.
  Case "n1 = S n1'".
    intros n2 m H.
    destruct m as [| m'].
    SCase "m = 0".
      inversion H.
    SCase "m = S m'".
      split.
      apply Sn_le_Sm__n_le_m in H.
      unfold lt in *.
      apply n_le_m__Sn_le_Sm.
      simpl in H.
      apply IHn1' in H.
      inversion H.
      assumption.
      destruct n2 as [| n2'].
      SSCase "n2 = 0".
        apply n_le_m__Sn_le_Sm.
        apply O_le_n.
      SSCase "n2 = S n2'".
        apply n_le_m__Sn_le_Sm.
        rewrite <- plus_n_Sm in H.
        apply Sn_le_Sm__n_le_m in H.
        simpl in H.
        apply Sn_le_m__n_le_m in H.
        apply IHn1' in H.
        inversion H.
        unfold lt in H1 ; assumption.
Qed.

Theorem lt_S : forall n m,
  n < m ->
  n < S m.
Proof.
  induction n as [| n'].
  Case "n = 0".
    intros m H.
    unfold lt.
    apply n_le_m__Sn_le_Sm.
    apply O_le_n.
  Case "n = S n'".
    intros m H.
    unfold lt in H.
    apply Sn_le_m__n_le_m in H.
    apply n_le_m__Sn_le_Sm.
    assumption.
Qed.

Theorem ble_nat_true : forall n m,
  ble_nat n m = true -> n <= m.
Proof.
  induction n as [| n'].
  Case "n = 0".
    intros m Heq.
    apply O_le_n.
  Case "n = S n'".
    intros m.
    destruct m as [| m'].
    SCase "m = 0".
      simpl. intros H. inversion H.
    SCase "m = S m'".
      simpl. intros H. apply IHn' in H.
      apply n_le_m__Sn_le_Sm ; assumption.
Qed.

Theorem ble_nat_n_Sn_false : forall n m,
  ble_nat n (S m) = false ->
  ble_nat n m = false.
Proof.
  intros n m.
  generalize dependent n.
  induction m as [| m'].
  Case "m = 0".
    intros n H.
    destruct n as [| n'].
    SCase "n = 0".
      simpl. discriminate.
    SCase "n = S n'".
      reflexivity.
  Case "n = S n'".
    intros n.
    destruct n as [| n'].
    SCase "n = 0".
      simpl. trivial.
    SCase "n = S n'".
      simpl.
      intros H.
      apply IHm' in H.
      assumption.
Qed.

Theorem ble_nat_false : forall n m,
  ble_nat n m = false -> ~(n <= m).
Proof.
  induction n as [| n'].
  Case "n = 0".
    intros m.
    destruct m as [| m'].
    SCase "m = 0".
      intros H. inversion H.
    SCase "m = S m'".
      simpl.
      intros H. inversion H.
  Case "n = S n'".
    intros m.
    destruct m as [| m'].
    SCase "m = 0".
      simpl.
      intros H contra. clear H.
      inversion contra.
    SCase "m = S m'".
      simpl.
      intros H. apply IHn' in H.
      intros H1.
      apply Sn_le_Sm__n_le_m in H1.
      contradiction.
Qed.

(** **** Exercise: 3 stars (nostutter) *)
(** Formulating inductive definitions of predicates is an important
    skill you'll need in this course.  Try to solve this exercise
    without any help at all (except from your study group partner, if
    you have one).

    We say that a list of numbers "stutters" if it repeats the same
    number consecutively.  The predicate "[nostutter mylist]" means
    that [mylist] does not stutter.  Formulate an inductive definition
    for [nostutter].  (This is different from the [no_repeats]
    predicate in the exercise above; the sequence [1,4,1] repeats but
    does not stutter.) *)

Inductive nostutter:  list nat -> Prop :=
 (* FILL IN HERE *)
.

(** Make sure each of these tests succeeds, but you are free
    to change the proof if the given one doesn't work for you.
    Your definition might be different from mine and still correct,
    in which case the examples might need a different proof.

    The suggested proofs for the examples (in comments) use a number
    of tactics we haven't talked about, to try to make them robust
    with respect to different possible ways of defining [nostutter].
    You should be able to just uncomment and use them as-is, but if
    you prefer you can also prove each example with more basic
    tactics.  *)

Example test_nostutter_1:      nostutter [3,1,4,1,5,6].
(* FILL IN HERE *) Admitted.
(*
  Proof. repeat constructor; apply beq_false_not_eq; auto. Qed.
*)

Example test_nostutter_2:  nostutter [].
(* FILL IN HERE *) Admitted.
(*
  Proof. repeat constructor; apply beq_false_not_eq; auto. Qed.
*)

Example test_nostutter_3:  nostutter [5].
(* FILL IN HERE *) Admitted.
(*
  Proof. repeat constructor; apply beq_false_not_eq; auto. Qed.
*)

Example test_nostutter_4:      not (nostutter [3,1,1,4]).
(* FILL IN HERE *) Admitted.
(*
  Proof. intro.
  repeat match goal with
    h: nostutter _ |- _ => inversion h; clear h; subst
  end.
  contradiction H1; auto. Qed.
*)
(** [] *)

(** **** Exercise: 4 stars, advanced (pigeonhole principle) *)
(** The "pigeonhole principle" states a basic fact about counting:
   if you distribute more than [n] items into [n] pigeonholes, some
   pigeonhole must contain at least two items.  As is often the case,
   this apparently trivial fact about numbers requires non-trivial
   machinery to prove, but we now have enough... *)

(** First a pair of useful lemmas (we already proved these for lists
    of naturals, but not for arbitrary lists). *)

Lemma app_length : forall {X:Type} (l1 l2 : list X),
  length (l1 ++ l2) = length l1 + length l2.
Proof.
  (* FILL IN HERE *) Admitted.

Lemma appears_in_app_split : forall {X:Type} (x:X) (l:list X),
  appears_in x l ->
  exists l1, exists l2, l = l1 ++ (x::l2).
Proof.
  (* FILL IN HERE *) Admitted.

(** Now define a predicate [repeats] (analogous to [no_repeats] in the
   exercise above), such that [repeats X l] asserts that [l] contains
   at least one repeated element (of type [X]).  *)

Inductive repeats {X:Type} : list X -> Prop :=
  (* FILL IN HERE *)
.

(** Now here's a way to formalize the pigeonhole principle. List [l2]
   represents a list of pigeonhole labels, and list [l1] represents an
   assignment of items to labels: if there are more items than labels,
   at least two items must have the same label.  You will almost
   certainly need to use the [excluded_middle] hypothesis. *)

Theorem pigeonhole_principle: forall {X:Type} (l1 l2:list X),
  excluded_middle ->
  (forall x, appears_in x l1 -> appears_in x l2) ->
  length l2 < length l1 ->
  repeats l1.
Proof.  intros X l1. induction l1.
  (* FILL IN HERE *) Admitted.
(** [] *)

(* $Date: 2013-02-10 18:08:54 -0500 (Sun, 10 Feb 2013) $ *)


Inductive list (A: Type): Type  :=
 |nil: list A
 |cons: A -> list A -> list A
.

(*Inductive bool: Set :=
| true
| false
.

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

Definition n0 := O.
Definition n1 := (S O).
Definition n1' := (S n0).
Definition n2 := (S n1).

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

 Fixpoint length (A: Type) (l: list A): nat :=
match l with
  |nil => O
  |cons h t => S (length A  t)
end.

Definition l0: list nat := nil nat.
Definition l1: list nat := cons nat (S O) (nil nat).
Definition l2:= cons nat (S (S O)) (cons nat (S O) (nil nat)).
Definition l2' := cons nat (S (S O)) l1.

Compute length nat l2.
*)

Arguments nil {A}.
Arguments cons {A} _ _.

Definition l0':= cons 1 nil.
(* 0 - 1 are recognized in Coq libraries *)
Check l0'.

Definition l1':= cons false nil.
Check l1'.

Definition l2'' := @nil bool.
(* Turns off implicit typing *)

Notation "h :: t" := (cons h t)(at level 60, right associativity).

Definition l6 := 5::4::3::2::1::0::nil.

Fixpoint length {A: Type} (l: list A): nat :=
  match l with
    |nil => 0
    |h :: t => S (length t)
end.

Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).

Definition l8 := [5;4;3;2;1;0].

Fixpoint append {A: Type} (l1 l2: list A): list A:=
  match l1 with
  |nil => l2
  |h :: t => h::(append t l2)
end.

Fixpoint snoc {A: Type} (h: A) (t: list A): list A :=
  match t with
    |nil => [h]
    |h'::t'' => append t [h]
end.

(*Fixpoint reverse {A: Type} {l: list A}: list A :=
  match l with
    |nil => nil
    |h :: t => append snoc h (rev t)
  end.

Compute @rev nat [].
*)

Fixpoint reverse {A: Type} (l: list A): list A :=
  match l with
    |nil => nil
    |h :: t => append (reverse t) [h]
  end.


(** * 6.887 Formal Reasoning About Programs - Lab 2
    * Transition Systems and Invariants, including modeling dynamic thread creation *)

Require Import Frap.

Set Implicit Arguments.

(* Authors: Adam Chlipala (adamc@csail.mit.edu), Peng Wang (wangpeng@csail.mit.edu) *)

(* Examples based on ideas introduced in TransitionSystem.v, from the book source.
 * Key definitions about invariants are imported from the Frap library. *)

(** * A simple transition-system example: list search *)

(* Consider a transition system equivalent to this program, which checks whether
 * a number appears in a list:
   lsearch(needle, haystack) {
     while (haystack != null) {
       if (haystack.data == needle) {
         return true;
       } else {
         haystack = haystack.next;
       }
     }
     return false;
   } *)

(* Here's a suitable state type. *)
Inductive lsearch_state :=
| AnswerIs (answer : bool)
  (* The function has returned, with this answer. *)
| Searching (remaining : list nat).
  (* We are right before a loop iteration, with this value of "haystack". *)

(* Initial states: searching the full input list. *)
Inductive lsearch_init (haystack : list nat) : lsearch_state -> Prop :=
| LsearchInit : lsearch_init haystack (Searching haystack).

(* Steps, each corresponding to one loop iteration. *)
Inductive lsearch_step (needle : nat) : lsearch_state -> lsearch_state -> Prop :=
| LsearchNotFound :
  lsearch_step needle (Searching nil) (AnswerIs false)
| LsearchFound : forall ls,
  lsearch_step needle (Searching (needle :: ls)) (AnswerIs true)
| LsearchKeepLooking : forall x ls,
  x <> needle
  -> lsearch_step needle (Searching (x :: ls)) (Searching ls).

(* Putting it together into a full system, parameterized on the two function
 * parameters. *)
Definition lsearch_sys (needle : nat) (haystack : list nat) := {|
  Initial := lsearch_init haystack;
  Step := lsearch_step needle
|}.

(* A basic invariant capturing the final correctness condition. *)
Definition lsearch_correct (needle : nat) (haystack : list nat) (st : lsearch_state) :=
  match st with
  | AnswerIs b => b = true <-> In needle haystack
  | _ => True
  end.
(* Note the use of infix operator [<->] for "if and only if."
 * The [propositional] tactic knows what to do with it!
 * Also see the definition of [In], a predicate we use for list membership. *)
Print In.

(* CHALLENGE #1: Prove that the system meets its spec. *)
Theorem lsearch_ok : forall needle haystack,
  invariantFor (lsearch_sys needle haystack) (lsearch_correct needle haystack).
Proof.
Admitted.

(* Hint: You will almost certainly want to define and use a stronger
 * invariant! *)

(** * Transition systems for dynamic thread creation *)

(* Let's define a general way of building transition systems that allow dynamic
 * thread creation.  We will work with step relations returning values in this
 * type, capturing whether to spawn a new thread. *)
Inductive thread_step_outcome shared private :=
| Basic (sh : shared) (pr : private)
  (* Meaning: shared state is mutated to [sh] and private state to [pr]. *)
| Spawn (sh : shared) (state_of_new_thread my_own_next_state : private).
  (* Meaning: shared state to [sh] and private state to [my_own_next_state].
   * Additionally, spawn a new thread whose private state is
   * [state_of_new_thread]. *)

(* Now system states can include arbitrarily many threads. *)
Record threads_state shared private := {
  Shared : shared;
  (* State shared across all threads *)
  Private : list private
  (* Private states of all threads that have been spawned *)
}.

(* Initial states of multithreaded programs, based on given initial values of
 * shared and private state.  We start with exactly one thread, which has that
 * private state. *)
Inductive threads_init shared private (sh0 : shared) (pr0 : private)
  : threads_state shared private -> Prop :=
| ThreadsInit : threads_init sh0 pr0 {| Shared := sh0; Private := [pr0] |}.

(* CHALLENGE #2: Fill in this definition of a step relation.
 * To get more of a sense for the definitions, it may be helpful to look first
 * at the example system just below.
 * Another hint: you probably want to write rules that pick threads out of lists
 * and then build new modified lists.  A useful pattern for that picking is as
 * follows:
 *   [forall ts1 t ts2, threads_step step (... (ts1 ++ t :: ts2) ...)
 *                                        (... (ts1 ++ t' :: ts2) ...)]
 * That is, we quantify over all threads before and after a given thread [t],
 * in the list of active threads.  Recall that [++] is list concatenation,
 * while [::] is the operator to add a new element to the front of a list.
 *)
Inductive threads_step shared private
          (step : shared -> private -> thread_step_outcome shared private -> Prop)
  : threads_state shared private -> threads_state shared private -> Prop :=
| ThisIsWrong : forall sh pr sh' pr',
    threads_step step {| Shared := sh; Private := pr |}
                      {| Shared := sh'; Private := pr' |}.

(* Package it all together into a system. *)
Definition threads_sys shared private (sh0 : shared) (pr0 : private)
           (step : shared -> private -> thread_step_outcome shared private -> Prop)
  : trsys (threads_state shared private) := {|
  Initial := threads_init sh0 pr0;
  Step := threads_step step
|}.

(* Now our example of a thread-spawning program.  It is for checking whether a
 * number occurs in a binary tree, based on this type definition. *)
Inductive tree :=
| Leaf (n : nat)
| Branch (tr1 tr2 : tree).

(* Here is what it means for a number to be in the tree. *)
Fixpoint InTree (n : nat) (tr : tree) : Prop :=
  match tr with
  | Leaf n' => n = n'
  | Branch tr1 tr2 => InTree n tr1 \/ InTree n tr2
  end.

(* The pseudocode of our algorithm, where we write "spawn(c)" for running
 * command "c" in a new thread:

   bool found = false;

   treesearch(needle, haystack) {
     switch (haystack) {
       case Leaf n:
         if (n == needle) {
           found = true;
         }
         return;
       case Branch tr1 tr2:
         spawn(treesearch(needle, tr1));
         treesearch(needle, tr2);
     }
   }

 * If global variable "found" is ever set to "true", then we know the number
 * has been found in the tree.
*)

(* We only need to model the following two private program states. *)
Inductive treesearch_thread :=
| SearchTree (tr : tree)
  (* Just beginning a call to the function, searching the given tree *)
| Done.
  (* Finished a call to the function *)

(* Step relation, crucially using the [Spawn] outcome in the last rule *)
Inductive treesearch_step (needle : nat) : bool
  -> treesearch_thread
  -> thread_step_outcome bool treesearch_thread
  -> Prop :=
| TsLeafMatch : forall b,
  treesearch_step needle b (SearchTree (Leaf needle)) (Basic true Done)
| TsLeafNoMatch : forall b n,
  n <> needle
  -> treesearch_step needle b (SearchTree (Leaf n)) (Basic b Done)
| TsBranch : forall b tr1 tr2,
  treesearch_step needle b (SearchTree (Branch tr1 tr2))
                  (Spawn b (SearchTree tr1) (SearchTree tr2)).

Definition treesearch_sys (needle : nat) (haystack : tree) :=
  threads_sys false (SearchTree haystack) (treesearch_step needle).

(* Next, we use a helper function to define an overall correctness condition. *)
Fixpoint allDone (ls : list treesearch_thread) : Prop :=
  match ls with
  | nil => True
  | th :: ls' => th = Done /\ allDone ls'
  end.

Definition treesearch_correct (needle : nat) (haystack : tree)
           (st : threads_state bool treesearch_thread) :=
  allDone st.(Private)
  -> (InTree needle haystack <-> st.(Shared) = true).

(* CHALLENGE #3: Prove that the system meets its spec. *)
Theorem treesearch_ok : forall needle haystack,
  invariantFor (treesearch_sys needle haystack) (treesearch_correct needle haystack).
Proof.
Admitted.

(* Hint: you probably want to define another helper function or two, similar to
 * [allDone], to use in your strengthened invariant. *)

(* Some suggested tactics:
 * [left] or [right]: prove a disjunction (using [\/]) by choosing to prove the
 *   left or right disjunct, respectively.
 * [first_order]: simplify with rules of first-order logic.
 *   WARNING: Sometimes this one goes off the rails and runs effectively
 *   forever!
 * [rewrite H1 in H2]: use [H1] to make a rewrite as usual, but within
 *   hypothesis [H2], rather than in the conclusion formula.
 * [apply H1 in H2]: forward-reasoning version of [apply], for when [H1] proves
 *   an implication from [H2].  The effect is to replace [H2] with the
 *   conclusion of the implication.
 * [unfold X in *]: like [unfold X] but applies to the whole goal, not just the
 *   conclusion.
 * [exfalso]: switch to proving [False], from any conclusion.  That is, we
 *   switch into a proof by contradiction. *)