# HG changeset patch # User haftmann # Date 1622637927 0 # Node ID e75635a0bafd22d334f7cff6a81f822900ea320e # Parent 26c0ccf17f318614a645ec9136edea1e845989e2 lexorders the locale way diff -r 26c0ccf17f31 -r e75635a0bafd src/HOL/Library/Lexord.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Library/Lexord.thy Wed Jun 02 12:45:27 2021 +0000 @@ -0,0 +1,208 @@ +section \Lexicographic orderings\ + +theory Lexord + imports Main +begin + +subsection \The preorder case\ + +locale lex_preordering = preordering +begin + +inductive lex_less :: \'a list \ 'a list \ bool\ (infix \[\<^bold><]\ 50) +where + Nil: \[] [\<^bold><] y # ys\ +| Cons: \x \<^bold>< y \ x # xs [\<^bold><] y # ys\ +| Cons_eq: \x \<^bold>\ y \ y \<^bold>\ x \ xs [\<^bold><] ys \ x # xs [\<^bold><] y # ys\ + +inductive lex_less_eq :: \'a list \ 'a list \ bool\ (infix \[\<^bold>\]\ 50) +where + Nil: \[] [\<^bold>\] ys\ +| Cons: \x \<^bold>< y \ x # xs [\<^bold>\] y # ys\ +| Cons_eq: \x \<^bold>\ y \ y \<^bold>\ x \ xs [\<^bold>\] ys \ x # xs [\<^bold>\] y # ys\ + +lemma lex_less_simps [simp]: + \[] [\<^bold><] y # ys\ + \\ xs [\<^bold><] []\ + \x # xs [\<^bold><] y # ys \ x \<^bold>< y \ x \<^bold>\ y \ y \<^bold>\ x \ xs [\<^bold><] ys\ + by (auto intro: lex_less.intros elim: lex_less.cases) + +lemma lex_less_eq_simps [simp]: + \[] [\<^bold>\] ys\ + \\ x # xs [\<^bold>\] []\ + \x # xs [\<^bold>\] y # ys \ x \<^bold>< y \ x \<^bold>\ y \ y \<^bold>\ x \ xs [\<^bold>\] ys\ + by (auto intro: lex_less_eq.intros elim: lex_less_eq.cases) + +lemma lex_less_code [code]: + \[] [\<^bold><] y # ys \ True\ + \xs [\<^bold><] [] \ False\ + \x # xs [\<^bold><] y # ys \ x \<^bold>< y \ x \<^bold>\ y \ y \<^bold>\ x \ xs [\<^bold><] ys\ + by simp_all + +lemma lex_less_eq_code [code]: + \[] [\<^bold>\] ys \ True\ + \x # xs [\<^bold>\] [] \ False\ + \x # xs [\<^bold>\] y # ys \ x \<^bold>< y \ x \<^bold>\ y \ y \<^bold>\ x \ xs [\<^bold>\] ys\ + by simp_all + +lemma preordering: + \preordering ([\<^bold>\]) ([\<^bold><])\ +proof + fix xs ys zs + show \xs [\<^bold>\] xs\ + by (induction xs) (simp_all add: refl) + show \xs [\<^bold>\] zs\ if \xs [\<^bold>\] ys\ \ys [\<^bold>\] zs\ + using that proof (induction arbitrary: zs) + case (Nil ys) + then show ?case by simp + next + case (Cons x y xs ys) + then show ?case + by (cases zs) (auto dest: strict_trans strict_trans2) + next + case (Cons_eq x y xs ys) + then show ?case + by (cases zs) (auto dest: strict_trans1 intro: trans) + qed + show \xs [\<^bold><] ys \ xs [\<^bold>\] ys \ \ ys [\<^bold>\] xs\ (is \?P \ ?Q\) + proof + assume ?P + then have \xs [\<^bold>\] ys\ + by induction simp_all + moreover have \\ ys [\<^bold>\] xs\ + using \?P\ + by induction (simp_all, simp_all add: strict_iff_not asym) + ultimately show ?Q .. + next + assume ?Q + then have \xs [\<^bold>\] ys\ \\ ys [\<^bold>\] xs\ + by auto + then show ?P + proof induction + case (Nil ys) + then show ?case + by (cases ys) simp_all + next + case (Cons x y xs ys) + then show ?case + by simp + next + case (Cons_eq x y xs ys) + then show ?case + by simp + qed + qed +qed + +interpretation lex: preordering \([\<^bold>\])\ \([\<^bold><])\ + by (fact preordering) + +end + + +subsection \The order case\ + +locale lex_ordering = lex_preordering + ordering +begin + +interpretation lex: preordering \([\<^bold>\])\ \([\<^bold><])\ + by (fact preordering) + +lemma less_lex_Cons_iff [simp]: + \x # xs [\<^bold><] y # ys \ x \<^bold>< y \ x = y \ xs [\<^bold><] ys\ + by (auto intro: refl antisym) + +lemma less_eq_lex_Cons_iff [simp]: + \x # xs [\<^bold>\] y # ys \ x \<^bold>< y \ x = y \ xs [\<^bold>\] ys\ + by (auto intro: refl antisym) + +lemma ordering: + \ordering ([\<^bold>\]) ([\<^bold><])\ +proof + fix xs ys + show *: \xs = ys\ if \xs [\<^bold>\] ys\ \ys [\<^bold>\] xs\ + using that proof induction + case (Nil ys) + then show ?case by (cases ys) simp + next + case (Cons x y xs ys) + then show ?case by (auto dest: asym intro: antisym) + (simp add: strict_iff_not) + next + case (Cons_eq x y xs ys) + then show ?case by (auto intro: antisym) + (simp add: strict_iff_not) + qed + show \xs [\<^bold><] ys \ xs [\<^bold>\] ys \ xs \ ys\ + by (auto simp add: lex.strict_iff_not dest: *) +qed + +interpretation lex: ordering \([\<^bold>\])\ \([\<^bold><])\ + by (fact ordering) + +end + + +subsection \Canonical instance\ + +instantiation list :: (preorder) preorder +begin + +global_interpretation lex: lex_preordering \(\) :: 'a::preorder \ 'a \ bool\ \(<) :: 'a \ 'a \ bool\ + defines less_eq_list = lex.lex_less_eq + and less_list = lex.lex_less .. + +instance + by (rule class.preorder.of_class.intro, rule preordering_preorderI, fact lex.preordering) + +end + +global_interpretation lex: lex_ordering \(\) :: 'a::order \ 'a \ bool\ \(<) :: 'a \ 'a \ bool\ + rewrites \lex_preordering.lex_less_eq (\) (<) = ((\) :: 'a list \ 'a list \ bool)\ + and \lex_preordering.lex_less (\) (<) = ((<) :: 'a list \ 'a list \ bool)\ +proof - + interpret lex_ordering \(\) :: 'a \ 'a \ bool\ \(<) :: 'a \ 'a \ bool\ .. + show \lex_ordering ((\) :: 'a \ 'a \ bool) (<)\ + by (fact lex_ordering_axioms) + show \lex_preordering.lex_less_eq (\) (<) = (\)\ + by (simp add: less_eq_list_def) + show \lex_preordering.lex_less (\) (<) = (<)\ + by (simp add: less_list_def) +qed + +instance list :: (order) order + by (rule class.order.of_class.intro, rule ordering_orderI, fact lex.ordering) + +export_code \(\) :: _ list \ _ list \ bool\ \(<) :: _ list \ _ list \ bool\ in Haskell + + +subsection \Non-canonical instance\ + +context comm_monoid_mult +begin + +definition dvd_strict :: \'a \ 'a \ bool\ + where \dvd_strict a b \ a dvd b \ \ b dvd a\ + +end + +global_interpretation dvd: lex_preordering \(dvd) :: 'a::comm_monoid_mult \ 'a \ bool\ dvd_strict + defines lex_dvd = dvd.lex_less_eq + and lex_dvd_strict = dvd.lex_less + apply (rule lex_preordering.intro) + apply standard + apply (auto simp add: dvd_strict_def) + done + +print_theorems + +global_interpretation lex_dvd: preordering lex_dvd lex_dvd_strict + by (fact dvd.preordering) + +definition \example = lex_dvd [(4::int), - 7, 8] [- 8, 13, 5]\ + +export_code example in Haskell + +value example + +end diff -r 26c0ccf17f31 -r e75635a0bafd src/HOL/Orderings.thy --- a/src/HOL/Orderings.thy Mon May 31 20:27:45 2021 +0000 +++ b/src/HOL/Orderings.thy Wed Jun 02 12:45:27 2021 +0000 @@ -279,6 +279,16 @@ end +lemma preordering_preorderI: + \class.preorder (\<^bold>\) (\<^bold><)\ if \preordering (\<^bold>\) (\<^bold><)\ + for less_eq (infix \\<^bold>\\ 50) and less (infix \\<^bold><\ 50) +proof - + from that interpret preordering \(\<^bold>\)\ \(\<^bold><)\ . + show ?thesis + by standard (auto simp add: strict_iff_not refl intro: trans) +qed + + subsection \Partial orders\ @@ -409,12 +419,12 @@ qed lemma order_strictI: - fixes less (infix "\" 50) - and less_eq (infix "\" 50) - assumes "\a b. a \ b \ a \ b \ a = b" - assumes "\a b. a \ b \ \ b \ a" - assumes "\a. \ a \ a" - assumes "\a b c. a \ b \ b \ c \ a \ c" + fixes less (infix "\<^bold><" 50) + and less_eq (infix "\<^bold>\" 50) + assumes "\a b. a \<^bold>\ b \ a \<^bold>< b \ a = b" + assumes "\a b. a \<^bold>< b \ \ b \<^bold>< a" + assumes "\a. \ a \<^bold>< a" + assumes "\a b c. a \<^bold>< b \ b \<^bold>< c \ a \<^bold>< c" shows "class.order less_eq less" by (rule ordering_orderI) (rule ordering_strictI, (fact assms)+)