dropped relics of ancient binary numeral case study
authorhaftmann
Mon Jun 10 20:30:23 2013 +0200 (2013-06-10)
changeset 5236341d7946e2595
parent 52362 6b80ba92c4fe
child 52364 3bed446c305b
dropped relics of ancient binary numeral case study
src/HOL/ROOT
src/HOL/ex/Numeral_Representation.thy
     1.1 --- a/src/HOL/ROOT	Mon Jun 10 16:04:34 2013 +0200
     1.2 +++ b/src/HOL/ROOT	Mon Jun 10 20:30:23 2013 +0200
     1.3 @@ -502,7 +502,6 @@
     1.4    theories
     1.5      Iff_Oracle
     1.6      Coercion_Examples
     1.7 -    Numeral_Representation
     1.8      Higher_Order_Logic
     1.9      Abstract_NAT
    1.10      Guess
     2.1 --- a/src/HOL/ex/Numeral_Representation.thy	Mon Jun 10 16:04:34 2013 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,973 +0,0 @@
     2.4 -(*  Title:      HOL/ex/Numeral_Representation.thy
     2.5 -    Author:     Florian Haftmann
     2.6 -*)
     2.7 -
     2.8 -header {* First experiments for a numeral representation (now obsolete). *}
     2.9 -
    2.10 -theory Numeral_Representation
    2.11 -imports Main
    2.12 -begin
    2.13 -
    2.14 -subsection {* The @{text num} type *}
    2.15 -
    2.16 -datatype num = One | Dig0 num | Dig1 num
    2.17 -
    2.18 -text {* Increment function for type @{typ num} *}
    2.19 -
    2.20 -primrec inc :: "num \<Rightarrow> num" where
    2.21 -  "inc One = Dig0 One"
    2.22 -| "inc (Dig0 x) = Dig1 x"
    2.23 -| "inc (Dig1 x) = Dig0 (inc x)"
    2.24 -
    2.25 -text {* Converting between type @{typ num} and type @{typ nat} *}
    2.26 -
    2.27 -primrec nat_of_num :: "num \<Rightarrow> nat" where
    2.28 -  "nat_of_num One = Suc 0"
    2.29 -| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x"
    2.30 -| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)"
    2.31 -
    2.32 -primrec num_of_nat :: "nat \<Rightarrow> num" where
    2.33 -  "num_of_nat 0 = One"
    2.34 -| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
    2.35 -
    2.36 -lemma nat_of_num_pos: "0 < nat_of_num x"
    2.37 -  by (induct x) simp_all
    2.38 -
    2.39 -lemma nat_of_num_neq_0: "nat_of_num x \<noteq> 0"
    2.40 -  by (induct x) simp_all
    2.41 -
    2.42 -lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
    2.43 -  by (induct x) simp_all
    2.44 -
    2.45 -lemma num_of_nat_double:
    2.46 -  "0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)"
    2.47 -  by (induct n) simp_all
    2.48 -
    2.49 -text {*
    2.50 -  Type @{typ num} is isomorphic to the strictly positive
    2.51 -  natural numbers.
    2.52 -*}
    2.53 -
    2.54 -lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
    2.55 -  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
    2.56 -
    2.57 -lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
    2.58 -  by (induct n) (simp_all add: nat_of_num_inc)
    2.59 -
    2.60 -lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
    2.61 -proof
    2.62 -  assume "nat_of_num x = nat_of_num y"
    2.63 -  then have "num_of_nat (nat_of_num x) = num_of_nat (nat_of_num y)" by simp
    2.64 -  then show "x = y" by (simp add: nat_of_num_inverse)
    2.65 -qed simp
    2.66 -
    2.67 -lemma num_induct [case_names One inc]:
    2.68 -  fixes P :: "num \<Rightarrow> bool"
    2.69 -  assumes One: "P One"
    2.70 -    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
    2.71 -  shows "P x"
    2.72 -proof -
    2.73 -  obtain n where n: "Suc n = nat_of_num x"
    2.74 -    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
    2.75 -  have "P (num_of_nat (Suc n))"
    2.76 -  proof (induct n)
    2.77 -    case 0 show ?case using One by simp
    2.78 -  next
    2.79 -    case (Suc n)
    2.80 -    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
    2.81 -    then show "P (num_of_nat (Suc (Suc n)))" by simp
    2.82 -  qed
    2.83 -  with n show "P x"
    2.84 -    by (simp add: nat_of_num_inverse)
    2.85 -qed
    2.86 -
    2.87 -text {*
    2.88 -  From now on, there are two possible models for @{typ num}: as
    2.89 -  positive naturals (rule @{text "num_induct"}) and as digit
    2.90 -  representation (rules @{text "num.induct"}, @{text "num.cases"}).
    2.91 -
    2.92 -  It is not entirely clear in which context it is better to use the
    2.93 -  one or the other, or whether the construction should be reversed.
    2.94 -*}
    2.95 -
    2.96 -
    2.97 -subsection {* Numeral operations *}
    2.98 -
    2.99 -ML {*
   2.100 -structure Dig_Simps = Named_Thms
   2.101 -(
   2.102 -  val name = @{binding numeral}
   2.103 -  val description = "simplification rules for numerals"
   2.104 -)
   2.105 -*}
   2.106 -
   2.107 -setup Dig_Simps.setup
   2.108 -
   2.109 -instantiation num :: "{plus,times,ord}"
   2.110 -begin
   2.111 -
   2.112 -definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
   2.113 -  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
   2.114 -
   2.115 -definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
   2.116 -  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
   2.117 -
   2.118 -definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
   2.119 -  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
   2.120 -
   2.121 -definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
   2.122 -  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
   2.123 -
   2.124 -instance ..
   2.125 -
   2.126 -end
   2.127 -
   2.128 -lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
   2.129 -  unfolding plus_num_def
   2.130 -  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
   2.131 -
   2.132 -lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
   2.133 -  unfolding times_num_def
   2.134 -  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
   2.135 -
   2.136 -lemma Dig_plus [numeral, simp, code]:
   2.137 -  "One + One = Dig0 One"
   2.138 -  "One + Dig0 m = Dig1 m"
   2.139 -  "One + Dig1 m = Dig0 (m + One)"
   2.140 -  "Dig0 n + One = Dig1 n"
   2.141 -  "Dig0 n + Dig0 m = Dig0 (n + m)"
   2.142 -  "Dig0 n + Dig1 m = Dig1 (n + m)"
   2.143 -  "Dig1 n + One = Dig0 (n + One)"
   2.144 -  "Dig1 n + Dig0 m = Dig1 (n + m)"
   2.145 -  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
   2.146 -  by (simp_all add: num_eq_iff nat_of_num_add)
   2.147 -
   2.148 -lemma Dig_times [numeral, simp, code]:
   2.149 -  "One * One = One"
   2.150 -  "One * Dig0 n = Dig0 n"
   2.151 -  "One * Dig1 n = Dig1 n"
   2.152 -  "Dig0 n * One = Dig0 n"
   2.153 -  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
   2.154 -  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
   2.155 -  "Dig1 n * One = Dig1 n"
   2.156 -  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
   2.157 -  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
   2.158 -  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
   2.159 -                    distrib_right distrib_left)
   2.160 -
   2.161 -lemma less_eq_num_code [numeral, simp, code]:
   2.162 -  "One \<le> n \<longleftrightarrow> True"
   2.163 -  "Dig0 m \<le> One \<longleftrightarrow> False"
   2.164 -  "Dig1 m \<le> One \<longleftrightarrow> False"
   2.165 -  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
   2.166 -  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
   2.167 -  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
   2.168 -  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
   2.169 -  using nat_of_num_pos [of n] nat_of_num_pos [of m]
   2.170 -  by (auto simp add: less_eq_num_def less_num_def)
   2.171 -
   2.172 -lemma less_num_code [numeral, simp, code]:
   2.173 -  "m < One \<longleftrightarrow> False"
   2.174 -  "One < One \<longleftrightarrow> False"
   2.175 -  "One < Dig0 n \<longleftrightarrow> True"
   2.176 -  "One < Dig1 n \<longleftrightarrow> True"
   2.177 -  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
   2.178 -  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
   2.179 -  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
   2.180 -  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
   2.181 -  using nat_of_num_pos [of n] nat_of_num_pos [of m]
   2.182 -  by (auto simp add: less_eq_num_def less_num_def)
   2.183 -
   2.184 -text {* Rules using @{text One} and @{text inc} as constructors *}
   2.185 -
   2.186 -lemma add_One: "x + One = inc x"
   2.187 -  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   2.188 -
   2.189 -lemma add_inc: "x + inc y = inc (x + y)"
   2.190 -  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   2.191 -
   2.192 -lemma mult_One: "x * One = x"
   2.193 -  by (simp add: num_eq_iff nat_of_num_mult)
   2.194 -
   2.195 -lemma mult_inc: "x * inc y = x * y + x"
   2.196 -  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
   2.197 -
   2.198 -text {* A double-and-decrement function *}
   2.199 -
   2.200 -primrec DigM :: "num \<Rightarrow> num" where
   2.201 -  "DigM One = One"
   2.202 -| "DigM (Dig0 n) = Dig1 (DigM n)"
   2.203 -| "DigM (Dig1 n) = Dig1 (Dig0 n)"
   2.204 -
   2.205 -lemma DigM_plus_one: "DigM n + One = Dig0 n"
   2.206 -  by (induct n) simp_all
   2.207 -
   2.208 -lemma add_One_commute: "One + n = n + One"
   2.209 -  by (induct n) simp_all
   2.210 -
   2.211 -lemma one_plus_DigM: "One + DigM n = Dig0 n"
   2.212 -  by (simp add: add_One_commute DigM_plus_one)
   2.213 -
   2.214 -text {* Squaring and exponentiation *}
   2.215 -
   2.216 -primrec square :: "num \<Rightarrow> num" where
   2.217 -  "square One = One"
   2.218 -| "square (Dig0 n) = Dig0 (Dig0 (square n))"
   2.219 -| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
   2.220 -
   2.221 -primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   2.222 -  "pow x One = x"
   2.223 -| "pow x (Dig0 y) = square (pow x y)"
   2.224 -| "pow x (Dig1 y) = x * square (pow x y)"
   2.225 -
   2.226 -
   2.227 -subsection {* Binary numerals *}
   2.228 -
   2.229 -text {*
   2.230 -  We embed binary representations into a generic algebraic
   2.231 -  structure using @{text of_num}.
   2.232 -*}
   2.233 -
   2.234 -class semiring_numeral = semiring + monoid_mult
   2.235 -begin
   2.236 -
   2.237 -primrec of_num :: "num \<Rightarrow> 'a" where
   2.238 -  of_num_One [numeral]: "of_num One = 1"
   2.239 -| "of_num (Dig0 n) = of_num n + of_num n"
   2.240 -| "of_num (Dig1 n) = of_num n + of_num n + 1"
   2.241 -
   2.242 -lemma of_num_inc: "of_num (inc n) = of_num n + 1"
   2.243 -  by (induct n) (simp_all add: add_ac)
   2.244 -
   2.245 -lemma of_num_add: "of_num (m + n) = of_num m + of_num n"
   2.246 -  by (induct n rule: num_induct) (simp_all add: add_One add_inc of_num_inc add_ac)
   2.247 -
   2.248 -lemma of_num_mult: "of_num (m * n) = of_num m * of_num n"
   2.249 -  by (induct n rule: num_induct) (simp_all add: mult_One mult_inc of_num_add of_num_inc distrib_left)
   2.250 -
   2.251 -declare of_num.simps [simp del]
   2.252 -
   2.253 -end
   2.254 -
   2.255 -ML {*
   2.256 -fun mk_num k =
   2.257 -  if k > 1 then
   2.258 -    let
   2.259 -      val (l, b) = Integer.div_mod k 2;
   2.260 -      val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
   2.261 -    in bit $ (mk_num l) end
   2.262 -  else if k = 1 then @{term One}
   2.263 -  else error ("mk_num " ^ string_of_int k);
   2.264 -
   2.265 -fun dest_num @{term One} = 1
   2.266 -  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
   2.267 -  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1
   2.268 -  | dest_num t = raise TERM ("dest_num", [t]);
   2.269 -
   2.270 -fun mk_numeral phi T k = Morphism.term phi (Const (@{const_name of_num}, @{typ num} --> T))
   2.271 -  $ mk_num k
   2.272 -
   2.273 -fun dest_numeral phi (u $ t) =
   2.274 -  if Term.aconv_untyped (u, Morphism.term phi (Const (@{const_name of_num}, dummyT)))
   2.275 -  then (range_type (fastype_of u), dest_num t)
   2.276 -  else raise TERM ("dest_numeral", [u, t]);
   2.277 -*}
   2.278 -
   2.279 -syntax
   2.280 -  "_Numerals" :: "xnum_token \<Rightarrow> 'a"    ("_")
   2.281 -
   2.282 -parse_translation {*
   2.283 -  let
   2.284 -    fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
   2.285 -       of (0, 1) => Const (@{const_name One}, dummyT)
   2.286 -        | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
   2.287 -        | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
   2.288 -      else raise Match;
   2.289 -    fun numeral_tr [Free (num, _)] =
   2.290 -          let
   2.291 -            val {leading_zeros, value, ...} = Lexicon.read_xnum num;
   2.292 -            val _ = leading_zeros = 0 andalso value > 0
   2.293 -              orelse error ("Bad numeral: " ^ num);
   2.294 -          in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
   2.295 -      | numeral_tr ts = raise TERM ("numeral_tr", ts);
   2.296 -  in [(@{syntax_const "_Numerals"}, K numeral_tr)] end
   2.297 -*}
   2.298 -
   2.299 -typed_print_translation {*
   2.300 -  let
   2.301 -    fun dig b n = b + 2 * n; 
   2.302 -    fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
   2.303 -          dig 0 (int_of_num' n)
   2.304 -      | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
   2.305 -          dig 1 (int_of_num' n)
   2.306 -      | int_of_num' (Const (@{const_syntax One}, _)) = 1;
   2.307 -    fun num_tr' ctxt T [n] =
   2.308 -      let
   2.309 -        val k = int_of_num' n;
   2.310 -        val t' = Syntax.const @{syntax_const "_Numerals"} $ Syntax.free ("#" ^ string_of_int k);
   2.311 -      in
   2.312 -        (case T of
   2.313 -          Type (@{type_name fun}, [_, T']) =>
   2.314 -            if Printer.type_emphasis ctxt T' then
   2.315 -              Syntax.const @{syntax_const "_constrain"} $ t' $
   2.316 -                Syntax_Phases.term_of_typ ctxt T'
   2.317 -            else t'
   2.318 -        | T' => if T' = dummyT then t' else raise Match)
   2.319 -      end;
   2.320 -  in [(@{const_syntax of_num}, num_tr')] end
   2.321 -*}
   2.322 -
   2.323 -
   2.324 -subsection {* Class-specific numeral rules *}
   2.325 -
   2.326 -subsubsection {* Class @{text semiring_numeral} *}
   2.327 -
   2.328 -context semiring_numeral
   2.329 -begin
   2.330 -
   2.331 -abbreviation "Num1 \<equiv> of_num One"
   2.332 -
   2.333 -text {*
   2.334 -  Alas, there is still the duplication of @{term 1}, although the
   2.335 -  duplicated @{term 0} has disappeared.  We could get rid of it by
   2.336 -  replacing the constructor @{term 1} in @{typ num} by two
   2.337 -  constructors @{text two} and @{text three}, resulting in a further
   2.338 -  blow-up.  But it could be worth the effort.
   2.339 -*}
   2.340 -
   2.341 -lemma of_num_plus_one [numeral]:
   2.342 -  "of_num n + 1 = of_num (n + One)"
   2.343 -  by (simp only: of_num_add of_num_One)
   2.344 -
   2.345 -lemma of_num_one_plus [numeral]:
   2.346 -  "1 + of_num n = of_num (One + n)"
   2.347 -  by (simp only: of_num_add of_num_One)
   2.348 -
   2.349 -lemma of_num_plus [numeral]:
   2.350 -  "of_num m + of_num n = of_num (m + n)"
   2.351 -  by (simp only: of_num_add)
   2.352 -
   2.353 -lemma of_num_times_one [numeral]:
   2.354 -  "of_num n * 1 = of_num n"
   2.355 -  by simp
   2.356 -
   2.357 -lemma of_num_one_times [numeral]:
   2.358 -  "1 * of_num n = of_num n"
   2.359 -  by simp
   2.360 -
   2.361 -lemma of_num_times [numeral]:
   2.362 -  "of_num m * of_num n = of_num (m * n)"
   2.363 -  unfolding of_num_mult ..
   2.364 -
   2.365 -end
   2.366 -
   2.367 -
   2.368 -subsubsection {* Structures with a zero: class @{text semiring_1} *}
   2.369 -
   2.370 -context semiring_1
   2.371 -begin
   2.372 -
   2.373 -subclass semiring_numeral ..
   2.374 -
   2.375 -lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
   2.376 -  by (induct n)
   2.377 -    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
   2.378 -
   2.379 -declare of_nat_1 [numeral]
   2.380 -
   2.381 -lemma Dig_plus_zero [numeral]:
   2.382 -  "0 + 1 = 1"
   2.383 -  "0 + of_num n = of_num n"
   2.384 -  "1 + 0 = 1"
   2.385 -  "of_num n + 0 = of_num n"
   2.386 -  by simp_all
   2.387 -
   2.388 -lemma Dig_times_zero [numeral]:
   2.389 -  "0 * 1 = 0"
   2.390 -  "0 * of_num n = 0"
   2.391 -  "1 * 0 = 0"
   2.392 -  "of_num n * 0 = 0"
   2.393 -  by simp_all
   2.394 -
   2.395 -end
   2.396 -
   2.397 -lemma nat_of_num_of_num: "nat_of_num = of_num"
   2.398 -proof
   2.399 -  fix n
   2.400 -  have "of_num n = nat_of_num n"
   2.401 -    by (induct n) (simp_all add: of_num.simps)
   2.402 -  then show "nat_of_num n = of_num n" by simp
   2.403 -qed
   2.404 -
   2.405 -
   2.406 -subsubsection {* Equality: class @{text semiring_char_0} *}
   2.407 -
   2.408 -context semiring_char_0
   2.409 -begin
   2.410 -
   2.411 -lemma of_num_eq_iff [numeral]: "of_num m = of_num n \<longleftrightarrow> m = n"
   2.412 -  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
   2.413 -    of_nat_eq_iff num_eq_iff ..
   2.414 -
   2.415 -lemma of_num_eq_one_iff [numeral]: "of_num n = 1 \<longleftrightarrow> n = One"
   2.416 -  using of_num_eq_iff [of n One] by (simp add: of_num_One)
   2.417 -
   2.418 -lemma one_eq_of_num_iff [numeral]: "1 = of_num n \<longleftrightarrow> One = n"
   2.419 -  using of_num_eq_iff [of One n] by (simp add: of_num_One)
   2.420 -
   2.421 -end
   2.422 -
   2.423 -
   2.424 -subsubsection {* Comparisons: class @{text linordered_semidom} *}
   2.425 -
   2.426 -text {*
   2.427 -  Perhaps the underlying structure could even 
   2.428 -  be more general than @{text linordered_semidom}.
   2.429 -*}
   2.430 -
   2.431 -context linordered_semidom
   2.432 -begin
   2.433 -
   2.434 -lemma of_num_pos [numeral]: "0 < of_num n"
   2.435 -  by (induct n) (simp_all add: of_num.simps add_pos_pos)
   2.436 -
   2.437 -lemma of_num_not_zero [numeral]: "of_num n \<noteq> 0"
   2.438 -  using of_num_pos [of n] by simp
   2.439 -
   2.440 -lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
   2.441 -proof -
   2.442 -  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
   2.443 -    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
   2.444 -  then show ?thesis by (simp add: of_nat_of_num)
   2.445 -qed
   2.446 -
   2.447 -lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n \<le> One"
   2.448 -  using of_num_less_eq_iff [of n One] by (simp add: of_num_One)
   2.449 -
   2.450 -lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
   2.451 -  using of_num_less_eq_iff [of One n] by (simp add: of_num_One)
   2.452 -
   2.453 -lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
   2.454 -proof -
   2.455 -  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
   2.456 -    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
   2.457 -  then show ?thesis by (simp add: of_nat_of_num)
   2.458 -qed
   2.459 -
   2.460 -lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
   2.461 -  using of_num_less_iff [of n One] by (simp add: of_num_One)
   2.462 -
   2.463 -lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> One < n"
   2.464 -  using of_num_less_iff [of One n] by (simp add: of_num_One)
   2.465 -
   2.466 -lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
   2.467 -  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
   2.468 -
   2.469 -lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
   2.470 -  by (simp add: not_less of_num_nonneg)
   2.471 -
   2.472 -lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
   2.473 -  by (simp add: not_le of_num_pos)
   2.474 -
   2.475 -end
   2.476 -
   2.477 -context linordered_idom
   2.478 -begin
   2.479 -
   2.480 -lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
   2.481 -proof -
   2.482 -  have "- of_num m < 0" by (simp add: of_num_pos)
   2.483 -  also have "0 < of_num n" by (simp add: of_num_pos)
   2.484 -  finally show ?thesis .
   2.485 -qed
   2.486 -
   2.487 -lemma minus_of_num_not_equal_of_num: "- of_num m \<noteq> of_num n"
   2.488 -  using minus_of_num_less_of_num_iff [of m n] by simp
   2.489 -
   2.490 -lemma minus_of_num_less_one_iff: "- of_num n < 1"
   2.491 -  using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_One)
   2.492 -
   2.493 -lemma minus_one_less_of_num_iff: "- 1 < of_num n"
   2.494 -  using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_One)
   2.495 -
   2.496 -lemma minus_one_less_one_iff: "- 1 < 1"
   2.497 -  using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_One)
   2.498 -
   2.499 -lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
   2.500 -  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
   2.501 -
   2.502 -lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
   2.503 -  by (simp add: less_imp_le minus_of_num_less_one_iff)
   2.504 -
   2.505 -lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
   2.506 -  by (simp only: less_imp_le minus_one_less_of_num_iff)
   2.507 -
   2.508 -lemma minus_one_le_one_iff: "- 1 \<le> 1"
   2.509 -  by (simp add: less_imp_le minus_one_less_one_iff)
   2.510 -
   2.511 -lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
   2.512 -  by (simp add: not_le minus_of_num_less_of_num_iff)
   2.513 -
   2.514 -lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
   2.515 -  by (simp add: not_le minus_of_num_less_one_iff)
   2.516 -
   2.517 -lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
   2.518 -  by (simp only: not_le minus_one_less_of_num_iff)
   2.519 -
   2.520 -lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
   2.521 -  by (simp add: not_le minus_one_less_one_iff)
   2.522 -
   2.523 -lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
   2.524 -  by (simp add: not_less minus_of_num_le_of_num_iff)
   2.525 -
   2.526 -lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
   2.527 -  by (simp add: not_less minus_of_num_le_one_iff)
   2.528 -
   2.529 -lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
   2.530 -  by (simp only: not_less minus_one_le_of_num_iff)
   2.531 -
   2.532 -lemma one_less_minus_one_iff: "\<not> 1 < - 1"
   2.533 -  by (simp only: not_less minus_one_le_one_iff)
   2.534 -
   2.535 -lemmas le_signed_numeral_special [numeral] =
   2.536 -  minus_of_num_le_of_num_iff
   2.537 -  minus_of_num_le_one_iff
   2.538 -  minus_one_le_of_num_iff
   2.539 -  minus_one_le_one_iff
   2.540 -  of_num_le_minus_of_num_iff
   2.541 -  one_le_minus_of_num_iff
   2.542 -  of_num_le_minus_one_iff
   2.543 -  one_le_minus_one_iff
   2.544 -
   2.545 -lemmas less_signed_numeral_special [numeral] =
   2.546 -  minus_of_num_less_of_num_iff
   2.547 -  minus_of_num_not_equal_of_num
   2.548 -  minus_of_num_less_one_iff
   2.549 -  minus_one_less_of_num_iff
   2.550 -  minus_one_less_one_iff
   2.551 -  of_num_less_minus_of_num_iff
   2.552 -  one_less_minus_of_num_iff
   2.553 -  of_num_less_minus_one_iff
   2.554 -  one_less_minus_one_iff
   2.555 -
   2.556 -end
   2.557 -
   2.558 -subsubsection {* Structures with subtraction: class @{text semiring_1_minus} *}
   2.559 -
   2.560 -class semiring_minus = semiring + minus + zero +
   2.561 -  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
   2.562 -  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
   2.563 -begin
   2.564 -
   2.565 -lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
   2.566 -  by (simp add: add_ac minus_inverts_plus1 [of b a])
   2.567 -
   2.568 -lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
   2.569 -  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
   2.570 -
   2.571 -end
   2.572 -
   2.573 -class semiring_1_minus = semiring_1 + semiring_minus
   2.574 -begin
   2.575 -
   2.576 -lemma Dig_of_num_pos:
   2.577 -  assumes "k + n = m"
   2.578 -  shows "of_num m - of_num n = of_num k"
   2.579 -  using assms by (simp add: of_num_plus minus_inverts_plus1)
   2.580 -
   2.581 -lemma Dig_of_num_zero:
   2.582 -  shows "of_num n - of_num n = 0"
   2.583 -  by (rule minus_inverts_plus1) simp
   2.584 -
   2.585 -lemma Dig_of_num_neg:
   2.586 -  assumes "k + m = n"
   2.587 -  shows "of_num m - of_num n = 0 - of_num k"
   2.588 -  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
   2.589 -
   2.590 -lemmas Dig_plus_eval =
   2.591 -  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
   2.592 -
   2.593 -simproc_setup numeral_minus ("of_num m - of_num n") = {*
   2.594 -  let
   2.595 -    (*TODO proper implicit use of morphism via pattern antiquotations*)
   2.596 -    fun cdest_of_num ct = (List.last o snd o Drule.strip_comb) ct;
   2.597 -    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
   2.598 -    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
   2.599 -    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
   2.600 -    val simplify = Raw_Simplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
   2.601 -    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq}
   2.602 -      OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
   2.603 -        [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
   2.604 -  in fn phi => fn _ => fn ct => case try cdifference ct
   2.605 -   of NONE => (NONE)
   2.606 -    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
   2.607 -        then Raw_Simplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
   2.608 -        else mk_meta_eq (let
   2.609 -          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
   2.610 -        in
   2.611 -          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
   2.612 -          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
   2.613 -        end) end)
   2.614 -  end
   2.615 -*}
   2.616 -
   2.617 -lemma Dig_of_num_minus_zero [numeral]:
   2.618 -  "of_num n - 0 = of_num n"
   2.619 -  by (simp add: minus_inverts_plus1)
   2.620 -
   2.621 -lemma Dig_one_minus_zero [numeral]:
   2.622 -  "1 - 0 = 1"
   2.623 -  by (simp add: minus_inverts_plus1)
   2.624 -
   2.625 -lemma Dig_one_minus_one [numeral]:
   2.626 -  "1 - 1 = 0"
   2.627 -  by (simp add: minus_inverts_plus1)
   2.628 -
   2.629 -lemma Dig_of_num_minus_one [numeral]:
   2.630 -  "of_num (Dig0 n) - 1 = of_num (DigM n)"
   2.631 -  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
   2.632 -  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
   2.633 -
   2.634 -lemma Dig_one_minus_of_num [numeral]:
   2.635 -  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
   2.636 -  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
   2.637 -  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
   2.638 -
   2.639 -end
   2.640 -
   2.641 -
   2.642 -subsubsection {* Structures with negation: class @{text ring_1} *}
   2.643 -
   2.644 -context ring_1
   2.645 -begin
   2.646 -
   2.647 -subclass semiring_1_minus proof
   2.648 -qed (simp_all add: algebra_simps)
   2.649 -
   2.650 -lemma Dig_zero_minus_of_num [numeral]:
   2.651 -  "0 - of_num n = - of_num n"
   2.652 -  by simp
   2.653 -
   2.654 -lemma Dig_zero_minus_one [numeral]:
   2.655 -  "0 - 1 = - 1"
   2.656 -  by simp
   2.657 -
   2.658 -lemma Dig_uminus_uminus [numeral]:
   2.659 -  "- (- of_num n) = of_num n"
   2.660 -  by simp
   2.661 -
   2.662 -lemma Dig_plus_uminus [numeral]:
   2.663 -  "of_num m + - of_num n = of_num m - of_num n"
   2.664 -  "- of_num m + of_num n = of_num n - of_num m"
   2.665 -  "- of_num m + - of_num n = - (of_num m + of_num n)"
   2.666 -  "of_num m - - of_num n = of_num m + of_num n"
   2.667 -  "- of_num m - of_num n = - (of_num m + of_num n)"
   2.668 -  "- of_num m - - of_num n = of_num n - of_num m"
   2.669 -  by (simp_all add: diff_minus add_commute)
   2.670 -
   2.671 -lemma Dig_times_uminus [numeral]:
   2.672 -  "- of_num n * of_num m = - (of_num n * of_num m)"
   2.673 -  "of_num n * - of_num m = - (of_num n * of_num m)"
   2.674 -  "- of_num n * - of_num m = of_num n * of_num m"
   2.675 -  by simp_all
   2.676 -
   2.677 -lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
   2.678 -by (induct n)
   2.679 -  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
   2.680 -
   2.681 -declare of_int_1 [numeral]
   2.682 -
   2.683 -end
   2.684 -
   2.685 -
   2.686 -subsubsection {* Structures with exponentiation *}
   2.687 -
   2.688 -lemma of_num_square: "of_num (square x) = of_num x * of_num x"
   2.689 -by (induct x)
   2.690 -   (simp_all add: of_num.simps of_num_add algebra_simps)
   2.691 -
   2.692 -lemma of_num_pow: "of_num (pow x y) = of_num x ^ of_num y"
   2.693 -by (induct y)
   2.694 -   (simp_all add: of_num.simps of_num_square of_num_mult power_add)
   2.695 -
   2.696 -lemma power_of_num [numeral]: "of_num x ^ of_num y = of_num (pow x y)"
   2.697 -  unfolding of_num_pow ..
   2.698 -
   2.699 -lemma power_zero_of_num [numeral]:
   2.700 -  "0 ^ of_num n = (0::'a::semiring_1)"
   2.701 -  using of_num_pos [where n=n and ?'a=nat]
   2.702 -  by (simp add: power_0_left)
   2.703 -
   2.704 -lemma power_minus_Dig0 [numeral]:
   2.705 -  fixes x :: "'a::ring_1"
   2.706 -  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
   2.707 -  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
   2.708 -
   2.709 -lemma power_minus_Dig1 [numeral]:
   2.710 -  fixes x :: "'a::ring_1"
   2.711 -  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
   2.712 -  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
   2.713 -
   2.714 -declare power_one [numeral]
   2.715 -
   2.716 -
   2.717 -subsubsection {* Greetings to @{typ nat}. *}
   2.718 -
   2.719 -instance nat :: semiring_1_minus proof
   2.720 -qed simp_all
   2.721 -
   2.722 -lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
   2.723 -  unfolding of_num_plus_one [symmetric] by simp
   2.724 -
   2.725 -lemma nat_number:
   2.726 -  "1 = Suc 0"
   2.727 -  "of_num One = Suc 0"
   2.728 -  "of_num (Dig0 n) = Suc (of_num (DigM n))"
   2.729 -  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
   2.730 -  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
   2.731 -
   2.732 -declare diff_0_eq_0 [numeral]
   2.733 -
   2.734 -
   2.735 -subsection {* Proof tools setup *}
   2.736 -
   2.737 -subsubsection {* Numeral equations as default simplification rules *}
   2.738 -
   2.739 -declare (in semiring_numeral) of_num_One [simp]
   2.740 -declare (in semiring_numeral) of_num_plus_one [simp]
   2.741 -declare (in semiring_numeral) of_num_one_plus [simp]
   2.742 -declare (in semiring_numeral) of_num_plus [simp]
   2.743 -declare (in semiring_numeral) of_num_times [simp]
   2.744 -
   2.745 -declare (in semiring_1) of_nat_of_num [simp]
   2.746 -
   2.747 -declare (in semiring_char_0) of_num_eq_iff [simp]
   2.748 -declare (in semiring_char_0) of_num_eq_one_iff [simp]
   2.749 -declare (in semiring_char_0) one_eq_of_num_iff [simp]
   2.750 -
   2.751 -declare (in linordered_semidom) of_num_pos [simp]
   2.752 -declare (in linordered_semidom) of_num_not_zero [simp]
   2.753 -declare (in linordered_semidom) of_num_less_eq_iff [simp]
   2.754 -declare (in linordered_semidom) of_num_less_eq_one_iff [simp]
   2.755 -declare (in linordered_semidom) one_less_eq_of_num_iff [simp]
   2.756 -declare (in linordered_semidom) of_num_less_iff [simp]
   2.757 -declare (in linordered_semidom) of_num_less_one_iff [simp]
   2.758 -declare (in linordered_semidom) one_less_of_num_iff [simp]
   2.759 -declare (in linordered_semidom) of_num_nonneg [simp]
   2.760 -declare (in linordered_semidom) of_num_less_zero_iff [simp]
   2.761 -declare (in linordered_semidom) of_num_le_zero_iff [simp]
   2.762 -
   2.763 -declare (in linordered_idom) le_signed_numeral_special [simp]
   2.764 -declare (in linordered_idom) less_signed_numeral_special [simp]
   2.765 -
   2.766 -declare (in semiring_1_minus) Dig_of_num_minus_one [simp]
   2.767 -declare (in semiring_1_minus) Dig_one_minus_of_num [simp]
   2.768 -
   2.769 -declare (in ring_1) Dig_plus_uminus [simp]
   2.770 -declare (in ring_1) of_int_of_num [simp]
   2.771 -
   2.772 -declare power_of_num [simp]
   2.773 -declare power_zero_of_num [simp]
   2.774 -declare power_minus_Dig0 [simp]
   2.775 -declare power_minus_Dig1 [simp]
   2.776 -
   2.777 -declare Suc_of_num [simp]
   2.778 -
   2.779 -
   2.780 -subsubsection {* Reorientation of equalities *}
   2.781 -
   2.782 -setup {*
   2.783 -  Reorient_Proc.add
   2.784 -    (fn Const(@{const_name of_num}, _) $ _ => true
   2.785 -      | Const(@{const_name uminus}, _) $
   2.786 -          (Const(@{const_name of_num}, _) $ _) => true
   2.787 -      | _ => false)
   2.788 -*}
   2.789 -
   2.790 -simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = Reorient_Proc.proc
   2.791 -
   2.792 -
   2.793 -subsubsection {* Constant folding for multiplication in semirings *}
   2.794 -
   2.795 -context semiring_numeral
   2.796 -begin
   2.797 -
   2.798 -lemma mult_of_num_commute: "x * of_num n = of_num n * x"
   2.799 -by (induct n)
   2.800 -  (simp_all only: of_num.simps distrib_right distrib_left mult_1_left mult_1_right)
   2.801 -
   2.802 -definition
   2.803 -  "commutes_with a b \<longleftrightarrow> a * b = b * a"
   2.804 -
   2.805 -lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a"
   2.806 -unfolding commutes_with_def .
   2.807 -
   2.808 -lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)"
   2.809 -unfolding commutes_with_def by (simp only: mult_assoc [symmetric])
   2.810 -
   2.811 -lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x"
   2.812 -unfolding commutes_with_def by (simp_all add: mult_of_num_commute)
   2.813 -
   2.814 -lemmas mult_ac_numeral =
   2.815 -  mult_assoc
   2.816 -  commutes_with_commute
   2.817 -  commutes_with_left_commute
   2.818 -  commutes_with_numeral
   2.819 -
   2.820 -end
   2.821 -
   2.822 -ML {*
   2.823 -structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   2.824 -struct
   2.825 -  val assoc_ss = simpset_of (put_simpset HOL_ss @{context} addsimps @{thms mult_ac_numeral})
   2.826 -  val eq_reflection = eq_reflection
   2.827 -  fun is_numeral (Const(@{const_name of_num}, _) $ _) = true
   2.828 -    | is_numeral _ = false;
   2.829 -end;
   2.830 -
   2.831 -structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   2.832 -*}
   2.833 -
   2.834 -simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") =
   2.835 -  {* fn phi => fn ss => fn ct =>
   2.836 -    Semiring_Times_Assoc.proc ss (Thm.term_of ct) *}
   2.837 -
   2.838 -
   2.839 -subsection {* Code generator setup for @{typ int} *}
   2.840 -
   2.841 -text {* Reversing standard setup *}
   2.842 -
   2.843 -lemma [code_unfold del]: "(1::int) \<equiv> Numeral1" by simp
   2.844 -  
   2.845 -lemma [code, code del]:
   2.846 -  "(1 :: int) = 1"
   2.847 -  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
   2.848 -  "(uminus :: int \<Rightarrow> int) = uminus"
   2.849 -  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
   2.850 -  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
   2.851 -  "(HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool) = HOL.equal"
   2.852 -  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
   2.853 -  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
   2.854 -  by rule+
   2.855 -
   2.856 -text {* Constructors *}
   2.857 -
   2.858 -definition Pls :: "num \<Rightarrow> int" where
   2.859 -  [simp, code_post]: "Pls n = of_num n"
   2.860 -
   2.861 -definition Mns :: "num \<Rightarrow> int" where
   2.862 -  [simp, code_post]: "Mns n = - of_num n"
   2.863 -
   2.864 -code_datatype "0::int" Pls Mns
   2.865 -
   2.866 -lemmas [code_unfold] = Pls_def [symmetric] Mns_def [symmetric]
   2.867 -
   2.868 -text {* Auxiliary operations *}
   2.869 -
   2.870 -definition dup :: "int \<Rightarrow> int" where
   2.871 -  [simp]: "dup k = k + k"
   2.872 -
   2.873 -lemma Dig_dup [code]:
   2.874 -  "dup 0 = 0"
   2.875 -  "dup (Pls n) = Pls (Dig0 n)"
   2.876 -  "dup (Mns n) = Mns (Dig0 n)"
   2.877 -  by (simp_all add: of_num.simps)
   2.878 -
   2.879 -definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
   2.880 -  [simp]: "sub m n = (of_num m - of_num n)"
   2.881 -
   2.882 -lemma Dig_sub [code]:
   2.883 -  "sub One One = 0"
   2.884 -  "sub (Dig0 m) One = of_num (DigM m)"
   2.885 -  "sub (Dig1 m) One = of_num (Dig0 m)"
   2.886 -  "sub One (Dig0 n) = - of_num (DigM n)"
   2.887 -  "sub One (Dig1 n) = - of_num (Dig0 n)"
   2.888 -  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
   2.889 -  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
   2.890 -  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
   2.891 -  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
   2.892 -  by (simp_all add: algebra_simps num_eq_iff nat_of_num_add)
   2.893 -
   2.894 -text {* Implementations *}
   2.895 -
   2.896 -lemma one_int_code [code]:
   2.897 -  "1 = Pls One"
   2.898 -  by simp
   2.899 -
   2.900 -lemma plus_int_code [code]:
   2.901 -  "k + 0 = (k::int)"
   2.902 -  "0 + l = (l::int)"
   2.903 -  "Pls m + Pls n = Pls (m + n)"
   2.904 -  "Pls m + Mns n = sub m n"
   2.905 -  "Mns m + Pls n = sub n m"
   2.906 -  "Mns m + Mns n = Mns (m + n)"
   2.907 -  by simp_all
   2.908 -
   2.909 -lemma uminus_int_code [code]:
   2.910 -  "uminus 0 = (0::int)"
   2.911 -  "uminus (Pls m) = Mns m"
   2.912 -  "uminus (Mns m) = Pls m"
   2.913 -  by simp_all
   2.914 -
   2.915 -lemma minus_int_code [code]:
   2.916 -  "k - 0 = (k::int)"
   2.917 -  "0 - l = uminus (l::int)"
   2.918 -  "Pls m - Pls n = sub m n"
   2.919 -  "Pls m - Mns n = Pls (m + n)"
   2.920 -  "Mns m - Pls n = Mns (m + n)"
   2.921 -  "Mns m - Mns n = sub n m"
   2.922 -  by simp_all
   2.923 -
   2.924 -lemma times_int_code [code]:
   2.925 -  "k * 0 = (0::int)"
   2.926 -  "0 * l = (0::int)"
   2.927 -  "Pls m * Pls n = Pls (m * n)"
   2.928 -  "Pls m * Mns n = Mns (m * n)"
   2.929 -  "Mns m * Pls n = Mns (m * n)"
   2.930 -  "Mns m * Mns n = Pls (m * n)"
   2.931 -  by simp_all
   2.932 -
   2.933 -lemma eq_int_code [code]:
   2.934 -  "HOL.equal 0 (0::int) \<longleftrightarrow> True"
   2.935 -  "HOL.equal 0 (Pls l) \<longleftrightarrow> False"
   2.936 -  "HOL.equal 0 (Mns l) \<longleftrightarrow> False"
   2.937 -  "HOL.equal (Pls k) 0 \<longleftrightarrow> False"
   2.938 -  "HOL.equal (Pls k) (Pls l) \<longleftrightarrow> HOL.equal k l"
   2.939 -  "HOL.equal (Pls k) (Mns l) \<longleftrightarrow> False"
   2.940 -  "HOL.equal (Mns k) 0 \<longleftrightarrow> False"
   2.941 -  "HOL.equal (Mns k) (Pls l) \<longleftrightarrow> False"
   2.942 -  "HOL.equal (Mns k) (Mns l) \<longleftrightarrow> HOL.equal k l"
   2.943 -  by (auto simp add: equal dest: sym)
   2.944 -
   2.945 -lemma [code nbe]:
   2.946 -  "HOL.equal (k::int) k \<longleftrightarrow> True"
   2.947 -  by (fact equal_refl)
   2.948 -
   2.949 -lemma less_eq_int_code [code]:
   2.950 -  "0 \<le> (0::int) \<longleftrightarrow> True"
   2.951 -  "0 \<le> Pls l \<longleftrightarrow> True"
   2.952 -  "0 \<le> Mns l \<longleftrightarrow> False"
   2.953 -  "Pls k \<le> 0 \<longleftrightarrow> False"
   2.954 -  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
   2.955 -  "Pls k \<le> Mns l \<longleftrightarrow> False"
   2.956 -  "Mns k \<le> 0 \<longleftrightarrow> True"
   2.957 -  "Mns k \<le> Pls l \<longleftrightarrow> True"
   2.958 -  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
   2.959 -  by simp_all
   2.960 -
   2.961 -lemma less_int_code [code]:
   2.962 -  "0 < (0::int) \<longleftrightarrow> False"
   2.963 -  "0 < Pls l \<longleftrightarrow> True"
   2.964 -  "0 < Mns l \<longleftrightarrow> False"
   2.965 -  "Pls k < 0 \<longleftrightarrow> False"
   2.966 -  "Pls k < Pls l \<longleftrightarrow> k < l"
   2.967 -  "Pls k < Mns l \<longleftrightarrow> False"
   2.968 -  "Mns k < 0 \<longleftrightarrow> True"
   2.969 -  "Mns k < Pls l \<longleftrightarrow> True"
   2.970 -  "Mns k < Mns l \<longleftrightarrow> l < k"
   2.971 -  by simp_all
   2.972 -
   2.973 -hide_const (open) sub dup
   2.974 -
   2.975 -end
   2.976 -