# HG changeset patch # User webertj # Date 1274607463 -3600 # Node ID 36c13099d10f7254af713d78a6073dbc289448fd # Parent 0010f08e288e8c39b8edf1e849f1c72db04e0c2a Refactoring, minor extensions (e.g., church_rosser). diff -r 0010f08e288e -r 36c13099d10f src/HOL/Library/Kleene_Algebra.thy --- a/src/HOL/Library/Kleene_Algebra.thy Fri May 14 14:14:22 2010 +0200 +++ b/src/HOL/Library/Kleene_Algebra.thy Sun May 23 10:37:43 2010 +0100 @@ -1,106 +1,164 @@ (* Title: HOL/Library/Kleene_Algebra.thy Author: Alexander Krauss, TU Muenchen + Author: Tjark Weber, University of Cambridge *) -header "Kleene Algebra" +header {* Kleene Algebra *} theory Kleene_Algebra imports Main begin -text {* WARNING: This is work in progress. Expect changes in the future *} +text {* WARNING: This is work in progress. Expect changes in the future. *} -text {* A type class of Kleene algebras *} +text {* Various lemmas correspond to entries in a database of theorems + about Kleene algebras and related structures maintained by Peter + H\"ofner: see + \url{http://www.informatik.uni-augsburg.de/~hoefnepe/kleene_db/lemmas/index.html}. *} + +subsection {* Preliminaries *} -class star = - fixes star :: "'a \ 'a" +text {* A class where addition is idempotent. *} -class idem_add = ab_semigroup_add + +class idem_add = plus + assumes add_idem [simp]: "x + x = x" + +text {* A class of idempotent abelian semigroups (written additively). *} + +class idem_ab_semigroup_add = ab_semigroup_add + idem_add begin -lemma add_idem2[simp]: "(x::'a) + (x + y) = x + y" +lemma add_idem2 [simp]: "x + (x + y) = x + y" unfolding add_assoc[symmetric] by simp +lemma add_idem3 [simp]: "x + (y + x) = x + y" +by (simp add: add_commute) + end -class order_by_add = idem_add + ord + - assumes order_def: "a \ b \ a + b = b" - assumes strict_order_def: "a < b \ a \ b \ \ b \ a" +text {* A class where order is defined in terms of addition. *} + +class order_by_add = plus + ord + + assumes order_def: "x \ y \ x + y = y" + assumes strict_order_def: "x < y \ x \ y \ \ y \ x" begin -lemma ord_simp1[simp]: "x \ y \ x + y = y" +lemma ord_simp [simp]: "x \ y \ x + y = y" unfolding order_def . -lemma ord_simp2[simp]: "x \ y \ y + x = y" - unfolding order_def add_commute . - lemma ord_intro: "x + y = y \ x \ y" unfolding order_def . +end + +text {* A class of idempotent abelian semigroups (written additively) + where order is defined in terms of addition. *} + +class ordered_idem_ab_semigroup_add = idem_ab_semigroup_add + order_by_add +begin + +lemma ord_simp2 [simp]: "x \ y \ y + x = y" + unfolding order_def add_commute . + subclass order proof fix x y z :: 'a - show "x \ x" unfolding order_def by simp + show "x \ x" + unfolding order_def by simp show "x \ y \ y \ z \ x \ z" - proof (rule ord_intro) - assume "x \ y" "y \ z" - have "x + z = x + y + z" by (simp add:`y \ z` add_assoc) - also have "\ = y + z" by (simp add:`x \ y`) - also have "\ = z" by (simp add:`y \ z`) - finally show "x + z = z" . - qed - show "x \ y \ y \ x \ x = y" unfolding order_def - by (simp add: add_commute) - show "x < y \ x \ y \ \ y \ x" by (fact strict_order_def) + unfolding order_def by (metis add_assoc) + show "x \ y \ y \ x \ x = y" + unfolding order_def by (simp add: add_commute) + show "x < y \ x \ y \ \ y \ x" + by (fact strict_order_def) qed -lemma plus_leI: +subclass ordered_ab_semigroup_add proof + fix a b c :: 'a + assume "a \ b" show "c + a \ c + b" + proof (rule ord_intro) + have "c + a + (c + b) = a + b + c" by (simp add: add_ac) + also have "\ = c + b" by (simp add: `a \ b` add_ac) + finally show "c + a + (c + b) = c + b" . + qed +qed + +lemma plus_leI [simp]: "x \ z \ y \ z \ x + y \ z" unfolding order_def by (simp add: add_assoc) -lemma less_add[simp]: "a \ a + b" "b \ a + b" -unfolding order_def by (auto simp:add_ac) +lemma less_add [simp]: "x \ x + y" "y \ x + y" +unfolding order_def by (auto simp: add_ac) -lemma add_est1: "a + b \ c \ a \ c" +lemma add_est1 [elim]: "x + y \ z \ x \ z" using less_add(1) by (rule order_trans) -lemma add_est2: "a + b \ c \ b \ c" +lemma add_est2 [elim]: "x + y \ z \ y \ z" using less_add(2) by (rule order_trans) +lemma add_supremum: "(x + y \ z) = (x \ z \ y \ z)" +by auto + end -class pre_kleene = semiring_1 + order_by_add +text {* A class of commutative monoids (written additively) where + order is defined in terms of addition. *} + +class ordered_comm_monoid_add = comm_monoid_add + order_by_add +begin + +lemma zero_minimum [simp]: "0 \ x" +unfolding order_def by simp + +end + +text {* A class of idempotent commutative monoids (written additively) + where order is defined in terms of addition. *} + +class ordered_idem_comm_monoid_add = ordered_comm_monoid_add + idem_add begin +subclass ordered_idem_ab_semigroup_add .. + +lemma sum_is_zero: "(x + y = 0) = (x = 0 \ y = 0)" +by (simp add: add_supremum eq_iff) + +end + +section {* Kleene Algebras *} + +text {* Class @{text pre_kleene} provides all operations of Kleene + algebras except for the Kleene star. *} + +class pre_kleene = semiring_1 + idem_add + order_by_add +begin + +subclass ordered_idem_comm_monoid_add .. + subclass ordered_semiring proof - fix x y z :: 'a + fix a b c :: 'a + assume "a \ b" - assume "x \ y" - - show "z + x \ z + y" + show "c * a \ c * b" proof (rule ord_intro) - have "z + x + (z + y) = x + y + z" by (simp add:add_ac) - also have "\ = z + y" by (simp add:`x \ y` add_ac) - finally show "z + x + (z + y) = z + y" . + from `a \ b` have "c * (a + b) = c * b" by simp + thus "c * a + c * b = c * b" by (simp add: right_distrib) qed - show "z * x \ z * y" + show "a * c \ b * c" proof (rule ord_intro) - from `x \ y` have "z * (x + y) = z * y" by simp - thus "z * x + z * y = z * y" by (simp add:right_distrib) - qed - - show "x * z \ y * z" - proof (rule ord_intro) - from `x \ y` have "(x + y) * z = y * z" by simp - thus "x * z + y * z = y * z" by (simp add:left_distrib) + from `a \ b` have "(a + b) * c = b * c" by simp + thus "a * c + b * c = b * c" by (simp add: left_distrib) qed qed -lemma zero_minimum [simp]: "0 \ x" - unfolding order_def by simp +end + +text {* A class that provides a star operator. *} -end +class star = + fixes star :: "'a \ 'a" + +text {* Finally, a class of Kleene algebras. *} class kleene = pre_kleene + star + assumes star1: "1 + a * star a \ star a" @@ -109,38 +167,20 @@ and star4: "x * a \ x \ x * star a \ x" begin -lemma star3': +lemma star3' [simp]: assumes a: "b + a * x \ x" shows "star a * b \ x" -proof (rule order_trans) - from a have "b \ x" by (rule add_est1) - show "star a * b \ star a * x" - by (rule mult_mono) (auto simp:`b \ x`) +by (metis assms less_add mult_left_mono order_trans star3 zero_minimum) - from a have "a * x \ x" by (rule add_est2) - with star3 show "star a * x \ x" . -qed - -lemma star4': +lemma star4' [simp]: assumes a: "b + x * a \ x" shows "b * star a \ x" -proof (rule order_trans) - from a have "b \ x" by (rule add_est1) - show "b * star a \ x * star a" - by (rule mult_mono) (auto simp:`b \ x`) +by (metis assms less_add mult_right_mono order_trans star4 zero_minimum) - from a have "x * a \ x" by (rule add_est2) - with star4 show "x * star a \ x" . -qed - -lemma star_unfold_left: - shows "1 + a * star a = star a" +lemma star_unfold_left: "1 + a * star a = star a" proof (rule antisym, rule star1) have "1 + a * (1 + a * star a) \ 1 + a * star a" - apply (rule add_mono, rule) - apply (rule mult_mono, auto) - apply (rule star1) - done + by (metis add_left_mono mult_left_mono star1 zero_minimum) with star3' have "star a * 1 \ 1 + a * star a" . thus "star a \ 1 + a * star a" by simp qed @@ -148,76 +188,66 @@ lemma star_unfold_right: "1 + star a * a = star a" proof (rule antisym, rule star2) have "1 + (1 + star a * a) * a \ 1 + star a * a" - apply (rule add_mono, rule) - apply (rule mult_mono, auto) - apply (rule star2) - done + by (metis add_left_mono mult_right_mono star2 zero_minimum) with star4' have "1 * star a \ 1 + star a * a" . thus "star a \ 1 + star a * a" by simp qed -lemma star_zero[simp]: "star 0 = 1" +lemma star_zero [simp]: "star 0 = 1" by (fact star_unfold_left[of 0, simplified, symmetric]) -lemma star_one[simp]: "star 1 = 1" +lemma star_one [simp]: "star 1 = 1" by (metis add_idem2 eq_iff mult_1_right ord_simp2 star3 star_unfold_left) -lemma one_less_star: "1 \ star x" +lemma one_less_star [simp]: "1 \ star x" by (metis less_add(1) star_unfold_left) -lemma ka1: "x * star x \ star x" +lemma ka1 [simp]: "x * star x \ star x" by (metis less_add(2) star_unfold_left) -lemma star_mult_idem[simp]: "star x * star x = star x" +lemma star_mult_idem [simp]: "star x * star x = star x" by (metis add_commute add_est1 eq_iff mult_1_right right_distrib star3 star_unfold_left) -lemma less_star: "x \ star x" +lemma less_star [simp]: "x \ star x" by (metis less_add(2) mult_1_right mult_left_mono one_less_star order_trans star_unfold_left zero_minimum) -lemma star_simulation: +lemma star_simulation_leq_1: + assumes a: "a * x \ x * b" + shows "star a * x \ x * star b" +proof (rule star3', rule order_trans) + from a have "a * x \ x * b" by simp + hence "a * x * star b \ x * b * star b" + by (rule mult_right_mono) simp + thus "x + a * (x * star b) \ x + x * b * star b" + using add_left_mono by (auto simp: mult_assoc) + show "\ \ x * star b" + by (metis add_supremum ka1 mult.right_neutral mult_assoc mult_left_mono one_less_star zero_minimum) +qed + +lemma star_simulation_leq_2: + assumes a: "x * a \ b * x" + shows "x * star a \ star b * x" +proof (rule star4', rule order_trans) + have "star b * x * a \ star b * b * x" + by (metis assms mult_assoc mult_mono order_refl zero_minimum) + thus "x + star b * x * a \ x + star b * b * x" + using add_mono by auto + show "\ \ star b * x" + by (metis add_supremum left_distrib less_add mult.left_neutral mult_assoc mult_right_mono star_unfold_left star_unfold_right zero_minimum) +qed + +lemma star_simulation [simp]: assumes a: "a * x = x * b" shows "star a * x = x * star b" -proof (rule antisym) - show "star a * x \ x * star b" - proof (rule star3', rule order_trans) - from a have "a * x \ x * b" by simp - hence "a * x * star b \ x * b * star b" - by (rule mult_mono) auto - thus "x + a * (x * star b) \ x + x * b * star b" - using add_mono by (auto simp: mult_assoc) - show "\ \ x * star b" - proof - - have "x * (1 + b * star b) \ x * star b" - by (rule mult_mono[OF _ star1]) auto - thus ?thesis - by (simp add:right_distrib mult_assoc) - qed - qed - show "x * star b \ star a * x" - proof (rule star4', rule order_trans) - from a have b: "x * b \ a * x" by simp - have "star a * x * b \ star a * a * x" - unfolding mult_assoc - by (rule mult_mono[OF _ b]) auto - thus "x + star a * x * b \ x + star a * a * x" - using add_mono by auto - show "\ \ star a * x" - proof - - have "(1 + star a * a) * x \ star a * x" - by (rule mult_mono[OF star2]) auto - thus ?thesis - by (simp add:left_distrib mult_assoc) - qed - qed -qed +by (metis antisym assms order_refl star_simulation_leq_1 star_simulation_leq_2) -lemma star_slide2[simp]: "star x * x = x * star x" +lemma star_slide2 [simp]: "star x * x = x * star x" by (metis star_simulation) -lemma star_idemp[simp]: "star (star x) = star x" +lemma star_idemp [simp]: "star (star x) = star x" by (metis add_idem2 eq_iff less_star mult_1_right star3' star_mult_idem star_unfold_left) -lemma star_slide[simp]: "star (x * y) * x = x * star (y * x)" +lemma star_slide [simp]: "star (x * y) * x = x * star (y * x)" by (auto simp: mult_assoc star_simulation) lemma star_one': @@ -234,26 +264,22 @@ finally show ?thesis . qed -lemma x_less_star[simp]: "x \ x * star a" -proof - - have "x \ x * (1 + a * star a)" by (simp add: right_distrib) - also have "\ = x * star a" by (simp only: star_unfold_left) - finally show ?thesis . -qed +lemma x_less_star [simp]: "x \ x * star a" +by (metis mult.right_neutral mult_left_mono one_less_star zero_minimum) -lemma star_mono: "x \ y \ star x \ star y" +lemma star_mono [simp]: "x \ y \ star x \ star y" by (metis add_commute eq_iff less_star ord_simp2 order_trans star3 star4' star_idemp star_mult_idem x_less_star) lemma star_sub: "x \ 1 \ star x = 1" -by (metis add_commute ord_simp1 star_idemp star_mono star_mult_idem star_one star_unfold_left) +by (metis add_commute ord_simp star_idemp star_mono star_mult_idem star_one star_unfold_left) lemma star_unfold2: "star x * y = y + x * star x * y" by (subst star_unfold_right[symmetric]) (simp add: mult_assoc left_distrib) -lemma star_absorb_one[simp]: "star (x + 1) = star x" -by (metis add_commute eq_iff left_distrib less_add(1) less_add(2) mult_1_left mult_assoc star3 star_mono star_mult_idem star_unfold2 x_less_star) +lemma star_absorb_one [simp]: "star (x + 1) = star x" +by (metis add_commute eq_iff left_distrib less_add mult_1_left mult_assoc star3 star_mono star_mult_idem star_unfold2 x_less_star) -lemma star_absorb_one'[simp]: "star (1 + x) = star x" +lemma star_absorb_one' [simp]: "star (1 + x) = star x" by (subst add_commute) (fact star_absorb_one) lemma ka16: "(y * star x) * star (y * star x) \ star x * star (y * star x)" @@ -269,21 +295,17 @@ \ star x * star (y * star x)" by (metis ka16 ka17 left_distrib mult_assoc plus_leI) -lemma kleene_church_rosser: - "star y * star x \ star x * star y \ star (x + y) \ star x * star y" -oops - -lemma star_decomp: "star (a + b) = star a * star (b * star a)" +lemma star_decomp: "star (x + y) = star x * star (y * star x)" proof (rule antisym) - have "1 + (a + b) * star a * star (b * star a) \ - 1 + a * star a * star (b * star a) + b * star a * star (b * star a)" + have "1 + (x + y) * star x * star (y * star x) \ + 1 + x * star x * star (y * star x) + y * star x * star (y * star x)" by (metis add_commute add_left_commute eq_iff left_distrib mult_assoc) - also have "\ \ star a * star (b * star a)" + also have "\ \ star x * star (y * star x)" by (metis add_commute add_est1 add_left_commute ka18 plus_leI star_unfold_left x_less_star) - finally show "star (a + b) \ star a * star (b * star a)" + finally show "star (x + y) \ star x * star (y * star x)" by (metis mult_1_right mult_assoc star3') next - show "star a * star (b * star a) \ star (a + b)" + show "star x * star (y * star x) \ star (x + y)" by (metis add_assoc add_est1 add_est2 add_left_commute less_star mult_mono' star_absorb_one star_absorb_one' star_idemp star_mono star_mult_idem zero_minimum) qed @@ -298,14 +320,40 @@ by (metis add_est1 add_est2 less_add(1) mult_assoc order_def plus_leI star_absorb_one star_mono star_slide2 star_unfold2 star_unfold_left x_less_star) lemma ka25: "star y * star x \ star x * star y \ star (star y * star x) \ star x * star y" -oops +-- {* Takes several minutes on my computer. *} +by (metis mult_assoc mult_right_mono order_trans star_idemp star_mult_idem star_simulation_leq_2 star_slide x_less_star zero_minimum) + +lemma church_rosser: + "star y * star x \ star x * star y \ star (x + y) \ star x * star y" +by (metis add_commute ka24 ka25 order_trans) lemma kleene_bubblesort: "y * x \ x * y \ star (x + y) \ star x * star y" -oops +by (metis church_rosser star_simulation_leq_1 star_simulation_leq_2) + +lemma ka27: "star (x + star y) = star (x + y)" +by (metis add_commute star_decomp star_idemp) + +lemma ka28: "star (star x + star y) = star (x + y)" +by (metis add_commute ka27) + +lemma ka29: "(y * (1 + x) \ (1 + x) * star y) = (y * x \ (1 + x) * star y)" +by (metis add_supremum left_distrib less_add(1) less_star mult.left_neutral mult.right_neutral order_trans right_distrib) + +lemma ka30: "star x * star y \ star (x + y)" +by (metis mult_left_mono star_decomp star_mono x_less_star zero_minimum) + +lemma simple_simulation: "x * y = 0 \ star x * y = y" +by (metis mult.right_neutral mult_zero_right star_simulation star_zero) + +lemma ka32: "star (x * y) = 1 + x * star (y * x) * y" +by (metis mult_assoc star_slide star_unfold_left) + +lemma ka33: "x * y + 1 \ y \ star x \ y" +by (metis add_commute mult.right_neutral star3') end -subsection {* Complete lattices are Kleene algebras *} +subsection {* Complete Lattices are Kleene Algebras *} lemma (in complete_lattice) le_SUPI': assumes "l \ M i" @@ -325,7 +373,7 @@ unfolding star_cont[of 1 a 1, simplified] by (subst power_0[symmetric]) (rule le_SUPI [OF UNIV_I]) - show "1 + a * star a \ star a" + show "1 + a * star a \ star a" apply (rule plus_leI, simp) apply (simp add:star_cont[of a a 1, simplified]) apply (simp add:star_cont[of 1 a 1, simplified]) @@ -411,38 +459,26 @@ end - subsection {* Transitive Closure *} context kleene begin -definition - tcl_def: "tcl x = star x * x" +definition + tcl_def: "tcl x = star x * x" lemma tcl_zero: "tcl 0 = 0" unfolding tcl_def by simp lemma tcl_unfold_right: "tcl a = a + tcl a * a" -proof - - from star_unfold_right[of a] - have "a * (1 + star a * a) = a * star a" by simp - from this[simplified right_distrib, simplified] - show ?thesis - by (simp add:tcl_def mult_assoc) -qed +by (metis star_slide2 star_unfold2 tcl_def) lemma less_tcl: "a \ tcl a" -proof - - have "a \ a + tcl a * a" by simp - also have "\ = tcl a" by (rule tcl_unfold_right[symmetric]) - finally show ?thesis . -qed +by (metis star_slide2 tcl_def x_less_star) end - -subsection {* Naive Algorithm to generate the transitive closure *} +subsection {* A Naive Algorithm to Generate the Transitive Closure *} function (default "\x. 0", tailrec, domintros) mk_tcl :: "('a::{plus,times,ord,zero}) \ 'a \ 'a" @@ -461,19 +497,11 @@ context kleene begin -lemma mk_tcl_lemma1: - "(X + X * A) * star A = X * star A" -proof - - have "A * star A \ 1 + A * star A" by simp - also have "\ = star A" by (simp add:star_unfold_left) - finally have "star A + A * star A = star A" by simp - hence "X * (star A + A * star A) = X * star A" by simp - thus ?thesis by (simp add:left_distrib right_distrib mult_assoc) -qed +lemma mk_tcl_lemma1: "(X + X * A) * star A = X * star A" +by (metis ka1 left_distrib mult_assoc mult_left_mono ord_simp2 zero_minimum) -lemma mk_tcl_lemma2: - shows "X * A \ X \ X * star A = X" - by (rule antisym) (auto simp:star4) +lemma mk_tcl_lemma2: "X * A \ X \ X * star A = X" +by (rule antisym) (auto simp: star4) end @@ -484,7 +512,6 @@ using assms by induct (auto simp: mk_tcl_lemma1 mk_tcl_lemma2) - lemma graph_implies_dom: "mk_tcl_graph x y \ mk_tcl_dom x" by (rule mk_tcl_graph.induct) (auto intro:accp.accI elim:mk_tcl_rel.cases) @@ -492,9 +519,8 @@ unfolding mk_tcl_def by (rule fundef_default_value[OF mk_tcl_sumC_def graph_implies_dom]) - text {* We can replace the dom-Condition of the correctness theorem - with something executable *} + with something executable: *} lemma mk_tcl_correctness2: fixes A X :: "'a :: {kleene}"