(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)


(* Contribution to the Coq Library   V6.3 (July 1999)                    *)

(****************************************************************)
(*           The Calculus of Inductive Constructions            *)
(*                       COQ v5.10                              *)
(*                                                              *)
(* Laurent Arditi.  Laboratoire I3S. CNRS ura 1376.             *)
(* Universite de Nice - Sophia Antipolis                        *)
(* arditi@unice.fr, http://wwwi3s.unice.fr/~arditi/lolo.html    *)
(*                                                              *)
(* date: october 1995                                           *)
(* file: IncrDecr.v                                             *)
(* contents: definnition of an incrementer and a decrementer    *)
(* on BVs     **** TO BE COMPLETED ****                         *)
(****************************************************************)

Require Export AdderProof.



Fixpoint BV_increment (l : list bool) : BV :=
  match l with
  | nil => nilbv
  | false :: v => true :: v
  | true :: v => false :: BV_increment v
  end.


Fixpoint BV_increment_carry (l : list bool) : bool :=
  match l with
  | nil => true
  | false :: v => false
  | true :: v => BV_increment_carry v
  end.



Fixpoint BV_decrement (l : list bool) : BV :=
  match l with
  | nil => nilbv
  | false :: v => true :: BV_decrement v
  | true :: v => false :: v
  end.

Fixpoint BV_decrement_carry (l : list bool) : bool :=
  match l with
  | nil => true
  | false :: v => BV_decrement_carry v
  | true :: v => false
  end.



Lemma length_BV_increment :
 forall v : BV, lengthbv (BV_increment v) = lengthbv v.
(* Goal: forall v : BV, @eq BV (BV_increment (BV_decrement v)) v *)
simple induction v. auto.
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_decrement (BV_increment l)) l), @eq BV (BV_decrement (BV_increment (@cons bool a l))) (@cons bool a l) *)
simple induction a.
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
simpl in |- *. intros. rewrite H; trivial.
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
simpl in |- *. intros. trivial.
Qed.

Lemma length_BV_decrement :
 forall v : BV, lengthbv (BV_decrement v) = lengthbv v.
(* Goal: forall v : BV, @eq BV (BV_increment (BV_decrement v)) v *)
simple induction v. auto.
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_decrement (BV_increment l)) l), @eq BV (BV_decrement (BV_increment (@cons bool a l))) (@cons bool a l) *)
simple induction a.
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
simpl in |- *. intros. trivial.

(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
simpl in |- *. intros. rewrite H; trivial.
Qed.

Lemma BV_increment_limit :
 forall n : nat,
 BV_increment (list_const bool n true) = list_const bool n false.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
simple induction n. simpl in |- *. auto. intros. simpl in |- *. rewrite H; trivial.
Qed.

Lemma BV_decrement_limit :
 forall n : nat,
 BV_decrement (list_const bool n false) = list_const bool n true.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
simple induction n. simpl in |- *. auto. intros. simpl in |- *. rewrite H; trivial.
Qed.


Lemma BV_increment_limit_carry :
 forall n : nat, BV_increment_carry (list_const bool n true) = true.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
simple induction n. auto. intros. simpl in |- *. exact H.
Qed.

Lemma BV_decrement_limit_carry :
 forall n : nat, BV_decrement_carry (list_const bool n false) = true.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
simple induction n. auto. intros. simpl in |- *. exact H.
Qed.


Lemma BV_increment_adder :
 forall v : BV,
 appbv (BV_increment v) (consbv (BV_increment_carry v) nilbv) =
 BV_full_adder v nilbv true.
(* Goal: forall v : BV, @eq BV (BV_increment (BV_decrement v)) v *)
simple induction v.
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
simpl in |- *. unfold BV_full_adder in |- *. unfold nilbv, consbv, appbv in |- *. simpl in |- *. trivial.
(* Induction a. Unfold consbv nilbv appbv . Simpl. Intros. Rewrite -> H.*)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_decrement (BV_increment l)) l), @eq BV (BV_decrement (BV_increment (@cons bool a l))) (@cons bool a l) *)
simple induction a. intros. unfold consbv, nilbv, appbv in |- *. simpl in |- *.
(* Goal: @eq BV (@cons bool false (@app bool (BV_increment l) (@cons bool (BV_increment_carry l) (@nil bool)))) (BV_full_adder (@cons bool true l) (@nil bool) true) *)
(* Goal: forall (l : list bool) (_ : @eq BV (appbv (BV_increment l) (consbv (BV_increment_carry l) nilbv)) (BV_full_adder l nilbv true)), @eq BV (appbv (BV_increment (@cons bool false l)) (consbv (BV_increment_carry (@cons bool false l)) nilbv)) (BV_full_adder (@cons bool false l) nilbv true) *)
unfold consbv, nilbv, appbv in H. rewrite H.
(* *)
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
unfold BV_full_adder in |- *. simpl in |- *. replace (half_adder_sum true true) with false.
(* Goal: @eq BV (@cons bool true l) (@cons bool true l) *)
(* Goal: @eq BV (@cons bool false (BV_increment (BV_decrement l))) (@cons bool false l) *)
trivial.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
auto.
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
unfold BV_full_adder in |- *. unfold consbv, nilbv, appbv in |- *. intros. simpl in |- *.
(* Goal: @eq BV (@cons bool true (@app bool l (@cons bool false (@nil bool)))) (@cons bool (half_adder_sum false true) (@app bool (BV_full_adder_sum l (@nil bool) false) (@cons bool (BV_full_adder_carry l (@nil bool) false) (@nil bool)))) *)
replace (half_adder_sum false true) with true.
(* Goal: @eq BV (@cons bool true (@app bool l (@cons bool false (@nil bool)))) (@cons bool true (@app bool (BV_full_adder_sum l (@nil bool) false) (@cons bool (BV_full_adder_carry l (@nil bool) false) (@nil bool)))) *)
(* Goal: @eq bool true (half_adder_sum false true) *)
replace (BV_full_adder_sum l nil false) with l.
(* Goal: @eq BV (@cons bool true (@app bool l (@cons bool false (@nil bool)))) (@cons bool true (@app bool l (@cons bool (BV_full_adder_carry l (@nil bool) false) (@nil bool)))) *)
(* Goal: @eq (list bool) l (BV_full_adder_sum l (@nil bool) false) *)
(* Goal: @eq bool true (half_adder_sum false true) *)
replace (BV_full_adder_carry l nil false) with false.
(* Goal: @eq BV (@cons bool true l) (@cons bool true l) *)
(* Goal: @eq BV (@cons bool false (BV_increment (BV_decrement l))) (@cons bool false l) *)
trivial.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
auto. auto. auto.
Qed.

Lemma BV_increment_ok :
 forall v : BV,
 BV_to_nat (appbv (BV_increment v) (consbv (BV_increment_carry v) nilbv)) =
 S (BV_to_nat v).
(* Goal: forall v : BV, @eq nat (BV_to_nat (appbv (BV_increment v) (consbv (BV_increment_carry v) nilbv))) (S (BV_to_nat v)) *)
intro. rewrite BV_increment_adder. rewrite BV_full_adder_ok.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
simpl in |- *. elim plus_n_O. elim plus_n_Sm. auto.
Qed.

Lemma BV_decr_incr : forall v : BV, BV_decrement (BV_increment v) = v.
(* Goal: forall v : BV, @eq BV (BV_increment (BV_decrement v)) v *)
simple induction v. simpl in |- *. trivial.
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_decrement (BV_increment l)) l), @eq BV (BV_decrement (BV_increment (@cons bool a l))) (@cons bool a l) *)
simple induction a. intros. simpl in |- *. rewrite H; trivial.
(* Goal: @eq BV (BV_decrement (BV_increment (@cons bool false l))) (@cons bool false l) *)
intros. simpl in |- *. trivial.
Qed.

Lemma BV_incr_decr : forall v : BV, BV_increment (BV_decrement v) = v.
(* Goal: forall v : BV, @eq BV (BV_increment (BV_decrement v)) v *)
simple induction v.
(* Goal: @eq BV (BV_increment (BV_decrement (@nil bool))) (@nil bool) *)
(* Goal: forall (a : bool) (l : list bool) (_ : @eq BV (BV_increment (BV_decrement l)) l), @eq BV (BV_increment (BV_decrement (@cons bool a l))) (@cons bool a l) *)
auto. simple induction a; intros; simpl in |- *. trivial. rewrite H; trivial.
Qed.
