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