wenzelm@32960: (* Title: HOL/Wellfounded.thy wenzelm@32960: Author: Tobias Nipkow wenzelm@32960: Author: Lawrence C Paulson wenzelm@32960: Author: Konrad Slind wenzelm@32960: Author: Alexander Krauss krauss@26748: *) krauss@26748: krauss@26748: header {*Well-founded Recursion*} krauss@26748: krauss@26748: theory Wellfounded haftmann@35727: imports Transitive_Closure haftmann@31775: uses ("Tools/Function/size.ML") krauss@26748: begin krauss@26748: krauss@26976: subsection {* Basic Definitions *} krauss@26976: krauss@33217: definition wf :: "('a * 'a) set => bool" where haftmann@45137: "wf r \ (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))" krauss@26748: krauss@33217: definition wfP :: "('a => 'a => bool) => bool" where haftmann@45137: "wfP r \ wf {(x, y). r x y}" krauss@26748: krauss@26748: lemma wfP_wf_eq [pred_set_conv]: "wfP (\x y. (x, y) \ r) = wf r" krauss@26748: by (simp add: wfP_def) krauss@26748: krauss@26748: lemma wfUNIVI: krauss@26748: "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)" krauss@26748: unfolding wf_def by blast krauss@26748: krauss@26748: lemmas wfPUNIVI = wfUNIVI [to_pred] krauss@26748: krauss@26748: text{*Restriction to domain @{term A} and range @{term B}. If @{term r} is krauss@26748: well-founded over their intersection, then @{term "wf r"}*} krauss@26748: lemma wfI: krauss@26748: "[| r \ A <*> B; krauss@26748: !!x P. [|\x. (\y. (y,x) : r --> P y) --> P x; x : A; x : B |] ==> P x |] krauss@26748: ==> wf r" krauss@26748: unfolding wf_def by blast krauss@26748: krauss@26748: lemma wf_induct: krauss@26748: "[| wf(r); krauss@26748: !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x) krauss@26748: |] ==> P(a)" krauss@26748: unfolding wf_def by blast krauss@26748: krauss@26748: lemmas wfP_induct = wf_induct [to_pred] krauss@26748: krauss@26748: lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf] krauss@26748: krauss@26748: lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP] krauss@26748: krauss@26748: lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r" krauss@26748: by (induct a arbitrary: x set: wf) blast krauss@26748: krauss@33215: lemma wf_asym: krauss@33215: assumes "wf r" "(a, x) \ r" krauss@33215: obtains "(x, a) \ r" krauss@33215: by (drule wf_not_sym[OF assms]) krauss@26748: krauss@26748: lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r" krauss@26748: by (blast elim: wf_asym) krauss@26748: krauss@33215: lemma wf_irrefl: assumes "wf r" obtains "(a, a) \ r" krauss@33215: by (drule wf_not_refl[OF assms]) krauss@26748: haftmann@27823: lemma wf_wellorderI: haftmann@27823: assumes wf: "wf {(x::'a::ord, y). x < y}" haftmann@27823: assumes lin: "OFCLASS('a::ord, linorder_class)" haftmann@27823: shows "OFCLASS('a::ord, wellorder_class)" haftmann@27823: using lin by (rule wellorder_class.intro) haftmann@36635: (blast intro: class.wellorder_axioms.intro wf_induct_rule [OF wf]) haftmann@27823: haftmann@27823: lemma (in wellorder) wf: haftmann@27823: "wf {(x, y). x < y}" haftmann@27823: unfolding wf_def by (blast intro: less_induct) haftmann@27823: haftmann@27823: krauss@26976: subsection {* Basic Results *} krauss@26976: krauss@33216: text {* Point-free characterization of well-foundedness *} krauss@33216: krauss@33216: lemma wfE_pf: krauss@33216: assumes wf: "wf R" krauss@33216: assumes a: "A \ R `` A" krauss@33216: shows "A = {}" krauss@33216: proof - krauss@33216: { fix x krauss@33216: from wf have "x \ A" krauss@33216: proof induct krauss@33216: fix x assume "\y. (y, x) \ R \ y \ A" krauss@33216: then have "x \ R `` A" by blast krauss@33216: with a show "x \ A" by blast krauss@33216: qed krauss@33216: } thus ?thesis by auto krauss@33216: qed krauss@33216: krauss@33216: lemma wfI_pf: krauss@33216: assumes a: "\A. A \ R `` A \ A = {}" krauss@33216: shows "wf R" krauss@33216: proof (rule wfUNIVI) krauss@33216: fix P :: "'a \ bool" and x krauss@33216: let ?A = "{x. \ P x}" krauss@33216: assume "\x. (\y. (y, x) \ R \ P y) \ P x" krauss@33216: then have "?A \ R `` ?A" by blast krauss@33216: with a show "P x" by blast krauss@33216: qed krauss@33216: krauss@33216: text{*Minimal-element characterization of well-foundedness*} krauss@33216: krauss@33216: lemma wfE_min: krauss@33216: assumes wf: "wf R" and Q: "x \ Q" krauss@33216: obtains z where "z \ Q" "\y. (y, z) \ R \ y \ Q" krauss@33216: using Q wfE_pf[OF wf, of Q] by blast krauss@33216: krauss@33216: lemma wfI_min: krauss@33216: assumes a: "\x Q. x \ Q \ \z\Q. \y. (y, z) \ R \ y \ Q" krauss@33216: shows "wf R" krauss@33216: proof (rule wfI_pf) krauss@33216: fix A assume b: "A \ R `` A" krauss@33216: { fix x assume "x \ A" krauss@33216: from a[OF this] b have "False" by blast krauss@33216: } krauss@33216: thus "A = {}" by blast krauss@33216: qed krauss@33216: krauss@33216: lemma wf_eq_minimal: "wf r = (\Q x. x\Q --> (\z\Q. \y. (y,z)\r --> y\Q))" krauss@33216: apply auto krauss@33216: apply (erule wfE_min, assumption, blast) krauss@33216: apply (rule wfI_min, auto) krauss@33216: done krauss@33216: krauss@33216: lemmas wfP_eq_minimal = wf_eq_minimal [to_pred] krauss@33216: krauss@33216: text{* Well-foundedness of transitive closure *} krauss@33216: krauss@26748: lemma wf_trancl: krauss@26748: assumes "wf r" krauss@26748: shows "wf (r^+)" krauss@26748: proof - krauss@26748: { krauss@26748: fix P and x krauss@26748: assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x" krauss@26748: have "P x" krauss@26748: proof (rule induct_step) krauss@26748: fix y assume "(y, x) : r^+" krauss@26748: with `wf r` show "P y" krauss@26748: proof (induct x arbitrary: y) wenzelm@32960: case (less x) wenzelm@32960: note hyp = `\x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'` wenzelm@32960: from `(y, x) : r^+` show "P y" wenzelm@32960: proof cases wenzelm@32960: case base wenzelm@32960: show "P y" wenzelm@32960: proof (rule induct_step) wenzelm@32960: fix y' assume "(y', y) : r^+" wenzelm@32960: with `(y, x) : r` show "P y'" by (rule hyp [of y y']) wenzelm@32960: qed wenzelm@32960: next wenzelm@32960: case step wenzelm@32960: then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp wenzelm@32960: then show "P y" by (rule hyp [of x' y]) wenzelm@32960: qed krauss@26748: qed krauss@26748: qed krauss@26748: } then show ?thesis unfolding wf_def by blast krauss@26748: qed krauss@26748: krauss@26748: lemmas wfP_trancl = wf_trancl [to_pred] krauss@26748: krauss@26748: lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)" krauss@26748: apply (subst trancl_converse [symmetric]) krauss@26748: apply (erule wf_trancl) krauss@26748: done krauss@26748: krauss@33216: text {* Well-foundedness of subsets *} krauss@26748: krauss@26748: lemma wf_subset: "[| wf(r); p<=r |] ==> wf(p)" krauss@26748: apply (simp (no_asm_use) add: wf_eq_minimal) krauss@26748: apply fast krauss@26748: done krauss@26748: krauss@26748: lemmas wfP_subset = wf_subset [to_pred] krauss@26748: krauss@26748: text {* Well-foundedness of the empty relation *} krauss@33216: krauss@33216: lemma wf_empty [iff]: "wf {}" krauss@26748: by (simp add: wf_def) krauss@26748: haftmann@32205: lemma wfP_empty [iff]: haftmann@32205: "wfP (\x y. False)" haftmann@32205: proof - haftmann@32205: have "wfP bot" by (fact wf_empty [to_pred bot_empty_eq2]) huffman@44921: then show ?thesis by (simp add: bot_fun_def) haftmann@32205: qed krauss@26748: krauss@26748: lemma wf_Int1: "wf r ==> wf (r Int r')" krauss@26748: apply (erule wf_subset) krauss@26748: apply (rule Int_lower1) krauss@26748: done krauss@26748: krauss@26748: lemma wf_Int2: "wf r ==> wf (r' Int r)" krauss@26748: apply (erule wf_subset) krauss@26748: apply (rule Int_lower2) krauss@26748: done krauss@26748: krauss@33216: text {* Exponentiation *} krauss@33216: krauss@33216: lemma wf_exp: krauss@33216: assumes "wf (R ^^ n)" krauss@33216: shows "wf R" krauss@33216: proof (rule wfI_pf) krauss@33216: fix A assume "A \ R `` A" krauss@33216: then have "A \ (R ^^ n) `` A" by (induct n) force+ krauss@33216: with `wf (R ^^ n)` krauss@33216: show "A = {}" by (rule wfE_pf) krauss@33216: qed krauss@33216: krauss@33216: text {* Well-foundedness of insert *} krauss@33216: krauss@26748: lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)" krauss@26748: apply (rule iffI) krauss@26748: apply (blast elim: wf_trancl [THEN wf_irrefl] krauss@26748: intro: rtrancl_into_trancl1 wf_subset krauss@26748: rtrancl_mono [THEN [2] rev_subsetD]) krauss@26748: apply (simp add: wf_eq_minimal, safe) krauss@26748: apply (rule allE, assumption, erule impE, blast) krauss@26748: apply (erule bexE) krauss@26748: apply (rename_tac "a", case_tac "a = x") krauss@26748: prefer 2 krauss@26748: apply blast krauss@26748: apply (case_tac "y:Q") krauss@26748: prefer 2 apply blast krauss@26748: apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE) krauss@26748: apply assumption krauss@26748: apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) krauss@26748: --{*essential for speed*} krauss@26748: txt{*Blast with new substOccur fails*} krauss@26748: apply (fast intro: converse_rtrancl_into_rtrancl) krauss@26748: done krauss@26748: krauss@26748: text{*Well-foundedness of image*} krauss@33216: haftmann@40607: lemma wf_map_pair_image: "[| wf r; inj f |] ==> wf(map_pair f f ` r)" krauss@26748: apply (simp only: wf_eq_minimal, clarify) krauss@26748: apply (case_tac "EX p. f p : Q") krauss@26748: apply (erule_tac x = "{p. f p : Q}" in allE) krauss@26748: apply (fast dest: inj_onD, blast) krauss@26748: done krauss@26748: krauss@26748: krauss@26976: subsection {* Well-Foundedness Results for Unions *} krauss@26748: krauss@26748: lemma wf_union_compatible: krauss@26748: assumes "wf R" "wf S" krauss@32235: assumes "R O S \ R" krauss@26748: shows "wf (R \ S)" krauss@26748: proof (rule wfI_min) krauss@26748: fix x :: 'a and Q krauss@26748: let ?Q' = "{x \ Q. \y. (y, x) \ R \ y \ Q}" krauss@26748: assume "x \ Q" krauss@26748: obtain a where "a \ ?Q'" krauss@26748: by (rule wfE_min [OF `wf R` `x \ Q`]) blast krauss@26748: with `wf S` krauss@26748: obtain z where "z \ ?Q'" and zmin: "\y. (y, z) \ S \ y \ ?Q'" by (erule wfE_min) krauss@26748: { krauss@26748: fix y assume "(y, z) \ S" krauss@26748: then have "y \ ?Q'" by (rule zmin) krauss@26748: krauss@26748: have "y \ Q" krauss@26748: proof krauss@26748: assume "y \ Q" krauss@26748: with `y \ ?Q'` krauss@26748: obtain w where "(w, y) \ R" and "w \ Q" by auto krauss@32235: from `(w, y) \ R` `(y, z) \ S` have "(w, z) \ R O S" by (rule rel_compI) krauss@32235: with `R O S \ R` have "(w, z) \ R" .. krauss@26748: with `z \ ?Q'` have "w \ Q" by blast krauss@26748: with `w \ Q` show False by contradiction krauss@26748: qed krauss@26748: } krauss@26748: with `z \ ?Q'` show "\z\Q. \y. (y, z) \ R \ S \ y \ Q" by blast krauss@26748: qed krauss@26748: krauss@26748: krauss@26748: text {* Well-foundedness of indexed union with disjoint domains and ranges *} krauss@26748: krauss@26748: lemma wf_UN: "[| ALL i:I. wf(r i); krauss@26748: ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {} krauss@26748: |] ==> wf(UN i:I. r i)" krauss@26748: apply (simp only: wf_eq_minimal, clarify) krauss@26748: apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i") krauss@26748: prefer 2 krauss@26748: apply force krauss@26748: apply clarify krauss@26748: apply (drule bspec, assumption) krauss@26748: apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE) krauss@26748: apply (blast elim!: allE) krauss@26748: done krauss@26748: haftmann@32263: lemma wfP_SUP: haftmann@32263: "\i. wfP (r i) \ \i j. r i \ r j \ inf (DomainP (r i)) (RangeP (r j)) = bot \ wfP (SUPR UNIV r)" berghofe@46177: apply (rule wf_UN [where I=UNIV and r="\i. {(x, y). r i x y}", to_pred]) haftmann@45970: apply (simp_all add: inf_set_def) haftmann@45970: apply auto haftmann@45970: done krauss@26748: krauss@26748: lemma wf_Union: krauss@26748: "[| ALL r:R. wf r; krauss@26748: ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {} krauss@26748: |] ==> wf(Union R)" hoelzl@44937: using wf_UN[of R "\i. i"] by (simp add: SUP_def) krauss@26748: krauss@26748: (*Intuition: we find an (R u S)-min element of a nonempty subset A krauss@26748: by case distinction. krauss@26748: 1. There is a step a -R-> b with a,b : A. krauss@26748: Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}. krauss@26748: By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the krauss@26748: subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot krauss@26748: have an S-successor and is thus S-min in A as well. krauss@26748: 2. There is no such step. krauss@26748: Pick an S-min element of A. In this case it must be an R-min krauss@26748: element of A as well. krauss@26748: krauss@26748: *) krauss@26748: lemma wf_Un: krauss@26748: "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)" krauss@26748: using wf_union_compatible[of s r] krauss@26748: by (auto simp: Un_ac) krauss@26748: krauss@26748: lemma wf_union_merge: krauss@32235: "wf (R \ S) = wf (R O R \ S O R \ S)" (is "wf ?A = wf ?B") krauss@26748: proof krauss@26748: assume "wf ?A" krauss@26748: with wf_trancl have wfT: "wf (?A^+)" . krauss@26748: moreover have "?B \ ?A^+" krauss@26748: by (subst trancl_unfold, subst trancl_unfold) blast krauss@26748: ultimately show "wf ?B" by (rule wf_subset) krauss@26748: next krauss@26748: assume "wf ?B" krauss@26748: krauss@26748: show "wf ?A" krauss@26748: proof (rule wfI_min) krauss@26748: fix Q :: "'a set" and x krauss@26748: assume "x \ Q" krauss@26748: krauss@26748: with `wf ?B` krauss@26748: obtain z where "z \ Q" and "\y. (y, z) \ ?B \ y \ Q" krauss@26748: by (erule wfE_min) krauss@26748: then have A1: "\y. (y, z) \ R O R \ y \ Q" krauss@32235: and A2: "\y. (y, z) \ S O R \ y \ Q" krauss@26748: and A3: "\y. (y, z) \ S \ y \ Q" krauss@26748: by auto krauss@26748: krauss@26748: show "\z\Q. \y. (y, z) \ ?A \ y \ Q" krauss@26748: proof (cases "\y. (y, z) \ R \ y \ Q") krauss@26748: case True krauss@26748: with `z \ Q` A3 show ?thesis by blast krauss@26748: next krauss@26748: case False krauss@26748: then obtain z' where "z'\Q" "(z', z) \ R" by blast krauss@26748: krauss@26748: have "\y. (y, z') \ ?A \ y \ Q" krauss@26748: proof (intro allI impI) krauss@26748: fix y assume "(y, z') \ ?A" krauss@26748: then show "y \ Q" krauss@26748: proof krauss@26748: assume "(y, z') \ R" krauss@26748: then have "(y, z) \ R O R" using `(z', z) \ R` .. krauss@26748: with A1 show "y \ Q" . krauss@26748: next krauss@26748: assume "(y, z') \ S" krauss@32235: then have "(y, z) \ S O R" using `(z', z) \ R` .. krauss@26748: with A2 show "y \ Q" . krauss@26748: qed krauss@26748: qed krauss@26748: with `z' \ Q` show ?thesis .. krauss@26748: qed krauss@26748: qed krauss@26748: qed krauss@26748: krauss@26748: lemma wf_comp_self: "wf R = wf (R O R)" -- {* special case *} krauss@26748: by (rule wf_union_merge [where S = "{}", simplified]) krauss@26748: krauss@26748: krauss@33217: subsection {* Acyclic relations *} krauss@33217: krauss@26748: lemma wf_acyclic: "wf r ==> acyclic r" krauss@26748: apply (simp add: acyclic_def) krauss@26748: apply (blast elim: wf_trancl [THEN wf_irrefl]) krauss@26748: done krauss@26748: krauss@26748: lemmas wfP_acyclicP = wf_acyclic [to_pred] krauss@26748: krauss@26748: text{* Wellfoundedness of finite acyclic relations*} krauss@26748: krauss@26748: lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r" krauss@26748: apply (erule finite_induct, blast) krauss@26748: apply (simp (no_asm_simp) only: split_tupled_all) krauss@26748: apply simp krauss@26748: done krauss@26748: krauss@26748: lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)" krauss@26748: apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf]) krauss@26748: apply (erule acyclic_converse [THEN iffD2]) krauss@26748: done krauss@26748: krauss@26748: lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r" krauss@26748: by (blast intro: finite_acyclic_wf wf_acyclic) krauss@26748: krauss@26748: krauss@26748: subsection {* @{typ nat} is well-founded *} krauss@26748: krauss@26748: lemma less_nat_rel: "op < = (\m n. n = Suc m)^++" krauss@26748: proof (rule ext, rule ext, rule iffI) krauss@26748: fix n m :: nat krauss@26748: assume "m < n" krauss@26748: then show "(\m n. n = Suc m)^++ m n" krauss@26748: proof (induct n) krauss@26748: case 0 then show ?case by auto krauss@26748: next krauss@26748: case (Suc n) then show ?case krauss@26748: by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl) krauss@26748: qed krauss@26748: next krauss@26748: fix n m :: nat krauss@26748: assume "(\m n. n = Suc m)^++ m n" krauss@26748: then show "m < n" krauss@26748: by (induct n) krauss@26748: (simp_all add: less_Suc_eq_le reflexive le_less) krauss@26748: qed krauss@26748: krauss@26748: definition krauss@26748: pred_nat :: "(nat * nat) set" where krauss@26748: "pred_nat = {(m, n). n = Suc m}" krauss@26748: krauss@26748: definition krauss@26748: less_than :: "(nat * nat) set" where krauss@26748: "less_than = pred_nat^+" krauss@26748: krauss@26748: lemma less_eq: "(m, n) \ pred_nat^+ \ m < n" krauss@26748: unfolding less_nat_rel pred_nat_def trancl_def by simp krauss@26748: krauss@26748: lemma pred_nat_trancl_eq_le: krauss@26748: "(m, n) \ pred_nat^* \ m \ n" krauss@26748: unfolding less_eq rtrancl_eq_or_trancl by auto krauss@26748: krauss@26748: lemma wf_pred_nat: "wf pred_nat" krauss@26748: apply (unfold wf_def pred_nat_def, clarify) krauss@26748: apply (induct_tac x, blast+) krauss@26748: done krauss@26748: krauss@26748: lemma wf_less_than [iff]: "wf less_than" krauss@26748: by (simp add: less_than_def wf_pred_nat [THEN wf_trancl]) krauss@26748: krauss@26748: lemma trans_less_than [iff]: "trans less_than" huffman@35216: by (simp add: less_than_def) krauss@26748: krauss@26748: lemma less_than_iff [iff]: "((x,y): less_than) = (x 'a set" krauss@26748: for r :: "('a * 'a) set" krauss@26748: where krauss@26748: accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r" krauss@26748: krauss@26748: abbreviation krauss@26748: termip :: "('a => 'a => bool) => 'a => bool" where haftmann@45137: "termip r \ accp (r\\)" krauss@26748: krauss@26748: abbreviation krauss@26748: termi :: "('a * 'a) set => 'a set" where haftmann@45137: "termi r \ acc (r\)" krauss@26748: krauss@26748: lemmas accpI = accp.accI krauss@26748: krauss@26748: text {* Induction rules *} krauss@26748: krauss@26748: theorem accp_induct: krauss@26748: assumes major: "accp r a" krauss@26748: assumes hyp: "!!x. accp r x ==> \y. r y x --> P y ==> P x" krauss@26748: shows "P a" krauss@26748: apply (rule major [THEN accp.induct]) krauss@26748: apply (rule hyp) krauss@26748: apply (rule accp.accI) krauss@26748: apply fast krauss@26748: apply fast krauss@26748: done krauss@26748: krauss@26748: theorems accp_induct_rule = accp_induct [rule_format, induct set: accp] krauss@26748: krauss@26748: theorem accp_downward: "accp r b ==> r a b ==> accp r a" krauss@26748: apply (erule accp.cases) krauss@26748: apply fast krauss@26748: done krauss@26748: krauss@26748: lemma not_accp_down: krauss@26748: assumes na: "\ accp R x" krauss@26748: obtains z where "R z x" and "\ accp R z" krauss@26748: proof - krauss@26748: assume a: "\z. \R z x; \ accp R z\ \ thesis" krauss@26748: krauss@26748: show thesis krauss@26748: proof (cases "\z. R z x \ accp R z") krauss@26748: case True krauss@26748: hence "\z. R z x \ accp R z" by auto krauss@26748: hence "accp R x" krauss@26748: by (rule accp.accI) krauss@26748: with na show thesis .. krauss@26748: next krauss@26748: case False then obtain z where "R z x" and "\ accp R z" krauss@26748: by auto krauss@26748: with a show thesis . krauss@26748: qed krauss@26748: qed krauss@26748: krauss@26748: lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b" krauss@26748: apply (erule rtranclp_induct) krauss@26748: apply blast krauss@26748: apply (blast dest: accp_downward) krauss@26748: done krauss@26748: krauss@26748: theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b" krauss@26748: apply (blast dest: accp_downwards_aux) krauss@26748: done krauss@26748: krauss@26748: theorem accp_wfPI: "\x. accp r x ==> wfP r" krauss@26748: apply (rule wfPUNIVI) huffman@44921: apply (rule_tac P=P in accp_induct) krauss@26748: apply blast krauss@26748: apply blast krauss@26748: done krauss@26748: krauss@26748: theorem accp_wfPD: "wfP r ==> accp r x" krauss@26748: apply (erule wfP_induct_rule) krauss@26748: apply (rule accp.accI) krauss@26748: apply blast krauss@26748: done krauss@26748: krauss@26748: theorem wfP_accp_iff: "wfP r = (\x. accp r x)" krauss@26748: apply (blast intro: accp_wfPI dest: accp_wfPD) krauss@26748: done krauss@26748: krauss@26748: krauss@26748: text {* Smaller relations have bigger accessible parts: *} krauss@26748: krauss@26748: lemma accp_subset: krauss@26748: assumes sub: "R1 \ R2" krauss@26748: shows "accp R2 \ accp R1" berghofe@26803: proof (rule predicate1I) krauss@26748: fix x assume "accp R2 x" krauss@26748: then show "accp R1 x" krauss@26748: proof (induct x) krauss@26748: fix x krauss@26748: assume ih: "\y. R2 y x \ accp R1 y" krauss@26748: with sub show "accp R1 x" krauss@26748: by (blast intro: accp.accI) krauss@26748: qed krauss@26748: qed krauss@26748: krauss@26748: krauss@26748: text {* This is a generalized induction theorem that works on krauss@26748: subsets of the accessible part. *} krauss@26748: krauss@26748: lemma accp_subset_induct: krauss@26748: assumes subset: "D \ accp R" krauss@26748: and dcl: "\x z. \D x; R z x\ \ D z" krauss@26748: and "D x" krauss@26748: and istep: "\x. \D x; (\z. R z x \ P z)\ \ P x" krauss@26748: shows "P x" krauss@26748: proof - krauss@26748: from subset and `D x` krauss@26748: have "accp R x" .. krauss@26748: then show "P x" using `D x` krauss@26748: proof (induct x) krauss@26748: fix x krauss@26748: assume "D x" krauss@26748: and "\y. R y x \ D y \ P y" krauss@26748: with dcl and istep show "P x" by blast krauss@26748: qed krauss@26748: qed krauss@26748: krauss@26748: krauss@26748: text {* Set versions of the above theorems *} krauss@26748: krauss@26748: lemmas acc_induct = accp_induct [to_set] krauss@26748: krauss@26748: lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc] krauss@26748: krauss@26748: lemmas acc_downward = accp_downward [to_set] krauss@26748: krauss@26748: lemmas not_acc_down = not_accp_down [to_set] krauss@26748: krauss@26748: lemmas acc_downwards_aux = accp_downwards_aux [to_set] krauss@26748: krauss@26748: lemmas acc_downwards = accp_downwards [to_set] krauss@26748: krauss@26748: lemmas acc_wfI = accp_wfPI [to_set] krauss@26748: krauss@26748: lemmas acc_wfD = accp_wfPD [to_set] krauss@26748: krauss@26748: lemmas wf_acc_iff = wfP_accp_iff [to_set] krauss@26748: berghofe@46177: lemmas acc_subset = accp_subset [to_set] krauss@26748: berghofe@46177: lemmas acc_subset_induct = accp_subset_induct [to_set] krauss@26748: krauss@26748: krauss@26748: subsection {* Tools for building wellfounded relations *} krauss@26748: krauss@26748: text {* Inverse Image *} krauss@26748: krauss@26748: lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))" krauss@26748: apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal) krauss@26748: apply clarify krauss@26748: apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }") krauss@26748: prefer 2 apply (blast del: allE) krauss@26748: apply (erule allE) krauss@26748: apply (erule (1) notE impE) krauss@26748: apply blast krauss@26748: done krauss@26748: krauss@36664: text {* Measure functions into @{typ nat} *} krauss@26748: krauss@26748: definition measure :: "('a => nat) => ('a * 'a)set" haftmann@45137: where "measure = inv_image less_than" krauss@26748: bulwahn@46356: lemma in_measure[simp, code_unfold]: "((x,y) : measure f) = (f x < f y)" krauss@26748: by (simp add:measure_def) krauss@26748: krauss@26748: lemma wf_measure [iff]: "wf (measure f)" krauss@26748: apply (unfold measure_def) krauss@26748: apply (rule wf_less_than [THEN wf_inv_image]) krauss@26748: done krauss@26748: nipkow@41720: lemma wf_if_measure: fixes f :: "'a \ nat" nipkow@41720: shows "(!!x. P x \ f(g x) < f x) \ wf {(y,x). P x \ y = g x}" nipkow@41720: apply(insert wf_measure[of f]) nipkow@41720: apply(simp only: measure_def inv_image_def less_than_def less_eq) nipkow@41720: apply(erule wf_subset) nipkow@41720: apply auto nipkow@41720: done nipkow@41720: nipkow@41720: krauss@26748: text{* Lexicographic combinations *} krauss@26748: haftmann@37767: definition lex_prod :: "('a \'a) set \ ('b \ 'b) set \ (('a \ 'b) \ ('a \ 'b)) set" (infixr "<*lex*>" 80) where haftmann@37767: "ra <*lex*> rb = {((a, b), (a', b')). (a, a') \ ra \ a = a' \ (b, b') \ rb}" krauss@26748: krauss@26748: lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)" krauss@26748: apply (unfold wf_def lex_prod_def) krauss@26748: apply (rule allI, rule impI) krauss@26748: apply (simp (no_asm_use) only: split_paired_All) krauss@26748: apply (drule spec, erule mp) krauss@26748: apply (rule allI, rule impI) krauss@26748: apply (drule spec, erule mp, blast) krauss@26748: done krauss@26748: krauss@26748: lemma in_lex_prod[simp]: krauss@26748: "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \ (a = a' \ (b, b') : s))" krauss@26748: by (auto simp:lex_prod_def) krauss@26748: krauss@26748: text{* @{term "op <*lex*>"} preserves transitivity *} krauss@26748: krauss@26748: lemma trans_lex_prod [intro!]: krauss@26748: "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)" krauss@26748: by (unfold trans_def lex_prod_def, blast) krauss@26748: krauss@36664: text {* lexicographic combinations with measure functions *} krauss@26748: krauss@26748: definition krauss@26748: mlex_prod :: "('a \ nat) \ ('a \ 'a) set \ ('a \ 'a) set" (infixr "<*mlex*>" 80) krauss@26748: where krauss@26748: "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))" krauss@26748: krauss@26748: lemma wf_mlex: "wf R \ wf (f <*mlex*> R)" krauss@26748: unfolding mlex_prod_def krauss@26748: by auto krauss@26748: krauss@26748: lemma mlex_less: "f x < f y \ (x, y) \ f <*mlex*> R" krauss@26748: unfolding mlex_prod_def by simp krauss@26748: krauss@26748: lemma mlex_leq: "f x \ f y \ (x, y) \ R \ (x, y) \ f <*mlex*> R" krauss@26748: unfolding mlex_prod_def by auto krauss@26748: krauss@26748: text {* proper subset relation on finite sets *} krauss@26748: krauss@26748: definition finite_psubset :: "('a set * 'a set) set" haftmann@45137: where "finite_psubset = {(A,B). A < B & finite B}" krauss@26748: krauss@28260: lemma wf_finite_psubset[simp]: "wf(finite_psubset)" krauss@26748: apply (unfold finite_psubset_def) krauss@26748: apply (rule wf_measure [THEN wf_subset]) krauss@26748: apply (simp add: measure_def inv_image_def less_than_def less_eq) krauss@26748: apply (fast elim!: psubset_card_mono) krauss@26748: done krauss@26748: krauss@26748: lemma trans_finite_psubset: "trans finite_psubset" berghofe@26803: by (simp add: finite_psubset_def less_le trans_def, blast) krauss@26748: krauss@28260: lemma in_finite_psubset[simp]: "(A, B) \ finite_psubset = (A < B & finite B)" krauss@28260: unfolding finite_psubset_def by auto krauss@26748: krauss@28735: text {* max- and min-extension of order to finite sets *} krauss@28735: krauss@28735: inductive_set max_ext :: "('a \ 'a) set \ ('a set \ 'a set) set" krauss@28735: for R :: "('a \ 'a) set" krauss@28735: where krauss@28735: max_extI[intro]: "finite X \ finite Y \ Y \ {} \ (\x. x \ X \ \y\Y. (x, y) \ R) \ (X, Y) \ max_ext R" krauss@28735: krauss@28735: lemma max_ext_wf: krauss@28735: assumes wf: "wf r" krauss@28735: shows "wf (max_ext r)" krauss@28735: proof (rule acc_wfI, intro allI) krauss@28735: fix M show "M \ acc (max_ext r)" (is "_ \ ?W") krauss@28735: proof cases krauss@28735: assume "finite M" krauss@28735: thus ?thesis krauss@28735: proof (induct M) krauss@28735: show "{} \ ?W" krauss@28735: by (rule accI) (auto elim: max_ext.cases) krauss@28735: next krauss@28735: fix M a assume "M \ ?W" "finite M" krauss@28735: with wf show "insert a M \ ?W" krauss@28735: proof (induct arbitrary: M) krauss@28735: fix M a krauss@28735: assume "M \ ?W" and [intro]: "finite M" krauss@28735: assume hyp: "\b M. (b, a) \ r \ M \ ?W \ finite M \ insert b M \ ?W" krauss@28735: { krauss@28735: fix N M :: "'a set" krauss@28735: assume "finite N" "finite M" krauss@28735: then krauss@28735: have "\M \ ?W ; (\y. y \ N \ (y, a) \ r)\ \ N \ M \ ?W" krauss@28735: by (induct N arbitrary: M) (auto simp: hyp) krauss@28735: } krauss@28735: note add_less = this krauss@28735: krauss@28735: show "insert a M \ ?W" krauss@28735: proof (rule accI) krauss@28735: fix N assume Nless: "(N, insert a M) \ max_ext r" krauss@28735: hence asm1: "\x. x \ N \ (x, a) \ r \ (\y \ M. (x, y) \ r)" krauss@28735: by (auto elim!: max_ext.cases) krauss@28735: krauss@28735: let ?N1 = "{ n \ N. (n, a) \ r }" krauss@28735: let ?N2 = "{ n \ N. (n, a) \ r }" nipkow@39302: have N: "?N1 \ ?N2 = N" by (rule set_eqI) auto krauss@28735: from Nless have "finite N" by (auto elim: max_ext.cases) krauss@28735: then have finites: "finite ?N1" "finite ?N2" by auto krauss@28735: krauss@28735: have "?N2 \ ?W" krauss@28735: proof cases krauss@28735: assume [simp]: "M = {}" krauss@28735: have Mw: "{} \ ?W" by (rule accI) (auto elim: max_ext.cases) krauss@28735: krauss@28735: from asm1 have "?N2 = {}" by auto krauss@28735: with Mw show "?N2 \ ?W" by (simp only:) krauss@28735: next krauss@28735: assume "M \ {}" krauss@28735: have N2: "(?N2, M) \ max_ext r" krauss@28735: by (rule max_extI[OF _ _ `M \ {}`]) (insert asm1, auto intro: finites) krauss@28735: krauss@28735: with `M \ ?W` show "?N2 \ ?W" by (rule acc_downward) krauss@28735: qed krauss@28735: with finites have "?N1 \ ?N2 \ ?W" krauss@28735: by (rule add_less) simp krauss@28735: then show "N \ ?W" by (simp only: N) krauss@28735: qed krauss@28735: qed krauss@28735: qed krauss@28735: next krauss@28735: assume [simp]: "\ finite M" krauss@28735: show ?thesis krauss@28735: by (rule accI) (auto elim: max_ext.cases) krauss@28735: qed krauss@28735: qed krauss@28735: krauss@29125: lemma max_ext_additive: krauss@29125: "(A, B) \ max_ext R \ (C, D) \ max_ext R \ krauss@29125: (A \ C, B \ D) \ max_ext R" krauss@29125: by (force elim!: max_ext.cases) krauss@29125: krauss@28735: haftmann@37767: definition min_ext :: "('a \ 'a) set \ ('a set \ 'a set) set" where haftmann@37767: "min_ext r = {(X, Y) | X Y. X \ {} \ (\y \ Y. (\x \ X. (x, y) \ r))}" krauss@28735: krauss@28735: lemma min_ext_wf: krauss@28735: assumes "wf r" krauss@28735: shows "wf (min_ext r)" krauss@28735: proof (rule wfI_min) krauss@28735: fix Q :: "'a set set" krauss@28735: fix x krauss@28735: assume nonempty: "x \ Q" krauss@28735: show "\m \ Q. (\ n. (n, m) \ min_ext r \ n \ Q)" krauss@28735: proof cases krauss@28735: assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def) krauss@28735: next krauss@28735: assume "Q \ {{}}" krauss@28735: with nonempty krauss@28735: obtain e x where "x \ Q" "e \ x" by force krauss@28735: then have eU: "e \ \Q" by auto krauss@28735: with `wf r` krauss@28735: obtain z where z: "z \ \Q" "\y. (y, z) \ r \ y \ \Q" krauss@28735: by (erule wfE_min) krauss@28735: from z obtain m where "m \ Q" "z \ m" by auto krauss@28735: from `m \ Q` krauss@28735: show ?thesis krauss@28735: proof (rule, intro bexI allI impI) krauss@28735: fix n krauss@28735: assume smaller: "(n, m) \ min_ext r" krauss@28735: with `z \ m` obtain y where y: "y \ n" "(y, z) \ r" by (auto simp: min_ext_def) krauss@28735: then show "n \ Q" using z(2) by auto krauss@28735: qed krauss@28735: qed krauss@28735: qed krauss@26748: nipkow@43137: text{* Bounded increase must terminate: *} nipkow@43137: nipkow@43137: lemma wf_bounded_measure: nipkow@43137: fixes ub :: "'a \ nat" and f :: "'a \ nat" nipkow@43140: assumes "!!a b. (b,a) : r \ ub b \ ub a & ub a \ f b & f b > f a" nipkow@43137: shows "wf r" nipkow@43137: apply(rule wf_subset[OF wf_measure[of "%a. ub a - f a"]]) nipkow@43137: apply (auto dest: assms) nipkow@43137: done nipkow@43137: nipkow@43137: lemma wf_bounded_set: nipkow@43137: fixes ub :: "'a \ 'b set" and f :: "'a \ 'b set" nipkow@43137: assumes "!!a b. (b,a) : r \ nipkow@43140: finite(ub a) & ub b \ ub a & ub a \ f b & f b \ f a" nipkow@43137: shows "wf r" nipkow@43137: apply(rule wf_bounded_measure[of r "%a. card(ub a)" "%a. card(f a)"]) nipkow@43137: apply(drule assms) nipkow@43140: apply (blast intro: card_mono finite_subset psubset_card_mono dest: psubset_eq[THEN iffD2]) nipkow@43137: done nipkow@43137: krauss@26748: krauss@26748: subsection {* size of a datatype value *} krauss@26748: haftmann@31775: use "Tools/Function/size.ML" krauss@26748: krauss@26748: setup Size.setup krauss@26748: haftmann@28562: lemma size_bool [code]: haftmann@27823: "size (b\bool) = 0" by (cases b) auto haftmann@27823: haftmann@28562: lemma nat_size [simp, code]: "size (n\nat) = n" krauss@26748: by (induct n) simp_all krauss@26748: blanchet@35828: declare "prod.size" [no_atp] krauss@26748: haftmann@46638: lemma [code]: haftmann@46638: "size (P :: 'a Predicate.pred) = 0" by (cases P) simp haftmann@46638: haftmann@46638: lemma [code]: haftmann@46638: "pred_size f P = 0" by (cases P) simp haftmann@46638: krauss@26748: end