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