src/HOL/Transitive_Closure.thy
 author ballarin Thu Feb 19 15:57:34 2004 +0100 (2004-02-19) changeset 14398 c5c47703f763 parent 14361 ad2f5da643b4 child 14404 4952c5a92e04 permissions -rw-r--r--
Efficient, graph-based reasoner for linear and partial orders.
+ Setup as solver in the HOL simplifier.
     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 consts

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

    30

    31 inductive "r^+"

    32   intros

    33     r_into_trancl [intro, CPure.intro]: "(a, b) : r ==> (a, b) : r^+"

    34     trancl_into_trancl [CPure.intro]: "(a, b) : r^+ ==> (b, c) : r ==> (a,c) : r^+"

    35

    36 syntax

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

    38 translations

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

    40

    41 syntax (xsymbols)

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

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

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

    45

    46

    47 subsection {* Reflexive-transitive closure *}

    48

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

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

    51   apply (simp only: split_tupled_all)

    52   apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])

    53   done

    54

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

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

    57   apply (rule subsetI)

    58   apply (simp only: split_tupled_all)

    59   apply (erule rtrancl.induct)

    60    apply (rule_tac [2] rtrancl_into_rtrancl, blast+)

    61   done

    62

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

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

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

    66   shows "P b"

    67 proof -

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

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

    70   thus ?thesis by rules

    71 qed

    72

    73 ML_setup {*

    74   bind_thm ("rtrancl_induct2", split_rule

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

    76 *}

    77

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

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

    80 proof (rule transI)

    81   fix x y z

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

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

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

    85 qed

    86

    87 lemmas rtrancl_trans = trans_rtrancl [THEN transD, standard]

    88

    89 lemma rtranclE:

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

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

    92    |] ==> P"

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

    94 proof -

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

    96   case rule_context

    97   show ?thesis

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

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

   100       prefer 2 apply (blast!)

   101       prefer 2 apply (blast!)

   102     apply (erule asm_rl exE disjE conjE prems)+

   103     done

   104 qed

   105

   106 lemma converse_rtrancl_into_rtrancl:

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

   108   by (rule rtrancl_trans) rules+

   109

   110 text {*

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

   112 *}

   113

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

   115   apply auto

   116   apply (erule rtrancl_induct)

   117    apply (rule rtrancl_refl)

   118   apply (blast intro: rtrancl_trans)

   119   done

   120

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

   122   apply (rule set_ext)

   123   apply (simp only: split_tupled_all)

   124   apply (blast intro: rtrancl_trans)

   125   done

   126

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

   128 by (drule rtrancl_mono, simp)

   129

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

   131   apply (drule rtrancl_mono)

   132   apply (drule rtrancl_mono, simp)

   133   done

   134

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

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

   137

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

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

   140

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

   142   apply (rule sym)

   143   apply (rule rtrancl_subset, blast, clarify)

   144   apply (rename_tac a b)

   145   apply (case_tac "a = b", blast)

   146   apply (blast intro!: r_into_rtrancl)

   147   done

   148

   149 theorem rtrancl_converseD:

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

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

   152 proof -

   153   from r show ?thesis

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

   155 qed

   156

   157 theorem rtrancl_converseI:

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

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

   160 proof -

   161   from r show ?thesis

   162     by induct (rules intro: rtrancl_trans converseI)+

   163 qed

   164

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

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

   167

   168 theorem converse_rtrancl_induct:

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

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

   171   shows "P a"

   172 proof -

   173   from rtrancl_converseI [OF major]

   174   show ?thesis

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

   176 qed

   177

   178 ML_setup {*

   179   bind_thm ("converse_rtrancl_induct2", split_rule

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

   181 *}

   182

   183 lemma converse_rtranclE:

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

   185       x=z ==> P;

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

   187    |] ==> P"

   188 proof -

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

   190   case rule_context

   191   show ?thesis

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

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

   194       prefer 2 apply rules

   195      prefer 2 apply rules

   196     apply (erule asm_rl exE disjE conjE prems)+

   197     done

   198 qed

   199

   200 ML_setup {*

   201   bind_thm ("converse_rtranclE2", split_rule

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

   203 *}

   204

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

   206   by (blast elim: rtranclE converse_rtranclE

   207     intro: rtrancl_into_rtrancl converse_rtrancl_into_rtrancl)

   208

   209

   210 subsection {* Transitive closure *}

   211

   212 lemma trancl_mono: "!!p. p \<in> r^+ ==> r \<subseteq> s ==> p \<in> s^+"

   213   apply (simp only: split_tupled_all)

   214   apply (erule trancl.induct)

   215   apply (rules dest: subsetD)+

   216   done

   217

   218 lemma r_into_trancl': "!!p. p : r ==> p : r^+"

   219   by (simp only: split_tupled_all) (erule r_into_trancl)

   220

   221 text {*

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

   223 *}

   224

   225 lemma trancl_into_rtrancl: "(a, b) \<in> r^+ ==> (a, b) \<in> r^*"

   226   by (erule trancl.induct) rules+

   227

   228 lemma rtrancl_into_trancl1: assumes r: "(a, b) \<in> r^*"

   229   shows "!!c. (b, c) \<in> r ==> (a, c) \<in> r^+" using r

   230   by induct rules+

   231

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

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

   234   apply (erule rtranclE, rules)

   235   apply (rule rtrancl_trans [THEN rtrancl_into_trancl1])

   236    apply (assumption | rule r_into_rtrancl)+

   237   done

   238

   239 lemma trancl_induct [consumes 1, induct set: trancl]:

   240   assumes a: "(a,b) : r^+"

   241   and cases: "!!y. (a, y) : r ==> P y"

   242     "!!y z. (a,y) : r^+ ==> (y, z) : r ==> P y ==> P z"

   243   shows "P b"

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

   245 proof -

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

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

   248   thus ?thesis by rules

   249 qed

   250

   251 lemma trancl_trans_induct:

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

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

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

   255    |] ==> P x y"

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

   257 proof -

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

   259   case rule_context

   260   show ?thesis

   261     by (rules intro: r_into_trancl major [THEN trancl_induct] prems)

   262 qed

   263

   264 inductive_cases tranclE: "(a, b) : r^+"

   265

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

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

   268 proof (rule transI)

   269   fix x y z

   270   assume "(x, y) \<in> r^+"

   271   assume "(y, z) \<in> r^+"

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

   273 qed

   274

   275 lemmas trancl_trans = trans_trancl [THEN transD, standard]

   276

   277 lemma rtrancl_trancl_trancl: assumes r: "(x, y) \<in> r^*"

   278   shows "!!z. (y, z) \<in> r^+ ==> (x, z) \<in> r^+" using r

   279   by induct (rules intro: trancl_trans)+

   280

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

   282   by (erule transD [OF trans_trancl r_into_trancl])

   283

   284 lemma trancl_insert:

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

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

   287   apply (rule equalityI)

   288    apply (rule subsetI)

   289    apply (simp only: split_tupled_all)

   290    apply (erule trancl_induct, blast)

   291    apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl r_into_trancl trancl_trans)

   292   apply (rule subsetI)

   293   apply (blast intro: trancl_mono rtrancl_mono

   294     [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)

   295   done

   296

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

   298   apply (drule converseD)

   299   apply (erule trancl.induct)

   300   apply (rules intro: converseI trancl_trans)+

   301   done

   302

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

   304   apply (rule converseI)

   305   apply (erule trancl.induct)

   306   apply (rules dest: converseD intro: trancl_trans)+

   307   done

   308

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

   310   by (fastsimp simp add: split_tupled_all

   311     intro!: trancl_converseI trancl_converseD)

   312

   313 lemma converse_trancl_induct:

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

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

   316     ==> P(a)"

   317 proof -

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

   319   case rule_context

   320   show ?thesis

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

   322      apply (rule prems)

   323      apply (erule converseD)

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

   325     done

   326 qed

   327

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

   329   apply (erule converse_trancl_induct, auto)

   330   apply (blast intro: rtrancl_trans)

   331   done

   332

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

   334 by(blast elim: tranclE dest: trancl_into_rtrancl)

   335

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

   337   by (blast dest: r_into_trancl)

   338

   339 lemma trancl_subset_Sigma_aux:

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

   341   apply (erule rtrancl_induct, auto)

   342   done

   343

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

   345   apply (rule subsetI)

   346   apply (simp only: split_tupled_all)

   347   apply (erule tranclE)

   348   apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+

   349   done

   350

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

   352   apply safe

   353    apply (erule trancl_into_rtrancl)

   354   apply (blast elim: rtranclE dest: rtrancl_into_trancl1)

   355   done

   356

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

   358   apply safe

   359    apply (drule trancl_into_rtrancl, simp)

   360   apply (erule rtranclE, safe)

   361    apply (rule r_into_trancl, simp)

   362   apply (rule rtrancl_into_trancl1)

   363    apply (erule rtrancl_reflcl [THEN equalityD2, THEN subsetD], fast)

   364   done

   365

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

   367   by (auto elim: trancl_induct)

   368

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

   370   by (rule subst [OF reflcl_trancl]) simp

   371

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

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

   374

   375

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

   377

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

   379   by blast

   380

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

   382   by blast

   383

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

   385   by (rule rtrancl_Un_rtrancl [THEN subst]) fast

   386

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

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

   389

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

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

   392

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

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

   395

   396 lemma Not_Domain_rtrancl:

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

   398   apply auto

   399   by (erule rev_mp, erule rtrancl_induct, auto)

   400

   401

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

   403   be merged with main body. *}

   404

   405 lemma single_valued_confluent:

   406   "\<lbrakk> single_valued r; (x,y) \<in> r^*; (x,z) \<in> r^* \<rbrakk>

   407   \<Longrightarrow> (y,z) \<in> r^* \<or> (z,y) \<in> r^*"

   408 apply(erule rtrancl_induct)

   409  apply simp

   410 apply(erule disjE)

   411  apply(blast elim:converse_rtranclE dest:single_valuedD)

   412 apply(blast intro:rtrancl_trans)

   413 done

   414

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

   416   by (fast intro: trancl_trans)

   417

   418 lemma trancl_into_trancl [rule_format]:

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

   420   apply (erule trancl_induct)

   421    apply (fast intro: r_r_into_trancl)

   422   apply (fast intro: r_r_into_trancl trancl_trans)

   423   done

   424

   425 lemma trancl_rtrancl_trancl:

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

   427   apply (drule tranclD)

   428   apply (erule exE, erule conjE)

   429   apply (drule rtrancl_trans, assumption)

   430   apply (drule rtrancl_into_trancl2, assumption, assumption)

   431   done

   432

   433 lemmas transitive_closure_trans [trans] =

   434   r_r_into_trancl trancl_trans rtrancl_trans

   435   trancl_into_trancl trancl_into_trancl2

   436   rtrancl_into_rtrancl converse_rtrancl_into_rtrancl

   437   rtrancl_trancl_trancl trancl_rtrancl_trancl

   438

   439 declare trancl_into_rtrancl [elim]

   440

   441 declare rtranclE [cases set: rtrancl]

   442 declare tranclE [cases set: trancl]

   443

   444 end