463 lemma pos_zdiv_mult_2: \<open>(1 + 2 * b) div (2 * a) = b div a\<close> (is ?Q) |
455 lemma pos_zdiv_mult_2: \<open>(1 + 2 * b) div (2 * a) = b div a\<close> (is ?Q) |
464 and pos_zmod_mult_2: \<open>(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)\<close> (is ?R) |
456 and pos_zmod_mult_2: \<open>(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)\<close> (is ?R) |
465 if \<open>0 \<le> a\<close> for a b :: int |
457 if \<open>0 \<le> a\<close> for a b :: int |
466 proof - |
458 proof - |
467 have \<open>((1 + 2 * b) div (2 * a), (1 + 2 * b) mod (2 * a)) = (b div a, 1 + 2 * (b mod a))\<close> |
459 have \<open>((1 + 2 * b) div (2 * a), (1 + 2 * b) mod (2 * a)) = (b div a, 1 + 2 * (b mod a))\<close> |
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') |
460 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) |
469 case by0 |
461 case by0 |
470 then show ?case |
462 then show ?case |
471 by simp |
463 by simp |
472 next |
464 next |
473 case pos |
465 case divides |
474 then have \<open>a > 0\<close> |
466 have \<open>even (2 * a)\<close> |
|
467 by simp |
|
468 then have \<open>even (1 + 2 * b)\<close> |
|
469 using \<open>2 * a dvd 1 + 2 * b\<close> by (rule dvd_trans) |
|
470 then show ?case |
|
471 by simp |
|
472 next |
|
473 case euclidean_relation |
|
474 with that have \<open>a > 0\<close> |
475 by simp |
475 by simp |
476 moreover have \<open>b mod a < a\<close> |
476 moreover have \<open>b mod a < a\<close> |
477 using \<open>a > 0\<close> by simp |
477 using \<open>a > 0\<close> by simp |
478 then have \<open>1 + 2 * (b mod a) < 2 * a\<close> |
478 then have \<open>1 + 2 * (b mod a) < 2 * a\<close> |
479 by simp |
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> |
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) |
481 by (simp only: algebra_simps) |
|
482 moreover have \<open>0 \<le> 2 * (b mod a)\<close> |
|
483 using \<open>a > 0\<close> by simp |
482 ultimately show ?case |
484 ultimately show ?case |
483 by (simp add: algebra_simps) |
485 by (simp add: algebra_simps) |
484 next |
|
485 case neg |
|
486 with that show ?case |
|
487 by simp |
|
488 qed |
486 qed |
489 then show ?Q and ?R |
487 then show ?Q and ?R |
490 by simp_all |
488 by simp_all |
491 qed |
489 qed |
492 |
490 |
493 lemma neg_zdiv_mult_2: \<open>(1 + 2 * b) div (2 * a) = (b + 1) div a\<close> (is ?Q) |
491 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) |
492 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 |
493 if \<open>a \<le> 0\<close> for a b :: int |
496 proof - |
494 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> |
495 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') |
496 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 |
497 case by0 |
500 then show ?case |
498 then show ?case |
501 by simp |
499 by simp |
502 next |
500 next |
503 case pos |
501 case divides |
504 with that show ?case |
502 have \<open>even (2 * a)\<close> |
|
503 by simp |
|
504 then have \<open>even (1 + 2 * b)\<close> |
|
505 using \<open>2 * a dvd 1 + 2 * b\<close> by (rule dvd_trans) |
|
506 then show ?case |
505 by simp |
507 by simp |
506 next |
508 next |
507 case neg |
509 case euclidean_relation |
508 then have \<open>a < 0\<close> |
510 with that have \<open>a < 0\<close> |
509 by simp |
511 by simp |
510 moreover have \<open>(b + 1) mod a > a\<close> |
512 moreover have \<open>(b + 1) mod a > a\<close> |
511 using \<open>a < 0\<close> by simp |
513 using \<open>a < 0\<close> by simp |
512 then have \<open>2 * ((b + 1) mod a) > 1 + 2 * a\<close> |
514 then have \<open>2 * ((b + 1) mod a) > 1 + 2 * a\<close> |
513 by simp |
515 by simp |