New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/List_Lenlexorder.thy Fri Apr 17 20:55:53 2020 +0100
@@ -0,0 +1,95 @@
+(* Title: HOL/Library/List_Lenlexorder.thy
+*)
+
+section \<open>Lexicographic order on lists\<close>
+
+theory List_Lenlexorder
+imports Main
+begin
+
+
+instantiation list :: (ord) ord
+begin
+
+definition
+ list_less_def: "xs < ys \<longleftrightarrow> (xs, ys) \<in> lenlex {(u, v). u < v}"
+
+definition
+ list_le_def: "(xs :: _ list) \<le> ys \<longleftrightarrow> xs < ys \<or> xs = ys"
+
+instance ..
+
+end
+
+instance list :: (order) order
+proof
+ have tr: "trans {(u, v::'a). u < v}"
+ using trans_def by fastforce
+ have \<section>: False
+ if "(xs,ys) \<in> lenlex {(u, v). u < v}" "(ys,xs) \<in> lenlex {(u, v). u < v}" for xs ys :: "'a list"
+ proof -
+ have "(xs,xs) \<in> lenlex {(u, v). u < v}"
+ using that transD [OF lenlex_transI [OF tr]] by blast
+ then show False
+ by (meson case_prodD lenlex_irreflexive less_irrefl mem_Collect_eq)
+ qed
+ show "xs \<le> xs" for xs :: "'a list" by (simp add: list_le_def)
+ show "xs \<le> zs" if "xs \<le> ys" and "ys \<le> zs" for xs ys zs :: "'a list"
+ using that transD [OF lenlex_transI [OF tr]] by (auto simp add: list_le_def list_less_def)
+ show "xs = ys" if "xs \<le> ys" "ys \<le> xs" for xs ys :: "'a list"
+ using \<section> that list_le_def list_less_def by blast
+ show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs" for xs ys :: "'a list"
+ by (auto simp add: list_less_def list_le_def dest: \<section>)
+qed
+
+instance list :: (linorder) linorder
+proof
+ fix xs ys :: "'a list"
+ have "total (lenlex {(u, v::'a). u < v})"
+ by (rule total_lenlex) (auto simp: total_on_def)
+ then show "xs \<le> ys \<or> ys \<le> xs"
+ by (auto simp add: total_on_def list_le_def list_less_def)
+qed
+
+instantiation list :: (linorder) distrib_lattice
+begin
+
+definition "(inf :: 'a list \<Rightarrow> _) = min"
+
+definition "(sup :: 'a list \<Rightarrow> _) = max"
+
+instance
+ by standard (auto simp add: inf_list_def sup_list_def max_min_distrib2)
+
+end
+
+lemma not_less_Nil [simp]: "\<not> x < []"
+ by (simp add: list_less_def)
+
+lemma Nil_less_Cons [simp]: "[] < a # x"
+ by (simp add: list_less_def)
+
+lemma Cons_less_Cons: "a # x < b # y \<longleftrightarrow> length x < length y \<or> length x = length y \<and> (a < b \<or> a = b \<and> x < y)"
+ using lenlex_length
+ by (fastforce simp: list_less_def Cons_lenlex_iff)
+
+lemma le_Nil [simp]: "x \<le> [] \<longleftrightarrow> x = []"
+ unfolding list_le_def by (cases x) auto
+
+lemma Nil_le_Cons [simp]: "[] \<le> x"
+ unfolding list_le_def by (cases x) auto
+
+lemma Cons_le_Cons: "a # x \<le> b # y \<longleftrightarrow> length x < length y \<or> length x = length y \<and> (a < b \<or> a = b \<and> x \<le> y)"
+ by (auto simp: list_le_def Cons_less_Cons)
+
+instantiation list :: (order) order_bot
+begin
+
+definition "bot = []"
+
+instance
+ by standard (simp add: bot_list_def)
+
+end
+
+end
--- a/src/HOL/Library/List_Lexorder.thy Fri Apr 17 17:32:11 2020 +0200
+++ b/src/HOL/Library/List_Lexorder.thy Fri Apr 17 20:55:53 2020 +0100
@@ -23,47 +23,33 @@
instance list :: (order) order
proof
- fix xs :: "'a list"
- show "xs \<le> xs" by (simp add: list_le_def)
-next
- fix xs ys zs :: "'a list"
- assume "xs \<le> ys" and "ys \<le> zs"
- then show "xs \<le> zs"
- apply (auto simp add: list_le_def list_less_def)
- apply (rule lexord_trans)
- apply (auto intro: transI)
- done
-next
- fix xs ys :: "'a list"
- assume "xs \<le> ys" and "ys \<le> xs"
- then show "xs = ys"
- apply (auto simp add: list_le_def list_less_def)
- apply (rule lexord_irreflexive [THEN notE])
- defer
- apply (rule lexord_trans)
- apply (auto intro: transI)
- done
-next
- fix xs ys :: "'a list"
- show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs"
- apply (auto simp add: list_less_def list_le_def)
- defer
- apply (rule lexord_irreflexive [THEN notE])
- apply auto
- apply (rule lexord_irreflexive [THEN notE])
- defer
- apply (rule lexord_trans)
- apply (auto intro: transI)
- done
+ let ?r = "{(u, v::'a). u < v}"
+ have tr: "trans ?r"
+ using trans_def by fastforce
+ have \<section>: False
+ if "(xs,ys) \<in> lexord ?r" "(ys,xs) \<in> lexord ?r" for xs ys :: "'a list"
+ proof -
+ have "(xs,xs) \<in> lexord ?r"
+ using that transD [OF lexord_transI [OF tr]] by blast
+ then show False
+ by (meson case_prodD lexord_irreflexive less_irrefl mem_Collect_eq)
+ qed
+ show "xs \<le> xs" for xs :: "'a list" by (simp add: list_le_def)
+ show "xs \<le> zs" if "xs \<le> ys" and "ys \<le> zs" for xs ys zs :: "'a list"
+ using that transD [OF lexord_transI [OF tr]] by (auto simp add: list_le_def list_less_def)
+ show "xs = ys" if "xs \<le> ys" "ys \<le> xs" for xs ys :: "'a list"
+ using \<section> that list_le_def list_less_def by blast
+ show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs" for xs ys :: "'a list"
+ by (auto simp add: list_less_def list_le_def dest: \<section>)
qed
instance list :: (linorder) linorder
proof
fix xs ys :: "'a list"
- have "(xs, ys) \<in> lexord {(u, v). u < v} \<or> xs = ys \<or> (ys, xs) \<in> lexord {(u, v). u < v}"
- by (rule lexord_linear) auto
+ have "total (lexord {(u, v::'a). u < v})"
+ by (rule total_lexord) (auto simp: total_on_def)
then show "xs \<le> ys \<or> ys \<le> xs"
- by (auto simp add: list_le_def list_less_def)
+ by (auto simp add: total_on_def list_le_def list_less_def)
qed
instantiation list :: (linorder) distrib_lattice
--- a/src/HOL/List.thy Fri Apr 17 17:32:11 2020 +0200
+++ b/src/HOL/List.thy Fri Apr 17 20:55:53 2020 +0100
@@ -6178,6 +6178,13 @@
\<or> (m = n \<and> (ms,ns) \<in> lenlex r)"
by (auto simp: lenlex_def)
+lemma lenlex_irreflexive: "(\<And>x. (x,x) \<notin> r) \<Longrightarrow> (xs,xs) \<notin> lenlex r"
+ by (induction xs) (auto simp add: Cons_lenlex_iff)
+
+lemma lenlex_trans:
+ "\<lbrakk>(x,y) \<in> lenlex r; (y,z) \<in> lenlex r; trans r\<rbrakk> \<Longrightarrow> (x,z) \<in> lenlex r"
+ by (meson lenlex_transI transD)
+
lemma lenlex_length: "(ms, ns) \<in> lenlex r \<Longrightarrow> length ms \<le> length ns"
by (auto simp: lenlex_def)
@@ -6316,15 +6323,27 @@
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)"
by (rule transI, drule lexord_trans, blast)
-lemma lexord_linear: "(\<forall>a b. (a,b) \<in> r \<or> a = b \<or> (b,a) \<in> r) \<Longrightarrow> (x,y) \<in> lexord r \<or> x = y \<or> (y,x) \<in> lexord r"
-proof (induction x arbitrary: y)
- case Nil
- then show ?case
- by (metis lexord_Nil_left list.exhaust)
-next
- case (Cons a x y) then show ?case
- by (cases y) (force+)
-qed
+lemma total_lexord: "total r \<Longrightarrow> total (lexord r)"
+ unfolding total_on_def
+proof clarsimp
+ fix x y
+ assume "\<forall>x y. x \<noteq> y \<longrightarrow> (x, y) \<in> r \<or> (y, x) \<in> r"
+ and "(x::'a list) \<noteq> y"
+ and "(y, x) \<notin> lexord r"
+ then
+ show "(x, y) \<in> lexord r"
+ proof (induction x arbitrary: y)
+ case Nil
+ then show ?case
+ by (metis lexord_Nil_left list.exhaust)
+ next
+ case (Cons a x y) then show ?case
+ by (cases y) (force+)
+ qed
+qed
+
+corollary lexord_linear: "(\<forall>a b. (a,b) \<in> r \<or> a = b \<or> (b,a) \<in> r) \<Longrightarrow> (x,y) \<in> lexord r \<or> x = y \<or> (y,x) \<in> lexord r"
+ using total_lexord by (metis UNIV_I total_on_def)
lemma lexord_irrefl:
"irrefl R \<Longrightarrow> irrefl (lexord R)"
--- a/src/HOL/ROOT Fri Apr 17 17:32:11 2020 +0200
+++ b/src/HOL/ROOT Fri Apr 17 20:55:53 2020 +0100
@@ -32,6 +32,7 @@
(*conflicting type class instantiations and dependent applications*)
Finite_Lattice
List_Lexorder
+ List_Lenlexorder
Prefix_Order
Product_Lexorder
Product_Order
--- a/src/HOL/Wellfounded.thy Fri Apr 17 17:32:11 2020 +0200
+++ b/src/HOL/Wellfounded.thy Fri Apr 17 20:55:53 2020 +0100
@@ -580,8 +580,8 @@
lemma less_than_iff [iff]: "((x,y) \<in> less_than) = (x<y)"
by (simp add: less_than_def less_eq)
-lemma total_less_than: "total less_than"
- using total_on_def by force
+lemma total_less_than: "total less_than" and total_on_less_than [simp]: "total_on A less_than"
+ using total_on_def by force+
lemma wf_less: "wf {(x, y::nat). x < y}"
by (rule Wellfounded.wellorder_class.wf)