src/HOL/Semiring_Normalization.thy
author haftmann
Wed May 12 12:31:52 2010 +0200 (2010-05-12)
changeset 36871 3763c349c8c1
parent 36845 d778c64fc35d
child 36872 6520ba1256a6
permissions -rw-r--r--
grouped local statements
haftmann@36751
     1
(*  Title:      HOL/Semiring_Normalization.thy
wenzelm@23252
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@23252
     3
*)
wenzelm@23252
     4
haftmann@36751
     5
header {* Semiring normalization *}
haftmann@28402
     6
haftmann@36751
     7
theory Semiring_Normalization
haftmann@36699
     8
imports Numeral_Simprocs Nat_Transfer
wenzelm@23252
     9
uses
haftmann@36753
    10
  "Tools/semiring_normalizer.ML"
wenzelm@23252
    11
begin
wenzelm@23252
    12
haftmann@36756
    13
text {* FIXME prelude *}
haftmann@36756
    14
haftmann@36756
    15
class comm_semiring_1_cancel_norm (*FIXME name*) = comm_semiring_1_cancel +
haftmann@36756
    16
  assumes add_mult_solve: "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36756
    17
haftmann@36756
    18
sublocale idom < comm_semiring_1_cancel_norm
haftmann@36756
    19
proof
haftmann@36756
    20
  fix w x y z
haftmann@36756
    21
  show "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36756
    22
  proof
haftmann@36756
    23
    assume "w * y + x * z = w * z + x * y"
haftmann@36756
    24
    then have "w * y + x * z - w * z - x * y = 0" by (simp add: algebra_simps)
haftmann@36756
    25
    then have "w * (y - z) - x * (y - z) = 0" by (simp add: algebra_simps)
haftmann@36756
    26
    then have "(y - z) * (w - x) = 0" by (simp add: algebra_simps)
haftmann@36756
    27
    then have "y - z = 0 \<or> w - x = 0" by (rule divisors_zero)
haftmann@36756
    28
    then show "w = x \<or> y = z" by auto
haftmann@36756
    29
  qed (auto simp add: add_ac)
haftmann@36756
    30
qed
haftmann@36756
    31
haftmann@36756
    32
instance nat :: comm_semiring_1_cancel_norm
haftmann@36756
    33
proof
haftmann@36756
    34
  fix w x y z :: nat
haftmann@36756
    35
  { assume p: "w * y + x * z = w * z + x * y" and ynz: "y \<noteq> z"
haftmann@36756
    36
    hence "y < z \<or> y > z" by arith
haftmann@36756
    37
    moreover {
haftmann@36756
    38
      assume lt:"y <z" hence "\<exists>k. z = y + k \<and> k > 0" by (rule_tac x="z - y" in exI, auto)
haftmann@36756
    39
      then obtain k where kp: "k>0" and yz:"z = y + k" by blast
haftmann@36756
    40
      from p have "(w * y + x *y) + x*k = (w * y + x*y) + w*k" by (simp add: yz algebra_simps)
haftmann@36756
    41
      hence "x*k = w*k" by simp
haftmann@36756
    42
      hence "w = x" using kp by simp }
haftmann@36756
    43
    moreover {
haftmann@36756
    44
      assume lt: "y >z" hence "\<exists>k. y = z + k \<and> k>0" by (rule_tac x="y - z" in exI, auto)
haftmann@36756
    45
      then obtain k where kp: "k>0" and yz:"y = z + k" by blast
haftmann@36756
    46
      from p have "(w * z + x *z) + w*k = (w * z + x*z) + x*k" by (simp add: yz algebra_simps)
haftmann@36756
    47
      hence "w*k = x*k" by simp
haftmann@36756
    48
      hence "w = x" using kp by simp }
haftmann@36756
    49
    ultimately have "w=x" by blast }
haftmann@36756
    50
  then show "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z" by auto
haftmann@36756
    51
qed
haftmann@36756
    52
haftmann@36871
    53
text {* semiring normalization proper *}
haftmann@36871
    54
haftmann@36753
    55
setup Semiring_Normalizer.setup
wenzelm@23252
    56
haftmann@36871
    57
context comm_semiring_1
haftmann@36871
    58
begin
haftmann@36871
    59
haftmann@36871
    60
lemma semiring_ops:
hoelzl@36845
    61
  shows "TERM (x + y)" and "TERM (x * y)" and "TERM (x ^ n)"
hoelzl@36845
    62
    and "TERM 0" and "TERM 1" .
wenzelm@23252
    63
haftmann@36871
    64
lemma semiring_rules:
hoelzl@36845
    65
  "(a * m) + (b * m) = (a + b) * m"
hoelzl@36845
    66
  "(a * m) + m = (a + 1) * m"
hoelzl@36845
    67
  "m + (a * m) = (a + 1) * m"
hoelzl@36845
    68
  "m + m = (1 + 1) * m"
hoelzl@36845
    69
  "0 + a = a"
hoelzl@36845
    70
  "a + 0 = a"
hoelzl@36845
    71
  "a * b = b * a"
hoelzl@36845
    72
  "(a + b) * c = (a * c) + (b * c)"
hoelzl@36845
    73
  "0 * a = 0"
hoelzl@36845
    74
  "a * 0 = 0"
hoelzl@36845
    75
  "1 * a = a"
hoelzl@36845
    76
  "a * 1 = a"
hoelzl@36845
    77
  "(lx * ly) * (rx * ry) = (lx * rx) * (ly * ry)"
hoelzl@36845
    78
  "(lx * ly) * (rx * ry) = lx * (ly * (rx * ry))"
hoelzl@36845
    79
  "(lx * ly) * (rx * ry) = rx * ((lx * ly) * ry)"
hoelzl@36845
    80
  "(lx * ly) * rx = (lx * rx) * ly"
hoelzl@36845
    81
  "(lx * ly) * rx = lx * (ly * rx)"
hoelzl@36845
    82
  "lx * (rx * ry) = (lx * rx) * ry"
hoelzl@36845
    83
  "lx * (rx * ry) = rx * (lx * ry)"
hoelzl@36845
    84
  "(a + b) + (c + d) = (a + c) + (b + d)"
hoelzl@36845
    85
  "(a + b) + c = a + (b + c)"
hoelzl@36845
    86
  "a + (c + d) = c + (a + d)"
hoelzl@36845
    87
  "(a + b) + c = (a + c) + b"
hoelzl@36845
    88
  "a + c = c + a"
hoelzl@36845
    89
  "a + (c + d) = (a + c) + d"
hoelzl@36845
    90
  "(x ^ p) * (x ^ q) = x ^ (p + q)"
hoelzl@36845
    91
  "x * (x ^ q) = x ^ (Suc q)"
hoelzl@36845
    92
  "(x ^ q) * x = x ^ (Suc q)"
hoelzl@36845
    93
  "x * x = x ^ 2"
hoelzl@36845
    94
  "(x * y) ^ q = (x ^ q) * (y ^ q)"
hoelzl@36845
    95
  "(x ^ p) ^ q = x ^ (p * q)"
hoelzl@36845
    96
  "x ^ 0 = 1"
hoelzl@36845
    97
  "x ^ 1 = x"
hoelzl@36845
    98
  "x * (y + z) = (x * y) + (x * z)"
hoelzl@36845
    99
  "x ^ (Suc q) = x * (x ^ q)"
hoelzl@36845
   100
  "x ^ (2*n) = (x ^ n) * (x ^ n)"
hoelzl@36845
   101
  "x ^ (Suc (2*n)) = x * ((x ^ n) * (x ^ n))"
hoelzl@36845
   102
  by (simp_all add: algebra_simps power_add power2_eq_square power_mult_distrib power_mult)
wenzelm@23252
   103
haftmann@36871
   104
lemmas normalizing_comm_semiring_1_axioms =
haftmann@36756
   105
  comm_semiring_1_axioms [normalizer
hoelzl@36845
   106
    semiring ops: semiring_ops
hoelzl@36845
   107
    semiring rules: semiring_rules]
haftmann@36756
   108
haftmann@36871
   109
declaration
haftmann@36756
   110
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_axioms} *}
wenzelm@23573
   111
haftmann@36871
   112
end
wenzelm@23252
   113
haftmann@36871
   114
context comm_ring_1
haftmann@36871
   115
begin
haftmann@36871
   116
haftmann@36871
   117
lemma ring_ops: shows "TERM (x- y)" and "TERM (- x)" .
haftmann@36871
   118
haftmann@36871
   119
lemma ring_rules:
hoelzl@36845
   120
  "- x = (- 1) * x"
hoelzl@36845
   121
  "x - y = x + (- y)"
hoelzl@36845
   122
  by (simp_all add: diff_minus)
wenzelm@23252
   123
haftmann@36871
   124
lemmas normalizing_comm_ring_1_axioms =
haftmann@36756
   125
  comm_ring_1_axioms [normalizer
hoelzl@36845
   126
    semiring ops: semiring_ops
hoelzl@36845
   127
    semiring rules: semiring_rules
hoelzl@36845
   128
    ring ops: ring_ops
hoelzl@36845
   129
    ring rules: ring_rules]
chaieb@30866
   130
haftmann@36871
   131
declaration
haftmann@36756
   132
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_ring_1_axioms} *}
chaieb@23327
   133
haftmann@36871
   134
end
haftmann@36871
   135
haftmann@36871
   136
context comm_semiring_1_cancel_norm
haftmann@36871
   137
begin
haftmann@36871
   138
haftmann@36871
   139
lemma noteq_reduce:
hoelzl@36845
   140
  "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> (a * c) + (b * d) \<noteq> (a * d) + (b * c)"
wenzelm@23252
   141
proof-
wenzelm@23252
   142
  have "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> \<not> (a = b \<or> c = d)" by simp
hoelzl@36845
   143
  also have "\<dots> \<longleftrightarrow> (a * c) + (b * d) \<noteq> (a * d) + (b * c)"
hoelzl@36845
   144
    using add_mult_solve by blast
hoelzl@36845
   145
  finally show "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> (a * c) + (b * d) \<noteq> (a * d) + (b * c)"
wenzelm@23252
   146
    by simp
wenzelm@23252
   147
qed
wenzelm@23252
   148
haftmann@36871
   149
lemma add_scale_eq_noteq:
hoelzl@36845
   150
  "\<lbrakk>r \<noteq> 0 ; a = b \<and> c \<noteq> d\<rbrakk> \<Longrightarrow> a + (r * c) \<noteq> b + (r * d)"
wenzelm@23252
   151
proof(clarify)
hoelzl@36845
   152
  assume nz: "r\<noteq> 0" and cnd: "c\<noteq>d"
hoelzl@36845
   153
    and eq: "b + (r * c) = b + (r * d)"
hoelzl@36845
   154
  have "(0 * d) + (r * c) = (0 * c) + (r * d)"
hoelzl@36845
   155
    using add_imp_eq eq mult_zero_left by simp
hoelzl@36845
   156
  thus "False" using add_mult_solve[of 0 d] nz cnd by simp
wenzelm@23252
   157
qed
wenzelm@23252
   158
haftmann@36871
   159
lemma add_0_iff:
hoelzl@36845
   160
  "x = x + a \<longleftrightarrow> a = 0"
chaieb@25250
   161
proof-
hoelzl@36845
   162
  have "a = 0 \<longleftrightarrow> x + a = x + 0" using add_imp_eq[of x a 0] by auto
hoelzl@36845
   163
  thus "x = x + a \<longleftrightarrow> a = 0" by (auto simp add: add_commute)
chaieb@25250
   164
qed
chaieb@25250
   165
haftmann@36871
   166
declare
haftmann@36756
   167
  normalizing_comm_semiring_1_axioms [normalizer del]
wenzelm@23252
   168
haftmann@36871
   169
lemmas
haftmann@36756
   170
  normalizing_comm_semiring_1_cancel_norm_axioms =
haftmann@36756
   171
  comm_semiring_1_cancel_norm_axioms [normalizer
hoelzl@36845
   172
    semiring ops: semiring_ops
hoelzl@36845
   173
    semiring rules: semiring_rules
hoelzl@36845
   174
    idom rules: noteq_reduce add_scale_eq_noteq]
wenzelm@23252
   175
haftmann@36871
   176
declaration
haftmann@36756
   177
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_cancel_norm_axioms} *}
wenzelm@23252
   178
haftmann@36871
   179
end
wenzelm@23252
   180
haftmann@36871
   181
context idom
haftmann@36871
   182
begin
haftmann@36871
   183
haftmann@36871
   184
declare normalizing_comm_ring_1_axioms [normalizer del]
haftmann@36871
   185
haftmann@36871
   186
lemmas normalizing_idom_axioms = idom_axioms [normalizer
hoelzl@36845
   187
  semiring ops: semiring_ops
hoelzl@36845
   188
  semiring rules: semiring_rules
hoelzl@36845
   189
  ring ops: ring_ops
hoelzl@36845
   190
  ring rules: ring_rules
hoelzl@36845
   191
  idom rules: noteq_reduce add_scale_eq_noteq
hoelzl@36845
   192
  ideal rules: right_minus_eq add_0_iff]
wenzelm@23252
   193
haftmann@36871
   194
declaration
haftmann@36756
   195
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_idom_axioms} *}
wenzelm@23252
   196
haftmann@36871
   197
end
haftmann@36871
   198
haftmann@36871
   199
context field
haftmann@36871
   200
begin
haftmann@36871
   201
haftmann@36871
   202
lemma field_ops:
hoelzl@36845
   203
  shows "TERM (x / y)" and "TERM (inverse x)" .
chaieb@23327
   204
haftmann@36871
   205
lemmas field_rules = divide_inverse inverse_eq_divide
haftmann@28402
   206
haftmann@36871
   207
lemmas normalizing_field_axioms =
haftmann@36756
   208
  field_axioms [normalizer
hoelzl@36845
   209
    semiring ops: semiring_ops
hoelzl@36845
   210
    semiring rules: semiring_rules
hoelzl@36845
   211
    ring ops: ring_ops
hoelzl@36845
   212
    ring rules: ring_rules
hoelzl@36845
   213
    field ops: field_ops
hoelzl@36845
   214
    field rules: field_rules
hoelzl@36845
   215
    idom rules: noteq_reduce add_scale_eq_noteq
hoelzl@36845
   216
    ideal rules: right_minus_eq add_0_iff]
haftmann@36756
   217
haftmann@36871
   218
declaration
haftmann@36756
   219
  {* Semiring_Normalizer.field_funs @{thm normalizing_field_axioms} *}
haftmann@28402
   220
haftmann@36871
   221
end
haftmann@36871
   222
hoelzl@36845
   223
hide_fact (open) normalizing_comm_semiring_1_axioms
hoelzl@36845
   224
  normalizing_comm_semiring_1_cancel_norm_axioms semiring_ops semiring_rules
hoelzl@36845
   225
hoelzl@36845
   226
hide_fact (open) normalizing_comm_ring_1_axioms
hoelzl@36845
   227
  normalizing_idom_axioms ring_ops ring_rules
hoelzl@36845
   228
haftmann@36871
   229
hide_fact (open) normalizing_field_axioms field_ops field_rules
hoelzl@36845
   230
hoelzl@36845
   231
hide_fact (open) add_scale_eq_noteq noteq_reduce
hoelzl@36845
   232
haftmann@28402
   233
end