src/HOL/Library/Kleene_Algebra.thy
author bulwahn
Tue Apr 05 09:38:28 2011 +0200 (2011-04-05)
changeset 42231 bc1891226d00
parent 39749 fa94799e3a3b
child 44918 6a80fbc4e72c
permissions -rw-r--r--
removing bounded_forall code equation for characters when loading Code_Char
     1 (*  Title:      HOL/Library/Kleene_Algebra.thy
     2     Author:     Alexander Krauss, TU Muenchen
     3     Author:     Tjark Weber, University of Cambridge
     4 *)
     5 
     6 header {* Kleene Algebras *}
     7 
     8 theory Kleene_Algebra
     9 imports Main 
    10 begin
    11 
    12 text {* WARNING: This is work in progress. Expect changes in the future. *}
    13 
    14 text {* Various lemmas correspond to entries in a database of theorems
    15   about Kleene algebras and related structures maintained by Peter
    16   H\"ofner: see
    17   \url{http://www.informatik.uni-augsburg.de/~hoefnepe/kleene_db/lemmas/index.html}. *}
    18 
    19 subsection {* Preliminaries *}
    20 
    21 text {* A class where addition is idempotent. *}
    22 
    23 class idem_add = plus +
    24   assumes add_idem [simp]: "x + x = x"
    25 
    26 text {* A class of idempotent abelian semigroups (written additively). *}
    27 
    28 class idem_ab_semigroup_add = ab_semigroup_add + idem_add
    29 begin
    30 
    31 lemma add_idem2 [simp]: "x + (x + y) = x + y"
    32 unfolding add_assoc[symmetric] by simp
    33 
    34 lemma add_idem3 [simp]: "x + (y + x) = x + y"
    35 by (simp add: add_commute)
    36 
    37 end
    38 
    39 text {* A class where order is defined in terms of addition. *}
    40 
    41 class order_by_add = plus + ord +
    42   assumes order_def: "x \<le> y \<longleftrightarrow> x + y = y"
    43   assumes strict_order_def: "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
    44 begin
    45 
    46 lemma ord_simp [simp]: "x \<le> y \<Longrightarrow> x + y = y"
    47   unfolding order_def .
    48 
    49 lemma ord_intro: "x + y = y \<Longrightarrow> x \<le> y"
    50   unfolding order_def .
    51 
    52 end
    53 
    54 text {* A class of idempotent abelian semigroups (written additively)
    55   where order is defined in terms of addition. *}
    56 
    57 class ordered_idem_ab_semigroup_add = idem_ab_semigroup_add + order_by_add
    58 begin
    59 
    60 lemma ord_simp2 [simp]: "x \<le> y \<Longrightarrow> y + x = y"
    61   unfolding order_def add_commute .
    62 
    63 subclass order proof
    64   fix x y z :: 'a
    65   show "x \<le> x"
    66     unfolding order_def by simp
    67   show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
    68     unfolding order_def by (metis add_assoc)
    69   show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
    70     unfolding order_def by (simp add: add_commute)
    71   show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
    72     by (fact strict_order_def)
    73 qed
    74 
    75 subclass ordered_ab_semigroup_add proof
    76   fix a b c :: 'a
    77   assume "a \<le> b" show "c + a \<le> c + b"
    78   proof (rule ord_intro)
    79     have "c + a + (c + b) = a + b + c" by (simp add: add_ac)
    80     also have "\<dots> = c + b" by (simp add: `a \<le> b` add_ac)
    81     finally show "c + a + (c + b) = c + b" .
    82   qed
    83 qed
    84 
    85 lemma plus_leI [simp]: 
    86   "x \<le> z \<Longrightarrow> y \<le> z \<Longrightarrow> x + y \<le> z"
    87   unfolding order_def by (simp add: add_assoc)
    88 
    89 lemma less_add [simp]: "x \<le> x + y" "y \<le> x + y"
    90 unfolding order_def by (auto simp: add_ac)
    91 
    92 lemma add_est1 [elim]: "x + y \<le> z \<Longrightarrow> x \<le> z"
    93 using less_add(1) by (rule order_trans)
    94 
    95 lemma add_est2 [elim]: "x + y \<le> z \<Longrightarrow> y \<le> z"
    96 using less_add(2) by (rule order_trans)
    97 
    98 lemma add_supremum: "(x + y \<le> z) = (x \<le> z \<and> y \<le> z)"
    99 by auto
   100 
   101 end
   102 
   103 text {* A class of commutative monoids (written additively) where
   104   order is defined in terms of addition. *}
   105 
   106 class ordered_comm_monoid_add = comm_monoid_add + order_by_add
   107 begin
   108 
   109 lemma zero_minimum [simp]: "0 \<le> x"
   110 unfolding order_def by simp
   111 
   112 end
   113 
   114 text {* A class of idempotent commutative monoids (written additively)
   115   where order is defined in terms of addition. *}
   116 
   117 class ordered_idem_comm_monoid_add = ordered_comm_monoid_add + idem_add
   118 begin
   119 
   120 subclass ordered_idem_ab_semigroup_add ..
   121 
   122 lemma sum_is_zero: "(x + y = 0) = (x = 0 \<and> y = 0)"
   123 by (simp add: add_supremum eq_iff)
   124 
   125 end
   126 
   127 subsection {* A class of Kleene algebras *}
   128 
   129 text {* Class @{text pre_kleene} provides all operations of Kleene
   130   algebras except for the Kleene star. *}
   131 
   132 class pre_kleene = semiring_1 + idem_add + order_by_add
   133 begin
   134 
   135 subclass ordered_idem_comm_monoid_add ..
   136 
   137 subclass ordered_semiring proof
   138   fix a b c :: 'a
   139   assume "a \<le> b"
   140 
   141   show "c * a \<le> c * b"
   142   proof (rule ord_intro)
   143     from `a \<le> b` have "c * (a + b) = c * b" by simp
   144     thus "c * a + c * b = c * b" by (simp add: right_distrib)
   145   qed
   146 
   147   show "a * c \<le> b * c"
   148   proof (rule ord_intro)
   149     from `a \<le> b` have "(a + b) * c = b * c" by simp
   150     thus "a * c + b * c = b * c" by (simp add: left_distrib)
   151   qed
   152 qed
   153 
   154 end
   155 
   156 text {* A class that provides a star operator. *}
   157 
   158 class star =
   159   fixes star :: "'a \<Rightarrow> 'a"
   160 
   161 text {* Finally, a class of Kleene algebras. *}
   162 
   163 class kleene = pre_kleene + star +
   164   assumes star1: "1 + a * star a \<le> star a"
   165   and star2: "1 + star a * a \<le> star a"
   166   and star3: "a * x \<le> x \<Longrightarrow> star a * x \<le> x"
   167   and star4: "x * a \<le> x \<Longrightarrow> x * star a \<le> x"
   168 begin
   169 
   170 lemma star3' [simp]:
   171   assumes a: "b + a * x \<le> x"
   172   shows "star a * b \<le> x"
   173 by (metis assms less_add mult_left_mono order_trans star3 zero_minimum)
   174 
   175 lemma star4' [simp]:
   176   assumes a: "b + x * a \<le> x"
   177   shows "b * star a \<le> x"
   178 by (metis assms less_add mult_right_mono order_trans star4 zero_minimum)
   179 
   180 lemma star_unfold_left: "1 + a * star a = star a"
   181 proof (rule antisym, rule star1)
   182   have "1 + a * (1 + a * star a) \<le> 1 + a * star a"
   183     by (metis add_left_mono mult_left_mono star1 zero_minimum)
   184   with star3' have "star a * 1 \<le> 1 + a * star a" .
   185   thus "star a \<le> 1 + a * star a" by simp
   186 qed
   187 
   188 lemma star_unfold_right: "1 + star a * a = star a"
   189 proof (rule antisym, rule star2)
   190   have "1 + (1 + star a * a) * a \<le> 1 + star a * a"
   191     by (metis add_left_mono mult_right_mono star2 zero_minimum)
   192   with star4' have "1 * star a \<le> 1 + star a * a" .
   193   thus "star a \<le> 1 + star a * a" by simp
   194 qed
   195 
   196 lemma star_zero [simp]: "star 0 = 1"
   197 by (fact star_unfold_left[of 0, simplified, symmetric])
   198 
   199 lemma star_one [simp]: "star 1 = 1"
   200 by (metis add_idem2 eq_iff mult_1_right ord_simp2 star3 star_unfold_left)
   201 
   202 lemma one_less_star [simp]: "1 \<le> star x"
   203 by (metis less_add(1) star_unfold_left)
   204 
   205 lemma ka1 [simp]: "x * star x \<le> star x"
   206 by (metis less_add(2) star_unfold_left)
   207 
   208 lemma star_mult_idem [simp]: "star x * star x = star x"
   209 by (metis add_commute add_est1 eq_iff mult_1_right right_distrib star3 star_unfold_left)
   210 
   211 lemma less_star [simp]: "x \<le> star x"
   212 by (metis less_add(2) mult_1_right mult_left_mono one_less_star order_trans star_unfold_left zero_minimum)
   213 
   214 lemma star_simulation_leq_1:
   215   assumes a: "a * x \<le> x * b"
   216   shows "star a * x \<le> x * star b"
   217 proof (rule star3', rule order_trans)
   218   from a have "a * x * star b \<le> x * b * star b"
   219     by (rule mult_right_mono) simp
   220   thus "x + a * (x * star b) \<le> x + x * b * star b"
   221     using add_left_mono by (auto simp: mult_assoc)
   222   show "\<dots> \<le> x * star b"
   223     by (metis add_supremum ka1 mult.right_neutral mult_assoc mult_left_mono one_less_star zero_minimum)
   224 qed
   225 
   226 lemma star_simulation_leq_2:
   227   assumes a: "x * a \<le> b * x"
   228   shows "x * star a \<le> star b * x"
   229 proof (rule star4', rule order_trans)
   230   from a have "star b * x * a \<le> star b * b * x"
   231     by (metis mult_assoc mult_left_mono zero_minimum)
   232   thus "x + star b * x * a \<le> x + star b * b * x"
   233     using add_mono by auto
   234   show "\<dots> \<le> star b * x"
   235     by (metis add_supremum left_distrib less_add mult.left_neutral mult_assoc mult_right_mono star_unfold_right zero_minimum)
   236 qed
   237 
   238 lemma star_simulation [simp]:
   239   assumes a: "a * x = x * b"
   240   shows "star a * x = x * star b"
   241 by (metis antisym assms order_refl star_simulation_leq_1 star_simulation_leq_2)
   242 
   243 lemma star_slide2 [simp]: "star x * x = x * star x"
   244 by (metis star_simulation)
   245 
   246 lemma star_idemp [simp]: "star (star x) = star x"
   247 by (metis add_idem2 eq_iff less_star mult_1_right star3' star_mult_idem star_unfold_left)
   248 
   249 lemma star_slide [simp]: "star (x * y) * x = x * star (y * x)"
   250 by (metis mult_assoc star_simulation)
   251 
   252 lemma star_one':
   253   assumes "p * p' = 1" "p' * p = 1"
   254   shows "p' * star a * p = star (p' * a * p)"
   255 proof -
   256   from assms
   257   have "p' * star a * p = p' * star (p * p' * a) * p"
   258     by simp
   259   also have "\<dots> = p' * p * star (p' * a * p)"
   260     by (simp add: mult_assoc)
   261   also have "\<dots> = star (p' * a * p)"
   262     by (simp add: assms)
   263   finally show ?thesis .
   264 qed
   265 
   266 lemma x_less_star [simp]: "x \<le> x * star a"
   267 by (metis mult.right_neutral mult_left_mono one_less_star zero_minimum)
   268 
   269 lemma star_mono [simp]: "x \<le> y \<Longrightarrow> star x \<le> star y"
   270 by (metis add_commute eq_iff less_star ord_simp2 order_trans star3 star4' star_idemp star_mult_idem x_less_star)
   271 
   272 lemma star_sub: "x \<le> 1 \<Longrightarrow> star x = 1"
   273 by (metis add_commute ord_simp star_idemp star_mono star_mult_idem star_one star_unfold_left)
   274 
   275 lemma star_unfold2: "star x * y = y + x * star x * y"
   276 by (subst star_unfold_right[symmetric]) (simp add: mult_assoc left_distrib)
   277 
   278 lemma star_absorb_one [simp]: "star (x + 1) = star x"
   279 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)
   280 
   281 lemma star_absorb_one' [simp]: "star (1 + x) = star x"
   282 by (subst add_commute) (fact star_absorb_one)
   283 
   284 lemma ka16: "(y * star x) * star (y * star x) \<le> star x * star (y * star x)"
   285 by (metis ka1 less_add(1) mult_assoc order_trans star_unfold2)
   286 
   287 lemma ka16': "(star x * y) * star (star x * y) \<le> star (star x * y) * star x"
   288 by (metis ka1 mult_assoc order_trans star_slide x_less_star)
   289 
   290 lemma ka17: "(x * star x) * star (y * star x) \<le> star x * star (y * star x)"
   291 by (metis ka1 mult_assoc mult_right_mono zero_minimum)
   292 
   293 lemma ka18: "(x * star x) * star (y * star x) + (y * star x) * star (y * star x)
   294   \<le> star x * star (y * star x)"
   295 by (metis ka16 ka17 left_distrib mult_assoc plus_leI)
   296 
   297 lemma star_decomp: "star (x + y) = star x * star (y * star x)"
   298 proof (rule antisym)
   299   have "1 + (x + y) * star x * star (y * star x) \<le>
   300     1 + x * star x * star (y * star x) + y * star x * star (y * star x)"
   301     by (metis add_commute add_left_commute eq_iff left_distrib mult_assoc)
   302   also have "\<dots> \<le> star x * star (y * star x)"
   303     by (metis add_commute add_est1 add_left_commute ka18 plus_leI star_unfold_left x_less_star)
   304   finally show "star (x + y) \<le> star x * star (y * star x)"
   305     by (metis mult_1_right mult_assoc star3')
   306 next
   307   show "star x * star (y * star x) \<le> star (x + y)"
   308     by (metis add_assoc add_est1 add_est2 add_left_commute less_star mult_mono'
   309       star_absorb_one star_absorb_one' star_idemp star_mono star_mult_idem zero_minimum)
   310 qed
   311 
   312 lemma ka22: "y * star x \<le> star x * star y \<Longrightarrow>  star y * star x \<le> star x * star y"
   313 by (metis mult_assoc mult_right_mono plus_leI star3' star_mult_idem x_less_star zero_minimum)
   314 
   315 lemma ka23: "star y * star x \<le> star x * star y \<Longrightarrow> y * star x \<le> star x * star y"
   316 by (metis less_star mult_right_mono order_trans zero_minimum)
   317 
   318 lemma ka24: "star (x + y) \<le> star (star x * star y)"
   319 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)
   320 
   321 lemma ka25: "star y * star x \<le> star x * star y \<Longrightarrow> star (star y * star x) \<le> star x * star y"
   322 proof -
   323   assume "star y * star x \<le> star x * star y"
   324   hence "\<forall>x\<^isub>1. star y * (star x * x\<^isub>1) \<le> star x * (star y * x\<^isub>1)" by (metis mult_assoc mult_right_mono zero_minimum)
   325   hence "star y * (star x * star y) \<le> star x * star y" by (metis star_mult_idem)
   326   hence "\<exists>x\<^isub>1. star (star y * star x) * star x\<^isub>1 \<le> star x * star y" by (metis star_decomp star_idemp star_simulation_leq_2 star_slide)
   327   hence "\<exists>x\<^isub>1\<ge>star (star y * star x). x\<^isub>1 \<le> star x * star y" by (metis x_less_star)
   328   thus "star (star y * star x) \<le> star x * star y" by (metis order_trans)
   329 qed
   330 
   331 lemma church_rosser: 
   332   "star y * star x \<le> star x * star y \<Longrightarrow> star (x + y) \<le> star x * star y"
   333 by (metis add_commute ka24 ka25 order_trans)
   334 
   335 lemma kleene_bubblesort: "y * x \<le> x * y \<Longrightarrow> star (x + y) \<le> star x * star y"
   336 by (metis church_rosser star_simulation_leq_1 star_simulation_leq_2)
   337 
   338 lemma ka27: "star (x + star y) = star (x + y)"
   339 by (metis add_commute star_decomp star_idemp)
   340 
   341 lemma ka28: "star (star x + star y) = star (x + y)"
   342 by (metis add_commute ka27)
   343 
   344 lemma ka29: "(y * (1 + x) \<le> (1 + x) * star y) = (y * x \<le> (1 + x) * star y)"
   345 by (metis add_supremum left_distrib less_add(1) less_star mult.left_neutral mult.right_neutral order_trans right_distrib)
   346 
   347 lemma ka30: "star x * star y \<le> star (x + y)"
   348 by (metis mult_left_mono star_decomp star_mono x_less_star zero_minimum)
   349 
   350 lemma simple_simulation: "x * y = 0 \<Longrightarrow> star x * y = y"
   351 by (metis mult.right_neutral mult_zero_right star_simulation star_zero)
   352 
   353 lemma ka32: "star (x * y) = 1 + x * star (y * x) * y"
   354 by (metis mult_assoc star_slide star_unfold_left)
   355 
   356 lemma ka33: "x * y + 1 \<le> y \<Longrightarrow> star x \<le> y"
   357 by (metis add_commute mult.right_neutral star3')
   358 
   359 end
   360 
   361 subsection {* Complete lattices are Kleene algebras *}
   362 
   363 lemma (in complete_lattice) le_SUPI':
   364   assumes "l \<le> M i"
   365   shows "l \<le> (SUP i. M i)"
   366   using assms by (rule order_trans) (rule le_SUPI [OF UNIV_I])
   367 
   368 class kleene_by_complete_lattice = pre_kleene
   369   + complete_lattice + power + star +
   370   assumes star_cont: "a * star b * c = SUPR UNIV (\<lambda>n. a * b ^ n * c)"
   371 begin
   372 
   373 subclass kleene
   374 proof
   375   fix a x :: 'a
   376   
   377   have [simp]: "1 \<le> star a"
   378     unfolding star_cont[of 1 a 1, simplified] 
   379     by (subst power_0[symmetric]) (rule le_SUPI [OF UNIV_I])
   380   
   381   show "1 + a * star a \<le> star a"
   382     apply (rule plus_leI, simp)
   383     apply (simp add:star_cont[of a a 1, simplified])
   384     apply (simp add:star_cont[of 1 a 1, simplified])
   385     apply (subst power_Suc[symmetric])
   386     by (intro SUP_leI le_SUPI UNIV_I)
   387 
   388   show "1 + star a * a \<le> star a" 
   389     apply (rule plus_leI, simp)
   390     apply (simp add:star_cont[of 1 a a, simplified])
   391     apply (simp add:star_cont[of 1 a 1, simplified])
   392     by (auto intro: SUP_leI le_SUPI simp add: power_Suc[symmetric] power_commutes simp del: power_Suc)
   393 
   394   show "a * x \<le> x \<Longrightarrow> star a * x \<le> x"
   395   proof -
   396     assume a: "a * x \<le> x"
   397 
   398     {
   399       fix n
   400       have "a ^ (Suc n) * x \<le> a ^ n * x"
   401       proof (induct n)
   402         case 0 thus ?case by (simp add: a)
   403       next
   404         case (Suc n)
   405         hence "a * (a ^ Suc n * x) \<le> a * (a ^ n * x)"
   406           by (auto intro: mult_mono)
   407         thus ?case
   408           by (simp add: mult_assoc)
   409       qed
   410     }
   411     note a = this
   412     
   413     {
   414       fix n have "a ^ n * x \<le> x"
   415       proof (induct n)
   416         case 0 show ?case by simp
   417       next
   418         case (Suc n) with a[of n]
   419         show ?case by simp
   420       qed
   421     }
   422     note b = this
   423     
   424     show "star a * x \<le> x"
   425       unfolding star_cont[of 1 a x, simplified]
   426       by (rule SUP_leI) (rule b)
   427   qed
   428 
   429   show "x * a \<le> x \<Longrightarrow> x * star a \<le> x" (* symmetric *)
   430   proof -
   431     assume a: "x * a \<le> x"
   432 
   433     {
   434       fix n
   435       have "x * a ^ (Suc n) \<le> x * a ^ n"
   436       proof (induct n)
   437         case 0 thus ?case by (simp add: a)
   438       next
   439         case (Suc n)
   440         hence "(x * a ^ Suc n) * a  \<le> (x * a ^ n) * a"
   441           by (auto intro: mult_mono)
   442         thus ?case
   443           by (simp add: power_commutes mult_assoc)
   444       qed
   445     }
   446     note a = this
   447     
   448     {
   449       fix n have "x * a ^ n \<le> x"
   450       proof (induct n)
   451         case 0 show ?case by simp
   452       next
   453         case (Suc n) with a[of n]
   454         show ?case by simp
   455       qed
   456     }
   457     note b = this
   458     
   459     show "x * star a \<le> x"
   460       unfolding star_cont[of x a 1, simplified]
   461       by (rule SUP_leI) (rule b)
   462   qed
   463 qed
   464 
   465 end
   466 
   467 subsection {* Transitive closure *}
   468 
   469 context kleene
   470 begin
   471 
   472 definition
   473   tcl_def: "tcl x = star x * x"
   474 
   475 lemma tcl_zero: "tcl 0 = 0"
   476 unfolding tcl_def by simp
   477 
   478 lemma tcl_unfold_right: "tcl a = a + tcl a * a"
   479 by (metis star_slide2 star_unfold2 tcl_def)
   480 
   481 lemma less_tcl: "a \<le> tcl a"
   482 by (metis star_slide2 tcl_def x_less_star)
   483 
   484 end
   485 
   486 end