function package: name primitive defs "f_sumC_def" instead of "f_sum_def" to avoid clashes
1 (* Title: HOL/Library/Kleene_Algebras.thy
3 Author: Alexander Krauss, TU Muenchen
6 header "Kleene Algebras"
12 text {* A type class of kleene algebras *}
15 fixes star :: "'a \<Rightarrow> 'a"
17 class idem_add = ab_semigroup_add +
18 assumes add_idem [simp]: "x + x = x"
20 lemma add_idem2[simp]: "(x::'a::idem_add) + (x + y) = x + y"
21 unfolding add_assoc[symmetric]
24 class order_by_add = idem_add + ord +
25 assumes order_def: "a \<le> b \<longleftrightarrow> a + b = b"
26 assumes strict_order_def: "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
29 lemma ord_simp1[simp]: "x \<le> y \<Longrightarrow> x + y = y"
32 lemma ord_simp2[simp]: "x \<le> y \<Longrightarrow> y + x = y"
33 unfolding order_def add_commute .
35 lemma ord_intro: "x + y = y \<Longrightarrow> x \<le> y"
40 show "x \<le> x" unfolding order_def by simp
41 show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
42 proof (rule ord_intro)
43 assume "x \<le> y" "y \<le> z"
44 have "x + z = x + y + z" by (simp add:`y \<le> z` add_assoc)
45 also have "\<dots> = y + z" by (simp add:`x \<le> y`)
46 also have "\<dots> = z" by (simp add:`y \<le> z`)
47 finally show "x + z = z" .
49 show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y" unfolding order_def
50 by (simp add: add_commute)
51 show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x" by (fact strict_order_def)
55 "x \<le> z \<Longrightarrow> y \<le> z \<Longrightarrow> x + y \<le> z"
56 unfolding order_def by (simp add: add_assoc)
60 class pre_kleene = semiring_1 + order_by_add
63 subclass pordered_semiring proof
68 show "z + x \<le> z + y"
69 proof (rule ord_intro)
70 have "z + x + (z + y) = x + y + z" by (simp add:add_ac)
71 also have "\<dots> = z + y" by (simp add:`x \<le> y` add_ac)
72 finally show "z + x + (z + y) = z + y" .
75 show "z * x \<le> z * y"
76 proof (rule ord_intro)
77 from `x \<le> y` have "z * (x + y) = z * y" by simp
78 thus "z * x + z * y = z * y" by (simp add:right_distrib)
81 show "x * z \<le> y * z"
82 proof (rule ord_intro)
83 from `x \<le> y` have "(x + y) * z = y * z" by simp
84 thus "x * z + y * z = y * z" by (simp add:left_distrib)
88 lemma zero_minimum [simp]: "0 \<le> x"
89 unfolding order_def by simp
93 class kleene = pre_kleene + star +
94 assumes star1: "1 + a * star a \<le> star a"
95 and star2: "1 + star a * a \<le> star a"
96 and star3: "a * x \<le> x \<Longrightarrow> star a * x \<le> x"
97 and star4: "x * a \<le> x \<Longrightarrow> x * star a \<le> x"
99 class kleene_by_complete_lattice = pre_kleene
100 + complete_lattice + recpower + star +
101 assumes star_cont: "a * star b * c = SUPR UNIV (\<lambda>n. a * b ^ n * c)"
104 lemma (in complete_lattice) le_SUPI':
105 assumes "l \<le> M i"
106 shows "l \<le> (SUP i. M i)"
107 using assms by (rule order_trans) (rule le_SUPI [OF UNIV_I])
111 instance kleene_by_complete_lattice < kleene
116 have [simp]: "1 \<le> star a"
117 unfolding star_cont[of 1 a 1, simplified]
118 by (subst power_0[symmetric]) (rule le_SUPI [OF UNIV_I])
120 show "1 + a * star a \<le> star a"
121 apply (rule plus_leI, simp)
122 apply (simp add:star_cont[of a a 1, simplified])
123 apply (simp add:star_cont[of 1 a 1, simplified])
124 apply (subst power_Suc[symmetric])
125 by (intro SUP_leI le_SUPI UNIV_I)
127 show "1 + star a * a \<le> star a"
128 apply (rule plus_leI, simp)
129 apply (simp add:star_cont[of 1 a a, simplified])
130 apply (simp add:star_cont[of 1 a 1, simplified])
131 by (auto intro: SUP_leI le_SUPI UNIV_I simp add: power_Suc[symmetric] power_commutes)
133 show "a * x \<le> x \<Longrightarrow> star a * x \<le> x"
135 assume a: "a * x \<le> x"
139 have "a ^ (Suc n) * x \<le> a ^ n * x"
141 case 0 thus ?case by (simp add:a power_Suc)
144 hence "a * (a ^ Suc n * x) \<le> a * (a ^ n * x)"
145 by (auto intro: mult_mono)
147 by (simp add:power_Suc mult_assoc)
153 fix n have "a ^ n * x \<le> x"
155 case 0 show ?case by simp
157 case (Suc n) with a[of n]
163 show "star a * x \<le> x"
164 unfolding star_cont[of 1 a x, simplified]
165 by (rule SUP_leI) (rule b)
168 show "x * a \<le> x \<Longrightarrow> x * star a \<le> x" (* symmetric *)
170 assume a: "x * a \<le> x"
174 have "x * a ^ (Suc n) \<le> x * a ^ n"
176 case 0 thus ?case by (simp add:a power_Suc)
179 hence "(x * a ^ Suc n) * a \<le> (x * a ^ n) * a"
180 by (auto intro: mult_mono)
182 by (simp add:power_Suc power_commutes mult_assoc)
188 fix n have "x * a ^ n \<le> x"
190 case 0 show ?case by simp
192 case (Suc n) with a[of n]
198 show "x * star a \<le> x"
199 unfolding star_cont[of x a 1, simplified]
200 by (rule SUP_leI) (rule b)
204 lemma less_add[simp]:
205 fixes a b :: "'a :: order_by_add"
206 shows "a \<le> a + b"
209 by (auto simp:add_ac)
212 fixes a b c :: "'a :: order_by_add"
213 assumes a: "a + b \<le> c"
216 by (rule order_trans)
219 fixes a b c :: "'a :: order_by_add"
220 assumes a: "a + b \<le> c"
223 by (rule order_trans)
227 fixes a b x :: "'a :: kleene"
228 assumes a: "b + a * x \<le> x"
229 shows "star a * b \<le> x"
230 proof (rule order_trans)
231 from a have "b \<le> x" by (rule add_est1)
232 show "star a * b \<le> star a * x"
233 by (rule mult_mono) (auto simp:`b \<le> x`)
235 from a have "a * x \<le> x" by (rule add_est2)
236 with star3 show "star a * x \<le> x" .
241 fixes a b x :: "'a :: kleene"
242 assumes a: "b + x * a \<le> x"
243 shows "b * star a \<le> x"
244 proof (rule order_trans)
245 from a have "b \<le> x" by (rule add_est1)
246 show "b * star a \<le> x * star a"
247 by (rule mult_mono) (auto simp:`b \<le> x`)
249 from a have "x * a \<le> x" by (rule add_est2)
250 with star4 show "x * star a \<le> x" .
255 fixes x :: "'a :: kleene"
256 shows "star (star x) = star x"
259 lemma star_unfold_left:
260 fixes a :: "'a :: kleene"
261 shows "1 + a * star a = star a"
262 proof (rule order_antisym, rule star1)
264 have "1 + a * (1 + a * star a) \<le> 1 + a * star a"
265 apply (rule add_mono, rule)
266 apply (rule mult_mono, auto)
270 with star3' have "star a * 1 \<le> 1 + a * star a" .
271 thus "star a \<le> 1 + a * star a" by simp
275 lemma star_unfold_right:
276 fixes a :: "'a :: kleene"
277 shows "1 + star a * a = star a"
278 proof (rule order_antisym, rule star2)
280 have "1 + (1 + star a * a) * a \<le> 1 + star a * a"
281 apply (rule add_mono, rule)
282 apply (rule mult_mono, auto)
286 with star4' have "1 * star a \<le> 1 + star a * a" .
287 thus "star a \<le> 1 + star a * a" by simp
290 lemma star_zero[simp]:
291 shows "star (0::'a::kleene) = 1"
292 by (rule star_unfold_left[of 0, simplified])
295 fixes a b x :: "'a :: kleene"
296 assumes a: "a * x = x * b"
297 shows "star a * x = x * star b"
298 proof (rule order_antisym)
300 show "star a * x \<le> x * star b"
301 proof (rule star3', rule order_trans)
303 from a have "a * x \<le> x * b" by simp
304 hence "a * x * star b \<le> x * b * star b"
305 by (rule mult_mono) auto
306 thus "x + a * (x * star b) \<le> x + x * b * star b"
307 using add_mono by (auto simp: mult_assoc)
309 show "\<dots> \<le> x * star b"
311 have "x * (1 + b * star b) \<le> x * star b"
312 by (rule mult_mono[OF _ star1]) auto
314 by (simp add:right_distrib mult_assoc)
318 show "x * star b \<le> star a * x"
319 proof (rule star4', rule order_trans)
321 from a have b: "x * b \<le> a * x" by simp
322 have "star a * x * b \<le> star a * a * x"
324 by (rule mult_mono[OF _ b]) auto
325 thus "x + star a * x * b \<le> x + star a * a * x"
326 using add_mono by auto
328 show "\<dots> \<le> star a * x"
330 have "(1 + star a * a) * x \<le> star a * x"
331 by (rule mult_mono[OF star2]) auto
333 by (simp add:left_distrib mult_assoc)
339 fixes c d :: "'a :: kleene"
340 shows "star (c * d) * c = c * star (d * c)"
341 by (auto simp:mult_assoc star_commute)
344 fixes a b :: "'a :: kleene"
345 shows "star (a + b) = star a * star (b * star a)"
349 fixes a p p' :: "'a :: kleene"
350 assumes "p * p' = 1" and "p' * p = 1"
351 shows "p' * star a * p = star (p' * a * p)"
354 have "p' * star a * p = p' * star (p * p' * a) * p"
356 also have "\<dots> = p' * p * star (p' * a * p)"
357 by (simp add: mult_assoc star_assoc)
358 also have "\<dots> = star (p' * a * p)"
360 finally show ?thesis .
364 fixes x y :: "'a :: kleene"
366 shows "star x \<le> star y"
374 lemma x_less_star[simp]:
375 fixes x :: "'a :: kleene"
376 shows "x \<le> x * star a"
378 have "x \<le> x * (1 + a * star a)" by (simp add:right_distrib)
379 also have "\<dots> = x * star a" by (simp only: star_unfold_left)
380 finally show ?thesis .
383 subsection {* Transitive Closure *}
386 "tcl (x::'a::kleene) = star x * x"
389 "tcl (0::'a::kleene) = 0"
390 unfolding tcl_def by simp
392 lemma tcl_unfold_right: "tcl a = a + tcl a * a"
394 from star_unfold_right[of a]
395 have "a * (1 + star a * a) = a * star a" by simp
396 from this[simplified right_distrib, simplified]
398 by (simp add:tcl_def star_commute mult_ac)
401 lemma less_tcl: "a \<le> tcl a"
403 have "a \<le> a + tcl a * a" by simp
404 also have "\<dots> = tcl a" by (rule tcl_unfold_right[symmetric])
405 finally show ?thesis .
408 subsection {* Naive Algorithm to generate the transitive closure *}
410 function (default "\<lambda>x. 0", tailrec, domintros)
411 mk_tcl :: "('a::{plus,times,ord,zero}) \<Rightarrow> 'a \<Rightarrow> 'a"
413 "mk_tcl A X = (if X * A \<le> X then X else mk_tcl A (X + X * A))"
414 by pat_completeness simp
416 declare mk_tcl.simps[simp del] (* loops *)
418 lemma mk_tcl_code[code]:
421 in if XA \<le> X then X else mk_tcl A (X + XA))"
422 unfolding mk_tcl.simps[of A X] Let_def ..
425 fixes X :: "'a :: kleene"
426 shows "(X + X * A) * star A = X * star A"
428 have "A * star A \<le> 1 + A * star A" by simp
429 also have "\<dots> = star A" by (simp add:star_unfold_left)
430 finally have "star A + A * star A = star A" by simp
431 hence "X * (star A + A * star A) = X * star A" by simp
432 thus ?thesis by (simp add:left_distrib right_distrib mult_ac)
436 fixes X :: "'a :: kleene"
437 shows "X * A \<le> X \<Longrightarrow> X * star A = X"
438 by (rule order_antisym) (auto simp:star4)
443 lemma mk_tcl_correctness:
444 fixes A X :: "'a :: {kleene}"
445 assumes "mk_tcl_dom (A, X)"
446 shows "mk_tcl A X = X * star A"
448 by induct (auto simp:mk_tcl_lemma1 mk_tcl_lemma2)
450 lemma graph_implies_dom: "mk_tcl_graph x y \<Longrightarrow> mk_tcl_dom x"
451 by (rule mk_tcl_graph.induct) (auto intro:accp.accI elim:mk_tcl_rel.cases)
453 lemma mk_tcl_default: "\<not> mk_tcl_dom (a,x) \<Longrightarrow> mk_tcl a x = 0"
455 by (rule fundef_default_value[OF mk_tcl_sumC_def graph_implies_dom])
458 text {* We can replace the dom-Condition of the correctness theorem
459 with something executable *}
461 lemma mk_tcl_correctness2:
462 fixes A X :: "'a :: {kleene}"
463 assumes "mk_tcl A A \<noteq> 0"
464 shows "mk_tcl A A = tcl A"
465 using assms mk_tcl_default mk_tcl_correctness
467 by (auto simp:star_commute)