# HG changeset patch # User haftmann # Date 1463038447 -7200 # Node ID f2177f5d2aed353de67a495695e79cace526f710 # Parent be252979cfe50b140459d2787d229f8e6527be08 a quasi-recursive characterization of the multiset order (by Christian Sternagel) diff -r be252979cfe5 -r f2177f5d2aed src/HOL/Library/Multiset.thy --- a/src/HOL/Library/Multiset.thy Thu May 12 11:34:19 2016 +0200 +++ b/src/HOL/Library/Multiset.thy Thu May 12 09:34:07 2016 +0200 @@ -2066,6 +2066,117 @@ \ (I + K, I + J) \ mult r" using one_step_implies_mult_aux by blast +subsection \A quasi-recursive characterization\ + +text \ + The decreasing parts \A\ and \B\ of multisets in a multiset-comparison + \(I + B, I + A) \ mult r\, can always be made disjoint. +\ +lemma decreasing_parts_disj: + assumes "irrefl r" and "trans r" + and "A \ {#}" and *: "\b\#B. \a\#A. (b, a) \ r" + defines "Z \ A #\ B" + defines "X \ A - Z" + defines "Y \ B - Z" + shows "X \ {#} \ X #\ Y = {#} \ + A = X + Z \ B = Y + Z \ (\y\#Y. \x\#X. (y, x) \ r)" +proof - + define D + where "D = set_mset A \ set_mset B" + let ?r = "r \ D \ D" + have "irrefl ?r" and "trans ?r" and "finite ?r" + using \irrefl r\ and \trans r\ by (auto simp: D_def irrefl_def trans_Restr) + note wf_converse_induct = wf_induct [OF wf_converse [OF this]] + { fix b assume "b \# B" + then have "\x. x \# X \ (b, x) \ r" + proof (induction rule: wf_converse_induct) + case (1 b) + then obtain a where "b \# B" and "a \# A" and "(b, a) \ r" + using * by blast + then show ?case + proof (cases "a \# X") + case False + then have "a \# B" using \a \# A\ + by (simp add: X_def Z_def not_in_iff) + (metis le_zero_eq not_in_iff) + moreover have "(a, b) \ (r \ D \ D)\" + using \(b, a) \ r\ using \b \# B\ and \a \# B\ + by (auto simp: D_def) + ultimately obtain x where "x \# X" and "(a, x) \ r" + using "1.IH" by blast + moreover then have "(b, x) \ r" + using \trans r\ and \(b, a) \ r\ by (auto dest: transD) + ultimately show ?thesis by blast + qed blast + qed } + note B_less = this + then have "\y\#Y. \x\#X. (y, x) \ r" + by (auto simp: Y_def Z_def dest: in_diffD) + moreover have "X \ {#}" + proof - + obtain a where "a \# A" using \A \ {#}\ + by (auto simp: multiset_eq_iff) + show ?thesis + proof (cases "a \# X") + case False + then have "a \# B" using \a \# A\ + by (simp add: X_def Z_def not_in_iff) + (metis le_zero_eq not_in_iff) + then show ?thesis by (auto dest: B_less) + qed auto + qed + moreover have "A = X + Z" and "B = Y + Z" and "X #\ Y = {#}" + by (auto simp: X_def Y_def Z_def multiset_eq_iff) + ultimately show ?thesis by blast +qed + +text \ + A predicate variant of the reflexive closure of \mult\, which is + executable whenever the given predicate \P\ is. Together with the + standard code equations for \op #\\ and \op -\ this should yield + a quadratic (with respect to calls to \P\) implementation of \multeqp\. +\ +definition multeqp :: "('a \ 'a \ bool) \ 'a multiset \ 'a multiset \ bool" where + "multeqp P N M = + (let Z = M #\ N; X = M - Z; Y = N - Z in + (\y \ set_mset Y. \x \ set_mset X. P y x))" + +lemma multeqp_iff: + assumes "irrefl R" and "trans R" + and [simp]: "\x y. P x y \ (x, y) \ R" + shows "multeqp P N M \ (N, M) \ (mult R)\<^sup>=" +proof + assume "(N, M) \ (mult R)\<^sup>=" + then show "multeqp P N M" + proof + assume "(N, M) \ mult R" + from mult_implies_one_step [OF \trans R\ this] obtain I J K + where *: "I \ {#}" "\j\#J. \i\#I. (j, i) \ R" + and [simp]: "M = K + I" "N = K + J" by blast + from decreasing_parts_disj [OF \irrefl R\ \trans R\ *] + show "multeqp P N M" + by (auto simp: multeqp_def split: if_splits) + next + assume "(N, M) \ Id" then show "multeqp P N M" by (auto simp: multeqp_def) + qed +next + assume "multeqp P N M" + then obtain X Y Z where *: "Z = M #\ N" "X = M - Z" "Y = N - Z" + and **: "\y\#Y. \x\#X. (y, x) \ R" by (auto simp: multeqp_def Let_def) + then have M: "M = Z + X" and N: "N = Z + Y" by (auto simp: multiset_eq_iff) + show "(N, M) \ (mult R)\<^sup>=" + proof (cases "X \ {#}") + case True + with * and ** have "(Z + Y, Z + X) \ mult R" + by (auto intro: one_step_implies_mult) + then show ?thesis by (simp add: M N) + next + case False + then show ?thesis using ** + by (cases "Y = {#}") (auto simp: M N) + qed +qed + subsubsection \Partial-order properties\ diff -r be252979cfe5 -r f2177f5d2aed src/HOL/Wellfounded.thy --- a/src/HOL/Wellfounded.thy Thu May 12 11:34:19 2016 +0200 +++ b/src/HOL/Wellfounded.thy Thu May 12 09:34:07 2016 +0200 @@ -459,6 +459,20 @@ apply (erule acyclic_converse [THEN iffD2]) done +text \ + Observe that the converse of an irreflexive, transitive, + and finite relation is again well-founded. Thus, we may + employ it for well-founded induction. +\ +lemma wf_converse: + assumes "irrefl r" and "trans r" and "finite r" + shows "wf (r\)" +proof - + have "acyclic r" + using \irrefl r\ and \trans r\ by (simp add: irrefl_def acyclic_irrefl) + with \finite r\ show ?thesis by (rule finite_acyclic_wf_converse) +qed + lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r" by (blast intro: finite_acyclic_wf wf_acyclic)