src/HOL/IntDiv.thy
changeset 30652 752329615264
parent 30517 51a39ed24c0f
child 30930 11010e5f18f0
equal deleted inserted replaced
30648:17365ef082f3 30652:752329615264
     6 
     6 
     7 header{* The Division Operators div and mod *}
     7 header{* The Division Operators div and mod *}
     8 
     8 
     9 theory IntDiv
     9 theory IntDiv
    10 imports Int Divides FunDef
    10 imports Int Divides FunDef
       
    11 uses
       
    12   "~~/src/Provers/Arith/cancel_numeral_factor.ML"
       
    13   "~~/src/Provers/Arith/extract_common_term.ML"
       
    14   ("Tools/int_factor_simprocs.ML")
    11 begin
    15 begin
    12 
    16 
    13 definition divmod_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
    17 definition divmod_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
    14     --{*definition of quotient and remainder*}
    18     --{*definition of quotient and remainder*}
    15     [code]: "divmod_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
    19     [code]: "divmod_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
   918   assume "a=0"
   922   assume "a=0"
   919     thus ?thesis by simp
   923     thus ?thesis by simp
   920 next
   924 next
   921   assume "a\<noteq>0" and le_a: "0\<le>a"   
   925   assume "a\<noteq>0" and le_a: "0\<le>a"   
   922   hence a_pos: "1 \<le> a" by arith
   926   hence a_pos: "1 \<le> a" by arith
   923   hence one_less_a2: "1 < 2*a" by arith
   927   hence one_less_a2: "1 < 2 * a" by arith
   924   hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
   928   hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
   925     by (simp add: mult_le_cancel_left add_commute [of 1] add1_zle_eq)
   929     unfolding mult_le_cancel_left
       
   930     by (simp add: add1_zle_eq add_commute [of 1])
   926   with a_pos have "0 \<le> b mod a" by simp
   931   with a_pos have "0 \<le> b mod a" by simp
   927   hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
   932   hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
   928     by (simp add: mod_pos_pos_trivial one_less_a2)
   933     by (simp add: mod_pos_pos_trivial one_less_a2)
   929   with  le_2a
   934   with  le_2a
   930   have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
   935   have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
  1355   hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
  1360   hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
  1356   thus  ?lhs by simp
  1361   thus  ?lhs by simp
  1357 qed
  1362 qed
  1358 
  1363 
  1359 
  1364 
       
  1365 subsection {* Simproc setup *}
       
  1366 
       
  1367 use "Tools/int_factor_simprocs.ML"
       
  1368 
       
  1369 
  1360 subsection {* Code generation *}
  1370 subsection {* Code generation *}
  1361 
  1371 
  1362 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1372 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1363   "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  1373   "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  1364 
  1374