src/HOL/Library/Kleene_Algebra.thy
author wenzelm
Thu Feb 11 23:00:22 2010 +0100 (2010-02-11)
changeset 35115 446c5063e4fd
parent 35028 108662d50512
child 37088 36c13099d10f
permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
krauss@31990
     1
(*  Title:      HOL/Library/Kleene_Algebra.thy
krauss@31990
     2
    Author:     Alexander Krauss, TU Muenchen
krauss@31990
     3
*)
krauss@31990
     4
krauss@31990
     5
header "Kleene Algebra"
krauss@31990
     6
krauss@31990
     7
theory Kleene_Algebra
krauss@31990
     8
imports Main 
krauss@31990
     9
begin
krauss@31990
    10
krauss@31990
    11
text {* WARNING: This is work in progress. Expect changes in the future *}
krauss@31990
    12
krauss@31990
    13
text {* A type class of Kleene algebras *}
krauss@31990
    14
krauss@31990
    15
class star =
krauss@31990
    16
  fixes star :: "'a \<Rightarrow> 'a"
krauss@31990
    17
krauss@31990
    18
class idem_add = ab_semigroup_add +
krauss@31990
    19
  assumes add_idem [simp]: "x + x = x"
krauss@31990
    20
begin
krauss@31990
    21
krauss@31990
    22
lemma add_idem2[simp]: "(x::'a) + (x + y) = x + y"
krauss@31990
    23
unfolding add_assoc[symmetric] by simp
krauss@31990
    24
krauss@31990
    25
end
krauss@31990
    26
krauss@31990
    27
class order_by_add = idem_add + ord +
krauss@31990
    28
  assumes order_def: "a \<le> b \<longleftrightarrow> a + b = b"
krauss@31990
    29
  assumes strict_order_def: "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
krauss@31990
    30
begin
krauss@31990
    31
krauss@31990
    32
lemma ord_simp1[simp]: "x \<le> y \<Longrightarrow> x + y = y"
krauss@31990
    33
  unfolding order_def .
krauss@31990
    34
krauss@31990
    35
lemma ord_simp2[simp]: "x \<le> y \<Longrightarrow> y + x = y"
krauss@31990
    36
  unfolding order_def add_commute .
krauss@31990
    37
krauss@31990
    38
lemma ord_intro: "x + y = y \<Longrightarrow> x \<le> y"
krauss@31990
    39
  unfolding order_def .
krauss@31990
    40
krauss@31990
    41
subclass order proof
krauss@31990
    42
  fix x y z :: 'a
krauss@31990
    43
  show "x \<le> x" unfolding order_def by simp
krauss@31990
    44
  show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
krauss@31990
    45
  proof (rule ord_intro)
krauss@31990
    46
    assume "x \<le> y" "y \<le> z"
krauss@31990
    47
    have "x + z = x + y + z" by (simp add:`y \<le> z` add_assoc)
krauss@31990
    48
    also have "\<dots> = y + z" by (simp add:`x \<le> y`)
krauss@31990
    49
    also have "\<dots> = z" by (simp add:`y \<le> z`)
krauss@31990
    50
    finally show "x + z = z" .
krauss@31990
    51
  qed
krauss@31990
    52
  show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y" unfolding order_def
krauss@31990
    53
    by (simp add: add_commute)
krauss@31990
    54
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x" by (fact strict_order_def)
krauss@31990
    55
qed
krauss@31990
    56
krauss@31990
    57
lemma plus_leI: 
krauss@31990
    58
  "x \<le> z \<Longrightarrow> y \<le> z \<Longrightarrow> x + y \<le> z"
krauss@31990
    59
  unfolding order_def by (simp add: add_assoc)
krauss@31990
    60
krauss@31990
    61
lemma less_add[simp]: "a \<le> a + b" "b \<le> a + b"
krauss@31990
    62
unfolding order_def by (auto simp:add_ac)
krauss@31990
    63
krauss@31990
    64
lemma add_est1: "a + b \<le> c \<Longrightarrow> a \<le> c"
krauss@31990
    65
using less_add(1) by (rule order_trans)
krauss@31990
    66
krauss@31990
    67
lemma add_est2: "a + b \<le> c \<Longrightarrow> b \<le> c"
krauss@31990
    68
using less_add(2) by (rule order_trans)
krauss@31990
    69
krauss@31990
    70
end
krauss@31990
    71
krauss@31990
    72
class pre_kleene = semiring_1 + order_by_add
krauss@31990
    73
begin
krauss@31990
    74
haftmann@35028
    75
subclass ordered_semiring proof
krauss@31990
    76
  fix x y z :: 'a
krauss@31990
    77
krauss@31990
    78
  assume "x \<le> y"
krauss@31990
    79
   
krauss@31990
    80
  show "z + x \<le> z + y"
krauss@31990
    81
  proof (rule ord_intro)
krauss@31990
    82
    have "z + x + (z + y) = x + y + z" by (simp add:add_ac)
krauss@31990
    83
    also have "\<dots> = z + y" by (simp add:`x \<le> y` add_ac)
krauss@31990
    84
    finally show "z + x + (z + y) = z + y" .
krauss@31990
    85
  qed
krauss@31990
    86
krauss@31990
    87
  show "z * x \<le> z * y"
krauss@31990
    88
  proof (rule ord_intro)
krauss@31990
    89
    from `x \<le> y` have "z * (x + y) = z * y" by simp
krauss@31990
    90
    thus "z * x + z * y = z * y" by (simp add:right_distrib)
krauss@31990
    91
  qed
krauss@31990
    92
krauss@31990
    93
  show "x * z \<le> y * z"
krauss@31990
    94
  proof (rule ord_intro)
krauss@31990
    95
    from `x \<le> y` have "(x + y) * z = y * z" by simp
krauss@31990
    96
    thus "x * z + y * z = y * z" by (simp add:left_distrib)
krauss@31990
    97
  qed
krauss@31990
    98
qed
krauss@31990
    99
krauss@31990
   100
lemma zero_minimum [simp]: "0 \<le> x"
krauss@31990
   101
  unfolding order_def by simp
krauss@31990
   102
krauss@31990
   103
end
krauss@31990
   104
krauss@31990
   105
class kleene = pre_kleene + star +
krauss@31990
   106
  assumes star1: "1 + a * star a \<le> star a"
krauss@31990
   107
  and star2: "1 + star a * a \<le> star a"
krauss@31990
   108
  and star3: "a * x \<le> x \<Longrightarrow> star a * x \<le> x"
krauss@31990
   109
  and star4: "x * a \<le> x \<Longrightarrow> x * star a \<le> x"
krauss@31990
   110
begin
krauss@31990
   111
krauss@31990
   112
lemma star3':
krauss@31990
   113
  assumes a: "b + a * x \<le> x"
krauss@31990
   114
  shows "star a * b \<le> x"
krauss@31990
   115
proof (rule order_trans)
krauss@31990
   116
  from a have "b \<le> x" by (rule add_est1)
krauss@31990
   117
  show "star a * b \<le> star a * x"
krauss@31990
   118
    by (rule mult_mono) (auto simp:`b \<le> x`)
krauss@31990
   119
krauss@31990
   120
  from a have "a * x \<le> x" by (rule add_est2)
krauss@31990
   121
  with star3 show "star a * x \<le> x" .
krauss@31990
   122
qed
krauss@31990
   123
krauss@31990
   124
lemma star4':
krauss@31990
   125
  assumes a: "b + x * a \<le> x"
krauss@31990
   126
  shows "b * star a \<le> x"
krauss@31990
   127
proof (rule order_trans)
krauss@31990
   128
  from a have "b \<le> x" by (rule add_est1)
krauss@31990
   129
  show "b * star a \<le> x * star a"
krauss@31990
   130
    by (rule mult_mono) (auto simp:`b \<le> x`)
krauss@31990
   131
krauss@31990
   132
  from a have "x * a \<le> x" by (rule add_est2)
krauss@31990
   133
  with star4 show "x * star a \<le> x" .
krauss@31990
   134
qed
krauss@31990
   135
krauss@31990
   136
lemma star_unfold_left:
krauss@31990
   137
  shows "1 + a * star a = star a"
krauss@31990
   138
proof (rule antisym, rule star1)
krauss@31990
   139
  have "1 + a * (1 + a * star a) \<le> 1 + a * star a"
krauss@31990
   140
    apply (rule add_mono, rule)
krauss@31990
   141
    apply (rule mult_mono, auto)
krauss@31990
   142
    apply (rule star1)
krauss@31990
   143
    done
krauss@31990
   144
  with star3' have "star a * 1 \<le> 1 + a * star a" .
krauss@31990
   145
  thus "star a \<le> 1 + a * star a" by simp
krauss@31990
   146
qed
krauss@31990
   147
krauss@31990
   148
lemma star_unfold_right: "1 + star a * a = star a"
krauss@31990
   149
proof (rule antisym, rule star2)
krauss@31990
   150
  have "1 + (1 + star a * a) * a \<le> 1 + star a * a"
krauss@31990
   151
    apply (rule add_mono, rule)
krauss@31990
   152
    apply (rule mult_mono, auto)
krauss@31990
   153
    apply (rule star2)
krauss@31990
   154
    done
krauss@31990
   155
  with star4' have "1 * star a \<le> 1 + star a * a" .
krauss@31990
   156
  thus "star a \<le> 1 + star a * a" by simp
krauss@31990
   157
qed
krauss@31990
   158
krauss@31990
   159
lemma star_zero[simp]: "star 0 = 1"
krauss@31990
   160
by (fact star_unfold_left[of 0, simplified, symmetric])
krauss@31990
   161
krauss@31990
   162
lemma star_one[simp]: "star 1 = 1"
krauss@31990
   163
by (metis add_idem2 eq_iff mult_1_right ord_simp2 star3 star_unfold_left)
krauss@31990
   164
krauss@31990
   165
lemma one_less_star: "1 \<le> star x"
krauss@31990
   166
by (metis less_add(1) star_unfold_left)
krauss@31990
   167
krauss@31990
   168
lemma ka1: "x * star x \<le> star x"
krauss@31990
   169
by (metis less_add(2) star_unfold_left)
krauss@31990
   170
krauss@31990
   171
lemma star_mult_idem[simp]: "star x * star x = star x"
krauss@31990
   172
by (metis add_commute add_est1 eq_iff mult_1_right right_distrib star3 star_unfold_left)
krauss@31990
   173
krauss@31990
   174
lemma less_star: "x \<le> star x"
krauss@31990
   175
by (metis less_add(2) mult_1_right mult_left_mono one_less_star order_trans star_unfold_left zero_minimum)
krauss@31990
   176
krauss@31990
   177
lemma star_simulation:
krauss@31990
   178
  assumes a: "a * x = x * b"
krauss@31990
   179
  shows "star a * x = x * star b"
krauss@31990
   180
proof (rule antisym)
krauss@31990
   181
  show "star a * x \<le> x * star b"
krauss@31990
   182
  proof (rule star3', rule order_trans)
krauss@31990
   183
    from a have "a * x \<le> x * b" by simp
krauss@31990
   184
    hence "a * x * star b \<le> x * b * star b"
krauss@31990
   185
      by (rule mult_mono) auto
krauss@31990
   186
    thus "x + a * (x * star b) \<le> x + x * b * star b"
krauss@31990
   187
      using add_mono by (auto simp: mult_assoc)
krauss@31990
   188
    show "\<dots> \<le> x * star b"
krauss@31990
   189
    proof -
krauss@31990
   190
      have "x * (1 + b * star b) \<le> x * star b"
krauss@31990
   191
        by (rule mult_mono[OF _ star1]) auto
krauss@31990
   192
      thus ?thesis
krauss@31990
   193
        by (simp add:right_distrib mult_assoc)
krauss@31990
   194
    qed
krauss@31990
   195
  qed
krauss@31990
   196
  show "x * star b \<le> star a * x"
krauss@31990
   197
  proof (rule star4', rule order_trans)
krauss@31990
   198
    from a have b: "x * b \<le> a * x" by simp
krauss@31990
   199
    have "star a * x * b \<le> star a * a * x"
krauss@31990
   200
      unfolding mult_assoc
krauss@31990
   201
      by (rule mult_mono[OF _ b]) auto
krauss@31990
   202
    thus "x + star a * x * b \<le> x + star a * a * x"
krauss@31990
   203
      using add_mono by auto
krauss@31990
   204
    show "\<dots> \<le> star a * x"
krauss@31990
   205
    proof -
krauss@31990
   206
      have "(1 + star a * a) * x \<le> star a * x"
krauss@31990
   207
        by (rule mult_mono[OF star2]) auto
krauss@31990
   208
      thus ?thesis
krauss@31990
   209
        by (simp add:left_distrib mult_assoc)
krauss@31990
   210
    qed
krauss@31990
   211
  qed
krauss@31990
   212
qed
krauss@31990
   213
krauss@31990
   214
lemma star_slide2[simp]: "star x * x = x * star x"
krauss@31990
   215
by (metis star_simulation)
krauss@31990
   216
krauss@31990
   217
lemma star_idemp[simp]: "star (star x) = star x"
krauss@31990
   218
by (metis add_idem2 eq_iff less_star mult_1_right star3' star_mult_idem star_unfold_left)
krauss@31990
   219
krauss@31990
   220
lemma star_slide[simp]: "star (x * y) * x = x * star (y * x)"
krauss@31990
   221
by (auto simp: mult_assoc star_simulation)
krauss@31990
   222
krauss@31990
   223
lemma star_one':
krauss@31990
   224
  assumes "p * p' = 1" "p' * p = 1"
krauss@31990
   225
  shows "p' * star a * p = star (p' * a * p)"
krauss@31990
   226
proof -
krauss@31990
   227
  from assms
krauss@31990
   228
  have "p' * star a * p = p' * star (p * p' * a) * p"
krauss@31990
   229
    by simp
krauss@31990
   230
  also have "\<dots> = p' * p * star (p' * a * p)"
krauss@31990
   231
    by (simp add: mult_assoc)
krauss@31990
   232
  also have "\<dots> = star (p' * a * p)"
krauss@31990
   233
    by (simp add: assms)
krauss@31990
   234
  finally show ?thesis .
krauss@31990
   235
qed
krauss@31990
   236
krauss@31990
   237
lemma x_less_star[simp]: "x \<le> x * star a"
krauss@31990
   238
proof -
krauss@31990
   239
  have "x \<le> x * (1 + a * star a)" by (simp add: right_distrib)
krauss@31990
   240
  also have "\<dots> = x * star a" by (simp only: star_unfold_left)
krauss@31990
   241
  finally show ?thesis .
krauss@31990
   242
qed
krauss@31990
   243
krauss@31990
   244
lemma star_mono:  "x \<le> y \<Longrightarrow>  star x \<le> star y"
krauss@31990
   245
by (metis add_commute eq_iff less_star ord_simp2 order_trans star3 star4' star_idemp star_mult_idem x_less_star)
krauss@31990
   246
krauss@31990
   247
lemma star_sub: "x \<le> 1 \<Longrightarrow> star x = 1"
krauss@31990
   248
by (metis add_commute ord_simp1 star_idemp star_mono star_mult_idem star_one star_unfold_left)
krauss@31990
   249
krauss@31990
   250
lemma star_unfold2: "star x * y = y + x * star x * y"
krauss@31990
   251
by (subst star_unfold_right[symmetric]) (simp add: mult_assoc left_distrib)
krauss@31990
   252
krauss@31990
   253
lemma star_absorb_one[simp]: "star (x + 1) = star x"
krauss@31990
   254
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)
krauss@31990
   255
krauss@31990
   256
lemma star_absorb_one'[simp]: "star (1 + x) = star x"
krauss@31990
   257
by (subst add_commute) (fact star_absorb_one)
krauss@31990
   258
krauss@31990
   259
lemma ka16: "(y * star x) * star (y * star x) \<le> star x * star (y * star x)"
krauss@31990
   260
by (metis ka1 less_add(1) mult_assoc order_trans star_unfold2)
krauss@31990
   261
krauss@31990
   262
lemma ka16': "(star x * y) * star (star x * y) \<le> star (star x * y) * star x"
krauss@31990
   263
by (metis ka1 mult_assoc order_trans star_slide x_less_star)
krauss@31990
   264
krauss@31990
   265
lemma ka17: "(x * star x) * star (y * star x) \<le> star x * star (y * star x)"
krauss@31990
   266
by (metis ka1 mult_assoc mult_right_mono zero_minimum)
krauss@31990
   267
krauss@31990
   268
lemma ka18: "(x * star x) * star (y * star x) + (y * star x) * star (y * star x)
krauss@31990
   269
  \<le> star x * star (y * star x)"
krauss@31990
   270
by (metis ka16 ka17 left_distrib mult_assoc plus_leI)
krauss@31990
   271
krauss@31990
   272
lemma kleene_church_rosser: 
krauss@31990
   273
  "star y * star x \<le> star x * star y \<Longrightarrow> star (x + y) \<le> star x * star y"
krauss@31990
   274
oops
krauss@31990
   275
krauss@31990
   276
lemma star_decomp: "star (a + b) = star a * star (b * star a)"
krauss@32238
   277
proof (rule antisym)
krauss@32238
   278
  have "1 + (a + b) * star a * star (b * star a) \<le>
krauss@32238
   279
    1 + a * star a * star (b * star a) + b * star a * star (b * star a)"
krauss@32238
   280
    by (metis add_commute add_left_commute eq_iff left_distrib mult_assoc)
krauss@32238
   281
  also have "\<dots> \<le> star a * star (b * star a)"
krauss@32238
   282
    by (metis add_commute add_est1 add_left_commute ka18 plus_leI star_unfold_left x_less_star)
krauss@32238
   283
  finally show "star (a + b) \<le> star a * star (b * star a)"
krauss@32238
   284
    by (metis mult_1_right mult_assoc star3')
krauss@32238
   285
next
krauss@32238
   286
  show "star a * star (b * star a) \<le> star (a + b)"
krauss@32238
   287
    by (metis add_assoc add_est1 add_est2 add_left_commute less_star mult_mono'
krauss@32238
   288
      star_absorb_one star_absorb_one' star_idemp star_mono star_mult_idem zero_minimum)
krauss@32238
   289
qed
krauss@31990
   290
krauss@31990
   291
lemma ka22: "y * star x \<le> star x * star y \<Longrightarrow>  star y * star x \<le> star x * star y"
krauss@31990
   292
by (metis mult_assoc mult_right_mono plus_leI star3' star_mult_idem x_less_star zero_minimum)
krauss@31990
   293
krauss@31990
   294
lemma ka23: "star y * star x \<le> star x * star y \<Longrightarrow> y * star x \<le> star x * star y"
krauss@31990
   295
by (metis less_star mult_right_mono order_trans zero_minimum)
krauss@31990
   296
krauss@31990
   297
lemma ka24: "star (x + y) \<le> star (star x * star y)"
krauss@31990
   298
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)
krauss@31990
   299
krauss@31990
   300
lemma ka25: "star y * star x \<le> star x * star y \<Longrightarrow> star (star y * star x) \<le> star x * star y"
krauss@31990
   301
oops
krauss@31990
   302
krauss@31990
   303
lemma kleene_bubblesort: "y * x \<le> x * y \<Longrightarrow> star (x + y) \<le> star x * star y"
krauss@31990
   304
oops
krauss@31990
   305
krauss@31990
   306
end
krauss@31990
   307
krauss@31990
   308
subsection {* Complete lattices are Kleene algebras *}
krauss@31990
   309
krauss@31990
   310
lemma (in complete_lattice) le_SUPI':
krauss@31990
   311
  assumes "l \<le> M i"
krauss@31990
   312
  shows "l \<le> (SUP i. M i)"
krauss@31990
   313
  using assms by (rule order_trans) (rule le_SUPI [OF UNIV_I])
krauss@31990
   314
krauss@31990
   315
class kleene_by_complete_lattice = pre_kleene
krauss@31990
   316
  + complete_lattice + power + star +
krauss@31990
   317
  assumes star_cont: "a * star b * c = SUPR UNIV (\<lambda>n. a * b ^ n * c)"
krauss@31990
   318
begin
krauss@31990
   319
krauss@31990
   320
subclass kleene
krauss@31990
   321
proof
krauss@31990
   322
  fix a x :: 'a
krauss@31990
   323
  
krauss@31990
   324
  have [simp]: "1 \<le> star a"
krauss@31990
   325
    unfolding star_cont[of 1 a 1, simplified] 
krauss@31990
   326
    by (subst power_0[symmetric]) (rule le_SUPI [OF UNIV_I])
krauss@31990
   327
  
krauss@31990
   328
  show "1 + a * star a \<le> star a" 
krauss@31990
   329
    apply (rule plus_leI, simp)
krauss@31990
   330
    apply (simp add:star_cont[of a a 1, simplified])
krauss@31990
   331
    apply (simp add:star_cont[of 1 a 1, simplified])
krauss@31990
   332
    apply (subst power_Suc[symmetric])
krauss@31990
   333
    by (intro SUP_leI le_SUPI UNIV_I)
krauss@31990
   334
krauss@31990
   335
  show "1 + star a * a \<le> star a" 
krauss@31990
   336
    apply (rule plus_leI, simp)
krauss@31990
   337
    apply (simp add:star_cont[of 1 a a, simplified])
krauss@31990
   338
    apply (simp add:star_cont[of 1 a 1, simplified])
krauss@31990
   339
    by (auto intro: SUP_leI le_SUPI simp add: power_Suc[symmetric] power_commutes simp del: power_Suc)
krauss@31990
   340
krauss@31990
   341
  show "a * x \<le> x \<Longrightarrow> star a * x \<le> x"
krauss@31990
   342
  proof -
krauss@31990
   343
    assume a: "a * x \<le> x"
krauss@31990
   344
krauss@31990
   345
    {
krauss@31990
   346
      fix n
krauss@31990
   347
      have "a ^ (Suc n) * x \<le> a ^ n * x"
krauss@31990
   348
      proof (induct n)
krauss@31990
   349
        case 0 thus ?case by (simp add: a)
krauss@31990
   350
      next
krauss@31990
   351
        case (Suc n)
krauss@31990
   352
        hence "a * (a ^ Suc n * x) \<le> a * (a ^ n * x)"
krauss@31990
   353
          by (auto intro: mult_mono)
krauss@31990
   354
        thus ?case
krauss@31990
   355
          by (simp add: mult_assoc)
krauss@31990
   356
      qed
krauss@31990
   357
    }
krauss@31990
   358
    note a = this
krauss@31990
   359
    
krauss@31990
   360
    {
krauss@31990
   361
      fix n have "a ^ n * x \<le> x"
krauss@31990
   362
      proof (induct n)
krauss@31990
   363
        case 0 show ?case by simp
krauss@31990
   364
      next
krauss@31990
   365
        case (Suc n) with a[of n]
krauss@31990
   366
        show ?case by simp
krauss@31990
   367
      qed
krauss@31990
   368
    }
krauss@31990
   369
    note b = this
krauss@31990
   370
    
krauss@31990
   371
    show "star a * x \<le> x"
krauss@31990
   372
      unfolding star_cont[of 1 a x, simplified]
krauss@31990
   373
      by (rule SUP_leI) (rule b)
krauss@31990
   374
  qed
krauss@31990
   375
krauss@31990
   376
  show "x * a \<le> x \<Longrightarrow> x * star a \<le> x" (* symmetric *)
krauss@31990
   377
  proof -
krauss@31990
   378
    assume a: "x * a \<le> x"
krauss@31990
   379
krauss@31990
   380
    {
krauss@31990
   381
      fix n
krauss@31990
   382
      have "x * a ^ (Suc n) \<le> x * a ^ n"
krauss@31990
   383
      proof (induct n)
krauss@31990
   384
        case 0 thus ?case by (simp add: a)
krauss@31990
   385
      next
krauss@31990
   386
        case (Suc n)
krauss@31990
   387
        hence "(x * a ^ Suc n) * a  \<le> (x * a ^ n) * a"
krauss@31990
   388
          by (auto intro: mult_mono)
krauss@31990
   389
        thus ?case
krauss@31990
   390
          by (simp add: power_commutes mult_assoc)
krauss@31990
   391
      qed
krauss@31990
   392
    }
krauss@31990
   393
    note a = this
krauss@31990
   394
    
krauss@31990
   395
    {
krauss@31990
   396
      fix n have "x * a ^ n \<le> x"
krauss@31990
   397
      proof (induct n)
krauss@31990
   398
        case 0 show ?case by simp
krauss@31990
   399
      next
krauss@31990
   400
        case (Suc n) with a[of n]
krauss@31990
   401
        show ?case by simp
krauss@31990
   402
      qed
krauss@31990
   403
    }
krauss@31990
   404
    note b = this
krauss@31990
   405
    
krauss@31990
   406
    show "x * star a \<le> x"
krauss@31990
   407
      unfolding star_cont[of x a 1, simplified]
krauss@31990
   408
      by (rule SUP_leI) (rule b)
krauss@31990
   409
  qed
krauss@31990
   410
qed
krauss@31990
   411
krauss@31990
   412
end
krauss@31990
   413
krauss@31990
   414
krauss@31990
   415
subsection {* Transitive Closure *}
krauss@31990
   416
krauss@31990
   417
context kleene
krauss@31990
   418
begin
krauss@31990
   419
krauss@31990
   420
definition 
krauss@31990
   421
  tcl_def:  "tcl x = star x * x"
krauss@31990
   422
krauss@31990
   423
lemma tcl_zero: "tcl 0 = 0"
krauss@31990
   424
unfolding tcl_def by simp
krauss@31990
   425
krauss@31990
   426
lemma tcl_unfold_right: "tcl a = a + tcl a * a"
krauss@31990
   427
proof -
krauss@31990
   428
  from star_unfold_right[of a]
krauss@31990
   429
  have "a * (1 + star a * a) = a * star a" by simp
krauss@31990
   430
  from this[simplified right_distrib, simplified]
krauss@31990
   431
  show ?thesis
krauss@31990
   432
    by (simp add:tcl_def mult_assoc)
krauss@31990
   433
qed
krauss@31990
   434
krauss@31990
   435
lemma less_tcl: "a \<le> tcl a"
krauss@31990
   436
proof -
krauss@31990
   437
  have "a \<le> a + tcl a * a" by simp
krauss@31990
   438
  also have "\<dots> = tcl a" by (rule tcl_unfold_right[symmetric])
krauss@31990
   439
  finally show ?thesis .
krauss@31990
   440
qed
krauss@31990
   441
krauss@31990
   442
end
krauss@31990
   443
krauss@31990
   444
krauss@31990
   445
subsection {* Naive Algorithm to generate the transitive closure *}
krauss@31990
   446
krauss@31990
   447
function (default "\<lambda>x. 0", tailrec, domintros)
krauss@31990
   448
  mk_tcl :: "('a::{plus,times,ord,zero}) \<Rightarrow> 'a \<Rightarrow> 'a"
krauss@31990
   449
where
krauss@31990
   450
  "mk_tcl A X = (if X * A \<le> X then X else mk_tcl A (X + X * A))"
krauss@31990
   451
  by pat_completeness simp
krauss@31990
   452
krauss@31990
   453
declare mk_tcl.simps[simp del] (* loops *)
krauss@31990
   454
krauss@31990
   455
lemma mk_tcl_code[code]:
krauss@31990
   456
  "mk_tcl A X = 
krauss@31990
   457
  (let XA = X * A 
krauss@31990
   458
  in if XA \<le> X then X else mk_tcl A (X + XA))"
krauss@31990
   459
  unfolding mk_tcl.simps[of A X] Let_def ..
krauss@31990
   460
krauss@31990
   461
context kleene
krauss@31990
   462
begin
krauss@31990
   463
krauss@31990
   464
lemma mk_tcl_lemma1:
krauss@31990
   465
  "(X + X * A) * star A = X * star A"
krauss@31990
   466
proof -
krauss@31990
   467
  have "A * star A \<le> 1 + A * star A" by simp
krauss@31990
   468
  also have "\<dots> = star A" by (simp add:star_unfold_left)
krauss@31990
   469
  finally have "star A + A * star A = star A" by simp
krauss@31990
   470
  hence "X * (star A + A * star A) = X * star A" by simp
krauss@31990
   471
  thus ?thesis by (simp add:left_distrib right_distrib mult_assoc)
krauss@31990
   472
qed
krauss@31990
   473
krauss@31990
   474
lemma mk_tcl_lemma2:
krauss@31990
   475
  shows "X * A \<le> X \<Longrightarrow> X * star A = X"
krauss@31990
   476
  by (rule antisym) (auto simp:star4)
krauss@31990
   477
krauss@31990
   478
end
krauss@31990
   479
krauss@31990
   480
lemma mk_tcl_correctness:
krauss@31990
   481
  fixes X :: "'a::kleene"
krauss@31990
   482
  assumes "mk_tcl_dom (A, X)"
krauss@31990
   483
  shows "mk_tcl A X = X * star A"
krauss@31990
   484
  using assms
krauss@31990
   485
  by induct (auto simp: mk_tcl_lemma1 mk_tcl_lemma2)
krauss@31990
   486
krauss@31990
   487
krauss@31990
   488
lemma graph_implies_dom: "mk_tcl_graph x y \<Longrightarrow> mk_tcl_dom x"
krauss@31990
   489
  by (rule mk_tcl_graph.induct) (auto intro:accp.accI elim:mk_tcl_rel.cases)
krauss@31990
   490
krauss@31990
   491
lemma mk_tcl_default: "\<not> mk_tcl_dom (a,x) \<Longrightarrow> mk_tcl a x = 0"
krauss@31990
   492
  unfolding mk_tcl_def
krauss@31990
   493
  by (rule fundef_default_value[OF mk_tcl_sumC_def graph_implies_dom])
krauss@31990
   494
krauss@31990
   495
krauss@31990
   496
text {* We can replace the dom-Condition of the correctness theorem 
krauss@31990
   497
  with something executable *}
krauss@31990
   498
krauss@31990
   499
lemma mk_tcl_correctness2:
krauss@31990
   500
  fixes A X :: "'a :: {kleene}"
krauss@31990
   501
  assumes "mk_tcl A A \<noteq> 0"
krauss@31990
   502
  shows "mk_tcl A A = tcl A"
krauss@31990
   503
  using assms mk_tcl_default mk_tcl_correctness
krauss@31990
   504
  unfolding tcl_def 
krauss@31990
   505
  by auto
krauss@31990
   506
krauss@31990
   507
end