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