src/HOL/Num.thy
author blanchet
Wed Sep 03 00:06:24 2014 +0200 (2014-09-03)
changeset 58152 6fe60a9a5bad
parent 58128 43a1ba26a8cb
child 58154 47c3c7019b97
permissions -rw-r--r--
use 'datatype_new' in 'Main'
     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 BNF_Least_Fixpoint Old_Datatype
    10 begin
    11 
    12 subsection {* The @{text num} type *}
    13 
    14 datatype_new 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 {* Numeral syntax. *}
   279 
   280 syntax
   281   "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
   282 
   283 parse_translation {*
   284   let
   285     fun num_of_int n =
   286       if n > 0 then
   287         (case IntInf.quotRem (n, 2) of
   288           (0, 1) => Syntax.const @{const_syntax One}
   289         | (n, 0) => Syntax.const @{const_syntax Bit0} $ num_of_int n
   290         | (n, 1) => Syntax.const @{const_syntax Bit1} $ num_of_int n)
   291       else raise Match
   292     val numeral = Syntax.const @{const_syntax numeral}
   293     val uminus = Syntax.const @{const_syntax uminus}
   294     val one = Syntax.const @{const_syntax Groups.one}
   295     val zero = Syntax.const @{const_syntax Groups.zero}
   296     fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
   297           c $ numeral_tr [t] $ u
   298       | numeral_tr [Const (num, _)] =
   299           let
   300             val {value, ...} = Lexicon.read_xnum num;
   301           in
   302             if value = 0 then zero else
   303             if value > 0
   304             then numeral $ num_of_int value
   305             else if value = ~1 then uminus $ one
   306             else uminus $ (numeral $ num_of_int (~ value))
   307           end
   308       | numeral_tr ts = raise TERM ("numeral_tr", ts);
   309   in [(@{syntax_const "_Numeral"}, K numeral_tr)] end
   310 *}
   311 
   312 typed_print_translation {*
   313   let
   314     fun dest_num (Const (@{const_syntax Bit0}, _) $ n) = 2 * dest_num n
   315       | dest_num (Const (@{const_syntax Bit1}, _) $ n) = 2 * dest_num n + 1
   316       | dest_num (Const (@{const_syntax One}, _)) = 1;
   317     fun num_tr' ctxt T [n] =
   318       let
   319         val k = dest_num n;
   320         val t' =
   321           Syntax.const @{syntax_const "_Numeral"} $
   322             Syntax.free (string_of_int k);
   323       in
   324         (case T of
   325           Type (@{type_name fun}, [_, T']) =>
   326             if Printer.type_emphasis ctxt T' then
   327               Syntax.const @{syntax_const "_constrain"} $ t' $
   328                 Syntax_Phases.term_of_typ ctxt T'
   329             else t'
   330         | _ => if T = dummyT then t' else raise Match)
   331       end;
   332   in
   333    [(@{const_syntax numeral}, num_tr')]
   334   end
   335 *}
   336 
   337 ML_file "Tools/numeral.ML"
   338 
   339 
   340 subsection {* Class-specific numeral rules *}
   341 
   342 text {*
   343   @{const numeral} is a morphism.
   344 *}
   345 
   346 subsubsection {* Structures with addition: class @{text numeral} *}
   347 
   348 context numeral
   349 begin
   350 
   351 lemma numeral_add: "numeral (m + n) = numeral m + numeral n"
   352   by (induct n rule: num_induct)
   353      (simp_all only: numeral_One add_One add_inc numeral_inc add.assoc)
   354 
   355 lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)"
   356   by (rule numeral_add [symmetric])
   357 
   358 lemma numeral_plus_one: "numeral n + 1 = numeral (n + One)"
   359   using numeral_add [of n One] by (simp add: numeral_One)
   360 
   361 lemma one_plus_numeral: "1 + numeral n = numeral (One + n)"
   362   using numeral_add [of One n] by (simp add: numeral_One)
   363 
   364 lemma one_add_one: "1 + 1 = 2"
   365   using numeral_add [of One One] by (simp add: numeral_One)
   366 
   367 lemmas add_numeral_special =
   368   numeral_plus_one one_plus_numeral one_add_one
   369 
   370 end
   371 
   372 subsubsection {*
   373   Structures with negation: class @{text neg_numeral}
   374 *}
   375 
   376 class neg_numeral = numeral + group_add
   377 begin
   378 
   379 lemma uminus_numeral_One:
   380   "- Numeral1 = - 1"
   381   by (simp add: numeral_One)
   382 
   383 text {* Numerals form an abelian subgroup. *}
   384 
   385 inductive is_num :: "'a \<Rightarrow> bool" where
   386   "is_num 1" |
   387   "is_num x \<Longrightarrow> is_num (- x)" |
   388   "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> is_num (x + y)"
   389 
   390 lemma is_num_numeral: "is_num (numeral k)"
   391   by (induct k, simp_all add: numeral.simps is_num.intros)
   392 
   393 lemma is_num_add_commute:
   394   "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + y = y + x"
   395   apply (induct x rule: is_num.induct)
   396   apply (induct y rule: is_num.induct)
   397   apply simp
   398   apply (rule_tac a=x in add_left_imp_eq)
   399   apply (rule_tac a=x in add_right_imp_eq)
   400   apply (simp add: add.assoc)
   401   apply (simp add: add.assoc [symmetric], simp add: add.assoc)
   402   apply (rule_tac a=x in add_left_imp_eq)
   403   apply (rule_tac a=x in add_right_imp_eq)
   404   apply (simp add: add.assoc)
   405   apply (simp add: add.assoc, simp add: add.assoc [symmetric])
   406   done
   407 
   408 lemma is_num_add_left_commute:
   409   "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + (y + z) = y + (x + z)"
   410   by (simp only: add.assoc [symmetric] is_num_add_commute)
   411 
   412 lemmas is_num_normalize =
   413   add.assoc is_num_add_commute is_num_add_left_commute
   414   is_num.intros is_num_numeral
   415   minus_add
   416 
   417 definition dbl :: "'a \<Rightarrow> 'a" where "dbl x = x + x"
   418 definition dbl_inc :: "'a \<Rightarrow> 'a" where "dbl_inc x = x + x + 1"
   419 definition dbl_dec :: "'a \<Rightarrow> 'a" where "dbl_dec x = x + x - 1"
   420 
   421 definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" where
   422   "sub k l = numeral k - numeral l"
   423 
   424 lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1"
   425   by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq)
   426 
   427 lemma dbl_simps [simp]:
   428   "dbl (- numeral k) = - dbl (numeral k)"
   429   "dbl 0 = 0"
   430   "dbl 1 = 2"
   431   "dbl (- 1) = - 2"
   432   "dbl (numeral k) = numeral (Bit0 k)"
   433   by (simp_all add: dbl_def numeral.simps minus_add)
   434 
   435 lemma dbl_inc_simps [simp]:
   436   "dbl_inc (- numeral k) = - dbl_dec (numeral k)"
   437   "dbl_inc 0 = 1"
   438   "dbl_inc 1 = 3"
   439   "dbl_inc (- 1) = - 1"
   440   "dbl_inc (numeral k) = numeral (Bit1 k)"
   441   by (simp_all add: dbl_inc_def dbl_dec_def numeral.simps numeral_BitM is_num_normalize algebra_simps del: add_uminus_conv_diff)
   442 
   443 lemma dbl_dec_simps [simp]:
   444   "dbl_dec (- numeral k) = - dbl_inc (numeral k)"
   445   "dbl_dec 0 = - 1"
   446   "dbl_dec 1 = 1"
   447   "dbl_dec (- 1) = - 3"
   448   "dbl_dec (numeral k) = numeral (BitM k)"
   449   by (simp_all add: dbl_dec_def dbl_inc_def numeral.simps numeral_BitM is_num_normalize)
   450 
   451 lemma sub_num_simps [simp]:
   452   "sub One One = 0"
   453   "sub One (Bit0 l) = - numeral (BitM l)"
   454   "sub One (Bit1 l) = - numeral (Bit0 l)"
   455   "sub (Bit0 k) One = numeral (BitM k)"
   456   "sub (Bit1 k) One = numeral (Bit0 k)"
   457   "sub (Bit0 k) (Bit0 l) = dbl (sub k l)"
   458   "sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)"
   459   "sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)"
   460   "sub (Bit1 k) (Bit1 l) = dbl (sub k l)"
   461   by (simp_all add: dbl_def dbl_dec_def dbl_inc_def sub_def numeral.simps
   462     numeral_BitM is_num_normalize del: add_uminus_conv_diff add: diff_conv_add_uminus)
   463 
   464 lemma add_neg_numeral_simps:
   465   "numeral m + - numeral n = sub m n"
   466   "- numeral m + numeral n = sub n m"
   467   "- numeral m + - numeral n = - (numeral m + numeral n)"
   468   by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
   469     del: add_uminus_conv_diff add: diff_conv_add_uminus)
   470 
   471 lemma add_neg_numeral_special:
   472   "1 + - numeral m = sub One m"
   473   "- numeral m + 1 = sub One m"
   474   "numeral m + - 1 = sub m One"
   475   "- 1 + numeral n = sub n One"
   476   "- 1 + - numeral n = - numeral (inc n)"
   477   "- numeral m + - 1 = - numeral (inc m)"
   478   "1 + - 1 = 0"
   479   "- 1 + 1 = 0"
   480   "- 1 + - 1 = - 2"
   481   by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize right_minus numeral_inc
   482     del: add_uminus_conv_diff add: diff_conv_add_uminus)
   483 
   484 lemma diff_numeral_simps:
   485   "numeral m - numeral n = sub m n"
   486   "numeral m - - numeral n = numeral (m + n)"
   487   "- numeral m - numeral n = - numeral (m + n)"
   488   "- numeral m - - numeral n = sub n m"
   489   by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
   490     del: add_uminus_conv_diff add: diff_conv_add_uminus)
   491 
   492 lemma diff_numeral_special:
   493   "1 - numeral n = sub One n"
   494   "numeral m - 1 = sub m One"
   495   "1 - - numeral n = numeral (One + n)"
   496   "- numeral m - 1 = - numeral (m + One)"
   497   "- 1 - numeral n = - numeral (inc n)"
   498   "numeral m - - 1 = numeral (inc m)"
   499   "- 1 - - numeral n = sub n One"
   500   "- numeral m - - 1 = sub One m"
   501   "1 - 1 = 0"
   502   "- 1 - 1 = - 2"
   503   "1 - - 1 = 2"
   504   "- 1 - - 1 = 0"
   505   by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize numeral_inc
   506     del: add_uminus_conv_diff add: diff_conv_add_uminus)
   507 
   508 end
   509 
   510 subsubsection {*
   511   Structures with multiplication: class @{text semiring_numeral}
   512 *}
   513 
   514 class semiring_numeral = semiring + monoid_mult
   515 begin
   516 
   517 subclass numeral ..
   518 
   519 lemma numeral_mult: "numeral (m * n) = numeral m * numeral n"
   520   apply (induct n rule: num_induct)
   521   apply (simp add: numeral_One)
   522   apply (simp add: mult_inc numeral_inc numeral_add distrib_left)
   523   done
   524 
   525 lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)"
   526   by (rule numeral_mult [symmetric])
   527 
   528 lemma mult_2: "2 * z = z + z"
   529   unfolding one_add_one [symmetric] distrib_right by simp
   530 
   531 lemma mult_2_right: "z * 2 = z + z"
   532   unfolding one_add_one [symmetric] distrib_left by simp
   533 
   534 end
   535 
   536 subsubsection {*
   537   Structures with a zero: class @{text semiring_1}
   538 *}
   539 
   540 context semiring_1
   541 begin
   542 
   543 subclass semiring_numeral ..
   544 
   545 lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n"
   546   by (induct n,
   547     simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)
   548 
   549 end
   550 
   551 lemma nat_of_num_numeral [code_abbrev]:
   552   "nat_of_num = numeral"
   553 proof
   554   fix n
   555   have "numeral n = nat_of_num n"
   556     by (induct n) (simp_all add: numeral.simps)
   557   then show "nat_of_num n = numeral n" by simp
   558 qed
   559 
   560 lemma nat_of_num_code [code]:
   561   "nat_of_num One = 1"
   562   "nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)"
   563   "nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))"
   564   by (simp_all add: Let_def)
   565 
   566 subsubsection {*
   567   Equality: class @{text semiring_char_0}
   568 *}
   569 
   570 context semiring_char_0
   571 begin
   572 
   573 lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n"
   574   unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   575     of_nat_eq_iff num_eq_iff ..
   576 
   577 lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One"
   578   by (rule numeral_eq_iff [of n One, unfolded numeral_One])
   579 
   580 lemma one_eq_numeral_iff: "1 = numeral n \<longleftrightarrow> One = n"
   581   by (rule numeral_eq_iff [of One n, unfolded numeral_One])
   582 
   583 lemma numeral_neq_zero: "numeral n \<noteq> 0"
   584   unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
   585   by (simp add: nat_of_num_pos)
   586 
   587 lemma zero_neq_numeral: "0 \<noteq> numeral n"
   588   unfolding eq_commute [of 0] by (rule numeral_neq_zero)
   589 
   590 lemmas eq_numeral_simps [simp] =
   591   numeral_eq_iff
   592   numeral_eq_one_iff
   593   one_eq_numeral_iff
   594   numeral_neq_zero
   595   zero_neq_numeral
   596 
   597 end
   598 
   599 subsubsection {*
   600   Comparisons: class @{text linordered_semidom}
   601 *}
   602 
   603 text {*  Could be perhaps more general than here. *}
   604 
   605 context linordered_semidom
   606 begin
   607 
   608 lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n"
   609 proof -
   610   have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n"
   611     unfolding less_eq_num_def nat_of_num_numeral of_nat_le_iff ..
   612   then show ?thesis by simp
   613 qed
   614 
   615 lemma one_le_numeral: "1 \<le> numeral n"
   616 using numeral_le_iff [of One n] by (simp add: numeral_One)
   617 
   618 lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> One"
   619 using numeral_le_iff [of n One] by (simp add: numeral_One)
   620 
   621 lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n"
   622 proof -
   623   have "of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n"
   624     unfolding less_num_def nat_of_num_numeral of_nat_less_iff ..
   625   then show ?thesis by simp
   626 qed
   627 
   628 lemma not_numeral_less_one: "\<not> numeral n < 1"
   629   using numeral_less_iff [of n One] by (simp add: numeral_One)
   630 
   631 lemma one_less_numeral_iff: "1 < numeral n \<longleftrightarrow> One < n"
   632   using numeral_less_iff [of One n] by (simp add: numeral_One)
   633 
   634 lemma zero_le_numeral: "0 \<le> numeral n"
   635   by (induct n) (simp_all add: numeral.simps)
   636 
   637 lemma zero_less_numeral: "0 < numeral n"
   638   by (induct n) (simp_all add: numeral.simps add_pos_pos)
   639 
   640 lemma not_numeral_le_zero: "\<not> numeral n \<le> 0"
   641   by (simp add: not_le zero_less_numeral)
   642 
   643 lemma not_numeral_less_zero: "\<not> numeral n < 0"
   644   by (simp add: not_less zero_le_numeral)
   645 
   646 lemmas le_numeral_extra =
   647   zero_le_one not_one_le_zero
   648   order_refl [of 0] order_refl [of 1]
   649 
   650 lemmas less_numeral_extra =
   651   zero_less_one not_one_less_zero
   652   less_irrefl [of 0] less_irrefl [of 1]
   653 
   654 lemmas le_numeral_simps [simp] =
   655   numeral_le_iff
   656   one_le_numeral
   657   numeral_le_one_iff
   658   zero_le_numeral
   659   not_numeral_le_zero
   660 
   661 lemmas less_numeral_simps [simp] =
   662   numeral_less_iff
   663   one_less_numeral_iff
   664   not_numeral_less_one
   665   zero_less_numeral
   666   not_numeral_less_zero
   667 
   668 end
   669 
   670 subsubsection {*
   671   Multiplication and negation: class @{text ring_1}
   672 *}
   673 
   674 context ring_1
   675 begin
   676 
   677 subclass neg_numeral ..
   678 
   679 lemma mult_neg_numeral_simps:
   680   "- numeral m * - numeral n = numeral (m * n)"
   681   "- numeral m * numeral n = - numeral (m * n)"
   682   "numeral m * - numeral n = - numeral (m * n)"
   683   unfolding mult_minus_left mult_minus_right
   684   by (simp_all only: minus_minus numeral_mult)
   685 
   686 lemma mult_minus1 [simp]: "- 1 * z = - z"
   687   unfolding numeral.simps mult_minus_left by simp
   688 
   689 lemma mult_minus1_right [simp]: "z * - 1 = - z"
   690   unfolding numeral.simps mult_minus_right by simp
   691 
   692 end
   693 
   694 subsubsection {*
   695   Equality using @{text iszero} for rings with non-zero characteristic
   696 *}
   697 
   698 context ring_1
   699 begin
   700 
   701 definition iszero :: "'a \<Rightarrow> bool"
   702   where "iszero z \<longleftrightarrow> z = 0"
   703 
   704 lemma iszero_0 [simp]: "iszero 0"
   705   by (simp add: iszero_def)
   706 
   707 lemma not_iszero_1 [simp]: "\<not> iszero 1"
   708   by (simp add: iszero_def)
   709 
   710 lemma not_iszero_Numeral1: "\<not> iszero Numeral1"
   711   by (simp add: numeral_One)
   712 
   713 lemma not_iszero_neg_1 [simp]: "\<not> iszero (- 1)"
   714   by (simp add: iszero_def)
   715 
   716 lemma not_iszero_neg_Numeral1: "\<not> iszero (- Numeral1)"
   717   by (simp add: numeral_One)
   718 
   719 lemma iszero_neg_numeral [simp]:
   720   "iszero (- numeral w) \<longleftrightarrow> iszero (numeral w)"
   721   unfolding iszero_def
   722   by (rule neg_equal_0_iff_equal)
   723 
   724 lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)"
   725   unfolding iszero_def by (rule eq_iff_diff_eq_0)
   726 
   727 text {* The @{text "eq_numeral_iff_iszero"} lemmas are not declared
   728 @{text "[simp]"} by default, because for rings of characteristic zero,
   729 better simp rules are possible. For a type like integers mod @{text
   730 "n"}, type-instantiated versions of these rules should be added to the
   731 simplifier, along with a type-specific rule for deciding propositions
   732 of the form @{text "iszero (numeral w)"}.
   733 
   734 bh: Maybe it would not be so bad to just declare these as simp
   735 rules anyway? I should test whether these rules take precedence over
   736 the @{text "ring_char_0"} rules in the simplifier.
   737 *}
   738 
   739 lemma eq_numeral_iff_iszero:
   740   "numeral x = numeral y \<longleftrightarrow> iszero (sub x y)"
   741   "numeral x = - numeral y \<longleftrightarrow> iszero (numeral (x + y))"
   742   "- numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))"
   743   "- numeral x = - numeral y \<longleftrightarrow> iszero (sub y x)"
   744   "numeral x = 1 \<longleftrightarrow> iszero (sub x One)"
   745   "1 = numeral y \<longleftrightarrow> iszero (sub One y)"
   746   "- numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))"
   747   "1 = - numeral y \<longleftrightarrow> iszero (numeral (One + y))"
   748   "numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
   749   "0 = numeral y \<longleftrightarrow> iszero (numeral y)"
   750   "- numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
   751   "0 = - numeral y \<longleftrightarrow> iszero (numeral y)"
   752   unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special
   753   by simp_all
   754 
   755 end
   756 
   757 subsubsection {*
   758   Equality and negation: class @{text ring_char_0}
   759 *}
   760 
   761 class ring_char_0 = ring_1 + semiring_char_0
   762 begin
   763 
   764 lemma not_iszero_numeral [simp]: "\<not> iszero (numeral w)"
   765   by (simp add: iszero_def)
   766 
   767 lemma neg_numeral_eq_iff: "- numeral m = - numeral n \<longleftrightarrow> m = n"
   768   by simp
   769 
   770 lemma numeral_neq_neg_numeral: "numeral m \<noteq> - numeral n"
   771   unfolding eq_neg_iff_add_eq_0
   772   by (simp add: numeral_plus_numeral)
   773 
   774 lemma neg_numeral_neq_numeral: "- numeral m \<noteq> numeral n"
   775   by (rule numeral_neq_neg_numeral [symmetric])
   776 
   777 lemma zero_neq_neg_numeral: "0 \<noteq> - numeral n"
   778   unfolding neg_0_equal_iff_equal by simp
   779 
   780 lemma neg_numeral_neq_zero: "- numeral n \<noteq> 0"
   781   unfolding neg_equal_0_iff_equal by simp
   782 
   783 lemma one_neq_neg_numeral: "1 \<noteq> - numeral n"
   784   using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)
   785 
   786 lemma neg_numeral_neq_one: "- numeral n \<noteq> 1"
   787   using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)
   788 
   789 lemma neg_one_neq_numeral:
   790   "- 1 \<noteq> numeral n"
   791   using neg_numeral_neq_numeral [of One n] by (simp add: numeral_One)
   792 
   793 lemma numeral_neq_neg_one:
   794   "numeral n \<noteq> - 1"
   795   using numeral_neq_neg_numeral [of n One] by (simp add: numeral_One)
   796 
   797 lemma neg_one_eq_numeral_iff:
   798   "- 1 = - numeral n \<longleftrightarrow> n = One"
   799   using neg_numeral_eq_iff [of One n] by (auto simp add: numeral_One)
   800 
   801 lemma numeral_eq_neg_one_iff:
   802   "- numeral n = - 1 \<longleftrightarrow> n = One"
   803   using neg_numeral_eq_iff [of n One] by (auto simp add: numeral_One)
   804 
   805 lemma neg_one_neq_zero:
   806   "- 1 \<noteq> 0"
   807   by simp
   808 
   809 lemma zero_neq_neg_one:
   810   "0 \<noteq> - 1"
   811   by simp
   812 
   813 lemma neg_one_neq_one:
   814   "- 1 \<noteq> 1"
   815   using neg_numeral_neq_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
   816 
   817 lemma one_neq_neg_one:
   818   "1 \<noteq> - 1"
   819   using numeral_neq_neg_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
   820 
   821 lemmas eq_neg_numeral_simps [simp] =
   822   neg_numeral_eq_iff
   823   numeral_neq_neg_numeral neg_numeral_neq_numeral
   824   one_neq_neg_numeral neg_numeral_neq_one
   825   zero_neq_neg_numeral neg_numeral_neq_zero
   826   neg_one_neq_numeral numeral_neq_neg_one
   827   neg_one_eq_numeral_iff numeral_eq_neg_one_iff
   828   neg_one_neq_zero zero_neq_neg_one
   829   neg_one_neq_one one_neq_neg_one
   830 
   831 end
   832 
   833 subsubsection {*
   834   Structures with negation and order: class @{text linordered_idom}
   835 *}
   836 
   837 context linordered_idom
   838 begin
   839 
   840 subclass ring_char_0 ..
   841 
   842 lemma neg_numeral_le_iff: "- numeral m \<le> - numeral n \<longleftrightarrow> n \<le> m"
   843   by (simp only: neg_le_iff_le numeral_le_iff)
   844 
   845 lemma neg_numeral_less_iff: "- numeral m < - numeral n \<longleftrightarrow> n < m"
   846   by (simp only: neg_less_iff_less numeral_less_iff)
   847 
   848 lemma neg_numeral_less_zero: "- numeral n < 0"
   849   by (simp only: neg_less_0_iff_less zero_less_numeral)
   850 
   851 lemma neg_numeral_le_zero: "- numeral n \<le> 0"
   852   by (simp only: neg_le_0_iff_le zero_le_numeral)
   853 
   854 lemma not_zero_less_neg_numeral: "\<not> 0 < - numeral n"
   855   by (simp only: not_less neg_numeral_le_zero)
   856 
   857 lemma not_zero_le_neg_numeral: "\<not> 0 \<le> - numeral n"
   858   by (simp only: not_le neg_numeral_less_zero)
   859 
   860 lemma neg_numeral_less_numeral: "- numeral m < numeral n"
   861   using neg_numeral_less_zero zero_less_numeral by (rule less_trans)
   862 
   863 lemma neg_numeral_le_numeral: "- numeral m \<le> numeral n"
   864   by (simp only: less_imp_le neg_numeral_less_numeral)
   865 
   866 lemma not_numeral_less_neg_numeral: "\<not> numeral m < - numeral n"
   867   by (simp only: not_less neg_numeral_le_numeral)
   868 
   869 lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> - numeral n"
   870   by (simp only: not_le neg_numeral_less_numeral)
   871   
   872 lemma neg_numeral_less_one: "- numeral m < 1"
   873   by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One])
   874 
   875 lemma neg_numeral_le_one: "- numeral m \<le> 1"
   876   by (rule neg_numeral_le_numeral [of m One, unfolded numeral_One])
   877 
   878 lemma not_one_less_neg_numeral: "\<not> 1 < - numeral m"
   879   by (simp only: not_less neg_numeral_le_one)
   880 
   881 lemma not_one_le_neg_numeral: "\<not> 1 \<le> - numeral m"
   882   by (simp only: not_le neg_numeral_less_one)
   883 
   884 lemma not_numeral_less_neg_one: "\<not> numeral m < - 1"
   885   using not_numeral_less_neg_numeral [of m One] by (simp add: numeral_One)
   886 
   887 lemma not_numeral_le_neg_one: "\<not> numeral m \<le> - 1"
   888   using not_numeral_le_neg_numeral [of m One] by (simp add: numeral_One)
   889 
   890 lemma neg_one_less_numeral: "- 1 < numeral m"
   891   using neg_numeral_less_numeral [of One m] by (simp add: numeral_One)
   892 
   893 lemma neg_one_le_numeral: "- 1 \<le> numeral m"
   894   using neg_numeral_le_numeral [of One m] by (simp add: numeral_One)
   895 
   896 lemma neg_numeral_less_neg_one_iff: "- numeral m < - 1 \<longleftrightarrow> m \<noteq> One"
   897   by (cases m) simp_all
   898 
   899 lemma neg_numeral_le_neg_one: "- numeral m \<le> - 1"
   900   by simp
   901 
   902 lemma not_neg_one_less_neg_numeral: "\<not> - 1 < - numeral m"
   903   by simp
   904 
   905 lemma not_neg_one_le_neg_numeral_iff: "\<not> - 1 \<le> - numeral m \<longleftrightarrow> m \<noteq> One"
   906   by (cases m) simp_all
   907 
   908 lemma sub_non_negative:
   909   "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"
   910   by (simp only: sub_def le_diff_eq) simp
   911 
   912 lemma sub_positive:
   913   "sub n m > 0 \<longleftrightarrow> n > m"
   914   by (simp only: sub_def less_diff_eq) simp
   915 
   916 lemma sub_non_positive:
   917   "sub n m \<le> 0 \<longleftrightarrow> n \<le> m"
   918   by (simp only: sub_def diff_le_eq) simp
   919 
   920 lemma sub_negative:
   921   "sub n m < 0 \<longleftrightarrow> n < m"
   922   by (simp only: sub_def diff_less_eq) simp
   923 
   924 lemmas le_neg_numeral_simps [simp] =
   925   neg_numeral_le_iff
   926   neg_numeral_le_numeral not_numeral_le_neg_numeral
   927   neg_numeral_le_zero not_zero_le_neg_numeral
   928   neg_numeral_le_one not_one_le_neg_numeral
   929   neg_one_le_numeral not_numeral_le_neg_one
   930   neg_numeral_le_neg_one not_neg_one_le_neg_numeral_iff
   931 
   932 lemma le_minus_one_simps [simp]:
   933   "- 1 \<le> 0"
   934   "- 1 \<le> 1"
   935   "\<not> 0 \<le> - 1"
   936   "\<not> 1 \<le> - 1"
   937   by simp_all
   938 
   939 lemmas less_neg_numeral_simps [simp] =
   940   neg_numeral_less_iff
   941   neg_numeral_less_numeral not_numeral_less_neg_numeral
   942   neg_numeral_less_zero not_zero_less_neg_numeral
   943   neg_numeral_less_one not_one_less_neg_numeral
   944   neg_one_less_numeral not_numeral_less_neg_one
   945   neg_numeral_less_neg_one_iff not_neg_one_less_neg_numeral
   946 
   947 lemma less_minus_one_simps [simp]:
   948   "- 1 < 0"
   949   "- 1 < 1"
   950   "\<not> 0 < - 1"
   951   "\<not> 1 < - 1"
   952   by (simp_all add: less_le)
   953 
   954 lemma abs_numeral [simp]: "abs (numeral n) = numeral n"
   955   by simp
   956 
   957 lemma abs_neg_numeral [simp]: "abs (- numeral n) = numeral n"
   958   by (simp only: abs_minus_cancel abs_numeral)
   959 
   960 lemma abs_neg_one [simp]:
   961   "abs (- 1) = 1"
   962   by simp
   963 
   964 end
   965 
   966 subsubsection {*
   967   Natural numbers
   968 *}
   969 
   970 lemma Suc_1 [simp]: "Suc 1 = 2"
   971   unfolding Suc_eq_plus1 by (rule one_add_one)
   972 
   973 lemma Suc_numeral [simp]: "Suc (numeral n) = numeral (n + One)"
   974   unfolding Suc_eq_plus1 by (rule numeral_plus_one)
   975 
   976 definition pred_numeral :: "num \<Rightarrow> nat"
   977   where [code del]: "pred_numeral k = numeral k - 1"
   978 
   979 lemma numeral_eq_Suc: "numeral k = Suc (pred_numeral k)"
   980   unfolding pred_numeral_def by simp
   981 
   982 lemma eval_nat_numeral:
   983   "numeral One = Suc 0"
   984   "numeral (Bit0 n) = Suc (numeral (BitM n))"
   985   "numeral (Bit1 n) = Suc (numeral (Bit0 n))"
   986   by (simp_all add: numeral.simps BitM_plus_one)
   987 
   988 lemma pred_numeral_simps [simp]:
   989   "pred_numeral One = 0"
   990   "pred_numeral (Bit0 k) = numeral (BitM k)"
   991   "pred_numeral (Bit1 k) = numeral (Bit0 k)"
   992   unfolding pred_numeral_def eval_nat_numeral
   993   by (simp_all only: diff_Suc_Suc diff_0)
   994 
   995 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
   996   by (simp add: eval_nat_numeral)
   997 
   998 lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
   999   by (simp add: eval_nat_numeral)
  1000 
  1001 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
  1002   by (simp only: numeral_One One_nat_def)
  1003 
  1004 lemma Suc_nat_number_of_add:
  1005   "Suc (numeral v + n) = numeral (v + One) + n"
  1006   by simp
  1007 
  1008 (*Maps #n to n for n = 1, 2*)
  1009 lemmas numerals = numeral_One [where 'a=nat] numeral_2_eq_2
  1010 
  1011 text {* Comparisons involving @{term Suc}. *}
  1012 
  1013 lemma eq_numeral_Suc [simp]: "numeral k = Suc n \<longleftrightarrow> pred_numeral k = n"
  1014   by (simp add: numeral_eq_Suc)
  1015 
  1016 lemma Suc_eq_numeral [simp]: "Suc n = numeral k \<longleftrightarrow> n = pred_numeral k"
  1017   by (simp add: numeral_eq_Suc)
  1018 
  1019 lemma less_numeral_Suc [simp]: "numeral k < Suc n \<longleftrightarrow> pred_numeral k < n"
  1020   by (simp add: numeral_eq_Suc)
  1021 
  1022 lemma less_Suc_numeral [simp]: "Suc n < numeral k \<longleftrightarrow> n < pred_numeral k"
  1023   by (simp add: numeral_eq_Suc)
  1024 
  1025 lemma le_numeral_Suc [simp]: "numeral k \<le> Suc n \<longleftrightarrow> pred_numeral k \<le> n"
  1026   by (simp add: numeral_eq_Suc)
  1027 
  1028 lemma le_Suc_numeral [simp]: "Suc n \<le> numeral k \<longleftrightarrow> n \<le> pred_numeral k"
  1029   by (simp add: numeral_eq_Suc)
  1030 
  1031 lemma diff_Suc_numeral [simp]: "Suc n - numeral k = n - pred_numeral k"
  1032   by (simp add: numeral_eq_Suc)
  1033 
  1034 lemma diff_numeral_Suc [simp]: "numeral k - Suc n = pred_numeral k - n"
  1035   by (simp add: numeral_eq_Suc)
  1036 
  1037 lemma max_Suc_numeral [simp]:
  1038   "max (Suc n) (numeral k) = Suc (max n (pred_numeral k))"
  1039   by (simp add: numeral_eq_Suc)
  1040 
  1041 lemma max_numeral_Suc [simp]:
  1042   "max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)"
  1043   by (simp add: numeral_eq_Suc)
  1044 
  1045 lemma min_Suc_numeral [simp]:
  1046   "min (Suc n) (numeral k) = Suc (min n (pred_numeral k))"
  1047   by (simp add: numeral_eq_Suc)
  1048 
  1049 lemma min_numeral_Suc [simp]:
  1050   "min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)"
  1051   by (simp add: numeral_eq_Suc)
  1052 
  1053 text {* For @{term case_nat} and @{term rec_nat}. *}
  1054 
  1055 lemma case_nat_numeral [simp]:
  1056   "case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)"
  1057   by (simp add: numeral_eq_Suc)
  1058 
  1059 lemma case_nat_add_eq_if [simp]:
  1060   "case_nat a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))"
  1061   by (simp add: numeral_eq_Suc)
  1062 
  1063 lemma rec_nat_numeral [simp]:
  1064   "rec_nat a f (numeral v) =
  1065     (let pv = pred_numeral v in f pv (rec_nat a f pv))"
  1066   by (simp add: numeral_eq_Suc Let_def)
  1067 
  1068 lemma rec_nat_add_eq_if [simp]:
  1069   "rec_nat a f (numeral v + n) =
  1070     (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))"
  1071   by (simp add: numeral_eq_Suc Let_def)
  1072 
  1073 text {* Case analysis on @{term "n < 2"} *}
  1074 
  1075 lemma less_2_cases: "n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0"
  1076   by (auto simp add: numeral_2_eq_2)
  1077 
  1078 text {* Removal of Small Numerals: 0, 1 and (in additive positions) 2 *}
  1079 text {* bh: Are these rules really a good idea? *}
  1080 
  1081 lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
  1082   by simp
  1083 
  1084 lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
  1085   by simp
  1086 
  1087 text {* Can be used to eliminate long strings of Sucs, but not by default. *}
  1088 
  1089 lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
  1090   by simp
  1091 
  1092 lemmas nat_1_add_1 = one_add_one [where 'a=nat] (* legacy *)
  1093 
  1094 
  1095 subsection {* Numeral equations as default simplification rules *}
  1096 
  1097 declare (in numeral) numeral_One [simp]
  1098 declare (in numeral) numeral_plus_numeral [simp]
  1099 declare (in numeral) add_numeral_special [simp]
  1100 declare (in neg_numeral) add_neg_numeral_simps [simp]
  1101 declare (in neg_numeral) add_neg_numeral_special [simp]
  1102 declare (in neg_numeral) diff_numeral_simps [simp]
  1103 declare (in neg_numeral) diff_numeral_special [simp]
  1104 declare (in semiring_numeral) numeral_times_numeral [simp]
  1105 declare (in ring_1) mult_neg_numeral_simps [simp]
  1106 
  1107 subsection {* Setting up simprocs *}
  1108 
  1109 lemma mult_numeral_1: "Numeral1 * a = (a::'a::semiring_numeral)"
  1110   by simp
  1111 
  1112 lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::semiring_numeral)"
  1113   by simp
  1114 
  1115 lemma divide_numeral_1: "a / Numeral1 = (a::'a::field)"
  1116   by simp
  1117 
  1118 lemma inverse_numeral_1:
  1119   "inverse Numeral1 = (Numeral1::'a::division_ring)"
  1120   by simp
  1121 
  1122 text{*Theorem lists for the cancellation simprocs. The use of a binary
  1123 numeral for 1 reduces the number of special cases.*}
  1124 
  1125 lemma mult_1s:
  1126   fixes a :: "'a::semiring_numeral"
  1127     and b :: "'b::ring_1"
  1128   shows "Numeral1 * a = a"
  1129     "a * Numeral1 = a"
  1130     "- Numeral1 * b = - b"
  1131     "b * - Numeral1 = - b"
  1132   by simp_all
  1133 
  1134 setup {*
  1135   Reorient_Proc.add
  1136     (fn Const (@{const_name numeral}, _) $ _ => true
  1137     | Const (@{const_name uminus}, _) $ (Const (@{const_name numeral}, _) $ _) => true
  1138     | _ => false)
  1139 *}
  1140 
  1141 simproc_setup reorient_numeral
  1142   ("numeral w = x" | "- numeral w = y") = Reorient_Proc.proc
  1143 
  1144 
  1145 subsubsection {* Simplification of arithmetic operations on integer constants. *}
  1146 
  1147 lemmas arith_special = (* already declared simp above *)
  1148   add_numeral_special add_neg_numeral_special
  1149   diff_numeral_special
  1150 
  1151 (* rules already in simpset *)
  1152 lemmas arith_extra_simps =
  1153   numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right
  1154   minus_zero
  1155   diff_numeral_simps diff_0 diff_0_right
  1156   numeral_times_numeral mult_neg_numeral_simps
  1157   mult_zero_left mult_zero_right
  1158   abs_numeral abs_neg_numeral
  1159 
  1160 text {*
  1161   For making a minimal simpset, one must include these default simprules.
  1162   Also include @{text simp_thms}.
  1163 *}
  1164 
  1165 lemmas arith_simps =
  1166   add_num_simps mult_num_simps sub_num_simps
  1167   BitM.simps dbl_simps dbl_inc_simps dbl_dec_simps
  1168   abs_zero abs_one arith_extra_simps
  1169 
  1170 lemmas more_arith_simps =
  1171   neg_le_iff_le
  1172   minus_zero left_minus right_minus
  1173   mult_1_left mult_1_right
  1174   mult_minus_left mult_minus_right
  1175   minus_add_distrib minus_minus mult.assoc
  1176 
  1177 lemmas of_nat_simps =
  1178   of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
  1179 
  1180 text {* Simplification of relational operations *}
  1181 
  1182 lemmas eq_numeral_extra =
  1183   zero_neq_one one_neq_zero
  1184 
  1185 lemmas rel_simps =
  1186   le_num_simps less_num_simps eq_num_simps
  1187   le_numeral_simps le_neg_numeral_simps le_minus_one_simps le_numeral_extra
  1188   less_numeral_simps less_neg_numeral_simps less_minus_one_simps less_numeral_extra
  1189   eq_numeral_simps eq_neg_numeral_simps eq_numeral_extra
  1190 
  1191 lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"
  1192   -- {* Unfold all @{text let}s involving constants *}
  1193   unfolding Let_def ..
  1194 
  1195 lemma Let_neg_numeral [simp]: "Let (- numeral v) f = f (- numeral v)"
  1196   -- {* Unfold all @{text let}s involving constants *}
  1197   unfolding Let_def ..
  1198 
  1199 declaration {*
  1200 let 
  1201   fun number_of thy T n =
  1202     if not (Sign.of_sort thy (T, @{sort numeral}))
  1203     then raise CTERM ("number_of", [])
  1204     else Numeral.mk_cnumber (Thm.ctyp_of thy T) n;
  1205 in
  1206   K (
  1207     Lin_Arith.add_simps (@{thms arith_simps} @ @{thms more_arith_simps}
  1208       @ @{thms rel_simps}
  1209       @ @{thms pred_numeral_simps}
  1210       @ @{thms arith_special numeral_One}
  1211       @ @{thms of_nat_simps})
  1212     #> Lin_Arith.add_simps [@{thm Suc_numeral},
  1213       @{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
  1214       @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
  1215       @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
  1216       @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
  1217       @{thm mult_Suc}, @{thm mult_Suc_right},
  1218       @{thm of_nat_numeral}]
  1219     #> Lin_Arith.set_number_of number_of)
  1220 end
  1221 *}
  1222 
  1223 
  1224 subsubsection {* Simplification of arithmetic when nested to the right. *}
  1225 
  1226 lemma add_numeral_left [simp]:
  1227   "numeral v + (numeral w + z) = (numeral(v + w) + z)"
  1228   by (simp_all add: add.assoc [symmetric])
  1229 
  1230 lemma add_neg_numeral_left [simp]:
  1231   "numeral v + (- numeral w + y) = (sub v w + y)"
  1232   "- numeral v + (numeral w + y) = (sub w v + y)"
  1233   "- numeral v + (- numeral w + y) = (- numeral(v + w) + y)"
  1234   by (simp_all add: add.assoc [symmetric])
  1235 
  1236 lemma mult_numeral_left [simp]:
  1237   "numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)"
  1238   "- numeral v * (numeral w * y) = (- numeral(v * w) * y :: 'b::ring_1)"
  1239   "numeral v * (- numeral w * y) = (- numeral(v * w) * y :: 'b::ring_1)"
  1240   "- numeral v * (- numeral w * y) = (numeral(v * w) * y :: 'b::ring_1)"
  1241   by (simp_all add: mult.assoc [symmetric])
  1242 
  1243 hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec
  1244 
  1245 
  1246 subsection {* code module namespace *}
  1247 
  1248 code_identifier
  1249   code_module Num \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  1250 
  1251 end