Timings for Spec.v
Require Import Spaces.Pos.
Require Import Spaces.Int.Core.
Local Set Universe Minimization ToSet.
Local Open Scope int_scope.
(** ** Addition is commutative *)
Lemma int_add_comm n m : n + m = m + n.
all: apply ap, pos_add_comm.
(** ** Zero is the additive identity. *)
Definition int_add_0_l n : 0 + n = n
:= 1.
Lemma int_add_0_r n : n + 0 = n.
(** ** Multiplication by zero is zero *)
Definition int_mul_0_l n : 0 * n = 0
:= 1.
Lemma int_mul_0_r n : n * 0 = 0.
(** ** One is the multiplicative identity *)
Lemma int_mul_1_l n : 1 * n = n.
Lemma int_mul_1_r n : n * 1 = n.
destruct n; trivial; cbn; apply ap, pos_mul_1_r.
Lemma int_pos_sub_diag n : int_pos_sub n n = 0.
induction n; trivial; cbn.
all: exact (ap int_double IHn).
Lemma int_add_negation_l n : (-n) + n = 0.
destruct n; trivial; cbn; apply int_pos_sub_diag.
Lemma int_add_negation_r n : n + (-n) = 0.
destruct n; trivial; cbn; apply int_pos_sub_diag.
(** ** Permutation of neg and pos_succ *)
Lemma int_neg_pos_succ p : neg (pos_succ p) = int_pred (neg p).
(** ** Permutation of pos and pos_succ *)
Lemma int_pos_pos_succ p : pos (pos_succ p) = int_succ (pos p).
(** ** Negation of a doubled positive integer *)
Lemma int_negation_double a : - (int_double a) = int_double (- a).
(** Negation of the predecessor of a doubled positive integer. *)
Lemma int_negation_pred_double a : - (int_pred_double a) = int_succ_double (- a).
(** Negation of the doubling of the sucessor of an positive. *)
Lemma int_negation_succ_double a : - (int_succ_double a) = int_pred_double (- a).
(** Negation of subtraction of positive integers *)
Lemma int_pos_sub_negation a b : - (int_pos_sub a b) = int_pos_sub b a.
induction a as [|a ah|a ah];
destruct b;
cbn; trivial.
all: rewrite ?int_negation_double,
?int_negation_succ_double,
?int_negation_pred_double.
(** ** int_succ is a retract of int_pred *)
Definition int_succ_pred : int_succ o int_pred == idmap.
intros [n | | n]; [|trivial|].
all: destruct n; trivial.
1: apply pos_pred_double_succ.
apply pos_succ_pred_double.
(** ** int_pred is a retract of int_succ *)
Definition int_pred_succ : int_pred o int_succ == idmap.
intros [n | | n]; [|trivial|].
all: destruct n; trivial.
1: apply pos_succ_pred_double.
apply pos_pred_double_succ.
(* ** The successor autoequivalence. *)
Global Instance isequiv_int_succ : IsEquiv int_succ | 0
:= isequiv_adjointify int_succ _ int_succ_pred int_pred_succ.
Definition equiv_int_succ : Int <~> Int
:= Build_Equiv _ _ _ isequiv_int_succ.
(** ** Negation distributes over addition *)
Lemma int_negation_add_distr n m : - (n + m) = - n + - m.
destruct n, m; simpl; trivial using int_pos_sub_negation.
(** ** Negation is injective *)
Lemma int_negation_inj n m : -n = -m -> n = m.
destruct n, m; simpl; intro H.
2: apply pos_neq_zero in H.
3: apply pos_neq_neg in H.
4: apply zero_neq_pos in H.
6: apply zero_neq_neg in H.
7: apply neg_neq_pos in H.
8: apply neg_neq_zero in H.
(** ** Subtracting 1 from a sucessor gives the positive integer. *)
Lemma int_pos_sub_succ_l a
: int_pos_sub (pos_succ a) 1%pos = pos a.
cbn; apply ap, pos_pred_double_succ.
(** ** Subtracting a sucessor from 1 gives minus the integer. *)
Lemma int_pos_sub_succ_r a
: int_pos_sub 1%pos (pos_succ a) = neg a.
cbn; apply ap, pos_pred_double_succ.
(** ** Interaction of doubling functions and subtraction *)
Lemma int_succ_double_int_pos_sub a b
: int_succ_double (int_pos_sub a (pos_succ b))
= int_pred_double (int_pos_sub a b).
induction a; induction b; trivial.
by rewrite pos_pred_double_succ.
cbn; destruct (int_pos_sub a b); trivial.
destruct (int_pos_sub a (pos_succ b)); trivial.
cbn; destruct (int_pos_sub a b); trivial.
cbn; destruct (int_pos_sub a b); trivial.
Lemma int_pred_double_int_pos_sub a b
: int_pred_double (int_pos_sub (pos_succ a) b)
= int_succ_double (int_pos_sub a b).
induction a; induction b; trivial.
cbn; by destruct (int_pos_sub a b).
cbn; by destruct (int_pos_sub a b).
by rewrite pos_pred_double_succ.
by destruct (int_pos_sub (pos_succ a) b).
by destruct (int_pos_sub a b).
(** ** Subtractions cancel sucessors. *)
Lemma int_pos_sub_succ_succ a b
: int_pos_sub (pos_succ a) (pos_succ b) = int_pos_sub a b.
rewrite <- 2 pos_add_1_r.
induction a; induction b; trivial.
by rewrite pos_pred_double_succ.
1: apply int_succ_double_int_pos_sub.
cbn; apply ap, ap, pos_pred_double_succ.
1: apply int_pred_double_int_pos_sub.
rewrite <- 2 pos_add_1_r.
(** ** Predecessor of a subtraction is the subtraction of a sucessor. *)
Lemma int_pred_pos_sub_r a b
: int_pred (int_pos_sub a b) = int_pos_sub a (pos_succ b).
induction b as [|b bH] using pos_peano_ind.
1: destruct a; trivial; destruct a; trivial.
induction a as [|a aH] using pos_peano_ind.
rewrite pos_succ_pred_double.
rewrite pos_pred_double_succ.
rewrite 2 int_pos_sub_succ_succ.
(** ** Negation of the predecessor is an involution. *)
Lemma int_negation_pred_negation_red x
: - int_pred (- int_pred x) = x.
destruct x as [x| |x]; trivial;
destruct x; trivial; cbn; apply ap.
1: apply pos_pred_double_succ.
apply pos_succ_pred_double.
(** ** Predecessor of a sum is the sum with a predecessor *)
Lemma int_pred_add_r a b
: int_pred (a + b) = a + int_pred b.
intros [a| |a] [b| |b]; trivial.
by rewrite pos_add_assoc.
induction b as [|b bH] using pos_peano_ind.
intro a; exact (int_pred_succ (neg a)).
rewrite (int_pred_succ (pos b)).
rewrite <- int_pos_sub_negation.
rewrite <- int_pred_pos_sub_r.
rewrite int_pos_sub_negation.
apply int_negation_pred_negation_red.
apply int_pred_pos_sub_r.
induction b as [|b bH] using pos_peano_ind.
intro a; exact (int_pred_succ (pos a)).
rewrite (int_pred_succ (pos b)).
cbn; rewrite pos_add_assoc.
change (int_pred (int_succ (pos (a + b)%pos)) = pos a + pos b).
(** ** Subtraction from a sum is the sum of a subtraction *)
Lemma int_pos_sub_add (a b c : Pos)
: int_pos_sub (a + b)%pos c = pos a + int_pos_sub b c.
induction c as [|c ch] using pos_peano_ind.
change (int_pred (pos a + pos b) = pos a + (int_pred (pos b))).
rewrite <- int_pred_pos_sub_r.
rewrite <- int_pred_pos_sub_r.
(** An auxillary lemma used to prove associativity. *)
Lemma int_add_assoc_pos p n m : pos p + (n + m) = pos p + n + m.
destruct n as [n| |n], m as [m| |m]; trivial.
cbn; apply int_negation_inj.
rewrite !int_negation_add_distr, !int_pos_sub_negation.
rewrite int_add_comm, pos_add_comm.
by rewrite <- int_pos_sub_add, int_add_comm,
<- int_pos_sub_add, pos_add_comm.
cbn; apply ap, pos_add_assoc.
(** ** Associativity of addition *)
Lemma int_add_assoc n m p : n + (m + p) = n + m + p.
rewrite !int_negation_add_distr.
(** ** Relationship between [int_succ], [int_pred] and addition. *)
Lemma int_add_succ_l a b : int_succ a + b = int_succ (a + b).
rewrite <- int_add_assoc, (int_add_comm 1 b).
Lemma int_add_succ_r a b : a + int_succ b = int_succ (a + b).
Lemma int_add_pred_l a b : int_pred a + b = int_pred (a + b).
rewrite <- int_add_assoc, (int_add_comm (-1) b).
Lemma int_add_pred_r a b : a + int_pred b = int_pred (a + b).
(** ** Commutativity of multiplication *)
Lemma int_mul_comm n m : n * m = m * n.
destruct n, m; cbn; try reflexivity;
apply ap; apply pos_mul_comm.
(** Distributivity of multiplication over addition *)
Lemma int_pos_sub_mul_pos n m p
: int_pos_sub n m * pos p = int_pos_sub (n * p)%pos (m * p)%pos.
rewrite 2 (pos_mul_comm _ p).
set (int_pos_sub n m) as k.
rewrite <- (int_pos_sub_negation _ (x0 _)).
rewrite int_negation_add_distr.
rewrite int_pos_sub_negation.
set (int_pos_sub n m) as k.
Lemma int_pos_sub_mul_neg n m p
: int_pos_sub m n * neg p = int_pos_sub (n * p)%pos (m * p)%pos.
rewrite 2 (pos_mul_comm _ p).
rewrite <- int_pos_sub_negation.
by destruct (int_pos_sub n m).
rewrite <- int_pos_sub_negation.
set (int_pos_sub n m) as k.
rewrite <- (int_pos_sub_negation _ (x0 _)).
rewrite int_negation_add_distr.
rewrite int_pos_sub_negation.
rewrite <- (int_pos_sub_negation m).
set (int_pos_sub m n) as k.
Lemma int_mul_add_distr_r n m p : (n + m) * p = n * p + m * p.
induction p; destruct n, m; cbn; trivial; try f_ap;
try apply pos_mul_add_distr_r;
try apply int_pos_sub_mul_neg;
try apply int_pos_sub_mul_pos;
apply int_mul_0_r.
Lemma int_mul_add_distr_l n m p : n * (m + p) = n * m + n * p.
rewrite 3 (int_mul_comm n); apply int_mul_add_distr_r.
Lemma int_mul_assoc n m p : n * (m * p) = n * m * p.
destruct n, m, p; cbn; trivial; f_ap; apply pos_mul_assoc.