src/HOL/Transitive_Closure.thy
 author wenzelm Mon Feb 25 20:48:14 2002 +0100 (2002-02-25) changeset 12937 0c4fd7529467 parent 12823 9d3f5056296b child 13704 854501b1e957 permissions -rw-r--r--
clarified syntax of long'' statements: fixes/assumes/shows;
     1 (*  Title:      HOL/Transitive_Closure.thy

     2     ID:         $Id$

     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory

     4     Copyright   1992  University of Cambridge

     5 *)

     6

     7 header {* Reflexive and Transitive closure of a relation *}

     8

     9 theory Transitive_Closure = Inductive:

    10

    11 text {*

    12   @{text rtrancl} is reflexive/transitive closure,

    13   @{text trancl} is transitive closure,

    14   @{text reflcl} is reflexive closure.

    15

    16   These postfix operators have \emph{maximum priority}, forcing their

    17   operands to be atomic.

    18 *}

    19

    20 consts

    21   rtrancl :: "('a \<times> 'a) set => ('a \<times> 'a) set"    ("(_^*)" [1000] 999)

    22

    23 inductive "r^*"

    24   intros

    25     rtrancl_refl [intro!, CPure.intro!, simp]: "(a, a) : r^*"

    26     rtrancl_into_rtrancl [CPure.intro]: "(a, b) : r^* ==> (b, c) : r ==> (a, c) : r^*"

    27

    28 constdefs

    29   trancl :: "('a \<times> 'a) set => ('a \<times> 'a) set"    ("(_^+)" [1000] 999)

    30   "r^+ ==  r O rtrancl r"

    31

    32 syntax

    33   "_reflcl" :: "('a \<times> 'a) set => ('a \<times> 'a) set"    ("(_^=)" [1000] 999)

    34 translations

    35   "r^=" == "r \<union> Id"

    36

    37 syntax (xsymbols)

    38   rtrancl :: "('a \<times> 'a) set => ('a \<times> 'a) set"    ("(_\\<^sup>*)" [1000] 999)

    39   trancl :: "('a \<times> 'a) set => ('a \<times> 'a) set"    ("(_\\<^sup>+)" [1000] 999)

    40   "_reflcl" :: "('a \<times> 'a) set => ('a \<times> 'a) set"    ("(_\\<^sup>=)" [1000] 999)

    41

    42

    43 subsection {* Reflexive-transitive closure *}

    44

    45 lemma r_into_rtrancl [intro]: "!!p. p \<in> r ==> p \<in> r^*"

    46   -- {* @{text rtrancl} of @{text r} contains @{text r} *}

    47   apply (simp only: split_tupled_all)

    48   apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])

    49   done

    50

    51 lemma rtrancl_mono: "r \<subseteq> s ==> r^* \<subseteq> s^*"

    52   -- {* monotonicity of @{text rtrancl} *}

    53   apply (rule subsetI)

    54   apply (simp only: split_tupled_all)

    55   apply (erule rtrancl.induct)

    56    apply (rule_tac [2] rtrancl_into_rtrancl)

    57     apply blast+

    58   done

    59

    60 theorem rtrancl_induct [consumes 1, induct set: rtrancl]:

    61   assumes a: "(a, b) : r^*"

    62     and cases: "P a" "!!y z. [| (a, y) : r^*; (y, z) : r; P y |] ==> P z"

    63   shows "P b"

    64 proof -

    65   from a have "a = a --> P b"

    66     by (induct "%x y. x = a --> P y" a b) (rules intro: cases)+

    67   thus ?thesis by rules

    68 qed

    69

    70 ML_setup {*

    71   bind_thm ("rtrancl_induct2", split_rule

    72     (read_instantiate [("a","(ax,ay)"), ("b","(bx,by)")] (thm "rtrancl_induct")));

    73 *}

    74

    75 lemma trans_rtrancl: "trans(r^*)"

    76   -- {* transitivity of transitive closure!! -- by induction *}

    77 proof (rule transI)

    78   fix x y z

    79   assume "(x, y) \<in> r\<^sup>*"

    80   assume "(y, z) \<in> r\<^sup>*"

    81   thus "(x, z) \<in> r\<^sup>*" by induct (rules!)+

    82 qed

    83

    84 lemmas rtrancl_trans = trans_rtrancl [THEN transD, standard]

    85

    86 lemma rtranclE:

    87   "[| (a::'a,b) : r^*;  (a = b) ==> P;

    88       !!y.[| (a,y) : r^*; (y,b) : r |] ==> P

    89    |] ==> P"

    90   -- {* elimination of @{text rtrancl} -- by induction on a special formula *}

    91 proof -

    92   assume major: "(a::'a,b) : r^*"

    93   case rule_context

    94   show ?thesis

    95     apply (subgoal_tac "(a::'a) = b | (EX y. (a,y) : r^* & (y,b) : r)")

    96      apply (rule_tac [2] major [THEN rtrancl_induct])

    97       prefer 2 apply (blast!)

    98       prefer 2 apply (blast!)

    99     apply (erule asm_rl exE disjE conjE prems)+

   100     done

   101 qed

   102

   103 lemma converse_rtrancl_into_rtrancl:

   104   "(a, b) \<in> r \<Longrightarrow> (b, c) \<in> r\<^sup>* \<Longrightarrow> (a, c) \<in> r\<^sup>*"

   105   by (rule rtrancl_trans) rules+

   106

   107 text {*

   108   \medskip More @{term "r^*"} equations and inclusions.

   109 *}

   110

   111 lemma rtrancl_idemp [simp]: "(r^*)^* = r^*"

   112   apply auto

   113   apply (erule rtrancl_induct)

   114    apply (rule rtrancl_refl)

   115   apply (blast intro: rtrancl_trans)

   116   done

   117

   118 lemma rtrancl_idemp_self_comp [simp]: "R^* O R^* = R^*"

   119   apply (rule set_ext)

   120   apply (simp only: split_tupled_all)

   121   apply (blast intro: rtrancl_trans)

   122   done

   123

   124 lemma rtrancl_subset_rtrancl: "r \<subseteq> s^* ==> r^* \<subseteq> s^*"

   125   apply (drule rtrancl_mono)

   126   apply simp

   127   done

   128

   129 lemma rtrancl_subset: "R \<subseteq> S ==> S \<subseteq> R^* ==> S^* = R^*"

   130   apply (drule rtrancl_mono)

   131   apply (drule rtrancl_mono)

   132   apply simp

   133   apply blast

   134   done

   135

   136 lemma rtrancl_Un_rtrancl: "(R^* \<union> S^*)^* = (R \<union> S)^*"

   137   by (blast intro!: rtrancl_subset intro: r_into_rtrancl rtrancl_mono [THEN subsetD])

   138

   139 lemma rtrancl_reflcl [simp]: "(R^=)^* = R^*"

   140   by (blast intro!: rtrancl_subset intro: r_into_rtrancl)

   141

   142 lemma rtrancl_r_diff_Id: "(r - Id)^* = r^*"

   143   apply (rule sym)

   144   apply (rule rtrancl_subset)

   145    apply blast

   146   apply clarify

   147   apply (rename_tac a b)

   148   apply (case_tac "a = b")

   149    apply blast

   150   apply (blast intro!: r_into_rtrancl)

   151   done

   152

   153 theorem rtrancl_converseD:

   154   assumes r: "(x, y) \<in> (r^-1)^*"

   155   shows "(y, x) \<in> r^*"

   156 proof -

   157   from r show ?thesis

   158     by induct (rules intro: rtrancl_trans dest!: converseD)+

   159 qed

   160

   161 theorem rtrancl_converseI:

   162   assumes r: "(y, x) \<in> r^*"

   163   shows "(x, y) \<in> (r^-1)^*"

   164 proof -

   165   from r show ?thesis

   166     by induct (rules intro: rtrancl_trans converseI)+

   167 qed

   168

   169 lemma rtrancl_converse: "(r^-1)^* = (r^*)^-1"

   170   by (fast dest!: rtrancl_converseD intro!: rtrancl_converseI)

   171

   172 theorem converse_rtrancl_induct:

   173   assumes major: "(a, b) : r^*"

   174     and cases: "P b" "!!y z. [| (y, z) : r; (z, b) : r^*; P z |] ==> P y"

   175   shows "P a"

   176 proof -

   177   from rtrancl_converseI [OF major]

   178   show ?thesis

   179     by induct (rules intro: cases dest!: converseD rtrancl_converseD)+

   180 qed

   181

   182 ML_setup {*

   183   bind_thm ("converse_rtrancl_induct2", split_rule

   184     (read_instantiate [("a","(ax,ay)"),("b","(bx,by)")] (thm "converse_rtrancl_induct")));

   185 *}

   186

   187 lemma converse_rtranclE:

   188   "[| (x,z):r^*;

   189       x=z ==> P;

   190       !!y. [| (x,y):r; (y,z):r^* |] ==> P

   191    |] ==> P"

   192 proof -

   193   assume major: "(x,z):r^*"

   194   case rule_context

   195   show ?thesis

   196     apply (subgoal_tac "x = z | (EX y. (x,y) : r & (y,z) : r^*)")

   197      apply (rule_tac [2] major [THEN converse_rtrancl_induct])

   198       prefer 2 apply (blast!)

   199      prefer 2 apply (blast!)

   200     apply (erule asm_rl exE disjE conjE prems)+

   201     done

   202 qed

   203

   204 ML_setup {*

   205   bind_thm ("converse_rtranclE2", split_rule

   206     (read_instantiate [("x","(xa,xb)"), ("z","(za,zb)")] (thm "converse_rtranclE")));

   207 *}

   208

   209 lemma r_comp_rtrancl_eq: "r O r^* = r^* O r"

   210   by (blast elim: rtranclE converse_rtranclE

   211     intro: rtrancl_into_rtrancl converse_rtrancl_into_rtrancl)

   212

   213

   214 subsection {* Transitive closure *}

   215

   216 lemma trancl_mono: "p \<in> r^+ ==> r \<subseteq> s ==> p \<in> s^+"

   217   apply (unfold trancl_def)

   218   apply (blast intro: rtrancl_mono [THEN subsetD])

   219   done

   220

   221 text {*

   222   \medskip Conversions between @{text trancl} and @{text rtrancl}.

   223 *}

   224

   225 lemma trancl_into_rtrancl: "!!p. p \<in> r^+ ==> p \<in> r^*"

   226   apply (unfold trancl_def)

   227   apply (simp only: split_tupled_all)

   228   apply (erule rel_compEpair)

   229   apply (assumption | rule rtrancl_into_rtrancl)+

   230   done

   231

   232 lemma r_into_trancl [intro]: "!!p. p \<in> r ==> p \<in> r^+"

   233   -- {* @{text "r^+"} contains @{text r} *}

   234   apply (unfold trancl_def)

   235   apply (simp only: split_tupled_all)

   236   apply (assumption | rule rel_compI rtrancl_refl)+

   237   done

   238

   239 lemma rtrancl_into_trancl1: "(a, b) \<in> r^* ==> (b, c) \<in> r ==> (a, c) \<in> r^+"

   240   -- {* intro rule by definition: from @{text rtrancl} and @{text r} *}

   241   by (auto simp add: trancl_def)

   242

   243 lemma rtrancl_into_trancl2: "[| (a,b) : r;  (b,c) : r^* |]   ==>  (a,c) : r^+"

   244   -- {* intro rule from @{text r} and @{text rtrancl} *}

   245   apply (erule rtranclE)

   246    apply (blast intro: r_into_trancl)

   247   apply (rule rtrancl_trans [THEN rtrancl_into_trancl1])

   248    apply (assumption | rule r_into_rtrancl)+

   249   done

   250

   251 lemma trancl_induct:

   252   "[| (a,b) : r^+;

   253       !!y.  [| (a,y) : r |] ==> P(y);

   254       !!y z.[| (a,y) : r^+;  (y,z) : r;  P(y) |] ==> P(z)

   255    |] ==> P(b)"

   256   -- {* Nice induction rule for @{text trancl} *}

   257 proof -

   258   assume major: "(a, b) : r^+"

   259   case rule_context

   260   show ?thesis

   261     apply (rule major [unfolded trancl_def, THEN rel_compEpair])

   262     txt {* by induction on this formula *}

   263     apply (subgoal_tac "ALL z. (y,z) : r --> P (z)")

   264      txt {* now solve first subgoal: this formula is sufficient *}

   265      apply blast

   266     apply (erule rtrancl_induct)

   267     apply (blast intro: rtrancl_into_trancl1 prems)+

   268     done

   269 qed

   270

   271 lemma trancl_trans_induct:

   272   "[| (x,y) : r^+;

   273       !!x y. (x,y) : r ==> P x y;

   274       !!x y z. [| (x,y) : r^+; P x y; (y,z) : r^+; P y z |] ==> P x z

   275    |] ==> P x y"

   276   -- {* Another induction rule for trancl, incorporating transitivity *}

   277 proof -

   278   assume major: "(x,y) : r^+"

   279   case rule_context

   280   show ?thesis

   281     by (blast intro: r_into_trancl major [THEN trancl_induct] prems)

   282 qed

   283

   284 lemma tranclE:

   285   "[| (a::'a,b) : r^+;

   286       (a,b) : r ==> P;

   287       !!y.[| (a,y) : r^+;  (y,b) : r |] ==> P

   288    |] ==> P"

   289   -- {* elimination of @{text "r^+"} -- \emph{not} an induction rule *}

   290 proof -

   291   assume major: "(a::'a,b) : r^+"

   292   case rule_context

   293   show ?thesis

   294     apply (subgoal_tac "(a::'a, b) : r | (EX y. (a,y) : r^+ & (y,b) : r)")

   295      apply (erule asm_rl disjE exE conjE prems)+

   296     apply (rule major [unfolded trancl_def, THEN rel_compEpair])

   297     apply (erule rtranclE)

   298      apply blast

   299     apply (blast intro!: rtrancl_into_trancl1)

   300     done

   301 qed

   302

   303 lemma trans_trancl: "trans(r^+)"

   304   -- {* Transitivity of @{term "r^+"} *}

   305   -- {* Proved by unfolding since it uses transitivity of @{text rtrancl} *}

   306   apply (unfold trancl_def)

   307   apply (rule transI)

   308   apply (erule rel_compEpair)+

   309   apply (rule rtrancl_into_rtrancl [THEN rtrancl_trans [THEN rel_compI]])

   310   apply assumption+

   311   done

   312

   313 lemmas trancl_trans = trans_trancl [THEN transD, standard]

   314

   315 lemma rtrancl_trancl_trancl: "(x, y) \<in> r^* ==> (y, z) \<in> r^+ ==> (x, z) \<in> r^+"

   316   apply (unfold trancl_def)

   317   apply (blast intro: rtrancl_trans)

   318   done

   319

   320 lemma trancl_into_trancl2: "(a, b) \<in> r ==> (b, c) \<in> r^+ ==> (a, c) \<in> r^+"

   321   by (erule transD [OF trans_trancl r_into_trancl])

   322

   323 lemma trancl_insert:

   324   "(insert (y, x) r)^+ = r^+ \<union> {(a, b). (a, y) \<in> r^* \<and> (x, b) \<in> r^*}"

   325   -- {* primitive recursion for @{text trancl} over finite relations *}

   326   apply (rule equalityI)

   327    apply (rule subsetI)

   328    apply (simp only: split_tupled_all)

   329    apply (erule trancl_induct)

   330     apply blast

   331    apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl r_into_trancl trancl_trans)

   332   apply (rule subsetI)

   333   apply (blast intro: trancl_mono rtrancl_mono

   334     [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)

   335   done

   336

   337 lemma trancl_converse: "(r^-1)^+ = (r^+)^-1"

   338   apply (unfold trancl_def)

   339   apply (simp add: rtrancl_converse converse_rel_comp)

   340   apply (simp add: rtrancl_converse [symmetric] r_comp_rtrancl_eq)

   341   done

   342

   343 lemma trancl_converseI: "(x, y) \<in> (r^+)^-1 ==> (x,y) \<in> (r^-1)^+"

   344   by (simp add: trancl_converse)

   345

   346 lemma trancl_converseD: "(x, y) \<in> (r^-1)^+ ==> (x, y) \<in> (r^+)^-1"

   347   by (simp add: trancl_converse)

   348

   349 lemma converse_trancl_induct:

   350   "[| (a,b) : r^+; !!y. (y,b) : r ==> P(y);

   351       !!y z.[| (y,z) : r;  (z,b) : r^+;  P(z) |] ==> P(y) |]

   352     ==> P(a)"

   353 proof -

   354   assume major: "(a,b) : r^+"

   355   case rule_context

   356   show ?thesis

   357     apply (rule major [THEN converseI, THEN trancl_converseI [THEN trancl_induct]])

   358      apply (rule prems)

   359      apply (erule converseD)

   360     apply (blast intro: prems dest!: trancl_converseD)

   361     done

   362 qed

   363

   364 lemma tranclD: "(x, y) \<in> R^+ ==> EX z. (x, z) \<in> R \<and> (z, y) \<in> R^*"

   365   apply (erule converse_trancl_induct)

   366    apply auto

   367   apply (blast intro: rtrancl_trans)

   368   done

   369

   370 lemma irrefl_tranclI: "r^-1 \<inter> r^+ = {} ==> (x, x) \<notin> r^+"

   371   apply (subgoal_tac "ALL y. (x, y) : r^+ --> x \<noteq> y")

   372    apply fast

   373   apply (intro strip)

   374   apply (erule trancl_induct)

   375    apply (auto intro: r_into_trancl)

   376   done

   377

   378 lemma irrefl_trancl_rD: "!!X. ALL x. (x, x) \<notin> r^+ ==> (x, y) \<in> r ==> x \<noteq> y"

   379   by (blast dest: r_into_trancl)

   380

   381 lemma trancl_subset_Sigma_aux:

   382     "(a, b) \<in> r^* ==> r \<subseteq> A \<times> A ==> a = b \<or> a \<in> A"

   383   apply (erule rtrancl_induct)

   384    apply auto

   385   done

   386

   387 lemma trancl_subset_Sigma: "r \<subseteq> A \<times> A ==> r^+ \<subseteq> A \<times> A"

   388   apply (unfold trancl_def)

   389   apply (blast dest!: trancl_subset_Sigma_aux)

   390   done

   391

   392 lemma reflcl_trancl [simp]: "(r^+)^= = r^*"

   393   apply safe

   394    apply (erule trancl_into_rtrancl)

   395   apply (blast elim: rtranclE dest: rtrancl_into_trancl1)

   396   done

   397

   398 lemma trancl_reflcl [simp]: "(r^=)^+ = r^*"

   399   apply safe

   400    apply (drule trancl_into_rtrancl)

   401    apply simp

   402   apply (erule rtranclE)

   403    apply safe

   404    apply (rule r_into_trancl)

   405    apply simp

   406   apply (rule rtrancl_into_trancl1)

   407    apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD])

   408   apply fast

   409   done

   410

   411 lemma trancl_empty [simp]: "{}^+ = {}"

   412   by (auto elim: trancl_induct)

   413

   414 lemma rtrancl_empty [simp]: "{}^* = Id"

   415   by (rule subst [OF reflcl_trancl]) simp

   416

   417 lemma rtranclD: "(a, b) \<in> R^* ==> a = b \<or> a \<noteq> b \<and> (a, b) \<in> R^+"

   418   by (force simp add: reflcl_trancl [symmetric] simp del: reflcl_trancl)

   419

   420

   421 text {* @{text Domain} and @{text Range} *}

   422

   423 lemma Domain_rtrancl [simp]: "Domain (R^*) = UNIV"

   424   by blast

   425

   426 lemma Range_rtrancl [simp]: "Range (R^*) = UNIV"

   427   by blast

   428

   429 lemma rtrancl_Un_subset: "(R^* \<union> S^*) \<subseteq> (R Un S)^*"

   430   by (rule rtrancl_Un_rtrancl [THEN subst]) fast

   431

   432 lemma in_rtrancl_UnI: "x \<in> R^* \<or> x \<in> S^* ==> x \<in> (R \<union> S)^*"

   433   by (blast intro: subsetD [OF rtrancl_Un_subset])

   434

   435 lemma trancl_domain [simp]: "Domain (r^+) = Domain r"

   436   by (unfold Domain_def) (blast dest: tranclD)

   437

   438 lemma trancl_range [simp]: "Range (r^+) = Range r"

   439   by (simp add: Range_def trancl_converse [symmetric])

   440

   441 lemma Not_Domain_rtrancl:

   442     "x ~: Domain R ==> ((x, y) : R^*) = (x = y)"

   443   apply auto

   444   by (erule rev_mp, erule rtrancl_induct, auto)

   445

   446

   447 text {* More about converse @{text rtrancl} and @{text trancl}, should

   448   be merged with main body. *}

   449

   450 lemma r_r_into_trancl: "(a, b) \<in> R ==> (b, c) \<in> R ==> (a, c) \<in> R^+"

   451   by (fast intro: trancl_trans)

   452

   453 lemma trancl_into_trancl [rule_format]:

   454     "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r --> (a,c) \<in> r\<^sup>+"

   455   apply (erule trancl_induct)

   456    apply (fast intro: r_r_into_trancl)

   457   apply (fast intro: r_r_into_trancl trancl_trans)

   458   done

   459

   460 lemma trancl_rtrancl_trancl:

   461     "(a, b) \<in> r\<^sup>+ ==> (b, c) \<in> r\<^sup>* ==> (a, c) \<in> r\<^sup>+"

   462   apply (drule tranclD)

   463   apply (erule exE, erule conjE)

   464   apply (drule rtrancl_trans, assumption)

   465   apply (drule rtrancl_into_trancl2, assumption)

   466   apply assumption

   467   done

   468

   469 lemmas transitive_closure_trans [trans] =

   470   r_r_into_trancl trancl_trans rtrancl_trans

   471   trancl_into_trancl trancl_into_trancl2

   472   rtrancl_into_rtrancl converse_rtrancl_into_rtrancl

   473   rtrancl_trancl_trancl trancl_rtrancl_trancl

   474

   475 declare trancl_into_rtrancl [elim]

   476

   477 declare rtranclE [cases set: rtrancl]

   478 declare trancl_induct [induct set: trancl]

   479 declare tranclE [cases set: trancl]

   480

   481 end