src/HOL/Semiring_Normalization.thy
author hoelzl
Tue May 11 19:21:39 2010 +0200 (2010-05-11)
changeset 36845 d778c64fc35d
parent 36756 c1ae8a0b4265
child 36871 3763c349c8c1
permissions -rw-r--r--
Add rules directly to the corresponding class locales instead.
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@36753
    53
setup Semiring_Normalizer.setup
wenzelm@23252
    54
hoelzl@36845
    55
lemma (in comm_semiring_1) semiring_ops:
hoelzl@36845
    56
  shows "TERM (x + y)" and "TERM (x * y)" and "TERM (x ^ n)"
hoelzl@36845
    57
    and "TERM 0" and "TERM 1" .
wenzelm@23252
    58
hoelzl@36845
    59
lemma (in comm_semiring_1) semiring_rules:
hoelzl@36845
    60
  "(a * m) + (b * m) = (a + b) * m"
hoelzl@36845
    61
  "(a * m) + m = (a + 1) * m"
hoelzl@36845
    62
  "m + (a * m) = (a + 1) * m"
hoelzl@36845
    63
  "m + m = (1 + 1) * m"
hoelzl@36845
    64
  "0 + a = a"
hoelzl@36845
    65
  "a + 0 = a"
hoelzl@36845
    66
  "a * b = b * a"
hoelzl@36845
    67
  "(a + b) * c = (a * c) + (b * c)"
hoelzl@36845
    68
  "0 * a = 0"
hoelzl@36845
    69
  "a * 0 = 0"
hoelzl@36845
    70
  "1 * a = a"
hoelzl@36845
    71
  "a * 1 = a"
hoelzl@36845
    72
  "(lx * ly) * (rx * ry) = (lx * rx) * (ly * ry)"
hoelzl@36845
    73
  "(lx * ly) * (rx * ry) = lx * (ly * (rx * ry))"
hoelzl@36845
    74
  "(lx * ly) * (rx * ry) = rx * ((lx * ly) * ry)"
hoelzl@36845
    75
  "(lx * ly) * rx = (lx * rx) * ly"
hoelzl@36845
    76
  "(lx * ly) * rx = lx * (ly * rx)"
hoelzl@36845
    77
  "lx * (rx * ry) = (lx * rx) * ry"
hoelzl@36845
    78
  "lx * (rx * ry) = rx * (lx * ry)"
hoelzl@36845
    79
  "(a + b) + (c + d) = (a + c) + (b + d)"
hoelzl@36845
    80
  "(a + b) + c = a + (b + c)"
hoelzl@36845
    81
  "a + (c + d) = c + (a + d)"
hoelzl@36845
    82
  "(a + b) + c = (a + c) + b"
hoelzl@36845
    83
  "a + c = c + a"
hoelzl@36845
    84
  "a + (c + d) = (a + c) + d"
hoelzl@36845
    85
  "(x ^ p) * (x ^ q) = x ^ (p + q)"
hoelzl@36845
    86
  "x * (x ^ q) = x ^ (Suc q)"
hoelzl@36845
    87
  "(x ^ q) * x = x ^ (Suc q)"
hoelzl@36845
    88
  "x * x = x ^ 2"
hoelzl@36845
    89
  "(x * y) ^ q = (x ^ q) * (y ^ q)"
hoelzl@36845
    90
  "(x ^ p) ^ q = x ^ (p * q)"
hoelzl@36845
    91
  "x ^ 0 = 1"
hoelzl@36845
    92
  "x ^ 1 = x"
hoelzl@36845
    93
  "x * (y + z) = (x * y) + (x * z)"
hoelzl@36845
    94
  "x ^ (Suc q) = x * (x ^ q)"
hoelzl@36845
    95
  "x ^ (2*n) = (x ^ n) * (x ^ n)"
hoelzl@36845
    96
  "x ^ (Suc (2*n)) = x * ((x ^ n) * (x ^ n))"
hoelzl@36845
    97
  by (simp_all add: algebra_simps power_add power2_eq_square power_mult_distrib power_mult)
wenzelm@23252
    98
haftmann@36756
    99
lemmas (in comm_semiring_1) normalizing_comm_semiring_1_axioms =
haftmann@36756
   100
  comm_semiring_1_axioms [normalizer
hoelzl@36845
   101
    semiring ops: semiring_ops
hoelzl@36845
   102
    semiring rules: semiring_rules]
haftmann@36756
   103
haftmann@36756
   104
declaration (in comm_semiring_1)
haftmann@36756
   105
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_axioms} *}
wenzelm@23573
   106
hoelzl@36845
   107
lemma (in comm_ring_1) ring_ops: shows "TERM (x- y)" and "TERM (- x)" .
wenzelm@23252
   108
hoelzl@36845
   109
lemma (in comm_ring_1) ring_rules:
hoelzl@36845
   110
  "- x = (- 1) * x"
hoelzl@36845
   111
  "x - y = x + (- y)"
hoelzl@36845
   112
  by (simp_all add: diff_minus)
wenzelm@23252
   113
haftmann@36756
   114
lemmas (in comm_ring_1) normalizing_comm_ring_1_axioms =
haftmann@36756
   115
  comm_ring_1_axioms [normalizer
hoelzl@36845
   116
    semiring ops: semiring_ops
hoelzl@36845
   117
    semiring rules: semiring_rules
hoelzl@36845
   118
    ring ops: ring_ops
hoelzl@36845
   119
    ring rules: ring_rules]
chaieb@30866
   120
haftmann@36756
   121
declaration (in comm_ring_1)
haftmann@36756
   122
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_ring_1_axioms} *}
chaieb@23327
   123
hoelzl@36845
   124
lemma (in comm_semiring_1_cancel_norm) noteq_reduce:
hoelzl@36845
   125
  "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> (a * c) + (b * d) \<noteq> (a * d) + (b * c)"
wenzelm@23252
   126
proof-
wenzelm@23252
   127
  have "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> \<not> (a = b \<or> c = d)" by simp
hoelzl@36845
   128
  also have "\<dots> \<longleftrightarrow> (a * c) + (b * d) \<noteq> (a * d) + (b * c)"
hoelzl@36845
   129
    using add_mult_solve by blast
hoelzl@36845
   130
  finally show "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> (a * c) + (b * d) \<noteq> (a * d) + (b * c)"
wenzelm@23252
   131
    by simp
wenzelm@23252
   132
qed
wenzelm@23252
   133
hoelzl@36845
   134
lemma (in comm_semiring_1_cancel_norm) add_scale_eq_noteq:
hoelzl@36845
   135
  "\<lbrakk>r \<noteq> 0 ; a = b \<and> c \<noteq> d\<rbrakk> \<Longrightarrow> a + (r * c) \<noteq> b + (r * d)"
wenzelm@23252
   136
proof(clarify)
hoelzl@36845
   137
  assume nz: "r\<noteq> 0" and cnd: "c\<noteq>d"
hoelzl@36845
   138
    and eq: "b + (r * c) = b + (r * d)"
hoelzl@36845
   139
  have "(0 * d) + (r * c) = (0 * c) + (r * d)"
hoelzl@36845
   140
    using add_imp_eq eq mult_zero_left by simp
hoelzl@36845
   141
  thus "False" using add_mult_solve[of 0 d] nz cnd by simp
wenzelm@23252
   142
qed
wenzelm@23252
   143
hoelzl@36845
   144
lemma (in comm_semiring_1_cancel_norm) add_0_iff:
hoelzl@36845
   145
  "x = x + a \<longleftrightarrow> a = 0"
chaieb@25250
   146
proof-
hoelzl@36845
   147
  have "a = 0 \<longleftrightarrow> x + a = x + 0" using add_imp_eq[of x a 0] by auto
hoelzl@36845
   148
  thus "x = x + a \<longleftrightarrow> a = 0" by (auto simp add: add_commute)
chaieb@25250
   149
qed
chaieb@25250
   150
haftmann@36756
   151
declare (in comm_semiring_1_cancel_norm)
haftmann@36756
   152
  normalizing_comm_semiring_1_axioms [normalizer del]
wenzelm@23252
   153
haftmann@36756
   154
lemmas (in comm_semiring_1_cancel_norm)
haftmann@36756
   155
  normalizing_comm_semiring_1_cancel_norm_axioms =
haftmann@36756
   156
  comm_semiring_1_cancel_norm_axioms [normalizer
hoelzl@36845
   157
    semiring ops: semiring_ops
hoelzl@36845
   158
    semiring rules: semiring_rules
hoelzl@36845
   159
    idom rules: noteq_reduce add_scale_eq_noteq]
wenzelm@23252
   160
haftmann@36756
   161
declaration (in comm_semiring_1_cancel_norm)
haftmann@36756
   162
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_cancel_norm_axioms} *}
wenzelm@23252
   163
haftmann@36756
   164
declare (in idom) normalizing_comm_ring_1_axioms [normalizer del]
wenzelm@23252
   165
haftmann@36756
   166
lemmas (in idom) normalizing_idom_axioms = idom_axioms [normalizer
hoelzl@36845
   167
  semiring ops: semiring_ops
hoelzl@36845
   168
  semiring rules: semiring_rules
hoelzl@36845
   169
  ring ops: ring_ops
hoelzl@36845
   170
  ring rules: ring_rules
hoelzl@36845
   171
  idom rules: noteq_reduce add_scale_eq_noteq
hoelzl@36845
   172
  ideal rules: right_minus_eq add_0_iff]
wenzelm@23252
   173
haftmann@36756
   174
declaration (in idom)
haftmann@36756
   175
  {* Semiring_Normalizer.semiring_funs @{thm normalizing_idom_axioms} *}
wenzelm@23252
   176
hoelzl@36845
   177
lemma (in field) field_ops:
hoelzl@36845
   178
  shows "TERM (x / y)" and "TERM (inverse x)" .
chaieb@23327
   179
hoelzl@36845
   180
lemmas (in field) field_rules = divide_inverse inverse_eq_divide
haftmann@28402
   181
haftmann@36756
   182
lemmas (in field) normalizing_field_axioms =
haftmann@36756
   183
  field_axioms [normalizer
hoelzl@36845
   184
    semiring ops: semiring_ops
hoelzl@36845
   185
    semiring rules: semiring_rules
hoelzl@36845
   186
    ring ops: ring_ops
hoelzl@36845
   187
    ring rules: ring_rules
hoelzl@36845
   188
    field ops: field_ops
hoelzl@36845
   189
    field rules: field_rules
hoelzl@36845
   190
    idom rules: noteq_reduce add_scale_eq_noteq
hoelzl@36845
   191
    ideal rules: right_minus_eq add_0_iff]
haftmann@36756
   192
haftmann@36756
   193
declaration (in field)
haftmann@36756
   194
  {* Semiring_Normalizer.field_funs @{thm normalizing_field_axioms} *}
haftmann@28402
   195
hoelzl@36845
   196
hide_fact (open) normalizing_comm_semiring_1_axioms
hoelzl@36845
   197
  normalizing_comm_semiring_1_cancel_norm_axioms semiring_ops semiring_rules
hoelzl@36845
   198
hoelzl@36845
   199
hide_fact (open) normalizing_comm_ring_1_axioms
hoelzl@36845
   200
  normalizing_idom_axioms ring_ops ring_rules
hoelzl@36845
   201
hoelzl@36845
   202
hide_fact (open)  normalizing_field_axioms field_ops field_rules
hoelzl@36845
   203
hoelzl@36845
   204
hide_fact (open) add_scale_eq_noteq noteq_reduce
hoelzl@36845
   205
haftmann@28402
   206
end