src/HOL/Wellfounded_Recursion.thy
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 15343 444bb25d3da0
child 15950 5c067c956a20
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
     1 (*  ID:         $Id$
     2     Author:     Tobias Nipkow
     3     Copyright   1992  University of Cambridge
     4 *)
     5 
     6 header {*Well-founded Recursion*}
     7 
     8 theory Wellfounded_Recursion
     9 imports Transitive_Closure
    10 begin
    11 
    12 consts
    13   wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => ('a * 'b) set"
    14 
    15 inductive "wfrec_rel R F"
    16 intros
    17   wfrecI: "ALL z. (z, x) : R --> (z, g z) : wfrec_rel R F ==>
    18             (x, F g x) : wfrec_rel R F"
    19 
    20 constdefs
    21   wf         :: "('a * 'a)set => bool"
    22   "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
    23 
    24   acyclic :: "('a*'a)set => bool"
    25   "acyclic r == !x. (x,x) ~: r^+"
    26 
    27   cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
    28   "cut f r x == (%y. if (y,x):r then f y else arbitrary)"
    29 
    30   adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
    31   "adm_wf R F == ALL f g x.
    32      (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
    33 
    34   wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
    35   "wfrec R F == %x. THE y. (x, y) : wfrec_rel R (%f x. F (cut f R x) x)"
    36 
    37 axclass wellorder \<subseteq> linorder
    38   wf: "wf {(x,y::'a::ord). x<y}"
    39 
    40 
    41 lemma wfUNIVI: 
    42    "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
    43 by (unfold wf_def, blast)
    44 
    45 text{*Restriction to domain @{term A}.  
    46   If @{term r} is well-founded over @{term A} then @{term "wf r"}*}
    47 lemma wfI: 
    48  "[| r <= A <*> A;   
    49      !!x P. [| ALL x. (ALL y. (y,x) : r --> P y) --> P x;  x:A |] ==> P x |]   
    50   ==>  wf r"
    51 by (unfold wf_def, blast)
    52 
    53 lemma wf_induct: 
    54     "[| wf(r);           
    55         !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
    56      |]  ==>  P(a)"
    57 by (unfold wf_def, blast)
    58 
    59 lemma wf_not_sym [rule_format]: "wf(r) ==> ALL x. (a,x):r --> (x,a)~:r"
    60 by (erule_tac a=a in wf_induct, blast)
    61 
    62 (* [| wf r;  ~Z ==> (a,x) : r;  (x,a) ~: r ==> Z |] ==> Z *)
    63 lemmas wf_asym = wf_not_sym [elim_format]
    64 
    65 lemma wf_not_refl [simp]: "wf(r) ==> (a,a) ~: r"
    66 by (blast elim: wf_asym)
    67 
    68 (* [| wf r;  (a,a) ~: r ==> PROP W |] ==> PROP W *)
    69 lemmas wf_irrefl = wf_not_refl [elim_format]
    70 
    71 text{*transitive closure of a well-founded relation is well-founded! *}
    72 lemma wf_trancl: "wf(r) ==> wf(r^+)"
    73 apply (subst wf_def, clarify)
    74 apply (rule allE, assumption)
    75   --{*Retains the universal formula for later use!*}
    76 apply (erule mp)
    77 apply (erule_tac a = x in wf_induct)
    78 apply (blast elim: tranclE)
    79 done
    80 
    81 lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
    82 apply (subst trancl_converse [symmetric])
    83 apply (erule wf_trancl)
    84 done
    85 
    86 
    87 subsubsection{*Minimal-element characterization of well-foundedness*}
    88 
    89 lemma lemma1: "wf r ==> x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)"
    90 apply (unfold wf_def)
    91 apply (drule spec)
    92 apply (erule mp [THEN spec], blast)
    93 done
    94 
    95 lemma lemma2: "(ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)) ==> wf r"
    96 apply (unfold wf_def, clarify)
    97 apply (drule_tac x = "{x. ~ P x}" in spec, blast)
    98 done
    99 
   100 lemma wf_eq_minimal: "wf r = (ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q))"
   101 by (blast intro!: lemma1 lemma2)
   102 
   103 subsubsection{*Other simple well-foundedness results*}
   104 
   105 
   106 text{*Well-foundedness of subsets*}
   107 lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
   108 apply (simp (no_asm_use) add: wf_eq_minimal)
   109 apply fast
   110 done
   111 
   112 text{*Well-foundedness of the empty relation*}
   113 lemma wf_empty [iff]: "wf({})"
   114 by (simp add: wf_def)
   115 
   116 text{*Well-foundedness of insert*}
   117 lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
   118 apply (rule iffI)
   119  apply (blast elim: wf_trancl [THEN wf_irrefl]
   120               intro: rtrancl_into_trancl1 wf_subset 
   121                      rtrancl_mono [THEN [2] rev_subsetD])
   122 apply (simp add: wf_eq_minimal, safe)
   123 apply (rule allE, assumption, erule impE, blast) 
   124 apply (erule bexE)
   125 apply (rename_tac "a", case_tac "a = x")
   126  prefer 2
   127 apply blast 
   128 apply (case_tac "y:Q")
   129  prefer 2 apply blast
   130 apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
   131  apply assumption
   132 apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) 
   133   --{*essential for speed*}
   134 txt{*Blast with new substOccur fails*}
   135 apply (fast intro: converse_rtrancl_into_rtrancl)
   136 done
   137 
   138 text{*Well-foundedness of image*}
   139 lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
   140 apply (simp only: wf_eq_minimal, clarify)
   141 apply (case_tac "EX p. f p : Q")
   142 apply (erule_tac x = "{p. f p : Q}" in allE)
   143 apply (fast dest: inj_onD, blast)
   144 done
   145 
   146 
   147 subsubsection{*Well-Foundedness Results for Unions*}
   148 
   149 text{*Well-foundedness of indexed union with disjoint domains and ranges*}
   150 
   151 lemma wf_UN: "[| ALL i:I. wf(r i);  
   152          ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
   153       |] ==> wf(UN i:I. r i)"
   154 apply (simp only: wf_eq_minimal, clarify)
   155 apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
   156  prefer 2
   157  apply force 
   158 apply clarify
   159 apply (drule bspec, assumption)  
   160 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
   161 apply (blast elim!: allE)  
   162 done
   163 
   164 lemma wf_Union: 
   165  "[| ALL r:R. wf r;  
   166      ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
   167   |] ==> wf(Union R)"
   168 apply (simp add: Union_def)
   169 apply (blast intro: wf_UN)
   170 done
   171 
   172 (*Intuition: we find an (R u S)-min element of a nonempty subset A
   173              by case distinction.
   174   1. There is a step a -R-> b with a,b : A.
   175      Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
   176      By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
   177      subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
   178      have an S-successor and is thus S-min in A as well.
   179   2. There is no such step.
   180      Pick an S-min element of A. In this case it must be an R-min
   181      element of A as well.
   182 
   183 *)
   184 lemma wf_Un:
   185      "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
   186 apply (simp only: wf_eq_minimal, clarify) 
   187 apply (rename_tac A a)
   188 apply (case_tac "EX a:A. EX b:A. (b,a) : r") 
   189  prefer 2
   190  apply simp
   191  apply (drule_tac x=A in spec)+
   192  apply blast 
   193 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r) }" in allE)+
   194 apply (blast elim!: allE)  
   195 done
   196 
   197 subsubsection {*acyclic*}
   198 
   199 lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
   200 by (simp add: acyclic_def)
   201 
   202 lemma wf_acyclic: "wf r ==> acyclic r"
   203 apply (simp add: acyclic_def)
   204 apply (blast elim: wf_trancl [THEN wf_irrefl])
   205 done
   206 
   207 lemma acyclic_insert [iff]:
   208      "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
   209 apply (simp add: acyclic_def trancl_insert)
   210 apply (blast intro: rtrancl_trans)
   211 done
   212 
   213 lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
   214 by (simp add: acyclic_def trancl_converse)
   215 
   216 lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
   217 apply (simp add: acyclic_def antisym_def)
   218 apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
   219 done
   220 
   221 (* Other direction:
   222 acyclic = no loops
   223 antisym = only self loops
   224 Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
   225 ==> antisym( r^* ) = acyclic(r - Id)";
   226 *)
   227 
   228 lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
   229 apply (simp add: acyclic_def)
   230 apply (blast intro: trancl_mono)
   231 done
   232 
   233 
   234 subsection{*Well-Founded Recursion*}
   235 
   236 text{*cut*}
   237 
   238 lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
   239 by (simp add: expand_fun_eq cut_def)
   240 
   241 lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
   242 by (simp add: cut_def)
   243 
   244 text{*Inductive characterization of wfrec combinator; for details see:  
   245 John Harrison, "Inductive definitions: automation and application"*}
   246 
   247 lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. (x, y) : wfrec_rel R F"
   248 apply (simp add: adm_wf_def)
   249 apply (erule_tac a=x in wf_induct) 
   250 apply (rule ex1I)
   251 apply (rule_tac g = "%x. THE y. (x, y) : wfrec_rel R F" in wfrec_rel.wfrecI)
   252 apply (fast dest!: theI')
   253 apply (erule wfrec_rel.cases, simp)
   254 apply (erule allE, erule allE, erule allE, erule mp)
   255 apply (fast intro: the_equality [symmetric])
   256 done
   257 
   258 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
   259 apply (simp add: adm_wf_def)
   260 apply (intro strip)
   261 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
   262 apply (rule refl)
   263 done
   264 
   265 lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
   266 apply (simp add: wfrec_def)
   267 apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
   268 apply (rule wfrec_rel.wfrecI)
   269 apply (intro strip)
   270 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
   271 done
   272 
   273 
   274 text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
   275 lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
   276 apply auto
   277 apply (blast intro: wfrec)
   278 done
   279 
   280 
   281 subsection{*Variants for TFL: the Recdef Package*}
   282 
   283 lemma tfl_wf_induct: "ALL R. wf R -->  
   284        (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
   285 apply clarify
   286 apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
   287 done
   288 
   289 lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
   290 apply clarify
   291 apply (rule cut_apply, assumption)
   292 done
   293 
   294 lemma tfl_wfrec:
   295      "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
   296 apply clarify
   297 apply (erule wfrec)
   298 done
   299 
   300 subsection {*LEAST and wellorderings*}
   301 
   302 text{* See also @{text wf_linord_ex_has_least} and its consequences in
   303  @{text Wellfounded_Relations.ML}*}
   304 
   305 lemma wellorder_Least_lemma [rule_format]:
   306      "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"
   307 apply (rule_tac a = k in wf [THEN wf_induct])
   308 apply (rule impI)
   309 apply (rule classical)
   310 apply (rule_tac s = x in Least_equality [THEN ssubst], auto)
   311 apply (auto simp add: linorder_not_less [symmetric])
   312 done
   313 
   314 lemmas LeastI   = wellorder_Least_lemma [THEN conjunct1, standard]
   315 lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard]
   316 
   317 lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"
   318 apply (simp (no_asm_use) add: linorder_not_le [symmetric])
   319 apply (erule contrapos_nn)
   320 apply (erule Least_le)
   321 done
   322 
   323 ML
   324 {*
   325 val wf_def = thm "wf_def";
   326 val wfUNIVI = thm "wfUNIVI";
   327 val wfI = thm "wfI";
   328 val wf_induct = thm "wf_induct";
   329 val wf_not_sym = thm "wf_not_sym";
   330 val wf_asym = thm "wf_asym";
   331 val wf_not_refl = thm "wf_not_refl";
   332 val wf_irrefl = thm "wf_irrefl";
   333 val wf_trancl = thm "wf_trancl";
   334 val wf_converse_trancl = thm "wf_converse_trancl";
   335 val wf_eq_minimal = thm "wf_eq_minimal";
   336 val wf_subset = thm "wf_subset";
   337 val wf_empty = thm "wf_empty";
   338 val wf_insert = thm "wf_insert";
   339 val wf_UN = thm "wf_UN";
   340 val wf_Union = thm "wf_Union";
   341 val wf_Un = thm "wf_Un";
   342 val wf_prod_fun_image = thm "wf_prod_fun_image";
   343 val acyclicI = thm "acyclicI";
   344 val wf_acyclic = thm "wf_acyclic";
   345 val acyclic_insert = thm "acyclic_insert";
   346 val acyclic_converse = thm "acyclic_converse";
   347 val acyclic_impl_antisym_rtrancl = thm "acyclic_impl_antisym_rtrancl";
   348 val acyclic_subset = thm "acyclic_subset";
   349 val cuts_eq = thm "cuts_eq";
   350 val cut_apply = thm "cut_apply";
   351 val wfrec_unique = thm "wfrec_unique";
   352 val wfrec = thm "wfrec";
   353 val def_wfrec = thm "def_wfrec";
   354 val tfl_wf_induct = thm "tfl_wf_induct";
   355 val tfl_cut_apply = thm "tfl_cut_apply";
   356 val tfl_wfrec = thm "tfl_wfrec";
   357 val LeastI = thm "LeastI";
   358 val Least_le = thm "Least_le";
   359 val not_less_Least = thm "not_less_Least";
   360 *}
   361 
   362 end