src/HOL/Semiring_Normalization.thy
author haftmann
Wed May 12 13:51:22 2010 +0200 (2010-05-12)
changeset 36872 6520ba1256a6
parent 36871 3763c349c8c1
child 36873 112e613e8d0b
permissions -rw-r--r--
tuned fact collection names and some proofs
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@36872
    60
lemma normalizing_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@36872
    64
lemma normalizing_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
haftmann@36872
   106
    semiring ops: normalizing_semiring_ops
haftmann@36872
   107
    semiring rules: normalizing_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@36872
   117
lemma normalizing_ring_ops: shows "TERM (x- y)" and "TERM (- x)" .
haftmann@36871
   118
haftmann@36872
   119
lemma normalizing_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
haftmann@36872
   126
    semiring ops: normalizing_semiring_ops
haftmann@36872
   127
    semiring rules: normalizing_semiring_rules
haftmann@36872
   128
    ring ops: normalizing_ring_ops
haftmann@36872
   129
    ring rules: normalizing_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:
haftmann@36872
   140
  "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> a * c + b * d \<noteq> a * d + b * c"
haftmann@36872
   141
  by (simp add: add_mult_solve)
wenzelm@23252
   142
haftmann@36871
   143
lemma add_scale_eq_noteq:
haftmann@36872
   144
  "r \<noteq> 0 \<Longrightarrow> a = b \<and> c \<noteq> d \<Longrightarrow> a + r * c \<noteq> b + r * d"
haftmann@36872
   145
proof (rule notI)
haftmann@36872
   146
  assume nz: "r\<noteq> 0" and cnd: "a = b \<and> c\<noteq>d"
haftmann@36872
   147
    and eq: "a + (r * c) = b + (r * d)"
hoelzl@36845
   148
  have "(0 * d) + (r * c) = (0 * c) + (r * d)"
haftmann@36872
   149
    using add_imp_eq eq mult_zero_left by (simp add: cnd)
haftmann@36872
   150
  then show False using add_mult_solve [of 0 d] nz cnd by simp
wenzelm@23252
   151
qed
wenzelm@23252
   152
haftmann@36871
   153
lemma add_0_iff:
hoelzl@36845
   154
  "x = x + a \<longleftrightarrow> a = 0"
haftmann@36872
   155
  using add_imp_eq [of x a 0] by auto
chaieb@25250
   156
haftmann@36871
   157
declare
haftmann@36756
   158
  normalizing_comm_semiring_1_axioms [normalizer del]
wenzelm@23252
   159
haftmann@36871
   160
lemmas
haftmann@36756
   161
  normalizing_comm_semiring_1_cancel_norm_axioms =
haftmann@36756
   162
  comm_semiring_1_cancel_norm_axioms [normalizer
haftmann@36872
   163
    semiring ops: normalizing_semiring_ops
haftmann@36872
   164
    semiring rules: normalizing_semiring_rules
hoelzl@36845
   165
    idom rules: noteq_reduce add_scale_eq_noteq]
wenzelm@23252
   166
haftmann@36871
   167
declaration
haftmann@36756
   168
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_cancel_norm_axioms} *}
wenzelm@23252
   169
haftmann@36871
   170
end
wenzelm@23252
   171
haftmann@36871
   172
context idom
haftmann@36871
   173
begin
haftmann@36871
   174
haftmann@36871
   175
declare normalizing_comm_ring_1_axioms [normalizer del]
haftmann@36871
   176
haftmann@36871
   177
lemmas normalizing_idom_axioms = idom_axioms [normalizer
haftmann@36872
   178
  semiring ops: normalizing_semiring_ops
haftmann@36872
   179
  semiring rules: normalizing_semiring_rules
haftmann@36872
   180
  ring ops: normalizing_ring_ops
haftmann@36872
   181
  ring rules: normalizing_ring_rules
hoelzl@36845
   182
  idom rules: noteq_reduce add_scale_eq_noteq
hoelzl@36845
   183
  ideal rules: right_minus_eq add_0_iff]
wenzelm@23252
   184
haftmann@36871
   185
declaration
haftmann@36756
   186
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_idom_axioms} *}
wenzelm@23252
   187
haftmann@36871
   188
end
haftmann@36871
   189
haftmann@36871
   190
context field
haftmann@36871
   191
begin
haftmann@36871
   192
haftmann@36872
   193
lemma normalizing_field_ops:
hoelzl@36845
   194
  shows "TERM (x / y)" and "TERM (inverse x)" .
chaieb@23327
   195
haftmann@36872
   196
lemmas normalizing_field_rules = divide_inverse inverse_eq_divide
haftmann@28402
   197
haftmann@36871
   198
lemmas normalizing_field_axioms =
haftmann@36756
   199
  field_axioms [normalizer
haftmann@36872
   200
    semiring ops: normalizing_semiring_ops
haftmann@36872
   201
    semiring rules: normalizing_semiring_rules
haftmann@36872
   202
    ring ops: normalizing_ring_ops
haftmann@36872
   203
    ring rules: normalizing_ring_rules
haftmann@36872
   204
    field ops: normalizing_field_ops
haftmann@36872
   205
    field rules: normalizing_field_rules
hoelzl@36845
   206
    idom rules: noteq_reduce add_scale_eq_noteq
hoelzl@36845
   207
    ideal rules: right_minus_eq add_0_iff]
haftmann@36756
   208
haftmann@36871
   209
declaration
haftmann@36756
   210
  {* Semiring_Normalizer.field_funs @{thm normalizing_field_axioms} *}
haftmann@28402
   211
haftmann@36871
   212
end
haftmann@36871
   213
hoelzl@36845
   214
hide_fact (open) normalizing_comm_semiring_1_axioms
haftmann@36872
   215
  normalizing_comm_semiring_1_cancel_norm_axioms normalizing_semiring_ops normalizing_semiring_rules
hoelzl@36845
   216
hoelzl@36845
   217
hide_fact (open) normalizing_comm_ring_1_axioms
haftmann@36872
   218
  normalizing_idom_axioms normalizing_ring_ops normalizing_ring_rules
hoelzl@36845
   219
haftmann@36872
   220
hide_fact (open) normalizing_field_axioms normalizing_field_ops normalizing_field_rules
hoelzl@36845
   221
hoelzl@36845
   222
hide_fact (open) add_scale_eq_noteq noteq_reduce
hoelzl@36845
   223
haftmann@28402
   224
end