400 qed |
400 qed |
401 |
401 |
402 |
402 |
403 subsubsection \<open>Uniqueness rules\<close> |
403 subsubsection \<open>Uniqueness rules\<close> |
404 |
404 |
405 inductive eucl_rel_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" |
405 lemma euclidean_relation_intI [case_names by0 divides euclidean_relation]: |
406 where eucl_rel_int_by0: "eucl_rel_int k 0 (0, k)" |
406 \<open>(k div l, k mod l) = (q, r)\<close> |
407 | eucl_rel_int_dividesI: "l \<noteq> 0 \<Longrightarrow> k = q * l \<Longrightarrow> eucl_rel_int k l (q, 0)" |
407 if by0': \<open>l = 0 \<Longrightarrow> q = 0 \<and> r = k\<close> |
408 | eucl_rel_int_remainderI: "sgn r = sgn l \<Longrightarrow> \<bar>r\<bar> < \<bar>l\<bar> |
408 and divides': \<open>l \<noteq> 0 \<Longrightarrow> l dvd k \<Longrightarrow> r = 0 \<and> k = q * l\<close> |
409 \<Longrightarrow> k = q * l + r \<Longrightarrow> eucl_rel_int k l (q, r)" |
409 and euclidean_relation': \<open>l \<noteq> 0 \<Longrightarrow> \<not> l dvd k \<Longrightarrow> sgn r = sgn l |
410 |
410 \<and> \<bar>r\<bar> < \<bar>l\<bar> \<and> k = q * l + r\<close> for k l :: int |
411 lemma eucl_rel_int_iff: |
411 proof (cases l q r k rule: euclidean_relationI) |
412 "eucl_rel_int k l (q, r) \<longleftrightarrow> |
412 case by0 |
413 k = l * q + r \<and> |
413 then show ?case |
414 (if 0 < l then 0 \<le> r \<and> r < l else if l < 0 then l < r \<and> r \<le> 0 else q = 0)" |
414 by (rule by0') |
415 by (cases "r = 0") |
|
416 (auto elim!: eucl_rel_int.cases intro: eucl_rel_int_by0 eucl_rel_int_dividesI eucl_rel_int_remainderI |
|
417 simp add: ac_simps sgn_1_pos sgn_1_neg) |
|
418 |
|
419 lemma eucl_rel_int: |
|
420 "eucl_rel_int k l (k div l, k mod l)" |
|
421 proof (cases k rule: int_cases3) |
|
422 case zero |
|
423 then show ?thesis |
|
424 by (simp add: eucl_rel_int_iff divide_int_def modulo_int_def) |
|
425 next |
415 next |
426 case (pos n) |
416 case divides |
427 then show ?thesis |
417 then show ?case |
428 using div_mult_mod_eq [of n] |
418 by (rule divides') |
429 by (cases l rule: int_cases3) |
|
430 (auto simp del: of_nat_mult of_nat_add |
|
431 simp add: mod_greater_zero_iff_not_dvd of_nat_mult [symmetric] of_nat_add [symmetric] algebra_simps |
|
432 eucl_rel_int_iff divide_int_def modulo_int_def) |
|
433 next |
419 next |
434 case (neg n) |
420 case euclidean_relation |
435 then show ?thesis |
421 with euclidean_relation' have \<open>sgn r = sgn l\<close> \<open>\<bar>r\<bar> < \<bar>l\<bar>\<close> \<open>k = q * l + r\<close> |
436 using div_mult_mod_eq [of n] |
422 by simp_all |
437 by (cases l rule: int_cases3) |
423 from \<open>sgn r = sgn l\<close> \<open>l \<noteq> 0\<close> have \<open>division_segment r = division_segment l\<close> |
438 (auto simp del: of_nat_mult of_nat_add |
424 by (simp add: division_segment_int_def sgn_if split: if_splits) |
439 simp add: mod_greater_zero_iff_not_dvd of_nat_mult [symmetric] of_nat_add [symmetric] algebra_simps |
425 with \<open>\<bar>r\<bar> < \<bar>l\<bar>\<close> \<open>k = q * l + r\<close> |
440 eucl_rel_int_iff divide_int_def modulo_int_def) |
426 show ?case |
441 qed |
427 by simp |
442 |
428 qed |
443 lemma unique_quotient: |
429 |
444 \<open>q = q'\<close> if \<open>eucl_rel_int a b (q, r)\<close> \<open>eucl_rel_int a b (q', r')\<close> |
430 lemma euclidean_relation_intI' [case_names by0 pos neg]: |
445 apply (rule order_antisym) |
431 \<open>(k div l, k mod l) = (q, r)\<close> |
446 using that |
432 if \<open>l = 0 \<Longrightarrow> q = 0 \<and> r = k\<close> |
447 apply (simp_all add: eucl_rel_int_iff linorder_neq_iff split: if_split_asm) |
433 and \<open>l > 0 \<Longrightarrow> 0 \<le> r \<and> r < l \<and> k = q * l + r\<close> |
448 apply (blast intro: order_eq_refl [THEN unique_quotient_lemma] order_eq_refl [THEN unique_quotient_lemma_neg] sym)+ |
434 and \<open>l < 0 \<Longrightarrow> l < r \<and> r \<le> 0 \<and> k = q * l + r\<close> for k l q r :: int |
449 done |
435 by (cases l \<open>0::int\<close> rule: linorder_cases) |
450 |
436 (auto dest: that) |
451 lemma unique_remainder: |
|
452 \<open>r = r'\<close> if \<open>eucl_rel_int a b (q, r)\<close> \<open>eucl_rel_int a b (q', r')\<close> |
|
453 proof - |
|
454 have \<open>q = q'\<close> |
|
455 using that by (blast intro: unique_quotient) |
|
456 then show ?thesis |
|
457 using that by (simp add: eucl_rel_int_iff) |
|
458 qed |
|
459 |
|
460 lemma divmod_int_unique: |
|
461 assumes \<open>eucl_rel_int k l (q, r)\<close> |
|
462 shows div_int_unique: \<open>k div l = q\<close> and mod_int_unique: \<open>k mod l = r\<close> |
|
463 using assms eucl_rel_int [of k l] |
|
464 using unique_quotient [of k l] unique_remainder [of k l] |
|
465 by auto |
|
466 |
437 |
467 |
438 |
468 subsubsection \<open>Computing \<open>div\<close> and \<open>mod\<close> with shifting\<close> |
439 subsubsection \<open>Computing \<open>div\<close> and \<open>mod\<close> with shifting\<close> |
469 |
440 |
470 lemma div_pos_geq: |
441 lemma div_pos_geq: |
485 have "k = (k - l) + l" by simp |
456 have "k = (k - l) + l" by simp |
486 then obtain j where k: "k = j + l" .. |
457 then obtain j where k: "k = j + l" .. |
487 with assms show ?thesis by simp |
458 with assms show ?thesis by simp |
488 qed |
459 qed |
489 |
460 |
490 lemma pos_eucl_rel_int_mult_2: |
|
491 assumes "0 \<le> b" |
|
492 assumes "eucl_rel_int a b (q, r)" |
|
493 shows "eucl_rel_int (1 + 2*a) (2*b) (q, 1 + 2*r)" |
|
494 using assms unfolding eucl_rel_int_iff by auto |
|
495 |
|
496 lemma neg_eucl_rel_int_mult_2: |
|
497 assumes "b \<le> 0" |
|
498 assumes "eucl_rel_int (a + 1) b (q, r)" |
|
499 shows "eucl_rel_int (1 + 2*a) (2*b) (q, 2*r - 1)" |
|
500 using assms unfolding eucl_rel_int_iff by auto |
|
501 |
|
502 text\<open>computing div by shifting\<close> |
461 text\<open>computing div by shifting\<close> |
503 |
462 |
504 lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a" |
463 lemma pos_zdiv_mult_2: \<open>(1 + 2 * b) div (2 * a) = b div a\<close> (is ?Q) |
505 using pos_eucl_rel_int_mult_2 [OF _ eucl_rel_int] |
464 and pos_zmod_mult_2: \<open>(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)\<close> (is ?R) |
506 by (rule div_int_unique) |
465 if \<open>0 \<le> a\<close> for a b :: int |
507 |
466 proof - |
508 lemma neg_zdiv_mult_2: |
467 have \<open>((1 + 2 * b) div (2 * a), (1 + 2 * b) mod (2 * a)) = (b div a, 1 + 2 * (b mod a))\<close> |
509 assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a" |
468 proof (cases \<open>2 * a\<close> \<open>b div a\<close> \<open>1 + 2 * (b mod a)\<close> \<open>1 + 2 * b\<close> rule: euclidean_relation_intI') |
510 using neg_eucl_rel_int_mult_2 [OF A eucl_rel_int] |
469 case by0 |
511 by (rule div_int_unique) |
470 then show ?case |
|
471 by simp |
|
472 next |
|
473 case pos |
|
474 then have \<open>a > 0\<close> |
|
475 by simp |
|
476 moreover have \<open>b mod a < a\<close> |
|
477 using \<open>a > 0\<close> by simp |
|
478 then have \<open>1 + 2 * (b mod a) < 2 * a\<close> |
|
479 by simp |
|
480 moreover have \<open>2 * (b mod a) + a * (2 * (b div a)) = 2 * (b div a * a + b mod a)\<close> |
|
481 by (simp only: algebra_simps) |
|
482 ultimately show ?case |
|
483 by (simp add: algebra_simps) |
|
484 next |
|
485 case neg |
|
486 with that show ?case |
|
487 by simp |
|
488 qed |
|
489 then show ?Q and ?R |
|
490 by simp_all |
|
491 qed |
|
492 |
|
493 lemma neg_zdiv_mult_2: \<open>(1 + 2 * b) div (2 * a) = (b + 1) div a\<close> (is ?Q) |
|
494 and neg_zmod_mult_2: \<open>(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1\<close> (is ?R) |
|
495 if \<open>a \<le> 0\<close> for a b :: int |
|
496 proof - |
|
497 have \<open>((1 + 2 * b) div (2 * a), (1 + 2 * b) mod (2 * a)) = ((b + 1) div a, 2 * ((b + 1) mod a) - 1)\<close> |
|
498 proof (cases \<open>2 * a\<close> \<open>(b + 1) div a\<close> \<open>2 * ((b + 1) mod a) - 1\<close> \<open>1 + 2 * b\<close> rule: euclidean_relation_intI') |
|
499 case by0 |
|
500 then show ?case |
|
501 by simp |
|
502 next |
|
503 case pos |
|
504 with that show ?case |
|
505 by simp |
|
506 next |
|
507 case neg |
|
508 then have \<open>a < 0\<close> |
|
509 by simp |
|
510 moreover have \<open>(b + 1) mod a > a\<close> |
|
511 using \<open>a < 0\<close> by simp |
|
512 then have \<open>2 * ((b + 1) mod a) > 1 + 2 * a\<close> |
|
513 by simp |
|
514 moreover have \<open>((1 + b) mod a) \<le> 0\<close> |
|
515 using \<open>a < 0\<close> by simp |
|
516 then have \<open>2 * ((1 + b) mod a) \<le> 0\<close> |
|
517 by simp |
|
518 moreover have \<open>2 * ((1 + b) mod a) + a * (2 * ((1 + b) div a)) = |
|
519 2 * ((1 + b) div a * a + (1 + b) mod a)\<close> |
|
520 by (simp only: algebra_simps) |
|
521 ultimately show ?case |
|
522 by (simp add: algebra_simps) |
|
523 qed |
|
524 then show ?Q and ?R |
|
525 by simp_all |
|
526 qed |
512 |
527 |
513 lemma zdiv_numeral_Bit0 [simp]: |
528 lemma zdiv_numeral_Bit0 [simp]: |
514 "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) = |
529 "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) = |
515 numeral v div (numeral w :: int)" |
530 numeral v div (numeral w :: int)" |
516 unfolding numeral.simps unfolding mult_2 [symmetric] |
531 unfolding numeral.simps unfolding mult_2 [symmetric] |
520 "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) = |
535 "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) = |
521 (numeral v div (numeral w :: int))" |
536 (numeral v div (numeral w :: int))" |
522 unfolding numeral.simps |
537 unfolding numeral.simps |
523 unfolding mult_2 [symmetric] add.commute [of _ 1] |
538 unfolding mult_2 [symmetric] add.commute [of _ 1] |
524 by (rule pos_zdiv_mult_2, simp) |
539 by (rule pos_zdiv_mult_2, simp) |
525 |
|
526 lemma pos_zmod_mult_2: |
|
527 fixes a b :: int |
|
528 assumes "0 \<le> a" |
|
529 shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)" |
|
530 using pos_eucl_rel_int_mult_2 [OF assms eucl_rel_int] |
|
531 by (rule mod_int_unique) |
|
532 |
|
533 lemma neg_zmod_mult_2: |
|
534 fixes a b :: int |
|
535 assumes "a \<le> 0" |
|
536 shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1" |
|
537 using neg_eucl_rel_int_mult_2 [OF assms eucl_rel_int] |
|
538 by (rule mod_int_unique) |
|
539 |
540 |
540 lemma zmod_numeral_Bit0 [simp]: |
541 lemma zmod_numeral_Bit0 [simp]: |
541 "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) = |
542 "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) = |
542 (2::int) * (numeral v mod numeral w)" |
543 (2::int) * (numeral v mod numeral w)" |
543 unfolding numeral_Bit0 [of v] numeral_Bit0 [of w] |
544 unfolding numeral_Bit0 [of v] numeral_Bit0 [of w] |