src/HOL/Transitive_Closure.thy
author bulwahn
Thu Jun 11 22:17:13 2009 +0200 (2009-06-11)
changeset 31576 525073f7aff6
parent 31351 b8d856545a02
child 31577 ce3721fa1e17
permissions -rw-r--r--
added lemma
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
bulwahn@31576
   481
lemma converse_tranclpE:
bulwahn@31576
   482
  assumes "tranclp r x z "
bulwahn@31576
   483
  assumes "r x z ==> P"
bulwahn@31576
   484
  assumes "\<And> y. [| r x y; tranclp r y z |] ==> P"
bulwahn@31576
   485
  shows P
bulwahn@31576
   486
proof -
bulwahn@31576
   487
  from tranclpD[OF assms(1)]
bulwahn@31576
   488
  obtain y where "r x y" and "rtranclp r y z" by iprover
bulwahn@31576
   489
  with assms(2-3) rtranclpD[OF this(2)] this(1)
bulwahn@31576
   490
  show P by iprover
bulwahn@31576
   491
qed  
bulwahn@31576
   492
bulwahn@31576
   493
lemmas converse_tranclE = converse_tranclpE [to_set]
bulwahn@31576
   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
kleing@25295
   503
lemma tranclD2:
kleing@25295
   504
  "(x, y) \<in> R\<^sup>+ \<Longrightarrow> \<exists>z. (x, z) \<in> R\<^sup>* \<and> (z, y) \<in> R"
kleing@25295
   505
  by (blast elim: tranclE intro: trancl_into_rtrancl)
kleing@25295
   506
nipkow@13867
   507
lemma irrefl_tranclI: "r^-1 \<inter> r^* = {} ==> (x, x) \<notin> r^+"
wenzelm@18372
   508
  by (blast elim: tranclE dest: trancl_into_rtrancl)
wenzelm@12691
   509
wenzelm@12691
   510
lemma irrefl_trancl_rD: "!!X. ALL x. (x, x) \<notin> r^+ ==> (x, y) \<in> r ==> x \<noteq> y"
wenzelm@12691
   511
  by (blast dest: r_into_trancl)
wenzelm@12691
   512
wenzelm@12691
   513
lemma trancl_subset_Sigma_aux:
wenzelm@12691
   514
    "(a, b) \<in> r^* ==> r \<subseteq> A \<times> A ==> a = b \<or> a \<in> A"
wenzelm@18372
   515
  by (induct rule: rtrancl_induct) auto
wenzelm@12691
   516
wenzelm@12691
   517
lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A ==> r^+ \<subseteq> A \<times> A"
berghofe@13704
   518
  apply (rule subsetI)
berghofe@13704
   519
  apply (simp only: split_tupled_all)
berghofe@13704
   520
  apply (erule tranclE)
wenzelm@26179
   521
   apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+
wenzelm@12691
   522
  done
nipkow@10996
   523
berghofe@23743
   524
lemma reflcl_tranclp [simp]: "(r^++)^== = r^**"
berghofe@22262
   525
  apply (safe intro!: order_antisym)
berghofe@23743
   526
   apply (erule tranclp_into_rtranclp)
berghofe@23743
   527
  apply (blast elim: rtranclp.cases dest: rtranclp_into_tranclp1)
wenzelm@11084
   528
  done
nipkow@10996
   529
berghofe@23743
   530
lemmas reflcl_trancl [simp] = reflcl_tranclp [to_set]
berghofe@22262
   531
wenzelm@11090
   532
lemma trancl_reflcl [simp]: "(r^=)^+ = r^*"
wenzelm@11084
   533
  apply safe
paulson@14208
   534
   apply (drule trancl_into_rtrancl, simp)
paulson@14208
   535
  apply (erule rtranclE, safe)
paulson@14208
   536
   apply (rule r_into_trancl, simp)
wenzelm@11084
   537
  apply (rule rtrancl_into_trancl1)
paulson@14208
   538
   apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD], fast)
wenzelm@11084
   539
  done
nipkow@10996
   540
wenzelm@11090
   541
lemma trancl_empty [simp]: "{}^+ = {}"
wenzelm@11084
   542
  by (auto elim: trancl_induct)
nipkow@10996
   543
wenzelm@11090
   544
lemma rtrancl_empty [simp]: "{}^* = Id"
wenzelm@11084
   545
  by (rule subst [OF reflcl_trancl]) simp
nipkow@10996
   546
berghofe@23743
   547
lemma rtranclpD: "R^** a b ==> a = b \<or> a \<noteq> b \<and> R^++ a b"
berghofe@23743
   548
  by (force simp add: reflcl_tranclp [symmetric] simp del: reflcl_tranclp)
berghofe@22262
   549
berghofe@23743
   550
lemmas rtranclD = rtranclpD [to_set]
wenzelm@11084
   551
kleing@16514
   552
lemma rtrancl_eq_or_trancl:
kleing@16514
   553
  "(x,y) \<in> R\<^sup>* = (x=y \<or> x\<noteq>y \<and> (x,y) \<in> R\<^sup>+)"
kleing@16514
   554
  by (fast elim: trancl_into_rtrancl dest: rtranclD)
nipkow@10996
   555
wenzelm@12691
   556
text {* @{text Domain} and @{text Range} *}
nipkow@10996
   557
wenzelm@11090
   558
lemma Domain_rtrancl [simp]: "Domain (R^*) = UNIV"
wenzelm@11084
   559
  by blast
nipkow@10996
   560
wenzelm@11090
   561
lemma Range_rtrancl [simp]: "Range (R^*) = UNIV"
wenzelm@11084
   562
  by blast
nipkow@10996
   563
wenzelm@11090
   564
lemma rtrancl_Un_subset: "(R^* \<union> S^*) \<subseteq> (R Un S)^*"
wenzelm@11084
   565
  by (rule rtrancl_Un_rtrancl [THEN subst]) fast
nipkow@10996
   566
wenzelm@11090
   567
lemma in_rtrancl_UnI: "x \<in> R^* \<or> x \<in> S^* ==> x \<in> (R \<union> S)^*"
wenzelm@11084
   568
  by (blast intro: subsetD [OF rtrancl_Un_subset])
nipkow@10996
   569
wenzelm@11090
   570
lemma trancl_domain [simp]: "Domain (r^+) = Domain r"
wenzelm@11084
   571
  by (unfold Domain_def) (blast dest: tranclD)
nipkow@10996
   572
wenzelm@11090
   573
lemma trancl_range [simp]: "Range (r^+) = Range r"
nipkow@26271
   574
unfolding Range_def by(simp add: trancl_converse [symmetric])
nipkow@10996
   575
paulson@11115
   576
lemma Not_Domain_rtrancl:
wenzelm@12691
   577
    "x ~: Domain R ==> ((x, y) : R^*) = (x = y)"
wenzelm@12691
   578
  apply auto
wenzelm@26179
   579
  apply (erule rev_mp)
wenzelm@26179
   580
  apply (erule rtrancl_induct)
wenzelm@26179
   581
   apply auto
wenzelm@26179
   582
  done
berghofe@11327
   583
haftmann@29609
   584
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
haftmann@29609
   585
  apply clarify
haftmann@29609
   586
  apply (erule trancl_induct)
haftmann@29609
   587
   apply (auto simp add: Field_def)
haftmann@29609
   588
  done
haftmann@29609
   589
haftmann@29609
   590
lemma finite_trancl: "finite (r^+) = finite r"
haftmann@29609
   591
  apply auto
haftmann@29609
   592
   prefer 2
haftmann@29609
   593
   apply (rule trancl_subset_Field2 [THEN finite_subset])
haftmann@29609
   594
   apply (rule finite_SigmaI)
haftmann@29609
   595
    prefer 3
haftmann@29609
   596
    apply (blast intro: r_into_trancl' finite_subset)
haftmann@29609
   597
   apply (auto simp add: finite_Field)
haftmann@29609
   598
  done
haftmann@29609
   599
wenzelm@12691
   600
text {* More about converse @{text rtrancl} and @{text trancl}, should
wenzelm@12691
   601
  be merged with main body. *}
kleing@12428
   602
nipkow@14337
   603
lemma single_valued_confluent:
nipkow@14337
   604
  "\<lbrakk> single_valued r; (x,y) \<in> r^*; (x,z) \<in> r^* \<rbrakk>
nipkow@14337
   605
  \<Longrightarrow> (y,z) \<in> r^* \<or> (z,y) \<in> r^*"
wenzelm@26179
   606
  apply (erule rtrancl_induct)
wenzelm@26179
   607
  apply simp
wenzelm@26179
   608
  apply (erule disjE)
wenzelm@26179
   609
   apply (blast elim:converse_rtranclE dest:single_valuedD)
wenzelm@26179
   610
  apply(blast intro:rtrancl_trans)
wenzelm@26179
   611
  done
nipkow@14337
   612
wenzelm@12691
   613
lemma r_r_into_trancl: "(a, b) \<in> R ==> (b, c) \<in> R ==> (a, c) \<in> R^+"
kleing@12428
   614
  by (fast intro: trancl_trans)
kleing@12428
   615
kleing@12428
   616
lemma trancl_into_trancl [rule_format]:
wenzelm@12691
   617
    "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r --> (a,c) \<in> r\<^sup>+"
wenzelm@12691
   618
  apply (erule trancl_induct)
kleing@12428
   619
   apply (fast intro: r_r_into_trancl)
kleing@12428
   620
  apply (fast intro: r_r_into_trancl trancl_trans)
kleing@12428
   621
  done
kleing@12428
   622
berghofe@23743
   623
lemma tranclp_rtranclp_tranclp:
berghofe@22262
   624
    "r\<^sup>+\<^sup>+ a b ==> r\<^sup>*\<^sup>* b c ==> r\<^sup>+\<^sup>+ a c"
berghofe@23743
   625
  apply (drule tranclpD)
wenzelm@26179
   626
  apply (elim exE conjE)
berghofe@23743
   627
  apply (drule rtranclp_trans, assumption)
berghofe@23743
   628
  apply (drule rtranclp_into_tranclp2, assumption, assumption)
kleing@12428
   629
  done
kleing@12428
   630
berghofe@23743
   631
lemmas trancl_rtrancl_trancl = tranclp_rtranclp_tranclp [to_set]
berghofe@22262
   632
wenzelm@12691
   633
lemmas transitive_closure_trans [trans] =
wenzelm@12691
   634
  r_r_into_trancl trancl_trans rtrancl_trans
berghofe@23743
   635
  trancl.trancl_into_trancl trancl_into_trancl2
berghofe@23743
   636
  rtrancl.rtrancl_into_rtrancl converse_rtrancl_into_rtrancl
wenzelm@12691
   637
  rtrancl_trancl_trancl trancl_rtrancl_trancl
kleing@12428
   638
berghofe@23743
   639
lemmas transitive_closurep_trans' [trans] =
berghofe@23743
   640
  tranclp_trans rtranclp_trans
berghofe@23743
   641
  tranclp.trancl_into_trancl tranclp_into_tranclp2
berghofe@23743
   642
  rtranclp.rtrancl_into_rtrancl converse_rtranclp_into_rtranclp
berghofe@23743
   643
  rtranclp_tranclp_tranclp tranclp_rtranclp_tranclp
berghofe@22262
   644
kleing@12428
   645
declare trancl_into_rtrancl [elim]
berghofe@11327
   646
haftmann@30954
   647
subsection {* The power operation on relations *}
haftmann@30954
   648
haftmann@30954
   649
text {* @{text "R ^^ n = R O ... O R"}, the n-fold composition of @{text R} *}
haftmann@30954
   650
haftmann@30971
   651
overloading
haftmann@30971
   652
  relpow == "compow :: nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"
haftmann@30971
   653
begin
haftmann@30954
   654
haftmann@30971
   655
primrec relpow :: "nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" where
haftmann@30971
   656
    "relpow 0 R = Id"
haftmann@30971
   657
  | "relpow (Suc n) R = R O (R ^^ n)"
haftmann@30954
   658
haftmann@30971
   659
end
haftmann@30954
   660
haftmann@30954
   661
lemma rel_pow_1 [simp]:
haftmann@30971
   662
  fixes R :: "('a \<times> 'a) set"
haftmann@30971
   663
  shows "R ^^ 1 = R"
haftmann@30954
   664
  by simp
haftmann@30954
   665
haftmann@30954
   666
lemma rel_pow_0_I: 
haftmann@30954
   667
  "(x, x) \<in> R ^^ 0"
haftmann@30954
   668
  by simp
haftmann@30954
   669
haftmann@30954
   670
lemma rel_pow_Suc_I:
haftmann@30954
   671
  "(x, y) \<in>  R ^^ n \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
haftmann@30954
   672
  by auto
haftmann@30954
   673
haftmann@30954
   674
lemma rel_pow_Suc_I2:
haftmann@30954
   675
  "(x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ n \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
haftmann@30954
   676
  by (induct n arbitrary: z) (simp, fastsimp)
haftmann@30954
   677
haftmann@30954
   678
lemma rel_pow_0_E:
haftmann@30954
   679
  "(x, y) \<in> R ^^ 0 \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30954
   680
  by simp
haftmann@30954
   681
haftmann@30954
   682
lemma rel_pow_Suc_E:
haftmann@30954
   683
  "(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
   684
  by auto
haftmann@30954
   685
haftmann@30954
   686
lemma rel_pow_E:
haftmann@30954
   687
  "(x, z) \<in>  R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
haftmann@30954
   688
   \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in>  R ^^ m \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> P)
haftmann@30954
   689
   \<Longrightarrow> P"
haftmann@30954
   690
  by (cases n) auto
haftmann@30954
   691
haftmann@30954
   692
lemma rel_pow_Suc_D2:
haftmann@30954
   693
  "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<exists>y. (x, y) \<in> R \<and> (y, z) \<in> R ^^ n)"
haftmann@30954
   694
  apply (induct n arbitrary: x z)
haftmann@30954
   695
   apply (blast intro: rel_pow_0_I elim: rel_pow_0_E rel_pow_Suc_E)
haftmann@30954
   696
  apply (blast intro: rel_pow_Suc_I elim: rel_pow_0_E rel_pow_Suc_E)
haftmann@30954
   697
  done
haftmann@30954
   698
haftmann@30954
   699
lemma rel_pow_Suc_E2:
haftmann@30954
   700
  "(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
   701
  by (blast dest: rel_pow_Suc_D2)
haftmann@30954
   702
haftmann@30954
   703
lemma rel_pow_Suc_D2':
haftmann@30954
   704
  "\<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
   705
  by (induct n) (simp_all, blast)
haftmann@30954
   706
haftmann@30954
   707
lemma rel_pow_E2:
haftmann@30954
   708
  "(x, z) \<in> R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
haftmann@30954
   709
     \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ m \<Longrightarrow> P)
haftmann@30954
   710
   \<Longrightarrow> P"
haftmann@30954
   711
  apply (cases n, simp)
haftmann@30954
   712
  apply (cut_tac n=nat and R=R in rel_pow_Suc_D2', simp, blast)
haftmann@30954
   713
  done
haftmann@30954
   714
nipkow@31351
   715
lemma rel_pow_add: "R ^^ (m+n) = R^^n O R^^m"
nipkow@31351
   716
by(induct n) auto
nipkow@31351
   717
haftmann@30954
   718
lemma rtrancl_imp_UN_rel_pow:
haftmann@30954
   719
  assumes "p \<in> R^*"
haftmann@30954
   720
  shows "p \<in> (\<Union>n. R ^^ n)"
haftmann@30954
   721
proof (cases p)
haftmann@30954
   722
  case (Pair x y)
haftmann@30954
   723
  with assms have "(x, y) \<in> R^*" by simp
haftmann@30954
   724
  then have "(x, y) \<in> (\<Union>n. R ^^ n)" proof induct
haftmann@30954
   725
    case base show ?case by (blast intro: rel_pow_0_I)
haftmann@30954
   726
  next
haftmann@30954
   727
    case step then show ?case by (blast intro: rel_pow_Suc_I)
haftmann@30954
   728
  qed
haftmann@30954
   729
  with Pair show ?thesis by simp
haftmann@30954
   730
qed
haftmann@30954
   731
haftmann@30954
   732
lemma rel_pow_imp_rtrancl:
haftmann@30954
   733
  assumes "p \<in> R ^^ n"
haftmann@30954
   734
  shows "p \<in> R^*"
haftmann@30954
   735
proof (cases p)
haftmann@30954
   736
  case (Pair x y)
haftmann@30954
   737
  with assms have "(x, y) \<in> R ^^ n" by simp
haftmann@30954
   738
  then have "(x, y) \<in> R^*" proof (induct n arbitrary: x y)
haftmann@30954
   739
    case 0 then show ?case by simp
haftmann@30954
   740
  next
haftmann@30954
   741
    case Suc then show ?case
haftmann@30954
   742
      by (blast elim: rel_pow_Suc_E intro: rtrancl_into_rtrancl)
haftmann@30954
   743
  qed
haftmann@30954
   744
  with Pair show ?thesis by simp
haftmann@30954
   745
qed
haftmann@30954
   746
haftmann@30954
   747
lemma rtrancl_is_UN_rel_pow:
haftmann@30954
   748
  "R^* = (\<Union>n. R ^^ n)"
haftmann@30954
   749
  by (blast intro: rtrancl_imp_UN_rel_pow rel_pow_imp_rtrancl)
haftmann@30954
   750
haftmann@30954
   751
lemma rtrancl_power:
haftmann@30954
   752
  "p \<in> R^* \<longleftrightarrow> (\<exists>n. p \<in> R ^^ n)"
haftmann@30954
   753
  by (simp add: rtrancl_is_UN_rel_pow)
haftmann@30954
   754
haftmann@30954
   755
lemma trancl_power:
haftmann@30954
   756
  "p \<in> R^+ \<longleftrightarrow> (\<exists>n > 0. p \<in> R ^^ n)"
haftmann@30954
   757
  apply (cases p)
haftmann@30954
   758
  apply simp
haftmann@30954
   759
  apply (rule iffI)
haftmann@30954
   760
   apply (drule tranclD2)
haftmann@30954
   761
   apply (clarsimp simp: rtrancl_is_UN_rel_pow)
haftmann@30971
   762
   apply (rule_tac x="Suc n" in exI)
haftmann@30954
   763
   apply (clarsimp simp: rel_comp_def)
haftmann@30954
   764
   apply fastsimp
haftmann@30954
   765
  apply clarsimp
haftmann@30954
   766
  apply (case_tac n, simp)
haftmann@30954
   767
  apply clarsimp
haftmann@30954
   768
  apply (drule rel_pow_imp_rtrancl)
haftmann@30954
   769
  apply (drule rtrancl_into_trancl1) apply auto
haftmann@30954
   770
  done
haftmann@30954
   771
haftmann@30954
   772
lemma rtrancl_imp_rel_pow:
haftmann@30954
   773
  "p \<in> R^* \<Longrightarrow> \<exists>n. p \<in> R ^^ n"
haftmann@30954
   774
  by (auto dest: rtrancl_imp_UN_rel_pow)
haftmann@30954
   775
haftmann@30954
   776
lemma single_valued_rel_pow:
haftmann@30954
   777
  fixes R :: "('a * 'a) set"
haftmann@30954
   778
  shows "single_valued R \<Longrightarrow> single_valued (R ^^ n)"
haftmann@30954
   779
  apply (induct n arbitrary: R)
haftmann@30954
   780
  apply simp_all
haftmann@30954
   781
  apply (rule single_valuedI)
haftmann@30954
   782
  apply (fast dest: single_valuedD elim: rel_pow_Suc_E)
haftmann@30954
   783
  done
paulson@15551
   784
ballarin@15076
   785
subsection {* Setup of transitivity reasoner *}
ballarin@15076
   786
wenzelm@26340
   787
ML {*
ballarin@15076
   788
ballarin@15076
   789
structure Trancl_Tac = Trancl_Tac_Fun (
ballarin@15076
   790
  struct
wenzelm@26340
   791
    val r_into_trancl = @{thm trancl.r_into_trancl};
wenzelm@26340
   792
    val trancl_trans  = @{thm trancl_trans};
wenzelm@26340
   793
    val rtrancl_refl = @{thm rtrancl.rtrancl_refl};
wenzelm@26340
   794
    val r_into_rtrancl = @{thm r_into_rtrancl};
wenzelm@26340
   795
    val trancl_into_rtrancl = @{thm trancl_into_rtrancl};
wenzelm@26340
   796
    val rtrancl_trancl_trancl = @{thm rtrancl_trancl_trancl};
wenzelm@26340
   797
    val trancl_rtrancl_trancl = @{thm trancl_rtrancl_trancl};
wenzelm@26340
   798
    val rtrancl_trans = @{thm rtrancl_trans};
ballarin@15096
   799
berghofe@30107
   800
  fun decomp (@{const Trueprop} $ t) =
wenzelm@18372
   801
    let fun dec (Const ("op :", _) $ (Const ("Pair", _) $ a $ b) $ rel ) =
berghofe@23743
   802
        let fun decr (Const ("Transitive_Closure.rtrancl", _ ) $ r) = (r,"r*")
berghofe@23743
   803
              | decr (Const ("Transitive_Closure.trancl", _ ) $ r)  = (r,"r+")
wenzelm@18372
   804
              | decr r = (r,"r");
berghofe@26801
   805
            val (rel,r) = decr (Envir.beta_eta_contract rel);
wenzelm@18372
   806
        in SOME (a,b,rel,r) end
wenzelm@18372
   807
      | dec _ =  NONE
berghofe@30107
   808
    in dec t end
berghofe@30107
   809
    | decomp _ = NONE;
wenzelm@18372
   810
wenzelm@21589
   811
  end);
ballarin@15076
   812
berghofe@22262
   813
structure Tranclp_Tac = Trancl_Tac_Fun (
berghofe@22262
   814
  struct
wenzelm@26340
   815
    val r_into_trancl = @{thm tranclp.r_into_trancl};
wenzelm@26340
   816
    val trancl_trans  = @{thm tranclp_trans};
wenzelm@26340
   817
    val rtrancl_refl = @{thm rtranclp.rtrancl_refl};
wenzelm@26340
   818
    val r_into_rtrancl = @{thm r_into_rtranclp};
wenzelm@26340
   819
    val trancl_into_rtrancl = @{thm tranclp_into_rtranclp};
wenzelm@26340
   820
    val rtrancl_trancl_trancl = @{thm rtranclp_tranclp_tranclp};
wenzelm@26340
   821
    val trancl_rtrancl_trancl = @{thm tranclp_rtranclp_tranclp};
wenzelm@26340
   822
    val rtrancl_trans = @{thm rtranclp_trans};
berghofe@22262
   823
berghofe@30107
   824
  fun decomp (@{const Trueprop} $ t) =
berghofe@22262
   825
    let fun dec (rel $ a $ b) =
berghofe@23743
   826
        let fun decr (Const ("Transitive_Closure.rtranclp", _ ) $ r) = (r,"r*")
berghofe@23743
   827
              | decr (Const ("Transitive_Closure.tranclp", _ ) $ r)  = (r,"r+")
berghofe@22262
   828
              | decr r = (r,"r");
berghofe@22262
   829
            val (rel,r) = decr rel;
berghofe@26801
   830
        in SOME (a, b, rel, r) end
berghofe@22262
   831
      | dec _ =  NONE
berghofe@30107
   832
    in dec t end
berghofe@30107
   833
    | decomp _ = NONE;
berghofe@22262
   834
berghofe@22262
   835
  end);
wenzelm@26340
   836
*}
berghofe@22262
   837
wenzelm@26340
   838
declaration {* fn _ =>
wenzelm@26340
   839
  Simplifier.map_ss (fn ss => ss
wenzelm@26340
   840
    addSolver (mk_solver "Trancl" (fn _ => Trancl_Tac.trancl_tac))
wenzelm@26340
   841
    addSolver (mk_solver "Rtrancl" (fn _ => Trancl_Tac.rtrancl_tac))
wenzelm@26340
   842
    addSolver (mk_solver "Tranclp" (fn _ => Tranclp_Tac.trancl_tac))
wenzelm@26340
   843
    addSolver (mk_solver "Rtranclp" (fn _ => Tranclp_Tac.rtrancl_tac)))
ballarin@15076
   844
*}
ballarin@15076
   845
wenzelm@21589
   846
(* Optional methods *)
ballarin@15076
   847
ballarin@15076
   848
method_setup trancl =
wenzelm@30549
   849
  {* Scan.succeed (K (SIMPLE_METHOD' Trancl_Tac.trancl_tac)) *}
wenzelm@18372
   850
  {* simple transitivity reasoner *}
ballarin@15076
   851
method_setup rtrancl =
wenzelm@30549
   852
  {* Scan.succeed (K (SIMPLE_METHOD' Trancl_Tac.rtrancl_tac)) *}
ballarin@15076
   853
  {* simple transitivity reasoner *}
berghofe@22262
   854
method_setup tranclp =
wenzelm@30549
   855
  {* Scan.succeed (K (SIMPLE_METHOD' Tranclp_Tac.trancl_tac)) *}
berghofe@22262
   856
  {* simple transitivity reasoner (predicate version) *}
berghofe@22262
   857
method_setup rtranclp =
wenzelm@30549
   858
  {* Scan.succeed (K (SIMPLE_METHOD' Tranclp_Tac.rtrancl_tac)) *}
berghofe@22262
   859
  {* simple transitivity reasoner (predicate version) *}
ballarin@15076
   860
nipkow@10213
   861
end