
haftmann@36751: (*  Title:      HOL/Semiring_Normalization.thy
wenzelm@23252:     Author:     Amine Chaieb, TU Muenchen
wenzelm@23252: *)
wenzelm@23252: 
haftmann@36751: header {* Semiring normalization *}
haftmann@28402: 
haftmann@36751: theory Semiring_Normalization
haftmann@36699: imports Numeral_Simprocs Nat_Transfer
wenzelm@23252: uses
haftmann@36753:   "Tools/semiring_normalizer.ML"
wenzelm@23252: begin
wenzelm@23252: 
haftmann@36873: text {* Prelude *}
haftmann@36873: 
haftmann@36873: class comm_semiring_1_cancel_crossproduct = comm_semiring_1_cancel +
haftmann@36873:   assumes crossproduct_eq: "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36873: begin
haftmann@36873: 
haftmann@36873: lemma crossproduct_noteq:
haftmann@36873:   "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> a * c + b * d \<noteq> a * d + b * c"
haftmann@36873:   by (simp add: crossproduct_eq)
haftmann@36756: 
haftmann@36873: lemma add_scale_eq_noteq:
haftmann@36873:   "r \<noteq> 0 \<Longrightarrow> a = b \<and> c \<noteq> d \<Longrightarrow> a + r * c \<noteq> b + r * d"
haftmann@36873: proof (rule notI)
haftmann@36873:   assume nz: "r\<noteq> 0" and cnd: "a = b \<and> c\<noteq>d"
haftmann@36873:     and eq: "a + (r * c) = b + (r * d)"
haftmann@36873:   have "(0 * d) + (r * c) = (0 * c) + (r * d)"
haftmann@36873:     using add_imp_eq eq mult_zero_left by (simp add: cnd)
haftmann@36873:   then show False using crossproduct_eq [of 0 d] nz cnd by simp
haftmann@36873: qed
haftmann@36756: 
haftmann@36873: lemma add_0_iff:
haftmann@36873:   "b = b + a \<longleftrightarrow> a = 0"
haftmann@36873:   using add_imp_eq [of b a 0] by auto
haftmann@36873: 
haftmann@36873: end
haftmann@36873: 
haftmann@36873: sublocale idom < comm_semiring_1_cancel_crossproduct
haftmann@36756: proof
haftmann@36756:   fix w x y z
haftmann@36756:   show "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36756:   proof
haftmann@36756:     assume "w * y + x * z = w * z + x * y"
haftmann@36756:     then have "w * y + x * z - w * z - x * y = 0" by (simp add: algebra_simps)
haftmann@36756:     then have "w * (y - z) - x * (y - z) = 0" by (simp add: algebra_simps)
haftmann@36756:     then have "(y - z) * (w - x) = 0" by (simp add: algebra_simps)
haftmann@36756:     then have "y - z = 0 \<or> w - x = 0" by (rule divisors_zero)
haftmann@36756:     then show "w = x \<or> y = z" by auto
haftmann@36756:   qed (auto simp add: add_ac)
haftmann@36756: qed
haftmann@36756: 
haftmann@36873: instance nat :: comm_semiring_1_cancel_crossproduct
haftmann@36756: proof
haftmann@36756:   fix w x y z :: nat
haftmann@36873:   have aux: "\<And>y z. y < z \<Longrightarrow> w * y + x * z = w * z + x * y \<Longrightarrow> w = x"
haftmann@36873:   proof -
haftmann@36873:     fix y z :: nat
haftmann@36873:     assume "y < z" then have "\<exists>k. z = y + k \<and> k \<noteq> 0" by (intro exI [of _ "z - y"]) auto
haftmann@36873:     then obtain k where "z = y + k" and "k \<noteq> 0" by blast
haftmann@36873:     assume "w * y + x * z = w * z + x * y"
haftmann@36873:     then have "(w * y + x * y) + x * k = (w * y + x * y) + w * k" by (simp add: `z = y + k` algebra_simps)
haftmann@36873:     then have "x * k = w * k" by simp
haftmann@36873:     then show "w = x" using `k \<noteq> 0` by simp
haftmann@36873:   qed
haftmann@36873:   show "w * y + x * z = w * z + x * y \<longleftrightarrow> w = x \<or> y = z"
haftmann@36873:     by (auto simp add: neq_iff dest!: aux)
haftmann@36756: qed
haftmann@36756: 
haftmann@36873: text {* Semiring normalization proper *}
haftmann@36871: 
haftmann@36753: setup Semiring_Normalizer.setup
wenzelm@23252: 
haftmann@36871: context comm_semiring_1
haftmann@36871: begin
haftmann@36871: 
haftmann@36872: lemma normalizing_semiring_ops:
hoelzl@36845:   shows "TERM (x + y)" and "TERM (x * y)" and "TERM (x ^ n)"
hoelzl@36845:     and "TERM 0" and "TERM 1" .
wenzelm@23252: 
haftmann@36872: lemma normalizing_semiring_rules:
hoelzl@36845:   "(a * m) + (b * m) = (a + b) * m"
hoelzl@36845:   "(a * m) + m = (a + 1) * m"
hoelzl@36845:   "m + (a * m) = (a + 1) * m"
hoelzl@36845:   "m + m = (1 + 1) * m"
hoelzl@36845:   "0 + a = a"
hoelzl@36845:   "a + 0 = a"
hoelzl@36845:   "a * b = b * a"
hoelzl@36845:   "(a + b) * c = (a * c) + (b * c)"
hoelzl@36845:   "0 * a = 0"
hoelzl@36845:   "a * 0 = 0"
hoelzl@36845:   "1 * a = a"
hoelzl@36845:   "a * 1 = a"
hoelzl@36845:   "(lx * ly) * (rx * ry) = (lx * rx) * (ly * ry)"
hoelzl@36845:   "(lx * ly) * (rx * ry) = lx * (ly * (rx * ry))"
hoelzl@36845:   "(lx * ly) * (rx * ry) = rx * ((lx * ly) * ry)"
hoelzl@36845:   "(lx * ly) * rx = (lx * rx) * ly"
hoelzl@36845:   "(lx * ly) * rx = lx * (ly * rx)"
hoelzl@36845:   "lx * (rx * ry) = (lx * rx) * ry"
hoelzl@36845:   "lx * (rx * ry) = rx * (lx * ry)"
hoelzl@36845:   "(a + b) + (c + d) = (a + c) + (b + d)"
hoelzl@36845:   "(a + b) + c = a + (b + c)"
hoelzl@36845:   "a + (c + d) = c + (a + d)"
hoelzl@36845:   "(a + b) + c = (a + c) + b"
hoelzl@36845:   "a + c = c + a"
hoelzl@36845:   "a + (c + d) = (a + c) + d"
hoelzl@36845:   "(x ^ p) * (x ^ q) = x ^ (p + q)"
hoelzl@36845:   "x * (x ^ q) = x ^ (Suc q)"
hoelzl@36845:   "(x ^ q) * x = x ^ (Suc q)"
hoelzl@36845:   "x * x = x ^ 2"
hoelzl@36845:   "(x * y) ^ q = (x ^ q) * (y ^ q)"
hoelzl@36845:   "(x ^ p) ^ q = x ^ (p * q)"
hoelzl@36845:   "x ^ 0 = 1"
hoelzl@36845:   "x ^ 1 = x"
hoelzl@36845:   "x * (y + z) = (x * y) + (x * z)"
hoelzl@36845:   "x ^ (Suc q) = x * (x ^ q)"
hoelzl@36845:   "x ^ (2*n) = (x ^ n) * (x ^ n)"
hoelzl@36845:   "x ^ (Suc (2*n)) = x * ((x ^ n) * (x ^ n))"
hoelzl@36845:   by (simp_all add: algebra_simps power_add power2_eq_square power_mult_distrib power_mult)
wenzelm@23252: 
haftmann@36871: lemmas normalizing_comm_semiring_1_axioms =
haftmann@36756:   comm_semiring_1_axioms [normalizer
haftmann@36872:     semiring ops: normalizing_semiring_ops
haftmann@36872:     semiring rules: normalizing_semiring_rules]
haftmann@36756: 
haftmann@36871: declaration
haftmann@36756:   {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_axioms} *}
wenzelm@23573: 
haftmann@36871: end
wenzelm@23252: 
haftmann@36871: context comm_ring_1
haftmann@36871: begin
haftmann@36871: 
haftmann@36872: lemma normalizing_ring_ops: shows "TERM (x- y)" and "TERM (- x)" .
haftmann@36871: 
haftmann@36872: lemma normalizing_ring_rules:
hoelzl@36845:   "- x = (- 1) * x"
hoelzl@36845:   "x - y = x + (- y)"
hoelzl@36845:   by (simp_all add: diff_minus)
wenzelm@23252: 
haftmann@36871: lemmas normalizing_comm_ring_1_axioms =
haftmann@36756:   comm_ring_1_axioms [normalizer
haftmann@36872:     semiring ops: normalizing_semiring_ops
haftmann@36872:     semiring rules: normalizing_semiring_rules
haftmann@36872:     ring ops: normalizing_ring_ops
haftmann@36872:     ring rules: normalizing_ring_rules]
chaieb@30866: 
haftmann@36871: declaration
haftmann@36756:   {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_ring_1_axioms} *}
chaieb@23327: 
haftmann@36871: end
haftmann@36871: 
haftmann@36873: context comm_semiring_1_cancel_crossproduct
haftmann@36871: begin
haftmann@36871: 
haftmann@36871: declare
haftmann@36756:   normalizing_comm_semiring_1_axioms [normalizer del]
wenzelm@23252: 
haftmann@36871: lemmas
haftmann@36873:   normalizing_comm_semiring_1_cancel_crossproduct_axioms =
haftmann@36873:   comm_semiring_1_cancel_crossproduct_axioms [normalizer
haftmann@36872:     semiring ops: normalizing_semiring_ops
haftmann@36872:     semiring rules: normalizing_semiring_rules
haftmann@36873:     idom rules: crossproduct_noteq add_scale_eq_noteq]
wenzelm@23252: 
haftmann@36871: declaration
haftmann@36873:   {* Semiring_Normalizer.semiring_funs @{thm normalizing_comm_semiring_1_cancel_crossproduct_axioms} *}
wenzelm@23252: 
haftmann@36871: end
wenzelm@23252: 
haftmann@36871: context idom
haftmann@36871: begin
haftmann@36871: 
haftmann@36871: declare normalizing_comm_ring_1_axioms [normalizer del]
haftmann@36871: 
haftmann@36871: lemmas normalizing_idom_axioms = idom_axioms [normalizer
haftmann@36872:   semiring ops: normalizing_semiring_ops
haftmann@36872:   semiring rules: normalizing_semiring_rules
haftmann@36872:   ring ops: normalizing_ring_ops
haftmann@36872:   ring rules: normalizing_ring_rules
haftmann@36873:   idom rules: crossproduct_noteq add_scale_eq_noteq
hoelzl@36845:   ideal rules: right_minus_eq add_0_iff]
wenzelm@23252: 
haftmann@36871: declaration
haftmann@36756:   {* Semiring_Normalizer.semiring_funs @{thm normalizing_idom_axioms} *}
wenzelm@23252: 
haftmann@36871: end
haftmann@36871: 
haftmann@36871: context field
haftmann@36871: begin
haftmann@36871: 
haftmann@36872: lemma normalizing_field_ops:
hoelzl@36845:   shows "TERM (x / y)" and "TERM (inverse x)" .
chaieb@23327: 
haftmann@36872: lemmas normalizing_field_rules = divide_inverse inverse_eq_divide
haftmann@28402: 
haftmann@36871: lemmas normalizing_field_axioms =
haftmann@36756:   field_axioms [normalizer
haftmann@36872:     semiring ops: normalizing_semiring_ops
haftmann@36872:     semiring rules: normalizing_semiring_rules
haftmann@36872:     ring ops: normalizing_ring_ops
haftmann@36872:     ring rules: normalizing_ring_rules
haftmann@36872:     field ops: normalizing_field_ops
haftmann@36872:     field rules: normalizing_field_rules
haftmann@36873:     idom rules: crossproduct_noteq add_scale_eq_noteq
hoelzl@36845:     ideal rules: right_minus_eq add_0_iff]
haftmann@36756: 
haftmann@36871: declaration
haftmann@36756:   {* Semiring_Normalizer.field_funs @{thm normalizing_field_axioms} *}
haftmann@28402: 
haftmann@36871: end
haftmann@36871: 
hoelzl@36845: hide_fact (open) normalizing_comm_semiring_1_axioms
haftmann@36873:   normalizing_comm_semiring_1_cancel_crossproduct_axioms normalizing_semiring_ops normalizing_semiring_rules
hoelzl@36845: 
hoelzl@36845: hide_fact (open) normalizing_comm_ring_1_axioms
haftmann@36872:   normalizing_idom_axioms normalizing_ring_ops normalizing_ring_rules
hoelzl@36845: 
haftmann@36872: hide_fact (open) normalizing_field_axioms normalizing_field_ops normalizing_field_rules
hoelzl@36845: 
haftmann@28402: end




