src/HOL/Transitive_Closure.thy
 author obua Mon Apr 10 16:00:34 2006 +0200 (2006-04-10) changeset 19404 9bf2cdc9e8e8 parent 19228 30fce6da8cbe child 19623 12e6cc4382ae permissions -rw-r--r--
Moved stuff from Ring_and_Field to Matrix
     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

    10 imports Inductive

    11 uses ("../Provers/trancl.ML")

    12 begin

    13

    14 text {*

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

    16   @{text trancl} is transitive closure,

    17   @{text reflcl} is reflexive closure.

    18

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

    20   operands to be atomic.

    21 *}

    22

    23 consts

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

    25

    26 inductive "r^*"

    27   intros

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

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

    30

    31 consts

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

    33

    34 inductive "r^+"

    35   intros

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

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

    38

    39 syntax

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

    41 translations

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

    43

    44 syntax (xsymbols)

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

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

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

    48

    49 syntax (HTML output)

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

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

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

    53

    54

    55 subsection {* Reflexive-transitive closure *}

    56

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

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

    59   apply (simp only: split_tupled_all)

    60   apply (erule rtrancl_refl [THEN rtrancl_into_rtrancl])

    61   done

    62

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

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

    65   apply (rule subsetI)

    66   apply (simp only: split_tupled_all)

    67   apply (erule rtrancl.induct)

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

    69   done

    70

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

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

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

    74   shows "P b"

    75 proof -

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

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

    78   thus ?thesis by iprover

    79 qed

    80

    81 lemmas rtrancl_induct2 =

    82   rtrancl_induct[of "(ax,ay)" "(bx,by)", split_format (complete),

    83                  consumes 1, case_names refl step]

    84

    85 lemma reflexive_rtrancl: "reflexive (r^*)"

    86   by (unfold refl_def) fast

    87

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

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

    90 proof (rule transI)

    91   fix x y z

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

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

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

    95 qed

    96

    97 lemmas rtrancl_trans = trans_rtrancl [THEN transD, standard]

    98

    99 lemma rtranclE:

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

   101     and cases: "(a = b) ==> P"

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

   103   shows P

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

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

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

   107     prefer 2 apply blast

   108    prefer 2 apply blast

   109   apply (erule asm_rl exE disjE conjE cases)+

   110   done

   111

   112 lemma converse_rtrancl_into_rtrancl:

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

   114   by (rule rtrancl_trans) iprover+

   115

   116 text {*

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

   118 *}

   119

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

   121   apply auto

   122   apply (erule rtrancl_induct)

   123    apply (rule rtrancl_refl)

   124   apply (blast intro: rtrancl_trans)

   125   done

   126

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

   128   apply (rule set_ext)

   129   apply (simp only: split_tupled_all)

   130   apply (blast intro: rtrancl_trans)

   131   done

   132

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

   134 by (drule rtrancl_mono, simp)

   135

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

   137   apply (drule rtrancl_mono)

   138   apply (drule rtrancl_mono, simp)

   139   done

   140

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

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

   143

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

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

   146

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

   148   apply (rule sym)

   149   apply (rule rtrancl_subset, blast, clarify)

   150   apply (rename_tac a b)

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

   152   apply (blast intro!: r_into_rtrancl)

   153   done

   154

   155 theorem rtrancl_converseD:

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

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

   158 proof -

   159   from r show ?thesis

   160     by induct (iprover intro: rtrancl_trans dest!: converseD)+

   161 qed

   162

   163 theorem rtrancl_converseI:

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

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

   166 proof -

   167   from r show ?thesis

   168     by induct (iprover intro: rtrancl_trans converseI)+

   169 qed

   170

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

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

   173

   174 lemma sym_rtrancl: "sym r ==> sym (r^*)"

   175   by (simp only: sym_conv_converse_eq rtrancl_converse [symmetric])

   176

   177 theorem converse_rtrancl_induct[consumes 1]:

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

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

   180   shows "P a"

   181 proof -

   182   from rtrancl_converseI [OF major]

   183   show ?thesis

   184     by induct (iprover intro: cases dest!: converseD rtrancl_converseD)+

   185 qed

   186

   187 lemmas converse_rtrancl_induct2 =

   188   converse_rtrancl_induct[of "(ax,ay)" "(bx,by)", split_format (complete),

   189                  consumes 1, case_names refl step]

   190

   191 lemma converse_rtranclE:

   192   assumes major: "(x,z):r^*"

   193     and cases: "x=z ==> P"

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

   195   shows P

   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 iprover

   199    prefer 2 apply iprover

   200   apply (erule asm_rl exE disjE conjE cases)+

   201   done

   202

   203 ML_setup {*

   204   bind_thm ("converse_rtranclE2", split_rule

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

   206 *}

   207

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

   209   by (blast elim: rtranclE converse_rtranclE

   210     intro: rtrancl_into_rtrancl converse_rtrancl_into_rtrancl)

   211

   212 lemma rtrancl_unfold: "r^* = Id Un (r O r^*)"

   213   by (auto intro: rtrancl_into_rtrancl elim: rtranclE)

   214

   215

   216 subsection {* Transitive closure *}

   217

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

   219   apply (simp only: split_tupled_all)

   220   apply (erule trancl.induct)

   221   apply (iprover dest: subsetD)+

   222   done

   223

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

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

   226

   227 text {*

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

   229 *}

   230

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

   232   by (erule trancl.induct) iprover+

   233

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

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

   236   by induct iprover+

   237

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

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

   240   apply (erule rtranclE, iprover)

   241   apply (rule rtrancl_trans [THEN rtrancl_into_trancl1])

   242    apply (assumption | rule r_into_rtrancl)+

   243   done

   244

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

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

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

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

   249   shows "P b"

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

   251 proof -

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

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

   254   thus ?thesis by iprover

   255 qed

   256

   257 lemma trancl_trans_induct:

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

   259     and cases: "!!x y. (x,y) : r ==> P x y"

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

   261   shows "P x y"

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

   263   by (iprover intro: r_into_trancl major [THEN trancl_induct] cases)

   264

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

   266

   267 lemma trancl_unfold: "r^+ = r Un (r O r^+)"

   268   by (auto intro: trancl_into_trancl elim: tranclE)

   269

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

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

   272 proof (rule transI)

   273   fix x y z

   274   assume xy: "(x, y) \<in> r^+"

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

   276   thus "(x, z) \<in> r^+" by induct (insert xy, iprover)+

   277 qed

   278

   279 lemmas trancl_trans = trans_trancl [THEN transD, standard]

   280

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

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

   283   by induct (iprover intro: trancl_trans)+

   284

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

   286   by (erule transD [OF trans_trancl r_into_trancl])

   287

   288 lemma trancl_insert:

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

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

   291   apply (rule equalityI)

   292    apply (rule subsetI)

   293    apply (simp only: split_tupled_all)

   294    apply (erule trancl_induct, blast)

   295    apply (blast intro: rtrancl_into_trancl1 trancl_into_rtrancl r_into_trancl trancl_trans)

   296   apply (rule subsetI)

   297   apply (blast intro: trancl_mono rtrancl_mono

   298     [THEN [2] rev_subsetD] rtrancl_trancl_trancl rtrancl_into_trancl2)

   299   done

   300

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

   302   apply (drule converseD)

   303   apply (erule trancl.induct)

   304   apply (iprover intro: converseI trancl_trans)+

   305   done

   306

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

   308   apply (rule converseI)

   309   apply (erule trancl.induct)

   310   apply (iprover dest: converseD intro: trancl_trans)+

   311   done

   312

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

   314   by (fastsimp simp add: split_tupled_all

   315     intro!: trancl_converseI trancl_converseD)

   316

   317 lemma sym_trancl: "sym r ==> sym (r^+)"

   318   by (simp only: sym_conv_converse_eq trancl_converse [symmetric])

   319

   320 lemma converse_trancl_induct:

   321   assumes major: "(a,b) : r^+"

   322     and cases: "!!y. (y,b) : r ==> P(y)"

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

   324   shows "P a"

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

   326    apply (rule cases)

   327    apply (erule converseD)

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

   329   done

   330

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

   332   apply (erule converse_trancl_induct, auto)

   333   apply (blast intro: rtrancl_trans)

   334   done

   335

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

   337   by (blast elim: tranclE dest: trancl_into_rtrancl)

   338

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

   340   by (blast dest: r_into_trancl)

   341

   342 lemma trancl_subset_Sigma_aux:

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

   344   by (induct rule: rtrancl_induct) auto

   345

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

   347   apply (rule subsetI)

   348   apply (simp only: split_tupled_all)

   349   apply (erule tranclE)

   350   apply (blast dest!: trancl_into_rtrancl trancl_subset_Sigma_aux)+

   351   done

   352

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

   354   apply safe

   355    apply (erule trancl_into_rtrancl)

   356   apply (blast elim: rtranclE dest: rtrancl_into_trancl1)

   357   done

   358

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

   360   apply safe

   361    apply (drule trancl_into_rtrancl, simp)

   362   apply (erule rtranclE, safe)

   363    apply (rule r_into_trancl, simp)

   364   apply (rule rtrancl_into_trancl1)

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

   366   done

   367

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

   369   by (auto elim: trancl_induct)

   370

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

   372   by (rule subst [OF reflcl_trancl]) simp

   373

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

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

   376

   377 lemma rtrancl_eq_or_trancl:

   378   "(x,y) \<in> R\<^sup>* = (x=y \<or> x\<noteq>y \<and> (x,y) \<in> R\<^sup>+)"

   379   by (fast elim: trancl_into_rtrancl dest: rtranclD)

   380

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

   382

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

   384   by blast

   385

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

   387   by blast

   388

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

   390   by (rule rtrancl_Un_rtrancl [THEN subst]) fast

   391

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

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

   394

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

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

   397

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

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

   400

   401 lemma Not_Domain_rtrancl:

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

   403   apply auto

   404   by (erule rev_mp, erule rtrancl_induct, auto)

   405

   406

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

   408   be merged with main body. *}

   409

   410 lemma single_valued_confluent:

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

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

   413 apply(erule rtrancl_induct)

   414  apply simp

   415 apply(erule disjE)

   416  apply(blast elim:converse_rtranclE dest:single_valuedD)

   417 apply(blast intro:rtrancl_trans)

   418 done

   419

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

   421   by (fast intro: trancl_trans)

   422

   423 lemma trancl_into_trancl [rule_format]:

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

   425   apply (erule trancl_induct)

   426    apply (fast intro: r_r_into_trancl)

   427   apply (fast intro: r_r_into_trancl trancl_trans)

   428   done

   429

   430 lemma trancl_rtrancl_trancl:

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

   432   apply (drule tranclD)

   433   apply (erule exE, erule conjE)

   434   apply (drule rtrancl_trans, assumption)

   435   apply (drule rtrancl_into_trancl2, assumption, assumption)

   436   done

   437

   438 lemmas transitive_closure_trans [trans] =

   439   r_r_into_trancl trancl_trans rtrancl_trans

   440   trancl_into_trancl trancl_into_trancl2

   441   rtrancl_into_rtrancl converse_rtrancl_into_rtrancl

   442   rtrancl_trancl_trancl trancl_rtrancl_trancl

   443

   444 declare trancl_into_rtrancl [elim]

   445

   446 declare rtranclE [cases set: rtrancl]

   447 declare tranclE [cases set: trancl]

   448

   449

   450

   451

   452

   453 subsection {* Setup of transitivity reasoner *}

   454

   455 use "../Provers/trancl.ML";

   456

   457 ML_setup {*

   458

   459 structure Trancl_Tac = Trancl_Tac_Fun (

   460   struct

   461     val r_into_trancl = thm "r_into_trancl";

   462     val trancl_trans  = thm "trancl_trans";

   463     val rtrancl_refl = thm "rtrancl_refl";

   464     val r_into_rtrancl = thm "r_into_rtrancl";

   465     val trancl_into_rtrancl = thm "trancl_into_rtrancl";

   466     val rtrancl_trancl_trancl = thm "rtrancl_trancl_trancl";

   467     val trancl_rtrancl_trancl = thm "trancl_rtrancl_trancl";

   468     val rtrancl_trans = thm "rtrancl_trans";

   469

   470   fun decomp (Trueprop $t) =   471 let fun dec (Const ("op :", _)$ (Const ("Pair", _) $a$ b) $rel ) =   472 let fun decr (Const ("Transitive_Closure.rtrancl", _ )$ r) = (r,"r*")

   473               | decr (Const ("Transitive_Closure.trancl", _ ) \$ r)  = (r,"r+")

   474               | decr r = (r,"r");

   475             val (rel,r) = decr rel;

   476         in SOME (a,b,rel,r) end

   477       | dec _ =  NONE

   478     in dec t end;

   479

   480   end); (* struct *)

   481

   482 change_simpset (fn ss => ss

   483   addSolver (mk_solver "Trancl" (fn _ => Trancl_Tac.trancl_tac))

   484   addSolver (mk_solver "Rtrancl" (fn _ => Trancl_Tac.rtrancl_tac)));

   485

   486 *}

   487

   488 (* Optional methods

   489

   490 method_setup trancl =

   491   {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (trancl_tac)) *}

   492   {* simple transitivity reasoner *}

   493 method_setup rtrancl =

   494   {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (rtrancl_tac)) *}

   495   {* simple transitivity reasoner *}

   496

   497 *)

   498

   499 end