(* Ingénierie de la preuve : proposition de solution pour le CC 2007 *)


(* Question 1 *)

Inductive even : nat -> Prop :=
| even_O : (even O)
| even_S : forall n:nat, even n -> even (S (S n)).

(* prédicat : être pair !                *) 
(* 0 est pair                            *)
(* si n est pair, alors n+2 est pair     *)

(* Question 2 *)

Fixpoint puissance (x:nat) (n:nat) {struct n} : nat :=
match n with
| O => (S O)
| S p => x * (puissance x p)
end.

(* puissance x O -> (S O)                          *)
(* puissance x (S p) -> x * (puissance x p)        *)

(* Question 3 *)

(* Logique intuitionniste = constructive                    *)
(* on doit fournir des témoins lors de preuves              *)
(* de théorèmes d'existence                                 *)
(* Logique classique = logique intuitionniste + tiers exclu *)
(* forall P:Prop, P\/~P                                     *)

(* Question 4 *)

(* -------------  ------------------                                         *)
(* A -> B,A |- A  A -> B,A |- A -> B                                         *)
(* ---------------------------------                                         *)
(*           A -> B,A |- B             A -> B, ~A |- ~A                      *)
(* ---------------------------------  -------------------  ----------------  *)
(*         A -> B, A |- ~A\/B         A -> B, ~A |- ~A\/B  A->B,~A |- A\/~A  *)
(* ------------------------------------------------------------------------- *) 
(*                                      A -> B  |- ~A\/B                     *)
(*                               ------------------------------              *)
(*                                  |- (A -> B) -> (~A\/B)                   *)


(* ----------------  -----------------                                       *)
(* ~A\/B,A, ~A |- A  ~A\/B,A ,~A |- ~A                                       *)
(* ----------------  -----------------                                       *)
(*         ~A\/B, A, ~A |- False                                             *)
(* ----------------------------------- ---------------- -------------------- *)
(*             ~A\/B, A, ~A |- B       ~A\/B, A, B |- B ~A\/B, A, B |- ~A\/B *)
(* ------------------------------------------------------------------------- *)
(*                                ~A\/B, A |- B                              *)
(* ------------------------------------------------------------------------- *)
(*                            |- ~A\/B -> (A -> B)                           *)

Axiom tiers_exclu : forall P:Prop, P\/~P.

(* Question 5 *)
Lemma sens1 : forall A B:Prop, (A -> B) -> (~A\/B).
intros A B HAB.
elim (tiers_exclu A).
intros HA.
right.
apply HAB.
assumption.
intros HA.
left.
assumption.
Qed.

(* Question 6 *)
Lemma sens2 : forall A B:Prop, (~A\/B) -> A -> B.
intros A B HnAB HA.
elim HnAB.
intros HnA.
apply False_ind.
apply HnA.
assumption.
intros HB.
assumption.
Qed.

Print sens2.

(* sens2 =                                                                 *)
(* fun (A B : Prop) (HnAB : ~ A \/ B) (HA : A) =>                          *)
(* or_ind (fun HnA : ~ A => False_ind B (HnA HA)) (fun HB : B => HB) HnAB  *)
(*      : forall A B : Prop, ~ A \/ B -> A -> B                            *)



(* Question 7 *)

Inductive positive : Set := xH : positive 
                          | xI : positive -> positive 
                          | xO : positive -> positive.

Definition un := xH.
Definition trois := (xI xH).
Definition quatre := (xO (xO xH)).
Definition sept := xI trois.

(* Question 8 *)
Fixpoint Sp (p:positive) {struct p} : positive :=
match p with
| xH => (xO xH)
| xI q => xO (Sp q)
| xO q => xI q
end.

(* Question 9 *)
(* on remarque que 2p-1 = 2(p-1)+1 *)

Fixpoint Pdouble_minus_one (p:positive) {struct p} : positive :=
match p with
| xH => xH
| xI q => xI (xO q)
| xO q => xI (Pdouble_minus_one q)
end.

Definition Pb (p:positive) : positive :=
match p with 
| xH => xH
| xI q => xO q
| xO q => Pdouble_minus_one q
end.

(* Question 10 *)
Check positive_ind.

(* positive_ind                                                              *)
(*     : forall P : positive -> Prop,                                        *)
(*       P xH ->                                                             *)
(*       (forall p : positive, P p -> P (xI p)) ->                           *)
(*       (forall p : positive, P p -> P (xO p)) -> forall p : positive, P p  *)

(* Question 11 *)
Lemma thm' : forall p:positive, Sp (Pdouble_minus_one p) = xO p.
intros p; elim p.
simpl.
reflexivity.
intros q Hq.
change (Sp (xI (xO q)) = xO (xI q)).
change (xO (Sp (xO q))=xO (xI q)).
change (xO (xI q) = xO (xI q)).
reflexivity.
intros q Hq.
change (Sp (xI (Pdouble_minus_one q)) = xO (xO q)).
change (xO (Sp (Pdouble_minus_one q)) = xO (xO q)).
rewrite Hq.
reflexivity.
Qed.

(* finalement supprimé *)
Lemma thm : forall p:positive, Pb (Sp p)=p.
intros p; elim p.
simpl.
reflexivity.
intros q Hq.
change (Pb (xO (Sp q))=xI q).
change (Pdouble_minus_one (Sp q)=xI q).
2: intros q Hq.
2:change (Pb (xI q)=xO q).
2:change (xO q=xO q).
2:reflexivity.

elim q.
simpl.
reflexivity.
intros q1 Hq1.
simpl.
intros.
rewrite Hq1.
reflexivity.
intros q1 Hq1.
simpl.
reflexivity.
Qed.

(* Question 12 *)
Inductive bin : Set := b0 : bin | bp : positive -> bin.

Check bin_ind.
(* bin_ind                                                               *)
(*     : forall P : bin -> Prop,                                         *)
(*       P b0 -> (forall p : positive, P (bp p)) -> forall b : bin, P b  *)

(* Question 13 *)
Definition Sb (b:bin) : bin :=
match b with
| b0 => bp un
| bp x => bp (Sp x)
end.

(* Question 14 *)
(* voir NArith *)