src/HOL/Transitive_Closure.thy
author huffman
Sat Jun 06 09:11:12 2009 -0700 (2009-06-06)
changeset 31488 5691ccb8d6b5
parent 31351 b8d856545a02
child 31576 525073f7aff6
permissions -rw-r--r--
generalize tendsto to class topological_space
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
berghofe@23743
    79
lemma reflcl_set_eq [pred_set_conv]: "(sup (\<lambda>x y. (x, y) \<in> r) op =) = (\<lambda>x y. (x, y) \<in> r Un Id)"
berghofe@22262
    80
  by (simp add: expand_fun_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
paulson@22080
   162
lemma rtrancl_Int_subset: "[| Id \<subseteq> s; r O (r^* \<inter> s) \<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@20716
   294
lemma rtrancl_unfold: "r^* = Id Un r O r^*"
paulson@15551
   295
  by (auto intro: rtrancl_into_rtrancl elim: rtranclE)
paulson@15551
   296
wenzelm@12691
   297
wenzelm@12691
   298
subsection {* Transitive closure *}
wenzelm@10331
   299
berghofe@13704
   300
lemma trancl_mono: "!!p. p \<in> r^+ ==> r \<subseteq> s ==> p \<in> s^+"
berghofe@23743
   301
  apply (simp add: split_tupled_all)
berghofe@13704
   302
  apply (erule trancl.induct)
wenzelm@26179
   303
   apply (iprover dest: subsetD)+
wenzelm@12691
   304
  done
wenzelm@12691
   305
berghofe@13704
   306
lemma r_into_trancl': "!!p. p : r ==> p : r^+"
berghofe@13704
   307
  by (simp only: split_tupled_all) (erule r_into_trancl)
berghofe@13704
   308
wenzelm@12691
   309
text {*
wenzelm@12691
   310
  \medskip Conversions between @{text trancl} and @{text rtrancl}.
wenzelm@12691
   311
*}
wenzelm@12691
   312
berghofe@23743
   313
lemma tranclp_into_rtranclp: "r^++ a b ==> r^** a b"
berghofe@23743
   314
  by (erule tranclp.induct) iprover+
wenzelm@12691
   315
berghofe@23743
   316
lemmas trancl_into_rtrancl = tranclp_into_rtranclp [to_set]
berghofe@22262
   317
berghofe@23743
   318
lemma rtranclp_into_tranclp1: assumes r: "r^** a b"
berghofe@22262
   319
  shows "!!c. r b c ==> r^++ a c" using r
nipkow@17589
   320
  by induct iprover+
wenzelm@12691
   321
berghofe@23743
   322
lemmas rtrancl_into_trancl1 = rtranclp_into_tranclp1 [to_set]
berghofe@22262
   323
berghofe@23743
   324
lemma rtranclp_into_tranclp2: "[| r a b; r^** b c |] ==> r^++ a c"
wenzelm@12691
   325
  -- {* intro rule from @{text r} and @{text rtrancl} *}
wenzelm@26179
   326
  apply (erule rtranclp.cases)
wenzelm@26179
   327
   apply iprover
berghofe@23743
   328
  apply (rule rtranclp_trans [THEN rtranclp_into_tranclp1])
wenzelm@26179
   329
    apply (simp | rule r_into_rtranclp)+
wenzelm@12691
   330
  done
wenzelm@12691
   331
berghofe@23743
   332
lemmas rtrancl_into_trancl2 = rtranclp_into_tranclp2 [to_set]
berghofe@22262
   333
wenzelm@26179
   334
text {* Nice induction rule for @{text trancl} *}
wenzelm@26179
   335
lemma tranclp_induct [consumes 1, case_names base step, induct pred: tranclp]:
wenzelm@26179
   336
  assumes "r^++ a b"
berghofe@22262
   337
  and cases: "!!y. r a y ==> P y"
berghofe@22262
   338
    "!!y z. r^++ a y ==> r y z ==> P y ==> P z"
berghofe@13704
   339
  shows "P b"
wenzelm@12691
   340
proof -
wenzelm@26179
   341
  from `r^++ a b` have "a = a --> P b"
nipkow@17589
   342
    by (induct "%x y. x = a --> P y" a b) (iprover intro: cases)+
wenzelm@26179
   343
  then show ?thesis by iprover
wenzelm@12691
   344
qed
wenzelm@12691
   345
berghofe@25425
   346
lemmas trancl_induct [induct set: trancl] = tranclp_induct [to_set]
berghofe@22262
   347
berghofe@23743
   348
lemmas tranclp_induct2 =
wenzelm@26179
   349
  tranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
wenzelm@26179
   350
    consumes 1, case_names base step]
berghofe@22262
   351
paulson@22172
   352
lemmas trancl_induct2 =
wenzelm@26179
   353
  trancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
wenzelm@26179
   354
    consumes 1, case_names base step]
paulson@22172
   355
berghofe@23743
   356
lemma tranclp_trans_induct:
berghofe@22262
   357
  assumes major: "r^++ x y"
berghofe@22262
   358
    and cases: "!!x y. r x y ==> P x y"
berghofe@22262
   359
      "!!x y z. [| r^++ x y; P x y; r^++ y z; P y z |] ==> P x z"
wenzelm@18372
   360
  shows "P x y"
wenzelm@12691
   361
  -- {* Another induction rule for trancl, incorporating transitivity *}
berghofe@23743
   362
  by (iprover intro: major [THEN tranclp_induct] cases)
wenzelm@12691
   363
berghofe@23743
   364
lemmas trancl_trans_induct = tranclp_trans_induct [to_set]
berghofe@23743
   365
wenzelm@26174
   366
lemma tranclE [cases set: trancl]:
wenzelm@26174
   367
  assumes "(a, b) : r^+"
wenzelm@26174
   368
  obtains
wenzelm@26174
   369
    (base) "(a, b) : r"
wenzelm@26174
   370
  | (step) c where "(a, c) : r^+" and "(c, b) : r"
wenzelm@26174
   371
  using assms by cases simp_all
wenzelm@10980
   372
paulson@22080
   373
lemma trancl_Int_subset: "[| r \<subseteq> s; r O (r^+ \<inter> s) \<subseteq> s|] ==> r^+ \<subseteq> s"
paulson@22080
   374
  apply (rule subsetI)
wenzelm@26179
   375
  apply (rule_tac p = x in PairE)
wenzelm@26179
   376
  apply clarify
wenzelm@26179
   377
  apply (erule trancl_induct)
wenzelm@26179
   378
   apply auto
paulson@22080
   379
  done
paulson@22080
   380
krauss@20716
   381
lemma trancl_unfold: "r^+ = r Un r O r^+"
paulson@15551
   382
  by (auto intro: trancl_into_trancl elim: tranclE)
paulson@15551
   383
wenzelm@26179
   384
text {* Transitivity of @{term "r^+"} *}
wenzelm@26179
   385
lemma trans_trancl [simp]: "trans (r^+)"
berghofe@13704
   386
proof (rule transI)
berghofe@13704
   387
  fix x y z
wenzelm@26179
   388
  assume "(x, y) \<in> r^+"
berghofe@13704
   389
  assume "(y, z) \<in> r^+"
wenzelm@26179
   390
  then show "(x, z) \<in> r^+"
wenzelm@26179
   391
  proof induct
wenzelm@26179
   392
    case (base u)
wenzelm@26179
   393
    from `(x, y) \<in> r^+` and `(y, u) \<in> r`
wenzelm@26179
   394
    show "(x, u) \<in> r^+" ..
wenzelm@26179
   395
  next
wenzelm@26179
   396
    case (step u v)
wenzelm@26179
   397
    from `(x, u) \<in> r^+` and `(u, v) \<in> r`
wenzelm@26179
   398
    show "(x, v) \<in> r^+" ..
wenzelm@26179
   399
  qed
berghofe@13704
   400
qed
wenzelm@12691
   401
wenzelm@12691
   402
lemmas trancl_trans = trans_trancl [THEN transD, standard]
wenzelm@12691
   403
berghofe@23743
   404
lemma tranclp_trans:
berghofe@22262
   405
  assumes xy: "r^++ x y"
berghofe@22262
   406
  and yz: "r^++ y z"
berghofe@22262
   407
  shows "r^++ x z" using yz xy
berghofe@22262
   408
  by induct iprover+
berghofe@22262
   409
wenzelm@26179
   410
lemma trancl_id [simp]: "trans r \<Longrightarrow> r^+ = r"
wenzelm@26179
   411
  apply auto
wenzelm@26179
   412
  apply (erule trancl_induct)
wenzelm@26179
   413
   apply assumption
wenzelm@26179
   414
  apply (unfold trans_def)
wenzelm@26179
   415
  apply blast
wenzelm@26179
   416
  done
nipkow@19623
   417
wenzelm@26179
   418
lemma rtranclp_tranclp_tranclp:
wenzelm@26179
   419
  assumes "r^** x y"
wenzelm@26179
   420
  shows "!!z. r^++ y z ==> r^++ x z" using assms
berghofe@23743
   421
  by induct (iprover intro: tranclp_trans)+
wenzelm@12691
   422
berghofe@23743
   423
lemmas rtrancl_trancl_trancl = rtranclp_tranclp_tranclp [to_set]
berghofe@22262
   424
berghofe@23743
   425
lemma tranclp_into_tranclp2: "r a b ==> r^++ b c ==> r^++ a c"
berghofe@23743
   426
  by (erule tranclp_trans [OF tranclp.r_into_trancl])
berghofe@22262
   427
berghofe@23743
   428
lemmas trancl_into_trancl2 = tranclp_into_tranclp2 [to_set]
wenzelm@12691
   429
wenzelm@12691
   430
lemma trancl_insert:
wenzelm@12691
   431
  "(insert (y, x) r)^+ = r^+ \<union> {(a, b). (a, y) \<in> r^* \<and> (x, b) \<in> r^*}"
wenzelm@12691
   432
  -- {* primitive recursion for @{text trancl} over finite relations *}
wenzelm@12691
   433
  apply (rule equalityI)
wenzelm@12691
   434
   apply (rule subsetI)
wenzelm@12691
   435
   apply (simp only: split_tupled_all)
paulson@14208
   436
   apply (erule trancl_induct, blast)
wenzelm@12691
   437
   apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl r_into_trancl trancl_trans)
wenzelm@12691
   438
  apply (rule subsetI)
wenzelm@12691
   439
  apply (blast intro: trancl_mono rtrancl_mono
wenzelm@12691
   440
    [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)
wenzelm@12691
   441
  done
wenzelm@12691
   442
berghofe@23743
   443
lemma tranclp_converseI: "(r^++)^--1 x y ==> (r^--1)^++ x y"
berghofe@22262
   444
  apply (drule conversepD)
berghofe@23743
   445
  apply (erule tranclp_induct)
berghofe@23743
   446
  apply (iprover intro: conversepI tranclp_trans)+
wenzelm@12691
   447
  done
wenzelm@12691
   448
berghofe@23743
   449
lemmas trancl_converseI = tranclp_converseI [to_set]
berghofe@22262
   450
berghofe@23743
   451
lemma tranclp_converseD: "(r^--1)^++ x y ==> (r^++)^--1 x y"
berghofe@22262
   452
  apply (rule conversepI)
berghofe@23743
   453
  apply (erule tranclp_induct)
berghofe@23743
   454
  apply (iprover dest: conversepD intro: tranclp_trans)+
berghofe@13704
   455
  done
wenzelm@12691
   456
berghofe@23743
   457
lemmas trancl_converseD = tranclp_converseD [to_set]
berghofe@22262
   458
berghofe@23743
   459
lemma tranclp_converse: "(r^--1)^++ = (r^++)^--1"
berghofe@22262
   460
  by (fastsimp simp add: expand_fun_eq
berghofe@23743
   461
    intro!: tranclp_converseI dest!: tranclp_converseD)
berghofe@22262
   462
berghofe@23743
   463
lemmas trancl_converse = tranclp_converse [to_set]
wenzelm@12691
   464
huffman@19228
   465
lemma sym_trancl: "sym r ==> sym (r^+)"
huffman@19228
   466
  by (simp only: sym_conv_converse_eq trancl_converse [symmetric])
huffman@19228
   467
berghofe@23743
   468
lemma converse_tranclp_induct:
berghofe@22262
   469
  assumes major: "r^++ a b"
berghofe@22262
   470
    and cases: "!!y. r y b ==> P(y)"
berghofe@22262
   471
      "!!y z.[| r y z;  r^++ z b;  P(z) |] ==> P(y)"
wenzelm@18372
   472
  shows "P a"
berghofe@23743
   473
  apply (rule tranclp_induct [OF tranclp_converseI, OF conversepI, OF major])
wenzelm@18372
   474
   apply (rule cases)
berghofe@22262
   475
   apply (erule conversepD)
berghofe@23743
   476
  apply (blast intro: prems dest!: tranclp_converseD conversepD)
wenzelm@18372
   477
  done
wenzelm@12691
   478
berghofe@23743
   479
lemmas converse_trancl_induct = converse_tranclp_induct [to_set]
berghofe@22262
   480
berghofe@23743
   481
lemma tranclpD: "R^++ x y ==> EX z. R x z \<and> R^** z y"
wenzelm@26179
   482
  apply (erule converse_tranclp_induct)
wenzelm@26179
   483
   apply auto
berghofe@23743
   484
  apply (blast intro: rtranclp_trans)
wenzelm@12691
   485
  done
wenzelm@12691
   486
berghofe@23743
   487
lemmas tranclD = tranclpD [to_set]
berghofe@22262
   488
kleing@25295
   489
lemma tranclD2:
kleing@25295
   490
  "(x, y) \<in> R\<^sup>+ \<Longrightarrow> \<exists>z. (x, z) \<in> R\<^sup>* \<and> (z, y) \<in> R"
kleing@25295
   491
  by (blast elim: tranclE intro: trancl_into_rtrancl)
kleing@25295
   492
nipkow@13867
   493
lemma irrefl_tranclI: "r^-1 \<inter> r^* = {} ==> (x, x) \<notin> r^+"
wenzelm@18372
   494
  by (blast elim: tranclE dest: trancl_into_rtrancl)
wenzelm@12691
   495
wenzelm@12691
   496
lemma irrefl_trancl_rD: "!!X. ALL x. (x, x) \<notin> r^+ ==> (x, y) \<in> r ==> x \<noteq> y"
wenzelm@12691
   497
  by (blast dest: r_into_trancl)
wenzelm@12691
   498
wenzelm@12691
   499
lemma trancl_subset_Sigma_aux:
wenzelm@12691
   500
    "(a, b) \<in> r^* ==> r \<subseteq> A \<times> A ==> a = b \<or> a \<in> A"
wenzelm@18372
   501
  by (induct rule: rtrancl_induct) auto
wenzelm@12691
   502
wenzelm@12691
   503
lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A ==> r^+ \<subseteq> A \<times> A"
berghofe@13704
   504
  apply (rule subsetI)
berghofe@13704
   505
  apply (simp only: split_tupled_all)
berghofe@13704
   506
  apply (erule tranclE)
wenzelm@26179
   507
   apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+
wenzelm@12691
   508
  done
nipkow@10996
   509
berghofe@23743
   510
lemma reflcl_tranclp [simp]: "(r^++)^== = r^**"
berghofe@22262
   511
  apply (safe intro!: order_antisym)
berghofe@23743
   512
   apply (erule tranclp_into_rtranclp)
berghofe@23743
   513
  apply (blast elim: rtranclp.cases dest: rtranclp_into_tranclp1)
wenzelm@11084
   514
  done
nipkow@10996
   515
berghofe@23743
   516
lemmas reflcl_trancl [simp] = reflcl_tranclp [to_set]
berghofe@22262
   517
wenzelm@11090
   518
lemma trancl_reflcl [simp]: "(r^=)^+ = r^*"
wenzelm@11084
   519
  apply safe
paulson@14208
   520
   apply (drule trancl_into_rtrancl, simp)
paulson@14208
   521
  apply (erule rtranclE, safe)
paulson@14208
   522
   apply (rule r_into_trancl, simp)
wenzelm@11084
   523
  apply (rule rtrancl_into_trancl1)
paulson@14208
   524
   apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD], fast)
wenzelm@11084
   525
  done
nipkow@10996
   526
wenzelm@11090
   527
lemma trancl_empty [simp]: "{}^+ = {}"
wenzelm@11084
   528
  by (auto elim: trancl_induct)
nipkow@10996
   529
wenzelm@11090
   530
lemma rtrancl_empty [simp]: "{}^* = Id"
wenzelm@11084
   531
  by (rule subst [OF reflcl_trancl]) simp
nipkow@10996
   532
berghofe@23743
   533
lemma rtranclpD: "R^** a b ==> a = b \<or> a \<noteq> b \<and> R^++ a b"
berghofe@23743
   534
  by (force simp add: reflcl_tranclp [symmetric] simp del: reflcl_tranclp)
berghofe@22262
   535
berghofe@23743
   536
lemmas rtranclD = rtranclpD [to_set]
wenzelm@11084
   537
kleing@16514
   538
lemma rtrancl_eq_or_trancl:
kleing@16514
   539
  "(x,y) \<in> R\<^sup>* = (x=y \<or> x\<noteq>y \<and> (x,y) \<in> R\<^sup>+)"
kleing@16514
   540
  by (fast elim: trancl_into_rtrancl dest: rtranclD)
nipkow@10996
   541
wenzelm@12691
   542
text {* @{text Domain} and @{text Range} *}
nipkow@10996
   543
wenzelm@11090
   544
lemma Domain_rtrancl [simp]: "Domain (R^*) = UNIV"
wenzelm@11084
   545
  by blast
nipkow@10996
   546
wenzelm@11090
   547
lemma Range_rtrancl [simp]: "Range (R^*) = UNIV"
wenzelm@11084
   548
  by blast
nipkow@10996
   549
wenzelm@11090
   550
lemma rtrancl_Un_subset: "(R^* \<union> S^*) \<subseteq> (R Un S)^*"
wenzelm@11084
   551
  by (rule rtrancl_Un_rtrancl [THEN subst]) fast
nipkow@10996
   552
wenzelm@11090
   553
lemma in_rtrancl_UnI: "x \<in> R^* \<or> x \<in> S^* ==> x \<in> (R \<union> S)^*"
wenzelm@11084
   554
  by (blast intro: subsetD [OF rtrancl_Un_subset])
nipkow@10996
   555
wenzelm@11090
   556
lemma trancl_domain [simp]: "Domain (r^+) = Domain r"
wenzelm@11084
   557
  by (unfold Domain_def) (blast dest: tranclD)
nipkow@10996
   558
wenzelm@11090
   559
lemma trancl_range [simp]: "Range (r^+) = Range r"
nipkow@26271
   560
unfolding Range_def by(simp add: trancl_converse [symmetric])
nipkow@10996
   561
paulson@11115
   562
lemma Not_Domain_rtrancl:
wenzelm@12691
   563
    "x ~: Domain R ==> ((x, y) : R^*) = (x = y)"
wenzelm@12691
   564
  apply auto
wenzelm@26179
   565
  apply (erule rev_mp)
wenzelm@26179
   566
  apply (erule rtrancl_induct)
wenzelm@26179
   567
   apply auto
wenzelm@26179
   568
  done
berghofe@11327
   569
haftmann@29609
   570
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
haftmann@29609
   571
  apply clarify
haftmann@29609
   572
  apply (erule trancl_induct)
haftmann@29609
   573
   apply (auto simp add: Field_def)
haftmann@29609
   574
  done
haftmann@29609
   575
haftmann@29609
   576
lemma finite_trancl: "finite (r^+) = finite r"
haftmann@29609
   577
  apply auto
haftmann@29609
   578
   prefer 2
haftmann@29609
   579
   apply (rule trancl_subset_Field2 [THEN finite_subset])
haftmann@29609
   580
   apply (rule finite_SigmaI)
haftmann@29609
   581
    prefer 3
haftmann@29609
   582
    apply (blast intro: r_into_trancl' finite_subset)
haftmann@29609
   583
   apply (auto simp add: finite_Field)
haftmann@29609
   584
  done
haftmann@29609
   585
wenzelm@12691
   586
text {* More about converse @{text rtrancl} and @{text trancl}, should
wenzelm@12691
   587
  be merged with main body. *}
kleing@12428
   588
nipkow@14337
   589
lemma single_valued_confluent:
nipkow@14337
   590
  "\<lbrakk> single_valued r; (x,y) \<in> r^*; (x,z) \<in> r^* \<rbrakk>
nipkow@14337
   591
  \<Longrightarrow> (y,z) \<in> r^* \<or> (z,y) \<in> r^*"
wenzelm@26179
   592
  apply (erule rtrancl_induct)
wenzelm@26179
   593
  apply simp
wenzelm@26179
   594
  apply (erule disjE)
wenzelm@26179
   595
   apply (blast elim:converse_rtranclE dest:single_valuedD)
wenzelm@26179
   596
  apply(blast intro:rtrancl_trans)
wenzelm@26179
   597
  done
nipkow@14337
   598
wenzelm@12691
   599
lemma r_r_into_trancl: "(a, b) \<in> R ==> (b, c) \<in> R ==> (a, c) \<in> R^+"
kleing@12428
   600
  by (fast intro: trancl_trans)
kleing@12428
   601
kleing@12428
   602
lemma trancl_into_trancl [rule_format]:
wenzelm@12691
   603
    "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r --> (a,c) \<in> r\<^sup>+"
wenzelm@12691
   604
  apply (erule trancl_induct)
kleing@12428
   605
   apply (fast intro: r_r_into_trancl)
kleing@12428
   606
  apply (fast intro: r_r_into_trancl trancl_trans)
kleing@12428
   607
  done
kleing@12428
   608
berghofe@23743
   609
lemma tranclp_rtranclp_tranclp:
berghofe@22262
   610
    "r\<^sup>+\<^sup>+ a b ==> r\<^sup>*\<^sup>* b c ==> r\<^sup>+\<^sup>+ a c"
berghofe@23743
   611
  apply (drule tranclpD)
wenzelm@26179
   612
  apply (elim exE conjE)
berghofe@23743
   613
  apply (drule rtranclp_trans, assumption)
berghofe@23743
   614
  apply (drule rtranclp_into_tranclp2, assumption, assumption)
kleing@12428
   615
  done
kleing@12428
   616
berghofe@23743
   617
lemmas trancl_rtrancl_trancl = tranclp_rtranclp_tranclp [to_set]
berghofe@22262
   618
wenzelm@12691
   619
lemmas transitive_closure_trans [trans] =
wenzelm@12691
   620
  r_r_into_trancl trancl_trans rtrancl_trans
berghofe@23743
   621
  trancl.trancl_into_trancl trancl_into_trancl2
berghofe@23743
   622
  rtrancl.rtrancl_into_rtrancl converse_rtrancl_into_rtrancl
wenzelm@12691
   623
  rtrancl_trancl_trancl trancl_rtrancl_trancl
kleing@12428
   624
berghofe@23743
   625
lemmas transitive_closurep_trans' [trans] =
berghofe@23743
   626
  tranclp_trans rtranclp_trans
berghofe@23743
   627
  tranclp.trancl_into_trancl tranclp_into_tranclp2
berghofe@23743
   628
  rtranclp.rtrancl_into_rtrancl converse_rtranclp_into_rtranclp
berghofe@23743
   629
  rtranclp_tranclp_tranclp tranclp_rtranclp_tranclp
berghofe@22262
   630
kleing@12428
   631
declare trancl_into_rtrancl [elim]
berghofe@11327
   632
haftmann@30954
   633
subsection {* The power operation on relations *}
haftmann@30954
   634
haftmann@30954
   635
text {* @{text "R ^^ n = R O ... O R"}, the n-fold composition of @{text R} *}
haftmann@30954
   636
haftmann@30971
   637
overloading
haftmann@30971
   638
  relpow == "compow :: nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"
haftmann@30971
   639
begin
haftmann@30954
   640
haftmann@30971
   641
primrec relpow :: "nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" where
haftmann@30971
   642
    "relpow 0 R = Id"
haftmann@30971
   643
  | "relpow (Suc n) R = R O (R ^^ n)"
haftmann@30954
   644
haftmann@30971
   645
end
haftmann@30954
   646
haftmann@30954
   647
lemma rel_pow_1 [simp]:
haftmann@30971
   648
  fixes R :: "('a \<times> 'a) set"
haftmann@30971
   649
  shows "R ^^ 1 = R"
haftmann@30954
   650
  by simp
haftmann@30954
   651
haftmann@30954
   652
lemma rel_pow_0_I: 
haftmann@30954
   653
  "(x, x) \<in> R ^^ 0"
haftmann@30954
   654
  by simp
haftmann@30954
   655
haftmann@30954
   656
lemma rel_pow_Suc_I:
haftmann@30954
   657
  "(x, y) \<in>  R ^^ n \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
haftmann@30954
   658
  by auto
haftmann@30954
   659
haftmann@30954
   660
lemma rel_pow_Suc_I2:
haftmann@30954
   661
  "(x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ n \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
haftmann@30954
   662
  by (induct n arbitrary: z) (simp, fastsimp)
haftmann@30954
   663
haftmann@30954
   664
lemma rel_pow_0_E:
haftmann@30954
   665
  "(x, y) \<in> R ^^ 0 \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30954
   666
  by simp
haftmann@30954
   667
haftmann@30954
   668
lemma rel_pow_Suc_E:
haftmann@30954
   669
  "(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
   670
  by auto
haftmann@30954
   671
haftmann@30954
   672
lemma rel_pow_E:
haftmann@30954
   673
  "(x, z) \<in>  R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
haftmann@30954
   674
   \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in>  R ^^ m \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> P)
haftmann@30954
   675
   \<Longrightarrow> P"
haftmann@30954
   676
  by (cases n) auto
haftmann@30954
   677
haftmann@30954
   678
lemma rel_pow_Suc_D2:
haftmann@30954
   679
  "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<exists>y. (x, y) \<in> R \<and> (y, z) \<in> R ^^ n)"
haftmann@30954
   680
  apply (induct n arbitrary: x z)
haftmann@30954
   681
   apply (blast intro: rel_pow_0_I elim: rel_pow_0_E rel_pow_Suc_E)
haftmann@30954
   682
  apply (blast intro: rel_pow_Suc_I elim: rel_pow_0_E rel_pow_Suc_E)
haftmann@30954
   683
  done
haftmann@30954
   684
haftmann@30954
   685
lemma rel_pow_Suc_E2:
haftmann@30954
   686
  "(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
   687
  by (blast dest: rel_pow_Suc_D2)
haftmann@30954
   688
haftmann@30954
   689
lemma rel_pow_Suc_D2':
haftmann@30954
   690
  "\<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
   691
  by (induct n) (simp_all, blast)
haftmann@30954
   692
haftmann@30954
   693
lemma rel_pow_E2:
haftmann@30954
   694
  "(x, z) \<in> R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
haftmann@30954
   695
     \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ m \<Longrightarrow> P)
haftmann@30954
   696
   \<Longrightarrow> P"
haftmann@30954
   697
  apply (cases n, simp)
haftmann@30954
   698
  apply (cut_tac n=nat and R=R in rel_pow_Suc_D2', simp, blast)
haftmann@30954
   699
  done
haftmann@30954
   700
nipkow@31351
   701
lemma rel_pow_add: "R ^^ (m+n) = R^^n O R^^m"
nipkow@31351
   702
by(induct n) auto
nipkow@31351
   703
haftmann@30954
   704
lemma rtrancl_imp_UN_rel_pow:
haftmann@30954
   705
  assumes "p \<in> R^*"
haftmann@30954
   706
  shows "p \<in> (\<Union>n. R ^^ n)"
haftmann@30954
   707
proof (cases p)
haftmann@30954
   708
  case (Pair x y)
haftmann@30954
   709
  with assms have "(x, y) \<in> R^*" by simp
haftmann@30954
   710
  then have "(x, y) \<in> (\<Union>n. R ^^ n)" proof induct
haftmann@30954
   711
    case base show ?case by (blast intro: rel_pow_0_I)
haftmann@30954
   712
  next
haftmann@30954
   713
    case step then show ?case by (blast intro: rel_pow_Suc_I)
haftmann@30954
   714
  qed
haftmann@30954
   715
  with Pair show ?thesis by simp
haftmann@30954
   716
qed
haftmann@30954
   717
haftmann@30954
   718
lemma rel_pow_imp_rtrancl:
haftmann@30954
   719
  assumes "p \<in> R ^^ n"
haftmann@30954
   720
  shows "p \<in> R^*"
haftmann@30954
   721
proof (cases p)
haftmann@30954
   722
  case (Pair x y)
haftmann@30954
   723
  with assms have "(x, y) \<in> R ^^ n" by simp
haftmann@30954
   724
  then have "(x, y) \<in> R^*" proof (induct n arbitrary: x y)
haftmann@30954
   725
    case 0 then show ?case by simp
haftmann@30954
   726
  next
haftmann@30954
   727
    case Suc then show ?case
haftmann@30954
   728
      by (blast elim: rel_pow_Suc_E intro: rtrancl_into_rtrancl)
haftmann@30954
   729
  qed
haftmann@30954
   730
  with Pair show ?thesis by simp
haftmann@30954
   731
qed
haftmann@30954
   732
haftmann@30954
   733
lemma rtrancl_is_UN_rel_pow:
haftmann@30954
   734
  "R^* = (\<Union>n. R ^^ n)"
haftmann@30954
   735
  by (blast intro: rtrancl_imp_UN_rel_pow rel_pow_imp_rtrancl)
haftmann@30954
   736
haftmann@30954
   737
lemma rtrancl_power:
haftmann@30954
   738
  "p \<in> R^* \<longleftrightarrow> (\<exists>n. p \<in> R ^^ n)"
haftmann@30954
   739
  by (simp add: rtrancl_is_UN_rel_pow)
haftmann@30954
   740
haftmann@30954
   741
lemma trancl_power:
haftmann@30954
   742
  "p \<in> R^+ \<longleftrightarrow> (\<exists>n > 0. p \<in> R ^^ n)"
haftmann@30954
   743
  apply (cases p)
haftmann@30954
   744
  apply simp
haftmann@30954
   745
  apply (rule iffI)
haftmann@30954
   746
   apply (drule tranclD2)
haftmann@30954
   747
   apply (clarsimp simp: rtrancl_is_UN_rel_pow)
haftmann@30971
   748
   apply (rule_tac x="Suc n" in exI)
haftmann@30954
   749
   apply (clarsimp simp: rel_comp_def)
haftmann@30954
   750
   apply fastsimp
haftmann@30954
   751
  apply clarsimp
haftmann@30954
   752
  apply (case_tac n, simp)
haftmann@30954
   753
  apply clarsimp
haftmann@30954
   754
  apply (drule rel_pow_imp_rtrancl)
haftmann@30954
   755
  apply (drule rtrancl_into_trancl1) apply auto
haftmann@30954
   756
  done
haftmann@30954
   757
haftmann@30954
   758
lemma rtrancl_imp_rel_pow:
haftmann@30954
   759
  "p \<in> R^* \<Longrightarrow> \<exists>n. p \<in> R ^^ n"
haftmann@30954
   760
  by (auto dest: rtrancl_imp_UN_rel_pow)
haftmann@30954
   761
haftmann@30954
   762
lemma single_valued_rel_pow:
haftmann@30954
   763
  fixes R :: "('a * 'a) set"
haftmann@30954
   764
  shows "single_valued R \<Longrightarrow> single_valued (R ^^ n)"
haftmann@30954
   765
  apply (induct n arbitrary: R)
haftmann@30954
   766
  apply simp_all
haftmann@30954
   767
  apply (rule single_valuedI)
haftmann@30954
   768
  apply (fast dest: single_valuedD elim: rel_pow_Suc_E)
haftmann@30954
   769
  done
paulson@15551
   770
ballarin@15076
   771
subsection {* Setup of transitivity reasoner *}
ballarin@15076
   772
wenzelm@26340
   773
ML {*
ballarin@15076
   774
ballarin@15076
   775
structure Trancl_Tac = Trancl_Tac_Fun (
ballarin@15076
   776
  struct
wenzelm@26340
   777
    val r_into_trancl = @{thm trancl.r_into_trancl};
wenzelm@26340
   778
    val trancl_trans  = @{thm trancl_trans};
wenzelm@26340
   779
    val rtrancl_refl = @{thm rtrancl.rtrancl_refl};
wenzelm@26340
   780
    val r_into_rtrancl = @{thm r_into_rtrancl};
wenzelm@26340
   781
    val trancl_into_rtrancl = @{thm trancl_into_rtrancl};
wenzelm@26340
   782
    val rtrancl_trancl_trancl = @{thm rtrancl_trancl_trancl};
wenzelm@26340
   783
    val trancl_rtrancl_trancl = @{thm trancl_rtrancl_trancl};
wenzelm@26340
   784
    val rtrancl_trans = @{thm rtrancl_trans};
ballarin@15096
   785
berghofe@30107
   786
  fun decomp (@{const Trueprop} $ t) =
wenzelm@18372
   787
    let fun dec (Const ("op :", _) $ (Const ("Pair", _) $ a $ b) $ rel ) =
berghofe@23743
   788
        let fun decr (Const ("Transitive_Closure.rtrancl", _ ) $ r) = (r,"r*")
berghofe@23743
   789
              | decr (Const ("Transitive_Closure.trancl", _ ) $ r)  = (r,"r+")
wenzelm@18372
   790
              | decr r = (r,"r");
berghofe@26801
   791
            val (rel,r) = decr (Envir.beta_eta_contract rel);
wenzelm@18372
   792
        in SOME (a,b,rel,r) end
wenzelm@18372
   793
      | dec _ =  NONE
berghofe@30107
   794
    in dec t end
berghofe@30107
   795
    | decomp _ = NONE;
wenzelm@18372
   796
wenzelm@21589
   797
  end);
ballarin@15076
   798
berghofe@22262
   799
structure Tranclp_Tac = Trancl_Tac_Fun (
berghofe@22262
   800
  struct
wenzelm@26340
   801
    val r_into_trancl = @{thm tranclp.r_into_trancl};
wenzelm@26340
   802
    val trancl_trans  = @{thm tranclp_trans};
wenzelm@26340
   803
    val rtrancl_refl = @{thm rtranclp.rtrancl_refl};
wenzelm@26340
   804
    val r_into_rtrancl = @{thm r_into_rtranclp};
wenzelm@26340
   805
    val trancl_into_rtrancl = @{thm tranclp_into_rtranclp};
wenzelm@26340
   806
    val rtrancl_trancl_trancl = @{thm rtranclp_tranclp_tranclp};
wenzelm@26340
   807
    val trancl_rtrancl_trancl = @{thm tranclp_rtranclp_tranclp};
wenzelm@26340
   808
    val rtrancl_trans = @{thm rtranclp_trans};
berghofe@22262
   809
berghofe@30107
   810
  fun decomp (@{const Trueprop} $ t) =
berghofe@22262
   811
    let fun dec (rel $ a $ b) =
berghofe@23743
   812
        let fun decr (Const ("Transitive_Closure.rtranclp", _ ) $ r) = (r,"r*")
berghofe@23743
   813
              | decr (Const ("Transitive_Closure.tranclp", _ ) $ r)  = (r,"r+")
berghofe@22262
   814
              | decr r = (r,"r");
berghofe@22262
   815
            val (rel,r) = decr rel;
berghofe@26801
   816
        in SOME (a, b, rel, r) end
berghofe@22262
   817
      | dec _ =  NONE
berghofe@30107
   818
    in dec t end
berghofe@30107
   819
    | decomp _ = NONE;
berghofe@22262
   820
berghofe@22262
   821
  end);
wenzelm@26340
   822
*}
berghofe@22262
   823
wenzelm@26340
   824
declaration {* fn _ =>
wenzelm@26340
   825
  Simplifier.map_ss (fn ss => ss
wenzelm@26340
   826
    addSolver (mk_solver "Trancl" (fn _ => Trancl_Tac.trancl_tac))
wenzelm@26340
   827
    addSolver (mk_solver "Rtrancl" (fn _ => Trancl_Tac.rtrancl_tac))
wenzelm@26340
   828
    addSolver (mk_solver "Tranclp" (fn _ => Tranclp_Tac.trancl_tac))
wenzelm@26340
   829
    addSolver (mk_solver "Rtranclp" (fn _ => Tranclp_Tac.rtrancl_tac)))
ballarin@15076
   830
*}
ballarin@15076
   831
wenzelm@21589
   832
(* Optional methods *)
ballarin@15076
   833
ballarin@15076
   834
method_setup trancl =
wenzelm@30549
   835
  {* Scan.succeed (K (SIMPLE_METHOD' Trancl_Tac.trancl_tac)) *}
wenzelm@18372
   836
  {* simple transitivity reasoner *}
ballarin@15076
   837
method_setup rtrancl =
wenzelm@30549
   838
  {* Scan.succeed (K (SIMPLE_METHOD' Trancl_Tac.rtrancl_tac)) *}
ballarin@15076
   839
  {* simple transitivity reasoner *}
berghofe@22262
   840
method_setup tranclp =
wenzelm@30549
   841
  {* Scan.succeed (K (SIMPLE_METHOD' Tranclp_Tac.trancl_tac)) *}
berghofe@22262
   842
  {* simple transitivity reasoner (predicate version) *}
berghofe@22262
   843
method_setup rtranclp =
wenzelm@30549
   844
  {* Scan.succeed (K (SIMPLE_METHOD' Tranclp_Tac.rtrancl_tac)) *}
berghofe@22262
   845
  {* simple transitivity reasoner (predicate version) *}
ballarin@15076
   846
nipkow@10213
   847
end