src/HOL/Transitive_Closure.thy
changeset 71628 1f957615cae6
parent 71627 2a24c2015a61
child 74345 e5ff77db6f38
equal deleted inserted replaced
71626:4c8edd527940 71628:1f957615cae6
    85 lemma reflcl_set_eq [pred_set_conv]: "(sup (\<lambda>x y. (x, y) \<in> r) (=)) = (\<lambda>x y. (x, y) \<in> r \<union> Id)"
    85 lemma reflcl_set_eq [pred_set_conv]: "(sup (\<lambda>x y. (x, y) \<in> r) (=)) = (\<lambda>x y. (x, y) \<in> r \<union> Id)"
    86   by (auto simp: fun_eq_iff)
    86   by (auto simp: fun_eq_iff)
    87 
    87 
    88 lemma r_into_rtrancl [intro]: "\<And>p. p \<in> r \<Longrightarrow> p \<in> r\<^sup>*"
    88 lemma r_into_rtrancl [intro]: "\<And>p. p \<in> r \<Longrightarrow> p \<in> r\<^sup>*"
    89   \<comment> \<open>\<open>rtrancl\<close> of \<open>r\<close> contains \<open>r\<close>\<close>
    89   \<comment> \<open>\<open>rtrancl\<close> of \<open>r\<close> contains \<open>r\<close>\<close>
    90   apply (simp only: split_tupled_all)
    90   by (simp add: split_tupled_all rtrancl_refl [THEN rtrancl_into_rtrancl])
    91   apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])
       
    92   done
       
    93 
    91 
    94 lemma r_into_rtranclp [intro]: "r x y \<Longrightarrow> r\<^sup>*\<^sup>* x y"
    92 lemma r_into_rtranclp [intro]: "r x y \<Longrightarrow> r\<^sup>*\<^sup>* x y"
    95   \<comment> \<open>\<open>rtrancl\<close> of \<open>r\<close> contains \<open>r\<close>\<close>
    93   \<comment> \<open>\<open>rtrancl\<close> of \<open>r\<close> contains \<open>r\<close>\<close>
    96   by (erule rtranclp.rtrancl_refl [THEN rtranclp.rtrancl_into_rtrancl])
    94   by (erule rtranclp.rtrancl_refl [THEN rtranclp.rtrancl_into_rtrancl])
    97 
    95 
    98 lemma rtranclp_mono: "r \<le> s \<Longrightarrow> r\<^sup>*\<^sup>* \<le> s\<^sup>*\<^sup>*"
    96 lemma rtranclp_mono: "r \<le> s \<Longrightarrow> r\<^sup>*\<^sup>* \<le> s\<^sup>*\<^sup>*"
    99   \<comment> \<open>monotonicity of \<open>rtrancl\<close>\<close>
    97   \<comment> \<open>monotonicity of \<open>rtrancl\<close>\<close>
   100   apply (rule predicate2I)
    98 proof (rule predicate2I)
   101   apply (erule rtranclp.induct)
    99   show "s\<^sup>*\<^sup>* x y" if "r \<le> s" "r\<^sup>*\<^sup>* x y" for x y
   102    apply (rule_tac [2] rtranclp.rtrancl_into_rtrancl, blast+)
   100     using \<open>r\<^sup>*\<^sup>* x y\<close> \<open>r \<le> s\<close>
   103   done
   101     by (induction rule: rtranclp.induct) (blast intro: rtranclp.rtrancl_into_rtrancl)+
       
   102 qed
   104 
   103 
   105 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"
   104 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"
   106    using rtranclp_mono[of x y] by auto
   105    using rtranclp_mono[of x y] by auto
   107 
   106 
   108 lemmas rtrancl_mono = rtranclp_mono [to_set]
   107 lemmas rtrancl_mono = rtranclp_mono [to_set]
   162   then show ?thesis
   161   then show ?thesis
   163     by (auto intro: base step)
   162     by (auto intro: base step)
   164 qed
   163 qed
   165 
   164 
   166 lemma rtrancl_Int_subset: "Id \<subseteq> s \<Longrightarrow> (r\<^sup>* \<inter> s) O r \<subseteq> s \<Longrightarrow> r\<^sup>* \<subseteq> s"
   165 lemma rtrancl_Int_subset: "Id \<subseteq> s \<Longrightarrow> (r\<^sup>* \<inter> s) O r \<subseteq> s \<Longrightarrow> r\<^sup>* \<subseteq> s"
   167   apply clarify
   166   by (fastforce elim: rtrancl_induct)
   168   apply (erule rtrancl_induct, auto)
       
   169   done
       
   170 
   167 
   171 lemma converse_rtranclp_into_rtranclp: "r a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>*\<^sup>* a c"
   168 lemma converse_rtranclp_into_rtranclp: "r a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>*\<^sup>* a c"
   172   by (rule rtranclp_trans) iprover+
   169   by (rule rtranclp_trans) iprover+
   173 
   170 
   174 lemmas converse_rtrancl_into_rtrancl = converse_rtranclp_into_rtranclp [to_set]
   171 lemmas converse_rtrancl_into_rtrancl = converse_rtranclp_into_rtranclp [to_set]
   175 
   172 
   176 text \<open>\<^medskip> More \<^term>\<open>r\<^sup>*\<close> equations and inclusions.\<close>
   173 text \<open>\<^medskip> More \<^term>\<open>r\<^sup>*\<close> equations and inclusions.\<close>
   177 
   174 
   178 lemma rtranclp_idemp [simp]: "(r\<^sup>*\<^sup>*)\<^sup>*\<^sup>* = r\<^sup>*\<^sup>*"
   175 lemma rtranclp_idemp [simp]: "(r\<^sup>*\<^sup>*)\<^sup>*\<^sup>* = r\<^sup>*\<^sup>*"
   179   apply (auto intro!: order_antisym)
   176 proof -
   180   apply (erule rtranclp_induct)
   177   have "r\<^sup>*\<^sup>*\<^sup>*\<^sup>* x y \<Longrightarrow> r\<^sup>*\<^sup>* x y" for x y
   181    apply (rule rtranclp.rtrancl_refl)
   178     by (induction rule: rtranclp_induct) (blast intro: rtranclp_trans)+
   182   apply (blast intro: rtranclp_trans)
   179   then show ?thesis
   183   done
   180     by (auto intro!: order_antisym)
       
   181 qed
   184 
   182 
   185 lemmas rtrancl_idemp [simp] = rtranclp_idemp [to_set]
   183 lemmas rtrancl_idemp [simp] = rtranclp_idemp [to_set]
   186 
   184 
   187 lemma rtrancl_idemp_self_comp [simp]: "R\<^sup>* O R\<^sup>* = R\<^sup>*"
   185 lemma rtrancl_idemp_self_comp [simp]: "R\<^sup>* O R\<^sup>* = R\<^sup>*"
   188   apply (rule set_eqI)
   186   by (force intro: rtrancl_trans)
   189   apply (simp only: split_tupled_all)
       
   190   apply (blast intro: rtrancl_trans)
       
   191   done
       
   192 
   187 
   193 lemma rtrancl_subset_rtrancl: "r \<subseteq> s\<^sup>* \<Longrightarrow> r\<^sup>* \<subseteq> s\<^sup>*"
   188 lemma rtrancl_subset_rtrancl: "r \<subseteq> s\<^sup>* \<Longrightarrow> r\<^sup>* \<subseteq> s\<^sup>*"
   194 by (drule rtrancl_mono, simp)
   189   by (drule rtrancl_mono, simp)
   195 
   190 
   196 lemma rtranclp_subset: "R \<le> S \<Longrightarrow> S \<le> R\<^sup>*\<^sup>* \<Longrightarrow> S\<^sup>*\<^sup>* = R\<^sup>*\<^sup>*"
   191 lemma rtranclp_subset: "R \<le> S \<Longrightarrow> S \<le> R\<^sup>*\<^sup>* \<Longrightarrow> S\<^sup>*\<^sup>* = R\<^sup>*\<^sup>*"
   197   apply (drule rtranclp_mono)
   192   by (fastforce dest: rtranclp_mono)
   198   apply (drule rtranclp_mono, simp)
       
   199   done
       
   200 
   193 
   201 lemmas rtrancl_subset = rtranclp_subset [to_set]
   194 lemmas rtrancl_subset = rtranclp_subset [to_set]
   202 
   195 
   203 lemma rtranclp_sup_rtranclp: "(sup (R\<^sup>*\<^sup>*) (S\<^sup>*\<^sup>*))\<^sup>*\<^sup>* = (sup R S)\<^sup>*\<^sup>*"
   196 lemma rtranclp_sup_rtranclp: "(sup (R\<^sup>*\<^sup>*) (S\<^sup>*\<^sup>*))\<^sup>*\<^sup>* = (sup R S)\<^sup>*\<^sup>*"
   204   by (blast intro!: rtranclp_subset intro: rtranclp_mono [THEN predicate2D])
   197   by (blast intro!: rtranclp_subset intro: rtranclp_mono [THEN predicate2D])
   317 qed
   310 qed
   318 
   311 
   319 
   312 
   320 subsection \<open>Transitive closure\<close>
   313 subsection \<open>Transitive closure\<close>
   321 
   314 
   322 lemma trancl_mono: "\<And>p. p \<in> r\<^sup>+ \<Longrightarrow> r \<subseteq> s \<Longrightarrow> p \<in> s\<^sup>+"
   315 lemma trancl_mono:
   323   apply (simp add: split_tupled_all)
   316   assumes "p \<in> r\<^sup>+" "r \<subseteq> s"
   324   apply (erule trancl.induct)
   317   shows "p \<in> s\<^sup>+"
   325    apply (iprover dest: subsetD)+
   318 proof -
   326   done
   319   have "\<lbrakk>(a, b) \<in> r\<^sup>+; r \<subseteq> s\<rbrakk> \<Longrightarrow> (a, b) \<in> s\<^sup>+" for a b
       
   320     by (induction rule: trancl.induct) (iprover dest: subsetD)+
       
   321   with assms show ?thesis
       
   322     by (cases p) force
       
   323 qed
   327 
   324 
   328 lemma r_into_trancl': "\<And>p. p \<in> r \<Longrightarrow> p \<in> r\<^sup>+"
   325 lemma r_into_trancl': "\<And>p. p \<in> r \<Longrightarrow> p \<in> r\<^sup>+"
   329   by (simp only: split_tupled_all) (erule r_into_trancl)
   326   by (simp only: split_tupled_all) (erule r_into_trancl)
   330 
   327 
   331 text \<open>\<^medskip> Conversions between \<open>trancl\<close> and \<open>rtrancl\<close>.\<close>
   328 text \<open>\<^medskip> Conversions between \<open>trancl\<close> and \<open>rtrancl\<close>.\<close>
   340   shows "r b c \<Longrightarrow> r\<^sup>+\<^sup>+ a c"
   337   shows "r b c \<Longrightarrow> r\<^sup>+\<^sup>+ a c"
   341   using assms by (induct arbitrary: c) iprover+
   338   using assms by (induct arbitrary: c) iprover+
   342 
   339 
   343 lemmas rtrancl_into_trancl1 = rtranclp_into_tranclp1 [to_set]
   340 lemmas rtrancl_into_trancl1 = rtranclp_into_tranclp1 [to_set]
   344 
   341 
   345 lemma rtranclp_into_tranclp2: "r a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>+\<^sup>+ a c"
   342 lemma rtranclp_into_tranclp2:
       
   343   assumes "r a b" "r\<^sup>*\<^sup>* b c" shows "r\<^sup>+\<^sup>+ a c"
   346   \<comment> \<open>intro rule from \<open>r\<close> and \<open>rtrancl\<close>\<close>
   344   \<comment> \<open>intro rule from \<open>r\<close> and \<open>rtrancl\<close>\<close>
   347   apply (erule rtranclp.cases, iprover)
   345   using \<open>r\<^sup>*\<^sup>* b c\<close>
   348   apply (rule rtranclp_trans [THEN rtranclp_into_tranclp1])
   346 proof (cases rule: rtranclp.cases)
   349     apply (simp | rule r_into_rtranclp)+
   347   case rtrancl_refl
   350   done
   348   with assms show ?thesis
       
   349     by iprover
       
   350 next
       
   351   case rtrancl_into_rtrancl
       
   352   with assms show ?thesis
       
   353     by (auto intro: rtranclp_trans [THEN rtranclp_into_tranclp1])
       
   354 qed
   351 
   355 
   352 lemmas rtrancl_into_trancl2 = rtranclp_into_tranclp2 [to_set]
   356 lemmas rtrancl_into_trancl2 = rtranclp_into_tranclp2 [to_set]
   353 
   357 
   354 text \<open>Nice induction rule for \<open>trancl\<close>\<close>
   358 text \<open>Nice induction rule for \<open>trancl\<close>\<close>
   355 lemma tranclp_induct [consumes 1, case_names base step, induct pred: tranclp]:
   359 lemma tranclp_induct [consumes 1, case_names base step, induct pred: tranclp]:
   382     (base) "(a, b) \<in> r"
   386     (base) "(a, b) \<in> r"
   383   | (step) c where "(a, c) \<in> r\<^sup>+" and "(c, b) \<in> r"
   387   | (step) c where "(a, c) \<in> r\<^sup>+" and "(c, b) \<in> r"
   384   using assms by cases simp_all
   388   using assms by cases simp_all
   385 
   389 
   386 lemma trancl_Int_subset: "r \<subseteq> s \<Longrightarrow> (r\<^sup>+ \<inter> s) O r \<subseteq> s \<Longrightarrow> r\<^sup>+ \<subseteq> s"
   390 lemma trancl_Int_subset: "r \<subseteq> s \<Longrightarrow> (r\<^sup>+ \<inter> s) O r \<subseteq> s \<Longrightarrow> r\<^sup>+ \<subseteq> s"
   387   apply clarify
   391   by (fastforce simp add: elim: trancl_induct)
   388   apply (erule trancl_induct, auto)
       
   389   done
       
   390 
   392 
   391 lemma trancl_unfold: "r\<^sup>+ = r \<union> r\<^sup>+ O r"
   393 lemma trancl_unfold: "r\<^sup>+ = r \<union> r\<^sup>+ O r"
   392   by (auto intro: trancl_into_trancl elim: tranclE)
   394   by (auto intro: trancl_into_trancl elim: tranclE)
   393 
   395 
   394 text \<open>Transitivity of \<^term>\<open>r\<^sup>+\<close>\<close>
   396 text \<open>Transitivity of \<^term>\<open>r\<^sup>+\<close>\<close>
   416     and "r\<^sup>+\<^sup>+ y z"
   418     and "r\<^sup>+\<^sup>+ y z"
   417   shows "r\<^sup>+\<^sup>+ x z"
   419   shows "r\<^sup>+\<^sup>+ x z"
   418   using assms(2,1) by induct iprover+
   420   using assms(2,1) by induct iprover+
   419 
   421 
   420 lemma trancl_id [simp]: "trans r \<Longrightarrow> r\<^sup>+ = r"
   422 lemma trancl_id [simp]: "trans r \<Longrightarrow> r\<^sup>+ = r"
   421   apply auto
   423   unfolding trans_def by (fastforce simp add: elim: trancl_induct)
   422   apply (erule trancl_induct, assumption)
       
   423   apply (unfold trans_def, blast)
       
   424   done
       
   425 
   424 
   426 lemma rtranclp_tranclp_tranclp:
   425 lemma rtranclp_tranclp_tranclp:
   427   assumes "r\<^sup>*\<^sup>* x y"
   426   assumes "r\<^sup>*\<^sup>* x y"
   428   shows "\<And>z. r\<^sup>+\<^sup>+ y z \<Longrightarrow> r\<^sup>+\<^sup>+ x z"
   427   shows "\<And>z. r\<^sup>+\<^sup>+ y z \<Longrightarrow> r\<^sup>+\<^sup>+ x z"
   429   using assms by induct (iprover intro: tranclp_trans)+
   428   using assms by induct (iprover intro: tranclp_trans)+
   433 lemma tranclp_into_tranclp2: "r a b \<Longrightarrow> r\<^sup>+\<^sup>+ b c \<Longrightarrow> r\<^sup>+\<^sup>+ a c"
   432 lemma tranclp_into_tranclp2: "r a b \<Longrightarrow> r\<^sup>+\<^sup>+ b c \<Longrightarrow> r\<^sup>+\<^sup>+ a c"
   434   by (erule tranclp_trans [OF tranclp.r_into_trancl])
   433   by (erule tranclp_trans [OF tranclp.r_into_trancl])
   435 
   434 
   436 lemmas trancl_into_trancl2 = tranclp_into_tranclp2 [to_set]
   435 lemmas trancl_into_trancl2 = tranclp_into_tranclp2 [to_set]
   437 
   436 
   438 lemma tranclp_converseI: "(r\<^sup>+\<^sup>+)\<inverse>\<inverse> x y \<Longrightarrow> (r\<inverse>\<inverse>)\<^sup>+\<^sup>+ x y"
   437 lemma tranclp_converseI:
   439   apply (drule conversepD)
   438   assumes "(r\<^sup>+\<^sup>+)\<inverse>\<inverse> x y" shows "(r\<inverse>\<inverse>)\<^sup>+\<^sup>+ x y"
   440   apply (erule tranclp_induct)
   439   using conversepD [OF assms]
   441    apply (iprover intro: conversepI tranclp_trans)+
   440 proof (induction rule: tranclp_induct)
   442   done
   441   case (base y)
       
   442   then show ?case 
       
   443     by (iprover intro: conversepI)
       
   444 next
       
   445   case (step y z)
       
   446   then show ?case
       
   447     by (iprover intro: conversepI tranclp_trans)
       
   448 qed
   443 
   449 
   444 lemmas trancl_converseI = tranclp_converseI [to_set]
   450 lemmas trancl_converseI = tranclp_converseI [to_set]
   445 
   451 
   446 lemma tranclp_converseD: "(r\<inverse>\<inverse>)\<^sup>+\<^sup>+ x y \<Longrightarrow> (r\<^sup>+\<^sup>+)\<inverse>\<inverse> x y"
   452 lemma tranclp_converseD:
   447   apply (rule conversepI)
   453   assumes "(r\<inverse>\<inverse>)\<^sup>+\<^sup>+ x y" shows "(r\<^sup>+\<^sup>+)\<inverse>\<inverse> x y"
   448   apply (erule tranclp_induct)
   454 proof -
   449    apply (iprover dest: conversepD intro: tranclp_trans)+
   455   have "r\<^sup>+\<^sup>+ y x"
   450   done
   456     using assms
       
   457     by (induction rule: tranclp_induct) (iprover dest: conversepD intro: tranclp_trans)+
       
   458   then show ?thesis
       
   459     by (rule conversepI)
       
   460 qed
   451 
   461 
   452 lemmas trancl_converseD = tranclp_converseD [to_set]
   462 lemmas trancl_converseD = tranclp_converseD [to_set]
   453 
   463 
   454 lemma tranclp_converse: "(r\<inverse>\<inverse>)\<^sup>+\<^sup>+ = (r\<^sup>+\<^sup>+)\<inverse>\<inverse>"
   464 lemma tranclp_converse: "(r\<inverse>\<inverse>)\<^sup>+\<^sup>+ = (r\<^sup>+\<^sup>+)\<inverse>\<inverse>"
   455   by (fastforce simp add: fun_eq_iff intro!: tranclp_converseI dest!: tranclp_converseD)
   465   by (fastforce simp add: fun_eq_iff intro!: tranclp_converseI dest!: tranclp_converseD)
   461 
   471 
   462 lemma converse_tranclp_induct [consumes 1, case_names base step]:
   472 lemma converse_tranclp_induct [consumes 1, case_names base step]:
   463   assumes major: "r\<^sup>+\<^sup>+ a b"
   473   assumes major: "r\<^sup>+\<^sup>+ a b"
   464     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"
   474     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"
   465   shows "P a"
   475   shows "P a"
   466   apply (rule tranclp_induct [OF tranclp_converseI, OF conversepI, OF major])
   476 proof -
   467    apply (blast intro: cases)
   477   have "r\<inverse>\<inverse>\<^sup>+\<^sup>+ b a"
   468   apply (blast intro: assms dest!: tranclp_converseD)
   478     by (intro tranclp_converseI conversepI major)
   469   done
   479   then show ?thesis
       
   480     by (induction rule: tranclp_induct) (blast intro: cases dest: tranclp_converseD)+
       
   481 qed
   470 
   482 
   471 lemmas converse_trancl_induct = converse_tranclp_induct [to_set]
   483 lemmas converse_trancl_induct = converse_tranclp_induct [to_set]
   472 
   484 
   473 lemma tranclpD: "R\<^sup>+\<^sup>+ x y \<Longrightarrow> \<exists>z. R x z \<and> R\<^sup>*\<^sup>* z y"
   485 lemma tranclpD: "R\<^sup>+\<^sup>+ x y \<Longrightarrow> \<exists>z. R x z \<and> R\<^sup>*\<^sup>* z y"
   474   apply (erule converse_tranclp_induct, auto)
   486 proof (induction rule: converse_tranclp_induct)
   475   apply (blast intro: rtranclp_trans)
   487   case (step u v)
   476   done
   488   then show ?case
       
   489     by (blast intro: rtranclp_trans)
       
   490 qed auto
   477 
   491 
   478 lemmas tranclD = tranclpD [to_set]
   492 lemmas tranclD = tranclpD [to_set]
   479 
   493 
   480 lemma converse_tranclpE:
   494 lemma converse_tranclpE:
   481   assumes major: "tranclp r x z"
   495   assumes major: "tranclp r x z"
   490     case rtrancl_refl
   504     case rtrancl_refl
   491     with \<open>r x y\<close> base show P
   505     with \<open>r x y\<close> base show P
   492       by iprover
   506       by iprover
   493   next
   507   next
   494     case rtrancl_into_rtrancl
   508     case rtrancl_into_rtrancl
   495     from this have "tranclp r y z"
   509     then have "tranclp r y z"
   496       by (iprover intro: rtranclp_into_tranclp1)
   510       by (iprover intro: rtranclp_into_tranclp1)
   497     with \<open>r x y\<close> step show P
   511     with \<open>r x y\<close> step show P
   498       by iprover
   512       by iprover
   499   qed
   513   qed
   500 qed
   514 qed
   511   by (blast dest: r_into_trancl)
   525   by (blast dest: r_into_trancl)
   512 
   526 
   513 lemma trancl_subset_Sigma_aux: "(a, b) \<in> r\<^sup>* \<Longrightarrow> r \<subseteq> A \<times> A \<Longrightarrow> a = b \<or> a \<in> A"
   527 lemma trancl_subset_Sigma_aux: "(a, b) \<in> r\<^sup>* \<Longrightarrow> r \<subseteq> A \<times> A \<Longrightarrow> a = b \<or> a \<in> A"
   514   by (induct rule: rtrancl_induct) auto
   528   by (induct rule: rtrancl_induct) auto
   515 
   529 
   516 lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A \<Longrightarrow> r\<^sup>+ \<subseteq> A \<times> A"
   530 lemma trancl_subset_Sigma:
   517   apply (clarsimp simp:)
   531   assumes "r \<subseteq> A \<times> A" shows "r\<^sup>+ \<subseteq> A \<times> A"
   518   apply (erule tranclE)
   532 proof (rule trancl_Int_subset [OF assms])
   519    apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+
   533   show "(r\<^sup>+ \<inter> A \<times> A) O r \<subseteq> A \<times> A"
   520   done
   534     using assms by auto
       
   535 qed
   521 
   536 
   522 lemma reflclp_tranclp [simp]: "(r\<^sup>+\<^sup>+)\<^sup>=\<^sup>= = r\<^sup>*\<^sup>*"
   537 lemma reflclp_tranclp [simp]: "(r\<^sup>+\<^sup>+)\<^sup>=\<^sup>= = r\<^sup>*\<^sup>*"
   523   apply (safe intro!: order_antisym)
   538   by (fast elim: rtranclp.cases tranclp_into_rtranclp dest: rtranclp_into_tranclp1)
   524    apply (erule tranclp_into_rtranclp)
       
   525   apply (blast elim: rtranclp.cases dest: rtranclp_into_tranclp1)
       
   526   done
       
   527 
   539 
   528 lemmas reflcl_trancl [simp] = reflclp_tranclp [to_set]
   540 lemmas reflcl_trancl [simp] = reflclp_tranclp [to_set]
   529 
   541 
   530 lemma trancl_reflcl [simp]: "(r\<^sup>=)\<^sup>+ = r\<^sup>*"
   542 lemma trancl_reflcl [simp]: "(r\<^sup>=)\<^sup>+ = r\<^sup>*"
   531 proof -
   543 proof -
   627   then show ?thesis
   639   then show ?thesis
   628     by auto
   640     by auto
   629 qed
   641 qed
   630 
   642 
   631 lemma trancl_subset_Field2: "r\<^sup>+ \<subseteq> Field r \<times> Field r"
   643 lemma trancl_subset_Field2: "r\<^sup>+ \<subseteq> Field r \<times> Field r"
   632   apply clarify
   644   by (rule trancl_Int_subset) (auto simp: Field_def)
   633   apply (erule trancl_induct)
       
   634    apply (auto simp: Field_def)
       
   635   done
       
   636 
   645 
   637 lemma finite_trancl[simp]: "finite (r\<^sup>+) = finite r"
   646 lemma finite_trancl[simp]: "finite (r\<^sup>+) = finite r"
   638 proof
   647 proof
   639   show "finite (r\<^sup>+) \<Longrightarrow> finite r"
   648   show "finite (r\<^sup>+) \<Longrightarrow> finite r"
   640     by (blast intro: r_into_trancl' finite_subset)
   649     by (blast intro: r_into_trancl' finite_subset)
   641   show "finite r \<Longrightarrow> finite (r\<^sup>+)"
   650   show "finite r \<Longrightarrow> finite (r\<^sup>+)"
   642    apply (rule trancl_subset_Field2 [THEN finite_subset])
   651     by (auto simp: finite_Field trancl_subset_Field2 [THEN finite_subset])
   643    apply (auto simp: finite_Field)
       
   644   done
       
   645 qed
   652 qed
   646 
   653 
   647 lemma finite_rtrancl_Image[simp]: assumes "finite R" "finite A" shows "finite (R\<^sup>* `` A)"
   654 lemma finite_rtrancl_Image[simp]: assumes "finite R" "finite A" shows "finite (R\<^sup>* `` A)"
   648 proof (rule ccontr)
   655 proof (rule ccontr)
   649   assume "infinite (R\<^sup>* `` A)"
   656   assume "infinite (R\<^sup>* `` A)"
   663   show ?case
   670   show ?case
   664     by (simp add: assms)
   671     by (simp add: assms)
   665 next
   672 next
   666   case (step y z)
   673   case (step y z)
   667   with xz \<open>single_valued r\<close> show ?case
   674   with xz \<open>single_valued r\<close> show ?case
   668     apply (auto simp: elim: converse_rtranclE dest: single_valuedD)
   675     by (auto elim: converse_rtranclE dest: single_valuedD intro: rtrancl_trans)
   669     apply (blast intro: rtrancl_trans)
       
   670     done
       
   671 qed
   676 qed
   672 
   677 
   673 lemma r_r_into_trancl: "(a, b) \<in> R \<Longrightarrow> (b, c) \<in> R \<Longrightarrow> (a, c) \<in> R\<^sup>+"
   678 lemma r_r_into_trancl: "(a, b) \<in> R \<Longrightarrow> (b, c) \<in> R \<Longrightarrow> (a, c) \<in> R\<^sup>+"
   674   by (fast intro: trancl_trans)
   679   by (fast intro: trancl_trans)
   675 
   680 
   676 lemma trancl_into_trancl: "(a, b) \<in> r\<^sup>+ \<Longrightarrow> (b, c) \<in> r \<Longrightarrow> (a, c) \<in> r\<^sup>+"
   681 lemma trancl_into_trancl: "(a, b) \<in> r\<^sup>+ \<Longrightarrow> (b, c) \<in> r \<Longrightarrow> (a, c) \<in> r\<^sup>+"
   677   by (induct rule: trancl_induct) (fast intro: r_r_into_trancl trancl_trans)+
   682   by (induct rule: trancl_induct) (fast intro: r_r_into_trancl trancl_trans)+
   678 
   683 
   679 lemma tranclp_rtranclp_tranclp: "r\<^sup>+\<^sup>+ a b \<Longrightarrow> r\<^sup>*\<^sup>* b c \<Longrightarrow> r\<^sup>+\<^sup>+ a c"
   684 lemma tranclp_rtranclp_tranclp:
   680   apply (drule tranclpD)
   685   assumes "r\<^sup>+\<^sup>+ a b" "r\<^sup>*\<^sup>* b c" shows "r\<^sup>+\<^sup>+ a c"
   681   apply (elim exE conjE)
   686 proof -
   682   apply (drule rtranclp_trans, assumption)
   687   obtain z where "r a z" "r\<^sup>*\<^sup>* z c"
   683   apply (drule (2) rtranclp_into_tranclp2)
   688     using assms by (iprover dest: tranclpD rtranclp_trans)
   684   done
   689   then show ?thesis
       
   690     by (blast dest: rtranclp_into_tranclp2)
       
   691 qed
   685 
   692 
   686 lemma rtranclp_conversep: "r\<inverse>\<inverse>\<^sup>*\<^sup>* = r\<^sup>*\<^sup>*\<inverse>\<inverse>"
   693 lemma rtranclp_conversep: "r\<inverse>\<inverse>\<^sup>*\<^sup>* = r\<^sup>*\<^sup>*\<inverse>\<inverse>"
   687   by(auto simp add: fun_eq_iff intro: rtranclp_converseI rtranclp_converseD)
   694   by(auto simp add: fun_eq_iff intro: rtranclp_converseI rtranclp_converseD)
   688 
   695 
   689 lemmas symp_rtranclp = sym_rtrancl[to_pred]
   696 lemmas symp_rtranclp = sym_rtrancl[to_pred]
   715 definition symclp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
   722 definition symclp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
   716 where "symclp r x y \<longleftrightarrow> r x y \<or> r y x"
   723 where "symclp r x y \<longleftrightarrow> r x y \<or> r y x"
   717 
   724 
   718 lemma symclpI [simp, intro?]:
   725 lemma symclpI [simp, intro?]:
   719   shows symclpI1: "r x y \<Longrightarrow> symclp r x y"
   726   shows symclpI1: "r x y \<Longrightarrow> symclp r x y"
   720   and symclpI2: "r y x \<Longrightarrow> symclp r x y"
   727     and symclpI2: "r y x \<Longrightarrow> symclp r x y"
   721 by(simp_all add: symclp_def)
   728   by(simp_all add: symclp_def)
   722 
   729 
   723 lemma symclpE [consumes 1, cases pred]:
   730 lemma symclpE [consumes 1, cases pred]:
   724   assumes "symclp r x y"
   731   assumes "symclp r x y"
   725   obtains (base) "r x y" | (sym) "r y x"
   732   obtains (base) "r x y" | (sym) "r y x"
   726 using assms by(auto simp add: symclp_def)
   733   using assms by(auto simp add: symclp_def)
   727 
   734 
   728 lemma symclp_pointfree: "symclp r = sup r r\<inverse>\<inverse>"
   735 lemma symclp_pointfree: "symclp r = sup r r\<inverse>\<inverse>"
   729   by(auto simp add: symclp_def fun_eq_iff)
   736   by(auto simp add: symclp_def fun_eq_iff)
   730 
   737 
   731 lemma symclp_greater: "r \<le> symclp r"
   738 lemma symclp_greater: "r \<le> symclp r"
   960 lemma rtranclp_power: "(P\<^sup>*\<^sup>*) x y \<longleftrightarrow> (\<exists>n. (P ^^ n) x y)"
   967 lemma rtranclp_power: "(P\<^sup>*\<^sup>*) x y \<longleftrightarrow> (\<exists>n. (P ^^ n) x y)"
   961   by (simp add: rtranclp_is_Sup_relpowp)
   968   by (simp add: rtranclp_is_Sup_relpowp)
   962 
   969 
   963 lemma trancl_power: "p \<in> R\<^sup>+ \<longleftrightarrow> (\<exists>n > 0. p \<in> R ^^ n)"
   970 lemma trancl_power: "p \<in> R\<^sup>+ \<longleftrightarrow> (\<exists>n > 0. p \<in> R ^^ n)"
   964 proof -
   971 proof -
   965   have "((a, b) \<in> R\<^sup>+) = (\<exists>n>0. (a, b) \<in> R ^^ n)" for a b
   972   have "(a, b) \<in> R\<^sup>+ \<longleftrightarrow> (\<exists>n>0. (a, b) \<in> R ^^ n)" for a b
   966   proof safe
   973   proof safe
   967     show "(a, b) \<in> R\<^sup>+ \<Longrightarrow> \<exists>n>0. (a, b) \<in> R ^^ n"
   974     show "(a, b) \<in> R\<^sup>+ \<Longrightarrow> \<exists>n>0. (a, b) \<in> R ^^ n"
   968       apply (drule tranclD2)
   975       by (fastforce simp: rtrancl_is_UN_relpow relcomp_unfold dest: tranclD2)
   969       apply (fastforce simp: rtrancl_is_UN_relpow relcomp_unfold)
       
   970       done
       
   971     show "(a, b) \<in> R\<^sup>+" if "n > 0" "(a, b) \<in> R ^^ n" for n
   976     show "(a, b) \<in> R\<^sup>+" if "n > 0" "(a, b) \<in> R ^^ n" for n
   972     proof (cases n)
   977     proof (cases n)
   973       case (Suc m)
   978       case (Suc m)
   974       with that show ?thesis
   979       with that show ?thesis
   975         by (auto simp: dest: relpow_imp_rtrancl rtrancl_into_trancl1)
   980         by (auto simp: dest: relpow_imp_rtrancl rtrancl_into_trancl1)
  1115 
  1120 
  1116 lemma relpow_finite_bounded:
  1121 lemma relpow_finite_bounded:
  1117   fixes R :: "('a \<times> 'a) set"
  1122   fixes R :: "('a \<times> 'a) set"
  1118   assumes "finite R"
  1123   assumes "finite R"
  1119   shows "R^^k \<subseteq> (\<Union>n\<in>{n. n \<le> card R}. R^^n)"
  1124   shows "R^^k \<subseteq> (\<Union>n\<in>{n. n \<le> card R}. R^^n)"
  1120   apply (cases k, force)
  1125 proof (cases k)
  1121   apply (use relpow_finite_bounded1[OF assms, of k] in auto)
  1126   case (Suc k')
  1122   done
  1127   then show ?thesis
       
  1128     using relpow_finite_bounded1[OF assms, of k] by auto
       
  1129 qed force
  1123 
  1130 
  1124 lemma rtrancl_finite_eq_relpow: "finite R \<Longrightarrow> R\<^sup>* = (\<Union>n\<in>{n. n \<le> card R}. R^^n)"
  1131 lemma rtrancl_finite_eq_relpow: "finite R \<Longrightarrow> R\<^sup>* = (\<Union>n\<in>{n. n \<le> card R}. R^^n)"
  1125   by (fastforce simp: rtrancl_power dest: relpow_finite_bounded)
  1132   by (fastforce simp: rtrancl_power dest: relpow_finite_bounded)
  1126 
  1133 
  1127 lemma trancl_finite_eq_relpow: "finite R \<Longrightarrow> R\<^sup>+ = (\<Union>n\<in>{n. 0 < n \<and> n \<le> card R}. R^^n)"
  1134 lemma trancl_finite_eq_relpow:
  1128   apply (auto simp: trancl_power)
  1135   assumes "finite R" shows "R\<^sup>+ = (\<Union>n\<in>{n. 0 < n \<and> n \<le> card R}. R^^n)"
  1129   apply (auto dest: relpow_finite_bounded1)
  1136 proof -
  1130   done
  1137   have "\<And>a b n. \<lbrakk>0 < n; (a, b) \<in> R ^^ n\<rbrakk> \<Longrightarrow> \<exists>x>0. x \<le> card R \<and> (a, b) \<in> R ^^ x"
       
  1138     using assms by (auto dest: relpow_finite_bounded1)
       
  1139   then show ?thesis
       
  1140     by (auto simp: trancl_power)
       
  1141 qed
  1131 
  1142 
  1132 lemma finite_relcomp[simp,intro]:
  1143 lemma finite_relcomp[simp,intro]:
  1133   assumes "finite R" and "finite S"
  1144   assumes "finite R" and "finite S"
  1134   shows "finite (R O S)"
  1145   shows "finite (R O S)"
  1135 proof-
  1146 proof-
  1187     from that obtain i where "0 < i" "i \<le> Suc (Suc n)" "(a, b) \<in> R ^^ i"
  1198     from that obtain i where "0 < i" "i \<le> Suc (Suc n)" "(a, b) \<in> R ^^ i"
  1188       unfolding ntrancl_def by auto
  1199       unfolding ntrancl_def by auto
  1189     show ?thesis
  1200     show ?thesis
  1190     proof (cases "i = 1")
  1201     proof (cases "i = 1")
  1191       case True
  1202       case True
  1192       from this \<open>(a, b) \<in> R ^^ i\<close> show ?thesis
  1203       with \<open>(a, b) \<in> R ^^ i\<close> show ?thesis
  1193         by (auto simp: ntrancl_def)
  1204         by (auto simp: ntrancl_def)
  1194     next
  1205     next
  1195       case False
  1206       case False
  1196       with \<open>0 < i\<close> obtain j where j: "i = Suc j" "0 < j"
  1207       with \<open>0 < i\<close> obtain j where j: "i = Suc j" "0 < j"
  1197         by (cases i) auto
  1208         by (cases i) auto