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