# HG changeset patch # User paulson # Date 1101733375 -3600 # Node ID 254f6f00b60e4f2ed98d5eb6fecd269f71fd7f4c # Parent cd18d7b73a64b58ec1badc914821ed0a6342f1be converted to Isar script, simplifying some results diff -r cd18d7b73a64 -r 254f6f00b60e src/HOL/IsaMakefile --- a/src/HOL/IsaMakefile Mon Nov 29 11:25:32 2004 +0100 +++ b/src/HOL/IsaMakefile Mon Nov 29 14:02:55 2004 +0100 @@ -112,7 +112,7 @@ Tools/specification_package.ML \ Tools/split_rule.ML Tools/typedef_package.ML \ Transitive_Closure.thy Transitive_Closure.ML Typedef.thy \ - Wellfounded_Recursion.ML Wellfounded_Recursion.thy Wellfounded_Relations.ML \ + Wellfounded_Recursion.thy Wellfounded_Relations.ML \ Wellfounded_Relations.thy arith_data.ML antisym_setup.ML \ blastdata.ML cladata.ML \ document/root.tex hologic.ML simpdata.ML thy_syntax.ML diff -r cd18d7b73a64 -r 254f6f00b60e src/HOL/MicroJava/BV/Listn.thy --- a/src/HOL/MicroJava/BV/Listn.thy Mon Nov 29 11:25:32 2004 +0100 +++ b/src/HOL/MicroJava/BV/Listn.thy Mon Nov 29 14:02:55 2004 +0100 @@ -185,19 +185,19 @@ done lemma in_list_Suc_iff: - "(xs : list (Suc n) A) = (? y:A. ? ys:list n A. xs = y#ys)" + "(xs : list (Suc n) A) = (\y\ A. \ys\ list n A. xs = y#ys)" apply (unfold list_def) apply (case_tac "xs") apply auto done lemma Cons_in_list_Suc [iff]: - "(x#xs : list (Suc n) A) = (x:A & xs : list n A)"; + "(x#xs : list (Suc n) A) = (x\ A & xs : list n A)"; apply (simp add: in_list_Suc_iff) done lemma list_not_empty: - "? a. a:A \ ? xs. xs : list n A"; + "\a. a\ A \ \xs. xs : list n A"; apply (induct "n") apply simp apply (simp add: in_list_Suc_iff) @@ -248,7 +248,7 @@ lemma listt_update_in_list [simp, intro!]: - "\ xs : list n A; x:A \ \ xs[i := x] : list n A" + "\ xs : list n A; x\ A \ \ xs[i := x] : list n A" apply (unfold list_def) apply simp done @@ -306,7 +306,7 @@ done lemma (in semilat) list_update_incr [rule_format]: - "x:A \ set xs <= A \ + "x\ A \ set xs <= A \ (!i. i xs <=[r] xs[i := x +_f xs!i])" apply (unfold unfold_lesub_list) apply (simp add: Listn.le_def list_all2_conv_all_nth) @@ -330,8 +330,6 @@ apply (rename_tac m n) apply (case_tac "m=n") apply simp - apply (rule conjI) - apply (fast intro!: equals0I dest: not_sym) apply (fast intro!: equals0I dest: not_sym) apply clarify apply (rename_tac n) @@ -342,15 +340,15 @@ apply (simp (no_asm) add: length_Suc_conv cong: conj_cong) apply clarify apply (rename_tac M m) -apply (case_tac "? x xs. size xs = k & x#xs : M") +apply (case_tac "\x xs. size xs = k & x#xs : M") prefer 2 apply (erule thin_rl) apply (erule thin_rl) apply blast -apply (erule_tac x = "{a. ? xs. size xs = k & a#xs:M}" in allE) +apply (erule_tac x = "{a. \xs. size xs = k & a#xs:M}" in allE) apply (erule impE) apply blast -apply (thin_tac "? x xs. ?P x xs") +apply (thin_tac "\x xs. ?P x xs") apply clarify apply (rename_tac maxA xs) apply (erule_tac x = "{ys. size ys = size xs & maxA#ys : M}" in allE) @@ -435,8 +433,8 @@ done lemma lift2_le_ub: - "\ semilat(err A, Err.le r, lift2 f); x:A; y:A; x +_f y = OK z; - u:A; x <=_r u; y <=_r u \ \ z <=_r u" + "\ semilat(err A, Err.le r, lift2 f); x\ A; y\ A; x +_f y = OK z; + u\ A; x <=_r u; y <=_r u \ \ z <=_r u" apply (unfold semilat_Def plussub_def err_def) apply (simp add: lift2_def) apply clarify @@ -464,16 +462,16 @@ done lemma lift2_eq_ErrD: - "\ x +_f y = Err; semilat(err A, Err.le r, lift2 f); x:A; y:A \ - \ ~(? u:A. x <=_r u & y <=_r u)" + "\ x +_f y = Err; semilat(err A, Err.le r, lift2 f); x\ A; y\ A \ + \ ~(\u\ A. x <=_r u & y <=_r u)" by (simp add: OK_plus_OK_eq_Err_conv [THEN iffD1]) lemma coalesce_eq_Err_D [rule_format]: "\ semilat(err A, Err.le r, lift2 f) \ - \ !xs. xs:list n A \ (!ys. ys:list n A \ + \ !xs. xs\ list n A \ (!ys. ys\ list n A \ coalesce (xs +[f] ys) = Err \ - ~(? zs:list n A. xs <=[r] zs & ys <=[r] zs))" + ~(\zs\ list n A. xs <=[r] zs & ys <=[r] zs))" apply (induct n) apply simp apply clarify @@ -484,14 +482,14 @@ done lemma closed_err_lift2_conv: - "closed (err A) (lift2 f) = (!x:A. !y:A. x +_f y : err A)" + "closed (err A) (lift2 f) = (\x\ A. \y\ A. x +_f y : err A)" apply (unfold closed_def) apply (simp add: err_def) done lemma closed_map2_list [rule_format]: "closed (err A) (lift2 f) \ - !xs. xs : list n A \ (!ys. ys : list n A \ + \xs. xs : list n A \ (\ys. ys : list n A \ map2 f xs ys : list n (err A))" apply (unfold map2_def) apply (induct n) diff -r cd18d7b73a64 -r 254f6f00b60e src/HOL/Nat.ML --- a/src/HOL/Nat.ML Mon Nov 29 11:25:32 2004 +0100 +++ b/src/HOL/Nat.ML Mon Nov 29 14:02:55 2004 +0100 @@ -25,10 +25,8 @@ bind_thm ("nat_case_0", nat_case_0); bind_thm ("nat_case_Suc", nat_case_Suc); -val LeastI = thm "LeastI"; val Least_Suc = thm "Least_Suc"; val Least_Suc2 = thm "Least_Suc2"; -val Least_le = thm "Least_le"; val One_nat_def = thm "One_nat_def"; val Suc_Suc_eq = thm "Suc_Suc_eq"; val Suc_def = thm "Suc_def"; @@ -218,7 +216,6 @@ val not_leE = thm "not_leE"; val not_le_iff_less = thm "not_le_iff_less"; val not_less0 = thm "not_less0"; -val not_less_Least = thm "not_less_Least"; val not_less_eq = thm "not_less_eq"; val not_less_iff_le = thm "not_less_iff_le"; val not_less_less_Suc_eq = thm "not_less_less_Suc_eq"; diff -r cd18d7b73a64 -r 254f6f00b60e src/HOL/Nat.thy --- a/src/HOL/Nat.thy Mon Nov 29 11:25:32 2004 +0100 +++ b/src/HOL/Nat.thy Mon Nov 29 14:02:55 2004 +0100 @@ -564,11 +564,7 @@ apply (blast intro: less_trans)+ done -subsection {* @{text LEAST} theorems for type @{typ nat} by specialization *} - -lemmas LeastI = wellorder_LeastI -lemmas Least_le = wellorder_Least_le -lemmas not_less_Least = wellorder_not_less_Least +subsection {* @{text LEAST} theorems for type @{typ nat}*} lemma Least_Suc: "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))" diff -r cd18d7b73a64 -r 254f6f00b60e src/HOL/Wellfounded_Recursion.ML --- a/src/HOL/Wellfounded_Recursion.ML Mon Nov 29 11:25:32 2004 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,333 +0,0 @@ -(* Title: HOL/Wellfounded_Recursion.ML - ID: $Id$ - Author: Tobias Nipkow, with minor changes by Konrad Slind - Copyright 1992 University of Cambridge/1995 TU Munich - -Wellfoundedness, induction, and recursion -*) - -Goal "x = y ==> H x z = H y z"; -by (Asm_simp_tac 1); -val H_cong2 = (*freeze H!*) - read_instantiate [("H","H")] (result()); - -val [prem] = Goalw [wf_def] - "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"; -by (Clarify_tac 1); -by (rtac prem 1); -by (assume_tac 1); -qed "wfUNIVI"; - -(*Restriction to domain A. If r is well-founded over A then wf(r)*) -val [prem1,prem2] = Goalw [wf_def] - "[| r <= A <*> A; \ -\ !!x P. [| ALL x. (ALL y. (y,x) : r --> P y) --> P x; x:A |] ==> P x |] \ -\ ==> wf r"; -by (cut_facts_tac [prem1] 1); -by (blast_tac (claset() addIs [prem2]) 1); -qed "wfI"; - -val major::prems = Goalw [wf_def] - "[| wf(r); \ -\ !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x) \ -\ |] ==> P(a)"; -by (rtac (major RS spec RS mp RS spec) 1); -by (blast_tac (claset() addIs prems) 1); -qed "wf_induct"; - -(*Perform induction on i, then prove the wf(r) subgoal using prems. *) -fun wf_ind_tac a prems i = - EVERY [res_inst_tac [("a",a)] wf_induct i, - rename_last_tac a ["1"] (i+1), - ares_tac prems i]; - -Goal "wf(r) ==> ALL x. (a,x):r --> (x,a)~:r"; -by (wf_ind_tac "a" [] 1); -by (Blast_tac 1); -qed_spec_mp "wf_not_sym"; - -(* [| wf r; ~Z ==> (a,x) : r; (x,a) ~: r ==> Z |] ==> Z *) -bind_thm ("wf_asym", cla_make_elim wf_not_sym); - -Goal "wf(r) ==> (a,a) ~: r"; -by (blast_tac (claset() addEs [wf_asym]) 1); -qed "wf_not_refl"; -Addsimps [wf_not_refl]; - -(* [| wf r; (a,a) ~: r ==> PROP W |] ==> PROP W *) -bind_thm ("wf_irrefl", make_elim wf_not_refl); - -(*transitive closure of a wf relation is wf! *) -Goal "wf(r) ==> wf(r^+)"; -by (stac wf_def 1); -by (Clarify_tac 1); -(*must retain the universal formula for later use!*) -by (rtac allE 1 THEN assume_tac 1); -by (etac mp 1); -by (eres_inst_tac [("a","x")] wf_induct 1); -by (blast_tac (claset() addEs [tranclE]) 1); -qed "wf_trancl"; - -Goal "wf (r^-1) ==> wf ((r^+)^-1)"; -by (stac (trancl_converse RS sym) 1); -by (etac wf_trancl 1); -qed "wf_converse_trancl"; - - -(*---------------------------------------------------------------------------- - * Minimal-element characterization of well-foundedness - *---------------------------------------------------------------------------*) - -Goalw [wf_def] "wf r ==> x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)"; -by (dtac spec 1); -by (etac (mp RS spec) 1); -by (Blast_tac 1); -val lemma1 = result(); - -Goalw [wf_def] "(ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)) ==> wf r"; -by (Clarify_tac 1); -by (dres_inst_tac [("x", "{x. ~ P x}")] spec 1); -by (Blast_tac 1); -val lemma2 = result(); - -Goal "wf r = (ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q))"; -by (blast_tac (claset() addSIs [lemma1, lemma2]) 1); -qed "wf_eq_minimal"; - -(*--------------------------------------------------------------------------- - * Wellfoundedness of subsets - *---------------------------------------------------------------------------*) - -Goal "[| wf(r); p<=r |] ==> wf(p)"; -by (full_simp_tac (simpset() addsimps [wf_eq_minimal]) 1); -by (Fast_tac 1); -qed "wf_subset"; - -(*--------------------------------------------------------------------------- - * Wellfoundedness of the empty relation. - *---------------------------------------------------------------------------*) - -Goal "wf({})"; -by (simp_tac (simpset() addsimps [wf_def]) 1); -qed "wf_empty"; -AddIffs [wf_empty]; - -(*--------------------------------------------------------------------------- - * Wellfoundedness of `insert' - *---------------------------------------------------------------------------*) - -Goal "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"; -by (rtac iffI 1); - by (blast_tac (claset() addEs [wf_trancl RS wf_irrefl] - addIs [rtrancl_into_trancl1,wf_subset,impOfSubs rtrancl_mono]) 1); -by (asm_full_simp_tac (simpset() addsimps [wf_eq_minimal]) 1); -by Safe_tac; -by (EVERY1[rtac allE, assume_tac, etac impE, Blast_tac]); -by (etac bexE 1); -by (rename_tac "a" 1 THEN case_tac "a = x" 1); - by (res_inst_tac [("x","a")]bexI 2); - by (assume_tac 3); - by (Blast_tac 2); -by (case_tac "y:Q" 1); - by (Blast_tac 2); -by (res_inst_tac [("x","{z. z:Q & (z,y) : r^*}")] allE 1); - by (assume_tac 1); -by (thin_tac "ALL Q. (EX x. x : Q) --> ?P Q" 1); (*essential for speed*) -(*Blast_tac with new substOccur fails*) -by (best_tac (claset() addIs [converse_rtrancl_into_rtrancl]) 1); -qed "wf_insert"; -AddIffs [wf_insert]; - -(*--------------------------------------------------------------------------- - * Wellfoundedness of `disjoint union' - *---------------------------------------------------------------------------*) - -(*Intuition behind this proof for the case of binary union: - - Goal: find an (R u S)-min element of a nonempty subset A. - by case distinction: - 1. There is a step a -R-> b with a,b : A. - Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}. - By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the - subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot - have an S-successor and is thus S-min in A as well. - 2. There is no such step. - Pick an S-min element of A. In this case it must be an R-min - element of A as well. - -*) - -Goal "[| ALL i:I. wf(r i); \ -\ ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {} & \ -\ Domain(r j) Int Range(r i) = {} \ -\ |] ==> wf(UN i:I. r i)"; -by (asm_full_simp_tac (HOL_basic_ss addsimps [wf_eq_minimal]) 1); -by (Clarify_tac 1); -by (rename_tac "A a" 1 THEN case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i" 1); - by (Asm_full_simp_tac 2); - by (Best_tac 2); (*much faster than Blast_tac*) -by (Clarify_tac 1); -by (EVERY1[dtac bspec, assume_tac, - eres_inst_tac [("x","{a. a:A & (EX b:A. (b,a) : r i)}")] allE]); -by (EVERY1[etac allE, etac impE]); - by (ALLGOALS Blast_tac); -qed "wf_UN"; - -Goalw [Union_def] - "[| ALL r:R. wf r; \ -\ ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {} & \ -\ Domain s Int Range r = {} \ -\ |] ==> wf(Union R)"; -by (blast_tac (claset() addIs [wf_UN]) 1); -qed "wf_Union"; - -Goal "[| wf r; wf s; Domain r Int Range s = {}; Domain s Int Range r = {} \ -\ |] ==> wf(r Un s)"; -by (rtac (simplify (simpset()) (read_instantiate[("R","{r,s}")]wf_Union)) 1); -by (Blast_tac 1); -by (Blast_tac 1); -qed "wf_Un"; - -(*--------------------------------------------------------------------------- - * Wellfoundedness of `image' - *---------------------------------------------------------------------------*) - -Goal "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"; -by (asm_full_simp_tac (HOL_basic_ss addsimps [wf_eq_minimal]) 1); -by (Clarify_tac 1); -by (case_tac "EX p. f p : Q" 1); -by (eres_inst_tac [("x","{p. f p : Q}")]allE 1); -by (fast_tac (claset() addDs [injD]) 1); -by (Blast_tac 1); -qed "wf_prod_fun_image"; - -(*** acyclic ***) - -Goalw [acyclic_def] "ALL x. (x, x) ~: r^+ ==> acyclic r"; -by (assume_tac 1); -qed "acyclicI"; - -Goalw [acyclic_def] "wf r ==> acyclic r"; -by (blast_tac (claset() addEs [wf_trancl RS wf_irrefl]) 1); -qed "wf_acyclic"; - -Goalw [acyclic_def] "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"; -by (simp_tac (simpset() addsimps [trancl_insert]) 1); -by (blast_tac (claset() addIs [rtrancl_trans]) 1); -qed "acyclic_insert"; -AddIffs [acyclic_insert]; - -Goalw [acyclic_def] "acyclic(r^-1) = acyclic r"; -by (simp_tac (simpset() addsimps [trancl_converse]) 1); -qed "acyclic_converse"; -AddIffs [acyclic_converse]; - -Goalw [acyclic_def,antisym_def] "acyclic r ==> antisym(r^*)"; -by (blast_tac (claset() addEs [rtranclE] - addIs [rtrancl_into_trancl1,rtrancl_trancl_trancl]) 1); -qed "acyclic_impl_antisym_rtrancl"; - -(* Other direction: -acyclic = no loops -antisym = only self loops -Goalw [acyclic_def,antisym_def] "antisym(r^* ) ==> acyclic(r - Id)"; -==> "antisym(r^* ) = acyclic(r - Id)"; -*) - -Goalw [acyclic_def] "[| acyclic s; r <= s |] ==> acyclic r"; -by (blast_tac (claset() addIs [trancl_mono]) 1); -qed "acyclic_subset"; - -(** cut **) - -(*This rewrite rule works upon formulae; thus it requires explicit use of - H_cong to expose the equality*) -Goalw [cut_def] "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"; -by (simp_tac (HOL_ss addsimps [expand_fun_eq]) 1); -qed "cuts_eq"; - -Goalw [cut_def] "(x,a):r ==> (cut f r a)(x) = f(x)"; -by (asm_simp_tac HOL_ss 1); -qed "cut_apply"; - -(*** Inductive characterization of wfrec combinator; for details see: ***) -(*** John Harrison, "Inductive definitions: automation and application" ***) - -Goalw [adm_wf_def] - "[| adm_wf R F; wf R |] ==> EX! y. (x, y) : wfrec_rel R F"; -by (wf_ind_tac "x" [] 1); -by (rtac ex1I 1); -by (res_inst_tac [("g","%x. THE y. (x, y) : wfrec_rel R F")] wfrec_rel.wfrecI 1); -by (fast_tac (claset() addSDs [theI']) 1); -by (etac wfrec_rel.elim 1); -by (Asm_full_simp_tac 1); -byev [etac allE 1, etac allE 1, etac allE 1, etac mp 1]; -by (fast_tac (claset() addIs [the_equality RS sym]) 1); -qed "wfrec_unique"; - -Goalw [adm_wf_def] "adm_wf R (%f x. F (cut f R x) x)"; -by (strip_tac 1); -by (rtac (cuts_eq RS iffD2 RS subst) 1); -by (atac 1); -by (rtac refl 1); -qed "adm_lemma"; - -Goalw [wfrec_def] - "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"; -by (rtac (adm_lemma RS wfrec_unique RS the1_equality) 1); -by (atac 1); -by (rtac wfrec_rel.wfrecI 1); -by (strip_tac 1); -by (etac (adm_lemma RS wfrec_unique RS theI') 1); -qed "wfrec"; - - -(*--------------------------------------------------------------------------- - * This form avoids giant explosions in proofs. NOTE USE OF == - *---------------------------------------------------------------------------*) -Goal "[| f==wfrec r H; wf(r) |] ==> f(a) = H (cut f r a) a"; -by Auto_tac; -by (blast_tac (claset() addIs [wfrec]) 1); -qed "def_wfrec"; - - -(**** TFL variants ****) - -Goal "ALL R. wf R --> \ -\ (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"; -by (Clarify_tac 1); -by (res_inst_tac [("r","R"),("P","P"), ("a","x")] wf_induct 1); -by (assume_tac 1); -by (Blast_tac 1); -qed"tfl_wf_induct"; - -Goal "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"; -by (Clarify_tac 1); -by (rtac cut_apply 1); -by (assume_tac 1); -qed"tfl_cut_apply"; - -Goal "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"; -by (Clarify_tac 1); -by (etac wfrec 1); -qed "tfl_wfrec"; - -(*LEAST and wellorderings*) -(* ### see also wf_linord_ex_has_least and its consequences in Wellfounded_Relations.ML *) - -Goal "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"; -by (res_inst_tac [("a","k")] (wf RS wf_induct) 1); -by (rtac impI 1); -by (rtac classical 1); -by (res_inst_tac [("s","x")] (Least_equality RS ssubst) 1); -by Auto_tac; -by (auto_tac (claset(), simpset() addsimps [linorder_not_less RS sym])); -bind_thm("wellorder_LeastI", result() RS mp RS conjunct1); -bind_thm("wellorder_Least_le", result() RS mp RS conjunct2); - -Goal "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"; -by (full_simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1); -by (etac contrapos_nn 1); -by (etac wellorder_Least_le 1); -qed "wellorder_not_less_Least"; - diff -r cd18d7b73a64 -r 254f6f00b60e src/HOL/Wellfounded_Recursion.thy --- a/src/HOL/Wellfounded_Recursion.thy Mon Nov 29 11:25:32 2004 +0100 +++ b/src/HOL/Wellfounded_Recursion.thy Mon Nov 29 14:02:55 2004 +0100 @@ -1,19 +1,20 @@ -(* Title: HOL/Wellfounded_Recursion.thy - ID: $Id$ +(* ID: $Id$ Author: Tobias Nipkow Copyright 1992 University of Cambridge - -Well-founded Recursion *) -Wellfounded_Recursion = Transitive_Closure + +header {*Well-founded Recursion*} + +theory Wellfounded_Recursion +imports Transitive_Closure +begin consts wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => ('a * 'b) set" inductive "wfrec_rel R F" -intrs - wfrecI "ALL z. (z, x) : R --> (z, g z) : wfrec_rel R F ==> +intros + wfrecI: "ALL z. (z, x) : R --> (z, g z) : wfrec_rel R F ==> (x, F g x) : wfrec_rel R F" constdefs @@ -33,8 +34,329 @@ wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" "wfrec R F == %x. THE y. (x, y) : wfrec_rel R (%f x. F (cut f R x) x)" -axclass - wellorder < linorder - wf "wf {(x,y::'a::ord). x linorder + wf: "wf {(x,y::'a::ord). x P(y)) --> P(x)) ==> P(x)) ==> wf(r)" +by (unfold wf_def, blast) + +text{*Restriction to domain @{term A}. + If @{term r} is well-founded over @{term A} then @{term "wf r"}*} +lemma wfI: + "[| r <= A <*> A; + !!x P. [| ALL x. (ALL y. (y,x) : r --> P y) --> P x; x:A |] ==> P x |] + ==> wf r" +by (unfold wf_def, blast) + +lemma wf_induct: + "[| wf(r); + !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x) + |] ==> P(a)" +by (unfold wf_def, blast) + +lemma wf_not_sym [rule_format]: "wf(r) ==> ALL x. (a,x):r --> (x,a)~:r" +by (erule_tac a=a in wf_induct, blast) + +(* [| wf r; ~Z ==> (a,x) : r; (x,a) ~: r ==> Z |] ==> Z *) +lemmas wf_asym = wf_not_sym [elim_format] + +lemma wf_not_refl [simp]: "wf(r) ==> (a,a) ~: r" +by (blast elim: wf_asym) + +(* [| wf r; (a,a) ~: r ==> PROP W |] ==> PROP W *) +lemmas wf_irrefl = wf_not_refl [elim_format] + +text{*transitive closure of a well-founded relation is well-founded! *} +lemma wf_trancl: "wf(r) ==> wf(r^+)" +apply (subst wf_def, clarify) +apply (rule allE, assumption) + --{*Retains the universal formula for later use!*} +apply (erule mp) +apply (erule_tac a = x in wf_induct) +apply (blast elim: tranclE) +done + +lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)" +apply (subst trancl_converse [symmetric]) +apply (erule wf_trancl) +done + + +subsubsection{*Minimal-element characterization of well-foundedness*} + +lemma lemma1: "wf r ==> x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)" +apply (unfold wf_def) +apply (drule spec) +apply (erule mp [THEN spec], blast) +done + +lemma lemma2: "(ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)) ==> wf r" +apply (unfold wf_def, clarify) +apply (drule_tac x = "{x. ~ P x}" in spec, blast) +done + +lemma wf_eq_minimal: "wf r = (ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q))" +by (blast intro!: lemma1 lemma2) + +subsubsection{*Other simple well-foundedness results*} + + +text{*Well-foundedness of subsets*} +lemma wf_subset: "[| wf(r); p<=r |] ==> wf(p)" +apply (simp (no_asm_use) add: wf_eq_minimal) +apply fast +done + +text{*Well-foundedness of the empty relation*} +lemma wf_empty [iff]: "wf({})" +by (simp add: wf_def) + +text{*Well-foundedness of insert*} +lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)" +apply (rule iffI) + apply (blast elim: wf_trancl [THEN wf_irrefl] + intro: rtrancl_into_trancl1 wf_subset + rtrancl_mono [THEN [2] rev_subsetD]) +apply (simp add: wf_eq_minimal, safe) +apply (rule allE, assumption, erule impE, blast) +apply (erule bexE) +apply (rename_tac "a", case_tac "a = x") + prefer 2 +apply blast +apply (case_tac "y:Q") + prefer 2 apply blast +apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE) + apply assumption +apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) + --{*essential for speed*} +txt{*Blast_tac with new substOccur fails*} +apply (fast intro: converse_rtrancl_into_rtrancl) +done + +text{*Well-foundedness of image*} +lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)" +apply (simp only: wf_eq_minimal, clarify) +apply (case_tac "EX p. f p : Q") +apply (erule_tac x = "{p. f p : Q}" in allE) +apply (fast dest: inj_onD, blast) +done + + +subsubsection{*Well-Foundedness Results for Unions*} + +text{*Well-foundedness of indexed union with disjoint domains and ranges*} + +lemma wf_UN: "[| ALL i:I. wf(r i); + ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {} + |] ==> wf(UN i:I. r i)" +apply (simp only: wf_eq_minimal, clarify) +apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i") + prefer 2 + apply force +apply clarify +apply (drule bspec, assumption) +apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE) +apply (blast elim!: allE) +done + +lemma wf_Union: + "[| ALL r:R. wf r; + ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {} + |] ==> wf(Union R)" +apply (simp add: Union_def) +apply (blast intro: wf_UN) +done + +(*Intuition: we find an (R u S)-min element of a nonempty subset A + by case distinction. + 1. There is a step a -R-> b with a,b : A. + Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}. + By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the + subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot + have an S-successor and is thus S-min in A as well. + 2. There is no such step. + Pick an S-min element of A. In this case it must be an R-min + element of A as well. + +*) +lemma wf_Un: + "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)" +apply (simp only: wf_eq_minimal, clarify) +apply (rename_tac A a) +apply (case_tac "EX a:A. EX b:A. (b,a) : r") + prefer 2 + apply simp + apply (drule_tac x=A in spec)+ + apply blast +apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r) }" in allE)+ +apply (blast elim!: allE) +done + +subsubsection {*acyclic*} + +lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r" +by (simp add: acyclic_def) + +lemma wf_acyclic: "wf r ==> acyclic r" +apply (simp add: acyclic_def) +apply (blast elim: wf_trancl [THEN wf_irrefl]) +done + +lemma acyclic_insert [iff]: + "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)" +apply (simp add: acyclic_def trancl_insert) +apply (blast intro: rtrancl_trans) +done + +lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r" +by (simp add: acyclic_def trancl_converse) + +lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)" +apply (simp add: acyclic_def antisym_def) +apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl) +done + +(* Other direction: +acyclic = no loops +antisym = only self loops +Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id) +==> antisym( r^* ) = acyclic(r - Id)"; +*) + +lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r" +apply (simp add: acyclic_def) +apply (blast intro: trancl_mono) +done + + +subsection{*Well-Founded Recursion*} + +text{*cut*} + +lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))" +by (simp add: expand_fun_eq cut_def) + +lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)" +by (simp add: cut_def) + +text{*Inductive characterization of wfrec combinator; for details see: +John Harrison, "Inductive definitions: automation and application"*} + +lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. (x, y) : wfrec_rel R F" +apply (simp add: adm_wf_def) +apply (erule_tac a=x in wf_induct) +apply (rule ex1I) +apply (rule_tac g = "%x. THE y. (x, y) : wfrec_rel R F" in wfrec_rel.wfrecI) +apply (fast dest!: theI') +apply (erule wfrec_rel.cases, simp) +apply (erule allE, erule allE, erule allE, erule mp) +apply (fast intro: the_equality [symmetric]) +done + +lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)" +apply (simp add: adm_wf_def) +apply (intro strip) +apply (rule cuts_eq [THEN iffD2, THEN subst], assumption) +apply (rule refl) +done + +lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a" +apply (simp add: wfrec_def) +apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption) +apply (rule wfrec_rel.wfrecI) +apply (intro strip) +apply (erule adm_lemma [THEN wfrec_unique, THEN theI']) +done + + +text{** This form avoids giant explosions in proofs. NOTE USE OF ==*} +lemma def_wfrec: "[| f==wfrec r H; wf(r) |] ==> f(a) = H (cut f r a) a" +apply auto +apply (blast intro: wfrec) +done + + +subsection{*Variants for TFL: the Recdef Package*} + +lemma tfl_wf_induct: "ALL R. wf R --> + (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))" +apply clarify +apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast) +done + +lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)" +apply clarify +apply (rule cut_apply, assumption) +done + +lemma tfl_wfrec: + "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)" +apply clarify +apply (erule wfrec) +done + +subsection {*LEAST and wellorderings*} + +text{* See also @{text wf_linord_ex_has_least} and its consequences in + @{text Wellfounded_Relations.ML}*} + +lemma wellorder_Least_lemma [rule_format]: + "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k" +apply (rule_tac a = k in wf [THEN wf_induct]) +apply (rule impI) +apply (rule classical) +apply (rule_tac s = x in Least_equality [THEN ssubst], auto) +apply (auto simp add: linorder_not_less [symmetric]) +done + +lemmas LeastI = wellorder_Least_lemma [THEN conjunct1, standard] +lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard] + +lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)" +apply (simp (no_asm_use) add: linorder_not_le [symmetric]) +apply (erule contrapos_nn) +apply (erule Least_le) +done + +ML +{* +val wf_def = thm "wf_def"; +val wfUNIVI = thm "wfUNIVI"; +val wfI = thm "wfI"; +val wf_induct = thm "wf_induct"; +val wf_not_sym = thm "wf_not_sym"; +val wf_asym = thm "wf_asym"; +val wf_not_refl = thm "wf_not_refl"; +val wf_irrefl = thm "wf_irrefl"; +val wf_trancl = thm "wf_trancl"; +val wf_converse_trancl = thm "wf_converse_trancl"; +val wf_eq_minimal = thm "wf_eq_minimal"; +val wf_subset = thm "wf_subset"; +val wf_empty = thm "wf_empty"; +val wf_insert = thm "wf_insert"; +val wf_UN = thm "wf_UN"; +val wf_Union = thm "wf_Union"; +val wf_Un = thm "wf_Un"; +val wf_prod_fun_image = thm "wf_prod_fun_image"; +val acyclicI = thm "acyclicI"; +val wf_acyclic = thm "wf_acyclic"; +val acyclic_insert = thm "acyclic_insert"; +val acyclic_converse = thm "acyclic_converse"; +val acyclic_impl_antisym_rtrancl = thm "acyclic_impl_antisym_rtrancl"; +val acyclic_subset = thm "acyclic_subset"; +val cuts_eq = thm "cuts_eq"; +val cut_apply = thm "cut_apply"; +val wfrec_unique = thm "wfrec_unique"; +val wfrec = thm "wfrec"; +val def_wfrec = thm "def_wfrec"; +val tfl_wf_induct = thm "tfl_wf_induct"; +val tfl_cut_apply = thm "tfl_cut_apply"; +val tfl_wfrec = thm "tfl_wfrec"; +val LeastI = thm "LeastI"; +val Least_le = thm "Least_le"; +val not_less_Least = thm "not_less_Least"; +*} end