krauss@26748: (* ID: $Id$ krauss@26748: Author: Tobias Nipkow krauss@26748: Author: Lawrence C Paulson, Cambridge University Computer Laboratory krauss@26748: Author: Konrad Slind, Alexander Krauss krauss@26748: Copyright 1992-2008 University of Cambridge and TU Muenchen krauss@26748: *) krauss@26748: krauss@26748: header {*Well-founded Recursion*} krauss@26748: krauss@26748: theory Wellfounded krauss@26748: imports Finite_Set Nat krauss@26748: uses ("Tools/function_package/size.ML") krauss@26748: begin krauss@26748: krauss@26976: subsection {* Basic Definitions *} krauss@26976: krauss@26748: inductive krauss@26748: wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool" krauss@26748: for R :: "('a * 'a) set" krauss@26748: and F :: "('a => 'b) => 'a => 'b" krauss@26748: where krauss@26748: wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==> krauss@26748: wfrec_rel R F x (F g x)" krauss@26748: krauss@26748: constdefs krauss@26748: wf :: "('a * 'a)set => bool" krauss@26748: "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))" krauss@26748: krauss@26748: wfP :: "('a => 'a => bool) => bool" krauss@26748: "wfP r == wf {(x, y). r x y}" krauss@26748: krauss@26748: acyclic :: "('a*'a)set => bool" krauss@26748: "acyclic r == !x. (x,x) ~: r^+" krauss@26748: krauss@26748: cut :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b" krauss@26748: "cut f r x == (%y. if (y,x):r then f y else arbitrary)" krauss@26748: krauss@26748: adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool" krauss@26748: "adm_wf R F == ALL f g x. krauss@26748: (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x" krauss@26748: krauss@26748: wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" krauss@26748: [code func del]: "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y" krauss@26748: krauss@26748: abbreviation acyclicP :: "('a => 'a => bool) => bool" where krauss@26748: "acyclicP r == acyclic {(x, y). r x y}" krauss@26748: krauss@26748: class wellorder = linorder + krauss@26748: assumes wf: "wf {(x, y). x < y}" krauss@26748: 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@26748: (* [| wf r; ~Z ==> (a,x) : r; (x,a) ~: r ==> Z |] ==> Z *) krauss@26748: lemmas wf_asym = wf_not_sym [elim_format] krauss@26748: krauss@26748: lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r" krauss@26748: by (blast elim: wf_asym) krauss@26748: krauss@26748: (* [| wf r; (a,a) ~: r ==> PROP W |] ==> PROP W *) krauss@26748: lemmas wf_irrefl = wf_not_refl [elim_format] krauss@26748: krauss@26976: subsection {* Basic Results *} krauss@26976: krauss@26748: text{*transitive closure of a well-founded relation is well-founded! *} 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) krauss@26748: case (less x) krauss@26748: note hyp = `\x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'` krauss@26748: from `(y, x) : r^+` show "P y" krauss@26748: proof cases krauss@26748: case base krauss@26748: show "P y" krauss@26748: proof (rule induct_step) krauss@26748: fix y' assume "(y', y) : r^+" krauss@26748: with `(y, x) : r` show "P y'" by (rule hyp [of y y']) krauss@26748: qed krauss@26748: next krauss@26748: case step krauss@26748: then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp krauss@26748: then show "P y" by (rule hyp [of x' y]) krauss@26748: 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@26748: krauss@26748: text{*Minimal-element characterization of well-foundedness*} krauss@26748: lemma wf_eq_minimal: "wf r = (\Q x. x\Q --> (\z\Q. \y. (y,z)\r --> y\Q))" krauss@26748: proof (intro iffI strip) krauss@26748: fix Q :: "'a set" and x krauss@26748: assume "wf r" and "x \ Q" krauss@26748: then show "\z\Q. \y. (y, z) \ r \ y \ Q" krauss@26748: unfolding wf_def krauss@26748: by (blast dest: spec [of _ "%x. x\Q \ (\z\Q. \y. (y,z) \ r \ y\Q)"]) krauss@26748: next krauss@26748: assume 1: "\Q x. x \ Q \ (\z\Q. \y. (y, z) \ r \ y \ Q)" krauss@26748: show "wf r" krauss@26748: proof (rule wfUNIVI) krauss@26748: fix P :: "'a \ bool" and x krauss@26748: assume 2: "\x. (\y. (y, x) \ r \ P y) \ P x" krauss@26748: let ?Q = "{x. \ P x}" krauss@26748: have "x \ ?Q \ (\z \ ?Q. \y. (y, z) \ r \ y \ ?Q)" krauss@26748: by (rule 1 [THEN spec, THEN spec]) krauss@26748: then have "\ P x \ (\z. \ P z \ (\y. (y, z) \ r \ P y))" by simp krauss@26748: with 2 have "\ P x \ (\z. \ P z \ P z)" by fast krauss@26748: then show "P x" by simp krauss@26748: qed krauss@26748: qed krauss@26748: krauss@26748: lemma wfE_min: krauss@26748: assumes "wf R" "x \ Q" krauss@26748: obtains z where "z \ Q" "\y. (y, z) \ R \ y \ Q" krauss@26748: using assms unfolding wf_eq_minimal by blast krauss@26748: krauss@26748: lemma wfI_min: krauss@26748: "(\x Q. x \ Q \ \z\Q. \y. (y, z) \ R \ y \ Q) krauss@26748: \ wf R" krauss@26748: unfolding wf_eq_minimal by blast krauss@26748: krauss@26748: lemmas wfP_eq_minimal = wf_eq_minimal [to_pred] krauss@26748: krauss@26748: text {* Well-foundedness of subsets *} 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@26748: lemma wf_empty [iff]: "wf({})" krauss@26748: by (simp add: wf_def) krauss@26748: krauss@26748: lemmas wfP_empty [iff] = krauss@26748: wf_empty [to_pred bot_empty_eq2, simplified bot_fun_eq bot_bool_eq] 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@26748: text{*Well-foundedness of insert*} 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@26748: lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun 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@26748: assumes "S O R \ 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@26748: from `(w, y) \ R` `(y, z) \ S` have "(w, z) \ S O R" by (rule rel_compI) krauss@26748: with `S O R \ 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: krauss@26748: lemmas wfP_SUP = wf_UN [where I=UNIV and r="\i. {(x, y). r i x y}", berghofe@26803: to_pred SUP_UN_eq2 bot_empty_eq pred_equals_eq, simplified, standard] 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)" krauss@26748: apply (simp add: Union_def) krauss@26748: apply (blast intro: wf_UN) krauss@26748: done 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@26748: "wf (R \ S) = wf (R O R \ R O S \ 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@26748: and A2: "\y. (y, z) \ R O S \ 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@26748: then have "(y, z) \ R O S" 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@26748: subsubsection {* acyclic *} krauss@26748: krauss@26748: lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r" krauss@26748: by (simp add: acyclic_def) krauss@26748: 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: lemma acyclic_insert [iff]: krauss@26748: "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)" krauss@26748: apply (simp add: acyclic_def trancl_insert) krauss@26748: apply (blast intro: rtrancl_trans) krauss@26748: done krauss@26748: krauss@26748: lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r" krauss@26748: by (simp add: acyclic_def trancl_converse) krauss@26748: krauss@26748: lemmas acyclicP_converse [iff] = acyclic_converse [to_pred] krauss@26748: krauss@26748: lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)" krauss@26748: apply (simp add: acyclic_def antisym_def) krauss@26748: apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl) krauss@26748: done krauss@26748: krauss@26748: (* Other direction: krauss@26748: acyclic = no loops krauss@26748: antisym = only self loops krauss@26748: Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id) krauss@26748: ==> antisym( r^* ) = acyclic(r - Id)"; krauss@26748: *) krauss@26748: krauss@26748: lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r" krauss@26748: apply (simp add: acyclic_def) krauss@26748: apply (blast intro: trancl_mono) krauss@26748: done 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{*Well-Founded Recursion*} krauss@26748: krauss@26748: text{*cut*} krauss@26748: krauss@26748: lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))" krauss@26748: by (simp add: expand_fun_eq cut_def) krauss@26748: krauss@26748: lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)" krauss@26748: by (simp add: cut_def) krauss@26748: krauss@26748: text{*Inductive characterization of wfrec combinator; for details see: krauss@26748: John Harrison, "Inductive definitions: automation and application"*} krauss@26748: krauss@26748: lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y" krauss@26748: apply (simp add: adm_wf_def) krauss@26748: apply (erule_tac a=x in wf_induct) krauss@26748: apply (rule ex1I) krauss@26748: apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI) krauss@26748: apply (fast dest!: theI') krauss@26748: apply (erule wfrec_rel.cases, simp) krauss@26748: apply (erule allE, erule allE, erule allE, erule mp) krauss@26748: apply (fast intro: the_equality [symmetric]) krauss@26748: done krauss@26748: krauss@26748: lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)" krauss@26748: apply (simp add: adm_wf_def) krauss@26748: apply (intro strip) krauss@26748: apply (rule cuts_eq [THEN iffD2, THEN subst], assumption) krauss@26748: apply (rule refl) krauss@26748: done krauss@26748: krauss@26748: lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a" krauss@26748: apply (simp add: wfrec_def) krauss@26748: apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption) krauss@26748: apply (rule wfrec_rel.wfrecI) krauss@26748: apply (intro strip) krauss@26748: apply (erule adm_lemma [THEN wfrec_unique, THEN theI']) krauss@26748: done krauss@26748: krauss@26748: subsection {* Code generator setup *} krauss@26748: krauss@26748: consts_code krauss@26748: "wfrec" ("\wfrec?") krauss@26748: attach {* krauss@26748: fun wfrec f x = f (wfrec f) x; krauss@26748: *} krauss@26748: krauss@26748: krauss@26748: subsection {*LEAST and wellorderings*} krauss@26748: krauss@26748: text{* See also @{text wf_linord_ex_has_least} and its consequences in krauss@26748: @{text Wellfounded_Relations.ML}*} krauss@26748: krauss@26748: lemma wellorder_Least_lemma [rule_format]: krauss@26748: "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k" krauss@26748: apply (rule_tac a = k in wf [THEN wf_induct]) krauss@26748: apply (rule impI) krauss@26748: apply (rule classical) krauss@26748: apply (rule_tac s = x in Least_equality [THEN ssubst], auto) krauss@26748: apply (auto simp add: linorder_not_less [symmetric]) krauss@26748: done krauss@26748: krauss@26748: lemmas LeastI = wellorder_Least_lemma [THEN conjunct1, standard] krauss@26748: lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard] krauss@26748: krauss@26748: -- "The following 3 lemmas are due to Brian Huffman" krauss@26748: lemma LeastI_ex: "EX x::'a::wellorder. P x ==> P (Least P)" krauss@26748: apply (erule exE) krauss@26748: apply (erule LeastI) krauss@26748: done krauss@26748: krauss@26748: lemma LeastI2: krauss@26748: "[| P (a::'a::wellorder); !!x. P x ==> Q x |] ==> Q (Least P)" krauss@26748: by (blast intro: LeastI) krauss@26748: krauss@26748: lemma LeastI2_ex: krauss@26748: "[| EX a::'a::wellorder. P a; !!x. P x ==> Q x |] ==> Q (Least P)" krauss@26748: by (blast intro: LeastI_ex) krauss@26748: krauss@26748: lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)" krauss@26748: apply (simp (no_asm_use) add: linorder_not_le [symmetric]) krauss@26748: apply (erule contrapos_nn) krauss@26748: apply (erule Least_le) krauss@26748: done 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" krauss@26748: by (simp add: less_than_def trans_trancl) krauss@26748: krauss@26748: lemma less_than_iff [iff]: "((x,y): less_than) = (x (LEAST n. P n) = Suc (LEAST m. P(Suc m))" krauss@26748: apply (case_tac "n", auto) krauss@26748: apply (frule LeastI) krauss@26748: apply (drule_tac P = "%x. P (Suc x) " in LeastI) krauss@26748: apply (subgoal_tac " (LEAST x. P x) \ Suc (LEAST x. P (Suc x))") krauss@26748: apply (erule_tac [2] Least_le) krauss@26748: apply (case_tac "LEAST x. P x", auto) krauss@26748: apply (drule_tac P = "%x. P (Suc x) " in Least_le) krauss@26748: apply (blast intro: order_antisym) krauss@26748: done krauss@26748: krauss@26748: lemma Least_Suc2: krauss@26748: "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)" krauss@26748: apply (erule (1) Least_Suc [THEN ssubst]) krauss@26748: apply simp krauss@26748: done krauss@26748: krauss@26748: lemma ex_least_nat_le: "\P(0) \ P(n::nat) \ \k\n. (\iP i) & P(k)" krauss@26748: apply (cases n) krauss@26748: apply blast krauss@26748: apply (rule_tac x="LEAST k. P(k)" in exI) krauss@26748: apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex) krauss@26748: done krauss@26748: krauss@26748: lemma ex_least_nat_less: "\P(0) \ P(n::nat) \ \ki\k. \P i) & P(k+1)" krauss@26748: apply (cases n) krauss@26748: apply blast krauss@26748: apply (frule (1) ex_least_nat_le) krauss@26748: apply (erule exE) krauss@26748: apply (case_tac k) krauss@26748: apply simp krauss@26748: apply (rename_tac k1) krauss@26748: apply (rule_tac x=k1 in exI) krauss@26748: apply fastsimp krauss@26748: done krauss@26748: krauss@26748: krauss@26748: subsection {* Accessible Part *} krauss@26748: krauss@26748: text {* krauss@26748: Inductive definition of the accessible part @{term "acc r"} of a krauss@26748: relation; see also \cite{paulin-tlca}. krauss@26748: *} krauss@26748: krauss@26748: inductive_set krauss@26748: acc :: "('a * 'a) set => '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 krauss@26748: "termip r == accp (r\\)" krauss@26748: krauss@26748: abbreviation krauss@26748: termi :: "('a * 'a) set => 'a set" where krauss@26748: "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) krauss@26748: apply (induct_tac P x rule: 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@26803: lemmas acc_subset = accp_subset [to_set pred_subset_eq] krauss@26748: berghofe@26803: lemmas acc_subset_induct = accp_subset_induct [to_set pred_subset_eq] 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@26748: lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)" krauss@26748: by (auto simp:inv_image_def) krauss@26748: krauss@26748: text {* Measure functions into @{typ nat} *} krauss@26748: krauss@26748: definition measure :: "('a => nat) => ('a * 'a)set" krauss@26748: where "measure == inv_image less_than" krauss@26748: krauss@26748: lemma in_measure[simp]: "((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: krauss@26748: text{* Lexicographic combinations *} krauss@26748: krauss@26748: definition krauss@26748: lex_prod :: "[('a*'a)set, ('b*'b)set] => (('a*'b)*('a*'b))set" krauss@26748: (infixr "<*lex*>" 80) krauss@26748: where krauss@26748: "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@26748: 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" krauss@26748: where "finite_psubset == {(A,B). A < B & finite B}" krauss@26748: krauss@26748: lemma wf_finite_psubset: "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@26748: krauss@26748: krauss@26748: krauss@26748: text {*Wellfoundedness of @{text same_fst}*} krauss@26748: krauss@26748: definition krauss@26748: same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set" krauss@26748: where krauss@26748: "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}" krauss@26748: --{*For @{text rec_def} declarations where the first n parameters krauss@26748: stay unchanged in the recursive call. krauss@26748: See @{text "Library/While_Combinator.thy"} for an application.*} krauss@26748: krauss@26748: lemma same_fstI [intro!]: krauss@26748: "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R" krauss@26748: by (simp add: same_fst_def) krauss@26748: krauss@26748: lemma wf_same_fst: krauss@26748: assumes prem: "(!!x. P x ==> wf(R x))" krauss@26748: shows "wf(same_fst P R)" krauss@26748: apply (simp cong del: imp_cong add: wf_def same_fst_def) krauss@26748: apply (intro strip) krauss@26748: apply (rename_tac a b) krauss@26748: apply (case_tac "wf (R a)") krauss@26748: apply (erule_tac a = b in wf_induct, blast) krauss@26748: apply (blast intro: prem) krauss@26748: done krauss@26748: krauss@26748: krauss@26748: subsection{*Weakly decreasing sequences (w.r.t. some well-founded order) krauss@26748: stabilize.*} krauss@26748: krauss@26748: text{*This material does not appear to be used any longer.*} krauss@26748: krauss@26748: lemma lemma1: "[| ALL i. (f (Suc i), f i) : r^* |] ==> (f (i+k), f i) : r^*" krauss@26748: apply (induct_tac "k", simp_all) krauss@26748: apply (blast intro: rtrancl_trans) krauss@26748: done krauss@26748: krauss@26748: lemma lemma2: "[| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |] krauss@26748: ==> ALL m. f m = x --> (EX i. ALL k. f (m+i+k) = f (m+i))" krauss@26748: apply (erule wf_induct, clarify) krauss@26748: apply (case_tac "EX j. (f (m+j), f m) : r^+") krauss@26748: apply clarify krauss@26748: apply (subgoal_tac "EX i. ALL k. f ((m+j) +i+k) = f ( (m+j) +i) ") krauss@26748: apply clarify krauss@26748: apply (rule_tac x = "j+i" in exI) krauss@26748: apply (simp add: add_ac, blast) krauss@26748: apply (rule_tac x = 0 in exI, clarsimp) krauss@26748: apply (drule_tac i = m and k = k in lemma1) krauss@26748: apply (blast elim: rtranclE dest: rtrancl_into_trancl1) krauss@26748: done krauss@26748: krauss@26748: lemma wf_weak_decr_stable: "[| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |] krauss@26748: ==> EX i. ALL k. f (i+k) = f i" krauss@26748: apply (drule_tac x = 0 in lemma2 [THEN spec], auto) krauss@26748: done krauss@26748: krauss@26748: (* special case of the theorem above: <= *) krauss@26748: lemma weak_decr_stable: krauss@26748: "ALL i. f (Suc i) <= ((f i)::nat) ==> EX i. ALL k. f (i+k) = f i" krauss@26748: apply (rule_tac r = pred_nat in wf_weak_decr_stable) krauss@26748: apply (simp add: pred_nat_trancl_eq_le) krauss@26748: apply (intro wf_trancl wf_pred_nat) krauss@26748: done krauss@26748: krauss@26748: krauss@26748: subsection {* size of a datatype value *} krauss@26748: krauss@26748: use "Tools/function_package/size.ML" krauss@26748: krauss@26748: setup Size.setup krauss@26748: krauss@26748: lemma nat_size [simp, code func]: "size (n\nat) = n" krauss@26748: by (induct n) simp_all krauss@26748: krauss@26748: krauss@26748: end