487 "[| a < 0; b \<le> 0 |] ==> a mod b = snd (negateSnd (posDivAlg (-a) (-b)))" |
487 "[| a < 0; b \<le> 0 |] ==> a mod b = snd (negateSnd (posDivAlg (-a) (-b)))" |
488 by (simp add: mod_def divAlg_def) |
488 by (simp add: mod_def divAlg_def) |
489 |
489 |
490 text {*Simplify expresions in which div and mod combine numerical constants*} |
490 text {*Simplify expresions in which div and mod combine numerical constants*} |
491 |
491 |
492 lemmas div_pos_pos_number_of [simp] = |
492 lemma quoremI: |
|
493 "\<lbrakk>a == b * q + r; if 0 < b then 0 \<le> r \<and> r < b else b < r \<and> r \<le> 0\<rbrakk> |
|
494 \<Longrightarrow> quorem ((a, b), (q, r))" |
|
495 unfolding quorem_def by simp |
|
496 |
|
497 lemmas quorem_div_eq = quoremI [THEN quorem_div, THEN eq_reflection] |
|
498 lemmas quorem_mod_eq = quoremI [THEN quorem_mod, THEN eq_reflection] |
|
499 lemmas arithmetic_simps = |
|
500 arith_simps |
|
501 add_special |
|
502 OrderedGroup.add_0_left |
|
503 OrderedGroup.add_0_right |
|
504 mult_zero_left |
|
505 mult_zero_right |
|
506 mult_1_left |
|
507 mult_1_right |
|
508 |
|
509 (* simprocs adapted from HOL/ex/Binary.thy *) |
|
510 ML {* |
|
511 local |
|
512 infix ==; |
|
513 val op == = Logic.mk_equals; |
|
514 fun plus m n = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"} $ m $ n; |
|
515 fun mult m n = @{term "times :: int \<Rightarrow> int \<Rightarrow> int"} $ m $ n; |
|
516 |
|
517 val binary_ss = HOL_basic_ss addsimps @{thms arithmetic_simps}; |
|
518 fun prove ctxt prop = |
|
519 Goal.prove ctxt [] [] prop (fn _ => ALLGOALS (full_simp_tac binary_ss)); |
|
520 |
|
521 fun binary_proc proc ss ct = |
|
522 (case Thm.term_of ct of |
|
523 _ $ t $ u => |
|
524 (case try (pairself (`(snd o HOLogic.dest_number))) (t, u) of |
|
525 SOME args => proc (Simplifier.the_context ss) args |
|
526 | NONE => NONE) |
|
527 | _ => NONE); |
|
528 in |
|
529 |
|
530 fun divmod_proc rule = binary_proc (fn ctxt => fn ((m, t), (n, u)) => |
|
531 if n = 0 then NONE |
|
532 else |
|
533 let val (k, l) = IntInf.divMod (m, n); |
|
534 fun mk_num x = HOLogic.mk_number HOLogic.intT x; |
|
535 in SOME (rule OF [prove ctxt (t == plus (mult u (mk_num k)) (mk_num l))]) |
|
536 end); |
|
537 |
|
538 end; |
|
539 *} |
|
540 |
|
541 simproc_setup binary_int_div ("number_of m div number_of n :: int") = |
|
542 {* K (divmod_proc (@{thm quorem_div_eq})) *} |
|
543 |
|
544 simproc_setup binary_int_mod ("number_of m mod number_of n :: int") = |
|
545 {* K (divmod_proc (@{thm quorem_mod_eq})) *} |
|
546 |
|
547 (* The following 8 lemmas are made unnecessary by the above simprocs: *) |
|
548 |
|
549 lemmas div_pos_pos_number_of = |
493 div_pos_pos [of "number_of v" "number_of w", standard] |
550 div_pos_pos [of "number_of v" "number_of w", standard] |
494 |
551 |
495 lemmas div_neg_pos_number_of [simp] = |
552 lemmas div_neg_pos_number_of = |
496 div_neg_pos [of "number_of v" "number_of w", standard] |
553 div_neg_pos [of "number_of v" "number_of w", standard] |
497 |
554 |
498 lemmas div_pos_neg_number_of [simp] = |
555 lemmas div_pos_neg_number_of = |
499 div_pos_neg [of "number_of v" "number_of w", standard] |
556 div_pos_neg [of "number_of v" "number_of w", standard] |
500 |
557 |
501 lemmas div_neg_neg_number_of [simp] = |
558 lemmas div_neg_neg_number_of = |
502 div_neg_neg [of "number_of v" "number_of w", standard] |
559 div_neg_neg [of "number_of v" "number_of w", standard] |
503 |
560 |
504 |
561 |
505 lemmas mod_pos_pos_number_of [simp] = |
562 lemmas mod_pos_pos_number_of = |
506 mod_pos_pos [of "number_of v" "number_of w", standard] |
563 mod_pos_pos [of "number_of v" "number_of w", standard] |
507 |
564 |
508 lemmas mod_neg_pos_number_of [simp] = |
565 lemmas mod_neg_pos_number_of = |
509 mod_neg_pos [of "number_of v" "number_of w", standard] |
566 mod_neg_pos [of "number_of v" "number_of w", standard] |
510 |
567 |
511 lemmas mod_pos_neg_number_of [simp] = |
568 lemmas mod_pos_neg_number_of = |
512 mod_pos_neg [of "number_of v" "number_of w", standard] |
569 mod_pos_neg [of "number_of v" "number_of w", standard] |
513 |
570 |
514 lemmas mod_neg_neg_number_of [simp] = |
571 lemmas mod_neg_neg_number_of = |
515 mod_neg_neg [of "number_of v" "number_of w", standard] |
572 mod_neg_neg [of "number_of v" "number_of w", standard] |
516 |
573 |
517 |
574 |
518 lemmas posDivAlg_eqn_number_of [simp] = |
575 lemmas posDivAlg_eqn_number_of [simp] = |
519 posDivAlg_eqn [of "number_of v" "number_of w", standard] |
576 posDivAlg_eqn [of "number_of v" "number_of w", standard] |