src/HOL/Transitive_Closure.thy
author haftmann
Tue Oct 06 18:44:06 2009 +0200 (2009-10-06)
changeset 32883 7cbd93dacef3
parent 32601 47d0c967c64e
child 32901 5564af2d0588
permissions -rw-r--r--
inf/sup1/2_iff are mere duplicates of underlying definitions: dropped
nipkow@10213
     1
(*  Title:      HOL/Transitive_Closure.thy
nipkow@10213
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
nipkow@10213
     3
    Copyright   1992  University of Cambridge
nipkow@10213
     4
*)
nipkow@10213
     5
wenzelm@12691
     6
header {* Reflexive and Transitive closure of a relation *}
wenzelm@12691
     7
nipkow@15131
     8
theory Transitive_Closure
berghofe@22262
     9
imports Predicate
wenzelm@21589
    10
uses "~~/src/Provers/trancl.ML"
nipkow@15131
    11
begin
wenzelm@12691
    12
wenzelm@12691
    13
text {*
wenzelm@12691
    14
  @{text rtrancl} is reflexive/transitive closure,
wenzelm@12691
    15
  @{text trancl} is transitive closure,
wenzelm@12691
    16
  @{text reflcl} is reflexive closure.
wenzelm@12691
    17
wenzelm@12691
    18
  These postfix operators have \emph{maximum priority}, forcing their
wenzelm@12691
    19
  operands to be atomic.
wenzelm@12691
    20
*}
nipkow@10213
    21
berghofe@23743
    22
inductive_set
berghofe@23743
    23
  rtrancl :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"   ("(_^*)" [1000] 999)
berghofe@23743
    24
  for r :: "('a \<times> 'a) set"
berghofe@22262
    25
where
berghofe@23743
    26
    rtrancl_refl [intro!, Pure.intro!, simp]: "(a, a) : r^*"
berghofe@23743
    27
  | rtrancl_into_rtrancl [Pure.intro]: "(a, b) : r^* ==> (b, c) : r ==> (a, c) : r^*"
berghofe@11327
    28
berghofe@23743
    29
inductive_set
berghofe@23743
    30
  trancl :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"  ("(_^+)" [1000] 999)
berghofe@23743
    31
  for r :: "('a \<times> 'a) set"
berghofe@22262
    32
where
berghofe@23743
    33
    r_into_trancl [intro, Pure.intro]: "(a, b) : r ==> (a, b) : r^+"
berghofe@23743
    34
  | trancl_into_trancl [Pure.intro]: "(a, b) : r^+ ==> (b, c) : r ==> (a, c) : r^+"
berghofe@13704
    35
berghofe@23743
    36
notation
berghofe@23743
    37
  rtranclp  ("(_^**)" [1000] 1000) and
berghofe@23743
    38
  tranclp  ("(_^++)" [1000] 1000)
nipkow@10213
    39
wenzelm@19656
    40
abbreviation
berghofe@23743
    41
  reflclp :: "('a => 'a => bool) => 'a => 'a => bool"  ("(_^==)" [1000] 1000) where
haftmann@22422
    42
  "r^== == sup r op ="
berghofe@22262
    43
berghofe@22262
    44
abbreviation
berghofe@23743
    45
  reflcl :: "('a \<times> 'a) set => ('a \<times> 'a) set"  ("(_^=)" [1000] 999) where
wenzelm@19656
    46
  "r^= == r \<union> Id"
nipkow@10213
    47
wenzelm@21210
    48
notation (xsymbols)
berghofe@23743
    49
  rtranclp  ("(_\<^sup>*\<^sup>*)" [1000] 1000) and
berghofe@23743
    50
  tranclp  ("(_\<^sup>+\<^sup>+)" [1000] 1000) and
berghofe@23743
    51
  reflclp  ("(_\<^sup>=\<^sup>=)" [1000] 1000) and
berghofe@23743
    52
  rtrancl  ("(_\<^sup>*)" [1000] 999) and
berghofe@23743
    53
  trancl  ("(_\<^sup>+)" [1000] 999) and
berghofe@23743
    54
  reflcl  ("(_\<^sup>=)" [1000] 999)
wenzelm@12691
    55
wenzelm@21210
    56
notation (HTML output)
berghofe@23743
    57
  rtranclp  ("(_\<^sup>*\<^sup>*)" [1000] 1000) and
berghofe@23743
    58
  tranclp  ("(_\<^sup>+\<^sup>+)" [1000] 1000) and
berghofe@23743
    59
  reflclp  ("(_\<^sup>=\<^sup>=)" [1000] 1000) and
berghofe@23743
    60
  rtrancl  ("(_\<^sup>*)" [1000] 999) and
berghofe@23743
    61
  trancl  ("(_\<^sup>+)" [1000] 999) and
berghofe@23743
    62
  reflcl  ("(_\<^sup>=)" [1000] 999)
kleing@14565
    63
wenzelm@12691
    64
nipkow@26271
    65
subsection {* Reflexive closure *}
nipkow@26271
    66
nipkow@30198
    67
lemma refl_reflcl[simp]: "refl(r^=)"
nipkow@30198
    68
by(simp add:refl_on_def)
nipkow@26271
    69
nipkow@26271
    70
lemma antisym_reflcl[simp]: "antisym(r^=) = antisym r"
nipkow@26271
    71
by(simp add:antisym_def)
nipkow@26271
    72
nipkow@26271
    73
lemma trans_reflclI[simp]: "trans r \<Longrightarrow> trans(r^=)"
nipkow@26271
    74
unfolding trans_def by blast
nipkow@26271
    75
nipkow@26271
    76
wenzelm@12691
    77
subsection {* Reflexive-transitive closure *}
wenzelm@12691
    78
haftmann@32883
    79
lemma reflcl_set_eq [pred_set_conv]: "(sup (\<lambda>x y. (x, y) \<in> r) op =) = (\<lambda>x y. (x, y) \<in> r \<union> Id)"
haftmann@32883
    80
  by (simp add: mem_def pair_in_Id_conv [simplified mem_def] sup_fun_eq sup_bool_eq)
berghofe@22262
    81
wenzelm@12691
    82
lemma r_into_rtrancl [intro]: "!!p. p \<in> r ==> p \<in> r^*"
wenzelm@12691
    83
  -- {* @{text rtrancl} of @{text r} contains @{text r} *}
wenzelm@12691
    84
  apply (simp only: split_tupled_all)
wenzelm@12691
    85
  apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])
wenzelm@12691
    86
  done
wenzelm@12691
    87
berghofe@23743
    88
lemma r_into_rtranclp [intro]: "r x y ==> r^** x y"
berghofe@22262
    89
  -- {* @{text rtrancl} of @{text r} contains @{text r} *}
berghofe@23743
    90
  by (erule rtranclp.rtrancl_refl [THEN rtranclp.rtrancl_into_rtrancl])
berghofe@22262
    91
berghofe@23743
    92
lemma rtranclp_mono: "r \<le> s ==> r^** \<le> s^**"
wenzelm@12691
    93
  -- {* monotonicity of @{text rtrancl} *}
berghofe@22262
    94
  apply (rule predicate2I)
berghofe@23743
    95
  apply (erule rtranclp.induct)
berghofe@23743
    96
   apply (rule_tac [2] rtranclp.rtrancl_into_rtrancl, blast+)
wenzelm@12691
    97
  done
wenzelm@12691
    98
berghofe@23743
    99
lemmas rtrancl_mono = rtranclp_mono [to_set]
berghofe@22262
   100
wenzelm@26179
   101
theorem rtranclp_induct [consumes 1, case_names base step, induct set: rtranclp]:
berghofe@22262
   102
  assumes a: "r^** a b"
berghofe@22262
   103
    and cases: "P a" "!!y z. [| r^** a y; r y z; P y |] ==> P z"
wenzelm@12937
   104
  shows "P b"
wenzelm@12691
   105
proof -
wenzelm@12691
   106
  from a have "a = a --> P b"
nipkow@17589
   107
    by (induct "%x y. x = a --> P y" a b) (iprover intro: cases)+
wenzelm@26179
   108
  then show ?thesis by iprover
wenzelm@12691
   109
qed
wenzelm@12691
   110
berghofe@25425
   111
lemmas rtrancl_induct [induct set: rtrancl] = rtranclp_induct [to_set]
berghofe@22262
   112
berghofe@23743
   113
lemmas rtranclp_induct2 =
berghofe@23743
   114
  rtranclp_induct[of _ "(ax,ay)" "(bx,by)", split_rule,
berghofe@22262
   115
                 consumes 1, case_names refl step]
berghofe@22262
   116
nipkow@14404
   117
lemmas rtrancl_induct2 =
nipkow@14404
   118
  rtrancl_induct[of "(ax,ay)" "(bx,by)", split_format (complete),
nipkow@14404
   119
                 consumes 1, case_names refl step]
wenzelm@18372
   120
nipkow@30198
   121
lemma refl_rtrancl: "refl (r^*)"
nipkow@30198
   122
by (unfold refl_on_def) fast
huffman@19228
   123
wenzelm@26179
   124
text {* Transitivity of transitive closure. *}
wenzelm@26179
   125
lemma trans_rtrancl: "trans (r^*)"
berghofe@12823
   126
proof (rule transI)
berghofe@12823
   127
  fix x y z
berghofe@12823
   128
  assume "(x, y) \<in> r\<^sup>*"
berghofe@12823
   129
  assume "(y, z) \<in> r\<^sup>*"
wenzelm@26179
   130
  then show "(x, z) \<in> r\<^sup>*"
wenzelm@26179
   131
  proof induct
wenzelm@26179
   132
    case base
wenzelm@26179
   133
    show "(x, y) \<in> r\<^sup>*" by fact
wenzelm@26179
   134
  next
wenzelm@26179
   135
    case (step u v)
wenzelm@26179
   136
    from `(x, u) \<in> r\<^sup>*` and `(u, v) \<in> r`
wenzelm@26179
   137
    show "(x, v) \<in> r\<^sup>*" ..
wenzelm@26179
   138
  qed
berghofe@12823
   139
qed
wenzelm@12691
   140
wenzelm@12691
   141
lemmas rtrancl_trans = trans_rtrancl [THEN transD, standard]
wenzelm@12691
   142
berghofe@23743
   143
lemma rtranclp_trans:
berghofe@22262
   144
  assumes xy: "r^** x y"
berghofe@22262
   145
  and yz: "r^** y z"
berghofe@22262
   146
  shows "r^** x z" using yz xy
berghofe@22262
   147
  by induct iprover+
berghofe@22262
   148
wenzelm@26174
   149
lemma rtranclE [cases set: rtrancl]:
wenzelm@26174
   150
  assumes major: "(a::'a, b) : r^*"
wenzelm@26174
   151
  obtains
wenzelm@26174
   152
    (base) "a = b"
wenzelm@26174
   153
  | (step) y where "(a, y) : r^*" and "(y, b) : r"
wenzelm@12691
   154
  -- {* elimination of @{text rtrancl} -- by induction on a special formula *}
wenzelm@18372
   155
  apply (subgoal_tac "(a::'a) = b | (EX y. (a,y) : r^* & (y,b) : r)")
wenzelm@18372
   156
   apply (rule_tac [2] major [THEN rtrancl_induct])
wenzelm@18372
   157
    prefer 2 apply blast
wenzelm@18372
   158
   prefer 2 apply blast
wenzelm@26174
   159
  apply (erule asm_rl exE disjE conjE base step)+
wenzelm@18372
   160
  done
wenzelm@12691
   161
krauss@32235
   162
lemma rtrancl_Int_subset: "[| Id \<subseteq> s; (r^* \<inter> s) O r \<subseteq> s|] ==> r^* \<subseteq> s"
paulson@22080
   163
  apply (rule subsetI)
paulson@22080
   164
  apply (rule_tac p="x" in PairE, clarify)
paulson@22080
   165
  apply (erule rtrancl_induct, auto) 
paulson@22080
   166
  done
paulson@22080
   167
berghofe@23743
   168
lemma converse_rtranclp_into_rtranclp:
berghofe@22262
   169
  "r a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>*\<^sup>* a c"
berghofe@23743
   170
  by (rule rtranclp_trans) iprover+
berghofe@22262
   171
berghofe@23743
   172
lemmas converse_rtrancl_into_rtrancl = converse_rtranclp_into_rtranclp [to_set]
wenzelm@12691
   173
wenzelm@12691
   174
text {*
wenzelm@12691
   175
  \medskip More @{term "r^*"} equations and inclusions.
wenzelm@12691
   176
*}
wenzelm@12691
   177
berghofe@23743
   178
lemma rtranclp_idemp [simp]: "(r^**)^** = r^**"
berghofe@22262
   179
  apply (auto intro!: order_antisym)
berghofe@23743
   180
  apply (erule rtranclp_induct)
berghofe@23743
   181
   apply (rule rtranclp.rtrancl_refl)
berghofe@23743
   182
  apply (blast intro: rtranclp_trans)
wenzelm@12691
   183
  done
wenzelm@12691
   184
berghofe@23743
   185
lemmas rtrancl_idemp [simp] = rtranclp_idemp [to_set]
berghofe@22262
   186
wenzelm@12691
   187
lemma rtrancl_idemp_self_comp [simp]: "R^* O R^* = R^*"
wenzelm@12691
   188
  apply (rule set_ext)
wenzelm@12691
   189
  apply (simp only: split_tupled_all)
wenzelm@12691
   190
  apply (blast intro: rtrancl_trans)
wenzelm@12691
   191
  done
wenzelm@12691
   192
wenzelm@12691
   193
lemma rtrancl_subset_rtrancl: "r \<subseteq> s^* ==> r^* \<subseteq> s^*"
wenzelm@26179
   194
  apply (drule rtrancl_mono)
wenzelm@26179
   195
  apply simp
wenzelm@26179
   196
  done
wenzelm@12691
   197
berghofe@23743
   198
lemma rtranclp_subset: "R \<le> S ==> S \<le> R^** ==> S^** = R^**"
berghofe@23743
   199
  apply (drule rtranclp_mono)
wenzelm@26179
   200
  apply (drule rtranclp_mono)
wenzelm@26179
   201
  apply simp
wenzelm@12691
   202
  done
wenzelm@12691
   203
berghofe@23743
   204
lemmas rtrancl_subset = rtranclp_subset [to_set]
berghofe@22262
   205
berghofe@23743
   206
lemma rtranclp_sup_rtranclp: "(sup (R^**) (S^**))^** = (sup R S)^**"
berghofe@23743
   207
  by (blast intro!: rtranclp_subset intro: rtranclp_mono [THEN predicate2D])
wenzelm@12691
   208
berghofe@23743
   209
lemmas rtrancl_Un_rtrancl = rtranclp_sup_rtranclp [to_set]
berghofe@22262
   210
berghofe@23743
   211
lemma rtranclp_reflcl [simp]: "(R^==)^** = R^**"
berghofe@23743
   212
  by (blast intro!: rtranclp_subset)
berghofe@22262
   213
berghofe@23743
   214
lemmas rtrancl_reflcl [simp] = rtranclp_reflcl [to_set]
wenzelm@12691
   215
wenzelm@12691
   216
lemma rtrancl_r_diff_Id: "(r - Id)^* = r^*"
wenzelm@12691
   217
  apply (rule sym)
paulson@14208
   218
  apply (rule rtrancl_subset, blast, clarify)
wenzelm@12691
   219
  apply (rename_tac a b)
wenzelm@26179
   220
  apply (case_tac "a = b")
wenzelm@26179
   221
   apply blast
wenzelm@12691
   222
  apply (blast intro!: r_into_rtrancl)
wenzelm@12691
   223
  done
wenzelm@12691
   224
berghofe@23743
   225
lemma rtranclp_r_diff_Id: "(inf r op ~=)^** = r^**"
berghofe@22262
   226
  apply (rule sym)
berghofe@23743
   227
  apply (rule rtranclp_subset)
wenzelm@26179
   228
   apply blast+
berghofe@22262
   229
  done
berghofe@22262
   230
berghofe@23743
   231
theorem rtranclp_converseD:
berghofe@22262
   232
  assumes r: "(r^--1)^** x y"
berghofe@22262
   233
  shows "r^** y x"
berghofe@12823
   234
proof -
berghofe@12823
   235
  from r show ?thesis
berghofe@23743
   236
    by induct (iprover intro: rtranclp_trans dest!: conversepD)+
berghofe@12823
   237
qed
wenzelm@12691
   238
berghofe@23743
   239
lemmas rtrancl_converseD = rtranclp_converseD [to_set]
berghofe@22262
   240
berghofe@23743
   241
theorem rtranclp_converseI:
wenzelm@26179
   242
  assumes "r^** y x"
berghofe@22262
   243
  shows "(r^--1)^** x y"
wenzelm@26179
   244
  using assms
wenzelm@26179
   245
  by induct (iprover intro: rtranclp_trans conversepI)+
wenzelm@12691
   246
berghofe@23743
   247
lemmas rtrancl_converseI = rtranclp_converseI [to_set]
berghofe@22262
   248
wenzelm@12691
   249
lemma rtrancl_converse: "(r^-1)^* = (r^*)^-1"
wenzelm@12691
   250
  by (fast dest!: rtrancl_converseD intro!: rtrancl_converseI)
wenzelm@12691
   251
huffman@19228
   252
lemma sym_rtrancl: "sym r ==> sym (r^*)"
huffman@19228
   253
  by (simp only: sym_conv_converse_eq rtrancl_converse [symmetric])
huffman@19228
   254
berghofe@23743
   255
theorem converse_rtranclp_induct[consumes 1]:
berghofe@22262
   256
  assumes major: "r^** a b"
berghofe@22262
   257
    and cases: "P b" "!!y z. [| r y z; r^** z b; P z |] ==> P y"
wenzelm@12937
   258
  shows "P a"
wenzelm@26179
   259
  using rtranclp_converseI [OF major]
wenzelm@26179
   260
  by induct (iprover intro: cases dest!: conversepD rtranclp_converseD)+
wenzelm@12691
   261
berghofe@25425
   262
lemmas converse_rtrancl_induct = converse_rtranclp_induct [to_set]
berghofe@22262
   263
berghofe@23743
   264
lemmas converse_rtranclp_induct2 =
wenzelm@26179
   265
  converse_rtranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
berghofe@22262
   266
                 consumes 1, case_names refl step]
berghofe@22262
   267
nipkow@14404
   268
lemmas converse_rtrancl_induct2 =
wenzelm@26179
   269
  converse_rtrancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
nipkow@14404
   270
                 consumes 1, case_names refl step]
wenzelm@12691
   271
berghofe@23743
   272
lemma converse_rtranclpE:
berghofe@22262
   273
  assumes major: "r^** x z"
wenzelm@18372
   274
    and cases: "x=z ==> P"
berghofe@22262
   275
      "!!y. [| r x y; r^** y z |] ==> P"
wenzelm@18372
   276
  shows P
berghofe@22262
   277
  apply (subgoal_tac "x = z | (EX y. r x y & r^** y z)")
berghofe@23743
   278
   apply (rule_tac [2] major [THEN converse_rtranclp_induct])
wenzelm@18372
   279
    prefer 2 apply iprover
wenzelm@18372
   280
   prefer 2 apply iprover
wenzelm@18372
   281
  apply (erule asm_rl exE disjE conjE cases)+
wenzelm@18372
   282
  done
wenzelm@12691
   283
berghofe@23743
   284
lemmas converse_rtranclE = converse_rtranclpE [to_set]
berghofe@22262
   285
berghofe@23743
   286
lemmas converse_rtranclpE2 = converse_rtranclpE [of _ "(xa,xb)" "(za,zb)", split_rule]
berghofe@22262
   287
berghofe@22262
   288
lemmas converse_rtranclE2 = converse_rtranclE [of "(xa,xb)" "(za,zb)", split_rule]
wenzelm@12691
   289
wenzelm@12691
   290
lemma r_comp_rtrancl_eq: "r O r^* = r^* O r"
wenzelm@12691
   291
  by (blast elim: rtranclE converse_rtranclE
wenzelm@12691
   292
    intro: rtrancl_into_rtrancl converse_rtrancl_into_rtrancl)
wenzelm@12691
   293
krauss@32235
   294
lemma rtrancl_unfold: "r^* = Id Un r^* O r"
paulson@15551
   295
  by (auto intro: rtrancl_into_rtrancl elim: rtranclE)
paulson@15551
   296
nipkow@31690
   297
lemma rtrancl_Un_separatorE:
nipkow@31690
   298
  "(a,b) : (P \<union> Q)^* \<Longrightarrow> \<forall>x y. (a,x) : P^* \<longrightarrow> (x,y) : Q \<longrightarrow> x=y \<Longrightarrow> (a,b) : P^*"
nipkow@31690
   299
apply (induct rule:rtrancl.induct)
nipkow@31690
   300
 apply blast
nipkow@31690
   301
apply (blast intro:rtrancl_trans)
nipkow@31690
   302
done
nipkow@31690
   303
nipkow@31690
   304
lemma rtrancl_Un_separator_converseE:
nipkow@31690
   305
  "(a,b) : (P \<union> Q)^* \<Longrightarrow> \<forall>x y. (x,b) : P^* \<longrightarrow> (y,x) : Q \<longrightarrow> y=x \<Longrightarrow> (a,b) : P^*"
nipkow@31690
   306
apply (induct rule:converse_rtrancl_induct)
nipkow@31690
   307
 apply blast
nipkow@31690
   308
apply (blast intro:rtrancl_trans)
nipkow@31690
   309
done
nipkow@31690
   310
wenzelm@12691
   311
wenzelm@12691
   312
subsection {* Transitive closure *}
wenzelm@10331
   313
berghofe@13704
   314
lemma trancl_mono: "!!p. p \<in> r^+ ==> r \<subseteq> s ==> p \<in> s^+"
berghofe@23743
   315
  apply (simp add: split_tupled_all)
berghofe@13704
   316
  apply (erule trancl.induct)
wenzelm@26179
   317
   apply (iprover dest: subsetD)+
wenzelm@12691
   318
  done
wenzelm@12691
   319
berghofe@13704
   320
lemma r_into_trancl': "!!p. p : r ==> p : r^+"
berghofe@13704
   321
  by (simp only: split_tupled_all) (erule r_into_trancl)
berghofe@13704
   322
wenzelm@12691
   323
text {*
wenzelm@12691
   324
  \medskip Conversions between @{text trancl} and @{text rtrancl}.
wenzelm@12691
   325
*}
wenzelm@12691
   326
berghofe@23743
   327
lemma tranclp_into_rtranclp: "r^++ a b ==> r^** a b"
berghofe@23743
   328
  by (erule tranclp.induct) iprover+
wenzelm@12691
   329
berghofe@23743
   330
lemmas trancl_into_rtrancl = tranclp_into_rtranclp [to_set]
berghofe@22262
   331
berghofe@23743
   332
lemma rtranclp_into_tranclp1: assumes r: "r^** a b"
berghofe@22262
   333
  shows "!!c. r b c ==> r^++ a c" using r
nipkow@17589
   334
  by induct iprover+
wenzelm@12691
   335
berghofe@23743
   336
lemmas rtrancl_into_trancl1 = rtranclp_into_tranclp1 [to_set]
berghofe@22262
   337
berghofe@23743
   338
lemma rtranclp_into_tranclp2: "[| r a b; r^** b c |] ==> r^++ a c"
wenzelm@12691
   339
  -- {* intro rule from @{text r} and @{text rtrancl} *}
wenzelm@26179
   340
  apply (erule rtranclp.cases)
wenzelm@26179
   341
   apply iprover
berghofe@23743
   342
  apply (rule rtranclp_trans [THEN rtranclp_into_tranclp1])
wenzelm@26179
   343
    apply (simp | rule r_into_rtranclp)+
wenzelm@12691
   344
  done
wenzelm@12691
   345
berghofe@23743
   346
lemmas rtrancl_into_trancl2 = rtranclp_into_tranclp2 [to_set]
berghofe@22262
   347
wenzelm@26179
   348
text {* Nice induction rule for @{text trancl} *}
wenzelm@26179
   349
lemma tranclp_induct [consumes 1, case_names base step, induct pred: tranclp]:
wenzelm@26179
   350
  assumes "r^++ a b"
berghofe@22262
   351
  and cases: "!!y. r a y ==> P y"
berghofe@22262
   352
    "!!y z. r^++ a y ==> r y z ==> P y ==> P z"
berghofe@13704
   353
  shows "P b"
wenzelm@12691
   354
proof -
wenzelm@26179
   355
  from `r^++ a b` have "a = a --> P b"
nipkow@17589
   356
    by (induct "%x y. x = a --> P y" a b) (iprover intro: cases)+
wenzelm@26179
   357
  then show ?thesis by iprover
wenzelm@12691
   358
qed
wenzelm@12691
   359
berghofe@25425
   360
lemmas trancl_induct [induct set: trancl] = tranclp_induct [to_set]
berghofe@22262
   361
berghofe@23743
   362
lemmas tranclp_induct2 =
wenzelm@26179
   363
  tranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
wenzelm@26179
   364
    consumes 1, case_names base step]
berghofe@22262
   365
paulson@22172
   366
lemmas trancl_induct2 =
wenzelm@26179
   367
  trancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
wenzelm@26179
   368
    consumes 1, case_names base step]
paulson@22172
   369
berghofe@23743
   370
lemma tranclp_trans_induct:
berghofe@22262
   371
  assumes major: "r^++ x y"
berghofe@22262
   372
    and cases: "!!x y. r x y ==> P x y"
berghofe@22262
   373
      "!!x y z. [| r^++ x y; P x y; r^++ y z; P y z |] ==> P x z"
wenzelm@18372
   374
  shows "P x y"
wenzelm@12691
   375
  -- {* Another induction rule for trancl, incorporating transitivity *}
berghofe@23743
   376
  by (iprover intro: major [THEN tranclp_induct] cases)
wenzelm@12691
   377
berghofe@23743
   378
lemmas trancl_trans_induct = tranclp_trans_induct [to_set]
berghofe@23743
   379
wenzelm@26174
   380
lemma tranclE [cases set: trancl]:
wenzelm@26174
   381
  assumes "(a, b) : r^+"
wenzelm@26174
   382
  obtains
wenzelm@26174
   383
    (base) "(a, b) : r"
wenzelm@26174
   384
  | (step) c where "(a, c) : r^+" and "(c, b) : r"
wenzelm@26174
   385
  using assms by cases simp_all
wenzelm@10980
   386
krauss@32235
   387
lemma trancl_Int_subset: "[| r \<subseteq> s; (r^+ \<inter> s) O r \<subseteq> s|] ==> r^+ \<subseteq> s"
paulson@22080
   388
  apply (rule subsetI)
wenzelm@26179
   389
  apply (rule_tac p = x in PairE)
wenzelm@26179
   390
  apply clarify
wenzelm@26179
   391
  apply (erule trancl_induct)
wenzelm@26179
   392
   apply auto
paulson@22080
   393
  done
paulson@22080
   394
krauss@32235
   395
lemma trancl_unfold: "r^+ = r Un r^+ O r"
paulson@15551
   396
  by (auto intro: trancl_into_trancl elim: tranclE)
paulson@15551
   397
wenzelm@26179
   398
text {* Transitivity of @{term "r^+"} *}
wenzelm@26179
   399
lemma trans_trancl [simp]: "trans (r^+)"
berghofe@13704
   400
proof (rule transI)
berghofe@13704
   401
  fix x y z
wenzelm@26179
   402
  assume "(x, y) \<in> r^+"
berghofe@13704
   403
  assume "(y, z) \<in> r^+"
wenzelm@26179
   404
  then show "(x, z) \<in> r^+"
wenzelm@26179
   405
  proof induct
wenzelm@26179
   406
    case (base u)
wenzelm@26179
   407
    from `(x, y) \<in> r^+` and `(y, u) \<in> r`
wenzelm@26179
   408
    show "(x, u) \<in> r^+" ..
wenzelm@26179
   409
  next
wenzelm@26179
   410
    case (step u v)
wenzelm@26179
   411
    from `(x, u) \<in> r^+` and `(u, v) \<in> r`
wenzelm@26179
   412
    show "(x, v) \<in> r^+" ..
wenzelm@26179
   413
  qed
berghofe@13704
   414
qed
wenzelm@12691
   415
wenzelm@12691
   416
lemmas trancl_trans = trans_trancl [THEN transD, standard]
wenzelm@12691
   417
berghofe@23743
   418
lemma tranclp_trans:
berghofe@22262
   419
  assumes xy: "r^++ x y"
berghofe@22262
   420
  and yz: "r^++ y z"
berghofe@22262
   421
  shows "r^++ x z" using yz xy
berghofe@22262
   422
  by induct iprover+
berghofe@22262
   423
wenzelm@26179
   424
lemma trancl_id [simp]: "trans r \<Longrightarrow> r^+ = r"
wenzelm@26179
   425
  apply auto
wenzelm@26179
   426
  apply (erule trancl_induct)
wenzelm@26179
   427
   apply assumption
wenzelm@26179
   428
  apply (unfold trans_def)
wenzelm@26179
   429
  apply blast
wenzelm@26179
   430
  done
nipkow@19623
   431
wenzelm@26179
   432
lemma rtranclp_tranclp_tranclp:
wenzelm@26179
   433
  assumes "r^** x y"
wenzelm@26179
   434
  shows "!!z. r^++ y z ==> r^++ x z" using assms
berghofe@23743
   435
  by induct (iprover intro: tranclp_trans)+
wenzelm@12691
   436
berghofe@23743
   437
lemmas rtrancl_trancl_trancl = rtranclp_tranclp_tranclp [to_set]
berghofe@22262
   438
berghofe@23743
   439
lemma tranclp_into_tranclp2: "r a b ==> r^++ b c ==> r^++ a c"
berghofe@23743
   440
  by (erule tranclp_trans [OF tranclp.r_into_trancl])
berghofe@22262
   441
berghofe@23743
   442
lemmas trancl_into_trancl2 = tranclp_into_tranclp2 [to_set]
wenzelm@12691
   443
wenzelm@12691
   444
lemma trancl_insert:
wenzelm@12691
   445
  "(insert (y, x) r)^+ = r^+ \<union> {(a, b). (a, y) \<in> r^* \<and> (x, b) \<in> r^*}"
wenzelm@12691
   446
  -- {* primitive recursion for @{text trancl} over finite relations *}
wenzelm@12691
   447
  apply (rule equalityI)
wenzelm@12691
   448
   apply (rule subsetI)
wenzelm@12691
   449
   apply (simp only: split_tupled_all)
paulson@14208
   450
   apply (erule trancl_induct, blast)
wenzelm@12691
   451
   apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl r_into_trancl trancl_trans)
wenzelm@12691
   452
  apply (rule subsetI)
wenzelm@12691
   453
  apply (blast intro: trancl_mono rtrancl_mono
wenzelm@12691
   454
    [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)
wenzelm@12691
   455
  done
wenzelm@12691
   456
berghofe@23743
   457
lemma tranclp_converseI: "(r^++)^--1 x y ==> (r^--1)^++ x y"
berghofe@22262
   458
  apply (drule conversepD)
berghofe@23743
   459
  apply (erule tranclp_induct)
berghofe@23743
   460
  apply (iprover intro: conversepI tranclp_trans)+
wenzelm@12691
   461
  done
wenzelm@12691
   462
berghofe@23743
   463
lemmas trancl_converseI = tranclp_converseI [to_set]
berghofe@22262
   464
berghofe@23743
   465
lemma tranclp_converseD: "(r^--1)^++ x y ==> (r^++)^--1 x y"
berghofe@22262
   466
  apply (rule conversepI)
berghofe@23743
   467
  apply (erule tranclp_induct)
berghofe@23743
   468
  apply (iprover dest: conversepD intro: tranclp_trans)+
berghofe@13704
   469
  done
wenzelm@12691
   470
berghofe@23743
   471
lemmas trancl_converseD = tranclp_converseD [to_set]
berghofe@22262
   472
berghofe@23743
   473
lemma tranclp_converse: "(r^--1)^++ = (r^++)^--1"
berghofe@22262
   474
  by (fastsimp simp add: expand_fun_eq
berghofe@23743
   475
    intro!: tranclp_converseI dest!: tranclp_converseD)
berghofe@22262
   476
berghofe@23743
   477
lemmas trancl_converse = tranclp_converse [to_set]
wenzelm@12691
   478
huffman@19228
   479
lemma sym_trancl: "sym r ==> sym (r^+)"
huffman@19228
   480
  by (simp only: sym_conv_converse_eq trancl_converse [symmetric])
huffman@19228
   481
berghofe@23743
   482
lemma converse_tranclp_induct:
berghofe@22262
   483
  assumes major: "r^++ a b"
berghofe@22262
   484
    and cases: "!!y. r y b ==> P(y)"
berghofe@22262
   485
      "!!y z.[| r y z;  r^++ z b;  P(z) |] ==> P(y)"
wenzelm@18372
   486
  shows "P a"
berghofe@23743
   487
  apply (rule tranclp_induct [OF tranclp_converseI, OF conversepI, OF major])
wenzelm@18372
   488
   apply (rule cases)
berghofe@22262
   489
   apply (erule conversepD)
berghofe@23743
   490
  apply (blast intro: prems dest!: tranclp_converseD conversepD)
wenzelm@18372
   491
  done
wenzelm@12691
   492
berghofe@23743
   493
lemmas converse_trancl_induct = converse_tranclp_induct [to_set]
berghofe@22262
   494
berghofe@23743
   495
lemma tranclpD: "R^++ x y ==> EX z. R x z \<and> R^** z y"
wenzelm@26179
   496
  apply (erule converse_tranclp_induct)
wenzelm@26179
   497
   apply auto
berghofe@23743
   498
  apply (blast intro: rtranclp_trans)
wenzelm@12691
   499
  done
wenzelm@12691
   500
berghofe@23743
   501
lemmas tranclD = tranclpD [to_set]
berghofe@22262
   502
bulwahn@31577
   503
lemma converse_tranclpE:
bulwahn@31577
   504
  assumes major: "tranclp r x z"
bulwahn@31577
   505
  assumes base: "r x z ==> P"
bulwahn@31577
   506
  assumes step: "\<And> y. [| r x y; tranclp r y z |] ==> P"
bulwahn@31577
   507
  shows P
bulwahn@31577
   508
proof -
bulwahn@31577
   509
  from tranclpD[OF major]
bulwahn@31577
   510
  obtain y where "r x y" and "rtranclp r y z" by iprover
bulwahn@31577
   511
  from this(2) show P
bulwahn@31577
   512
  proof (cases rule: rtranclp.cases)
bulwahn@31577
   513
    case rtrancl_refl
bulwahn@31577
   514
    with `r x y` base show P by iprover
bulwahn@31577
   515
  next
bulwahn@31577
   516
    case rtrancl_into_rtrancl
bulwahn@31577
   517
    from this have "tranclp r y z"
bulwahn@31577
   518
      by (iprover intro: rtranclp_into_tranclp1)
bulwahn@31577
   519
    with `r x y` step show P by iprover
bulwahn@31577
   520
  qed
bulwahn@31577
   521
qed
bulwahn@31577
   522
bulwahn@31577
   523
lemmas converse_tranclE = converse_tranclpE [to_set]
bulwahn@31577
   524
kleing@25295
   525
lemma tranclD2:
kleing@25295
   526
  "(x, y) \<in> R\<^sup>+ \<Longrightarrow> \<exists>z. (x, z) \<in> R\<^sup>* \<and> (z, y) \<in> R"
kleing@25295
   527
  by (blast elim: tranclE intro: trancl_into_rtrancl)
kleing@25295
   528
nipkow@13867
   529
lemma irrefl_tranclI: "r^-1 \<inter> r^* = {} ==> (x, x) \<notin> r^+"
wenzelm@18372
   530
  by (blast elim: tranclE dest: trancl_into_rtrancl)
wenzelm@12691
   531
wenzelm@12691
   532
lemma irrefl_trancl_rD: "!!X. ALL x. (x, x) \<notin> r^+ ==> (x, y) \<in> r ==> x \<noteq> y"
wenzelm@12691
   533
  by (blast dest: r_into_trancl)
wenzelm@12691
   534
wenzelm@12691
   535
lemma trancl_subset_Sigma_aux:
wenzelm@12691
   536
    "(a, b) \<in> r^* ==> r \<subseteq> A \<times> A ==> a = b \<or> a \<in> A"
wenzelm@18372
   537
  by (induct rule: rtrancl_induct) auto
wenzelm@12691
   538
wenzelm@12691
   539
lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A ==> r^+ \<subseteq> A \<times> A"
berghofe@13704
   540
  apply (rule subsetI)
berghofe@13704
   541
  apply (simp only: split_tupled_all)
berghofe@13704
   542
  apply (erule tranclE)
wenzelm@26179
   543
   apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+
wenzelm@12691
   544
  done
nipkow@10996
   545
berghofe@23743
   546
lemma reflcl_tranclp [simp]: "(r^++)^== = r^**"
berghofe@22262
   547
  apply (safe intro!: order_antisym)
berghofe@23743
   548
   apply (erule tranclp_into_rtranclp)
berghofe@23743
   549
  apply (blast elim: rtranclp.cases dest: rtranclp_into_tranclp1)
wenzelm@11084
   550
  done
nipkow@10996
   551
berghofe@23743
   552
lemmas reflcl_trancl [simp] = reflcl_tranclp [to_set]
berghofe@22262
   553
wenzelm@11090
   554
lemma trancl_reflcl [simp]: "(r^=)^+ = r^*"
wenzelm@11084
   555
  apply safe
paulson@14208
   556
   apply (drule trancl_into_rtrancl, simp)
paulson@14208
   557
  apply (erule rtranclE, safe)
paulson@14208
   558
   apply (rule r_into_trancl, simp)
wenzelm@11084
   559
  apply (rule rtrancl_into_trancl1)
paulson@14208
   560
   apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD], fast)
wenzelm@11084
   561
  done
nipkow@10996
   562
wenzelm@11090
   563
lemma trancl_empty [simp]: "{}^+ = {}"
wenzelm@11084
   564
  by (auto elim: trancl_induct)
nipkow@10996
   565
wenzelm@11090
   566
lemma rtrancl_empty [simp]: "{}^* = Id"
wenzelm@11084
   567
  by (rule subst [OF reflcl_trancl]) simp
nipkow@10996
   568
berghofe@23743
   569
lemma rtranclpD: "R^** a b ==> a = b \<or> a \<noteq> b \<and> R^++ a b"
berghofe@23743
   570
  by (force simp add: reflcl_tranclp [symmetric] simp del: reflcl_tranclp)
berghofe@22262
   571
berghofe@23743
   572
lemmas rtranclD = rtranclpD [to_set]
wenzelm@11084
   573
kleing@16514
   574
lemma rtrancl_eq_or_trancl:
kleing@16514
   575
  "(x,y) \<in> R\<^sup>* = (x=y \<or> x\<noteq>y \<and> (x,y) \<in> R\<^sup>+)"
kleing@16514
   576
  by (fast elim: trancl_into_rtrancl dest: rtranclD)
nipkow@10996
   577
wenzelm@12691
   578
text {* @{text Domain} and @{text Range} *}
nipkow@10996
   579
wenzelm@11090
   580
lemma Domain_rtrancl [simp]: "Domain (R^*) = UNIV"
wenzelm@11084
   581
  by blast
nipkow@10996
   582
wenzelm@11090
   583
lemma Range_rtrancl [simp]: "Range (R^*) = UNIV"
wenzelm@11084
   584
  by blast
nipkow@10996
   585
wenzelm@11090
   586
lemma rtrancl_Un_subset: "(R^* \<union> S^*) \<subseteq> (R Un S)^*"
wenzelm@11084
   587
  by (rule rtrancl_Un_rtrancl [THEN subst]) fast
nipkow@10996
   588
wenzelm@11090
   589
lemma in_rtrancl_UnI: "x \<in> R^* \<or> x \<in> S^* ==> x \<in> (R \<union> S)^*"
wenzelm@11084
   590
  by (blast intro: subsetD [OF rtrancl_Un_subset])
nipkow@10996
   591
wenzelm@11090
   592
lemma trancl_domain [simp]: "Domain (r^+) = Domain r"
wenzelm@11084
   593
  by (unfold Domain_def) (blast dest: tranclD)
nipkow@10996
   594
wenzelm@11090
   595
lemma trancl_range [simp]: "Range (r^+) = Range r"
nipkow@26271
   596
unfolding Range_def by(simp add: trancl_converse [symmetric])
nipkow@10996
   597
paulson@11115
   598
lemma Not_Domain_rtrancl:
wenzelm@12691
   599
    "x ~: Domain R ==> ((x, y) : R^*) = (x = y)"
wenzelm@12691
   600
  apply auto
wenzelm@26179
   601
  apply (erule rev_mp)
wenzelm@26179
   602
  apply (erule rtrancl_induct)
wenzelm@26179
   603
   apply auto
wenzelm@26179
   604
  done
berghofe@11327
   605
haftmann@29609
   606
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
haftmann@29609
   607
  apply clarify
haftmann@29609
   608
  apply (erule trancl_induct)
haftmann@29609
   609
   apply (auto simp add: Field_def)
haftmann@29609
   610
  done
haftmann@29609
   611
haftmann@29609
   612
lemma finite_trancl: "finite (r^+) = finite r"
haftmann@29609
   613
  apply auto
haftmann@29609
   614
   prefer 2
haftmann@29609
   615
   apply (rule trancl_subset_Field2 [THEN finite_subset])
haftmann@29609
   616
   apply (rule finite_SigmaI)
haftmann@29609
   617
    prefer 3
haftmann@29609
   618
    apply (blast intro: r_into_trancl' finite_subset)
haftmann@29609
   619
   apply (auto simp add: finite_Field)
haftmann@29609
   620
  done
haftmann@29609
   621
wenzelm@12691
   622
text {* More about converse @{text rtrancl} and @{text trancl}, should
wenzelm@12691
   623
  be merged with main body. *}
kleing@12428
   624
nipkow@14337
   625
lemma single_valued_confluent:
nipkow@14337
   626
  "\<lbrakk> single_valued r; (x,y) \<in> r^*; (x,z) \<in> r^* \<rbrakk>
nipkow@14337
   627
  \<Longrightarrow> (y,z) \<in> r^* \<or> (z,y) \<in> r^*"
wenzelm@26179
   628
  apply (erule rtrancl_induct)
wenzelm@26179
   629
  apply simp
wenzelm@26179
   630
  apply (erule disjE)
wenzelm@26179
   631
   apply (blast elim:converse_rtranclE dest:single_valuedD)
wenzelm@26179
   632
  apply(blast intro:rtrancl_trans)
wenzelm@26179
   633
  done
nipkow@14337
   634
wenzelm@12691
   635
lemma r_r_into_trancl: "(a, b) \<in> R ==> (b, c) \<in> R ==> (a, c) \<in> R^+"
kleing@12428
   636
  by (fast intro: trancl_trans)
kleing@12428
   637
kleing@12428
   638
lemma trancl_into_trancl [rule_format]:
wenzelm@12691
   639
    "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r --> (a,c) \<in> r\<^sup>+"
wenzelm@12691
   640
  apply (erule trancl_induct)
kleing@12428
   641
   apply (fast intro: r_r_into_trancl)
kleing@12428
   642
  apply (fast intro: r_r_into_trancl trancl_trans)
kleing@12428
   643
  done
kleing@12428
   644
berghofe@23743
   645
lemma tranclp_rtranclp_tranclp:
berghofe@22262
   646
    "r\<^sup>+\<^sup>+ a b ==> r\<^sup>*\<^sup>* b c ==> r\<^sup>+\<^sup>+ a c"
berghofe@23743
   647
  apply (drule tranclpD)
wenzelm@26179
   648
  apply (elim exE conjE)
berghofe@23743
   649
  apply (drule rtranclp_trans, assumption)
berghofe@23743
   650
  apply (drule rtranclp_into_tranclp2, assumption, assumption)
kleing@12428
   651
  done
kleing@12428
   652
berghofe@23743
   653
lemmas trancl_rtrancl_trancl = tranclp_rtranclp_tranclp [to_set]
berghofe@22262
   654
wenzelm@12691
   655
lemmas transitive_closure_trans [trans] =
wenzelm@12691
   656
  r_r_into_trancl trancl_trans rtrancl_trans
berghofe@23743
   657
  trancl.trancl_into_trancl trancl_into_trancl2
berghofe@23743
   658
  rtrancl.rtrancl_into_rtrancl converse_rtrancl_into_rtrancl
wenzelm@12691
   659
  rtrancl_trancl_trancl trancl_rtrancl_trancl
kleing@12428
   660
berghofe@23743
   661
lemmas transitive_closurep_trans' [trans] =
berghofe@23743
   662
  tranclp_trans rtranclp_trans
berghofe@23743
   663
  tranclp.trancl_into_trancl tranclp_into_tranclp2
berghofe@23743
   664
  rtranclp.rtrancl_into_rtrancl converse_rtranclp_into_rtranclp
berghofe@23743
   665
  rtranclp_tranclp_tranclp tranclp_rtranclp_tranclp
berghofe@22262
   666
kleing@12428
   667
declare trancl_into_rtrancl [elim]
berghofe@11327
   668
haftmann@30954
   669
subsection {* The power operation on relations *}
haftmann@30954
   670
haftmann@30954
   671
text {* @{text "R ^^ n = R O ... O R"}, the n-fold composition of @{text R} *}
haftmann@30954
   672
haftmann@30971
   673
overloading
haftmann@30971
   674
  relpow == "compow :: nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"
haftmann@30971
   675
begin
haftmann@30954
   676
haftmann@30971
   677
primrec relpow :: "nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" where
haftmann@30971
   678
    "relpow 0 R = Id"
krauss@32235
   679
  | "relpow (Suc n) R = (R ^^ n) O R"
haftmann@30954
   680
haftmann@30971
   681
end
haftmann@30954
   682
haftmann@30954
   683
lemma rel_pow_1 [simp]:
haftmann@30971
   684
  fixes R :: "('a \<times> 'a) set"
haftmann@30971
   685
  shows "R ^^ 1 = R"
haftmann@30954
   686
  by simp
haftmann@30954
   687
haftmann@30954
   688
lemma rel_pow_0_I: 
haftmann@30954
   689
  "(x, x) \<in> R ^^ 0"
haftmann@30954
   690
  by simp
haftmann@30954
   691
haftmann@30954
   692
lemma rel_pow_Suc_I:
haftmann@30954
   693
  "(x, y) \<in>  R ^^ n \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
haftmann@30954
   694
  by auto
haftmann@30954
   695
haftmann@30954
   696
lemma rel_pow_Suc_I2:
haftmann@30954
   697
  "(x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ n \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
haftmann@30954
   698
  by (induct n arbitrary: z) (simp, fastsimp)
haftmann@30954
   699
haftmann@30954
   700
lemma rel_pow_0_E:
haftmann@30954
   701
  "(x, y) \<in> R ^^ 0 \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30954
   702
  by simp
haftmann@30954
   703
haftmann@30954
   704
lemma rel_pow_Suc_E:
haftmann@30954
   705
  "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<And>y. (x, y) \<in> R ^^ n \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30954
   706
  by auto
haftmann@30954
   707
haftmann@30954
   708
lemma rel_pow_E:
haftmann@30954
   709
  "(x, z) \<in>  R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
haftmann@30954
   710
   \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in>  R ^^ m \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> P)
haftmann@30954
   711
   \<Longrightarrow> P"
haftmann@30954
   712
  by (cases n) auto
haftmann@30954
   713
haftmann@30954
   714
lemma rel_pow_Suc_D2:
haftmann@30954
   715
  "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<exists>y. (x, y) \<in> R \<and> (y, z) \<in> R ^^ n)"
haftmann@30954
   716
  apply (induct n arbitrary: x z)
haftmann@30954
   717
   apply (blast intro: rel_pow_0_I elim: rel_pow_0_E rel_pow_Suc_E)
haftmann@30954
   718
  apply (blast intro: rel_pow_Suc_I elim: rel_pow_0_E rel_pow_Suc_E)
haftmann@30954
   719
  done
haftmann@30954
   720
haftmann@30954
   721
lemma rel_pow_Suc_E2:
haftmann@30954
   722
  "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<And>y. (x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ n \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30954
   723
  by (blast dest: rel_pow_Suc_D2)
haftmann@30954
   724
haftmann@30954
   725
lemma rel_pow_Suc_D2':
haftmann@30954
   726
  "\<forall>x y z. (x, y) \<in> R ^^ n \<and> (y, z) \<in> R \<longrightarrow> (\<exists>w. (x, w) \<in> R \<and> (w, z) \<in> R ^^ n)"
haftmann@30954
   727
  by (induct n) (simp_all, blast)
haftmann@30954
   728
haftmann@30954
   729
lemma rel_pow_E2:
haftmann@30954
   730
  "(x, z) \<in> R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
haftmann@30954
   731
     \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ m \<Longrightarrow> P)
haftmann@30954
   732
   \<Longrightarrow> P"
haftmann@30954
   733
  apply (cases n, simp)
haftmann@30954
   734
  apply (cut_tac n=nat and R=R in rel_pow_Suc_D2', simp, blast)
haftmann@30954
   735
  done
haftmann@30954
   736
krauss@32235
   737
lemma rel_pow_add: "R ^^ (m+n) = R^^m O R^^n"
nipkow@31351
   738
by(induct n) auto
nipkow@31351
   739
krauss@31970
   740
lemma rel_pow_commute: "R O R ^^ n = R ^^ n O R"
krauss@32235
   741
by (induct n) (simp, simp add: O_assoc [symmetric])
krauss@31970
   742
haftmann@30954
   743
lemma rtrancl_imp_UN_rel_pow:
haftmann@30954
   744
  assumes "p \<in> R^*"
haftmann@30954
   745
  shows "p \<in> (\<Union>n. R ^^ n)"
haftmann@30954
   746
proof (cases p)
haftmann@30954
   747
  case (Pair x y)
haftmann@30954
   748
  with assms have "(x, y) \<in> R^*" by simp
haftmann@30954
   749
  then have "(x, y) \<in> (\<Union>n. R ^^ n)" proof induct
haftmann@30954
   750
    case base show ?case by (blast intro: rel_pow_0_I)
haftmann@30954
   751
  next
haftmann@30954
   752
    case step then show ?case by (blast intro: rel_pow_Suc_I)
haftmann@30954
   753
  qed
haftmann@30954
   754
  with Pair show ?thesis by simp
haftmann@30954
   755
qed
haftmann@30954
   756
haftmann@30954
   757
lemma rel_pow_imp_rtrancl:
haftmann@30954
   758
  assumes "p \<in> R ^^ n"
haftmann@30954
   759
  shows "p \<in> R^*"
haftmann@30954
   760
proof (cases p)
haftmann@30954
   761
  case (Pair x y)
haftmann@30954
   762
  with assms have "(x, y) \<in> R ^^ n" by simp
haftmann@30954
   763
  then have "(x, y) \<in> R^*" proof (induct n arbitrary: x y)
haftmann@30954
   764
    case 0 then show ?case by simp
haftmann@30954
   765
  next
haftmann@30954
   766
    case Suc then show ?case
haftmann@30954
   767
      by (blast elim: rel_pow_Suc_E intro: rtrancl_into_rtrancl)
haftmann@30954
   768
  qed
haftmann@30954
   769
  with Pair show ?thesis by simp
haftmann@30954
   770
qed
haftmann@30954
   771
haftmann@30954
   772
lemma rtrancl_is_UN_rel_pow:
haftmann@30954
   773
  "R^* = (\<Union>n. R ^^ n)"
haftmann@30954
   774
  by (blast intro: rtrancl_imp_UN_rel_pow rel_pow_imp_rtrancl)
haftmann@30954
   775
haftmann@30954
   776
lemma rtrancl_power:
haftmann@30954
   777
  "p \<in> R^* \<longleftrightarrow> (\<exists>n. p \<in> R ^^ n)"
haftmann@30954
   778
  by (simp add: rtrancl_is_UN_rel_pow)
haftmann@30954
   779
haftmann@30954
   780
lemma trancl_power:
haftmann@30954
   781
  "p \<in> R^+ \<longleftrightarrow> (\<exists>n > 0. p \<in> R ^^ n)"
haftmann@30954
   782
  apply (cases p)
haftmann@30954
   783
  apply simp
haftmann@30954
   784
  apply (rule iffI)
haftmann@30954
   785
   apply (drule tranclD2)
haftmann@30954
   786
   apply (clarsimp simp: rtrancl_is_UN_rel_pow)
haftmann@30971
   787
   apply (rule_tac x="Suc n" in exI)
haftmann@30954
   788
   apply (clarsimp simp: rel_comp_def)
haftmann@30954
   789
   apply fastsimp
haftmann@30954
   790
  apply clarsimp
haftmann@30954
   791
  apply (case_tac n, simp)
haftmann@30954
   792
  apply clarsimp
haftmann@30954
   793
  apply (drule rel_pow_imp_rtrancl)
haftmann@30954
   794
  apply (drule rtrancl_into_trancl1) apply auto
haftmann@30954
   795
  done
haftmann@30954
   796
haftmann@30954
   797
lemma rtrancl_imp_rel_pow:
haftmann@30954
   798
  "p \<in> R^* \<Longrightarrow> \<exists>n. p \<in> R ^^ n"
haftmann@30954
   799
  by (auto dest: rtrancl_imp_UN_rel_pow)
haftmann@30954
   800
haftmann@30954
   801
lemma single_valued_rel_pow:
haftmann@30954
   802
  fixes R :: "('a * 'a) set"
haftmann@30954
   803
  shows "single_valued R \<Longrightarrow> single_valued (R ^^ n)"
haftmann@30954
   804
  apply (induct n arbitrary: R)
haftmann@30954
   805
  apply simp_all
haftmann@30954
   806
  apply (rule single_valuedI)
haftmann@30954
   807
  apply (fast dest: single_valuedD elim: rel_pow_Suc_E)
haftmann@30954
   808
  done
paulson@15551
   809
ballarin@15076
   810
subsection {* Setup of transitivity reasoner *}
ballarin@15076
   811
wenzelm@26340
   812
ML {*
ballarin@15076
   813
wenzelm@32215
   814
structure Trancl_Tac = Trancl_Tac
wenzelm@32215
   815
(
wenzelm@32215
   816
  val r_into_trancl = @{thm trancl.r_into_trancl};
wenzelm@32215
   817
  val trancl_trans  = @{thm trancl_trans};
wenzelm@32215
   818
  val rtrancl_refl = @{thm rtrancl.rtrancl_refl};
wenzelm@32215
   819
  val r_into_rtrancl = @{thm r_into_rtrancl};
wenzelm@32215
   820
  val trancl_into_rtrancl = @{thm trancl_into_rtrancl};
wenzelm@32215
   821
  val rtrancl_trancl_trancl = @{thm rtrancl_trancl_trancl};
wenzelm@32215
   822
  val trancl_rtrancl_trancl = @{thm trancl_rtrancl_trancl};
wenzelm@32215
   823
  val rtrancl_trans = @{thm rtrancl_trans};
ballarin@15096
   824
berghofe@30107
   825
  fun decomp (@{const Trueprop} $ t) =
wenzelm@18372
   826
    let fun dec (Const ("op :", _) $ (Const ("Pair", _) $ a $ b) $ rel ) =
berghofe@23743
   827
        let fun decr (Const ("Transitive_Closure.rtrancl", _ ) $ r) = (r,"r*")
berghofe@23743
   828
              | decr (Const ("Transitive_Closure.trancl", _ ) $ r)  = (r,"r+")
wenzelm@18372
   829
              | decr r = (r,"r");
berghofe@26801
   830
            val (rel,r) = decr (Envir.beta_eta_contract rel);
wenzelm@18372
   831
        in SOME (a,b,rel,r) end
wenzelm@18372
   832
      | dec _ =  NONE
berghofe@30107
   833
    in dec t end
berghofe@30107
   834
    | decomp _ = NONE;
wenzelm@32215
   835
);
ballarin@15076
   836
wenzelm@32215
   837
structure Tranclp_Tac = Trancl_Tac
wenzelm@32215
   838
(
wenzelm@32215
   839
  val r_into_trancl = @{thm tranclp.r_into_trancl};
wenzelm@32215
   840
  val trancl_trans  = @{thm tranclp_trans};
wenzelm@32215
   841
  val rtrancl_refl = @{thm rtranclp.rtrancl_refl};
wenzelm@32215
   842
  val r_into_rtrancl = @{thm r_into_rtranclp};
wenzelm@32215
   843
  val trancl_into_rtrancl = @{thm tranclp_into_rtranclp};
wenzelm@32215
   844
  val rtrancl_trancl_trancl = @{thm rtranclp_tranclp_tranclp};
wenzelm@32215
   845
  val trancl_rtrancl_trancl = @{thm tranclp_rtranclp_tranclp};
wenzelm@32215
   846
  val rtrancl_trans = @{thm rtranclp_trans};
berghofe@22262
   847
berghofe@30107
   848
  fun decomp (@{const Trueprop} $ t) =
berghofe@22262
   849
    let fun dec (rel $ a $ b) =
berghofe@23743
   850
        let fun decr (Const ("Transitive_Closure.rtranclp", _ ) $ r) = (r,"r*")
berghofe@23743
   851
              | decr (Const ("Transitive_Closure.tranclp", _ ) $ r)  = (r,"r+")
berghofe@22262
   852
              | decr r = (r,"r");
berghofe@22262
   853
            val (rel,r) = decr rel;
berghofe@26801
   854
        in SOME (a, b, rel, r) end
berghofe@22262
   855
      | dec _ =  NONE
berghofe@30107
   856
    in dec t end
berghofe@30107
   857
    | decomp _ = NONE;
wenzelm@32215
   858
);
wenzelm@26340
   859
*}
berghofe@22262
   860
wenzelm@26340
   861
declaration {* fn _ =>
wenzelm@26340
   862
  Simplifier.map_ss (fn ss => ss
wenzelm@32215
   863
    addSolver (mk_solver' "Trancl" (Trancl_Tac.trancl_tac o Simplifier.the_context))
wenzelm@32215
   864
    addSolver (mk_solver' "Rtrancl" (Trancl_Tac.rtrancl_tac o Simplifier.the_context))
wenzelm@32215
   865
    addSolver (mk_solver' "Tranclp" (Tranclp_Tac.trancl_tac o Simplifier.the_context))
wenzelm@32215
   866
    addSolver (mk_solver' "Rtranclp" (Tranclp_Tac.rtrancl_tac o Simplifier.the_context)))
ballarin@15076
   867
*}
ballarin@15076
   868
wenzelm@32215
   869
wenzelm@32215
   870
text {* Optional methods. *}
ballarin@15076
   871
ballarin@15076
   872
method_setup trancl =
wenzelm@32215
   873
  {* Scan.succeed (SIMPLE_METHOD' o Trancl_Tac.trancl_tac) *}
wenzelm@18372
   874
  {* simple transitivity reasoner *}
ballarin@15076
   875
method_setup rtrancl =
wenzelm@32215
   876
  {* Scan.succeed (SIMPLE_METHOD' o Trancl_Tac.rtrancl_tac) *}
ballarin@15076
   877
  {* simple transitivity reasoner *}
berghofe@22262
   878
method_setup tranclp =
wenzelm@32215
   879
  {* Scan.succeed (SIMPLE_METHOD' o Tranclp_Tac.trancl_tac) *}
berghofe@22262
   880
  {* simple transitivity reasoner (predicate version) *}
berghofe@22262
   881
method_setup rtranclp =
wenzelm@32215
   882
  {* Scan.succeed (SIMPLE_METHOD' o Tranclp_Tac.rtrancl_tac) *}
berghofe@22262
   883
  {* simple transitivity reasoner (predicate version) *}
ballarin@15076
   884
nipkow@10213
   885
end