(* 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: may 1995                                               *)
(* file: FullAdder.v                                            *)
(* contents: definition and verification of a full adder        *)
(*                                                              *)
(****************************************************************)

(* Full adder sur les bits (bool) : rend la somme ou le carry *)
(* Verification: Theorem full_adder_ok *)

Require Export HalfAdder.

(****************************************************************)

Definition full_adder_sum (a b c : bool) :=
  half_adder_sum (half_adder_sum a b) c.

Definition full_adder_carry (a b c : bool) :=
  half_adder_carry a b || half_adder_carry (half_adder_sum a b) c.

(****************************************************************)

Lemma full_adder_sum_sym1 :
 forall a b c : bool, full_adder_sum a b c = full_adder_sum b a c.
(* Goal: forall a b c : bool, @eq bool (full_adder_carry a b c) (full_adder_carry b a c) *)
simple induction a; simple induction b; auto.
Qed. Hint Resolve full_adder_sum_sym1.

Lemma full_adder_sum_sym2 :
 forall a b c : bool, full_adder_sum a b c = full_adder_sum a c b.
(* Goal: forall a b c : bool, @eq bool (full_adder_carry a b c) (full_adder_carry a c b) *)
simple induction b.
(* Goal: forall c : bool, @eq bool (full_adder_carry a true c) (full_adder_carry a c true) *)
(* Goal: forall c : bool, @eq bool (full_adder_carry a false c) (full_adder_carry a c false) *)
simple induction c.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
auto.
(* Goal: forall c : bool, @eq bool (full_adder_sum a false c) (full_adder_sum a c false) *)
unfold full_adder_sum in |- *.
(* Goal: @eq bool (orb false (half_adder_carry (half_adder_sum a false) c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_sum_false.
(* Goal: @eq bool (orb false (half_adder_carry (half_adder_sum a false) c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_sum_false.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
auto.
(* Goal: forall c : bool, @eq bool (full_adder_sum a false c) (full_adder_sum a c false) *)
unfold full_adder_sum in |- *.
(* Goal: @eq bool (orb false (half_adder_carry (half_adder_sum a false) c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_sum_false.
(* Goal: forall c : bool, @eq bool (half_adder_sum a c) (half_adder_sum (half_adder_sum a c) false) *)
intro.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
auto.
Qed. Hint Resolve full_adder_sum_sym2.

Lemma full_adder_sum_false :
 forall a : bool, full_adder_sum a false false = a.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
simple induction a; auto.
Qed. Hint Resolve full_adder_sum_false.

Lemma full_adder_sum_true : forall a : bool, full_adder_sum a true true = a.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
simple induction a; auto.
Qed. Hint Resolve full_adder_sum_true.

Lemma full_adder_carry_sym1 :
 forall a b c : bool, full_adder_carry a b c = full_adder_carry b a c.
(* Goal: forall a b c : bool, @eq bool (full_adder_carry a b c) (full_adder_carry b a c) *)
simple induction a; simple induction b; auto.
Qed. Hint Resolve full_adder_carry_sym1.

Lemma full_adder_carry_sym2 :
 forall a b c : bool, full_adder_carry a b c = full_adder_carry a c b.
(* Goal: forall a b c : bool, @eq bool (full_adder_carry a b c) (full_adder_carry a c b) *)
simple induction b.
(* Goal: forall c : bool, @eq bool (full_adder_carry a true c) (full_adder_carry a c true) *)
(* Goal: forall c : bool, @eq bool (full_adder_carry a false c) (full_adder_carry a c false) *)
simple induction c.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
auto.
(* Goal: @eq bool (full_adder_carry false true true) true *)
unfold full_adder_carry in |- *.
(* Goal: @eq bool (orb false (half_adder_carry (half_adder_sum a false) c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_sum_false.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_carry_false.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_carry_false.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) false) *)
simpl in |- *.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
elim (half_adder_carry a true); auto.
(* Goal: forall c : bool, @eq bool (full_adder_carry a false c) (full_adder_carry a c false) *)
intros.
(* Goal: @eq bool (full_adder_carry false true true) true *)
unfold full_adder_carry in |- *.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_carry_false.
(* Goal: @eq bool (orb false (half_adder_carry (half_adder_sum a false) c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_sum_false.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) (half_adder_carry (half_adder_sum a c) false)) *)
rewrite half_adder_carry_false.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) false) *)
simpl in |- *.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
elim (half_adder_carry a c); auto.
Qed. Hint Resolve full_adder_carry_sym2.

Lemma full_adder_carry_false :
 forall a : bool, full_adder_carry a false false = false.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
simple induction a; auto.
Qed. Hint Resolve full_adder_carry_false.

Lemma full_adder_carry_true :
 forall a : bool, full_adder_carry a true true = true.
(* Goal: forall a : bool, @eq bool (full_adder_carry a true true) true *)
simple induction a.
(* Goal: @eq bool (full_adder_carry false true true) true *)
unfold full_adder_carry in |- *.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
auto.
(* Goal: @eq bool (full_adder_carry false true true) true *)
unfold full_adder_carry in |- *.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
auto.
Qed. Hint Resolve full_adder_carry_true.

Lemma full_adder_carry_true_false :
 forall a : bool, full_adder_carry a true false = a.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
simple induction a; auto.
Qed. Hint Resolve full_adder_carry_true_false.

Lemma full_adder_carry_neg :
 forall a b : bool, full_adder_carry a (negb a) b = b.
(* Goal: @eq bool (orb false (half_adder_carry a c)) (orb (half_adder_carry a c) false) *)
simple induction a; simple induction b; simpl in |- *.
(* Goal: @eq bool (full_adder_carry false true false) false *)
rewrite full_adder_carry_sym1. rewrite full_adder_carry_true. trivial.
(* Goal: @eq bool false false *)
rewrite full_adder_carry_false. trivial.
(* Goal: @eq bool (full_adder_carry false true true) true *)
(* Goal: @eq bool (full_adder_carry false true false) false *)
rewrite full_adder_carry_true. trivial.
(* Goal: @eq bool (full_adder_carry false true false) false *)
rewrite full_adder_carry_sym1. rewrite full_adder_carry_false. trivial.
Qed.

(****************************************************************)

Theorem full_adder_ok :
 forall a b c : bool,
 bool_to_nat (full_adder_sum a b c) +
 (bool_to_nat (full_adder_carry a b c) + bool_to_nat (full_adder_carry a b c)) =
 bool_to_nat a + bool_to_nat b + bool_to_nat c.
(* Goal: @eq bool (orb (half_adder_carry false true) (half_adder_carry (half_adder_sum false true) true)) true *)
simple induction a; simple induction b; simple induction c; auto.
Qed.
