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