src/HOL/Num.thy
changeset 47191 ebd8c46d156b
parent 47126 e980b14c347d
child 47192 0c0501cb6da6
equal deleted inserted replaced
47190:e261815d3a38 47191:ebd8c46d156b
     4 *)
     4 *)
     5 
     5 
     6 header {* Binary Numerals *}
     6 header {* Binary Numerals *}
     7 
     7 
     8 theory Num
     8 theory Num
     9 imports Datatype Power
     9 imports Datatype
    10 begin
    10 begin
    11 
    11 
    12 subsection {* The @{text num} type *}
    12 subsection {* The @{text num} type *}
    13 
    13 
    14 datatype num = One | Bit0 num | Bit1 num
    14 datatype num = One | Bit0 num | Bit1 num
   219   "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
   219   "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
   220 
   220 
   221 primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   221 primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   222   "pow x One = x" |
   222   "pow x One = x" |
   223   "pow x (Bit0 y) = sqr (pow x y)" |
   223   "pow x (Bit0 y) = sqr (pow x y)" |
   224   "pow x (Bit1 y) = x * sqr (pow x y)"
   224   "pow x (Bit1 y) = sqr (pow x y) * x"
   225 
   225 
   226 lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x"
   226 lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x"
   227   by (induct x, simp_all add: algebra_simps nat_of_num_add)
   227   by (induct x, simp_all add: algebra_simps nat_of_num_add)
   228 
   228 
   229 lemma sqr_conv_mult: "sqr x = x * x"
   229 lemma sqr_conv_mult: "sqr x = x * x"
   862   "numeral One = Suc 0"
   862   "numeral One = Suc 0"
   863   "numeral (Bit0 n) = Suc (numeral (BitM n))"
   863   "numeral (Bit0 n) = Suc (numeral (BitM n))"
   864   "numeral (Bit1 n) = Suc (numeral (Bit0 n))"
   864   "numeral (Bit1 n) = Suc (numeral (Bit0 n))"
   865   by (simp_all add: numeral.simps BitM_plus_one)
   865   by (simp_all add: numeral.simps BitM_plus_one)
   866 
   866 
   867 subsubsection {*
       
   868   Structures with exponentiation
       
   869 *}
       
   870 
       
   871 context semiring_numeral
       
   872 begin
       
   873 
       
   874 lemma numeral_sqr: "numeral (sqr n) = numeral n * numeral n"
       
   875   by (simp add: sqr_conv_mult numeral_mult)
       
   876 
       
   877 lemma numeral_pow: "numeral (pow m n) = numeral m ^ numeral n"
       
   878   by (induct n, simp_all add: numeral_class.numeral.simps
       
   879     power_add numeral_sqr numeral_mult)
       
   880 
       
   881 lemma power_numeral [simp]: "numeral m ^ numeral n = numeral (pow m n)"
       
   882   by (rule numeral_pow [symmetric])
       
   883 
       
   884 end
       
   885 
       
   886 context semiring_1
       
   887 begin
       
   888 
       
   889 lemma power_zero_numeral [simp]: "(0::'a) ^ numeral n = 0"
       
   890   by (induct n, simp_all add: numeral_class.numeral.simps power_add)
       
   891 
       
   892 end
       
   893 
       
   894 context ring_1
       
   895 begin
       
   896 
       
   897 lemma power_minus_Bit0: "(- x) ^ numeral (Bit0 n) = x ^ numeral (Bit0 n)"
       
   898   by (induct n, simp_all add: numeral_class.numeral.simps power_add)
       
   899 
       
   900 lemma power_minus_Bit1: "(- x) ^ numeral (Bit1 n) = - (x ^ numeral (Bit1 n))"
       
   901   by (simp only: nat_number(4) power_Suc power_minus_Bit0 mult_minus_left)
       
   902 
       
   903 lemma power_neg_numeral_Bit0 [simp]:
       
   904   "neg_numeral m ^ numeral (Bit0 n) = numeral (pow m (Bit0 n))"
       
   905   by (simp only: neg_numeral_def power_minus_Bit0 power_numeral)
       
   906 
       
   907 lemma power_neg_numeral_Bit1 [simp]:
       
   908   "neg_numeral m ^ numeral (Bit1 n) = neg_numeral (pow m (Bit1 n))"
       
   909   by (simp only: neg_numeral_def power_minus_Bit1 power_numeral pow.simps)
       
   910 
       
   911 end
       
   912 
   867 
   913 subsection {* Numeral equations as default simplification rules *}
   868 subsection {* Numeral equations as default simplification rules *}
   914 
   869 
   915 declare (in numeral) numeral_One [simp]
   870 declare (in numeral) numeral_One [simp]
   916 declare (in numeral) numeral_plus_numeral [simp]
   871 declare (in numeral) numeral_plus_numeral [simp]