src/HOL/Divides.thy
changeset 76120 3ae579092045
parent 76106 98cab94326d4
child 76141 e7497a1de8b9
equal deleted inserted replaced
76119:e5cfb05d312e 76120:3ae579092045
   425   with \<open>\<bar>r\<bar> < \<bar>l\<bar>\<close> \<open>k = q * l + r\<close>
   425   with \<open>\<bar>r\<bar> < \<bar>l\<bar>\<close> \<open>k = q * l + r\<close>
   426   show ?case
   426   show ?case
   427     by simp
   427     by simp
   428 qed
   428 qed
   429 
   429 
   430 lemma euclidean_relation_intI' [case_names by0 pos neg]:
       
   431   \<open>(k div l, k mod l) = (q, r)\<close>
       
   432     if \<open>l = 0 \<Longrightarrow> q = 0 \<and> r = k\<close>
       
   433     and \<open>l > 0 \<Longrightarrow> 0 \<le> r \<and> r < l \<and> k = q * l + r\<close>
       
   434     and \<open>l < 0 \<Longrightarrow> l < r \<and> r \<le> 0 \<and> k = q * l + r\<close> for k l q r :: int
       
   435   by (cases l \<open>0::int\<close> rule: linorder_cases)
       
   436     (auto dest: that)
       
   437 
       
   438 
   430 
   439 subsubsection \<open>Computing \<open>div\<close> and \<open>mod\<close> with shifting\<close>
   431 subsubsection \<open>Computing \<open>div\<close> and \<open>mod\<close> with shifting\<close>
   440 
   432 
   441 lemma div_pos_geq:
   433 lemma div_pos_geq:
   442   fixes k l :: int
   434   fixes k l :: int
   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
   517       by simp
   519       by simp
   518     moreover have \<open>2 * ((1 + b) mod a) + a * (2 * ((1 + b) div a)) =
   520     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>
   521       2 * ((1 + b) div a * a + (1 + b) mod a)\<close>
   520       by (simp only: algebra_simps)
   522       by (simp only: algebra_simps)
   521     ultimately show ?case
   523     ultimately show ?case
   522       by (simp add: algebra_simps)
   524       by (simp add: algebra_simps sgn_mult abs_mult)
   523   qed
   525   qed
   524   then show ?Q and ?R
   526   then show ?Q and ?R
   525     by simp_all
   527     by simp_all
   526 qed
   528 qed
   527 
   529