(* *)
(**************************************************************************)
-set "baseuri" "cic:/matita/nat/div_and_mod".
-
include "datatypes/constructors.ma".
include "nat/minus.ma".
-
let rec mod_aux p m n: nat \def
match (leb m n) with
[ true \Rightarrow m
apply div_aux_mod_aux.
qed.
+theorem eq_times_div_minus_mod:
+\forall a,b:nat. O \lt b \to
+(a /b)*b = a - (a \mod b).
+intros.
+rewrite > (div_mod a b) in \vdash (? ? ? (? % ?))
+[ apply (minus_plus_m_m (times (div a b) b) (mod a b))
+| assumption
+]
+qed.
+
inductive div_mod_spec (n,m,q,r:nat) : Prop \def
div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
|apply div_mod_spec_intro[assumption|reflexivity]
]
qed.
+
(* some properties of div and mod *)
theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
intros.
]
qed.
+(*a simple variant of div_times theorem *)
+theorem lt_O_to_div_times: \forall a,b:nat. O \lt b \to
+a*b/b = a.
+intros.
+rewrite > sym_times.
+rewrite > (S_pred b H).
+apply div_times.
+qed.
+
theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
intros.
apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
assumption.reflexivity.
qed.
+theorem mod_SO: \forall n:nat. mod n (S O) = O.
+intro.
+apply sym_eq.
+apply le_n_O_to_eq.
+apply le_S_S_to_le.
+apply lt_mod_m_m.
+apply le_n.
+qed.
+
+theorem div_SO: \forall n:nat. div n (S O) = n.
+intro.
+rewrite > (div_mod ? (S O)) in \vdash (? ? ? %)
+ [rewrite > mod_SO.
+ rewrite < plus_n_O.
+ apply times_n_SO
+ |apply le_n
+ ]
+qed.
+
+theorem or_div_mod: \forall n,q. O < q \to
+((S (n \mod q)=q) \land S n = (S (div n q)) * q \lor
+((S (n \mod q)<q) \land S n= (div n q) * q + S (n\mod q))).
+intros.
+elim (le_to_or_lt_eq ? ? (lt_mod_m_m n q H))
+ [right.split
+ [assumption
+ |rewrite < plus_n_Sm.
+ apply eq_f.
+ apply div_mod.
+ assumption
+ ]
+ |left.split
+ [assumption
+ |simplify.
+ rewrite > sym_plus.
+ rewrite < H1 in ⊢ (? ? ? (? ? %)).
+ rewrite < plus_n_Sm.
+ apply eq_f.
+ apply div_mod.
+ assumption
+ ]
+ ]
+qed.
+
(* injectivity *)
theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
\def lt_O_to_injective_times_l.
+
(* n_divides computes the pair (div,mod) *)
(* p is just an upper bound, acc is an accumulator *)
(* n_divides n m = <q,r> if m divides n q times, with remainder r *)
definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.
-
-(*a simple variant of div_times theorem *)
-theorem div_times_ltO: \forall a,b:nat. O \lt b \to
-a*b/b = a.
-intros.
-rewrite > sym_times.
-rewrite > (S_pred b H).
-apply div_times.
-qed.
-