Section Pdigit.
(* n is the base *)
Variable n : Z.
(* and it is greater or equal to 2 *)
Hypothesis nMoreThan1 : (1 < n)%Z.
Let nMoreThanOne := Zlt_1_O _ (Zlt_le_weak _ _ nMoreThan1).
Hint Resolve nMoreThanOne: zarith.
intros q; elim q; simpl in |- *;
auto with zarith.
Qed.
Hint Resolve Zpower_nat_less: zarith.
intros p; rewrite <- (Zmult_1_l (Zpower_nat n p)); replace (S p) with (1 + p);
[ rewrite Zpower_nat_is_exp | auto with zarith ].
rewrite Zpower_nat_1; auto with zarith.
apply Zmult_gt_0_lt_compat_r; auto with zarith.
apply Z.lt_gt; auto with zarith.
Qed.
intros p q H'; elim H'; simpl in |- *; auto.
apply Zpower_nat_monotone_S.
intros m H H0; apply Z.lt_trans with (1 := H0).
apply Zpower_nat_monotone_S.
Qed.
Hint Resolve Zpower_nat_monotone_lt: zarith.
intros p q H'.
case (le_or_lt q p); auto; (intros H'1; generalize H'; case H'1).
intros H'0; Contradict H'0; auto with zarith.
intros m H'0 H'2; Contradict H'2; auto with zarith.
Qed.
intros p q H'; case (le_lt_or_eq _ _ H'); auto with zarith.
intros H1; rewrite H1; auto with zarith.
Qed.
intros p q H'; case (le_or_lt p q); intros H'0; auto with arith.
Contradict H'; auto with zarith.
Qed.
intros p q H'; apply le_antisym; apply Zpower_nat_anti_monotone_le;
rewrite H'; auto with zarith.
Qed.
(* To compute the number of digits structurally, we suppose that we know already an upper bound q. So we start from q down to 0 to find the bigger exponent r such that n^(r-1) < v *)
Fixpoint digitAux (v r : Z) (q : positive) {struct q} : nat :=
match q with
| xH => 0
| xI q' =>
match (n * r)%Z with
| r' =>
match (r ?= v)%Z with
| Datatypes.Gt => 0
| _ => S (digitAux v r' q')
end
end
| xO q' =>
match (n * r)%Z with
| r' =>
match (r ?= v)%Z with
| Datatypes.Gt => 0
| _ => S (digitAux v r' q')
end
end
end.
(* As we know that log_n q < log_2 q we can define our function digit*)
Definition digit (q : Z) :=
match q with
| Z0 => 0
| Zpos q' => digitAux (Z.abs q) 1 (xO q')
| Zneg q' => digitAux (Z.abs q) 1 (xO q')
end.
Hint Unfold digit.
intros p r; replace (S p) with (1 + p);
[ rewrite Zpower_nat_is_exp | auto with arith ].
rewrite Zpower_nat_1; ring.
Qed.
intros p q; unfold Z.lt in |- *; generalize (Zcompare_EGAL p q);
(CaseEq (p ?= q)%Z; simpl in |- *; auto).
intros H H0; case (Zcompare_Gt_Lt_antisym p q); auto.
Qed.
intros v r q; generalize r; elim q; clear r q; simpl in |- *; auto.
intros q' Rec r; generalize (Zcompare_correct r v); case (r ?= v)%Z; auto.
intros H1; generalize (Rec (n * r)%Z); case (digitAux v (n * r) q').
intros; rewrite H1; rewrite Zpower_nat_O; auto with zarith.
intros r'; rewrite digitAux1; auto.
intros H1; generalize (Rec (n * r)%Z); case (digitAux v (n * r) q').
intros; rewrite Zpower_nat_O; auto with zarith.
apply Z.le_trans with (m := r); auto with zarith.
intros r'; rewrite digitAux1; auto.
intros q' Rec r; generalize (Zcompare_correct r v); case (r ?= v)%Z; auto.
intros H1; generalize (Rec (n * r)%Z); case (digitAux v (n * r) q').
intros; rewrite H1; rewrite Zpower_nat_O; auto with zarith.
intros r'; rewrite digitAux1; auto.
intros H1; generalize (Rec (n * r)%Z); case (digitAux v (n * r) q').
intros; rewrite Zpower_nat_O; auto with zarith.
apply Z.le_trans with (m := r); auto with zarith.
intros r'; rewrite digitAux1; auto.
Qed.
(* digit is correct (first part) *)
intros q; case q.
intros H; Contradict H; auto with zarith.
intros p H; unfold digit in |- *;
generalize (digitAuxLess (Z.abs (Zpos p)) 1 (xO p));
case (digitAux (Z.abs (Zpos p)) 1 (xO p)); simpl in |- *;
auto with zarith.
intros p H; unfold digit in |- *;
generalize (digitAuxLess (Z.abs (Zneg p)) 1 (xO p));
case (digitAux (Z.abs (Zneg p)) 1 (xO p)); simpl in |- *;
auto with zarith.
Qed.
Hint Resolve digitLess: zarith.
Hint Resolve Zmult_gt_0_lt_compat_r Zmult_gt_0_lt_compat_l: zarith.
Fixpoint pos_length (p : positive) : nat :=
match p with
| xH => 0
| xO p' => S (pos_length p')
| xI p' => S (pos_length p')
end.
intros v r q; generalize r; elim q; clear r q; simpl in |- *.
intros p Rec r Hr; generalize (Zcompare_correct r v); case (r ?= v)%Z; auto.
intros H1 H2; rewrite <- H1.
apply Z.le_lt_trans with (Zpower_nat n 0 * r)%Z; auto with zarith arith.
rewrite Zpower_nat_O; rewrite Zmult_1_l; auto with zarith.
intros H1 H2; rewrite digitAux1.
apply Rec.
apply Zlt_mult_ZERO; auto with zarith.
rewrite <- digitAux1; auto.
rewrite Zpower_nat_O; rewrite Zmult_1_l; auto with zarith.
intros p Rec r Hr; generalize (Zcompare_correct r v); case (r ?= v)%Z; auto.
intros H1 H2; rewrite <- H1.
apply Z.le_lt_trans with (Zpower_nat n 0 * r)%Z; auto with zarith arith.
rewrite Zpower_nat_O; rewrite Zmult_1_l; auto with zarith.
intros H1 H2; rewrite digitAux1.
apply Rec.
apply Zlt_mult_ZERO; auto with zarith.
rewrite <- digitAux1; auto.
rewrite Zpower_nat_O; rewrite Zmult_1_l; auto with zarith.
auto.
Qed.
intros p; elim p; simpl in |- *; auto.
intros p0 H; rewrite Zpos_xI.
apply Z.lt_le_trans with (2 * (n * Zpower_nat n (pos_length p0)))%Z;
auto with zarith.
intros p0 H; rewrite Zpos_xO.
apply Z.lt_le_trans with (2 * (n * Zpower_nat n (pos_length p0)))%Z;
auto with zarith.
auto with zarith.
Qed.
(* digit is correct (second part) *)
intros q; case q.
easy.
intros q'; rewrite <- (Zmult_1_r (Zpower_nat n (digit (Zpos q')))).
unfold digit in |- *; apply digitAuxMore; auto with zarith.
rewrite Zmult_1_r.
simpl in |- *; apply pos_length_pow.
intros q'; rewrite <- (Zmult_1_r (Zpower_nat n (digit (Zneg q')))).
unfold digit in |- *; apply digitAuxMore; auto with zarith.
rewrite Zmult_1_r.
simpl in |- *; apply pos_length_pow.
Qed.
Hint Resolve digitMore: zarith.
(* if we find an r such that n^(r-1) =< q < n^r then r is the number of digits *)
intros q r H' H'0; case (le_or_lt (digit q) r).
intros H'1; case (le_lt_or_eq _ _ H'1); auto; intros H'2.
absurd (Z.abs q < Zpower_nat n (digit q))%Z; auto with zarith.
apply Zle_not_lt; auto with zarith.
apply Z.le_trans with (m := Zpower_nat n (pred r)); auto with zarith.
apply Zpower_nat_monotone_le.
generalize H'2; case r; auto with arith.
intros H'1.
absurd (Zpower_nat n (pred (digit q)) <= Z.abs q)%Z; auto with zarith.
apply Zlt_not_le; auto with zarith.
apply Z.lt_le_trans with (m := Zpower_nat n r); auto.
apply Zpower_nat_monotone_le.
generalize H'1; case (digit q); auto with arith.
apply digitLess; auto with zarith.
generalize H'1; case q; unfold digit in |- *; intros tmp; intros; red in |- *;
intros; try discriminate; Contradict tmp; auto with arith.
Qed.
unfold digit in |- *; simpl in |- *; auto with arith.
Qed.
unfold digit in |- *; simpl in |- *; auto.
Qed.
(* digit is monotone *)
intros p q H; case (le_or_lt (digit p) (digit q)); auto; intros H1;
Contradict H.
apply Zlt_not_le.
cut (p <> 0%Z); [ intros H2 | idtac ].
apply Z.lt_le_trans with (2 := digitLess p H2).
cut (digit q <= pred (digit p)); [ intros H3 | idtac ].
apply Z.lt_le_trans with (2 := Zpower_nat_monotone_le _ _ H3);
auto with zarith.
generalize H1; case (digit p); simpl in |- *; auto with arith.
generalize H1; case p; simpl in |- *; intros tmp; intros; red in |- *; intros;
try discriminate; Contradict tmp; auto with arith.
Qed.
Hint Resolve digit_monotone: arith.
(* if the number is not null so is the number of digits *)
intros q H'.
apply lt_le_trans with (m := digit 1); auto with zarith.
apply digit_monotone.
generalize H'; case q; simpl in |- *; auto with zarith; intros q'; case q';
simpl in |- *; auto with zarith arith; intros; red in |- *;
simpl in |- *; red in |- *; intros; discriminate.
Qed.
Hint Resolve Z.lt_gt: zarith.

Theorem digitAdd :
forall (q : Z) (r : nat),
q <> 0%Z -> digit (q * Zpower_nat n r) = digit q + r.