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

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

Eval compute in ( next_weekday friday ).
Eval compute in ( next_weekday ( next_weekday friday )).

Example test_next_weekday :
( next_weekday ( next_weekday saturday )) = tuesday.

Proof. simpl. reflexivity. Qed.

Inductive bool : Type :=
  | true : bool
  | false : bool.

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

Definition andb ( x : bool ) ( y : bool ) : bool :=
  match x with
  | true => y
  | false => false
  end.

Definition orb ( x : bool ) ( y : bool ) : bool :=
  match x with
  | false => y
  | true => true
  end.

Example test_orb1 : ( orb true false ) = true.
Proof. simpl. reflexivity. Qed.

Example test_orb2 : ( orb false false ) = false.
Proof. simpl. reflexivity. Qed.

Definition nandb ( x : bool ) ( y : bool ) : bool :=
  match ( x, y ) with
  | ( true, true ) => false
  | ( _, _ ) => true
  end.

Example test_nandb1: (nandb true false) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb2: (nandb false false) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb3: (nandb false true) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb4: (nandb true true) = false.
Proof. simpl. reflexivity. Qed.

Definition andb3 ( x : bool ) ( y : bool ) ( z : bool ) : bool :=
  match ( x, y, z ) with
  | ( true, true, true ) => true
  | ( _, _, _ ) => false
  end.

Example test_andb31: (andb3 true true true) = true.
Proof. simpl. reflexivity. Qed.
Example test_andb32: (andb3 false true true) = false.
Proof. simpl. reflexivity. Qed.
Example test_andb33: (andb3 true false true) = false.
Proof. simpl. reflexivity. Qed.
Example test_andb34: (andb3 true true false) = false.
Proof. simpl. reflexivity. Qed.

Check true.
Check ( negb false ).
Check andb3.

Definition xorb ( x : bool ) ( y : bool ) : bool :=
  match x, y with
  | true, true => false
  | false, false => false
  | _, _  => true
  end.

Theorem xorb_equal : forall a : bool, xorb a a = false.
Proof.
intros a. destruct a as [ | ].
reflexivity. reflexivity.
Qed.

Theorem xorb_equalleft : forall a b : bool, xorb a b = false -> a = b.
Proof.
intros a b H. destruct a. destruct b.
reflexivity. discriminate.
destruct b. discriminate. reflexivity.
Qed.

(*
Theorem xorb_notequal : forall a b : bool, xorb a b = true -> a <> b.
Proof.
intros a b H.
destruct a. destruct b.
discriminate.
rewrite <- H.
*)

Module Playground1.
Inductive nat : Type :=
 | O : nat
 | S : nat -> nat.

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

End Playground1.

Definition minustwo ( n : nat ) : nat :=
  match n with
  | O => O
  | S O => O
  | S ( S m ) => m
  end.

Check (S (S (S (S O)))).
Eval simpl in ( minustwo 4 ).

Check S.
Check pred.
Check minustwo.

Fixpoint evenb ( n : nat ) : bool :=
  match n with
  | O => true
  | S O => false
  | S ( S n' ) => evenb n'
  end.

Definition oddb ( n : nat ) : bool :=
   negb ( evenb n ).

Example test_oddb1: (oddb ( S ( S (S O ) ) ) ) = true.
Proof. simpl. reflexivity. Qed.

Module Playground2.

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

Eval compute in plus ( S O ) ( S ( S O ) ).

Example plus_test : ( plus 3 4 ) = 7.
Proof. simpl. reflexivity. Qed.

(*
Example plus_comm : forall m n p : nat, plus m ( plus n p ) = plus ( plus m n ) p.
Proof.
intros m n p.
destruct m as [ | ].
reflexivity.
simpl.
Admitted.
*)

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

Eval compute in mult 4 9.

Example test_mult : ( mult 3 3 ) = 9.
Proof. simpl. reflexivity. Qed.

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

End Playground2.

Fixpoint exp ( base power : nat ) : nat :=
  match power with
  | O => S O
  | S p => mult base ( exp base p )
  end.

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

Example test_factorial1: (factorial 3) = 6.
Proof. simpl. reflexivity. Qed.

(*
Notation "x + y" := ( plus x y ) ( at level 50, left associativity) : nat_scope.
Notation "x - y" := ( minus x y ) ( at level 50, left associativity) : nat_scope.
Notation "x * y" := ( mult x y ) ( at level 40, left associativity) : nat_scope.
Notation "x ^ y" := ( exp x y ) ( at level 30, right associativity) : nat_scope.

Eval simpl in 2^2^3.
*)

Check ( 10 + 1 + 2 ).
Check (( 0 + 1 ) + 1 ).

Fixpoint beq_nat ( n m : nat ) : bool :=
  match n with
  | O => match m with
         | O => true
         | S _ => false
         end
  | S n' => match m with
          | O => false
          | S m' => beq_nat n' m'
          end
  end.

Fixpoint ble_nat ( n m : nat ) : bool :=
  match n with
  | O => true
  | S n' => match m with
            | O => false
            | S m' => ble_nat n' m'
            end
  end.

Example test_ble_nat1: (ble_nat 2 2) = true.
Proof. reflexivity. Qed.
Example test_ble_nat2: (ble_nat 2 4) = true.
Proof. reflexivity. Qed.
Example test_ble_nat3: (ble_nat 4 2) = false.
Proof. reflexivity. Qed.

Definition blt_nat ( n m : nat ) : bool :=
  andb ( ble_nat n m ) ( negb ( beq_nat n m ) ).

Example test_blt_nat1 : ( blt_nat 3 3 ) = false.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat2 : ( blt_nat 3 4 ) = true.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat3: (blt_nat 2 2) = false.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat4: (blt_nat 2 4) = true.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat5: (blt_nat 4 2) = false.
Proof. simpl. reflexivity. Qed.

Theorem plus_O_n : forall n : nat, O + n = n.
Proof. intros n. reflexivity. Qed.

Theorem plus_1_n : forall n : nat, 1 + n = S n.
Proof. intros. reflexivity. Qed.

Theorem mult_O_n : forall n : nat, O * n = O.
Proof. intros. reflexivity. Qed.

Theorem plus_id_example : forall m n : nat, m = n -> n + n = m + m.
Proof. intros n m. intros H. rewrite -> H.
reflexivity. Qed.

Theorem plus_id_exercise : forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof. intros n m o. intros H1 H2.
rewrite -> H1. rewrite -> H2.
reflexivity. Qed.

Theorem plus_id_exercise2 : forall n m o : nat,
    n = m -> n + o = m + o.
Proof. intros n m o. intros H. rewrite -> H. reflexivity. Qed.

Theorem mult_O_plus : forall m n : nat, ( O + n ) * m = n * m.
Proof. intros m n. rewrite -> plus_O_n. reflexivity. Qed.

(*

Theorem mult_comm : forall m n : nat, m * n = n * m.
Proof. Admitted.

*)

Theorem mult_S_1 : forall m n : nat, m = S n -> m * ( 1 + n ) = m * m.
Proof. intros m n. intros H. rewrite -> H. reflexivity. Qed.

Theorem plus_1_neq_0_firsttry : forall n : nat, beq_nat (n + 1) 0 = false.
Proof. intros n. destruct n as [ | n']. reflexivity.
reflexivity. Qed.

Theorem negb_involuted : forall b : bool, negb ( negb b ) = b.
Proof. intros b. destruct b. reflexivity.
reflexivity. Qed.

Theorem zero_nbeq_plus_1 : forall n : nat, beq_nat 0 ( n + 1 ) = false.
Proof. intros n. destruct n as [ | n' ]. reflexivity.
reflexivity. Qed.

Theorem identity_fn_applied_twice : forall ( f : bool -> bool ),
          ( forall (x : bool), f x = x ) ->
          forall (b : bool), f ( f b ) = b.
Proof. intros f H b. rewrite -> H. rewrite -> H. reflexivity. Qed.

Theorem negation_fn_applied_twice : forall (f : bool -> bool),
          ( forall (x : bool), f x = negb x ) ->
          forall (b : bool), f (f b) = b.
Proof. intros f H b. rewrite -> H. rewrite -> H. destruct b.
reflexivity. reflexivity. Qed.

Theorem andb_eq_orb_rev : forall (b c : bool), b = c -> (andb b c = orb b c).
Proof. intros b c H. rewrite -> H. destruct c. reflexivity. reflexivity. Qed.

Theorem andb_eq_orb_rev1 : forall (b c : bool), b = c -> (orb b c = orb b c).
Proof. intros b c H. rewrite -> H. destruct c. reflexivity. reflexivity. Qed.

Theorem andb_eq_orb : forall ( a b : bool ), ( andb a b  = orb a b ) -> a = b.
Proof.
intros a b. destruct a as [ false | true ].
simpl. intros H1. rewrite -> H1. reflexivity.
simpl. intros H2. rewrite -> H2. reflexivity.
Qed.

Inductive bin : Type :=
  | O' : bin
  | Twice : bin -> bin
  | DPlusOne : bin -> bin.

Fixpoint inc ( b : bin ) : bin :=
  match b with
  | O' => DPlusOne O'
  | Twice b' => DPlusOne b'
  | DPlusOne b' => Twice ( inc b' )
  end.

Fixpoint bintonat ( b : bin ) : nat :=
  match b with
  | O' => O
  | Twice b' => plus ( bintonat b' ) ( bintonat b')
  | DPlusOne b' => plus 1 ( plus ( bintonat b' ) ( bintonat b') )
  end.

Eval compute in bintonat ( Twice ( DPlusOne ( DPlusOne O' ) )).


(** * Logic: Logic in Coq *)

(* $Date: 2011-06-22 10:06:32 -0400 (Wed, 22 Jun 2011) $ *)

Require Export "Prop".

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

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

(** In fact, [->] and [forall] are the _same_ primitive!  Coq's [->]
    notation is actually just a shorthand for [forall].  The [forall]
    notation is more general, because it allows us to _name_ the
    hypothesis. *)

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

Definition funny_prop1 := forall n, forall (E : ev n), ev (n+4).

(** If we had a proof term inhabiting this proposition, it would be a
    function with two arguments: a number [n] and some evidence that
    [n] is even.  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.  We could write it like this instead: *)

Definition funny_prop1' := forall n, forall (_ : ev n), ev (n+4).

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

Definition funny_prop1'' := forall n, ev n -> ev (n+4).

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

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

(** The logical conjunction of propositions [P] and [Q] is
    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 :
  (ev 0) /\ (ev 4).
Proof.
  apply conj.
  (* Case "left". *) apply ev_0.
  (* Case "right". *) apply ev_SS. apply ev_SS. apply ev_0.  Qed.

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

Print and_example.
(* ===>  conj (ev 0) (ev 4) ev_0 (ev_SS 2 (ev_SS 0 ev_0))
            : ev 0 /\ ev 4 *)

(** Note that the proof is of the form
[[
    conj (ev 0) (ev 4) (...pf of ev 0...) (...pf of ev 4...)
]]
    which is what 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 put this evidence into 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.
  inversion H.
  assumption.
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 [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.
  assumption.
  assumption.
  assumption.
Qed.

(** [] *)

(** **** Exercise: 2 stars, recommended (even_ev) *)
(** Now we can prove the other direction of the equivalence of [even]
   and [ev], which we left hanging in the last chapter.  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.
  (* Hint: Use induction on [n]. *)
  intros.
  induction n.
  split.
  intros.
  apply ev_0.
  intros.
  inversion H.
  split.
  apply IHn.
  intros.
  apply ev_SS.
  apply IHn.
  unfold even.
  inversion H.
  reflexivity.
Qed.

(** [] *)

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

(**Definition conj_fact : forall P Q R, P /\ Q -> Q /\ R -> P /\ R :=
(* FILL IN HERE *) admit.
(** [] *)**)

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

(** The familiar logical "if and only if" 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 (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.
  split.
  intros.
  assumption.
  intros.
  assumption.
Qed.

Theorem iff_trans : forall P Q R : Prop,
  (P <-> Q) -> (Q <-> R) -> (P <-> R).
Proof.
  intros.
  inversion H.
  split.
  inversion H0.
  intros.
  apply H3.
  apply H1.
  assumption.
  intros.
  apply H2.
  inversion H0.
  apply H5.
  assumption.
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 (MyProp_iff_ev) *)
(** We have seen that the families of propositions [MyProp] and [ev]
    actually characterize the same set of numbers (the even ones).
    Prove that [MyProp n <-> ev 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 thoerems,
    you should only need a single line!) *)

(** Definition MyProp_iff_ev : forall n, MyProp n <-> ev n :=
  (* FILL IN HERE *) admit.**)
(** [] *)

(** 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 "right". apply or_intror. apply HP.
    Case "left". 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 "right". right. apply HP.
    Case "left". 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!). *)

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

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, recommended (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.
  destruct H.
  destruct H.
  left.
  assumption.
  destruct H0.
  left.
  assumption.
  right.
  split.
  assumption.
  assumption.
Qed.
(** [] *)

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

(** [] *)

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

(** 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 obviously analogs, in some sense, 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: 1 star (bool_prop) *)
Theorem andb_false : forall b c,
  andb b c = false -> b = false \/ c = false.
Proof.
  intros.
  destruct b.
  destruct c.
  inversion H.
  right.
  reflexivity.
  left.
  reflexivity.
Qed.

Theorem orb_true : forall b c,
  orb b c = true -> b = true \/ c = true.
Proof.
  intros.
  destruct b.
  left.
  reflexivity.
  destruct c.
  right.
  reflexivity.
  inversion H.
Qed.

Theorem orb_false : forall b c,
  orb b c = false -> b = false /\ c = false.
Proof.
  intros.
  destruct b.
  destruct c.
  split.
  inversion H.
  inversion H.
  inversion H.
  split.
  reflexivity.
  destruct c.
  inversion H.
  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. *)

(** **** Exercise: 1 star (False_ind_principle) *)
(** Can you predict the induction principle for falsehood? *)

(* Check False_ind. *)
(** [] *)

(** 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.
  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, we might wonder whether it
    is possible to define truth in the same way.  Naturally, the
    answer is yes. *)

(** **** Exercise: 2 stars (True_induction) *)
(** Define [True] as another inductively defined proposition.  What
    induction principle will Coq generate for your definition?  (The
    intution is that [True] should be a proposition for which it is
    trivial to give evidence.  Alternatively, you may find it easiest
    to start with the induction principle and work backwards to the
    inductive definition.) *)

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

(** 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, recommended (double_neg_inf) *)
(** Write an informal proof of [double_neg]:

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

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

(** **** Exercise: 2 stars, recommended (contrapositive) *)
Theorem contrapositive : forall P Q : Prop,
  (P -> Q) -> (~Q -> ~P).
Proof.
  intros.
  unfold not.
  unfold not in H0.
  intros.
  apply H0.
  apply H.
  assumption.
Qed.
(** [] *)

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

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! *)
  intros.
  inversion H.
  intros.
  apply IHev.
  inversion H0.
  assumption.
Qed.
(** [] *)

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

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

(** Note that some theorems that are true in classical logic
    are _not_ provable in Coq's "built in" constructive logic... *)

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]. *)
  Admitted.

(** **** Exercise: 5 stars, 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, recommended (not_eq_beq_false) *)
Theorem not_eq_beq_false : forall n n' : nat,
     n <> n' ->
     beq_nat n n' = false.
Proof.
  intro n.
  induction n.
  destruct n'.
  intros.
  unfold not in H.
  apply ex_falso_quodlibet.
  apply H.
  reflexivity.
  intros.
  unfold not in H.
  simpl.
  reflexivity.
  intros.
  destruct n'.
  reflexivity.
  simpl.
  apply IHn.
  unfold not.
  intros.
  apply H.
  apply eq_remove_S.
  assumption.
Qed.

Theorem remove_not_S : forall n m, (n = m -> False) = (S n = S m -> False).
Proof.
Admitted.

(** **** Exercise: 2 stars, optional (beq_false_not_eq) *)
Theorem beq_false_not_eq : forall n m,
  false = beq_nat n m -> n <> m.
Proof.
  intros n m Hbeq.
  intro Hnm.
  rewrite Hnm in Hbeq.
  SearchAbout beq_nat.
  rewrite beq_nat_refl in Hbeq.
  inversion Hbeq.
Qed.
 (**
 intro n.
  induction n.
  destruct m.
  intros.
  inversion H.
  simpl in *.
  unfold not.
  intros.
  inversion H0.
  intros.
  destruct m.
  unfold not.
  intros.
  inversion H0.
  simpl in *.
  unfold not.
  intros.
  unfold not in IHn.
  apply IHn.
**)
(** [] *)

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

(** Another critical logical connective is _existential
    quantification_.  We can capture what this means 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 definition 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 same set of tactics as always for
    manipulating existentials.  For example, if to prove an
    existential, we [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 (english_exists) *)
(** In English, what does the proposition
[[
      ex nat (fun n => ev (S n))
]]
    mean? *)

(* FILL IN HERE *)

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

(**Definition p : ex nat (fun n => ev (S n)) :=
(* FILL IN HERE *) admit.**)
(** [] *)

(** **** 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 Hfor.
  unfold not.
  intros.
  inversion H.
  apply H0.
  apply Hfor.
Qed.
(** [] *)

(** **** Exercise: 3 stars, optional (not_exists_dist) *)
(** The other direction 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.
  intros.
  apply classic_double_neg.
  red.
  intros.
  unfold not in H0.
  apply H0.
  exists x.
  unfold not in H1.
  assumption.
Qed.
(** [] *)

(** **** 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.
   split.
   intros.
   destruct H.
   destruct H.
   left.
   exists witness.
   assumption.
   right.
   exists witness.
   assumption.
   intros.
   destruct H.
   destruct H.
   exists witness.
   left.
   assumption.
   inversion H.
   exists witness.
   right.
   assumption.
Qed.
(** [] *)

(* Print dist_exists_or. *)

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

(** Even Coq's equality relation is not built in.  It has 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 (using Coq's type argument synthesis): *)

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

(** This 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]. *)

(** Here is a slightly different definition -- the one that actually
    appears in the Coq standard library. *)

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

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

(** **** Exercise: 3 stars, optional (two_defs_of_eq_coincide) *)
(** Verify that the two definitions of equality are equivalent. *)
(**
Theorem two_defs_of_eq_coincide : forall (X:Type) (x y : X),
  x = y <-> x =' y.
Proof.
  intros.
  split.
  intros.
  inversion H.
  **)
(** [] *)

(** The advantage of the second definition is that the induction
    principle that Coq derives for it is precisely the familiar
    principle of _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]. *)

Check eq'_ind.
(* ===>  forall (X : Type) (x : X) (P : X -> Prop),
             P x -> forall y : X, x =' y -> P y *)

(** One important consideration remains.  Clearly, 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 *)

(** 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 of the subgoal, 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.  *)

(* ####################################################### *)
(** * Relations as Propositions *)

(** A proposition parameterized numbers (such as [ev]) 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 LeFirstTry.

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

(** This 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)).

End LeFirstTry.

(** This is a reasonable definition of the [<=] relation, but we
    can streamline it a little by observing that the left-hand
    argument [n] is the same everywhere in the definition, so we can
    actually make it a "general parameter" to the whole definition,
    rather than an argument to each constructor.  This is similar to
    what we did in our second definition of the [eq] relation,
    above. *)

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

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

(** The second one is better, even though it looks less symmetric.
    Why?  Because it gives us a simpler induction principle.
    (The same was true of our second version of [eq].) *)

Check le_ind.
(* ===>  forall (n : nat) (P : nat -> Prop),
           P n ->
           (forall m : nat, n <= m -> P m -> P (S m)) ->
           forall n0 : nat, n <= n0 -> P n0 *)

(** By contrast, the induction principle that Coq calculates for the
    first definition has a lot of extra quantifiers, which makes it
    messier to work with when proving things by induction.  Here is
    the induction principle for the first [le]: *)

(* le_ind :
     forall P : nat -> nat -> Prop,
     (forall n : nat, P n n) ->
     (forall n m : nat, le n m -> P n m -> P n (S m)) ->
     forall n n0 : nat, le n n0 -> P n n0 *)

(** Proofs of facts about [<=] using the constructors [le_n] and
    [le_S] follow the same patterns as proofs about properties, like
    [ev] in the previous chapter.  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 H1.  Qed.

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

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, recommended (total_relation) *)
(** Define an inductive relation [total_relation] that holds
    between every pair of natural numbers. *)

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

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

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

(** **** Exercise: 3 stars, recommended (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.

(** **** Exercise: 3 stars, recommended (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 :=
  | all_empty : all P []
  | all_n_empty : forall (x : X) (l : list X), P x ->  all P l -> all P (x::l).

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

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, optional (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, 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, optional (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.
  induction xs.
  intros.
  right.
  simpl in *.
  assumption.
  intros.
  destruct ys.
  simpl in *.
  inversion H.
  rewrite <- H1.
  left.
  apply ai_here.
  left.
  apply ai_later.
  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.
  intros.
  destruct H.
  induction ys.
  simpl in *.
  inversion H.
  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 *)
(** [] *)

(* ######################################################### *)
(** ** Digression: More Facts about [<=] and [<] *)

(** Let's pause briefly to record several facts about the [<=]
    and [<] relations that we are going to need later in the
    course.  The proofs make good practice exercises. *)

(** **** Exercise: 2 stars, optional (le_exercises) *)
Theorem O_le_n : forall n,
  0 <= n.
Proof.
  intros.
  induction n.
  apply le_n.
  apply le_S.
  assumption.
Qed.

Theorem n_le_m__Sn_le_Sm : forall n m,
  n <= m -> S n <= S m.
Proof.
  intros.
  induction H.
  apply le_n.
  apply le_S.
  assumption.
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.
  intros.
  inversion H ; subst.
  apply le_n.
  inversion H1.
  intros.
  inversion H.
  apply le_n.
  apply IHm in H1.
  apply le_S.
  assumption.
Qed.

Theorem le_plus_l : forall a b,
  a <= a + b.
Proof.
  intros.
  induction b.
SearchAbout plus.
  rewrite <- plus_n_O.
  apply le_n.
    rewrite <- plus_n_Sm.
  apply le_S.
assumption.
Qed.

Theorem plus_lt : forall n1 n2 m,
  n1 + n2 < m ->
  n1 < m /\ n2 < m.
Proof.
 induction n1 as [| n'].
 intros.
 Case "n1 = 0".
 split.
 destruct m.
 SCase "m = 0 ".
 simpl in H.
 inversion H.
 SCase "m = S m".
 simpl in H.
 destruct n2.
 assumption.
 inversion H.
 apply le_S.
 subst.
 apply le_S. Admitted.

Theorem lt_S : forall n m,
  n < m ->
  n < S m.
Proof.
  intros.
  induction H.
  destruct n.
  apply le_S.
  apply le_n.
  apply le_S.
  apply le_n.
  apply le_S.
  apply le_S.
  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.
induction n as [| n'].
Case "n' = 0".
  intros.
  inversion H.
Case "n'= S n'".
intros.
destruct m.
SCase "m = 0".
reflexivity.
SCase "m= S m".
simpl.
apply IHn'.
simpl 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.
  inversion H.
  Case "n =S n".
  intros.
  destruct m.
  SCase "m = 0".
  unfold not.
  intros.
  inversion H0.
  SCase "m = S m".
  simpl in H.
  apply IHn' in H.
  unfold not.
  intros.
  apply Sn_le_Sm__n_le_m in H0.
  unfold not in H.
  apply H.
  assumption.
Qed.

(** [] *)

(** **** Exercise: 3 stars, recommended (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.   If you do receive
    assistance from anyone, please say so specifically in a comment.

    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, optional (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 this 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.
(** [] *)

(* ##################################################### *)
(** * Optional Material *)

(* ################################################### *)
(** ** Induction Principles for [/\] and [\/] *)

(** The induction principles for conjunction and disjunction are a
    good illustration of Coq's way of generating simplified induction
    principles for [Inductive]ly defined propositions, which we
    discussed in the last chapter.  You try first: *)

(** **** Exercise: 1 star (and_ind_principle) *)
(** See if you can predict the induction principle for conjunction. *)

(* Check and_ind. *)
(** [] *)

(** **** Exercise: 1 star (or_ind_principle) *)
(** See if you can predict the induction principle for disjunction. *)

(* Check or_ind. *)
(** [] *)

Check and_ind.

(** From the inductive definition of the proposition [and P Q]
[[
     Inductive and (P Q : Prop) : Prop :=
       conj : P -> Q -> (and P Q).
]]
    we might expect Coq to generate this induction principle
[[
     and_ind_max :
       forall (P Q : Prop) (P0 : P /\ Q -> Prop),
            (forall (a : P) (b : Q), P0 (conj P Q a b)) ->
            forall a : P /\ Q, P0 a
]]
    but actually it generates this simpler and more useful one:
[[
     and_ind :
       forall P Q P0 : Prop,
            (P -> Q -> P0) ->
            P /\ Q -> P0
]]
    In the same way, when given the inductive definition of [or P Q]
[[
     Inductive or (P Q : Prop) : Prop :=
       | or_introl : P -> or P Q
       | or_intror : Q -> or P Q.
]]
    instead of the "maximal induction principle"
[[
     or_ind_max :
       forall (P Q : Prop) (P0 : P \/ Q -> Prop),
            (forall a : P, P0 (or_introl P Q a)) ->
            (forall b : Q, P0 (or_intror P Q b)) ->
            forall o : P \/ Q, P0 o
]]
    what Coq actually generates is this:
[[
     or_ind :
       forall P Q P0 : Prop,
            (P -> P0) ->
            (Q -> P0) ->
            P \/ Q -> P0
]]
*)

(* ######################################################### *)
(** ** Explicit Proof Objects for Induction *)

(** Although tactic-based proofs are normally much easier to
    work with, the ability to write a proof term directly is sometimes
    very handy, particularly when we want Coq to do something slightly
    non-standard.  *)

(** Recall the induction principle on naturals that Coq generates for
    us automatically from the Inductive declation for [nat]. *)

(* Check nat_ind. *)
(* ===>
   nat_ind : forall P : nat -> Prop,
      P 0%nat ->
      (forall n : nat, P n -> P (S n)) ->
      forall n : nat, P n  *)

(** There's nothing magic about this induction lemma: it's just
   another Coq lemma that requires a proof.  Coq generates the proof
   automatically too...  *)

Print nat_ind.  Print nat_rect.
(* ===> (after some manual inlining)
   nat_ind =
    fun (P : nat -> Type)
        (f : P 0%nat)
        (f0 : forall n : nat, P n -> P (S n)) =>
          fix F (n : nat) : P n :=
             match n as n0 return (P n0) with
            | 0%nat => f
            | S n0 => f0 n0 (F n0)
            end.
*)

(** We can read this as follows:
     Suppose we have evidence [f] that [P] holds on 0,  and
     evidence [f0] that [forall n:nat, P n -> P (S n)].
     Then we can prove that [P] holds of an arbitrary nat [n] via
     a recursive function [F] (here defined using the expression
     form [Fix] rather than by a top-level [Fixpoint]
     declaration).  [F] pattern matches on [n]:
      - If it finds 0, [F] uses [f] to show that [P n] holds.
      - If it finds [S n0], [F] applies itself recursively on [n0]
         to obtain evidence that [P n0] holds; then it applies [f0]
         on that evidence to show that [P (S n)] holds.
    [F] is just an ordinary recursive function that happens to
    operate on evidence in [Prop] rather than on terms in [Set].

    Aside to those interested in functional programming: You may
    notice that the [match] in [F] requires an annotation [as n0
    return (P n0)] to help Coq's typechecker realize that the two arms
    of the [match] actually return the same type (namely [P n]).  This
    is essentially like matching over a GADT (generalized algebraic
    datatype) in Haskell.  In fact, [F] has a _dependent_ type: its
    result type depends on its argument; GADT's can be used to
    describe simple dependent types like this.

    We can adapt this approach to proving [nat_ind] to help prove
    _non-standard_ induction principles too.  Recall our desire to
    prove that

    [forall n : nat, even n -> ev n].

    Attempts to do this by standard induction on [n] fail, because the
    induction principle only lets us proceed when we can prove that
    [even n -> even (S n)] -- which is of course never provable.  What
    we did earlier in this chapter was a bit of a hack:

    [Theorem even_ev : forall n : nat,
     (even n -> ev n) /\ (even (S n) -> ev (S n))].

    We can make a much better proof by defining and proving a
    non-standard induction principle that goes "by twos":

 *)

 Definition nat_ind2 :
    forall (P : nat -> Prop),
    P 0 ->
    P 1 ->
    (forall n : nat, P n -> P (S(S n))) ->
    forall n : nat , P n :=
       fun P => fun P0 => fun P1 => fun PSS =>
          fix f (n:nat) := match n return P n with
                             0 => P0
                           | 1 => P1
                           | S (S n') => PSS n' (f n')
                          end.

 (** Once you get the hang of it, it is entirely straightforward to
     give an explicit proof term for induction principles like this.
     Proving this as a lemma using tactics is much less intuitive (try
     it!).

     The [induction ... using] tactic gives a convenient way to
     specify a non-standard induction principle like this. *)

Lemma even_ev' : forall n, even n -> ev n.
Proof.
 intros.
 induction n as [ | |n'] using nat_ind2.
  Case "even 0".
    apply ev_0.
  Case "even 1".
    inversion H.
  Case "even (S(S n'))".
    apply ev_SS.
    apply IHn'.  unfold even.  unfold even in H.  simpl in H. apply H.
Qed.

(* ######################################################### *)
(** ** The Coq Trusted Computing Base *)

(** One issue that arises with any automated proof assistant is "why
    trust it?": what if there is a bug in the implementation that
    renders all its reasoning suspect?

    While it is impossible to allay such concerns completely, the fact
    that Coq is based on the Curry-Howard Correspondence gives it a
    strong foundation. Because propositions are just types and proofs
    are just terms, checking that an alleged proof of a proposition is
    valid just amounts to _type-checking_ the term.  Type checkers are
    relatively small and straightforward programs, so the "trusted
    computing base" for Coq -- the part of the code that we have to
    believe is operating correctly -- is small too.

    What must a typechecker do?  Its primary job is to make sure that
    in each function application the expected and actual argument
    types match, that the arms of a [match] expression are constructor
    patterns belonging to the inductive type being matched over and
    all arms of the [match] return the same type, and so on.

    There are a few additional wrinkles:

    - Since Coq types can themselves be expressions, the checker must
      normalize these (by using the conversion rules) before
      comparing them.

    - The checker must make sure that [match] expressions are
      _exhaustive_.  That is, there must be an arm for every possible
      constructor.  To see why, consider the following alleged proof
      object:
[[
      Definition or_bogus : forall P Q, P \/ Q -> P :=
        fun (P Q : Prop) (A : P \/ Q) =>
           match A with
           | or_introl H => H
           end.
]]
      All the types here match correctly, but the [match] only
      considers one of the possible constructors for [or].  Coq's
      exhaustiveness check will reject this definition.

    - The checker must make sure that each [fix] expression
      terminates.  It does this using a syntactic check to make sure
      that each recursive call is on a subexpression of the original
      argument.  To see why this is essential, consider this alleged
      proof:
[[
          Definition nat_false : forall (n:nat), False :=
             fix f (n:nat) : False := f n.
]]
      Again, this is perfectly well-typed, but (fortunately) Coq will
      reject it. *)

(** Note that the soundness of Coq depends only on the correctness of
    this typechecking engine, not on the tactic machinery.  If there
    is a bug in a tactic implementation (and this certainly does
    happen!), that tactic might construct an invalid proof term.  But
    when you type [Qed], Coq checks the term for validity from
    scratch.  Only lemmas whose proofs pass the type-checker can be
    used in further proof developments.  *)


(* Ejercicio Semanal 3: Semántica y Verificación.
  Javier Enríquez Mendoza
  415000073 *)

Require Import List.
Require Import ArithRing.
Require Import Bool.

Variable A : Type.

Fixpoint filterbool (p : A -> bool) (l : list A) : list A :=
  match l with
  | nil => nil
  | a :: t => if p a then a :: filterbool p t else filterbool p t
  end.

Check filterbool.

Fixpoint length (l: list A) : nat:=
  match l with
  | nil => 0
  | x :: xs => 1 + (length xs)
  end.

Variable p q : A -> bool.

Lemma primero: forall (xs: list A), length(filterbool p xs) <= length xs.
Proof.
intros.
induction xs.
simpl.
trivial.
destruct (bool_dec (p a) true).
simpl.
rewrite e.
simpl.
intuition.
apply not_true_is_false in n.
simpl.
rewrite n.
intuition.
Qed.

Fixpoint andB (a b: bool): bool :=
  match a,b  with
  | false, _ => false
  | _, false => false
  | true, true => true
  end.

Definition andp (r q: A -> bool): A-> bool:= fun (x:A) => andB(r x)(q x).

Lemma segundo: forall (xs: list A), filterbool (andp p q) xs = filterbool p (filterbool q xs).
Proof.
intros.
induction xs.
simpl.
trivial.
unfold andp in IHxs.
destruct (bool_dec (p a) true).
destruct (bool_dec (q a) true).
simpl.
unfold andp.
rewrite e0.
simpl.
rewrite e.
simpl.
rewrite <- IHxs.
trivial.
apply not_true_is_false in n.
simpl.
unfold andp.
rewrite e.
simpl.
rewrite n.
exact IHxs.
apply not_true_is_false in n.
destruct (bool_dec (q a) true).
simpl.
unfold andp.
rewrite e.
simpl.
rewrite n.
simpl.
exact IHxs.
apply not_true_is_false in n0.
simpl.
unfold andp.
rewrite n0.
rewrite n.
simpl.
exact IHxs.
Qed.

Lemma tercero: forall (xs: list A), filterbool p (filterbool p xs)= filterbool p xs.
Proof.
intros.
induction xs.
simpl.
trivial.
destruct (bool_dec (p a) true).
simpl.
rewrite e.
simpl.
rewrite e.
rewrite IHxs.
trivial.
apply not_true_is_false in n.
simpl.
rewrite n.
exact IHxs.
Qed.


(** Generated by coq-of-ocaml *)
Require Import OCaml.OCaml.

Local Set Primitive Projections.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Local Open Scope type_scope.
Import ListNotations.

Unset Positivity Checking.
Unset Guard Checking.

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

Inductive natural : Set :=
| Zero : natural
| Succ : natural -> natural.

Fixpoint plus (plus_arg0 : natural) (plus_arg1 : natural) {struct plus_arg0}
  : natural :=
  match plus_arg0 with
  | Zero => plus_arg1
  | Succ n => Succ (plus n plus_arg1)
  end.

Fixpoint mult (mult_arg0 : natural) (mult_arg1 : natural) {struct mult_arg0}
  : natural :=
  match mult_arg0 with
  | Zero => Zero
  | Succ n => plus (mult n mult_arg1) mult_arg1
  end.

Definition synth (y : natural) (z : natural) (x : natural) : natural :=
  mult z (plus x y).


(** Generated by coq-of-ocaml *)
Require Import OCaml.OCaml.

Local Set Primitive Projections.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Local Open Scope type_scope.
Import ListNotations.

Unset Positivity Checking.
Unset Guard Checking.

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

Inductive natural : Set :=
| Zero : natural
| Succ : natural -> natural.

Fixpoint plus (plus_arg0 : natural) (plus_arg1 : natural) {struct plus_arg0}
  : natural :=
  match plus_arg0 with
  | Zero => plus_arg1
  | Succ n => Succ (plus n plus_arg1)
  end.

Fixpoint mult (mult_arg0 : natural) (mult_arg1 : natural) {struct mult_arg0}
  : natural :=
  match mult_arg0 with
  | Zero => Zero
  | Succ n => plus (mult n mult_arg1) mult_arg1
  end.

Definition synth (lf2 : natural) (lf3 : natural) : natural :=
  plus lf2 (plus lf3 Zero).


(* Script de ejercicios de lógica minimal. *)

(* Ejercicio 1a *)
Theorem triple_neg_single: forall (a: Prop), ~~~a <-> ~a.
Proof.
split.
+ unfold not.
  intros.
  apply H.
  intro.
  apply H1.
  trivial.
+ unfold not.
  intros.
  apply H0.
  intro.
  apply H; trivial.
Qed.

(* Ejercicio 1b *)
Theorem double_neg_and: forall (a b: Prop), ~~(a/\b) -> ~~a /\ ~~b.
Proof.
unfold not.
intros.
split.
+ intro.
  apply H.
  intro.
  apply H0.
  destruct H1.
  trivial.
+ intro.
  apply H.
  intro.
  apply H0.
  destruct H1.
  trivial.
Qed.

(* Ejercicio 1c *)
Theorem double_neg_forall: forall (T:Type) (a: T -> Prop), ~~(forall x:T, a x) -> forall x:T, ~~a x.
Proof.
unfold not.
intros.
apply H.
intro.
apply H0.
apply H1.
Qed.


(**  by Evelyne Contejean, LRI *)

(* Note by Pierre :
  May be already in standard library ?
 *)

Set Implicit Arguments.

From  Coq Require Import Relations.

Inductive trans_clos (A : Set) (R : relation A) : relation A:=
  | t_step : forall x y, R x y -> trans_clos R x y
  | t_trans : forall x y z, R x y -> trans_clos R y z -> trans_clos R x z.

Lemma trans_clos_is_trans :
  forall (A :Set) (R : relation A) a b c,
  trans_clos R a b -> trans_clos R b c -> trans_clos R a c.
Proof.
  intros A R a b c Hab; generalize c;
  clear c; induction Hab as [a b Hab | a b c Hab Hbc].
  intros c Hbc; apply t_trans with b; trivial.
  intros d Hcd; apply t_trans with b; trivial.
  apply IHHbc; trivial.
Qed.

Lemma acc_trans :
 forall (A : Set) (R : relation A) a, Acc R a -> Acc (trans_clos R) a.
Proof.
  intros A R a Acc_R_a.
  induction Acc_R_a as [a Acc_R_a IH].
  apply Acc_intro.
  intros b b_Rp_a; induction b_Rp_a.
  apply IH; trivial.
  apply Acc_inv with y.
  apply IHb_Rp_a; trivial.
  apply t_step; trivial.
Qed.

Lemma wf_trans :
  forall (A : Set) (R : relation A) , well_founded R ->
                                      well_founded (trans_clos R).
Proof.
  unfold well_founded; intros A R WR.
  intro; apply acc_trans; apply WR; trivial.
Qed.

Lemma inv_trans :
  forall (A : Set) (R : relation A) (P : A -> Prop),
  (forall a b, P a -> R a b -> P b) ->
  forall a b, P a -> trans_clos R a b -> P b.
Proof.
   intros A R P Inv a b Pa a_Rp_b; induction a_Rp_b.
   apply Inv with x; trivial.
   apply IHa_Rp_b; apply Inv with x; trivial.
Qed.


Require Import Lib BinNatDef BinIntDef.

Open Scope list_scope.

(** We represent here number in base 10 by lists of decimal digits,
    in big endian order (most significant digit comes first). *)

Inductive digit := D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | D9.

Definition dec := list digit. (** big endian *)

Definition ten := D1 :: D0 :: nil. (** For example... *)

(** This representation favors simplicity over canonicity :
    we might need later to normalize by removing the leading zeros *)

Fixpoint norm l :=
  match l with
  | D0 :: l => norm l
  | _ => l
  end.

(** Conversion between decimal and Peano nat representations *)

Module DecNat.

Definition digit2nat d :=
  match d with
  | D0 => 0
  | D1 => 1
  | D2 => 2
  | D3 => 3
  | D4 => 4
  | D5 => 5
  | D6 => 6
  | D7 => 7
  | D8 => 8
  | D9 => 9
  end.

Definition nat2digit n :=
  match n with
  | 0 => D0
  | 1 => D1
  | 2 => D2
  | 3 => D3
  | 4 => D4
  | 5 => D5
  | 6 => D6
  | 7 => D7
  | 8 => D8
  | _ => D9 (* n>9 shouldn't happen *)
  end.

Fixpoint d2n (d:dec)(acc:nat) :=
  match d with
  | nil => acc
  | d :: l => d2n l (TailNat.addmul (digit2nat d) 10 acc)
  end.

Definition dec2nat d := d2n d 0.

Fixpoint n2d (n:nat)(acc:dec)(count:nat) :=
 match count, n with
 | 0, _ => acc
 | _, 0 => acc
 | S count', _ =>
     let (q,r) := diveucl n 10 in
     n2d q (nat2digit r :: acc) count'
 end.

Definition nat2dec n := n2d n nil n.

End DecNat.

(** Same for decimal and binary N numbers *)

Module DecN.

Local Open Scope N.

Definition digit2n d : N :=
  match d with
  | D0 => 0
  | D1 => 1
  | D2 => 2
  | D3 => 3
  | D4 => 4
  | D5 => 5
  | D6 => 6
  | D7 => 7
  | D8 => 8
  | D9 => 9
  end.

Definition n2digit (n:N) :=
  match n with
  | 0 => D0
  | 1 => D1
  | 2 => D2
  | 3 => D3
  | 4 => D4
  | 5 => D5
  | 6 => D6
  | 7 => D7
  | 8 => D8
  | _ => D9 (* n>9 shouldn't happen *)
  end.

Fixpoint d2n (d:dec)(acc:N) :=
  match d with
  | nil => acc
  | d :: l => d2n l (N.add (digit2n d) (N.mul 10 acc))
  end.

Definition dec2n d := d2n d 0.

Fixpoint n2d (n:N)(acc:dec)(count:positive) :=
 match count, n with
 | xH, _ => acc
 | _, 0 => acc
 | xO count', _ | xI count', _ =>
     let (q,r) := N.div_eucl n 10 in
     n2d q (n2digit r :: acc) count'
 end.

Definition n2dec n :=
  n2d n nil (match n with 0 => xH | Npos p => xO p end).

End DecN.

(** For positive and Z numbers, we simply go through N for the moment. *)

Module DecPos.

Definition dec2pos d :=
 match DecN.dec2n d with
 | N0 => None
 | Npos p => Some p
 end.

Definition pos2dec p := DecN.n2dec (Npos p).

End DecPos.

Module DecZ.

Definition dec2z d :=
 match DecN.dec2n d with
 | N0 => Z0
 | Npos p => Zpos p
 end.

Definition z2dec z :=
 match z with
 | Zpos p => DecN.n2dec (Npos p)
 | _ => nil (* TODO : for now, we discard negative numbers *)
 end.

End DecZ.

(** A successor on decimal. Not really mandatory, just to state
    that our conversions preserve the order of numbers *)

Fixpoint bounded_succ l :=
 match l with
 | nil => Carry nil
 | d::l =>
   match bounded_succ l with
   | NoCarry l' => NoCarry (d::l')
   | Carry l' =>
     match d with
     | D0 => NoCarry (D1::l')
     | D1 => NoCarry (D2::l')
     | D2 => NoCarry (D3::l')
     | D3 => NoCarry (D4::l')
     | D4 => NoCarry (D5::l')
     | D5 => NoCarry (D6::l')
     | D6 => NoCarry (D7::l')
     | D7 => NoCarry (D8::l')
     | D8 => NoCarry (D9::l')
     | D9 => Carry (D0::l')
     end
   end
 end.

Definition succ l :=
 match bounded_succ l with
 | NoCarry l' => l'
 | Carry l' => D1::l'
 end.

(** The strict order on decimal numbers is the transitive
    closure of the successor *)

Inductive lt : dec -> dec -> Prop :=
 | Succ x : lt x (succ x)
 | Trans x y z : lt x y -> lt y z -> lt x z.


(* Exercise 27 *)

Require Import BenB.

Definition D := Z. (* Integers! *)

Variables P Q S T : D -> Prop.

(* Note that this theorem does not hold if D were
   real numbers instead of integers. *)

(* We have to tell Coq explicitly that we are dealing with integers: *)
Open Scope Z_scope.

Theorem exercise_027 :
  (forall x:D, x in [3,7] -> P x \/ Q x)
->
  (forall x:D, x in (2,8) -> ~ P x -> Q x).
Proof.
imp_i a1.
all_i a.
imp_i a2.
imp_i a3.
dis_e (P a \/ Q a) a4 a4.
imp_e (a in [3, 7]).
all_e (forall x:D, x in [3, 7] -> P x \/ Q x) a.
hyp a1.
interval.
con_i.
imp_e (2 < a).
imp_i a4.
lin_solve.
con_e1 (a < 8).
hyp a2.
imp_e (a < 8).
imp_i a4.
lin_solve.
con_e2 (2 < a).
hyp a2.
neg_e (P a).
hyp a3.
hyp a4.
hyp a4.
Qed.


Require Import Nat Arith.

Inductive Nat : Type := zero : Nat | succ : Nat -> Nat.

Inductive Lst : Type := nil : Lst | cons : Nat -> Lst -> Lst.

Inductive Tree : Type := node : Nat -> Tree -> Tree -> Tree |  leaf : Tree.

Inductive Pair : Type := mkpair : Nat -> Nat -> Pair
with ZLst : Type := zcons : Pair -> ZLst -> ZLst |  znil : ZLst.

Fixpoint drop (drop_arg0 : Nat) (drop_arg1 : Lst) : Lst
           := match drop_arg0, drop_arg1 with
              | x, nil => nil
              | zero, x => x
              | succ x, cons y z => drop x z
              end.

Fixpoint mem (mem_arg0 : Nat) (mem_arg1 : Lst) : Prop
:= match mem_arg0, mem_arg1 with
    | x, nil => False
    | x, cons y z => x = y \/ mem x z
    end.

Theorem theorem0 : forall (x : Nat) (y : Nat) (z : Lst), mem x (drop y z) -> mem x z.
Proof.
  intros.
  generalize dependent y.
  induction z.
  - intros. destruct y.
    + contradiction.
    + contradiction.
  - intros. destruct y.
    + assumption.
    + simpl in H. apply IHz in H. simpl. auto.
Qed.


Require Import Arith Lia List String.
Import ListNotations.
Open Scope string.

Definition eq_dec (A : Type) :=
  forall (x : A),
    forall (y : A),
      {x = y} + {x <> y}.

Notation var := string.
Definition var_eq : eq_dec var := string_dec.

Inductive arith : Set :=
| Const (n : nat)
| Var (x : var)
| Plus (e1 e2 : arith)
| Minus (e1 e2 : arith)
| Times (e1 e2 : arith).

Print arith.

Lemma syntax_is_not_semantics:
    Plus (Const 1) (Const 1) <> Const 2.
Proof.
    Locate "<>".
    Print not.
    unfold not.
    (*No way to prove False means the thing on the left shouldn't be provable*)
    intro Heq.
    (*Now need to call Coq's bluff i.e. for every way it would prove Heq, we would prove False*)
    (*Another flavor of destruct called inversion*)
    inversion Heq.
Qed.

Fixpoint has_zero (e : arith) : bool :=
  match e with
  | Const n => Nat.eqb n 0
  | Var x => false
  | Plus e1 e2  => has_zero e1 || has_zero e2
  | Minus e1 e2 => has_zero e1 || has_zero e2
  | Times e1 e2 => has_zero e1 || has_zero e2
  end.

Compute has_zero (Const 0). (* true *)
Compute has_zero (Const 1). (* false *)
Compute has_zero (Plus (Var "x") (Var "y")). (* false *)
Compute has_zero (Minus (Plus (Var "x") (Var "y")) (Const 0)). (* true *)

Declare Scope arith_scope. (* defines a name for our collection of notations *)
Coercion Const : nat >-> arith.
Coercion Var : var >-> arith.
Infix "+" := Plus : arith_scope.
Infix "-" := Minus : arith_scope.
Infix "*" := Times : arith_scope.
Delimit Scope arith_scope with arith. (* lets us use "%arith" annotations *)
Bind Scope arith_scope with arith.

(* Now let's see those same examples again. *)
Compute has_zero 0. (* true *)
Compute has_zero 1. (* false *)
Compute has_zero ("x" + "y")%arith. (* false *)
Compute has_zero ("x" + "y" - 0)%arith. (* true *)

(* memory *)
Definition valuation := list (var * nat).

Fixpoint lookup (x : var) (v : valuation) : option nat :=
  match v with
  | [] => None
  | (y, n) :: v' =>
    if var_eq x y
    then Some n
    else lookup x v'
  end.

Fixpoint eval_arith (e : arith) (v : valuation) : nat :=
  match e with
  | Const n => n
  | Var x =>
    match lookup x v with
    | None => 0  (* sorta bogus! *)
    | Some n => n
    end
  | Plus  e1 e2 => eval_arith e1 v + eval_arith e2 v
  | Minus e1 e2 => eval_arith e1 v - eval_arith e2 v
  | Times e1 e2 => eval_arith e1 v * eval_arith e2 v
  end.

Compute eval_arith (1 + 1)%arith [].  (* 2 *)
Compute eval_arith (2)%arith [].      (* also 2 *)

Locate "+".

Lemma eval_one_plus_one_is_eval_two :
  forall v,
    eval_arith (1 + 1)%arith v = eval_arith 2 v.
Proof.
  intro.
  simpl.
  reflexivity.
Qed.

(* from expressions to commands *)
Inductive cmd :=
| Skip
| Assign (x : var) (e : arith)
| Sequence (c1 c2 : cmd)
| Repeat (e : arith) (body : cmd).

Fixpoint do_n_times {A} (f : A -> A) (n : nat) (x : A) : A :=
  match n with
  | O => x
  | S n' => do_n_times f n' (f x)
  end.

Compute do_n_times (cons true) 7 [false; false]. (* 7 "true"s followed by 2 "false"s *)

Fixpoint eval_cmd (c : cmd) (v : valuation) : valuation :=
  match c with
  | Skip => v
  | Assign x e => (x, eval_arith e v) :: v
  | Sequence c1 c2 => eval_cmd c2 (eval_cmd c1 v)
  | Repeat e body => do_n_times (eval_cmd body) (eval_arith e v) v
  (* | Repeat e body => do_n_times (fun v => eval_cmd body v) (eval_arith e v) v *)
  end.

(* more notation *)
Declare Scope cmd_scope.
Delimit Scope cmd_scope with cmd.
Bind Scope cmd_scope with cmd.
Notation "x <- e" := (Assign x e%arith) (at level 75) : cmd_scope.
Infix ";" := Sequence (at level 76) : cmd_scope.
Notation "'repeat' e 'doing' body 'done'" :=
  (Repeat e%arith body) (at level 75) : cmd_scope.

Fixpoint factorial_tr (n : nat) (acc : nat) : nat :=
  match n with
  | O => acc
  | S m => factorial_tr m (n * acc)
  end.

Definition factorial_tailrec (n : nat) : nat :=
  factorial_tr n 1.

Definition factorial : cmd :=
  "output" <- 1;
  repeat "input" doing
    "output" <- "output" * "input";
    "input" <- "input" - 1
  done.

Print factorial.
(* if we put 4 in "input", then will compute 4 * 3 * 2 * 1 in "output" *)

Fixpoint coq_factorial (n : nat) : nat :=
  match n with
  | 0 => 1
  | S n' => n * coq_factorial n'
  end.

Compute coq_factorial 4.

Theorem factorial_correct :
  forall v n,
    lookup "input" v = Some n ->
    lookup "output" (eval_cmd factorial v) = Some (coq_factorial n).
Proof.
  intros.
  unfold factorial.
  simpl.
Qed.

Definition factorial_loop_body : cmd :=
  "output" <- "output" * "input";
  "input" <- "input" - 1.

Theorem factorial_ok :
  forall v input,
    lookup "input" v = Some input ->
    lookup "output" (eval_cmd factorial v) = Some (factorial_tailrec input).
Proof.
  (* motto... *)
  intros v input Hinput.
  unfold factorial, factorial_tailrec.
  fold factorial_loop_body.
  cbn -[factorial_loop_body].
  (* do_n_times (eval_cmd factorial_loop_body) *)
Abort.

Definition map_equiv m1 m2 := forall x, lookup x m1 = lookup x m2.

Example map_equiv_example :
  forall m,
    map_equiv (("x", 0) :: ("y", 1) :: m) (("y", 1) :: ("x", 0) :: m).
Proof.
  (* example of destructing on a non-variable *)
Admitted.

Ltac solve_map_cases :=
  unfold map_equiv; intros; simpl;
  repeat destruct (var_eq _ _); try congruence.

(* key lemma for factorial *)
Lemma factorial_loop_body_ok :
  forall n acc v,
    lookup "input" v = Some n ->
    lookup "output" v = Some acc ->
    map_equiv
      (do_n_times (eval_cmd factorial_loop_body) n v)
      (("input", 0) :: ("output", factorial_tr n acc) :: v).
Proof.
  (* do_n_times is recursive on n. So is factorial_tr. Let's induct on n. *)
  induction n; intros acc v Hinput Houtput x.
  - simpl. solve_map_cases.
  - cbn [do_n_times factorial_tr].
    unfold map_equiv in *.
    rewrite IHn with (acc := S n * acc); solve_map_cases.
    + rewrite Hinput. f_equal. lia.
    + rewrite Hinput, Houtput. f_equal. lia.
Qed.

Theorem factorial_ok :
  forall v input,
    lookup "input" v = Some input ->
    lookup "output" (eval_cmd factorial v) = Some (factorial_tailrec input).
Proof.
  intros v input Hinput.
  unfold factorial, factorial_tailrec.
  fold factorial_loop_body.
  cbn -[factorial_loop_body].
  rewrite Hinput.
  rewrite factorial_loop_body_ok with (acc := 1); solve_map_cases.
Qed.


From CoqAlgs Require Export WayOfTheCoq.

(** * Not that easy *)

Module NotEasy.

Fixpoint nth {A : Type} (n : nat) (l : list A) : option A :=
match l with
    | [] => None
    | h :: t =>
        match n with
            | 0 => Some h
            | S n' => nth n' t
        end
end.

Definition sorted {A : Type} (l : list nat) : Prop :=
  forall i j : nat, i <= j ->
    forall n m : nat,
      nth i l = Some n -> nth j l = Some m -> n <= m.

Print nat.
(*
Inductive nat : Set := O : nat | S : nat -> nat
*)

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

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

End NotEasy.

(** * Improving patchwork definitions *)

Module ImprovingPatchworkDefinitions1.

Inductive LessThanAll (n : nat) : list nat -> Prop :=
    | LessThanAll_nil : LessThanAll n []
    | LessThanAll_cons :
        forall (h : nat) (t : list nat),
          n <= h -> LessThanAll n t -> LessThanAll n (h :: t).

Inductive Sorted : list nat -> Prop :=
    | Sorted_nil : Sorted []
    | Sorted_cons :
        forall (h : nat) (t : list nat),
          LessThanAll h t -> Sorted t -> Sorted (h :: t).

End ImprovingPatchworkDefinitions1.

Module ImprovingPatchworkDefinitions2.

Inductive Sorted : list nat -> Prop :=
    | Sorted_nil : Sorted []
    | Sorted_singl : forall n : nat, Sorted [n]
    | Sorted_cons :
        forall (n m : nat) (l : list nat),
          n <= m -> Sorted (m :: l) -> Sorted (n :: m :: l).

End ImprovingPatchworkDefinitions2.

Inductive Sorted {A : Type} (R : A -> A -> Prop) : list A -> Prop :=
    | Sorted_nil : Sorted R []
    | Sorted_singl : forall x : A, Sorted R [x]
    | Sorted_cons :
        forall (x y : A) (l : list A),
          R x y -> Sorted R (y :: l) -> Sorted R (x :: y :: l).

(** * Just about right... or is it? Staying on the right track *)

Module Generating.

Inductive Permutation {A : Type} : list A -> list A -> Prop :=
  | perm_nil :
      Permutation [] []
  | perm_skip :
      forall (x : A) (l1 l2 : list A),
        Permutation l1 l2 -> Permutation (x :: l1) (x :: l2)
  | perm_swap :
      forall (x y : A) (l : list A),
        Permutation (y :: x :: l) (x :: y :: l)
  | perm_trans :
      forall l1 l2 l3 : list A,
        Permutation l1 l2 -> Permutation l2 l3 -> Permutation l1 l3.

End Generating.

Module Counting.

Fixpoint count {A : Type} (p : A -> bool) (l : list A) : nat :=
match l with
    | [] => 0
    | h :: t => (if p h then 1 else 0) + count p t
end.

Definition Permutation {A : Type} (l1 l2 : list A) : Prop :=
  forall p : A -> bool, count p l1 = count p l2.

End Counting.

Module Moving.

Inductive Transposition {A : Type} : list A -> list A -> Prop :=
    | Transposition' :
        forall (x y : A) (l1 l2 l3 : list A),
          Transposition (l1 ++ x :: l2 ++ y :: l3)
                        (l1 ++ y :: l2 ++ x :: l3).

Inductive Permutation {A : Type} : list A -> list A -> Prop :=
    | Permutation_refl :
        forall l : list A, Permutation l l
    | Permutation_step_trans :
        forall l1 l2 l3 : list A,
          Transposition l1 l2 ->
            Permutation l2 l3 -> Permutation l1 l3.

End Moving.

Module Moving2.

Inductive AdjacentTransposition {A : Type} : list A -> list A -> Prop :=
    | AdjacentTransposition' :
        forall (x y : A) (l1 l2 : list A),
          AdjacentTransposition (l1 ++ x :: y :: l2) (l1 ++ y :: x :: l2).

Inductive Permutation {A : Type} : list A -> list A -> Prop :=
    | Permutation_refl   :
        forall l : list A, Permutation l l
    | Permutation_step_trans :
        forall l1 l2 l3 : list A,
          AdjacentTransposition l1 l2 -> Permutation l1 l2 -> Permutation l2 l3.

End Moving2.

Class Sort
  {A : Type} (R : A -> A -> Prop) (f : list A -> list A) : Prop :=
{
    isSorted : forall l : list A, Sorted R (f l);
    isPermutation : forall l : list A, Permutation l (f l)
}.


(** Generated by coq-of-ocaml *)
Require Import OCaml.OCaml.

Local Set Primitive Projections.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Local Open Scope type_scope.
Import ListNotations.

Unset Positivity Checking.
Unset Guard Checking.

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

Inductive natural : Set :=
| Zero : natural
| Succ : natural -> natural.

Fixpoint plus (plus_arg0 : natural) (plus_arg1 : natural) {struct plus_arg0}
  : natural :=
  match plus_arg0 with
  | Zero => plus_arg1
  | Succ n => Succ (plus n plus_arg1)
  end.

Fixpoint mult (mult_arg0 : natural) (mult_arg1 : natural) {struct mult_arg0}
  : natural :=
  match mult_arg0 with
  | Zero => Zero
  | Succ n => plus (mult n mult_arg1) mult_arg1
  end.

Definition synth (y : natural) (lf1 : natural) (x : natural) : natural :=
  plus y (mult x y).


Definition plus (n : nat)(m : nat) : nat := n + m.
Definition plus': nat -> nat -> nat := fun n m => n + m.
Definition id (A: Type)(x: A): A := x.
Definition id' : forall (A: Type), A -> A := fun A x => x.
Definition prop0 : forall (A: Prop), A -> A := fun A x => x.

Definition prop1 : forall (A B C : Prop), (B -> C) -> (A -> B) -> A -> C
  := fun A B C f g x => f (g x).

(* 問0. 任意の命題 A B に対して、A ならば 「A ならば B」ならば Bが成り立つ。 *)

Definition q0 : forall (A B : Prop), A -> (A -> B) -> B
  := fun A B a f => f a.

(*問1. 任意の命題 A B C に対して、「A ならば B ならば C」ならば「B ならば A ならば C」が成り立つ*)
Definition q1 : forall (A B C : Prop), (A -> B -> C) -> (B -> A -> C)
  := fun A B C f b a => f a b.


(** Generated by coq-of-ocaml *)
Require Import OCaml.OCaml.

Local Set Primitive Projections.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Local Open Scope type_scope.
Import ListNotations.

Unset Positivity Checking.
Unset Guard Checking.

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

Inductive natural : Set :=
| Zero : natural
| Succ : natural -> natural.

Fixpoint plus (plus_arg0 : natural) (plus_arg1 : natural) {struct plus_arg0}
  : natural :=
  match plus_arg0 with
  | Zero => plus_arg1
  | Succ n => Succ (plus n plus_arg1)
  end.

Fixpoint mult (mult_arg0 : natural) (mult_arg1 : natural) {struct mult_arg0}
  : natural :=
  match mult_arg0 with
  | Zero => Zero
  | Succ n => plus (mult n mult_arg1) mult_arg1
  end.

Definition synth (lf2 : natural) (y : natural) (x : natural) : natural :=
  plus lf2 (mult y x).


(** Generated by coq-of-ocaml *)
Require Import OCaml.OCaml.

Local Set Primitive Projections.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Local Open Scope type_scope.
Import ListNotations.

Unset Positivity Checking.
Unset Guard Checking.

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

Inductive natural : Set :=
| Zero : natural
| Succ : natural -> natural.

Fixpoint plus (plus_arg0 : natural) (plus_arg1 : natural) {struct plus_arg0}
  : natural :=
  match plus_arg0 with
  | Zero => plus_arg1
  | Succ n => Succ (plus n plus_arg1)
  end.

Fixpoint mult (mult_arg0 : natural) (mult_arg1 : natural) {struct mult_arg0}
  : natural :=
  match mult_arg0 with
  | Zero => Zero
  | Succ n => plus (mult n mult_arg1) mult_arg1
  end.

Definition synth (lf2 : natural) (y : natural) (x : natural) : natural :=
  plus lf2 (mult x y).


(* Exercise 74 *)

Require Import BenB.

Variables A B C D : Prop.

Theorem exercise_074 : (A /\ (B -> C)) -> B -> (A /\ C).
Proof.
imp_i a1.
imp_i a2.
con_i.
con_e1 (B -> C).
hyp a1.
imp_e B.
con_e2 (A).
hyp a1.
hyp a2.
Qed.


Require Import Arith.

Inductive List A :=
 | Nil : List A
 | Cons : A -> List A -> List A.

Implicit Arguments Nil [A].
Implicit Arguments Cons [A].

Fixpoint append {A} (xs ys: List A): List A :=
  match xs with
   | Nil => ys
   | Cons x xs => Cons x (append xs ys)
  end.

Fixpoint reverse {A} (xs: List A): List A :=
  match xs with
   | Nil => Nil
   | Cons x xs => append (reverse xs) (Cons x Nil)
  end.

Theorem append_nil : forall (A: Type) (xs: List A),
  append xs Nil = xs.
Proof.
  intros A xs.
  induction xs.
    reflexivity.

    simpl.
    rewrite IHxs.
    reflexivity.
Qed.

Theorem append_assoc : forall (A: Type) (xs ys zs: List A),
  append xs (append ys zs) = append (append xs ys) zs.
Proof.
  intros A xs ys zs.
  induction xs.
    reflexivity.

    simpl.
    rewrite IHxs.
    reflexivity.
Qed.

Theorem rev_assoc : forall (A: Type) (xs ys: List A),
  reverse (append xs ys) = append (reverse ys) (reverse xs).
Proof.
  intros A xs ys.
  induction xs.
    simpl.
    rewrite append_nil.
    reflexivity.

    simpl.
    rewrite append_assoc.
    rewrite IHxs.
    reflexivity.
Qed.

Theorem rev_rev : forall (A: Type) (xs: List A),
  reverse (reverse xs) = xs.
Proof.
  intros A xs.
  induction xs.
    reflexivity.

    simpl.
    rewrite rev_assoc.
    rewrite IHxs.
    reflexivity.
Qed.

Fixpoint get {A: Type} (xs: List A) (n: nat) (def: A): A :=
  match xs, n with
   | Cons x _, O => x
   | Cons _ xs, S n => get xs n def
   | _, _ => def
  end.

Fixpoint length {A: Type} (xs: List A): nat :=
  match xs with
   | Nil => O
   | Cons _ xs => S (length xs)
  end.

Theorem IFPH_4_2_12: forall (A: Type) (xs ys: List A) (k: nat) (def: A),
  let n := length xs in
  get (append xs ys) k def = match nat_compare k n with
   | Lt => get xs k def
   | _ => get ys (k - n) def
  end.
Proof.
  intros A xs ys k def.
  simpl.
  generalize k.
  clear k.
  induction xs.
    intro k.
    simpl.
    destruct k.
      simpl. reflexivity.
      simpl. reflexivity.

    intro k.
    destruct k.
      simpl. reflexivity.
      simpl. rewrite (IHxs k). reflexivity.
Qed.