src/HOL/Transitive_Closure.thy
author wenzelm
Wed Apr 10 21:20:35 2013 +0200 (2013-04-10)
changeset 51692 ecd34f863242
parent 50616 5b6cf0fbc329
child 51717 9e7d1c139569
permissions -rw-r--r--
tuned pretty layout: avoid nested Pretty.string_of, which merely happens to work with Isabelle/jEdit since formatting is delegated to Scala side;
declare command "print_case_translations" where it is actually defined;
     1 (*  Title:      HOL/Transitive_Closure.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1992  University of Cambridge
     4 *)
     5 
     6 header {* Reflexive and Transitive closure of a relation *}
     7 
     8 theory Transitive_Closure
     9 imports Relation
    10 begin
    11 
    12 ML_file "~~/src/Provers/trancl.ML"
    13 
    14 text {*
    15   @{text rtrancl} is reflexive/transitive closure,
    16   @{text trancl} is transitive closure,
    17   @{text reflcl} is reflexive closure.
    18 
    19   These postfix operators have \emph{maximum priority}, forcing their
    20   operands to be atomic.
    21 *}
    22 
    23 inductive_set
    24   rtrancl :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"   ("(_^*)" [1000] 999)
    25   for r :: "('a \<times> 'a) set"
    26 where
    27     rtrancl_refl [intro!, Pure.intro!, simp]: "(a, a) : r^*"
    28   | rtrancl_into_rtrancl [Pure.intro]: "(a, b) : r^* ==> (b, c) : r ==> (a, c) : r^*"
    29 
    30 inductive_set
    31   trancl :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"  ("(_^+)" [1000] 999)
    32   for r :: "('a \<times> 'a) set"
    33 where
    34     r_into_trancl [intro, Pure.intro]: "(a, b) : r ==> (a, b) : r^+"
    35   | trancl_into_trancl [Pure.intro]: "(a, b) : r^+ ==> (b, c) : r ==> (a, c) : r^+"
    36 
    37 declare rtrancl_def [nitpick_unfold del]
    38         rtranclp_def [nitpick_unfold del]
    39         trancl_def [nitpick_unfold del]
    40         tranclp_def [nitpick_unfold del]
    41 
    42 notation
    43   rtranclp  ("(_^**)" [1000] 1000) and
    44   tranclp  ("(_^++)" [1000] 1000)
    45 
    46 abbreviation
    47   reflclp :: "('a => 'a => bool) => 'a => 'a => bool"  ("(_^==)" [1000] 1000) where
    48   "r^== \<equiv> sup r op ="
    49 
    50 abbreviation
    51   reflcl :: "('a \<times> 'a) set => ('a \<times> 'a) set"  ("(_^=)" [1000] 999) where
    52   "r^= \<equiv> r \<union> Id"
    53 
    54 notation (xsymbols)
    55   rtranclp  ("(_\<^sup>*\<^sup>*)" [1000] 1000) and
    56   tranclp  ("(_\<^sup>+\<^sup>+)" [1000] 1000) and
    57   reflclp  ("(_\<^sup>=\<^sup>=)" [1000] 1000) and
    58   rtrancl  ("(_\<^sup>*)" [1000] 999) and
    59   trancl  ("(_\<^sup>+)" [1000] 999) and
    60   reflcl  ("(_\<^sup>=)" [1000] 999)
    61 
    62 notation (HTML output)
    63   rtranclp  ("(_\<^sup>*\<^sup>*)" [1000] 1000) and
    64   tranclp  ("(_\<^sup>+\<^sup>+)" [1000] 1000) and
    65   reflclp  ("(_\<^sup>=\<^sup>=)" [1000] 1000) and
    66   rtrancl  ("(_\<^sup>*)" [1000] 999) and
    67   trancl  ("(_\<^sup>+)" [1000] 999) and
    68   reflcl  ("(_\<^sup>=)" [1000] 999)
    69 
    70 
    71 subsection {* Reflexive closure *}
    72 
    73 lemma refl_reflcl[simp]: "refl(r^=)"
    74 by(simp add:refl_on_def)
    75 
    76 lemma antisym_reflcl[simp]: "antisym(r^=) = antisym r"
    77 by(simp add:antisym_def)
    78 
    79 lemma trans_reflclI[simp]: "trans r \<Longrightarrow> trans(r^=)"
    80 unfolding trans_def by blast
    81 
    82 lemma reflclp_idemp [simp]: "(P^==)^==  =  P^=="
    83 by blast
    84 
    85 subsection {* Reflexive-transitive closure *}
    86 
    87 lemma reflcl_set_eq [pred_set_conv]: "(sup (\<lambda>x y. (x, y) \<in> r) op =) = (\<lambda>x y. (x, y) \<in> r \<union> Id)"
    88   by (auto simp add: fun_eq_iff)
    89 
    90 lemma r_into_rtrancl [intro]: "!!p. p \<in> r ==> p \<in> r^*"
    91   -- {* @{text rtrancl} of @{text r} contains @{text r} *}
    92   apply (simp only: split_tupled_all)
    93   apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])
    94   done
    95 
    96 lemma r_into_rtranclp [intro]: "r x y ==> r^** x y"
    97   -- {* @{text rtrancl} of @{text r} contains @{text r} *}
    98   by (erule rtranclp.rtrancl_refl [THEN rtranclp.rtrancl_into_rtrancl])
    99 
   100 lemma rtranclp_mono: "r \<le> s ==> r^** \<le> s^**"
   101   -- {* monotonicity of @{text rtrancl} *}
   102   apply (rule predicate2I)
   103   apply (erule rtranclp.induct)
   104    apply (rule_tac [2] rtranclp.rtrancl_into_rtrancl, blast+)
   105   done
   106 
   107 lemmas rtrancl_mono = rtranclp_mono [to_set]
   108 
   109 theorem rtranclp_induct [consumes 1, case_names base step, induct set: rtranclp]:
   110   assumes a: "r^** a b"
   111     and cases: "P a" "!!y z. [| r^** a y; r y z; P y |] ==> P z"
   112   shows "P b" using a
   113   by (induct x\<equiv>a b) (rule cases)+
   114 
   115 lemmas rtrancl_induct [induct set: rtrancl] = rtranclp_induct [to_set]
   116 
   117 lemmas rtranclp_induct2 =
   118   rtranclp_induct[of _ "(ax,ay)" "(bx,by)", split_rule,
   119                  consumes 1, case_names refl step]
   120 
   121 lemmas rtrancl_induct2 =
   122   rtrancl_induct[of "(ax,ay)" "(bx,by)", split_format (complete),
   123                  consumes 1, case_names refl step]
   124 
   125 lemma refl_rtrancl: "refl (r^*)"
   126 by (unfold refl_on_def) fast
   127 
   128 text {* Transitivity of transitive closure. *}
   129 lemma trans_rtrancl: "trans (r^*)"
   130 proof (rule transI)
   131   fix x y z
   132   assume "(x, y) \<in> r\<^sup>*"
   133   assume "(y, z) \<in> r\<^sup>*"
   134   then show "(x, z) \<in> r\<^sup>*"
   135   proof induct
   136     case base
   137     show "(x, y) \<in> r\<^sup>*" by fact
   138   next
   139     case (step u v)
   140     from `(x, u) \<in> r\<^sup>*` and `(u, v) \<in> r`
   141     show "(x, v) \<in> r\<^sup>*" ..
   142   qed
   143 qed
   144 
   145 lemmas rtrancl_trans = trans_rtrancl [THEN transD]
   146 
   147 lemma rtranclp_trans:
   148   assumes xy: "r^** x y"
   149   and yz: "r^** y z"
   150   shows "r^** x z" using yz xy
   151   by induct iprover+
   152 
   153 lemma rtranclE [cases set: rtrancl]:
   154   assumes major: "(a::'a, b) : r^*"
   155   obtains
   156     (base) "a = b"
   157   | (step) y where "(a, y) : r^*" and "(y, b) : r"
   158   -- {* elimination of @{text rtrancl} -- by induction on a special formula *}
   159   apply (subgoal_tac "(a::'a) = b | (EX y. (a,y) : r^* & (y,b) : r)")
   160    apply (rule_tac [2] major [THEN rtrancl_induct])
   161     prefer 2 apply blast
   162    prefer 2 apply blast
   163   apply (erule asm_rl exE disjE conjE base step)+
   164   done
   165 
   166 lemma rtrancl_Int_subset: "[| Id \<subseteq> s; (r^* \<inter> s) O r \<subseteq> s|] ==> r^* \<subseteq> s"
   167   apply (rule subsetI)
   168   apply (rule_tac p="x" in PairE, clarify)
   169   apply (erule rtrancl_induct, auto) 
   170   done
   171 
   172 lemma converse_rtranclp_into_rtranclp:
   173   "r a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>*\<^sup>* a c"
   174   by (rule rtranclp_trans) iprover+
   175 
   176 lemmas converse_rtrancl_into_rtrancl = converse_rtranclp_into_rtranclp [to_set]
   177 
   178 text {*
   179   \medskip More @{term "r^*"} equations and inclusions.
   180 *}
   181 
   182 lemma rtranclp_idemp [simp]: "(r^**)^** = r^**"
   183   apply (auto intro!: order_antisym)
   184   apply (erule rtranclp_induct)
   185    apply (rule rtranclp.rtrancl_refl)
   186   apply (blast intro: rtranclp_trans)
   187   done
   188 
   189 lemmas rtrancl_idemp [simp] = rtranclp_idemp [to_set]
   190 
   191 lemma rtrancl_idemp_self_comp [simp]: "R^* O R^* = R^*"
   192   apply (rule set_eqI)
   193   apply (simp only: split_tupled_all)
   194   apply (blast intro: rtrancl_trans)
   195   done
   196 
   197 lemma rtrancl_subset_rtrancl: "r \<subseteq> s^* ==> r^* \<subseteq> s^*"
   198   apply (drule rtrancl_mono)
   199   apply simp
   200   done
   201 
   202 lemma rtranclp_subset: "R \<le> S ==> S \<le> R^** ==> S^** = R^**"
   203   apply (drule rtranclp_mono)
   204   apply (drule rtranclp_mono)
   205   apply simp
   206   done
   207 
   208 lemmas rtrancl_subset = rtranclp_subset [to_set]
   209 
   210 lemma rtranclp_sup_rtranclp: "(sup (R^**) (S^**))^** = (sup R S)^**"
   211 by (blast intro!: rtranclp_subset intro: rtranclp_mono [THEN predicate2D])
   212 
   213 lemmas rtrancl_Un_rtrancl = rtranclp_sup_rtranclp [to_set]
   214 
   215 lemma rtranclp_reflclp [simp]: "(R^==)^** = R^**"
   216 by (blast intro!: rtranclp_subset)
   217 
   218 lemmas rtrancl_reflcl [simp] = rtranclp_reflclp [to_set]
   219 
   220 lemma rtrancl_r_diff_Id: "(r - Id)^* = r^*"
   221   apply (rule sym)
   222   apply (rule rtrancl_subset, blast, clarify)
   223   apply (rename_tac a b)
   224   apply (case_tac "a = b")
   225    apply blast
   226   apply blast
   227   done
   228 
   229 lemma rtranclp_r_diff_Id: "(inf r op ~=)^** = r^**"
   230   apply (rule sym)
   231   apply (rule rtranclp_subset)
   232    apply blast+
   233   done
   234 
   235 theorem rtranclp_converseD:
   236   assumes r: "(r^--1)^** x y"
   237   shows "r^** y x"
   238 proof -
   239   from r show ?thesis
   240     by induct (iprover intro: rtranclp_trans dest!: conversepD)+
   241 qed
   242 
   243 lemmas rtrancl_converseD = rtranclp_converseD [to_set]
   244 
   245 theorem rtranclp_converseI:
   246   assumes "r^** y x"
   247   shows "(r^--1)^** x y"
   248   using assms
   249   by induct (iprover intro: rtranclp_trans conversepI)+
   250 
   251 lemmas rtrancl_converseI = rtranclp_converseI [to_set]
   252 
   253 lemma rtrancl_converse: "(r^-1)^* = (r^*)^-1"
   254   by (fast dest!: rtrancl_converseD intro!: rtrancl_converseI)
   255 
   256 lemma sym_rtrancl: "sym r ==> sym (r^*)"
   257   by (simp only: sym_conv_converse_eq rtrancl_converse [symmetric])
   258 
   259 theorem converse_rtranclp_induct [consumes 1, case_names base step]:
   260   assumes major: "r^** a b"
   261     and cases: "P b" "!!y z. [| r y z; r^** z b; P z |] ==> P y"
   262   shows "P a"
   263   using rtranclp_converseI [OF major]
   264   by induct (iprover intro: cases dest!: conversepD rtranclp_converseD)+
   265 
   266 lemmas converse_rtrancl_induct = converse_rtranclp_induct [to_set]
   267 
   268 lemmas converse_rtranclp_induct2 =
   269   converse_rtranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
   270                  consumes 1, case_names refl step]
   271 
   272 lemmas converse_rtrancl_induct2 =
   273   converse_rtrancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
   274                  consumes 1, case_names refl step]
   275 
   276 lemma converse_rtranclpE [consumes 1, case_names base step]:
   277   assumes major: "r^** x z"
   278     and cases: "x=z ==> P"
   279       "!!y. [| r x y; r^** y z |] ==> P"
   280   shows P
   281   apply (subgoal_tac "x = z | (EX y. r x y & r^** y z)")
   282    apply (rule_tac [2] major [THEN converse_rtranclp_induct])
   283     prefer 2 apply iprover
   284    prefer 2 apply iprover
   285   apply (erule asm_rl exE disjE conjE cases)+
   286   done
   287 
   288 lemmas converse_rtranclE = converse_rtranclpE [to_set]
   289 
   290 lemmas converse_rtranclpE2 = converse_rtranclpE [of _ "(xa,xb)" "(za,zb)", split_rule]
   291 
   292 lemmas converse_rtranclE2 = converse_rtranclE [of "(xa,xb)" "(za,zb)", split_rule]
   293 
   294 lemma r_comp_rtrancl_eq: "r O r^* = r^* O r"
   295   by (blast elim: rtranclE converse_rtranclE
   296     intro: rtrancl_into_rtrancl converse_rtrancl_into_rtrancl)
   297 
   298 lemma rtrancl_unfold: "r^* = Id Un r^* O r"
   299   by (auto intro: rtrancl_into_rtrancl elim: rtranclE)
   300 
   301 lemma rtrancl_Un_separatorE:
   302   "(a,b) : (P \<union> Q)^* \<Longrightarrow> \<forall>x y. (a,x) : P^* \<longrightarrow> (x,y) : Q \<longrightarrow> x=y \<Longrightarrow> (a,b) : P^*"
   303 apply (induct rule:rtrancl.induct)
   304  apply blast
   305 apply (blast intro:rtrancl_trans)
   306 done
   307 
   308 lemma rtrancl_Un_separator_converseE:
   309   "(a,b) : (P \<union> Q)^* \<Longrightarrow> \<forall>x y. (x,b) : P^* \<longrightarrow> (y,x) : Q \<longrightarrow> y=x \<Longrightarrow> (a,b) : P^*"
   310 apply (induct rule:converse_rtrancl_induct)
   311  apply blast
   312 apply (blast intro:rtrancl_trans)
   313 done
   314 
   315 lemma Image_closed_trancl:
   316   assumes "r `` X \<subseteq> X" shows "r\<^sup>* `` X = X"
   317 proof -
   318   from assms have **: "{y. \<exists>x\<in>X. (x, y) \<in> r} \<subseteq> X" by auto
   319   have "\<And>x y. (y, x) \<in> r\<^sup>* \<Longrightarrow> y \<in> X \<Longrightarrow> x \<in> X"
   320   proof -
   321     fix x y
   322     assume *: "y \<in> X"
   323     assume "(y, x) \<in> r\<^sup>*"
   324     then show "x \<in> X"
   325     proof induct
   326       case base show ?case by (fact *)
   327     next
   328       case step with ** show ?case by auto
   329     qed
   330   qed
   331   then show ?thesis by auto
   332 qed
   333 
   334 
   335 subsection {* Transitive closure *}
   336 
   337 lemma trancl_mono: "!!p. p \<in> r^+ ==> r \<subseteq> s ==> p \<in> s^+"
   338   apply (simp add: split_tupled_all)
   339   apply (erule trancl.induct)
   340    apply (iprover dest: subsetD)+
   341   done
   342 
   343 lemma r_into_trancl': "!!p. p : r ==> p : r^+"
   344   by (simp only: split_tupled_all) (erule r_into_trancl)
   345 
   346 text {*
   347   \medskip Conversions between @{text trancl} and @{text rtrancl}.
   348 *}
   349 
   350 lemma tranclp_into_rtranclp: "r^++ a b ==> r^** a b"
   351   by (erule tranclp.induct) iprover+
   352 
   353 lemmas trancl_into_rtrancl = tranclp_into_rtranclp [to_set]
   354 
   355 lemma rtranclp_into_tranclp1: assumes r: "r^** a b"
   356   shows "!!c. r b c ==> r^++ a c" using r
   357   by induct iprover+
   358 
   359 lemmas rtrancl_into_trancl1 = rtranclp_into_tranclp1 [to_set]
   360 
   361 lemma rtranclp_into_tranclp2: "[| r a b; r^** b c |] ==> r^++ a c"
   362   -- {* intro rule from @{text r} and @{text rtrancl} *}
   363   apply (erule rtranclp.cases)
   364    apply iprover
   365   apply (rule rtranclp_trans [THEN rtranclp_into_tranclp1])
   366     apply (simp | rule r_into_rtranclp)+
   367   done
   368 
   369 lemmas rtrancl_into_trancl2 = rtranclp_into_tranclp2 [to_set]
   370 
   371 text {* Nice induction rule for @{text trancl} *}
   372 lemma tranclp_induct [consumes 1, case_names base step, induct pred: tranclp]:
   373   assumes a: "r^++ a b"
   374   and cases: "!!y. r a y ==> P y"
   375     "!!y z. r^++ a y ==> r y z ==> P y ==> P z"
   376   shows "P b" using a
   377   by (induct x\<equiv>a b) (iprover intro: cases)+
   378 
   379 lemmas trancl_induct [induct set: trancl] = tranclp_induct [to_set]
   380 
   381 lemmas tranclp_induct2 =
   382   tranclp_induct [of _ "(ax,ay)" "(bx,by)", split_rule,
   383     consumes 1, case_names base step]
   384 
   385 lemmas trancl_induct2 =
   386   trancl_induct [of "(ax,ay)" "(bx,by)", split_format (complete),
   387     consumes 1, case_names base step]
   388 
   389 lemma tranclp_trans_induct:
   390   assumes major: "r^++ x y"
   391     and cases: "!!x y. r x y ==> P x y"
   392       "!!x y z. [| r^++ x y; P x y; r^++ y z; P y z |] ==> P x z"
   393   shows "P x y"
   394   -- {* Another induction rule for trancl, incorporating transitivity *}
   395   by (iprover intro: major [THEN tranclp_induct] cases)
   396 
   397 lemmas trancl_trans_induct = tranclp_trans_induct [to_set]
   398 
   399 lemma tranclE [cases set: trancl]:
   400   assumes "(a, b) : r^+"
   401   obtains
   402     (base) "(a, b) : r"
   403   | (step) c where "(a, c) : r^+" and "(c, b) : r"
   404   using assms by cases simp_all
   405 
   406 lemma trancl_Int_subset: "[| r \<subseteq> s; (r^+ \<inter> s) O r \<subseteq> s|] ==> r^+ \<subseteq> s"
   407   apply (rule subsetI)
   408   apply (rule_tac p = x in PairE)
   409   apply clarify
   410   apply (erule trancl_induct)
   411    apply auto
   412   done
   413 
   414 lemma trancl_unfold: "r^+ = r Un r^+ O r"
   415   by (auto intro: trancl_into_trancl elim: tranclE)
   416 
   417 text {* Transitivity of @{term "r^+"} *}
   418 lemma trans_trancl [simp]: "trans (r^+)"
   419 proof (rule transI)
   420   fix x y z
   421   assume "(x, y) \<in> r^+"
   422   assume "(y, z) \<in> r^+"
   423   then show "(x, z) \<in> r^+"
   424   proof induct
   425     case (base u)
   426     from `(x, y) \<in> r^+` and `(y, u) \<in> r`
   427     show "(x, u) \<in> r^+" ..
   428   next
   429     case (step u v)
   430     from `(x, u) \<in> r^+` and `(u, v) \<in> r`
   431     show "(x, v) \<in> r^+" ..
   432   qed
   433 qed
   434 
   435 lemmas trancl_trans = trans_trancl [THEN transD]
   436 
   437 lemma tranclp_trans:
   438   assumes xy: "r^++ x y"
   439   and yz: "r^++ y z"
   440   shows "r^++ x z" using yz xy
   441   by induct iprover+
   442 
   443 lemma trancl_id [simp]: "trans r \<Longrightarrow> r^+ = r"
   444   apply auto
   445   apply (erule trancl_induct)
   446    apply assumption
   447   apply (unfold trans_def)
   448   apply blast
   449   done
   450 
   451 lemma rtranclp_tranclp_tranclp:
   452   assumes "r^** x y"
   453   shows "!!z. r^++ y z ==> r^++ x z" using assms
   454   by induct (iprover intro: tranclp_trans)+
   455 
   456 lemmas rtrancl_trancl_trancl = rtranclp_tranclp_tranclp [to_set]
   457 
   458 lemma tranclp_into_tranclp2: "r a b ==> r^++ b c ==> r^++ a c"
   459   by (erule tranclp_trans [OF tranclp.r_into_trancl])
   460 
   461 lemmas trancl_into_trancl2 = tranclp_into_tranclp2 [to_set]
   462 
   463 lemma trancl_insert:
   464   "(insert (y, x) r)^+ = r^+ \<union> {(a, b). (a, y) \<in> r^* \<and> (x, b) \<in> r^*}"
   465   -- {* primitive recursion for @{text trancl} over finite relations *}
   466   apply (rule equalityI)
   467    apply (rule subsetI)
   468    apply (simp only: split_tupled_all)
   469    apply (erule trancl_induct, blast)
   470    apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl trancl_trans)
   471   apply (rule subsetI)
   472   apply (blast intro: trancl_mono rtrancl_mono
   473     [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)
   474   done
   475 
   476 lemma tranclp_converseI: "(r^++)^--1 x y ==> (r^--1)^++ x y"
   477   apply (drule conversepD)
   478   apply (erule tranclp_induct)
   479   apply (iprover intro: conversepI tranclp_trans)+
   480   done
   481 
   482 lemmas trancl_converseI = tranclp_converseI [to_set]
   483 
   484 lemma tranclp_converseD: "(r^--1)^++ x y ==> (r^++)^--1 x y"
   485   apply (rule conversepI)
   486   apply (erule tranclp_induct)
   487   apply (iprover dest: conversepD intro: tranclp_trans)+
   488   done
   489 
   490 lemmas trancl_converseD = tranclp_converseD [to_set]
   491 
   492 lemma tranclp_converse: "(r^--1)^++ = (r^++)^--1"
   493   by (fastforce simp add: fun_eq_iff
   494     intro!: tranclp_converseI dest!: tranclp_converseD)
   495 
   496 lemmas trancl_converse = tranclp_converse [to_set]
   497 
   498 lemma sym_trancl: "sym r ==> sym (r^+)"
   499   by (simp only: sym_conv_converse_eq trancl_converse [symmetric])
   500 
   501 lemma converse_tranclp_induct [consumes 1, case_names base step]:
   502   assumes major: "r^++ a b"
   503     and cases: "!!y. r y b ==> P(y)"
   504       "!!y z.[| r y z;  r^++ z b;  P(z) |] ==> P(y)"
   505   shows "P a"
   506   apply (rule tranclp_induct [OF tranclp_converseI, OF conversepI, OF major])
   507    apply (rule cases)
   508    apply (erule conversepD)
   509   apply (blast intro: assms dest!: tranclp_converseD)
   510   done
   511 
   512 lemmas converse_trancl_induct = converse_tranclp_induct [to_set]
   513 
   514 lemma tranclpD: "R^++ x y ==> EX z. R x z \<and> R^** z y"
   515   apply (erule converse_tranclp_induct)
   516    apply auto
   517   apply (blast intro: rtranclp_trans)
   518   done
   519 
   520 lemmas tranclD = tranclpD [to_set]
   521 
   522 lemma converse_tranclpE:
   523   assumes major: "tranclp r x z"
   524   assumes base: "r x z ==> P"
   525   assumes step: "\<And> y. [| r x y; tranclp r y z |] ==> P"
   526   shows P
   527 proof -
   528   from tranclpD[OF major]
   529   obtain y where "r x y" and "rtranclp r y z" by iprover
   530   from this(2) show P
   531   proof (cases rule: rtranclp.cases)
   532     case rtrancl_refl
   533     with `r x y` base show P by iprover
   534   next
   535     case rtrancl_into_rtrancl
   536     from this have "tranclp r y z"
   537       by (iprover intro: rtranclp_into_tranclp1)
   538     with `r x y` step show P by iprover
   539   qed
   540 qed
   541 
   542 lemmas converse_tranclE = converse_tranclpE [to_set]
   543 
   544 lemma tranclD2:
   545   "(x, y) \<in> R\<^sup>+ \<Longrightarrow> \<exists>z. (x, z) \<in> R\<^sup>* \<and> (z, y) \<in> R"
   546   by (blast elim: tranclE intro: trancl_into_rtrancl)
   547 
   548 lemma irrefl_tranclI: "r^-1 \<inter> r^* = {} ==> (x, x) \<notin> r^+"
   549   by (blast elim: tranclE dest: trancl_into_rtrancl)
   550 
   551 lemma irrefl_trancl_rD: "!!X. ALL x. (x, x) \<notin> r^+ ==> (x, y) \<in> r ==> x \<noteq> y"
   552   by (blast dest: r_into_trancl)
   553 
   554 lemma trancl_subset_Sigma_aux:
   555     "(a, b) \<in> r^* ==> r \<subseteq> A \<times> A ==> a = b \<or> a \<in> A"
   556   by (induct rule: rtrancl_induct) auto
   557 
   558 lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A ==> r^+ \<subseteq> A \<times> A"
   559   apply (rule subsetI)
   560   apply (simp only: split_tupled_all)
   561   apply (erule tranclE)
   562    apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+
   563   done
   564 
   565 lemma reflclp_tranclp [simp]: "(r^++)^== = r^**"
   566   apply (safe intro!: order_antisym)
   567    apply (erule tranclp_into_rtranclp)
   568   apply (blast elim: rtranclp.cases dest: rtranclp_into_tranclp1)
   569   done
   570 
   571 lemmas reflcl_trancl [simp] = reflclp_tranclp [to_set]
   572 
   573 lemma trancl_reflcl [simp]: "(r^=)^+ = r^*"
   574   apply safe
   575    apply (drule trancl_into_rtrancl, simp)
   576   apply (erule rtranclE, safe)
   577    apply (rule r_into_trancl, simp)
   578   apply (rule rtrancl_into_trancl1)
   579    apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD], fast)
   580   done
   581 
   582 lemma rtrancl_trancl_reflcl [code]: "r^* = (r^+)^="
   583   by simp
   584 
   585 lemma trancl_empty [simp]: "{}^+ = {}"
   586   by (auto elim: trancl_induct)
   587 
   588 lemma rtrancl_empty [simp]: "{}^* = Id"
   589   by (rule subst [OF reflcl_trancl]) simp
   590 
   591 lemma rtranclpD: "R^** a b ==> a = b \<or> a \<noteq> b \<and> R^++ a b"
   592 by (force simp add: reflclp_tranclp [symmetric] simp del: reflclp_tranclp)
   593 
   594 lemmas rtranclD = rtranclpD [to_set]
   595 
   596 lemma rtrancl_eq_or_trancl:
   597   "(x,y) \<in> R\<^sup>* = (x=y \<or> x\<noteq>y \<and> (x,y) \<in> R\<^sup>+)"
   598   by (fast elim: trancl_into_rtrancl dest: rtranclD)
   599 
   600 lemma trancl_unfold_right: "r^+ = r^* O r"
   601 by (auto dest: tranclD2 intro: rtrancl_into_trancl1)
   602 
   603 lemma trancl_unfold_left: "r^+ = r O r^*"
   604 by (auto dest: tranclD intro: rtrancl_into_trancl2)
   605 
   606 
   607 text {* Simplifying nested closures *}
   608 
   609 lemma rtrancl_trancl_absorb[simp]: "(R^*)^+ = R^*"
   610 by (simp add: trans_rtrancl)
   611 
   612 lemma trancl_rtrancl_absorb[simp]: "(R^+)^* = R^*"
   613 by (subst reflcl_trancl[symmetric]) simp
   614 
   615 lemma rtrancl_reflcl_absorb[simp]: "(R^*)^= = R^*"
   616 by auto
   617 
   618 
   619 text {* @{text Domain} and @{text Range} *}
   620 
   621 lemma Domain_rtrancl [simp]: "Domain (R^*) = UNIV"
   622   by blast
   623 
   624 lemma Range_rtrancl [simp]: "Range (R^*) = UNIV"
   625   by blast
   626 
   627 lemma rtrancl_Un_subset: "(R^* \<union> S^*) \<subseteq> (R Un S)^*"
   628   by (rule rtrancl_Un_rtrancl [THEN subst]) fast
   629 
   630 lemma in_rtrancl_UnI: "x \<in> R^* \<or> x \<in> S^* ==> x \<in> (R \<union> S)^*"
   631   by (blast intro: subsetD [OF rtrancl_Un_subset])
   632 
   633 lemma trancl_domain [simp]: "Domain (r^+) = Domain r"
   634   by (unfold Domain_unfold) (blast dest: tranclD)
   635 
   636 lemma trancl_range [simp]: "Range (r^+) = Range r"
   637   unfolding Domain_converse [symmetric] by (simp add: trancl_converse [symmetric])
   638 
   639 lemma Not_Domain_rtrancl:
   640     "x ~: Domain R ==> ((x, y) : R^*) = (x = y)"
   641   apply auto
   642   apply (erule rev_mp)
   643   apply (erule rtrancl_induct)
   644    apply auto
   645   done
   646 
   647 lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
   648   apply clarify
   649   apply (erule trancl_induct)
   650    apply (auto simp add: Field_def)
   651   done
   652 
   653 lemma finite_trancl[simp]: "finite (r^+) = finite r"
   654   apply auto
   655    prefer 2
   656    apply (rule trancl_subset_Field2 [THEN finite_subset])
   657    apply (rule finite_SigmaI)
   658     prefer 3
   659     apply (blast intro: r_into_trancl' finite_subset)
   660    apply (auto simp add: finite_Field)
   661   done
   662 
   663 text {* More about converse @{text rtrancl} and @{text trancl}, should
   664   be merged with main body. *}
   665 
   666 lemma single_valued_confluent:
   667   "\<lbrakk> single_valued r; (x,y) \<in> r^*; (x,z) \<in> r^* \<rbrakk>
   668   \<Longrightarrow> (y,z) \<in> r^* \<or> (z,y) \<in> r^*"
   669   apply (erule rtrancl_induct)
   670   apply simp
   671   apply (erule disjE)
   672    apply (blast elim:converse_rtranclE dest:single_valuedD)
   673   apply(blast intro:rtrancl_trans)
   674   done
   675 
   676 lemma r_r_into_trancl: "(a, b) \<in> R ==> (b, c) \<in> R ==> (a, c) \<in> R^+"
   677   by (fast intro: trancl_trans)
   678 
   679 lemma trancl_into_trancl [rule_format]:
   680     "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r --> (a,c) \<in> r\<^sup>+"
   681   apply (erule trancl_induct)
   682    apply (fast intro: r_r_into_trancl)
   683   apply (fast intro: r_r_into_trancl trancl_trans)
   684   done
   685 
   686 lemma tranclp_rtranclp_tranclp:
   687     "r\<^sup>+\<^sup>+ a b ==> r\<^sup>*\<^sup>* b c ==> r\<^sup>+\<^sup>+ a c"
   688   apply (drule tranclpD)
   689   apply (elim exE conjE)
   690   apply (drule rtranclp_trans, assumption)
   691   apply (drule rtranclp_into_tranclp2, assumption, assumption)
   692   done
   693 
   694 lemmas trancl_rtrancl_trancl = tranclp_rtranclp_tranclp [to_set]
   695 
   696 lemmas transitive_closure_trans [trans] =
   697   r_r_into_trancl trancl_trans rtrancl_trans
   698   trancl.trancl_into_trancl trancl_into_trancl2
   699   rtrancl.rtrancl_into_rtrancl converse_rtrancl_into_rtrancl
   700   rtrancl_trancl_trancl trancl_rtrancl_trancl
   701 
   702 lemmas transitive_closurep_trans' [trans] =
   703   tranclp_trans rtranclp_trans
   704   tranclp.trancl_into_trancl tranclp_into_tranclp2
   705   rtranclp.rtrancl_into_rtrancl converse_rtranclp_into_rtranclp
   706   rtranclp_tranclp_tranclp tranclp_rtranclp_tranclp
   707 
   708 declare trancl_into_rtrancl [elim]
   709 
   710 subsection {* The power operation on relations *}
   711 
   712 text {* @{text "R ^^ n = R O ... O R"}, the n-fold composition of @{text R} *}
   713 
   714 overloading
   715   relpow == "compow :: nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"
   716   relpowp == "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool)"
   717 begin
   718 
   719 primrec relpow :: "nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" where
   720     "relpow 0 R = Id"
   721   | "relpow (Suc n) R = (R ^^ n) O R"
   722 
   723 primrec relpowp :: "nat \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool)" where
   724     "relpowp 0 R = HOL.eq"
   725   | "relpowp (Suc n) R = (R ^^ n) OO R"
   726 
   727 end
   728 
   729 lemma relpowp_relpow_eq [pred_set_conv]:
   730   fixes R :: "'a rel"
   731   shows "(\<lambda>x y. (x, y) \<in> R) ^^ n = (\<lambda>x y. (x, y) \<in> R ^^ n)"
   732   by (induct n) (simp_all add: relcompp_relcomp_eq)
   733 
   734 text {* for code generation *}
   735 
   736 definition relpow :: "nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" where
   737   relpow_code_def [code_abbrev]: "relpow = compow"
   738 
   739 definition relpowp :: "nat \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool)" where
   740   relpowp_code_def [code_abbrev]: "relpowp = compow"
   741 
   742 lemma [code]:
   743   "relpow (Suc n) R = (relpow n R) O R"
   744   "relpow 0 R = Id"
   745   by (simp_all add: relpow_code_def)
   746 
   747 lemma [code]:
   748   "relpowp (Suc n) R = (R ^^ n) OO R"
   749   "relpowp 0 R = HOL.eq"
   750   by (simp_all add: relpowp_code_def)
   751 
   752 hide_const (open) relpow
   753 hide_const (open) relpowp
   754 
   755 lemma relpow_1 [simp]:
   756   fixes R :: "('a \<times> 'a) set"
   757   shows "R ^^ 1 = R"
   758   by simp
   759 
   760 lemma relpowp_1 [simp]:
   761   fixes P :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   762   shows "P ^^ 1 = P"
   763   by (fact relpow_1 [to_pred])
   764 
   765 lemma relpow_0_I: 
   766   "(x, x) \<in> R ^^ 0"
   767   by simp
   768 
   769 lemma relpowp_0_I:
   770   "(P ^^ 0) x x"
   771   by (fact relpow_0_I [to_pred])
   772 
   773 lemma relpow_Suc_I:
   774   "(x, y) \<in>  R ^^ n \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
   775   by auto
   776 
   777 lemma relpowp_Suc_I:
   778   "(P ^^ n) x y \<Longrightarrow> P y z \<Longrightarrow> (P ^^ Suc n) x z"
   779   by (fact relpow_Suc_I [to_pred])
   780 
   781 lemma relpow_Suc_I2:
   782   "(x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ n \<Longrightarrow> (x, z) \<in> R ^^ Suc n"
   783   by (induct n arbitrary: z) (simp, fastforce)
   784 
   785 lemma relpowp_Suc_I2:
   786   "P x y \<Longrightarrow> (P ^^ n) y z \<Longrightarrow> (P ^^ Suc n) x z"
   787   by (fact relpow_Suc_I2 [to_pred])
   788 
   789 lemma relpow_0_E:
   790   "(x, y) \<in> R ^^ 0 \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
   791   by simp
   792 
   793 lemma relpowp_0_E:
   794   "(P ^^ 0) x y \<Longrightarrow> (x = y \<Longrightarrow> Q) \<Longrightarrow> Q"
   795   by (fact relpow_0_E [to_pred])
   796 
   797 lemma relpow_Suc_E:
   798   "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<And>y. (x, y) \<in> R ^^ n \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> P) \<Longrightarrow> P"
   799   by auto
   800 
   801 lemma relpowp_Suc_E:
   802   "(P ^^ Suc n) x z \<Longrightarrow> (\<And>y. (P ^^ n) x y \<Longrightarrow> P y z \<Longrightarrow> Q) \<Longrightarrow> Q"
   803   by (fact relpow_Suc_E [to_pred])
   804 
   805 lemma relpow_E:
   806   "(x, z) \<in>  R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
   807    \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in>  R ^^ m \<Longrightarrow> (y, z) \<in> R \<Longrightarrow> P)
   808    \<Longrightarrow> P"
   809   by (cases n) auto
   810 
   811 lemma relpowp_E:
   812   "(P ^^ n) x z \<Longrightarrow> (n = 0 \<Longrightarrow> x = z \<Longrightarrow> Q)
   813   \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (P ^^ m) x y \<Longrightarrow> P y z \<Longrightarrow> Q)
   814   \<Longrightarrow> Q"
   815   by (fact relpow_E [to_pred])
   816 
   817 lemma relpow_Suc_D2:
   818   "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<exists>y. (x, y) \<in> R \<and> (y, z) \<in> R ^^ n)"
   819   apply (induct n arbitrary: x z)
   820    apply (blast intro: relpow_0_I elim: relpow_0_E relpow_Suc_E)
   821   apply (blast intro: relpow_Suc_I elim: relpow_0_E relpow_Suc_E)
   822   done
   823 
   824 lemma relpowp_Suc_D2:
   825   "(P ^^ Suc n) x z \<Longrightarrow> \<exists>y. P x y \<and> (P ^^ n) y z"
   826   by (fact relpow_Suc_D2 [to_pred])
   827 
   828 lemma relpow_Suc_E2:
   829   "(x, z) \<in> R ^^ Suc n \<Longrightarrow> (\<And>y. (x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ n \<Longrightarrow> P) \<Longrightarrow> P"
   830   by (blast dest: relpow_Suc_D2)
   831 
   832 lemma relpowp_Suc_E2:
   833   "(P ^^ Suc n) x z \<Longrightarrow> (\<And>y. P x y \<Longrightarrow> (P ^^ n) y z \<Longrightarrow> Q) \<Longrightarrow> Q"
   834   by (fact relpow_Suc_E2 [to_pred])
   835 
   836 lemma relpow_Suc_D2':
   837   "\<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)"
   838   by (induct n) (simp_all, blast)
   839 
   840 lemma relpowp_Suc_D2':
   841   "\<forall>x y z. (P ^^ n) x y \<and> P y z \<longrightarrow> (\<exists>w. P x w \<and> (P ^^ n) w z)"
   842   by (fact relpow_Suc_D2' [to_pred])
   843 
   844 lemma relpow_E2:
   845   "(x, z) \<in> R ^^ n \<Longrightarrow>  (n = 0 \<Longrightarrow> x = z \<Longrightarrow> P)
   846      \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (y, z) \<in> R ^^ m \<Longrightarrow> P)
   847    \<Longrightarrow> P"
   848   apply (cases n, simp)
   849   apply (cut_tac n=nat and R=R in relpow_Suc_D2', simp, blast)
   850   done
   851 
   852 lemma relpowp_E2:
   853   "(P ^^ n) x z \<Longrightarrow> (n = 0 \<Longrightarrow> x = z \<Longrightarrow> Q)
   854     \<Longrightarrow> (\<And>y m. n = Suc m \<Longrightarrow> P x y \<Longrightarrow> (P ^^ m) y z \<Longrightarrow> Q)
   855   \<Longrightarrow> Q"
   856   by (fact relpow_E2 [to_pred])
   857 
   858 lemma relpow_add: "R ^^ (m+n) = R^^m O R^^n"
   859   by (induct n) auto
   860 
   861 lemma relpowp_add: "P ^^ (m + n) = P ^^ m OO P ^^ n"
   862   by (fact relpow_add [to_pred])
   863 
   864 lemma relpow_commute: "R O R ^^ n = R ^^ n O R"
   865   by (induct n) (simp, simp add: O_assoc [symmetric])
   866 
   867 lemma relpowp_commute: "P OO P ^^ n = P ^^ n OO P"
   868   by (fact relpow_commute [to_pred])
   869 
   870 lemma relpow_empty:
   871   "0 < n \<Longrightarrow> ({} :: ('a \<times> 'a) set) ^^ n = {}"
   872   by (cases n) auto
   873 
   874 lemma relpowp_bot:
   875   "0 < n \<Longrightarrow> (\<bottom> :: 'a \<Rightarrow> 'a \<Rightarrow> bool) ^^ n = \<bottom>"
   876   by (fact relpow_empty [to_pred])
   877 
   878 lemma rtrancl_imp_UN_relpow:
   879   assumes "p \<in> R^*"
   880   shows "p \<in> (\<Union>n. R ^^ n)"
   881 proof (cases p)
   882   case (Pair x y)
   883   with assms have "(x, y) \<in> R^*" by simp
   884   then have "(x, y) \<in> (\<Union>n. R ^^ n)" proof induct
   885     case base show ?case by (blast intro: relpow_0_I)
   886   next
   887     case step then show ?case by (blast intro: relpow_Suc_I)
   888   qed
   889   with Pair show ?thesis by simp
   890 qed
   891 
   892 lemma rtranclp_imp_Sup_relpowp:
   893   assumes "(P^**) x y"
   894   shows "(\<Squnion>n. P ^^ n) x y"
   895   using assms and rtrancl_imp_UN_relpow [to_pred] by blast
   896 
   897 lemma relpow_imp_rtrancl:
   898   assumes "p \<in> R ^^ n"
   899   shows "p \<in> R^*"
   900 proof (cases p)
   901   case (Pair x y)
   902   with assms have "(x, y) \<in> R ^^ n" by simp
   903   then have "(x, y) \<in> R^*" proof (induct n arbitrary: x y)
   904     case 0 then show ?case by simp
   905   next
   906     case Suc then show ?case
   907       by (blast elim: relpow_Suc_E intro: rtrancl_into_rtrancl)
   908   qed
   909   with Pair show ?thesis by simp
   910 qed
   911 
   912 lemma relpowp_imp_rtranclp:
   913   assumes "(P ^^ n) x y"
   914   shows "(P^**) x y"
   915   using assms and relpow_imp_rtrancl [to_pred] by blast
   916 
   917 lemma rtrancl_is_UN_relpow:
   918   "R^* = (\<Union>n. R ^^ n)"
   919   by (blast intro: rtrancl_imp_UN_relpow relpow_imp_rtrancl)
   920 
   921 lemma rtranclp_is_Sup_relpowp:
   922   "P^** = (\<Squnion>n. P ^^ n)"
   923   using rtrancl_is_UN_relpow [to_pred, of P] by auto
   924 
   925 lemma rtrancl_power:
   926   "p \<in> R^* \<longleftrightarrow> (\<exists>n. p \<in> R ^^ n)"
   927   by (simp add: rtrancl_is_UN_relpow)
   928 
   929 lemma rtranclp_power:
   930   "(P^**) x y \<longleftrightarrow> (\<exists>n. (P ^^ n) x y)"
   931   by (simp add: rtranclp_is_Sup_relpowp)
   932 
   933 lemma trancl_power:
   934   "p \<in> R^+ \<longleftrightarrow> (\<exists>n > 0. p \<in> R ^^ n)"
   935   apply (cases p)
   936   apply simp
   937   apply (rule iffI)
   938    apply (drule tranclD2)
   939    apply (clarsimp simp: rtrancl_is_UN_relpow)
   940    apply (rule_tac x="Suc n" in exI)
   941    apply (clarsimp simp: relcomp_unfold)
   942    apply fastforce
   943   apply clarsimp
   944   apply (case_tac n, simp)
   945   apply clarsimp
   946   apply (drule relpow_imp_rtrancl)
   947   apply (drule rtrancl_into_trancl1) apply auto
   948   done
   949 
   950 lemma tranclp_power:
   951   "(P^++) x y \<longleftrightarrow> (\<exists>n > 0. (P ^^ n) x y)"
   952   using trancl_power [to_pred, of P "(x, y)"] by simp
   953 
   954 lemma rtrancl_imp_relpow:
   955   "p \<in> R^* \<Longrightarrow> \<exists>n. p \<in> R ^^ n"
   956   by (auto dest: rtrancl_imp_UN_relpow)
   957 
   958 lemma rtranclp_imp_relpowp:
   959   "(P^**) x y \<Longrightarrow> \<exists>n. (P ^^ n) x y"
   960   by (auto dest: rtranclp_imp_Sup_relpowp)
   961 
   962 text{* By Sternagel/Thiemann: *}
   963 lemma relpow_fun_conv:
   964   "((a,b) \<in> R ^^ n) = (\<exists>f. f 0 = a \<and> f n = b \<and> (\<forall>i<n. (f i, f(Suc i)) \<in> R))"
   965 proof (induct n arbitrary: b)
   966   case 0 show ?case by auto
   967 next
   968   case (Suc n)
   969   show ?case
   970   proof (simp add: relcomp_unfold Suc)
   971     show "(\<exists>y. (\<exists>f. f 0 = a \<and> f n = y \<and> (\<forall>i<n. (f i,f(Suc i)) \<in> R)) \<and> (y,b) \<in> R)
   972      = (\<exists>f. f 0 = a \<and> f(Suc n) = b \<and> (\<forall>i<Suc n. (f i, f (Suc i)) \<in> R))"
   973     (is "?l = ?r")
   974     proof
   975       assume ?l
   976       then obtain c f where 1: "f 0 = a"  "f n = c"  "\<And>i. i < n \<Longrightarrow> (f i, f (Suc i)) \<in> R"  "(c,b) \<in> R" by auto
   977       let ?g = "\<lambda> m. if m = Suc n then b else f m"
   978       show ?r by (rule exI[of _ ?g], simp add: 1)
   979     next
   980       assume ?r
   981       then obtain f where 1: "f 0 = a"  "b = f (Suc n)"  "\<And>i. i < Suc n \<Longrightarrow> (f i, f (Suc i)) \<in> R" by auto
   982       show ?l by (rule exI[of _ "f n"], rule conjI, rule exI[of _ f], insert 1, auto)
   983     qed
   984   qed
   985 qed
   986 
   987 lemma relpowp_fun_conv:
   988   "(P ^^ n) x y \<longleftrightarrow> (\<exists>f. f 0 = x \<and> f n = y \<and> (\<forall>i<n. P (f i) (f (Suc i))))"
   989   by (fact relpow_fun_conv [to_pred])
   990 
   991 lemma relpow_finite_bounded1:
   992 assumes "finite(R :: ('a*'a)set)" and "k>0"
   993 shows "R^^k \<subseteq> (UN n:{n. 0<n & n <= card R}. R^^n)" (is "_ \<subseteq> ?r")
   994 proof-
   995   { fix a b k
   996     have "(a,b) : R^^(Suc k) \<Longrightarrow> EX n. 0<n & n <= card R & (a,b) : R^^n"
   997     proof(induct k arbitrary: b)
   998       case 0
   999       hence "R \<noteq> {}" by auto
  1000       with card_0_eq[OF `finite R`] have "card R >= Suc 0" by auto
  1001       thus ?case using 0 by force
  1002     next
  1003       case (Suc k)
  1004       then obtain a' where "(a,a') : R^^(Suc k)" and "(a',b) : R" by auto
  1005       from Suc(1)[OF `(a,a') : R^^(Suc k)`]
  1006       obtain n where "n \<le> card R" and "(a,a') \<in> R ^^ n" by auto
  1007       have "(a,b) : R^^(Suc n)" using `(a,a') \<in> R^^n` and `(a',b)\<in> R` by auto
  1008       { assume "n < card R"
  1009         hence ?case using `(a,b): R^^(Suc n)` Suc_leI[OF `n < card R`] by blast
  1010       } moreover
  1011       { assume "n = card R"
  1012         from `(a,b) \<in> R ^^ (Suc n)`[unfolded relpow_fun_conv]
  1013         obtain f where "f 0 = a" and "f(Suc n) = b"
  1014           and steps: "\<And>i. i <= n \<Longrightarrow> (f i, f (Suc i)) \<in> R" by auto
  1015         let ?p = "%i. (f i, f(Suc i))"
  1016         let ?N = "{i. i \<le> n}"
  1017         have "?p ` ?N <= R" using steps by auto
  1018         from card_mono[OF assms(1) this]
  1019         have "card(?p ` ?N) <= card R" .
  1020         also have "\<dots> < card ?N" using `n = card R` by simp
  1021         finally have "~ inj_on ?p ?N" by(rule pigeonhole)
  1022         then obtain i j where i: "i <= n" and j: "j <= n" and ij: "i \<noteq> j" and
  1023           pij: "?p i = ?p j" by(auto simp: inj_on_def)
  1024         let ?i = "min i j" let ?j = "max i j"
  1025         have i: "?i <= n" and j: "?j <= n" and pij: "?p ?i = ?p ?j" 
  1026           and ij: "?i < ?j"
  1027           using i j ij pij unfolding min_def max_def by auto
  1028         from i j pij ij obtain i j where i: "i<=n" and j: "j<=n" and ij: "i<j"
  1029           and pij: "?p i = ?p j" by blast
  1030         let ?g = "\<lambda> l. if l \<le> i then f l else f (l + (j - i))"
  1031         let ?n = "Suc(n - (j - i))"
  1032         have abl: "(a,b) \<in> R ^^ ?n" unfolding relpow_fun_conv
  1033         proof (rule exI[of _ ?g], intro conjI impI allI)
  1034           show "?g ?n = b" using `f(Suc n) = b` j ij by auto
  1035         next
  1036           fix k assume "k < ?n"
  1037           show "(?g k, ?g (Suc k)) \<in> R"
  1038           proof (cases "k < i")
  1039             case True
  1040             with i have "k <= n" by auto
  1041             from steps[OF this] show ?thesis using True by simp
  1042           next
  1043             case False
  1044             hence "i \<le> k" by auto
  1045             show ?thesis
  1046             proof (cases "k = i")
  1047               case True
  1048               thus ?thesis using ij pij steps[OF i] by simp
  1049             next
  1050               case False
  1051               with `i \<le> k` have "i < k" by auto
  1052               hence small: "k + (j - i) <= n" using `k<?n` by arith
  1053               show ?thesis using steps[OF small] `i<k` by auto
  1054             qed
  1055           qed
  1056         qed (simp add: `f 0 = a`)
  1057         moreover have "?n <= n" using i j ij by arith
  1058         ultimately have ?case using `n = card R` by blast
  1059       }
  1060       ultimately show ?case using `n \<le> card R` by force
  1061     qed
  1062   }
  1063   thus ?thesis using gr0_implies_Suc[OF `k>0`] by auto
  1064 qed
  1065 
  1066 lemma relpow_finite_bounded:
  1067 assumes "finite(R :: ('a*'a)set)"
  1068 shows "R^^k \<subseteq> (UN n:{n. n <= card R}. R^^n)"
  1069 apply(cases k)
  1070  apply force
  1071 using relpow_finite_bounded1[OF assms, of k] by auto
  1072 
  1073 lemma rtrancl_finite_eq_relpow:
  1074   "finite R \<Longrightarrow> R^* = (UN n : {n. n <= card R}. R^^n)"
  1075 by(fastforce simp: rtrancl_power dest: relpow_finite_bounded)
  1076 
  1077 lemma trancl_finite_eq_relpow:
  1078   "finite R \<Longrightarrow> R^+ = (UN n : {n. 0 < n & n <= card R}. R^^n)"
  1079 apply(auto simp add: trancl_power)
  1080 apply(auto dest: relpow_finite_bounded1)
  1081 done
  1082 
  1083 lemma finite_relcomp[simp,intro]:
  1084 assumes "finite R" and "finite S"
  1085 shows "finite(R O S)"
  1086 proof-
  1087   have "R O S = (UN (x,y) : R. \<Union>((%(u,v). if u=y then {(x,v)} else {}) ` S))"
  1088     by(force simp add: split_def)
  1089   thus ?thesis using assms by(clarsimp)
  1090 qed
  1091 
  1092 lemma finite_relpow[simp,intro]:
  1093   assumes "finite(R :: ('a*'a)set)" shows "n>0 \<Longrightarrow> finite(R^^n)"
  1094 apply(induct n)
  1095  apply simp
  1096 apply(case_tac n)
  1097  apply(simp_all add: assms)
  1098 done
  1099 
  1100 lemma single_valued_relpow:
  1101   fixes R :: "('a * 'a) set"
  1102   shows "single_valued R \<Longrightarrow> single_valued (R ^^ n)"
  1103 apply (induct n arbitrary: R)
  1104 apply simp_all
  1105 apply (rule single_valuedI)
  1106 apply (fast dest: single_valuedD elim: relpow_Suc_E)
  1107 done
  1108 
  1109 
  1110 subsection {* Bounded transitive closure *}
  1111 
  1112 definition ntrancl :: "nat \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set"
  1113 where
  1114   "ntrancl n R = (\<Union>i\<in>{i. 0 < i \<and> i \<le> Suc n}. R ^^ i)"
  1115 
  1116 lemma ntrancl_Zero [simp, code]:
  1117   "ntrancl 0 R = R"
  1118 proof
  1119   show "R \<subseteq> ntrancl 0 R"
  1120     unfolding ntrancl_def by fastforce
  1121 next
  1122   { 
  1123     fix i have "0 < i \<and> i \<le> Suc 0 \<longleftrightarrow> i = 1" by auto
  1124   }
  1125   from this show "ntrancl 0 R \<le> R"
  1126     unfolding ntrancl_def by auto
  1127 qed
  1128 
  1129 lemma ntrancl_Suc [simp]:
  1130   "ntrancl (Suc n) R = ntrancl n R O (Id \<union> R)"
  1131 proof
  1132   {
  1133     fix a b
  1134     assume "(a, b) \<in> ntrancl (Suc n) R"
  1135     from this obtain i where "0 < i" "i \<le> Suc (Suc n)" "(a, b) \<in> R ^^ i"
  1136       unfolding ntrancl_def by auto
  1137     have "(a, b) \<in> ntrancl n R O (Id \<union> R)"
  1138     proof (cases "i = 1")
  1139       case True
  1140       from this `(a, b) \<in> R ^^ i` show ?thesis
  1141         unfolding ntrancl_def by auto
  1142     next
  1143       case False
  1144       from this `0 < i` obtain j where j: "i = Suc j" "0 < j"
  1145         by (cases i) auto
  1146       from this `(a, b) \<in> R ^^ i` obtain c where c1: "(a, c) \<in> R ^^ j" and c2:"(c, b) \<in> R"
  1147         by auto
  1148       from c1 j `i \<le> Suc (Suc n)` have "(a, c) \<in> ntrancl n R"
  1149         unfolding ntrancl_def by fastforce
  1150       from this c2 show ?thesis by fastforce
  1151     qed
  1152   }
  1153   from this show "ntrancl (Suc n) R \<subseteq> ntrancl n R O (Id \<union> R)"
  1154     by auto
  1155 next
  1156   show "ntrancl n R O (Id \<union> R) \<subseteq> ntrancl (Suc n) R"
  1157     unfolding ntrancl_def by fastforce
  1158 qed
  1159 
  1160 lemma [code]:
  1161   "ntrancl (Suc n) r = (let r' = ntrancl n r in r' Un r' O r)"
  1162 unfolding Let_def by auto
  1163 
  1164 lemma finite_trancl_ntranl:
  1165   "finite R \<Longrightarrow> trancl R = ntrancl (card R - 1) R"
  1166   by (cases "card R") (auto simp add: trancl_finite_eq_relpow relpow_empty ntrancl_def)
  1167 
  1168 
  1169 subsection {* Acyclic relations *}
  1170 
  1171 definition acyclic :: "('a * 'a) set => bool" where
  1172   "acyclic r \<longleftrightarrow> (!x. (x,x) ~: r^+)"
  1173 
  1174 abbreviation acyclicP :: "('a => 'a => bool) => bool" where
  1175   "acyclicP r \<equiv> acyclic {(x, y). r x y}"
  1176 
  1177 lemma acyclic_irrefl [code]:
  1178   "acyclic r \<longleftrightarrow> irrefl (r^+)"
  1179   by (simp add: acyclic_def irrefl_def)
  1180 
  1181 lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
  1182   by (simp add: acyclic_def)
  1183 
  1184 lemma acyclic_insert [iff]:
  1185      "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
  1186 apply (simp add: acyclic_def trancl_insert)
  1187 apply (blast intro: rtrancl_trans)
  1188 done
  1189 
  1190 lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
  1191 by (simp add: acyclic_def trancl_converse)
  1192 
  1193 lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
  1194 
  1195 lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
  1196 apply (simp add: acyclic_def antisym_def)
  1197 apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
  1198 done
  1199 
  1200 (* Other direction:
  1201 acyclic = no loops
  1202 antisym = only self loops
  1203 Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
  1204 ==> antisym( r^* ) = acyclic(r - Id)";
  1205 *)
  1206 
  1207 lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
  1208 apply (simp add: acyclic_def)
  1209 apply (blast intro: trancl_mono)
  1210 done
  1211 
  1212 
  1213 subsection {* Setup of transitivity reasoner *}
  1214 
  1215 ML {*
  1216 
  1217 structure Trancl_Tac = Trancl_Tac
  1218 (
  1219   val r_into_trancl = @{thm trancl.r_into_trancl};
  1220   val trancl_trans  = @{thm trancl_trans};
  1221   val rtrancl_refl = @{thm rtrancl.rtrancl_refl};
  1222   val r_into_rtrancl = @{thm r_into_rtrancl};
  1223   val trancl_into_rtrancl = @{thm trancl_into_rtrancl};
  1224   val rtrancl_trancl_trancl = @{thm rtrancl_trancl_trancl};
  1225   val trancl_rtrancl_trancl = @{thm trancl_rtrancl_trancl};
  1226   val rtrancl_trans = @{thm rtrancl_trans};
  1227 
  1228   fun decomp (@{const Trueprop} $ t) =
  1229     let fun dec (Const (@{const_name Set.member}, _) $ (Const (@{const_name Pair}, _) $ a $ b) $ rel ) =
  1230         let fun decr (Const ("Transitive_Closure.rtrancl", _ ) $ r) = (r,"r*")
  1231               | decr (Const ("Transitive_Closure.trancl", _ ) $ r)  = (r,"r+")
  1232               | decr r = (r,"r");
  1233             val (rel,r) = decr (Envir.beta_eta_contract rel);
  1234         in SOME (a,b,rel,r) end
  1235       | dec _ =  NONE
  1236     in dec t end
  1237     | decomp _ = NONE;
  1238 );
  1239 
  1240 structure Tranclp_Tac = Trancl_Tac
  1241 (
  1242   val r_into_trancl = @{thm tranclp.r_into_trancl};
  1243   val trancl_trans  = @{thm tranclp_trans};
  1244   val rtrancl_refl = @{thm rtranclp.rtrancl_refl};
  1245   val r_into_rtrancl = @{thm r_into_rtranclp};
  1246   val trancl_into_rtrancl = @{thm tranclp_into_rtranclp};
  1247   val rtrancl_trancl_trancl = @{thm rtranclp_tranclp_tranclp};
  1248   val trancl_rtrancl_trancl = @{thm tranclp_rtranclp_tranclp};
  1249   val rtrancl_trans = @{thm rtranclp_trans};
  1250 
  1251   fun decomp (@{const Trueprop} $ t) =
  1252     let fun dec (rel $ a $ b) =
  1253         let fun decr (Const ("Transitive_Closure.rtranclp", _ ) $ r) = (r,"r*")
  1254               | decr (Const ("Transitive_Closure.tranclp", _ ) $ r)  = (r,"r+")
  1255               | decr r = (r,"r");
  1256             val (rel,r) = decr rel;
  1257         in SOME (a, b, rel, r) end
  1258       | dec _ =  NONE
  1259     in dec t end
  1260     | decomp _ = NONE;
  1261 );
  1262 *}
  1263 
  1264 setup {*
  1265   Simplifier.map_simpset_global (fn ss => ss
  1266     addSolver (mk_solver "Trancl" (Trancl_Tac.trancl_tac o Simplifier.the_context))
  1267     addSolver (mk_solver "Rtrancl" (Trancl_Tac.rtrancl_tac o Simplifier.the_context))
  1268     addSolver (mk_solver "Tranclp" (Tranclp_Tac.trancl_tac o Simplifier.the_context))
  1269     addSolver (mk_solver "Rtranclp" (Tranclp_Tac.rtrancl_tac o Simplifier.the_context)))
  1270 *}
  1271 
  1272 
  1273 text {* Optional methods. *}
  1274 
  1275 method_setup trancl =
  1276   {* Scan.succeed (SIMPLE_METHOD' o Trancl_Tac.trancl_tac) *}
  1277   {* simple transitivity reasoner *}
  1278 method_setup rtrancl =
  1279   {* Scan.succeed (SIMPLE_METHOD' o Trancl_Tac.rtrancl_tac) *}
  1280   {* simple transitivity reasoner *}
  1281 method_setup tranclp =
  1282   {* Scan.succeed (SIMPLE_METHOD' o Tranclp_Tac.trancl_tac) *}
  1283   {* simple transitivity reasoner (predicate version) *}
  1284 method_setup rtranclp =
  1285   {* Scan.succeed (SIMPLE_METHOD' o Tranclp_Tac.rtrancl_tac) *}
  1286   {* simple transitivity reasoner (predicate version) *}
  1287 
  1288 end
  1289