src/HOL/Library/List_Lexorder.thy
author paulson <lp15@cam.ac.uk>
Tue, 18 Aug 2020 21:45:24 +0100
changeset 72169 2d7619fc0e1a
parent 72166 bb37571139bf
child 72184 881bd98bddee
permissions -rw-r--r--
List_Lexorder finally working
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68312
e9b5f25f6712 canonical names
nipkow
parents: 61076
diff changeset
     1
(*  Title:      HOL/Library/List_Lexorder.thy
15737
nipkow
parents:
diff changeset
     2
    Author:     Norbert Voelker
nipkow
parents:
diff changeset
     3
*)
nipkow
parents:
diff changeset
     4
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
     5
section \<open>Lexicographic order on lists\<close>
15737
nipkow
parents:
diff changeset
     6
68312
e9b5f25f6712 canonical names
nipkow
parents: 61076
diff changeset
     7
theory List_Lexorder
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
     8
imports Main
15737
nipkow
parents:
diff changeset
     9
begin
nipkow
parents:
diff changeset
    10
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    11
instantiation list :: (ord) ord
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    12
begin
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    13
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    14
definition
37474
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    15
  list_less_def: "xs < ys \<longleftrightarrow> (xs, ys) \<in> lexord {(u, v). u < v}"
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    16
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    17
definition
37474
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    18
  list_le_def: "(xs :: _ list) \<le> ys \<longleftrightarrow> xs < ys \<or> xs = ys"
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    19
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    20
instance ..
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    21
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    22
end
15737
nipkow
parents:
diff changeset
    23
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15737
diff changeset
    24
instance list :: (order) order
27682
25aceefd4786 added class preorder
haftmann
parents: 27487
diff changeset
    25
proof
71766
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    26
  let ?r = "{(u, v::'a). u < v}"
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    27
  have tr: "trans ?r"
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    28
    using trans_def by fastforce
72166
bb37571139bf fixed for new lex-order. And the effing indentation!
paulson <lp15@cam.ac.uk>
parents: 71766
diff changeset
    29
  have *: "antisym {(u, v::'a). u < v}"
bb37571139bf fixed for new lex-order. And the effing indentation!
paulson <lp15@cam.ac.uk>
parents: 71766
diff changeset
    30
    using antisym_def by fastforce
71766
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    31
  have \<section>: False
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    32
    if "(xs,ys) \<in> lexord ?r" "(ys,xs) \<in> lexord ?r" for xs ys :: "'a list"
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    33
  proof -
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    34
    have "(xs,xs) \<in> lexord ?r"
72166
bb37571139bf fixed for new lex-order. And the effing indentation!
paulson <lp15@cam.ac.uk>
parents: 71766
diff changeset
    35
      using lexord_trans that tr * by blast
71766
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    36
    then show False
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    37
      by (meson case_prodD lexord_irreflexive less_irrefl mem_Collect_eq)
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    38
  qed
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    39
  show "xs \<le> xs" for xs :: "'a list" by (simp add: list_le_def)
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    40
  show "xs \<le> zs" if "xs \<le> ys" and "ys \<le> zs" for xs ys zs :: "'a list"
72166
bb37571139bf fixed for new lex-order. And the effing indentation!
paulson <lp15@cam.ac.uk>
parents: 71766
diff changeset
    41
    using that transD [OF lexord_transI [OF tr]] *
bb37571139bf fixed for new lex-order. And the effing indentation!
paulson <lp15@cam.ac.uk>
parents: 71766
diff changeset
    42
    by (auto simp add: list_le_def list_less_def)
71766
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    43
  show "xs = ys" if "xs \<le> ys" "ys \<le> xs" for xs ys :: "'a list"
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    44
    using \<section> that list_le_def list_less_def by blast
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    45
  show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs" for xs ys :: "'a list"
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    46
    by (auto simp add: list_less_def list_le_def dest: \<section>)
27682
25aceefd4786 added class preorder
haftmann
parents: 27487
diff changeset
    47
qed
15737
nipkow
parents:
diff changeset
    48
21458
475b321982f7 added code lemmas
haftmann
parents: 17200
diff changeset
    49
instance list :: (linorder) linorder
27682
25aceefd4786 added class preorder
haftmann
parents: 27487
diff changeset
    50
proof
25aceefd4786 added class preorder
haftmann
parents: 27487
diff changeset
    51
  fix xs ys :: "'a list"
71766
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    52
  have "total (lexord {(u, v::'a). u < v})"
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    53
    by (rule total_lexord) (auto simp: total_on_def)
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    54
  then show "xs \<le> ys \<or> ys \<le> xs"
71766
1249b998e377 New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents: 68312
diff changeset
    55
    by (auto simp add: total_on_def list_le_def list_less_def)
27682
25aceefd4786 added class preorder
haftmann
parents: 27487
diff changeset
    56
qed
15737
nipkow
parents:
diff changeset
    57
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    58
instantiation list :: (linorder) distrib_lattice
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    59
begin
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    60
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
    61
definition "(inf :: 'a list \<Rightarrow> _) = min"
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    62
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
    63
definition "(sup :: 'a list \<Rightarrow> _) = max"
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    64
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    65
instance
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60500
diff changeset
    66
  by standard (auto simp add: inf_list_def sup_list_def max_min_distrib2)
22483
86064f2f2188 added instance for lattice
haftmann
parents: 22316
diff changeset
    67
25764
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    68
end
878c37886eed removed some legacy instantiations
haftmann
parents: 25595
diff changeset
    69
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    70
lemma not_less_Nil [simp]: "\<not> x < []"
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    71
  by (simp add: list_less_def)
15737
nipkow
parents:
diff changeset
    72
22177
515021e98684 improved
haftmann
parents: 21458
diff changeset
    73
lemma Nil_less_Cons [simp]: "[] < a # x"
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    74
  by (simp add: list_less_def)
15737
nipkow
parents:
diff changeset
    75
72166
bb37571139bf fixed for new lex-order. And the effing indentation!
paulson <lp15@cam.ac.uk>
parents: 71766
diff changeset
    76
lemma Cons_less_Cons [simp]: "a # x < b # y \<longleftrightarrow> (if a = b then x < y else a < b)"
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    77
  by (simp add: list_less_def)
15737
nipkow
parents:
diff changeset
    78
22177
515021e98684 improved
haftmann
parents: 21458
diff changeset
    79
lemma le_Nil [simp]: "x \<le> [] \<longleftrightarrow> x = []"
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    80
  unfolding list_le_def by (cases x) auto
22177
515021e98684 improved
haftmann
parents: 21458
diff changeset
    81
515021e98684 improved
haftmann
parents: 21458
diff changeset
    82
lemma Nil_le_Cons [simp]: "[] \<le> x"
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    83
  unfolding list_le_def by (cases x) auto
15737
nipkow
parents:
diff changeset
    84
72169
2d7619fc0e1a List_Lexorder finally working
paulson <lp15@cam.ac.uk>
parents: 72166
diff changeset
    85
lemma Cons_le_Cons [simp]: "a # x \<le> b # y \<longleftrightarrow> (if a = b then x \<le> y else a < b)"
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    86
  unfolding list_le_def by auto
15737
nipkow
parents:
diff changeset
    87
52729
412c9e0381a1 factored syntactic type classes for bot and top (by Alessandro Coglio)
haftmann
parents: 38857
diff changeset
    88
instantiation list :: (order) order_bot
37474
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    89
begin
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    90
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    91
definition "bot = []"
37474
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    92
53214
bae01293f4dd tuned imports;
wenzelm
parents: 52729
diff changeset
    93
instance
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60500
diff changeset
    94
  by standard (simp add: bot_list_def)
37474
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    95
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    96
end
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    97
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
    98
lemma less_list_code [code]:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
    99
  "xs < ([]::'a::{equal, order} list) \<longleftrightarrow> False"
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   100
  "[] < (x::'a::{equal, order}) # xs \<longleftrightarrow> True"
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   101
  "(x::'a::{equal, order}) # xs < y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs < ys"
22177
515021e98684 improved
haftmann
parents: 21458
diff changeset
   102
  by simp_all
515021e98684 improved
haftmann
parents: 21458
diff changeset
   103
37474
ce943f9edf5e added bot instances; tuned
haftmann
parents: 30663
diff changeset
   104
lemma less_eq_list_code [code]:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   105
  "x # xs \<le> ([]::'a::{equal, order} list) \<longleftrightarrow> False"
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   106
  "[] \<le> (xs::'a::{equal, order} list) \<longleftrightarrow> True"
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   107
  "(x::'a::{equal, order}) # xs \<le> y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs \<le> ys"
22177
515021e98684 improved
haftmann
parents: 21458
diff changeset
   108
  by simp_all
515021e98684 improved
haftmann
parents: 21458
diff changeset
   109
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15737
diff changeset
   110
end