src/HOL/Transitive_Closure.thy
author berghofe
Wed May 07 10:56:49 2008 +0200 (2008-05-07)
changeset 26801 244184661a09
parent 26340 a85fe32e7b2f
child 29609 a010aab5bed0
permissions -rw-r--r--
- Function dec in Trancl_Tac must eta-contract relation before calling
decr, since it is now a function and could therefore be in eta-expanded form
- The trancl prover now does more eta-contraction itself, so eta-contraction
is no longer necessary in Tranclp_tac.
nipkow@10213
     1
(*  Title:      HOL/Transitive_Closure.thy
nipkow@10213
     2
    ID:         $Id$
nipkow@10213
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
nipkow@10213
     4
    Copyright   1992  University of Cambridge
nipkow@10213
     5
*)
nipkow@10213
     6
wenzelm@12691
     7
header {* Reflexive and Transitive closure of a relation *}
wenzelm@12691
     8
nipkow@15131
     9
theory Transitive_Closure
berghofe@22262
    10
imports Predicate
wenzelm@21589
    11
uses "~~/src/Provers/trancl.ML"
nipkow@15131
    12
begin
wenzelm@12691
    13
wenzelm@12691
    14
text {*
wenzelm@12691
    15
  @{text rtrancl} is reflexive/transitive closure,
wenzelm@12691
    16
  @{text trancl} is transitive closure,
wenzelm@12691
    17
  @{text reflcl} is reflexive closure.
wenzelm@12691
    18
wenzelm@12691
    19
  These postfix operators have \emph{maximum priority}, forcing their
wenzelm@12691
    20
  operands to be atomic.
wenzelm@12691
    21
*}
nipkow@10213
    22
berghofe@23743
    23
inductive_set
berghofe@23743
    24
  rtrancl :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"   ("(_^*)" [1000] 999)
berghofe@23743
    25
  for r :: "('a \<times> 'a) set"
berghofe@22262
    26
where
berghofe@23743
    27
    rtrancl_refl [intro!, Pure.intro!, simp]: "(a, a) : r^*"
berghofe@23743
    28
  | rtrancl_into_rtrancl [Pure.intro]: "(a, b) : r^* ==> (b, c) : r ==> (a, c) : r^*"
berghofe@11327
    29
berghofe@23743
    30
inductive_set
berghofe@23743
    31
  trancl :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"  ("(_^+)" [1000] 999)
berghofe@23743
    32
  for r :: "('a \<times> 'a) set"
berghofe@22262
    33
where
berghofe@23743
    34
    r_into_trancl [intro, Pure.intro]: "(a, b) : r ==> (a, b) : r^+"
berghofe@23743
    35
  | trancl_into_trancl [Pure.intro]: "(a, b) : r^+ ==> (b, c) : r ==> (a, c) : r^+"
berghofe@13704
    36
berghofe@23743
    37
notation
berghofe@23743
    38
  rtranclp  ("(_^**)" [1000] 1000) and
berghofe@23743
    39
  tranclp  ("(_^++)" [1000] 1000)
nipkow@10213
    40
wenzelm@19656
    41
abbreviation
berghofe@23743
    42
  reflclp :: "('a => 'a => bool) => 'a => 'a => bool"  ("(_^==)" [1000] 1000) where
haftmann@22422
    43
  "r^== == sup r op ="
berghofe@22262
    44
berghofe@22262
    45
abbreviation
berghofe@23743
    46
  reflcl :: "('a \<times> 'a) set => ('a \<times> 'a) set"  ("(_^=)" [1000] 999) where
wenzelm@19656
    47
  "r^= == r \<union> Id"
nipkow@10213
    48
wenzelm@21210
    49
notation (xsymbols)
berghofe@23743
    50
  rtranclp  ("(_\<^sup>*\<^sup>*)" [1000] 1000) and
berghofe@23743
    51
  tranclp  ("(_\<^sup>+\<^sup>+)" [1000] 1000) and
berghofe@23743
    52
  reflclp  ("(_\<^sup>=\<^sup>=)" [1000] 1000) and
berghofe@23743
    53
  rtrancl  ("(_\<^sup>*)" [1000] 999) and
berghofe@23743
    54
  trancl  ("(_\<^sup>+)" [1000] 999) and
berghofe@23743
    55
  reflcl  ("(_\<^sup>=)" [1000] 999)
wenzelm@12691
    56
wenzelm@21210
    57
notation (HTML output)
berghofe@23743
    58
  rtranclp  ("(_\<^sup>*\<^sup>*)" [1000] 1000) and
berghofe@23743
    59
  tranclp  ("(_\<^sup>+\<^sup>+)" [1000] 1000) and
berghofe@23743
    60
  reflclp  ("(_\<^sup>=\<^sup>=)" [1000] 1000) and
berghofe@23743
    61
  rtrancl  ("(_\<^sup>*)" [1000] 999) and
berghofe@23743
    62
  trancl  ("(_\<^sup>+)" [1000] 999) and
berghofe@23743
    63
  reflcl  ("(_\<^sup>=)" [1000] 999)
kleing@14565
    64
wenzelm@12691
    65
nipkow@26271
    66
subsection {* Reflexive closure *}
nipkow@26271
    67
nipkow@26271
    68
lemma reflexive_reflcl[simp]: "reflexive(r^=)"
nipkow@26271
    69
by(simp add:refl_def)
nipkow@26271
    70
nipkow@26271
    71
lemma antisym_reflcl[simp]: "antisym(r^=) = antisym r"
nipkow@26271
    72
by(simp add:antisym_def)
nipkow@26271
    73
nipkow@26271
    74
lemma trans_reflclI[simp]: "trans r \<Longrightarrow> trans(r^=)"
nipkow@26271
    75
unfolding trans_def by blast
nipkow@26271
    76
nipkow@26271
    77
wenzelm@12691
    78
subsection {* Reflexive-transitive closure *}
wenzelm@12691
    79
berghofe@23743
    80
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
    81
  by (simp add: expand_fun_eq)
berghofe@22262
    82
wenzelm@12691
    83
lemma r_into_rtrancl [intro]: "!!p. p \<in> r ==> p \<in> r^*"
wenzelm@12691
    84
  -- {* @{text rtrancl} of @{text r} contains @{text r} *}
wenzelm@12691
    85
  apply (simp only: split_tupled_all)
wenzelm@12691
    86
  apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])
wenzelm@12691
    87
  done
wenzelm@12691
    88
berghofe@23743
    89
lemma r_into_rtranclp [intro]: "r x y ==> r^** x y"
berghofe@22262
    90
  -- {* @{text rtrancl} of @{text r} contains @{text r} *}
berghofe@23743
    91
  by (erule rtranclp.rtrancl_refl [THEN rtranclp.rtrancl_into_rtrancl])
berghofe@22262
    92
berghofe@23743
    93
lemma rtranclp_mono: "r \<le> s ==> r^** \<le> s^**"
wenzelm@12691
    94
  -- {* monotonicity of @{text rtrancl} *}
berghofe@22262
    95
  apply (rule predicate2I)
berghofe@23743
    96
  apply (erule rtranclp.induct)
berghofe@23743
    97
   apply (rule_tac [2] rtranclp.rtrancl_into_rtrancl, blast+)
wenzelm@12691
    98
  done
wenzelm@12691
    99
berghofe@23743
   100
lemmas rtrancl_mono = rtranclp_mono [to_set]
berghofe@22262
   101
wenzelm@26179
   102
theorem rtranclp_induct [consumes 1, case_names base step, induct set: rtranclp]:
berghofe@22262
   103
  assumes a: "r^** a b"
berghofe@22262
   104
    and cases: "P a" "!!y z. [| r^** a y; r y z; P y |] ==> P z"
wenzelm@12937
   105
  shows "P b"
wenzelm@12691
   106
proof -
wenzelm@12691
   107
  from a have "a = a --> P b"
nipkow@17589
   108
    by (induct "%x y. x = a --> P y" a b) (iprover intro: cases)+
wenzelm@26179
   109
  then show ?thesis by iprover
wenzelm@12691
   110
qed
wenzelm@12691
   111
berghofe@25425
   112
lemmas rtrancl_induct [induct set: rtrancl] = rtranclp_induct [to_set]
berghofe@22262
   113
berghofe@23743
   114
lemmas rtranclp_induct2 =
berghofe@23743
   115
  rtranclp_induct[of _ "(ax,ay)" "(bx,by)", split_rule,
berghofe@22262
   116
                 consumes 1, case_names refl step]
berghofe@22262
   117
nipkow@14404
   118
lemmas rtrancl_induct2 =
nipkow@14404
   119
  rtrancl_induct[of "(ax,ay)" "(bx,by)", split_format (complete),
nipkow@14404
   120
                 consumes 1, case_names refl step]
wenzelm@18372
   121
huffman@19228
   122
lemma reflexive_rtrancl: "reflexive (r^*)"
huffman@19228
   123
  by (unfold refl_def) fast
huffman@19228
   124
wenzelm@26179
   125
text {* Transitivity of transitive closure. *}
wenzelm@26179
   126
lemma trans_rtrancl: "trans (r^*)"
berghofe@12823
   127
proof (rule transI)
berghofe@12823
   128
  fix x y z
berghofe@12823
   129
  assume "(x, y) \<in> r\<^sup>*"
berghofe@12823
   130
  assume "(y, z) \<in> r\<^sup>*"
wenzelm@26179
   131
  then show "(x, z) \<in> r\<^sup>*"
wenzelm@26179
   132
  proof induct
wenzelm@26179
   133
    case base
wenzelm@26179
   134
    show "(x, y) \<in> r\<^sup>*" by fact
wenzelm@26179
   135
  next
wenzelm@26179
   136
    case (step u v)
wenzelm@26179
   137
    from `(x, u) \<in> r\<^sup>*` and `(u, v) \<in> r`
wenzelm@26179
   138
    show "(x, v) \<in> r\<^sup>*" ..
wenzelm@26179
   139
  qed
berghofe@12823
   140
qed
wenzelm@12691
   141
wenzelm@12691
   142
lemmas rtrancl_trans = trans_rtrancl [THEN transD, standard]
wenzelm@12691
   143
berghofe@23743
   144
lemma rtranclp_trans:
berghofe@22262
   145
  assumes xy: "r^** x y"
berghofe@22262
   146
  and yz: "r^** y z"
berghofe@22262
   147
  shows "r^** x z" using yz xy
berghofe@22262
   148
  by induct iprover+
berghofe@22262
   149
wenzelm@26174
   150
lemma rtranclE [cases set: rtrancl]:
wenzelm@26174
   151
  assumes major: "(a::'a, b) : r^*"
wenzelm@26174
   152
  obtains
wenzelm@26174
   153
    (base) "a = b"
wenzelm@26174
   154
  | (step) y where "(a, y) : r^*" and "(y, b) : r"
wenzelm@12691
   155
  -- {* elimination of @{text rtrancl} -- by induction on a special formula *}
wenzelm@18372
   156
  apply (subgoal_tac "(a::'a) = b | (EX y. (a,y) : r^* & (y,b) : r)")
wenzelm@18372
   157
   apply (rule_tac [2] major [THEN rtrancl_induct])
wenzelm@18372
   158
    prefer 2 apply blast
wenzelm@18372
   159
   prefer 2 apply blast
wenzelm@26174
   160
  apply (erule asm_rl exE disjE conjE base step)+
wenzelm@18372
   161
  done
wenzelm@12691
   162
paulson@22080
   163
lemma rtrancl_Int_subset: "[| Id \<subseteq> s; r O (r^* \<inter> s) \<subseteq> s|] ==> r^* \<subseteq> s"
paulson@22080
   164
  apply (rule subsetI)
paulson@22080
   165
  apply (rule_tac p="x" in PairE, clarify)
paulson@22080
   166
  apply (erule rtrancl_induct, auto) 
paulson@22080
   167
  done
paulson@22080
   168
berghofe@23743
   169
lemma converse_rtranclp_into_rtranclp:
berghofe@22262
   170
  "r a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>*\<^sup>* a c"
berghofe@23743
   171
  by (rule rtranclp_trans) iprover+
berghofe@22262
   172
berghofe@23743
   173
lemmas converse_rtrancl_into_rtrancl = converse_rtranclp_into_rtranclp [to_set]
wenzelm@12691
   174
wenzelm@12691
   175
text {*
wenzelm@12691
   176
  \medskip More @{term "r^*"} equations and inclusions.
wenzelm@12691
   177
*}
wenzelm@12691
   178
berghofe@23743
   179
lemma rtranclp_idemp [simp]: "(r^**)^** = r^**"
berghofe@22262
   180
  apply (auto intro!: order_antisym)
berghofe@23743
   181
  apply (erule rtranclp_induct)
berghofe@23743
   182
   apply (rule rtranclp.rtrancl_refl)
berghofe@23743
   183
  apply (blast intro: rtranclp_trans)
wenzelm@12691
   184
  done
wenzelm@12691
   185
berghofe@23743
   186
lemmas rtrancl_idemp [simp] = rtranclp_idemp [to_set]
berghofe@22262
   187
wenzelm@12691
   188
lemma rtrancl_idemp_self_comp [simp]: "R^* O R^* = R^*"
wenzelm@12691
   189
  apply (rule set_ext)
wenzelm@12691
   190
  apply (simp only: split_tupled_all)
wenzelm@12691
   191
  apply (blast intro: rtrancl_trans)
wenzelm@12691
   192
  done
wenzelm@12691
   193
wenzelm@12691
   194
lemma rtrancl_subset_rtrancl: "r \<subseteq> s^* ==> r^* \<subseteq> s^*"
wenzelm@26179
   195
  apply (drule rtrancl_mono)
wenzelm@26179
   196
  apply simp
wenzelm@26179
   197
  done
wenzelm@12691
   198
berghofe@23743
   199
lemma rtranclp_subset: "R \<le> S ==> S \<le> R^** ==> S^** = R^**"
berghofe@23743
   200
  apply (drule rtranclp_mono)
wenzelm@26179
   201
  apply (drule rtranclp_mono)
wenzelm@26179
   202
  apply simp
wenzelm@12691
   203
  done
wenzelm@12691
   204
berghofe@23743
   205
lemmas rtrancl_subset = rtranclp_subset [to_set]
berghofe@22262
   206
berghofe@23743
   207
lemma rtranclp_sup_rtranclp: "(sup (R^**) (S^**))^** = (sup R S)^**"
berghofe@23743
   208
  by (blast intro!: rtranclp_subset intro: rtranclp_mono [THEN predicate2D])
wenzelm@12691
   209
berghofe@23743
   210
lemmas rtrancl_Un_rtrancl = rtranclp_sup_rtranclp [to_set]
berghofe@22262
   211
berghofe@23743
   212
lemma rtranclp_reflcl [simp]: "(R^==)^** = R^**"
berghofe@23743
   213
  by (blast intro!: rtranclp_subset)
berghofe@22262
   214
berghofe@23743
   215
lemmas rtrancl_reflcl [simp] = rtranclp_reflcl [to_set]
wenzelm@12691
   216
wenzelm@12691
   217
lemma rtrancl_r_diff_Id: "(r - Id)^* = r^*"
wenzelm@12691
   218
  apply (rule sym)
paulson@14208
   219
  apply (rule rtrancl_subset, blast, clarify)
wenzelm@12691
   220
  apply (rename_tac a b)
wenzelm@26179
   221
  apply (case_tac "a = b")
wenzelm@26179
   222
   apply blast
wenzelm@12691
   223
  apply (blast intro!: r_into_rtrancl)
wenzelm@12691
   224
  done
wenzelm@12691
   225
berghofe@23743
   226
lemma rtranclp_r_diff_Id: "(inf r op ~=)^** = r^**"
berghofe@22262
   227
  apply (rule sym)
berghofe@23743
   228
  apply (rule rtranclp_subset)
wenzelm@26179
   229
   apply blast+
berghofe@22262
   230
  done
berghofe@22262
   231
berghofe@23743
   232
theorem rtranclp_converseD:
berghofe@22262
   233
  assumes r: "(r^--1)^** x y"
berghofe@22262
   234
  shows "r^** y x"
berghofe@12823
   235
proof -
berghofe@12823
   236
  from r show ?thesis
berghofe@23743
   237
    by induct (iprover intro: rtranclp_trans dest!: conversepD)+
berghofe@12823
   238
qed
wenzelm@12691
   239
berghofe@23743
   240
lemmas rtrancl_converseD = rtranclp_converseD [to_set]
berghofe@22262
   241
berghofe@23743
   242
theorem rtranclp_converseI:
wenzelm@26179
   243
  assumes "r^** y x"
berghofe@22262
   244
  shows "(r^--1)^** x y"
wenzelm@26179
   245
  using assms
wenzelm@26179
   246
  by induct (iprover intro: rtranclp_trans conversepI)+
wenzelm@12691
   247
berghofe@23743
   248
lemmas rtrancl_converseI = rtranclp_converseI [to_set]
berghofe@22262
   249
wenzelm@12691
   250
lemma rtrancl_converse: "(r^-1)^* = (r^*)^-1"
wenzelm@12691
   251
  by (fast dest!: rtrancl_converseD intro!: rtrancl_converseI)
wenzelm@12691
   252
huffman@19228
   253
lemma sym_rtrancl: "sym r ==> sym (r^*)"
huffman@19228
   254
  by (simp only: sym_conv_converse_eq rtrancl_converse [symmetric])
huffman@19228
   255
berghofe@23743
   256
theorem converse_rtranclp_induct[consumes 1]:
berghofe@22262
   257
  assumes major: "r^** a b"
berghofe@22262
   258
    and cases: "P b" "!!y z. [| r y z; r^** z b; P z |] ==> P y"
wenzelm@12937
   259
  shows "P a"
wenzelm@26179
   260
  using rtranclp_converseI [OF major]
wenzelm@26179
   261
  by induct (iprover intro: cases dest!: conversepD rtranclp_converseD)+
wenzelm@12691
   262
berghofe@25425
   263
lemmas converse_rtrancl_induct = converse_rtranclp_induct [to_set]
berghofe@22262
   264
berghofe@23743
   265
lemmas converse_rtranclp_induct2 =
wenzelm@26179
   266
  converse_rtranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
berghofe@22262
   267
                 consumes 1, case_names refl step]
berghofe@22262
   268
nipkow@14404
   269
lemmas converse_rtrancl_induct2 =
wenzelm@26179
   270
  converse_rtrancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
nipkow@14404
   271
                 consumes 1, case_names refl step]
wenzelm@12691
   272
berghofe@23743
   273
lemma converse_rtranclpE:
berghofe@22262
   274
  assumes major: "r^** x z"
wenzelm@18372
   275
    and cases: "x=z ==> P"
berghofe@22262
   276
      "!!y. [| r x y; r^** y z |] ==> P"
wenzelm@18372
   277
  shows P
berghofe@22262
   278
  apply (subgoal_tac "x = z | (EX y. r x y & r^** y z)")
berghofe@23743
   279
   apply (rule_tac [2] major [THEN converse_rtranclp_induct])
wenzelm@18372
   280
    prefer 2 apply iprover
wenzelm@18372
   281
   prefer 2 apply iprover
wenzelm@18372
   282
  apply (erule asm_rl exE disjE conjE cases)+
wenzelm@18372
   283
  done
wenzelm@12691
   284
berghofe@23743
   285
lemmas converse_rtranclE = converse_rtranclpE [to_set]
berghofe@22262
   286
berghofe@23743
   287
lemmas converse_rtranclpE2 = converse_rtranclpE [of _ "(xa,xb)" "(za,zb)", split_rule]
berghofe@22262
   288
berghofe@22262
   289
lemmas converse_rtranclE2 = converse_rtranclE [of "(xa,xb)" "(za,zb)", split_rule]
wenzelm@12691
   290
wenzelm@12691
   291
lemma r_comp_rtrancl_eq: "r O r^* = r^* O r"
wenzelm@12691
   292
  by (blast elim: rtranclE converse_rtranclE
wenzelm@12691
   293
    intro: rtrancl_into_rtrancl converse_rtrancl_into_rtrancl)
wenzelm@12691
   294
krauss@20716
   295
lemma rtrancl_unfold: "r^* = Id Un r O r^*"
paulson@15551
   296
  by (auto intro: rtrancl_into_rtrancl elim: rtranclE)
paulson@15551
   297
wenzelm@12691
   298
wenzelm@12691
   299
subsection {* Transitive closure *}
wenzelm@10331
   300
berghofe@13704
   301
lemma trancl_mono: "!!p. p \<in> r^+ ==> r \<subseteq> s ==> p \<in> s^+"
berghofe@23743
   302
  apply (simp add: split_tupled_all)
berghofe@13704
   303
  apply (erule trancl.induct)
wenzelm@26179
   304
   apply (iprover dest: subsetD)+
wenzelm@12691
   305
  done
wenzelm@12691
   306
berghofe@13704
   307
lemma r_into_trancl': "!!p. p : r ==> p : r^+"
berghofe@13704
   308
  by (simp only: split_tupled_all) (erule r_into_trancl)
berghofe@13704
   309
wenzelm@12691
   310
text {*
wenzelm@12691
   311
  \medskip Conversions between @{text trancl} and @{text rtrancl}.
wenzelm@12691
   312
*}
wenzelm@12691
   313
berghofe@23743
   314
lemma tranclp_into_rtranclp: "r^++ a b ==> r^** a b"
berghofe@23743
   315
  by (erule tranclp.induct) iprover+
wenzelm@12691
   316
berghofe@23743
   317
lemmas trancl_into_rtrancl = tranclp_into_rtranclp [to_set]
berghofe@22262
   318
berghofe@23743
   319
lemma rtranclp_into_tranclp1: assumes r: "r^** a b"
berghofe@22262
   320
  shows "!!c. r b c ==> r^++ a c" using r
nipkow@17589
   321
  by induct iprover+
wenzelm@12691
   322
berghofe@23743
   323
lemmas rtrancl_into_trancl1 = rtranclp_into_tranclp1 [to_set]
berghofe@22262
   324
berghofe@23743
   325
lemma rtranclp_into_tranclp2: "[| r a b; r^** b c |] ==> r^++ a c"
wenzelm@12691
   326
  -- {* intro rule from @{text r} and @{text rtrancl} *}
wenzelm@26179
   327
  apply (erule rtranclp.cases)
wenzelm@26179
   328
   apply iprover
berghofe@23743
   329
  apply (rule rtranclp_trans [THEN rtranclp_into_tranclp1])
wenzelm@26179
   330
    apply (simp | rule r_into_rtranclp)+
wenzelm@12691
   331
  done
wenzelm@12691
   332
berghofe@23743
   333
lemmas rtrancl_into_trancl2 = rtranclp_into_tranclp2 [to_set]
berghofe@22262
   334
wenzelm@26179
   335
text {* Nice induction rule for @{text trancl} *}
wenzelm@26179
   336
lemma tranclp_induct [consumes 1, case_names base step, induct pred: tranclp]:
wenzelm@26179
   337
  assumes "r^++ a b"
berghofe@22262
   338
  and cases: "!!y. r a y ==> P y"
berghofe@22262
   339
    "!!y z. r^++ a y ==> r y z ==> P y ==> P z"
berghofe@13704
   340
  shows "P b"
wenzelm@12691
   341
proof -
wenzelm@26179
   342
  from `r^++ a b` have "a = a --> P b"
nipkow@17589
   343
    by (induct "%x y. x = a --> P y" a b) (iprover intro: cases)+
wenzelm@26179
   344
  then show ?thesis by iprover
wenzelm@12691
   345
qed
wenzelm@12691
   346
berghofe@25425
   347
lemmas trancl_induct [induct set: trancl] = tranclp_induct [to_set]
berghofe@22262
   348
berghofe@23743
   349
lemmas tranclp_induct2 =
wenzelm@26179
   350
  tranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
wenzelm@26179
   351
    consumes 1, case_names base step]
berghofe@22262
   352
paulson@22172
   353
lemmas trancl_induct2 =
wenzelm@26179
   354
  trancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
wenzelm@26179
   355
    consumes 1, case_names base step]
paulson@22172
   356
berghofe@23743
   357
lemma tranclp_trans_induct:
berghofe@22262
   358
  assumes major: "r^++ x y"
berghofe@22262
   359
    and cases: "!!x y. r x y ==> P x y"
berghofe@22262
   360
      "!!x y z. [| r^++ x y; P x y; r^++ y z; P y z |] ==> P x z"
wenzelm@18372
   361
  shows "P x y"
wenzelm@12691
   362
  -- {* Another induction rule for trancl, incorporating transitivity *}
berghofe@23743
   363
  by (iprover intro: major [THEN tranclp_induct] cases)
wenzelm@12691
   364
berghofe@23743
   365
lemmas trancl_trans_induct = tranclp_trans_induct [to_set]
berghofe@23743
   366
wenzelm@26174
   367
lemma tranclE [cases set: trancl]:
wenzelm@26174
   368
  assumes "(a, b) : r^+"
wenzelm@26174
   369
  obtains
wenzelm@26174
   370
    (base) "(a, b) : r"
wenzelm@26174
   371
  | (step) c where "(a, c) : r^+" and "(c, b) : r"
wenzelm@26174
   372
  using assms by cases simp_all
wenzelm@10980
   373
paulson@22080
   374
lemma trancl_Int_subset: "[| r \<subseteq> s; r O (r^+ \<inter> s) \<subseteq> s|] ==> r^+ \<subseteq> s"
paulson@22080
   375
  apply (rule subsetI)
wenzelm@26179
   376
  apply (rule_tac p = x in PairE)
wenzelm@26179
   377
  apply clarify
wenzelm@26179
   378
  apply (erule trancl_induct)
wenzelm@26179
   379
   apply auto
paulson@22080
   380
  done
paulson@22080
   381
krauss@20716
   382
lemma trancl_unfold: "r^+ = r Un r O r^+"
paulson@15551
   383
  by (auto intro: trancl_into_trancl elim: tranclE)
paulson@15551
   384
wenzelm@26179
   385
text {* Transitivity of @{term "r^+"} *}
wenzelm@26179
   386
lemma trans_trancl [simp]: "trans (r^+)"
berghofe@13704
   387
proof (rule transI)
berghofe@13704
   388
  fix x y z
wenzelm@26179
   389
  assume "(x, y) \<in> r^+"
berghofe@13704
   390
  assume "(y, z) \<in> r^+"
wenzelm@26179
   391
  then show "(x, z) \<in> r^+"
wenzelm@26179
   392
  proof induct
wenzelm@26179
   393
    case (base u)
wenzelm@26179
   394
    from `(x, y) \<in> r^+` and `(y, u) \<in> r`
wenzelm@26179
   395
    show "(x, u) \<in> r^+" ..
wenzelm@26179
   396
  next
wenzelm@26179
   397
    case (step u v)
wenzelm@26179
   398
    from `(x, u) \<in> r^+` and `(u, v) \<in> r`
wenzelm@26179
   399
    show "(x, v) \<in> r^+" ..
wenzelm@26179
   400
  qed
berghofe@13704
   401
qed
wenzelm@12691
   402
wenzelm@12691
   403
lemmas trancl_trans = trans_trancl [THEN transD, standard]
wenzelm@12691
   404
berghofe@23743
   405
lemma tranclp_trans:
berghofe@22262
   406
  assumes xy: "r^++ x y"
berghofe@22262
   407
  and yz: "r^++ y z"
berghofe@22262
   408
  shows "r^++ x z" using yz xy
berghofe@22262
   409
  by induct iprover+
berghofe@22262
   410
wenzelm@26179
   411
lemma trancl_id [simp]: "trans r \<Longrightarrow> r^+ = r"
wenzelm@26179
   412
  apply auto
wenzelm@26179
   413
  apply (erule trancl_induct)
wenzelm@26179
   414
   apply assumption
wenzelm@26179
   415
  apply (unfold trans_def)
wenzelm@26179
   416
  apply blast
wenzelm@26179
   417
  done
nipkow@19623
   418
wenzelm@26179
   419
lemma rtranclp_tranclp_tranclp:
wenzelm@26179
   420
  assumes "r^** x y"
wenzelm@26179
   421
  shows "!!z. r^++ y z ==> r^++ x z" using assms
berghofe@23743
   422
  by induct (iprover intro: tranclp_trans)+
wenzelm@12691
   423
berghofe@23743
   424
lemmas rtrancl_trancl_trancl = rtranclp_tranclp_tranclp [to_set]
berghofe@22262
   425
berghofe@23743
   426
lemma tranclp_into_tranclp2: "r a b ==> r^++ b c ==> r^++ a c"
berghofe@23743
   427
  by (erule tranclp_trans [OF tranclp.r_into_trancl])
berghofe@22262
   428
berghofe@23743
   429
lemmas trancl_into_trancl2 = tranclp_into_tranclp2 [to_set]
wenzelm@12691
   430
wenzelm@12691
   431
lemma trancl_insert:
wenzelm@12691
   432
  "(insert (y, x) r)^+ = r^+ \<union> {(a, b). (a, y) \<in> r^* \<and> (x, b) \<in> r^*}"
wenzelm@12691
   433
  -- {* primitive recursion for @{text trancl} over finite relations *}
wenzelm@12691
   434
  apply (rule equalityI)
wenzelm@12691
   435
   apply (rule subsetI)
wenzelm@12691
   436
   apply (simp only: split_tupled_all)
paulson@14208
   437
   apply (erule trancl_induct, blast)
wenzelm@12691
   438
   apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl r_into_trancl trancl_trans)
wenzelm@12691
   439
  apply (rule subsetI)
wenzelm@12691
   440
  apply (blast intro: trancl_mono rtrancl_mono
wenzelm@12691
   441
    [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)
wenzelm@12691
   442
  done
wenzelm@12691
   443
berghofe@23743
   444
lemma tranclp_converseI: "(r^++)^--1 x y ==> (r^--1)^++ x y"
berghofe@22262
   445
  apply (drule conversepD)
berghofe@23743
   446
  apply (erule tranclp_induct)
berghofe@23743
   447
  apply (iprover intro: conversepI tranclp_trans)+
wenzelm@12691
   448
  done
wenzelm@12691
   449
berghofe@23743
   450
lemmas trancl_converseI = tranclp_converseI [to_set]
berghofe@22262
   451
berghofe@23743
   452
lemma tranclp_converseD: "(r^--1)^++ x y ==> (r^++)^--1 x y"
berghofe@22262
   453
  apply (rule conversepI)
berghofe@23743
   454
  apply (erule tranclp_induct)
berghofe@23743
   455
  apply (iprover dest: conversepD intro: tranclp_trans)+
berghofe@13704
   456
  done
wenzelm@12691
   457
berghofe@23743
   458
lemmas trancl_converseD = tranclp_converseD [to_set]
berghofe@22262
   459
berghofe@23743
   460
lemma tranclp_converse: "(r^--1)^++ = (r^++)^--1"
berghofe@22262
   461
  by (fastsimp simp add: expand_fun_eq
berghofe@23743
   462
    intro!: tranclp_converseI dest!: tranclp_converseD)
berghofe@22262
   463
berghofe@23743
   464
lemmas trancl_converse = tranclp_converse [to_set]
wenzelm@12691
   465
huffman@19228
   466
lemma sym_trancl: "sym r ==> sym (r^+)"
huffman@19228
   467
  by (simp only: sym_conv_converse_eq trancl_converse [symmetric])
huffman@19228
   468
berghofe@23743
   469
lemma converse_tranclp_induct:
berghofe@22262
   470
  assumes major: "r^++ a b"
berghofe@22262
   471
    and cases: "!!y. r y b ==> P(y)"
berghofe@22262
   472
      "!!y z.[| r y z;  r^++ z b;  P(z) |] ==> P(y)"
wenzelm@18372
   473
  shows "P a"
berghofe@23743
   474
  apply (rule tranclp_induct [OF tranclp_converseI, OF conversepI, OF major])
wenzelm@18372
   475
   apply (rule cases)
berghofe@22262
   476
   apply (erule conversepD)
berghofe@23743
   477
  apply (blast intro: prems dest!: tranclp_converseD conversepD)
wenzelm@18372
   478
  done
wenzelm@12691
   479
berghofe@23743
   480
lemmas converse_trancl_induct = converse_tranclp_induct [to_set]
berghofe@22262
   481
berghofe@23743
   482
lemma tranclpD: "R^++ x y ==> EX z. R x z \<and> R^** z y"
wenzelm@26179
   483
  apply (erule converse_tranclp_induct)
wenzelm@26179
   484
   apply auto
berghofe@23743
   485
  apply (blast intro: rtranclp_trans)
wenzelm@12691
   486
  done
wenzelm@12691
   487
berghofe@23743
   488
lemmas tranclD = tranclpD [to_set]
berghofe@22262
   489
kleing@25295
   490
lemma tranclD2:
kleing@25295
   491
  "(x, y) \<in> R\<^sup>+ \<Longrightarrow> \<exists>z. (x, z) \<in> R\<^sup>* \<and> (z, y) \<in> R"
kleing@25295
   492
  by (blast elim: tranclE intro: trancl_into_rtrancl)
kleing@25295
   493
nipkow@13867
   494
lemma irrefl_tranclI: "r^-1 \<inter> r^* = {} ==> (x, x) \<notin> r^+"
wenzelm@18372
   495
  by (blast elim: tranclE dest: trancl_into_rtrancl)
wenzelm@12691
   496
wenzelm@12691
   497
lemma irrefl_trancl_rD: "!!X. ALL x. (x, x) \<notin> r^+ ==> (x, y) \<in> r ==> x \<noteq> y"
wenzelm@12691
   498
  by (blast dest: r_into_trancl)
wenzelm@12691
   499
wenzelm@12691
   500
lemma trancl_subset_Sigma_aux:
wenzelm@12691
   501
    "(a, b) \<in> r^* ==> r \<subseteq> A \<times> A ==> a = b \<or> a \<in> A"
wenzelm@18372
   502
  by (induct rule: rtrancl_induct) auto
wenzelm@12691
   503
wenzelm@12691
   504
lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A ==> r^+ \<subseteq> A \<times> A"
berghofe@13704
   505
  apply (rule subsetI)
berghofe@13704
   506
  apply (simp only: split_tupled_all)
berghofe@13704
   507
  apply (erule tranclE)
wenzelm@26179
   508
   apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+
wenzelm@12691
   509
  done
nipkow@10996
   510
berghofe@23743
   511
lemma reflcl_tranclp [simp]: "(r^++)^== = r^**"
berghofe@22262
   512
  apply (safe intro!: order_antisym)
berghofe@23743
   513
   apply (erule tranclp_into_rtranclp)
berghofe@23743
   514
  apply (blast elim: rtranclp.cases dest: rtranclp_into_tranclp1)
wenzelm@11084
   515
  done
nipkow@10996
   516
berghofe@23743
   517
lemmas reflcl_trancl [simp] = reflcl_tranclp [to_set]
berghofe@22262
   518
wenzelm@11090
   519
lemma trancl_reflcl [simp]: "(r^=)^+ = r^*"
wenzelm@11084
   520
  apply safe
paulson@14208
   521
   apply (drule trancl_into_rtrancl, simp)
paulson@14208
   522
  apply (erule rtranclE, safe)
paulson@14208
   523
   apply (rule r_into_trancl, simp)
wenzelm@11084
   524
  apply (rule rtrancl_into_trancl1)
paulson@14208
   525
   apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD], fast)
wenzelm@11084
   526
  done
nipkow@10996
   527
wenzelm@11090
   528
lemma trancl_empty [simp]: "{}^+ = {}"
wenzelm@11084
   529
  by (auto elim: trancl_induct)
nipkow@10996
   530
wenzelm@11090
   531
lemma rtrancl_empty [simp]: "{}^* = Id"
wenzelm@11084
   532
  by (rule subst [OF reflcl_trancl]) simp
nipkow@10996
   533
berghofe@23743
   534
lemma rtranclpD: "R^** a b ==> a = b \<or> a \<noteq> b \<and> R^++ a b"
berghofe@23743
   535
  by (force simp add: reflcl_tranclp [symmetric] simp del: reflcl_tranclp)
berghofe@22262
   536
berghofe@23743
   537
lemmas rtranclD = rtranclpD [to_set]
wenzelm@11084
   538
kleing@16514
   539
lemma rtrancl_eq_or_trancl:
kleing@16514
   540
  "(x,y) \<in> R\<^sup>* = (x=y \<or> x\<noteq>y \<and> (x,y) \<in> R\<^sup>+)"
kleing@16514
   541
  by (fast elim: trancl_into_rtrancl dest: rtranclD)
nipkow@10996
   542
wenzelm@12691
   543
text {* @{text Domain} and @{text Range} *}
nipkow@10996
   544
wenzelm@11090
   545
lemma Domain_rtrancl [simp]: "Domain (R^*) = UNIV"
wenzelm@11084
   546
  by blast
nipkow@10996
   547
wenzelm@11090
   548
lemma Range_rtrancl [simp]: "Range (R^*) = UNIV"
wenzelm@11084
   549
  by blast
nipkow@10996
   550
wenzelm@11090
   551
lemma rtrancl_Un_subset: "(R^* \<union> S^*) \<subseteq> (R Un S)^*"
wenzelm@11084
   552
  by (rule rtrancl_Un_rtrancl [THEN subst]) fast
nipkow@10996
   553
wenzelm@11090
   554
lemma in_rtrancl_UnI: "x \<in> R^* \<or> x \<in> S^* ==> x \<in> (R \<union> S)^*"
wenzelm@11084
   555
  by (blast intro: subsetD [OF rtrancl_Un_subset])
nipkow@10996
   556
wenzelm@11090
   557
lemma trancl_domain [simp]: "Domain (r^+) = Domain r"
wenzelm@11084
   558
  by (unfold Domain_def) (blast dest: tranclD)
nipkow@10996
   559
wenzelm@11090
   560
lemma trancl_range [simp]: "Range (r^+) = Range r"
nipkow@26271
   561
unfolding Range_def by(simp add: trancl_converse [symmetric])
nipkow@10996
   562
paulson@11115
   563
lemma Not_Domain_rtrancl:
wenzelm@12691
   564
    "x ~: Domain R ==> ((x, y) : R^*) = (x = y)"
wenzelm@12691
   565
  apply auto
wenzelm@26179
   566
  apply (erule rev_mp)
wenzelm@26179
   567
  apply (erule rtrancl_induct)
wenzelm@26179
   568
   apply auto
wenzelm@26179
   569
  done
berghofe@11327
   570
wenzelm@12691
   571
text {* More about converse @{text rtrancl} and @{text trancl}, should
wenzelm@12691
   572
  be merged with main body. *}
kleing@12428
   573
nipkow@14337
   574
lemma single_valued_confluent:
nipkow@14337
   575
  "\<lbrakk> single_valued r; (x,y) \<in> r^*; (x,z) \<in> r^* \<rbrakk>
nipkow@14337
   576
  \<Longrightarrow> (y,z) \<in> r^* \<or> (z,y) \<in> r^*"
wenzelm@26179
   577
  apply (erule rtrancl_induct)
wenzelm@26179
   578
  apply simp
wenzelm@26179
   579
  apply (erule disjE)
wenzelm@26179
   580
   apply (blast elim:converse_rtranclE dest:single_valuedD)
wenzelm@26179
   581
  apply(blast intro:rtrancl_trans)
wenzelm@26179
   582
  done
nipkow@14337
   583
wenzelm@12691
   584
lemma r_r_into_trancl: "(a, b) \<in> R ==> (b, c) \<in> R ==> (a, c) \<in> R^+"
kleing@12428
   585
  by (fast intro: trancl_trans)
kleing@12428
   586
kleing@12428
   587
lemma trancl_into_trancl [rule_format]:
wenzelm@12691
   588
    "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r --> (a,c) \<in> r\<^sup>+"
wenzelm@12691
   589
  apply (erule trancl_induct)
kleing@12428
   590
   apply (fast intro: r_r_into_trancl)
kleing@12428
   591
  apply (fast intro: r_r_into_trancl trancl_trans)
kleing@12428
   592
  done
kleing@12428
   593
berghofe@23743
   594
lemma tranclp_rtranclp_tranclp:
berghofe@22262
   595
    "r\<^sup>+\<^sup>+ a b ==> r\<^sup>*\<^sup>* b c ==> r\<^sup>+\<^sup>+ a c"
berghofe@23743
   596
  apply (drule tranclpD)
wenzelm@26179
   597
  apply (elim exE conjE)
berghofe@23743
   598
  apply (drule rtranclp_trans, assumption)
berghofe@23743
   599
  apply (drule rtranclp_into_tranclp2, assumption, assumption)
kleing@12428
   600
  done
kleing@12428
   601
berghofe@23743
   602
lemmas trancl_rtrancl_trancl = tranclp_rtranclp_tranclp [to_set]
berghofe@22262
   603
wenzelm@12691
   604
lemmas transitive_closure_trans [trans] =
wenzelm@12691
   605
  r_r_into_trancl trancl_trans rtrancl_trans
berghofe@23743
   606
  trancl.trancl_into_trancl trancl_into_trancl2
berghofe@23743
   607
  rtrancl.rtrancl_into_rtrancl converse_rtrancl_into_rtrancl
wenzelm@12691
   608
  rtrancl_trancl_trancl trancl_rtrancl_trancl
kleing@12428
   609
berghofe@23743
   610
lemmas transitive_closurep_trans' [trans] =
berghofe@23743
   611
  tranclp_trans rtranclp_trans
berghofe@23743
   612
  tranclp.trancl_into_trancl tranclp_into_tranclp2
berghofe@23743
   613
  rtranclp.rtrancl_into_rtrancl converse_rtranclp_into_rtranclp
berghofe@23743
   614
  rtranclp_tranclp_tranclp tranclp_rtranclp_tranclp
berghofe@22262
   615
kleing@12428
   616
declare trancl_into_rtrancl [elim]
berghofe@11327
   617
paulson@15551
   618
ballarin@15076
   619
subsection {* Setup of transitivity reasoner *}
ballarin@15076
   620
wenzelm@26340
   621
ML {*
ballarin@15076
   622
ballarin@15076
   623
structure Trancl_Tac = Trancl_Tac_Fun (
ballarin@15076
   624
  struct
wenzelm@26340
   625
    val r_into_trancl = @{thm trancl.r_into_trancl};
wenzelm@26340
   626
    val trancl_trans  = @{thm trancl_trans};
wenzelm@26340
   627
    val rtrancl_refl = @{thm rtrancl.rtrancl_refl};
wenzelm@26340
   628
    val r_into_rtrancl = @{thm r_into_rtrancl};
wenzelm@26340
   629
    val trancl_into_rtrancl = @{thm trancl_into_rtrancl};
wenzelm@26340
   630
    val rtrancl_trancl_trancl = @{thm rtrancl_trancl_trancl};
wenzelm@26340
   631
    val trancl_rtrancl_trancl = @{thm trancl_rtrancl_trancl};
wenzelm@26340
   632
    val rtrancl_trans = @{thm rtrancl_trans};
ballarin@15096
   633
wenzelm@18372
   634
  fun decomp (Trueprop $ t) =
wenzelm@18372
   635
    let fun dec (Const ("op :", _) $ (Const ("Pair", _) $ a $ b) $ rel ) =
berghofe@23743
   636
        let fun decr (Const ("Transitive_Closure.rtrancl", _ ) $ r) = (r,"r*")
berghofe@23743
   637
              | decr (Const ("Transitive_Closure.trancl", _ ) $ r)  = (r,"r+")
wenzelm@18372
   638
              | decr r = (r,"r");
berghofe@26801
   639
            val (rel,r) = decr (Envir.beta_eta_contract rel);
wenzelm@18372
   640
        in SOME (a,b,rel,r) end
wenzelm@18372
   641
      | dec _ =  NONE
ballarin@15076
   642
    in dec t end;
wenzelm@18372
   643
wenzelm@21589
   644
  end);
ballarin@15076
   645
berghofe@22262
   646
structure Tranclp_Tac = Trancl_Tac_Fun (
berghofe@22262
   647
  struct
wenzelm@26340
   648
    val r_into_trancl = @{thm tranclp.r_into_trancl};
wenzelm@26340
   649
    val trancl_trans  = @{thm tranclp_trans};
wenzelm@26340
   650
    val rtrancl_refl = @{thm rtranclp.rtrancl_refl};
wenzelm@26340
   651
    val r_into_rtrancl = @{thm r_into_rtranclp};
wenzelm@26340
   652
    val trancl_into_rtrancl = @{thm tranclp_into_rtranclp};
wenzelm@26340
   653
    val rtrancl_trancl_trancl = @{thm rtranclp_tranclp_tranclp};
wenzelm@26340
   654
    val trancl_rtrancl_trancl = @{thm tranclp_rtranclp_tranclp};
wenzelm@26340
   655
    val rtrancl_trans = @{thm rtranclp_trans};
berghofe@22262
   656
berghofe@22262
   657
  fun decomp (Trueprop $ t) =
berghofe@22262
   658
    let fun dec (rel $ a $ b) =
berghofe@23743
   659
        let fun decr (Const ("Transitive_Closure.rtranclp", _ ) $ r) = (r,"r*")
berghofe@23743
   660
              | decr (Const ("Transitive_Closure.tranclp", _ ) $ r)  = (r,"r+")
berghofe@22262
   661
              | decr r = (r,"r");
berghofe@22262
   662
            val (rel,r) = decr rel;
berghofe@26801
   663
        in SOME (a, b, rel, r) end
berghofe@22262
   664
      | dec _ =  NONE
berghofe@22262
   665
    in dec t end;
berghofe@22262
   666
berghofe@22262
   667
  end);
wenzelm@26340
   668
*}
berghofe@22262
   669
wenzelm@26340
   670
declaration {* fn _ =>
wenzelm@26340
   671
  Simplifier.map_ss (fn ss => ss
wenzelm@26340
   672
    addSolver (mk_solver "Trancl" (fn _ => Trancl_Tac.trancl_tac))
wenzelm@26340
   673
    addSolver (mk_solver "Rtrancl" (fn _ => Trancl_Tac.rtrancl_tac))
wenzelm@26340
   674
    addSolver (mk_solver "Tranclp" (fn _ => Tranclp_Tac.trancl_tac))
wenzelm@26340
   675
    addSolver (mk_solver "Rtranclp" (fn _ => Tranclp_Tac.rtrancl_tac)))
ballarin@15076
   676
*}
ballarin@15076
   677
wenzelm@21589
   678
(* Optional methods *)
ballarin@15076
   679
ballarin@15076
   680
method_setup trancl =
wenzelm@21589
   681
  {* Method.no_args (Method.SIMPLE_METHOD' Trancl_Tac.trancl_tac) *}
wenzelm@18372
   682
  {* simple transitivity reasoner *}
ballarin@15076
   683
method_setup rtrancl =
wenzelm@21589
   684
  {* Method.no_args (Method.SIMPLE_METHOD' Trancl_Tac.rtrancl_tac) *}
ballarin@15076
   685
  {* simple transitivity reasoner *}
berghofe@22262
   686
method_setup tranclp =
berghofe@22262
   687
  {* Method.no_args (Method.SIMPLE_METHOD' Tranclp_Tac.trancl_tac) *}
berghofe@22262
   688
  {* simple transitivity reasoner (predicate version) *}
berghofe@22262
   689
method_setup rtranclp =
berghofe@22262
   690
  {* Method.no_args (Method.SIMPLE_METHOD' Tranclp_Tac.rtrancl_tac) *}
berghofe@22262
   691
  {* simple transitivity reasoner (predicate version) *}
ballarin@15076
   692
nipkow@10213
   693
end