src/HOL/Num.thy
author hoelzl
Tue Nov 05 09:44:57 2013 +0100 (2013-11-05)
changeset 54257 5c7a3b6b05a9
parent 54249 ce00f2fef556
child 54489 03ff4d1e6784
permissions -rw-r--r--
generalize SUP and INF to the syntactic type classes Sup and Inf
     1 (*  Title:      HOL/Num.thy
     2     Author:     Florian Haftmann
     3     Author:     Brian Huffman
     4 *)
     5 
     6 header {* Binary Numerals *}
     7 
     8 theory Num
     9 imports Datatype
    10 begin
    11 
    12 subsection {* The @{text num} type *}
    13 
    14 datatype num = One | Bit0 num | Bit1 num
    15 
    16 text {* Increment function for type @{typ num} *}
    17 
    18 primrec inc :: "num \<Rightarrow> num" where
    19   "inc One = Bit0 One" |
    20   "inc (Bit0 x) = Bit1 x" |
    21   "inc (Bit1 x) = Bit0 (inc x)"
    22 
    23 text {* Converting between type @{typ num} and type @{typ nat} *}
    24 
    25 primrec nat_of_num :: "num \<Rightarrow> nat" where
    26   "nat_of_num One = Suc 0" |
    27   "nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x" |
    28   "nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)"
    29 
    30 primrec num_of_nat :: "nat \<Rightarrow> num" where
    31   "num_of_nat 0 = One" |
    32   "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
    33 
    34 lemma nat_of_num_pos: "0 < nat_of_num x"
    35   by (induct x) simp_all
    36 
    37 lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0"
    38   by (induct x) simp_all
    39 
    40 lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
    41   by (induct x) simp_all
    42 
    43 lemma num_of_nat_double:
    44   "0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)"
    45   by (induct n) simp_all
    46 
    47 text {*
    48   Type @{typ num} is isomorphic to the strictly positive
    49   natural numbers.
    50 *}
    51 
    52 lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
    53   by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
    54 
    55 lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
    56   by (induct n) (simp_all add: nat_of_num_inc)
    57 
    58 lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
    59   apply safe
    60   apply (drule arg_cong [where f=num_of_nat])
    61   apply (simp add: nat_of_num_inverse)
    62   done
    63 
    64 lemma num_induct [case_names One inc]:
    65   fixes P :: "num \<Rightarrow> bool"
    66   assumes One: "P One"
    67     and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
    68   shows "P x"
    69 proof -
    70   obtain n where n: "Suc n = nat_of_num x"
    71     by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
    72   have "P (num_of_nat (Suc n))"
    73   proof (induct n)
    74     case 0 show ?case using One by simp
    75   next
    76     case (Suc n)
    77     then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
    78     then show "P (num_of_nat (Suc (Suc n)))" by simp
    79   qed
    80   with n show "P x"
    81     by (simp add: nat_of_num_inverse)
    82 qed
    83 
    84 text {*
    85   From now on, there are two possible models for @{typ num}:
    86   as positive naturals (rule @{text "num_induct"})
    87   and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}).
    88 *}
    89 
    90 
    91 subsection {* Numeral operations *}
    92 
    93 instantiation num :: "{plus,times,linorder}"
    94 begin
    95 
    96 definition [code del]:
    97   "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
    98 
    99 definition [code del]:
   100   "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
   101 
   102 definition [code del]:
   103   "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
   104 
   105 definition [code del]:
   106   "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
   107 
   108 instance
   109   by (default, auto simp add: less_num_def less_eq_num_def num_eq_iff)
   110 
   111 end
   112 
   113 lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
   114   unfolding plus_num_def
   115   by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
   116 
   117 lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
   118   unfolding times_num_def
   119   by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
   120 
   121 lemma add_num_simps [simp, code]:
   122   "One + One = Bit0 One"
   123   "One + Bit0 n = Bit1 n"
   124   "One + Bit1 n = Bit0 (n + One)"
   125   "Bit0 m + One = Bit1 m"
   126   "Bit0 m + Bit0 n = Bit0 (m + n)"
   127   "Bit0 m + Bit1 n = Bit1 (m + n)"
   128   "Bit1 m + One = Bit0 (m + One)"
   129   "Bit1 m + Bit0 n = Bit1 (m + n)"
   130   "Bit1 m + Bit1 n = Bit0 (m + n + One)"
   131   by (simp_all add: num_eq_iff nat_of_num_add)
   132 
   133 lemma mult_num_simps [simp, code]:
   134   "m * One = m"
   135   "One * n = n"
   136   "Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))"
   137   "Bit0 m * Bit1 n = Bit0 (m * Bit1 n)"
   138   "Bit1 m * Bit0 n = Bit0 (Bit1 m * n)"
   139   "Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))"
   140   by (simp_all add: num_eq_iff nat_of_num_add
   141     nat_of_num_mult distrib_right distrib_left)
   142 
   143 lemma eq_num_simps:
   144   "One = One \<longleftrightarrow> True"
   145   "One = Bit0 n \<longleftrightarrow> False"
   146   "One = Bit1 n \<longleftrightarrow> False"
   147   "Bit0 m = One \<longleftrightarrow> False"
   148   "Bit1 m = One \<longleftrightarrow> False"
   149   "Bit0 m = Bit0 n \<longleftrightarrow> m = n"
   150   "Bit0 m = Bit1 n \<longleftrightarrow> False"
   151   "Bit1 m = Bit0 n \<longleftrightarrow> False"
   152   "Bit1 m = Bit1 n \<longleftrightarrow> m = n"
   153   by simp_all
   154 
   155 lemma le_num_simps [simp, code]:
   156   "One \<le> n \<longleftrightarrow> True"
   157   "Bit0 m \<le> One \<longleftrightarrow> False"
   158   "Bit1 m \<le> One \<longleftrightarrow> False"
   159   "Bit0 m \<le> Bit0 n \<longleftrightarrow> m \<le> n"
   160   "Bit0 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"
   161   "Bit1 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"
   162   "Bit1 m \<le> Bit0 n \<longleftrightarrow> m < n"
   163   using nat_of_num_pos [of n] nat_of_num_pos [of m]
   164   by (auto simp add: less_eq_num_def less_num_def)
   165 
   166 lemma less_num_simps [simp, code]:
   167   "m < One \<longleftrightarrow> False"
   168   "One < Bit0 n \<longleftrightarrow> True"
   169   "One < Bit1 n \<longleftrightarrow> True"
   170   "Bit0 m < Bit0 n \<longleftrightarrow> m < n"
   171   "Bit0 m < Bit1 n \<longleftrightarrow> m \<le> n"
   172   "Bit1 m < Bit1 n \<longleftrightarrow> m < n"
   173   "Bit1 m < Bit0 n \<longleftrightarrow> m < n"
   174   using nat_of_num_pos [of n] nat_of_num_pos [of m]
   175   by (auto simp add: less_eq_num_def less_num_def)
   176 
   177 text {* Rules using @{text One} and @{text inc} as constructors *}
   178 
   179 lemma add_One: "x + One = inc x"
   180   by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   181 
   182 lemma add_One_commute: "One + n = n + One"
   183   by (induct n) simp_all
   184 
   185 lemma add_inc: "x + inc y = inc (x + y)"
   186   by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   187 
   188 lemma mult_inc: "x * inc y = x * y + x"
   189   by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
   190 
   191 text {* The @{const num_of_nat} conversion *}
   192 
   193 lemma num_of_nat_One:
   194   "n \<le> 1 \<Longrightarrow> num_of_nat n = One"
   195   by (cases n) simp_all
   196 
   197 lemma num_of_nat_plus_distrib:
   198   "0 < m \<Longrightarrow> 0 < n \<Longrightarrow> num_of_nat (m + n) = num_of_nat m + num_of_nat n"
   199   by (induct n) (auto simp add: add_One add_One_commute add_inc)
   200 
   201 text {* A double-and-decrement function *}
   202 
   203 primrec BitM :: "num \<Rightarrow> num" where
   204   "BitM One = One" |
   205   "BitM (Bit0 n) = Bit1 (BitM n)" |
   206   "BitM (Bit1 n) = Bit1 (Bit0 n)"
   207 
   208 lemma BitM_plus_one: "BitM n + One = Bit0 n"
   209   by (induct n) simp_all
   210 
   211 lemma one_plus_BitM: "One + BitM n = Bit0 n"
   212   unfolding add_One_commute BitM_plus_one ..
   213 
   214 text {* Squaring and exponentiation *}
   215 
   216 primrec sqr :: "num \<Rightarrow> num" where
   217   "sqr One = One" |
   218   "sqr (Bit0 n) = Bit0 (Bit0 (sqr n))" |
   219   "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
   220 
   221 primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   222   "pow x One = x" |
   223   "pow x (Bit0 y) = sqr (pow x y)" |
   224   "pow x (Bit1 y) = sqr (pow x y) * x"
   225 
   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)
   228 
   229 lemma sqr_conv_mult: "sqr x = x * x"
   230   by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)
   231 
   232 
   233 subsection {* Binary numerals *}
   234 
   235 text {*
   236   We embed binary representations into a generic algebraic
   237   structure using @{text numeral}.
   238 *}
   239 
   240 class numeral = one + semigroup_add
   241 begin
   242 
   243 primrec numeral :: "num \<Rightarrow> 'a" where
   244   numeral_One: "numeral One = 1" |
   245   numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n" |
   246   numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1"
   247 
   248 lemma numeral_code [code]:
   249   "numeral One = 1"
   250   "numeral (Bit0 n) = (let m = numeral n in m + m)"
   251   "numeral (Bit1 n) = (let m = numeral n in m + m + 1)"
   252   by (simp_all add: Let_def)
   253   
   254 lemma one_plus_numeral_commute: "1 + numeral x = numeral x + 1"
   255   apply (induct x)
   256   apply simp
   257   apply (simp add: add_assoc [symmetric], simp add: add_assoc)
   258   apply (simp add: add_assoc [symmetric], simp add: add_assoc)
   259   done
   260 
   261 lemma numeral_inc: "numeral (inc x) = numeral x + 1"
   262 proof (induct x)
   263   case (Bit1 x)
   264   have "numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1"
   265     by (simp only: one_plus_numeral_commute)
   266   with Bit1 show ?case
   267     by (simp add: add_assoc)
   268 qed simp_all
   269 
   270 declare numeral.simps [simp del]
   271 
   272 abbreviation "Numeral1 \<equiv> numeral One"
   273 
   274 declare numeral_One [code_post]
   275 
   276 end
   277 
   278 text {* Negative numerals. *}
   279 
   280 class neg_numeral = numeral + group_add
   281 begin
   282 
   283 definition neg_numeral :: "num \<Rightarrow> 'a" where
   284   "neg_numeral k = - numeral k"
   285 
   286 end
   287 
   288 text {* Numeral syntax. *}
   289 
   290 syntax
   291   "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
   292 
   293 parse_translation {*
   294   let
   295     fun num_of_int n =
   296       if n > 0 then
   297         (case IntInf.quotRem (n, 2) of
   298           (0, 1) => Syntax.const @{const_name One}
   299         | (n, 0) => Syntax.const @{const_name Bit0} $ num_of_int n
   300         | (n, 1) => Syntax.const @{const_name Bit1} $ num_of_int n)
   301       else raise Match
   302     val pos = Syntax.const @{const_name numeral}
   303     val neg = Syntax.const @{const_name neg_numeral}
   304     val one = Syntax.const @{const_name Groups.one}
   305     val zero = Syntax.const @{const_name Groups.zero}
   306     fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
   307           c $ numeral_tr [t] $ u
   308       | numeral_tr [Const (num, _)] =
   309           let
   310             val {value, ...} = Lexicon.read_xnum num;
   311           in
   312             if value = 0 then zero else
   313             if value > 0
   314             then pos $ num_of_int value
   315             else neg $ num_of_int (~value)
   316           end
   317       | numeral_tr ts = raise TERM ("numeral_tr", ts);
   318   in [("_Numeral", K numeral_tr)] end
   319 *}
   320 
   321 typed_print_translation {*
   322   let
   323     fun dest_num (Const (@{const_syntax Bit0}, _) $ n) = 2 * dest_num n
   324       | dest_num (Const (@{const_syntax Bit1}, _) $ n) = 2 * dest_num n + 1
   325       | dest_num (Const (@{const_syntax One}, _)) = 1;
   326     fun num_tr' sign ctxt T [n] =
   327       let
   328         val k = dest_num n;
   329         val t' =
   330           Syntax.const @{syntax_const "_Numeral"} $
   331             Syntax.free (sign ^ string_of_int k);
   332       in
   333         (case T of
   334           Type (@{type_name fun}, [_, T']) =>
   335             if Printer.type_emphasis ctxt T' then
   336               Syntax.const @{syntax_const "_constrain"} $ t' $
   337                 Syntax_Phases.term_of_typ ctxt T'
   338             else t'
   339         | _ => if T = dummyT then t' else raise Match)
   340       end;
   341   in
   342    [(@{const_syntax numeral}, num_tr' ""),
   343     (@{const_syntax neg_numeral}, num_tr' "-")]
   344   end
   345 *}
   346 
   347 ML_file "Tools/numeral.ML"
   348 
   349 
   350 subsection {* Class-specific numeral rules *}
   351 
   352 text {*
   353   @{const numeral} is a morphism.
   354 *}
   355 
   356 subsubsection {* Structures with addition: class @{text numeral} *}
   357 
   358 context numeral
   359 begin
   360 
   361 lemma numeral_add: "numeral (m + n) = numeral m + numeral n"
   362   by (induct n rule: num_induct)
   363      (simp_all only: numeral_One add_One add_inc numeral_inc add_assoc)
   364 
   365 lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)"
   366   by (rule numeral_add [symmetric])
   367 
   368 lemma numeral_plus_one: "numeral n + 1 = numeral (n + One)"
   369   using numeral_add [of n One] by (simp add: numeral_One)
   370 
   371 lemma one_plus_numeral: "1 + numeral n = numeral (One + n)"
   372   using numeral_add [of One n] by (simp add: numeral_One)
   373 
   374 lemma one_add_one: "1 + 1 = 2"
   375   using numeral_add [of One One] by (simp add: numeral_One)
   376 
   377 lemmas add_numeral_special =
   378   numeral_plus_one one_plus_numeral one_add_one
   379 
   380 end
   381 
   382 subsubsection {*
   383   Structures with negation: class @{text neg_numeral}
   384 *}
   385 
   386 context neg_numeral
   387 begin
   388 
   389 text {* Numerals form an abelian subgroup. *}
   390 
   391 inductive is_num :: "'a \<Rightarrow> bool" where
   392   "is_num 1" |
   393   "is_num x \<Longrightarrow> is_num (- x)" |
   394   "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> is_num (x + y)"
   395 
   396 lemma is_num_numeral: "is_num (numeral k)"
   397   by (induct k, simp_all add: numeral.simps is_num.intros)
   398 
   399 lemma is_num_add_commute:
   400   "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + y = y + x"
   401   apply (induct x rule: is_num.induct)
   402   apply (induct y rule: is_num.induct)
   403   apply simp
   404   apply (rule_tac a=x in add_left_imp_eq)
   405   apply (rule_tac a=x in add_right_imp_eq)
   406   apply (simp add: add_assoc minus_add_cancel)
   407   apply (simp add: add_assoc [symmetric], simp add: add_assoc)
   408   apply (rule_tac a=x in add_left_imp_eq)
   409   apply (rule_tac a=x in add_right_imp_eq)
   410   apply (simp add: add_assoc)
   411   apply (simp add: add_assoc, simp add: add_assoc [symmetric])
   412   done
   413 
   414 lemma is_num_add_left_commute:
   415   "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + (y + z) = y + (x + z)"
   416   by (simp only: add_assoc [symmetric] is_num_add_commute)
   417 
   418 lemmas is_num_normalize =
   419   add_assoc is_num_add_commute is_num_add_left_commute
   420   is_num.intros is_num_numeral
   421   minus_add
   422 
   423 definition dbl :: "'a \<Rightarrow> 'a" where "dbl x = x + x"
   424 definition dbl_inc :: "'a \<Rightarrow> 'a" where "dbl_inc x = x + x + 1"
   425 definition dbl_dec :: "'a \<Rightarrow> 'a" where "dbl_dec x = x + x - 1"
   426 
   427 definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" where
   428   "sub k l = numeral k - numeral l"
   429 
   430 lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1"
   431   by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq)
   432 
   433 lemma dbl_simps [simp]:
   434   "dbl (neg_numeral k) = neg_numeral (Bit0 k)"
   435   "dbl 0 = 0"
   436   "dbl 1 = 2"
   437   "dbl (numeral k) = numeral (Bit0 k)"
   438   by (simp_all add: dbl_def neg_numeral_def numeral.simps minus_add)
   439 
   440 lemma dbl_inc_simps [simp]:
   441   "dbl_inc (neg_numeral k) = neg_numeral (BitM k)"
   442   "dbl_inc 0 = 1"
   443   "dbl_inc 1 = 3"
   444   "dbl_inc (numeral k) = numeral (Bit1 k)"
   445   by (simp_all add: dbl_inc_def neg_numeral_def numeral.simps numeral_BitM is_num_normalize algebra_simps del: add_uminus_conv_diff)
   446 
   447 lemma dbl_dec_simps [simp]:
   448   "dbl_dec (neg_numeral k) = neg_numeral (Bit1 k)"
   449   "dbl_dec 0 = -1"
   450   "dbl_dec 1 = 1"
   451   "dbl_dec (numeral k) = numeral (BitM k)"
   452   by (simp_all add: dbl_dec_def neg_numeral_def numeral.simps numeral_BitM is_num_normalize)
   453 
   454 lemma sub_num_simps [simp]:
   455   "sub One One = 0"
   456   "sub One (Bit0 l) = neg_numeral (BitM l)"
   457   "sub One (Bit1 l) = neg_numeral (Bit0 l)"
   458   "sub (Bit0 k) One = numeral (BitM k)"
   459   "sub (Bit1 k) One = numeral (Bit0 k)"
   460   "sub (Bit0 k) (Bit0 l) = dbl (sub k l)"
   461   "sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)"
   462   "sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)"
   463   "sub (Bit1 k) (Bit1 l) = dbl (sub k l)"
   464   by (simp_all add: dbl_def dbl_dec_def dbl_inc_def sub_def neg_numeral_def numeral.simps
   465     numeral_BitM is_num_normalize del: add_uminus_conv_diff add: diff_conv_add_uminus)
   466 
   467 lemma add_neg_numeral_simps:
   468   "numeral m + neg_numeral n = sub m n"
   469   "neg_numeral m + numeral n = sub n m"
   470   "neg_numeral m + neg_numeral n = neg_numeral (m + n)"
   471   by (simp_all add: sub_def neg_numeral_def numeral_add numeral.simps is_num_normalize
   472     del: add_uminus_conv_diff add: diff_conv_add_uminus)
   473 
   474 lemma add_neg_numeral_special:
   475   "1 + neg_numeral m = sub One m"
   476   "neg_numeral m + 1 = sub One m"
   477   by (simp_all add: sub_def neg_numeral_def numeral_add numeral.simps is_num_normalize)
   478 
   479 lemma diff_numeral_simps:
   480   "numeral m - numeral n = sub m n"
   481   "numeral m - neg_numeral n = numeral (m + n)"
   482   "neg_numeral m - numeral n = neg_numeral (m + n)"
   483   "neg_numeral m - neg_numeral n = sub n m"
   484   by (simp_all add: neg_numeral_def sub_def numeral_add numeral.simps is_num_normalize
   485     del: add_uminus_conv_diff add: diff_conv_add_uminus)
   486 
   487 lemma diff_numeral_special:
   488   "1 - numeral n = sub One n"
   489   "1 - neg_numeral n = numeral (One + n)"
   490   "numeral m - 1 = sub m One"
   491   "neg_numeral m - 1 = neg_numeral (m + One)"
   492   by (simp_all add: neg_numeral_def sub_def numeral_add numeral.simps add: is_num_normalize)
   493 
   494 lemma minus_one: "- 1 = -1"
   495   unfolding neg_numeral_def numeral.simps ..
   496 
   497 lemma minus_numeral: "- numeral n = neg_numeral n"
   498   unfolding neg_numeral_def ..
   499 
   500 lemma minus_neg_numeral: "- neg_numeral n = numeral n"
   501   unfolding neg_numeral_def by simp
   502 
   503 lemmas minus_numeral_simps [simp] =
   504   minus_one minus_numeral minus_neg_numeral
   505 
   506 end
   507 
   508 subsubsection {*
   509   Structures with multiplication: class @{text semiring_numeral}
   510 *}
   511 
   512 class semiring_numeral = semiring + monoid_mult
   513 begin
   514 
   515 subclass numeral ..
   516 
   517 lemma numeral_mult: "numeral (m * n) = numeral m * numeral n"
   518   apply (induct n rule: num_induct)
   519   apply (simp add: numeral_One)
   520   apply (simp add: mult_inc numeral_inc numeral_add distrib_left)
   521   done
   522 
   523 lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)"
   524   by (rule numeral_mult [symmetric])
   525 
   526 lemma mult_2: "2 * z = z + z"
   527   unfolding one_add_one [symmetric] distrib_right by simp
   528 
   529 lemma mult_2_right: "z * 2 = z + z"
   530   unfolding one_add_one [symmetric] distrib_left by simp
   531 
   532 end
   533 
   534 subsubsection {*
   535   Structures with a zero: class @{text semiring_1}
   536 *}
   537 
   538 context semiring_1
   539 begin
   540 
   541 subclass semiring_numeral ..
   542 
   543 lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n"
   544   by (induct n,
   545     simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)
   546 
   547 end
   548 
   549 lemma nat_of_num_numeral [code_abbrev]:
   550   "nat_of_num = numeral"
   551 proof
   552   fix n
   553   have "numeral n = nat_of_num n"
   554     by (induct n) (simp_all add: numeral.simps)
   555   then show "nat_of_num n = numeral n" by simp
   556 qed
   557 
   558 lemma nat_of_num_code [code]:
   559   "nat_of_num One = 1"
   560   "nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)"
   561   "nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))"
   562   by (simp_all add: Let_def)
   563 
   564 subsubsection {*
   565   Equality: class @{text semiring_char_0}
   566 *}
   567 
   568 context semiring_char_0
   569 begin
   570 
   571 lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n"
   572   unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   573     of_nat_eq_iff num_eq_iff ..
   574 
   575 lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One"
   576   by (rule numeral_eq_iff [of n One, unfolded numeral_One])
   577 
   578 lemma one_eq_numeral_iff: "1 = numeral n \<longleftrightarrow> One = n"
   579   by (rule numeral_eq_iff [of One n, unfolded numeral_One])
   580 
   581 lemma numeral_neq_zero: "numeral n \<noteq> 0"
   582   unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   583   by (simp add: nat_of_num_pos)
   584 
   585 lemma zero_neq_numeral: "0 \<noteq> numeral n"
   586   unfolding eq_commute [of 0] by (rule numeral_neq_zero)
   587 
   588 lemmas eq_numeral_simps [simp] =
   589   numeral_eq_iff
   590   numeral_eq_one_iff
   591   one_eq_numeral_iff
   592   numeral_neq_zero
   593   zero_neq_numeral
   594 
   595 end
   596 
   597 subsubsection {*
   598   Comparisons: class @{text linordered_semidom}
   599 *}
   600 
   601 text {*  Could be perhaps more general than here. *}
   602 
   603 context linordered_semidom
   604 begin
   605 
   606 lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n"
   607 proof -
   608   have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n"
   609     unfolding less_eq_num_def nat_of_num_numeral of_nat_le_iff ..
   610   then show ?thesis by simp
   611 qed
   612 
   613 lemma one_le_numeral: "1 \<le> numeral n"
   614 using numeral_le_iff [of One n] by (simp add: numeral_One)
   615 
   616 lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> One"
   617 using numeral_le_iff [of n One] by (simp add: numeral_One)
   618 
   619 lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n"
   620 proof -
   621   have "of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n"
   622     unfolding less_num_def nat_of_num_numeral of_nat_less_iff ..
   623   then show ?thesis by simp
   624 qed
   625 
   626 lemma not_numeral_less_one: "\<not> numeral n < 1"
   627   using numeral_less_iff [of n One] by (simp add: numeral_One)
   628 
   629 lemma one_less_numeral_iff: "1 < numeral n \<longleftrightarrow> One < n"
   630   using numeral_less_iff [of One n] by (simp add: numeral_One)
   631 
   632 lemma zero_le_numeral: "0 \<le> numeral n"
   633   by (induct n) (simp_all add: numeral.simps)
   634 
   635 lemma zero_less_numeral: "0 < numeral n"
   636   by (induct n) (simp_all add: numeral.simps add_pos_pos)
   637 
   638 lemma not_numeral_le_zero: "\<not> numeral n \<le> 0"
   639   by (simp add: not_le zero_less_numeral)
   640 
   641 lemma not_numeral_less_zero: "\<not> numeral n < 0"
   642   by (simp add: not_less zero_le_numeral)
   643 
   644 lemmas le_numeral_extra =
   645   zero_le_one not_one_le_zero
   646   order_refl [of 0] order_refl [of 1]
   647 
   648 lemmas less_numeral_extra =
   649   zero_less_one not_one_less_zero
   650   less_irrefl [of 0] less_irrefl [of 1]
   651 
   652 lemmas le_numeral_simps [simp] =
   653   numeral_le_iff
   654   one_le_numeral
   655   numeral_le_one_iff
   656   zero_le_numeral
   657   not_numeral_le_zero
   658 
   659 lemmas less_numeral_simps [simp] =
   660   numeral_less_iff
   661   one_less_numeral_iff
   662   not_numeral_less_one
   663   zero_less_numeral
   664   not_numeral_less_zero
   665 
   666 end
   667 
   668 subsubsection {*
   669   Multiplication and negation: class @{text ring_1}
   670 *}
   671 
   672 context ring_1
   673 begin
   674 
   675 subclass neg_numeral ..
   676 
   677 lemma mult_neg_numeral_simps:
   678   "neg_numeral m * neg_numeral n = numeral (m * n)"
   679   "neg_numeral m * numeral n = neg_numeral (m * n)"
   680   "numeral m * neg_numeral n = neg_numeral (m * n)"
   681   unfolding neg_numeral_def mult_minus_left mult_minus_right
   682   by (simp_all only: minus_minus numeral_mult)
   683 
   684 lemma mult_minus1 [simp]: "-1 * z = - z"
   685   unfolding neg_numeral_def numeral.simps mult_minus_left by simp
   686 
   687 lemma mult_minus1_right [simp]: "z * -1 = - z"
   688   unfolding neg_numeral_def numeral.simps mult_minus_right by simp
   689 
   690 end
   691 
   692 subsubsection {*
   693   Equality using @{text iszero} for rings with non-zero characteristic
   694 *}
   695 
   696 context ring_1
   697 begin
   698 
   699 definition iszero :: "'a \<Rightarrow> bool"
   700   where "iszero z \<longleftrightarrow> z = 0"
   701 
   702 lemma iszero_0 [simp]: "iszero 0"
   703   by (simp add: iszero_def)
   704 
   705 lemma not_iszero_1 [simp]: "\<not> iszero 1"
   706   by (simp add: iszero_def)
   707 
   708 lemma not_iszero_Numeral1: "\<not> iszero Numeral1"
   709   by (simp add: numeral_One)
   710 
   711 lemma iszero_neg_numeral [simp]:
   712   "iszero (neg_numeral w) \<longleftrightarrow> iszero (numeral w)"
   713   unfolding iszero_def neg_numeral_def
   714   by (rule neg_equal_0_iff_equal)
   715 
   716 lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)"
   717   unfolding iszero_def by (rule eq_iff_diff_eq_0)
   718 
   719 text {* The @{text "eq_numeral_iff_iszero"} lemmas are not declared
   720 @{text "[simp]"} by default, because for rings of characteristic zero,
   721 better simp rules are possible. For a type like integers mod @{text
   722 "n"}, type-instantiated versions of these rules should be added to the
   723 simplifier, along with a type-specific rule for deciding propositions
   724 of the form @{text "iszero (numeral w)"}.
   725 
   726 bh: Maybe it would not be so bad to just declare these as simp
   727 rules anyway? I should test whether these rules take precedence over
   728 the @{text "ring_char_0"} rules in the simplifier.
   729 *}
   730 
   731 lemma eq_numeral_iff_iszero:
   732   "numeral x = numeral y \<longleftrightarrow> iszero (sub x y)"
   733   "numeral x = neg_numeral y \<longleftrightarrow> iszero (numeral (x + y))"
   734   "neg_numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))"
   735   "neg_numeral x = neg_numeral y \<longleftrightarrow> iszero (sub y x)"
   736   "numeral x = 1 \<longleftrightarrow> iszero (sub x One)"
   737   "1 = numeral y \<longleftrightarrow> iszero (sub One y)"
   738   "neg_numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))"
   739   "1 = neg_numeral y \<longleftrightarrow> iszero (numeral (One + y))"
   740   "numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
   741   "0 = numeral y \<longleftrightarrow> iszero (numeral y)"
   742   "neg_numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
   743   "0 = neg_numeral y \<longleftrightarrow> iszero (numeral y)"
   744   unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special
   745   by simp_all
   746 
   747 end
   748 
   749 subsubsection {*
   750   Equality and negation: class @{text ring_char_0}
   751 *}
   752 
   753 class ring_char_0 = ring_1 + semiring_char_0
   754 begin
   755 
   756 lemma not_iszero_numeral [simp]: "\<not> iszero (numeral w)"
   757   by (simp add: iszero_def)
   758 
   759 lemma neg_numeral_eq_iff: "neg_numeral m = neg_numeral n \<longleftrightarrow> m = n"
   760   by (simp only: neg_numeral_def neg_equal_iff_equal numeral_eq_iff)
   761 
   762 lemma numeral_neq_neg_numeral: "numeral m \<noteq> neg_numeral n"
   763   unfolding neg_numeral_def eq_neg_iff_add_eq_0
   764   by (simp add: numeral_plus_numeral)
   765 
   766 lemma neg_numeral_neq_numeral: "neg_numeral m \<noteq> numeral n"
   767   by (rule numeral_neq_neg_numeral [symmetric])
   768 
   769 lemma zero_neq_neg_numeral: "0 \<noteq> neg_numeral n"
   770   unfolding neg_numeral_def neg_0_equal_iff_equal by simp
   771 
   772 lemma neg_numeral_neq_zero: "neg_numeral n \<noteq> 0"
   773   unfolding neg_numeral_def neg_equal_0_iff_equal by simp
   774 
   775 lemma one_neq_neg_numeral: "1 \<noteq> neg_numeral n"
   776   using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)
   777 
   778 lemma neg_numeral_neq_one: "neg_numeral n \<noteq> 1"
   779   using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)
   780 
   781 lemmas eq_neg_numeral_simps [simp] =
   782   neg_numeral_eq_iff
   783   numeral_neq_neg_numeral neg_numeral_neq_numeral
   784   one_neq_neg_numeral neg_numeral_neq_one
   785   zero_neq_neg_numeral neg_numeral_neq_zero
   786 
   787 end
   788 
   789 subsubsection {*
   790   Structures with negation and order: class @{text linordered_idom}
   791 *}
   792 
   793 context linordered_idom
   794 begin
   795 
   796 subclass ring_char_0 ..
   797 
   798 lemma neg_numeral_le_iff: "neg_numeral m \<le> neg_numeral n \<longleftrightarrow> n \<le> m"
   799   by (simp only: neg_numeral_def neg_le_iff_le numeral_le_iff)
   800 
   801 lemma neg_numeral_less_iff: "neg_numeral m < neg_numeral n \<longleftrightarrow> n < m"
   802   by (simp only: neg_numeral_def neg_less_iff_less numeral_less_iff)
   803 
   804 lemma neg_numeral_less_zero: "neg_numeral n < 0"
   805   by (simp only: neg_numeral_def neg_less_0_iff_less zero_less_numeral)
   806 
   807 lemma neg_numeral_le_zero: "neg_numeral n \<le> 0"
   808   by (simp only: neg_numeral_def neg_le_0_iff_le zero_le_numeral)
   809 
   810 lemma not_zero_less_neg_numeral: "\<not> 0 < neg_numeral n"
   811   by (simp only: not_less neg_numeral_le_zero)
   812 
   813 lemma not_zero_le_neg_numeral: "\<not> 0 \<le> neg_numeral n"
   814   by (simp only: not_le neg_numeral_less_zero)
   815 
   816 lemma neg_numeral_less_numeral: "neg_numeral m < numeral n"
   817   using neg_numeral_less_zero zero_less_numeral by (rule less_trans)
   818 
   819 lemma neg_numeral_le_numeral: "neg_numeral m \<le> numeral n"
   820   by (simp only: less_imp_le neg_numeral_less_numeral)
   821 
   822 lemma not_numeral_less_neg_numeral: "\<not> numeral m < neg_numeral n"
   823   by (simp only: not_less neg_numeral_le_numeral)
   824 
   825 lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> neg_numeral n"
   826   by (simp only: not_le neg_numeral_less_numeral)
   827   
   828 lemma neg_numeral_less_one: "neg_numeral m < 1"
   829   by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One])
   830 
   831 lemma neg_numeral_le_one: "neg_numeral m \<le> 1"
   832   by (rule neg_numeral_le_numeral [of m One, unfolded numeral_One])
   833 
   834 lemma not_one_less_neg_numeral: "\<not> 1 < neg_numeral m"
   835   by (simp only: not_less neg_numeral_le_one)
   836 
   837 lemma not_one_le_neg_numeral: "\<not> 1 \<le> neg_numeral m"
   838   by (simp only: not_le neg_numeral_less_one)
   839 
   840 lemma sub_non_negative:
   841   "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"
   842   by (simp only: sub_def le_diff_eq) simp
   843 
   844 lemma sub_positive:
   845   "sub n m > 0 \<longleftrightarrow> n > m"
   846   by (simp only: sub_def less_diff_eq) simp
   847 
   848 lemma sub_non_positive:
   849   "sub n m \<le> 0 \<longleftrightarrow> n \<le> m"
   850   by (simp only: sub_def diff_le_eq) simp
   851 
   852 lemma sub_negative:
   853   "sub n m < 0 \<longleftrightarrow> n < m"
   854   by (simp only: sub_def diff_less_eq) simp
   855 
   856 lemmas le_neg_numeral_simps [simp] =
   857   neg_numeral_le_iff
   858   neg_numeral_le_numeral not_numeral_le_neg_numeral
   859   neg_numeral_le_zero not_zero_le_neg_numeral
   860   neg_numeral_le_one not_one_le_neg_numeral
   861 
   862 lemmas less_neg_numeral_simps [simp] =
   863   neg_numeral_less_iff
   864   neg_numeral_less_numeral not_numeral_less_neg_numeral
   865   neg_numeral_less_zero not_zero_less_neg_numeral
   866   neg_numeral_less_one not_one_less_neg_numeral
   867 
   868 lemma abs_numeral [simp]: "abs (numeral n) = numeral n"
   869   by simp
   870 
   871 lemma abs_neg_numeral [simp]: "abs (neg_numeral n) = numeral n"
   872   by (simp only: neg_numeral_def abs_minus_cancel abs_numeral)
   873 
   874 end
   875 
   876 subsubsection {*
   877   Natural numbers
   878 *}
   879 
   880 lemma Suc_1 [simp]: "Suc 1 = 2"
   881   unfolding Suc_eq_plus1 by (rule one_add_one)
   882 
   883 lemma Suc_numeral [simp]: "Suc (numeral n) = numeral (n + One)"
   884   unfolding Suc_eq_plus1 by (rule numeral_plus_one)
   885 
   886 definition pred_numeral :: "num \<Rightarrow> nat"
   887   where [code del]: "pred_numeral k = numeral k - 1"
   888 
   889 lemma numeral_eq_Suc: "numeral k = Suc (pred_numeral k)"
   890   unfolding pred_numeral_def by simp
   891 
   892 lemma eval_nat_numeral:
   893   "numeral One = Suc 0"
   894   "numeral (Bit0 n) = Suc (numeral (BitM n))"
   895   "numeral (Bit1 n) = Suc (numeral (Bit0 n))"
   896   by (simp_all add: numeral.simps BitM_plus_one)
   897 
   898 lemma pred_numeral_simps [simp]:
   899   "pred_numeral One = 0"
   900   "pred_numeral (Bit0 k) = numeral (BitM k)"
   901   "pred_numeral (Bit1 k) = numeral (Bit0 k)"
   902   unfolding pred_numeral_def eval_nat_numeral
   903   by (simp_all only: diff_Suc_Suc diff_0)
   904 
   905 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
   906   by (simp add: eval_nat_numeral)
   907 
   908 lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
   909   by (simp add: eval_nat_numeral)
   910 
   911 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
   912   by (simp only: numeral_One One_nat_def)
   913 
   914 lemma Suc_nat_number_of_add:
   915   "Suc (numeral v + n) = numeral (v + One) + n"
   916   by simp
   917 
   918 (*Maps #n to n for n = 1, 2*)
   919 lemmas numerals = numeral_One [where 'a=nat] numeral_2_eq_2
   920 
   921 text {* Comparisons involving @{term Suc}. *}
   922 
   923 lemma eq_numeral_Suc [simp]: "numeral k = Suc n \<longleftrightarrow> pred_numeral k = n"
   924   by (simp add: numeral_eq_Suc)
   925 
   926 lemma Suc_eq_numeral [simp]: "Suc n = numeral k \<longleftrightarrow> n = pred_numeral k"
   927   by (simp add: numeral_eq_Suc)
   928 
   929 lemma less_numeral_Suc [simp]: "numeral k < Suc n \<longleftrightarrow> pred_numeral k < n"
   930   by (simp add: numeral_eq_Suc)
   931 
   932 lemma less_Suc_numeral [simp]: "Suc n < numeral k \<longleftrightarrow> n < pred_numeral k"
   933   by (simp add: numeral_eq_Suc)
   934 
   935 lemma le_numeral_Suc [simp]: "numeral k \<le> Suc n \<longleftrightarrow> pred_numeral k \<le> n"
   936   by (simp add: numeral_eq_Suc)
   937 
   938 lemma le_Suc_numeral [simp]: "Suc n \<le> numeral k \<longleftrightarrow> n \<le> pred_numeral k"
   939   by (simp add: numeral_eq_Suc)
   940 
   941 lemma diff_Suc_numeral [simp]: "Suc n - numeral k = n - pred_numeral k"
   942   by (simp add: numeral_eq_Suc)
   943 
   944 lemma diff_numeral_Suc [simp]: "numeral k - Suc n = pred_numeral k - n"
   945   by (simp add: numeral_eq_Suc)
   946 
   947 lemma max_Suc_numeral [simp]:
   948   "max (Suc n) (numeral k) = Suc (max n (pred_numeral k))"
   949   by (simp add: numeral_eq_Suc)
   950 
   951 lemma max_numeral_Suc [simp]:
   952   "max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)"
   953   by (simp add: numeral_eq_Suc)
   954 
   955 lemma min_Suc_numeral [simp]:
   956   "min (Suc n) (numeral k) = Suc (min n (pred_numeral k))"
   957   by (simp add: numeral_eq_Suc)
   958 
   959 lemma min_numeral_Suc [simp]:
   960   "min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)"
   961   by (simp add: numeral_eq_Suc)
   962 
   963 text {* For @{term nat_case} and @{term nat_rec}. *}
   964 
   965 lemma nat_case_numeral [simp]:
   966   "nat_case a f (numeral v) = (let pv = pred_numeral v in f pv)"
   967   by (simp add: numeral_eq_Suc)
   968 
   969 lemma nat_case_add_eq_if [simp]:
   970   "nat_case a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))"
   971   by (simp add: numeral_eq_Suc)
   972 
   973 lemma nat_rec_numeral [simp]:
   974   "nat_rec a f (numeral v) =
   975     (let pv = pred_numeral v in f pv (nat_rec a f pv))"
   976   by (simp add: numeral_eq_Suc Let_def)
   977 
   978 lemma nat_rec_add_eq_if [simp]:
   979   "nat_rec a f (numeral v + n) =
   980     (let pv = pred_numeral v in f (pv + n) (nat_rec a f (pv + n)))"
   981   by (simp add: numeral_eq_Suc Let_def)
   982 
   983 text {* Case analysis on @{term "n < 2"} *}
   984 
   985 lemma less_2_cases: "n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0"
   986   by (auto simp add: numeral_2_eq_2)
   987 
   988 text {* Removal of Small Numerals: 0, 1 and (in additive positions) 2 *}
   989 text {* bh: Are these rules really a good idea? *}
   990 
   991 lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
   992   by simp
   993 
   994 lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
   995   by simp
   996 
   997 text {* Can be used to eliminate long strings of Sucs, but not by default. *}
   998 
   999 lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
  1000   by simp
  1001 
  1002 lemmas nat_1_add_1 = one_add_one [where 'a=nat] (* legacy *)
  1003 
  1004 
  1005 subsection {* Numeral equations as default simplification rules *}
  1006 
  1007 declare (in numeral) numeral_One [simp]
  1008 declare (in numeral) numeral_plus_numeral [simp]
  1009 declare (in numeral) add_numeral_special [simp]
  1010 declare (in neg_numeral) add_neg_numeral_simps [simp]
  1011 declare (in neg_numeral) add_neg_numeral_special [simp]
  1012 declare (in neg_numeral) diff_numeral_simps [simp]
  1013 declare (in neg_numeral) diff_numeral_special [simp]
  1014 declare (in semiring_numeral) numeral_times_numeral [simp]
  1015 declare (in ring_1) mult_neg_numeral_simps [simp]
  1016 
  1017 subsection {* Setting up simprocs *}
  1018 
  1019 lemma mult_numeral_1: "Numeral1 * a = (a::'a::semiring_numeral)"
  1020   by simp
  1021 
  1022 lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::semiring_numeral)"
  1023   by simp
  1024 
  1025 lemma divide_numeral_1: "a / Numeral1 = (a::'a::field)"
  1026   by simp
  1027 
  1028 lemma inverse_numeral_1:
  1029   "inverse Numeral1 = (Numeral1::'a::division_ring)"
  1030   by simp
  1031 
  1032 text{*Theorem lists for the cancellation simprocs. The use of a binary
  1033 numeral for 1 reduces the number of special cases.*}
  1034 
  1035 lemmas mult_1s =
  1036   mult_numeral_1 mult_numeral_1_right 
  1037   mult_minus1 mult_minus1_right
  1038 
  1039 setup {*
  1040   Reorient_Proc.add
  1041     (fn Const (@{const_name numeral}, _) $ _ => true
  1042     | Const (@{const_name neg_numeral}, _) $ _ => true
  1043     | _ => false)
  1044 *}
  1045 
  1046 simproc_setup reorient_numeral
  1047   ("numeral w = x" | "neg_numeral w = y") = Reorient_Proc.proc
  1048 
  1049 
  1050 subsubsection {* Simplification of arithmetic operations on integer constants. *}
  1051 
  1052 lemmas arith_special = (* already declared simp above *)
  1053   add_numeral_special add_neg_numeral_special
  1054   diff_numeral_special minus_one
  1055 
  1056 (* rules already in simpset *)
  1057 lemmas arith_extra_simps =
  1058   numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right
  1059   minus_numeral minus_neg_numeral minus_zero minus_one
  1060   diff_numeral_simps diff_0 diff_0_right
  1061   numeral_times_numeral mult_neg_numeral_simps
  1062   mult_zero_left mult_zero_right
  1063   abs_numeral abs_neg_numeral
  1064 
  1065 text {*
  1066   For making a minimal simpset, one must include these default simprules.
  1067   Also include @{text simp_thms}.
  1068 *}
  1069 
  1070 lemmas arith_simps =
  1071   add_num_simps mult_num_simps sub_num_simps
  1072   BitM.simps dbl_simps dbl_inc_simps dbl_dec_simps
  1073   abs_zero abs_one arith_extra_simps
  1074 
  1075 lemmas more_arith_simps =
  1076   neg_le_iff_le
  1077   minus_zero left_minus right_minus
  1078   mult_1_left mult_1_right
  1079   mult_minus_left mult_minus_right
  1080   minus_add_distrib minus_minus mult_assoc
  1081 
  1082 lemmas of_nat_simps =
  1083   of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
  1084 
  1085 text {* Simplification of relational operations *}
  1086 
  1087 lemmas eq_numeral_extra =
  1088   zero_neq_one one_neq_zero
  1089 
  1090 lemmas rel_simps =
  1091   le_num_simps less_num_simps eq_num_simps
  1092   le_numeral_simps le_neg_numeral_simps le_numeral_extra
  1093   less_numeral_simps less_neg_numeral_simps less_numeral_extra
  1094   eq_numeral_simps eq_neg_numeral_simps eq_numeral_extra
  1095 
  1096 lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"
  1097   -- {* Unfold all @{text let}s involving constants *}
  1098   unfolding Let_def ..
  1099 
  1100 lemma Let_neg_numeral [simp]: "Let (neg_numeral v) f = f (neg_numeral v)"
  1101   -- {* Unfold all @{text let}s involving constants *}
  1102   unfolding Let_def ..
  1103 
  1104 declaration {*
  1105 let 
  1106   fun number_of thy T n =
  1107     if not (Sign.of_sort thy (T, @{sort numeral}))
  1108     then raise CTERM ("number_of", [])
  1109     else Numeral.mk_cnumber (Thm.ctyp_of thy T) n;
  1110 in
  1111   K (
  1112     Lin_Arith.add_simps (@{thms arith_simps} @ @{thms more_arith_simps}
  1113       @ @{thms rel_simps}
  1114       @ @{thms pred_numeral_simps}
  1115       @ @{thms arith_special numeral_One}
  1116       @ @{thms of_nat_simps})
  1117     #> Lin_Arith.add_simps [@{thm Suc_numeral},
  1118       @{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
  1119       @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
  1120       @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
  1121       @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
  1122       @{thm mult_Suc}, @{thm mult_Suc_right},
  1123       @{thm of_nat_numeral}]
  1124     #> Lin_Arith.set_number_of number_of)
  1125 end
  1126 *}
  1127 
  1128 
  1129 subsubsection {* Simplification of arithmetic when nested to the right. *}
  1130 
  1131 lemma add_numeral_left [simp]:
  1132   "numeral v + (numeral w + z) = (numeral(v + w) + z)"
  1133   by (simp_all add: add_assoc [symmetric])
  1134 
  1135 lemma add_neg_numeral_left [simp]:
  1136   "numeral v + (neg_numeral w + y) = (sub v w + y)"
  1137   "neg_numeral v + (numeral w + y) = (sub w v + y)"
  1138   "neg_numeral v + (neg_numeral w + y) = (neg_numeral(v + w) + y)"
  1139   by (simp_all add: add_assoc [symmetric])
  1140 
  1141 lemma mult_numeral_left [simp]:
  1142   "numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)"
  1143   "neg_numeral v * (numeral w * y) = (neg_numeral(v * w) * y :: 'b::ring_1)"
  1144   "numeral v * (neg_numeral w * y) = (neg_numeral(v * w) * y :: 'b::ring_1)"
  1145   "neg_numeral v * (neg_numeral w * y) = (numeral(v * w) * y :: 'b::ring_1)"
  1146   by (simp_all add: mult_assoc [symmetric])
  1147 
  1148 hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec
  1149 
  1150 
  1151 subsection {* code module namespace *}
  1152 
  1153 code_identifier
  1154   code_module Num \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  1155 
  1156 end
  1157