src/HOL/Library/List_lexord.thy
author haftmann
Fri Oct 10 06:45:53 2008 +0200 (2008-10-10)
changeset 28562 4e74209f113e
parent 27682 25aceefd4786
child 30663 0b6aff7451b2
permissions -rw-r--r--
`code func` now just `code`
nipkow@15737
     1
(*  Title:      HOL/Library/List_lexord.thy
nipkow@15737
     2
    ID:         $Id$
nipkow@15737
     3
    Author:     Norbert Voelker
nipkow@15737
     4
*)
nipkow@15737
     5
wenzelm@17200
     6
header {* Lexicographic order on lists *}
nipkow@15737
     7
nipkow@15737
     8
theory List_lexord
haftmann@27487
     9
imports Plain "~~/src/HOL/List"
nipkow@15737
    10
begin
nipkow@15737
    11
haftmann@25764
    12
instantiation list :: (ord) ord
haftmann@25764
    13
begin
haftmann@25764
    14
haftmann@25764
    15
definition
haftmann@28562
    16
  list_less_def [code del]: "(xs::('a::ord) list) < ys \<longleftrightarrow> (xs, ys) \<in> lexord {(u,v). u < v}"
haftmann@25764
    17
haftmann@25764
    18
definition
haftmann@28562
    19
  list_le_def [code del]: "(xs::('a::ord) list) \<le> ys \<longleftrightarrow> (xs < ys \<or> xs = ys)"
haftmann@25764
    20
haftmann@25764
    21
instance ..
haftmann@25764
    22
haftmann@25764
    23
end
nipkow@15737
    24
wenzelm@17200
    25
instance list :: (order) order
haftmann@27682
    26
proof
haftmann@27682
    27
  fix xs :: "'a list"
haftmann@27682
    28
  show "xs \<le> xs" by (simp add: list_le_def)
haftmann@27682
    29
next
haftmann@27682
    30
  fix xs ys zs :: "'a list"
haftmann@27682
    31
  assume "xs \<le> ys" and "ys \<le> zs"
haftmann@27682
    32
  then show "xs \<le> zs" by (auto simp add: list_le_def list_less_def)
haftmann@27682
    33
    (rule lexord_trans, auto intro: transI)
haftmann@27682
    34
next
haftmann@27682
    35
  fix xs ys :: "'a list"
haftmann@27682
    36
  assume "xs \<le> ys" and "ys \<le> xs"
haftmann@27682
    37
  then show "xs = ys" apply (auto simp add: list_le_def list_less_def)
haftmann@27682
    38
  apply (rule lexord_irreflexive [THEN notE])
haftmann@27682
    39
  defer
haftmann@27682
    40
  apply (rule lexord_trans) apply (auto intro: transI) done
haftmann@27682
    41
next
haftmann@27682
    42
  fix xs ys :: "'a list"
haftmann@27682
    43
  show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs" 
haftmann@27682
    44
  apply (auto simp add: list_less_def list_le_def)
haftmann@27682
    45
  defer
haftmann@27682
    46
  apply (rule lexord_irreflexive [THEN notE])
haftmann@27682
    47
  apply auto
haftmann@27682
    48
  apply (rule lexord_irreflexive [THEN notE])
haftmann@27682
    49
  defer
haftmann@27682
    50
  apply (rule lexord_trans) apply (auto intro: transI) done
haftmann@27682
    51
qed
nipkow@15737
    52
haftmann@21458
    53
instance list :: (linorder) linorder
haftmann@27682
    54
proof
haftmann@27682
    55
  fix xs ys :: "'a list"
haftmann@27682
    56
  have "(xs, ys) \<in> lexord {(u, v). u < v} \<or> xs = ys \<or> (ys, xs) \<in> lexord {(u, v). u < v}"
haftmann@27682
    57
    by (rule lexord_linear) auto
haftmann@27682
    58
  then show "xs \<le> ys \<or> ys \<le> xs" 
haftmann@27682
    59
    by (auto simp add: list_le_def list_less_def)
haftmann@27682
    60
qed
nipkow@15737
    61
haftmann@25764
    62
instantiation list :: (linorder) distrib_lattice
haftmann@25764
    63
begin
haftmann@25764
    64
haftmann@25764
    65
definition
haftmann@28562
    66
  [code del]: "(inf \<Colon> 'a list \<Rightarrow> _) = min"
haftmann@25764
    67
haftmann@25764
    68
definition
haftmann@28562
    69
  [code del]: "(sup \<Colon> 'a list \<Rightarrow> _) = max"
haftmann@25764
    70
haftmann@25764
    71
instance
haftmann@22483
    72
  by intro_classes
haftmann@22483
    73
    (auto simp add: inf_list_def sup_list_def min_max.sup_inf_distrib1)
haftmann@22483
    74
haftmann@25764
    75
end
haftmann@25764
    76
haftmann@22177
    77
lemma not_less_Nil [simp]: "\<not> (x < [])"
wenzelm@17200
    78
  by (unfold list_less_def) simp
nipkow@15737
    79
haftmann@22177
    80
lemma Nil_less_Cons [simp]: "[] < a # x"
wenzelm@17200
    81
  by (unfold list_less_def) simp
nipkow@15737
    82
haftmann@22177
    83
lemma Cons_less_Cons [simp]: "a # x < b # y \<longleftrightarrow> a < b \<or> a = b \<and> x < y"
wenzelm@17200
    84
  by (unfold list_less_def) simp
nipkow@15737
    85
haftmann@22177
    86
lemma le_Nil [simp]: "x \<le> [] \<longleftrightarrow> x = []"
haftmann@25502
    87
  by (unfold list_le_def, cases x) auto
haftmann@22177
    88
haftmann@22177
    89
lemma Nil_le_Cons [simp]: "[] \<le> x"
haftmann@25502
    90
  by (unfold list_le_def, cases x) auto
nipkow@15737
    91
haftmann@22177
    92
lemma Cons_le_Cons [simp]: "a # x \<le> b # y \<longleftrightarrow> a < b \<or> a = b \<and> x \<le> y"
haftmann@25502
    93
  by (unfold list_le_def) auto
nipkow@15737
    94
haftmann@28562
    95
lemma less_code [code]:
haftmann@22177
    96
  "xs < ([]\<Colon>'a\<Colon>{eq, order} list) \<longleftrightarrow> False"
haftmann@22177
    97
  "[] < (x\<Colon>'a\<Colon>{eq, order}) # xs \<longleftrightarrow> True"
haftmann@22177
    98
  "(x\<Colon>'a\<Colon>{eq, order}) # xs < y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs < ys"
haftmann@22177
    99
  by simp_all
haftmann@22177
   100
haftmann@28562
   101
lemma less_eq_code [code]:
haftmann@22177
   102
  "x # xs \<le> ([]\<Colon>'a\<Colon>{eq, order} list) \<longleftrightarrow> False"
haftmann@22177
   103
  "[] \<le> (xs\<Colon>'a\<Colon>{eq, order} list) \<longleftrightarrow> True"
haftmann@22177
   104
  "(x\<Colon>'a\<Colon>{eq, order}) # xs \<le> y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs \<le> ys"
haftmann@22177
   105
  by simp_all
haftmann@22177
   106
wenzelm@17200
   107
end