src/HOL/Semiring_Normalization.thy
author wenzelm
Sat Jul 18 22:58:50 2015 +0200 (2015-07-18)
changeset 60758 d8d85a8172b5
parent 59557 ebd8ecacfba6
child 61153 3d5e01b427cb
permissions -rw-r--r--
isabelle update_cartouches;
haftmann@36751
     1
(*  Title:      HOL/Semiring_Normalization.thy
wenzelm@23252
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@23252
     3
*)
wenzelm@23252
     4
wenzelm@60758
     5
section \<open>Semiring normalization\<close>
haftmann@28402
     6
haftmann@36751
     7
theory Semiring_Normalization
haftmann@36699
     8
imports Numeral_Simprocs Nat_Transfer
wenzelm@23252
     9
begin
wenzelm@23252
    10
wenzelm@60758
    11
text \<open>Prelude\<close>
haftmann@36873
    12
haftmann@36873
    13
class comm_semiring_1_cancel_crossproduct = comm_semiring_1_cancel +
haftmann@36873
    14
  assumes crossproduct_eq: "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36873
    15
begin
haftmann@36873
    16
haftmann@36873
    17
lemma crossproduct_noteq:
haftmann@36873
    18
  "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> a * c + b * d \<noteq> a * d + b * c"
haftmann@36873
    19
  by (simp add: crossproduct_eq)
haftmann@36756
    20
haftmann@36873
    21
lemma add_scale_eq_noteq:
haftmann@36873
    22
  "r \<noteq> 0 \<Longrightarrow> a = b \<and> c \<noteq> d \<Longrightarrow> a + r * c \<noteq> b + r * d"
haftmann@36873
    23
proof (rule notI)
haftmann@36873
    24
  assume nz: "r\<noteq> 0" and cnd: "a = b \<and> c\<noteq>d"
haftmann@36873
    25
    and eq: "a + (r * c) = b + (r * d)"
haftmann@36873
    26
  have "(0 * d) + (r * c) = (0 * c) + (r * d)"
haftmann@59557
    27
    using add_left_imp_eq eq mult_zero_left by (simp add: cnd)
haftmann@36873
    28
  then show False using crossproduct_eq [of 0 d] nz cnd by simp
haftmann@36873
    29
qed
haftmann@36756
    30
haftmann@36873
    31
lemma add_0_iff:
haftmann@36873
    32
  "b = b + a \<longleftrightarrow> a = 0"
haftmann@59557
    33
  using add_left_imp_eq [of b a 0] by auto
haftmann@36873
    34
haftmann@36873
    35
end
haftmann@36873
    36
haftmann@37946
    37
subclass (in idom) comm_semiring_1_cancel_crossproduct
haftmann@36756
    38
proof
haftmann@36756
    39
  fix w x y z
haftmann@36756
    40
  show "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36756
    41
  proof
haftmann@36756
    42
    assume "w * y + x * z = w * z + x * y"
haftmann@36756
    43
    then have "w * y + x * z - w * z - x * y = 0" by (simp add: algebra_simps)
haftmann@36756
    44
    then have "w * (y - z) - x * (y - z) = 0" by (simp add: algebra_simps)
haftmann@36756
    45
    then have "(y - z) * (w - x) = 0" by (simp add: algebra_simps)
haftmann@36756
    46
    then have "y - z = 0 \<or> w - x = 0" by (rule divisors_zero)
haftmann@36756
    47
    then show "w = x \<or> y = z" by auto
haftmann@57514
    48
  qed (auto simp add: ac_simps)
haftmann@36756
    49
qed
haftmann@36756
    50
haftmann@36873
    51
instance nat :: comm_semiring_1_cancel_crossproduct
haftmann@36756
    52
proof
haftmann@36756
    53
  fix w x y z :: nat
haftmann@36873
    54
  have aux: "\<And>y z. y < z \<Longrightarrow> w * y + x * z = w * z + x * y \<Longrightarrow> w = x"
haftmann@36873
    55
  proof -
haftmann@36873
    56
    fix y z :: nat
haftmann@36873
    57
    assume "y < z" then have "\<exists>k. z = y + k \<and> k \<noteq> 0" by (intro exI [of _ "z - y"]) auto
haftmann@36873
    58
    then obtain k where "z = y + k" and "k \<noteq> 0" by blast
haftmann@36873
    59
    assume "w * y + x * z = w * z + x * y"
wenzelm@60758
    60
    then have "(w * y + x * y) + x * k = (w * y + x * y) + w * k" by (simp add: \<open>z = y + k\<close> algebra_simps)
haftmann@36873
    61
    then have "x * k = w * k" by simp
wenzelm@60758
    62
    then show "w = x" using \<open>k \<noteq> 0\<close> by simp
haftmann@36873
    63
  qed
haftmann@36873
    64
  show "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36873
    65
    by (auto simp add: neq_iff dest!: aux)
haftmann@36756
    66
qed
haftmann@36756
    67
wenzelm@60758
    68
text \<open>Semiring normalization proper\<close>
haftmann@36871
    69
wenzelm@58826
    70
ML_file "Tools/semiring_normalizer.ML"
wenzelm@23252
    71
haftmann@36871
    72
context comm_semiring_1
haftmann@36871
    73
begin
haftmann@36871
    74
haftmann@59554
    75
declaration \<open>
haftmann@59554
    76
let
haftmann@59554
    77
  val rules = @{lemma
haftmann@59554
    78
    "(a * m) + (b * m) = (a + b) * m"
haftmann@59554
    79
    "(a * m) + m = (a + 1) * m"
haftmann@59554
    80
    "m + (a * m) = (a + 1) * m"
haftmann@59554
    81
    "m + m = (1 + 1) * m"
haftmann@59554
    82
    "0 + a = a"
haftmann@59554
    83
    "a + 0 = a"
haftmann@59554
    84
    "a * b = b * a"
haftmann@59554
    85
    "(a + b) * c = (a * c) + (b * c)"
haftmann@59554
    86
    "0 * a = 0"
haftmann@59554
    87
    "a * 0 = 0"
haftmann@59554
    88
    "1 * a = a"
haftmann@59554
    89
    "a * 1 = a"
haftmann@59554
    90
    "(lx * ly) * (rx * ry) = (lx * rx) * (ly * ry)"
haftmann@59554
    91
    "(lx * ly) * (rx * ry) = lx * (ly * (rx * ry))"
haftmann@59554
    92
    "(lx * ly) * (rx * ry) = rx * ((lx * ly) * ry)"
haftmann@59554
    93
    "(lx * ly) * rx = (lx * rx) * ly"
haftmann@59554
    94
    "(lx * ly) * rx = lx * (ly * rx)"
haftmann@59554
    95
    "lx * (rx * ry) = (lx * rx) * ry"
haftmann@59554
    96
    "lx * (rx * ry) = rx * (lx * ry)"
haftmann@59554
    97
    "(a + b) + (c + d) = (a + c) + (b + d)"
haftmann@59554
    98
    "(a + b) + c = a + (b + c)"
haftmann@59554
    99
    "a + (c + d) = c + (a + d)"
haftmann@59554
   100
    "(a + b) + c = (a + c) + b"
haftmann@59554
   101
    "a + c = c + a"
haftmann@59554
   102
    "a + (c + d) = (a + c) + d"
haftmann@59554
   103
    "(x ^ p) * (x ^ q) = x ^ (p + q)"
haftmann@59554
   104
    "x * (x ^ q) = x ^ (Suc q)"
haftmann@59554
   105
    "(x ^ q) * x = x ^ (Suc q)"
haftmann@59554
   106
    "x * x = x\<^sup>2"
haftmann@59554
   107
    "(x * y) ^ q = (x ^ q) * (y ^ q)"
haftmann@59554
   108
    "(x ^ p) ^ q = x ^ (p * q)"
haftmann@59554
   109
    "x ^ 0 = 1"
haftmann@59554
   110
    "x ^ 1 = x"
haftmann@59554
   111
    "x * (y + z) = (x * y) + (x * z)"
haftmann@59554
   112
    "x ^ (Suc q) = x * (x ^ q)"
haftmann@59554
   113
    "x ^ (2*n) = (x ^ n) * (x ^ n)"
haftmann@59554
   114
    by (simp_all add: algebra_simps power_add power2_eq_square
haftmann@59554
   115
      power_mult_distrib power_mult del: one_add_one)}
haftmann@59554
   116
in
haftmann@59554
   117
  Semiring_Normalizer.declare @{thm comm_semiring_1_axioms}
haftmann@59554
   118
    {semiring = ([@{cpat "?x + ?y"}, @{cpat "?x * ?y"}, @{cpat "?x ^ ?n"}, @{cpat 0}, @{cpat 1}],
haftmann@59554
   119
      rules), ring = ([], []), field = ([], []), idom = [], ideal = []}
haftmann@59554
   120
end\<close>
haftmann@36756
   121
haftmann@36871
   122
end
wenzelm@23252
   123
haftmann@36871
   124
context comm_ring_1
haftmann@36871
   125
begin
haftmann@36871
   126
haftmann@59554
   127
declaration \<open>
haftmann@59554
   128
let
haftmann@59554
   129
  val rules = @{lemma
haftmann@59554
   130
    "- x = (- 1) * x"
haftmann@59554
   131
    "x - y = x + (- y)"
haftmann@59554
   132
    by simp_all}
haftmann@59554
   133
in
haftmann@59554
   134
  Semiring_Normalizer.declare @{thm comm_ring_1_axioms}
haftmann@59554
   135
    {semiring = Semiring_Normalizer.the_semiring @{context} @{thm comm_semiring_1_axioms},
haftmann@59554
   136
      ring = ([@{cpat "?x - ?y"}, @{cpat "- ?x"}], rules), field = ([], []), idom = [], ideal = []}
haftmann@59554
   137
end\<close>
chaieb@30866
   138
haftmann@36871
   139
end
haftmann@36871
   140
haftmann@36873
   141
context comm_semiring_1_cancel_crossproduct
haftmann@36871
   142
begin
haftmann@36871
   143
haftmann@59553
   144
declaration \<open>Semiring_Normalizer.declare @{thm comm_semiring_1_cancel_crossproduct_axioms}
haftmann@59553
   145
  {semiring = Semiring_Normalizer.the_semiring @{context} @{thm comm_semiring_1_axioms},
haftmann@59553
   146
    ring = ([], []), field = ([], []), idom = @{thms crossproduct_noteq add_scale_eq_noteq}, ideal = []}\<close>
wenzelm@23252
   147
haftmann@36871
   148
end
wenzelm@23252
   149
haftmann@36871
   150
context idom
haftmann@36871
   151
begin
haftmann@36871
   152
haftmann@59553
   153
declaration \<open>Semiring_Normalizer.declare @{thm idom_axioms}
haftmann@59553
   154
  {semiring = Semiring_Normalizer.the_semiring @{context} @{thm comm_ring_1_axioms},
haftmann@59553
   155
    ring = Semiring_Normalizer.the_ring @{context} @{thm comm_ring_1_axioms},
haftmann@59553
   156
    field = ([], []), idom = @{thms crossproduct_noteq add_scale_eq_noteq},
haftmann@59553
   157
    ideal = @{thms right_minus_eq add_0_iff}}\<close>
wenzelm@23252
   158
haftmann@36871
   159
end
haftmann@36871
   160
haftmann@36871
   161
context field
haftmann@36871
   162
begin
haftmann@36871
   163
haftmann@59553
   164
declaration \<open>Semiring_Normalizer.declare @{thm field_axioms}
haftmann@59553
   165
  {semiring = Semiring_Normalizer.the_semiring @{context} @{thm idom_axioms},
haftmann@59553
   166
    ring = Semiring_Normalizer.the_ring @{context} @{thm idom_axioms},
haftmann@59554
   167
    field = ([@{cpat "?x / ?y"}, @{cpat "inverse ?x"}], @{thms divide_inverse inverse_eq_divide}),
haftmann@59553
   168
    idom = Semiring_Normalizer.the_idom @{context} @{thm idom_axioms},
haftmann@59553
   169
    ideal = Semiring_Normalizer.the_ideal @{context} @{thm idom_axioms}}\<close>
haftmann@36756
   170
haftmann@36871
   171
end
haftmann@36871
   172
haftmann@52435
   173
code_identifier
haftmann@52435
   174
  code_module Semiring_Normalization \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
huffman@47108
   175
haftmann@28402
   176
end