src/HOL/ex/Numeral.thy
author huffman
Fri, 27 Mar 2009 15:14:31 -0700
changeset 30792 809c38c1a26c
parent 29991 c60ace776315
child 31021 53642251a04f
permissions -rw-r--r--
add more lemmas for signed comparisons
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/ex/Numeral.thy
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
     3
*)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     4
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
     5
header {* An experimental alternative numeral representation. *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     6
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     7
theory Numeral
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     8
imports Int Inductive
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
     9
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    10
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    11
subsection {* The @{text num} type *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    12
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    13
datatype num = One | Dig0 num | Dig1 num
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    14
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    15
text {* Increment function for type @{typ num} *}
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    16
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    17
primrec
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    18
  inc :: "num \<Rightarrow> num"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    19
where
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    20
  "inc One = Dig0 One"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    21
| "inc (Dig0 x) = Dig1 x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    22
| "inc (Dig1 x) = Dig0 (inc x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    23
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    24
text {* Converting between type @{typ num} and type @{typ nat} *}
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    25
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    26
primrec
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    27
  nat_of_num :: "num \<Rightarrow> nat"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    28
where
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    29
  "nat_of_num One = Suc 0"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    30
| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    31
| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    32
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    33
primrec
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    34
  num_of_nat :: "nat \<Rightarrow> num"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    35
where
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    36
  "num_of_nat 0 = One"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    37
| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    38
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
    39
lemma nat_of_num_pos: "0 < nat_of_num x"
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    40
  by (induct x) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    41
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    42
lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    43
  by (induct x) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    44
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    45
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    46
  by (induct x) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    47
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    48
lemma num_of_nat_double:
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    49
  "0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    50
  by (induct n) simp_all
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    51
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    52
text {*
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    53
  Type @{typ num} is isomorphic to the strictly positive
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    54
  natural numbers.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    55
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    56
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    57
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
    58
  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    59
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    60
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    61
  by (induct n) (simp_all add: nat_of_num_inc)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    62
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
    63
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
    64
  apply safe
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    65
  apply (drule arg_cong [where f=num_of_nat])
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
    66
  apply (simp add: nat_of_num_inverse)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    67
  done
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    68
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    69
lemma num_induct [case_names One inc]:
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    70
  fixes P :: "num \<Rightarrow> bool"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    71
  assumes One: "P One"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    72
    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    73
  shows "P x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    74
proof -
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    75
  obtain n where n: "Suc n = nat_of_num x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    76
    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    77
  have "P (num_of_nat (Suc n))"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    78
  proof (induct n)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    79
    case 0 show ?case using One by simp
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    80
  next
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    81
    case (Suc n)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    82
    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    83
    then show "P (num_of_nat (Suc (Suc n)))" by simp
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    84
  qed
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    85
  with n show "P x"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    86
    by (simp add: nat_of_num_inverse)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    87
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    88
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    89
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    90
  From now on, there are two possible models for @{typ num}:
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
    91
  as positive naturals (rule @{text "num_induct"})
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    92
  and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}).
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    93
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    94
  It is not entirely clear in which context it is better to use
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    95
  the one or the other, or whether the construction should be reversed.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    96
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    97
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
    98
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
    99
subsection {* Numeral operations *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   100
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   101
ML {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   102
structure DigSimps =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   103
  NamedThmsFun(val name = "numeral"; val description = "Simplification rules for numerals")
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   104
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   105
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   106
setup DigSimps.setup
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   107
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   108
instantiation num :: "{plus,times,ord}"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   109
begin
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   110
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   111
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   112
  [code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   113
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   114
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   115
  [code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   116
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   117
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   118
  [code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   119
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   120
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   121
  [code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   122
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   123
instance ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   124
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   125
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   126
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   127
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   128
  unfolding plus_num_def
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   129
  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   130
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   131
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   132
  unfolding times_num_def
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   133
  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   134
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   135
lemma Dig_plus [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   136
  "One + One = Dig0 One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   137
  "One + Dig0 m = Dig1 m"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   138
  "One + Dig1 m = Dig0 (m + One)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   139
  "Dig0 n + One = Dig1 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   140
  "Dig0 n + Dig0 m = Dig0 (n + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   141
  "Dig0 n + Dig1 m = Dig1 (n + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   142
  "Dig1 n + One = Dig0 (n + One)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   143
  "Dig1 n + Dig0 m = Dig1 (n + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   144
  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   145
  by (simp_all add: num_eq_iff nat_of_num_add)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   146
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   147
lemma Dig_times [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   148
  "One * One = One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   149
  "One * Dig0 n = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   150
  "One * Dig1 n = Dig1 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   151
  "Dig0 n * One = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   152
  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   153
  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   154
  "Dig1 n * One = Dig1 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   155
  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   156
  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   157
  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   158
                    left_distrib right_distrib)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   159
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   160
lemma Dig_eq:
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   161
  "One = One \<longleftrightarrow> True"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   162
  "One = Dig0 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   163
  "One = Dig1 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   164
  "Dig0 m = One \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   165
  "Dig1 m = One \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   166
  "Dig0 m = Dig0 n \<longleftrightarrow> m = n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   167
  "Dig0 m = Dig1 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   168
  "Dig1 m = Dig0 n \<longleftrightarrow> False"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   169
  "Dig1 m = Dig1 n \<longleftrightarrow> m = n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   170
  by simp_all
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   171
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   172
lemma less_eq_num_code [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   173
  "One \<le> n \<longleftrightarrow> True"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   174
  "Dig0 m \<le> One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   175
  "Dig1 m \<le> One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   176
  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   177
  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   178
  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   179
  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   180
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   181
  by (auto simp add: less_eq_num_def less_num_def)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   182
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   183
lemma less_num_code [numeral, simp, code]:
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   184
  "m < One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   185
  "One < One \<longleftrightarrow> False"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   186
  "One < Dig0 n \<longleftrightarrow> True"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   187
  "One < Dig1 n \<longleftrightarrow> True"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   188
  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   189
  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   190
  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   191
  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   192
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   193
  by (auto simp add: less_eq_num_def less_num_def)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   194
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   195
text {* Rules using @{text One} and @{text inc} as constructors *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   196
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   197
lemma add_One: "x + One = inc x"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   198
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   199
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   200
lemma add_inc: "x + inc y = inc (x + y)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   201
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   202
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   203
lemma mult_One: "x * One = x"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   204
  by (simp add: num_eq_iff nat_of_num_mult)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   205
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   206
lemma mult_inc: "x * inc y = x * y + x"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   207
  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   208
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   209
text {* A double-and-decrement function *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   210
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   211
primrec DigM :: "num \<Rightarrow> num" where
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   212
  "DigM One = One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   213
  | "DigM (Dig0 n) = Dig1 (DigM n)"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   214
  | "DigM (Dig1 n) = Dig1 (Dig0 n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   215
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   216
lemma DigM_plus_one: "DigM n + One = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   217
  by (induct n) simp_all
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   218
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   219
lemma add_One_commute: "One + n = n + One"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   220
  by (induct n) simp_all
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   221
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   222
lemma one_plus_DigM: "One + DigM n = Dig0 n"
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   223
  unfolding add_One_commute DigM_plus_one ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   224
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   225
text {* Squaring and exponentiation *}
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   226
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   227
primrec square :: "num \<Rightarrow> num" where
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   228
  "square One = One"
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   229
| "square (Dig0 n) = Dig0 (Dig0 (square n))"
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   230
| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   231
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   232
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   233
where
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   234
  "pow x One = x"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   235
| "pow x (Dig0 y) = square (pow x y)"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   236
| "pow x (Dig1 y) = x * square (pow x y)"
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   237
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   238
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   239
subsection {* Binary numerals *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   240
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   241
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   242
  We embed binary representations into a generic algebraic
29934
5d81dd706206 tuned texts
haftmann
parents: 29667
diff changeset
   243
  structure using @{text of_num}.
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   244
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   245
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   246
class semiring_numeral = semiring + monoid_mult
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   247
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   248
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   249
primrec of_num :: "num \<Rightarrow> 'a" where
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   250
  of_num_one [numeral]: "of_num One = 1"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   251
  | "of_num (Dig0 n) = of_num n + of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   252
  | "of_num (Dig1 n) = of_num n + of_num n + 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   253
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   254
lemma of_num_inc: "of_num (inc x) = of_num x + 1"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   255
  by (induct x) (simp_all add: add_ac)
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   256
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   257
declare of_num.simps [simp del]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   258
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   259
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   260
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   261
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   262
  ML stuff and syntax.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   263
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   264
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   265
ML {*
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   266
fun mk_num 1 = @{term One}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   267
  | mk_num k =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   268
      let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   269
        val (l, b) = Integer.div_mod k 2;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   270
        val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   271
      in bit $ (mk_num l) end;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   272
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   273
fun dest_num @{term One} = 1
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   274
  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   275
  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   276
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   277
(*FIXME these have to gain proper context via morphisms phi*)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   278
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   279
fun mk_numeral T k = Const (@{const_name of_num}, @{typ num} --> T)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   280
  $ mk_num k
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   281
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   282
fun dest_numeral (Const (@{const_name of_num}, Type ("fun", [@{typ num}, T])) $ t) =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   283
  (T, dest_num t)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   284
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   285
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   286
syntax
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   287
  "_Numerals" :: "xnum \<Rightarrow> 'a"    ("_")
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   288
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   289
parse_translation {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   290
let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   291
  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   292
     of (0, 1) => Const (@{const_name One}, dummyT)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   293
      | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   294
      | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   295
    else raise Match;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   296
  fun numeral_tr [Free (num, _)] =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   297
        let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   298
          val {leading_zeros, value, ...} = Syntax.read_xnum num;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   299
          val _ = leading_zeros = 0 andalso value > 0
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   300
            orelse error ("Bad numeral: " ^ num);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   301
        in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   302
    | numeral_tr ts = raise TERM ("numeral_tr", ts);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   303
in [("_Numerals", numeral_tr)] end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   304
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   305
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   306
typed_print_translation {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   307
let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   308
  fun dig b n = b + 2 * n; 
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   309
  fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   310
        dig 0 (int_of_num' n)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   311
    | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   312
        dig 1 (int_of_num' n)
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   313
    | int_of_num' (Const (@{const_syntax One}, _)) = 1;
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   314
  fun num_tr' show_sorts T [n] =
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   315
    let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   316
      val k = int_of_num' n;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   317
      val t' = Syntax.const "_Numerals" $ Syntax.free ("#" ^ string_of_int k);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   318
    in case T
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   319
     of Type ("fun", [_, T']) =>
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   320
         if not (! show_types) andalso can Term.dest_Type T' then t'
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   321
         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T'
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   322
      | T' => if T' = dummyT then t' else raise Match
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   323
    end;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   324
in [(@{const_syntax of_num}, num_tr')] end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   325
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   326
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   327
subsection {* Class-specific numeral rules *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   328
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   329
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   330
  @{const of_num} is a morphism.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   331
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   332
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   333
subsubsection {* Class @{text semiring_numeral} *}
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   334
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   335
context semiring_numeral
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   336
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   337
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   338
abbreviation "Num1 \<equiv> of_num One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   339
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   340
text {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   341
  Alas, there is still the duplication of @{term 1},
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   342
  thought the duplicated @{term 0} has disappeared.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   343
  We could get rid of it by replacing the constructor
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   344
  @{term 1} in @{typ num} by two constructors
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   345
  @{text two} and @{text three}, resulting in a further
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   346
  blow-up.  But it could be worth the effort.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   347
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   348
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   349
lemma of_num_plus_one [numeral]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   350
  "of_num n + 1 = of_num (n + One)"
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   351
  by (rule sym, induct n) (simp_all add: of_num.simps add_ac)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   352
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   353
lemma of_num_one_plus [numeral]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   354
  "1 + of_num n = of_num (n + One)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   355
  unfolding of_num_plus_one [symmetric] add_commute ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   356
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   357
lemma of_num_plus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   358
  "of_num m + of_num n = of_num (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   359
  by (induct n rule: num_induct)
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   360
     (simp_all add: add_One add_inc of_num_one of_num_inc add_ac)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   361
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   362
lemma of_num_times_one [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   363
  "of_num n * 1 = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   364
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   365
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   366
lemma of_num_one_times [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   367
  "1 * of_num n = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   368
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   369
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   370
lemma of_num_times [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   371
  "of_num m * of_num n = of_num (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   372
  by (induct n rule: num_induct)
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   373
    (simp_all add: of_num_plus [symmetric] mult_One mult_inc
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   374
    semiring_class.right_distrib right_distrib of_num_one of_num_inc)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   375
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   376
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   377
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   378
subsubsection {*
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   379
  Structures with a zero: class @{text semiring_1}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   380
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   381
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   382
context semiring_1
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   383
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   384
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   385
subclass semiring_numeral ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   386
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   387
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   388
  by (induct n)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   389
    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   390
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   391
declare of_nat_1 [numeral]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   392
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   393
lemma Dig_plus_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   394
  "0 + 1 = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   395
  "0 + of_num n = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   396
  "1 + 0 = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   397
  "of_num n + 0 = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   398
  by simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   399
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   400
lemma Dig_times_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   401
  "0 * 1 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   402
  "0 * of_num n = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   403
  "1 * 0 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   404
  "of_num n * 0 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   405
  by simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   406
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   407
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   408
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   409
lemma nat_of_num_of_num: "nat_of_num = of_num"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   410
proof
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   411
  fix n
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   412
  have "of_num n = nat_of_num n"
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   413
    by (induct n) (simp_all add: of_num.simps)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   414
  then show "nat_of_num n = of_num n" by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   415
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   416
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   417
subsubsection {*
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   418
  Equality: class @{text semiring_char_0}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   419
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   420
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   421
context semiring_char_0
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   422
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   423
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   424
lemma of_num_eq_iff [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   425
  "of_num m = of_num n \<longleftrightarrow> m = n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   426
  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
29943
922b931fd2eb datatype num = One | Dig0 num | Dig1 num
huffman
parents: 29942
diff changeset
   427
    of_nat_eq_iff num_eq_iff ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   428
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   429
lemma of_num_eq_one_iff [numeral]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   430
  "of_num n = 1 \<longleftrightarrow> n = One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   431
proof -
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   432
  have "of_num n = of_num One \<longleftrightarrow> n = One" unfolding of_num_eq_iff ..
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   433
  then show ?thesis by (simp add: of_num_one)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   434
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   435
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   436
lemma one_eq_of_num_iff [numeral]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   437
  "1 = of_num n \<longleftrightarrow> n = One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   438
  unfolding of_num_eq_one_iff [symmetric] by auto
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   439
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   440
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   441
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   442
subsubsection {*
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   443
  Comparisons: class @{text ordered_semidom}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   444
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   445
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   446
text {*  Could be perhaps more general than here. *}
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   447
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   448
context ordered_semidom
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   449
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   450
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   451
lemma of_num_pos [numeral]: "0 < of_num n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   452
  by (induct n) (simp_all add: of_num.simps add_pos_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   453
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   454
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   455
proof -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   456
  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   457
    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   458
  then show ?thesis by (simp add: of_nat_of_num)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   459
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   460
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   461
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n = One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   462
proof -
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   463
  have "of_num n \<le> of_num One \<longleftrightarrow> n = One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   464
    by (cases n) (simp_all add: of_num_less_eq_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   465
  then show ?thesis by (simp add: of_num_one)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   466
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   467
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   468
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   469
proof -
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   470
  have "of_num One \<le> of_num n"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   471
    by (cases n) (simp_all add: of_num_less_eq_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   472
  then show ?thesis by (simp add: of_num_one)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   473
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   474
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   475
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   476
proof -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   477
  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   478
    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   479
  then show ?thesis by (simp add: of_nat_of_num)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   480
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   481
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   482
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   483
proof -
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   484
  have "\<not> of_num n < of_num One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   485
    by (cases n) (simp_all add: of_num_less_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   486
  then show ?thesis by (simp add: of_num_one)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   487
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   488
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   489
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> n \<noteq> One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   490
proof -
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   491
  have "of_num One < of_num n \<longleftrightarrow> n \<noteq> One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   492
    by (cases n) (simp_all add: of_num_less_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   493
  then show ?thesis by (simp add: of_num_one)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   494
qed
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   495
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   496
lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   497
  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   498
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   499
lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   500
  by (simp add: not_less of_num_nonneg)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   501
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   502
lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   503
  by (simp add: not_le of_num_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   504
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   505
end
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   506
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   507
context ordered_idom
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   508
begin
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   509
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   510
lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   511
proof -
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   512
  have "- of_num m < 0" by (simp add: of_num_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   513
  also have "0 < of_num n" by (simp add: of_num_pos)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   514
  finally show ?thesis .
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   515
qed
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   516
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   517
lemma minus_of_num_less_one_iff: "- of_num n < 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   518
using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_one)
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   519
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   520
lemma minus_one_less_of_num_iff: "- 1 < of_num n"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   521
using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_one)
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   522
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   523
lemma minus_one_less_one_iff: "- 1 < 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   524
using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_one)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   525
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   526
lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   527
  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   528
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   529
lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   530
  by (simp add: less_imp_le minus_of_num_less_one_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   531
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   532
lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   533
  by (simp add: less_imp_le minus_one_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   534
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   535
lemma minus_one_le_one_iff: "- 1 \<le> 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   536
  by (simp add: less_imp_le minus_one_less_one_iff)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   537
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   538
lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   539
  by (simp add: not_le minus_of_num_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   540
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   541
lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   542
  by (simp add: not_le minus_of_num_less_one_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   543
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   544
lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   545
  by (simp add: not_le minus_one_less_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   546
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   547
lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   548
  by (simp add: not_le minus_one_less_one_iff)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   549
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   550
lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   551
  by (simp add: not_less minus_of_num_le_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   552
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   553
lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   554
  by (simp add: not_less minus_of_num_le_one_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   555
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   556
lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
29991
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   557
  by (simp add: not_less minus_one_le_of_num_iff)
c60ace776315 add more ordering lemmas
huffman
parents: 29954
diff changeset
   558
30792
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   559
lemma one_less_minus_one_iff: "\<not> 1 < - 1"
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   560
  by (simp add: not_less minus_one_le_one_iff)
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   561
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   562
lemmas le_signed_numeral_special [numeral] =
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   563
  minus_of_num_le_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   564
  minus_of_num_le_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   565
  minus_one_le_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   566
  minus_one_le_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   567
  of_num_le_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   568
  one_le_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   569
  of_num_le_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   570
  one_le_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   571
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   572
lemmas less_signed_numeral_special [numeral] =
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   573
  minus_of_num_less_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   574
  minus_of_num_less_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   575
  minus_one_less_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   576
  minus_one_less_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   577
  of_num_less_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   578
  one_less_minus_of_num_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   579
  of_num_less_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   580
  one_less_minus_one_iff
809c38c1a26c add more lemmas for signed comparisons
huffman
parents: 29991
diff changeset
   581
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   582
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   583
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   584
subsubsection {*
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   585
  Structures with subtraction: class @{text semiring_1_minus}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   586
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   587
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   588
class semiring_minus = semiring + minus + zero +
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   589
  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   590
  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   591
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   592
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   593
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   594
  by (simp add: add_ac minus_inverts_plus1 [of b a])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   595
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   596
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   597
  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   598
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   599
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   600
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   601
class semiring_1_minus = semiring_1 + semiring_minus
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   602
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   603
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   604
lemma Dig_of_num_pos:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   605
  assumes "k + n = m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   606
  shows "of_num m - of_num n = of_num k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   607
  using assms by (simp add: of_num_plus minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   608
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   609
lemma Dig_of_num_zero:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   610
  shows "of_num n - of_num n = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   611
  by (rule minus_inverts_plus1) simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   612
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   613
lemma Dig_of_num_neg:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   614
  assumes "k + m = n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   615
  shows "of_num m - of_num n = 0 - of_num k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   616
  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   617
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   618
lemmas Dig_plus_eval =
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   619
  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   620
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   621
simproc_setup numeral_minus ("of_num m - of_num n") = {*
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   622
  let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   623
    (*TODO proper implicit use of morphism via pattern antiquotations*)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   624
    fun cdest_of_num ct = (snd o split_last o snd o Drule.strip_comb) ct;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   625
    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   626
    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   627
    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   628
    val simplify = MetaSimplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   629
    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq} OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   630
      [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   631
  in fn phi => fn _ => fn ct => case try cdifference ct
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   632
   of NONE => (NONE)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   633
    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   634
        then MetaSimplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   635
        else mk_meta_eq (let
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   636
          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   637
        in
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   638
          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   639
          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   640
        end) end)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   641
  end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   642
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   643
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   644
lemma Dig_of_num_minus_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   645
  "of_num n - 0 = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   646
  by (simp add: minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   647
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   648
lemma Dig_one_minus_zero [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   649
  "1 - 0 = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   650
  by (simp add: minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   651
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   652
lemma Dig_one_minus_one [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   653
  "1 - 1 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   654
  by (simp add: minus_inverts_plus1)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   655
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   656
lemma Dig_of_num_minus_one [numeral]:
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   657
  "of_num (Dig0 n) - 1 = of_num (DigM n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   658
  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   659
  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   660
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   661
lemma Dig_one_minus_of_num [numeral]:
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   662
  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   663
  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   664
  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   665
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   666
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   667
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   668
subsubsection {*
29947
0a51765d2084 tune section headings; add square function
huffman
parents: 29946
diff changeset
   669
  Structures with negation: class @{text ring_1}
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   670
*}
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   671
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   672
context ring_1
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   673
begin
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   674
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   675
subclass semiring_1_minus
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 28823
diff changeset
   676
  proof qed (simp_all add: algebra_simps)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   677
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   678
lemma Dig_zero_minus_of_num [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   679
  "0 - of_num n = - of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   680
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   681
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   682
lemma Dig_zero_minus_one [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   683
  "0 - 1 = - 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   684
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   685
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   686
lemma Dig_uminus_uminus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   687
  "- (- of_num n) = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   688
  by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   689
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   690
lemma Dig_plus_uminus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   691
  "of_num m + - of_num n = of_num m - of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   692
  "- of_num m + of_num n = of_num n - of_num m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   693
  "- of_num m + - of_num n = - (of_num m + of_num n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   694
  "of_num m - - of_num n = of_num m + of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   695
  "- of_num m - of_num n = - (of_num m + of_num n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   696
  "- of_num m - - of_num n = of_num n - of_num m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   697
  by (simp_all add: diff_minus add_commute)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   698
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   699
lemma Dig_times_uminus [numeral]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   700
  "- of_num n * of_num m = - (of_num n * of_num m)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   701
  "of_num n * - of_num m = - (of_num n * of_num m)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   702
  "- of_num n * - of_num m = of_num n * of_num m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   703
  by (simp_all add: minus_mult_left [symmetric] minus_mult_right [symmetric])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   704
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   705
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   706
by (induct n)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   707
  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   708
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   709
declare of_int_1 [numeral]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   710
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   711
end
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   712
29945
75df553549b1 rearrange subsections
huffman
parents: 29944
diff changeset
   713
subsubsection {*
29954
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   714
  Structures with exponentiation
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   715
*}
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   716
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   717
lemma of_num_square: "of_num (square x) = of_num x * of_num x"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   718
by (induct x)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   719
   (simp_all add: of_num.simps of_num_plus [symmetric] algebra_simps)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   720
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   721
lemma of_num_pow:
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   722
  "(of_num (pow x y)::'a::{semiring_numeral,recpower}) = of_num x ^ of_num y"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   723
by (induct y)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   724
   (simp_all add: of_num.simps of_num_square of_num_times [symmetric]
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   725
                  power_Suc power_add)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   726
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   727
lemma power_of_num [numeral]:
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   728
  "of_num x ^ of_num y = (of_num (pow x y)::'a::{semiring_numeral,recpower})"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   729
  by (rule of_num_pow [symmetric])
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   730
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   731
lemma power_zero_of_num [numeral]:
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   732
  "0 ^ of_num n = (0::'a::{semiring_0,recpower})"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   733
  using of_num_pos [where n=n and ?'a=nat]
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   734
  by (simp add: power_0_left)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   735
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   736
lemma power_minus_one_double:
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   737
  "(- 1) ^ (n + n) = (1::'a::{ring_1,recpower})"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   738
  by (induct n) (simp_all add: power_Suc)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   739
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   740
lemma power_minus_Dig0 [numeral]:
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   741
  fixes x :: "'a::{ring_1,recpower}"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   742
  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   743
  by (subst power_minus)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   744
     (simp add: of_num.simps power_minus_one_double)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   745
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   746
lemma power_minus_Dig1 [numeral]:
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   747
  fixes x :: "'a::{ring_1,recpower}"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   748
  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   749
  by (subst power_minus)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   750
     (simp add: of_num.simps power_Suc power_minus_one_double)
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   751
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   752
declare power_one [numeral]
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   753
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   754
8a95050c7044 add lemmas for exponentiation
huffman
parents: 29947
diff changeset
   755
subsubsection {*
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   756
  Greetings to @{typ nat}.
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   757
*}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   758
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   759
instance nat :: semiring_1_minus proof qed simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   760
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   761
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   762
  unfolding of_num_plus_one [symmetric] by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   763
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   764
lemma nat_number:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   765
  "1 = Suc 0"
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   766
  "of_num One = Suc 0"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   767
  "of_num (Dig0 n) = Suc (of_num (DigM n))"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   768
  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   769
  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   770
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   771
declare diff_0_eq_0 [numeral]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   772
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   773
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   774
subsection {* Code generator setup for @{typ int} *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   775
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   776
definition Pls :: "num \<Rightarrow> int" where
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   777
  [simp, code post]: "Pls n = of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   778
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   779
definition Mns :: "num \<Rightarrow> int" where
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   780
  [simp, code post]: "Mns n = - of_num n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   781
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   782
code_datatype "0::int" Pls Mns
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   783
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   784
lemmas [code inline] = Pls_def [symmetric] Mns_def [symmetric]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   785
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   786
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   787
  [simp, code del]: "sub m n = (of_num m - of_num n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   788
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   789
definition dup :: "int \<Rightarrow> int" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   790
  [code del]: "dup k = 2 * k"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   791
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   792
lemma Dig_sub [code]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   793
  "sub One One = 0"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   794
  "sub (Dig0 m) One = of_num (DigM m)"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   795
  "sub (Dig1 m) One = of_num (Dig0 m)"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   796
  "sub One (Dig0 n) = - of_num (DigM n)"
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   797
  "sub One (Dig1 n) = - of_num (Dig0 n)"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   798
  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   799
  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   800
  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   801
  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 28823
diff changeset
   802
  apply (simp_all add: dup_def algebra_simps)
29941
b951d80774d5 replace dec with double-and-decrement function
huffman
parents: 29667
diff changeset
   803
  apply (simp_all add: of_num_plus one_plus_DigM)[4]
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   804
  apply (simp_all add: of_num.simps)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   805
  done
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   806
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   807
lemma dup_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   808
  "dup 0 = 0"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   809
  "dup (Pls n) = Pls (Dig0 n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   810
  "dup (Mns n) = Mns (Dig0 n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   811
  by (simp_all add: dup_def of_num.simps)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   812
  
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28367
diff changeset
   813
lemma [code, code del]:
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   814
  "(1 :: int) = 1"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   815
  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   816
  "(uminus :: int \<Rightarrow> int) = uminus"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   817
  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   818
  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
28367
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   819
  "(eq_class.eq :: int \<Rightarrow> int \<Rightarrow> bool) = eq_class.eq"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   820
  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   821
  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   822
  by rule+
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   823
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   824
lemma one_int_code [code]:
29942
31069b8d39df replace 1::num with One; remove monoid_mult instance
huffman
parents: 29941
diff changeset
   825
  "1 = Pls One"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   826
  by (simp add: of_num_one)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   827
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   828
lemma plus_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   829
  "k + 0 = (k::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   830
  "0 + l = (l::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   831
  "Pls m + Pls n = Pls (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   832
  "Pls m - Pls n = sub m n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   833
  "Mns m + Mns n = Mns (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   834
  "Mns m - Mns n = sub n m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   835
  by (simp_all add: of_num_plus [symmetric])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   836
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   837
lemma uminus_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   838
  "uminus 0 = (0::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   839
  "uminus (Pls m) = Mns m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   840
  "uminus (Mns m) = Pls m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   841
  by simp_all
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   842
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   843
lemma minus_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   844
  "k - 0 = (k::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   845
  "0 - l = uminus (l::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   846
  "Pls m - Pls n = sub m n"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   847
  "Pls m - Mns n = Pls (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   848
  "Mns m - Pls n = Mns (m + n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   849
  "Mns m - Mns n = sub n m"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   850
  by (simp_all add: of_num_plus [symmetric])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   851
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   852
lemma times_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   853
  "k * 0 = (0::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   854
  "0 * l = (0::int)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   855
  "Pls m * Pls n = Pls (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   856
  "Pls m * Mns n = Mns (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   857
  "Mns m * Pls n = Mns (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   858
  "Mns m * Mns n = Pls (m * n)"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   859
  by (simp_all add: of_num_times [symmetric])
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   860
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   861
lemma eq_int_code [code]:
28367
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   862
  "eq_class.eq 0 (0::int) \<longleftrightarrow> True"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   863
  "eq_class.eq 0 (Pls l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   864
  "eq_class.eq 0 (Mns l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   865
  "eq_class.eq (Pls k) 0 \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   866
  "eq_class.eq (Pls k) (Pls l) \<longleftrightarrow> eq_class.eq k l"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   867
  "eq_class.eq (Pls k) (Mns l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   868
  "eq_class.eq (Mns k) 0 \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   869
  "eq_class.eq (Mns k) (Pls l) \<longleftrightarrow> False"
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   870
  "eq_class.eq (Mns k) (Mns l) \<longleftrightarrow> eq_class.eq k l"
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   871
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
28367
10ea34297962 op = vs. eq
haftmann
parents: 28053
diff changeset
   872
  by (simp_all add: of_num_eq_iff eq)
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   873
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   874
lemma less_eq_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   875
  "0 \<le> (0::int) \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   876
  "0 \<le> Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   877
  "0 \<le> Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   878
  "Pls k \<le> 0 \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   879
  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   880
  "Pls k \<le> Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   881
  "Mns k \<le> 0 \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   882
  "Mns k \<le> Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   883
  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   884
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   885
  by (simp_all add: of_num_less_eq_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   886
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   887
lemma less_int_code [code]:
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   888
  "0 < (0::int) \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   889
  "0 < Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   890
  "0 < Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   891
  "Pls k < 0 \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   892
  "Pls k < Pls l \<longleftrightarrow> k < l"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   893
  "Pls k < Mns l \<longleftrightarrow> False"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   894
  "Mns k < 0 \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   895
  "Mns k < Pls l \<longleftrightarrow> True"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   896
  "Mns k < Mns l \<longleftrightarrow> l < k"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   897
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   898
  by (simp_all add: of_num_less_iff)
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   899
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   900
lemma [code inline del]: "(0::int) \<equiv> Numeral0" by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   901
lemma [code inline del]: "(1::int) \<equiv> Numeral1" by simp
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   902
declare zero_is_num_zero [code inline del]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   903
declare one_is_num_one [code inline del]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   904
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   905
hide (open) const sub dup
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   906
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   907
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   908
subsection {* Numeral equations as default simplification rules *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   909
29934
5d81dd706206 tuned texts
haftmann
parents: 29667
diff changeset
   910
text {* TODO.  Be more precise here with respect to subsumed facts.  Or use named theorems anyway. *}
28021
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   911
declare (in semiring_numeral) numeral [simp]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   912
declare (in semiring_1) numeral [simp]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   913
declare (in semiring_char_0) numeral [simp]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   914
declare (in ring_1) numeral [simp]
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   915
thm numeral
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   916
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   917
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   918
text {* Toy examples *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   919
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   920
definition "bar \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat) \<and> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   921
code_thms bar
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   922
export_code bar in Haskell file -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   923
export_code bar in OCaml module_name Foo file -
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   924
ML {* @{code bar} *}
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   925
32acf3c6cd12 added HOL/ex/Numeral.thy
haftmann
parents:
diff changeset
   926
end