src/HOL/Library/List_lexord.thy
author Andreas Lochbihler
Wed Nov 20 12:11:12 2013 +0100 (2013-11-20)
changeset 54597 4af7c82463d3
parent 53214 bae01293f4dd
child 54599 17d76426c7da
permissions -rw-r--r--
no ord instance for String.literal in Haskell when list is also ordered lexicographically
nipkow@15737
     1
(*  Title:      HOL/Library/List_lexord.thy
nipkow@15737
     2
    Author:     Norbert Voelker
nipkow@15737
     3
*)
nipkow@15737
     4
wenzelm@17200
     5
header {* Lexicographic order on lists *}
nipkow@15737
     6
nipkow@15737
     7
theory List_lexord
wenzelm@53214
     8
imports Main
nipkow@15737
     9
begin
nipkow@15737
    10
haftmann@25764
    11
instantiation list :: (ord) ord
haftmann@25764
    12
begin
haftmann@25764
    13
haftmann@25764
    14
definition
haftmann@37474
    15
  list_less_def: "xs < ys \<longleftrightarrow> (xs, ys) \<in> lexord {(u, v). u < v}"
haftmann@25764
    16
haftmann@25764
    17
definition
haftmann@37474
    18
  list_le_def: "(xs :: _ list) \<le> ys \<longleftrightarrow> xs < ys \<or> xs = ys"
haftmann@25764
    19
haftmann@25764
    20
instance ..
haftmann@25764
    21
haftmann@25764
    22
end
nipkow@15737
    23
wenzelm@17200
    24
instance list :: (order) order
haftmann@27682
    25
proof
haftmann@27682
    26
  fix xs :: "'a list"
haftmann@27682
    27
  show "xs \<le> xs" by (simp add: list_le_def)
haftmann@27682
    28
next
haftmann@27682
    29
  fix xs ys zs :: "'a list"
haftmann@27682
    30
  assume "xs \<le> ys" and "ys \<le> zs"
wenzelm@53214
    31
  then show "xs \<le> zs"
wenzelm@53214
    32
    apply (auto simp add: list_le_def list_less_def)
wenzelm@53214
    33
    apply (rule lexord_trans)
wenzelm@53214
    34
    apply (auto intro: transI)
wenzelm@53214
    35
    done
haftmann@27682
    36
next
haftmann@27682
    37
  fix xs ys :: "'a list"
haftmann@27682
    38
  assume "xs \<le> ys" and "ys \<le> xs"
wenzelm@53214
    39
  then show "xs = ys"
wenzelm@53214
    40
    apply (auto simp add: list_le_def list_less_def)
wenzelm@53214
    41
    apply (rule lexord_irreflexive [THEN notE])
wenzelm@53214
    42
    defer
wenzelm@53214
    43
    apply (rule lexord_trans)
wenzelm@53214
    44
    apply (auto intro: transI)
wenzelm@53214
    45
    done
haftmann@27682
    46
next
haftmann@27682
    47
  fix xs ys :: "'a list"
wenzelm@53214
    48
  show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs"
wenzelm@53214
    49
    apply (auto simp add: list_less_def list_le_def)
wenzelm@53214
    50
    defer
wenzelm@53214
    51
    apply (rule lexord_irreflexive [THEN notE])
wenzelm@53214
    52
    apply auto
wenzelm@53214
    53
    apply (rule lexord_irreflexive [THEN notE])
wenzelm@53214
    54
    defer
wenzelm@53214
    55
    apply (rule lexord_trans)
wenzelm@53214
    56
    apply (auto intro: transI)
wenzelm@53214
    57
    done
haftmann@27682
    58
qed
nipkow@15737
    59
haftmann@21458
    60
instance list :: (linorder) linorder
haftmann@27682
    61
proof
haftmann@27682
    62
  fix xs ys :: "'a list"
haftmann@27682
    63
  have "(xs, ys) \<in> lexord {(u, v). u < v} \<or> xs = ys \<or> (ys, xs) \<in> lexord {(u, v). u < v}"
haftmann@27682
    64
    by (rule lexord_linear) auto
wenzelm@53214
    65
  then show "xs \<le> ys \<or> ys \<le> xs"
haftmann@27682
    66
    by (auto simp add: list_le_def list_less_def)
haftmann@27682
    67
qed
nipkow@15737
    68
haftmann@25764
    69
instantiation list :: (linorder) distrib_lattice
haftmann@25764
    70
begin
haftmann@25764
    71
wenzelm@53214
    72
definition "(inf \<Colon> 'a list \<Rightarrow> _) = min"
haftmann@25764
    73
wenzelm@53214
    74
definition "(sup \<Colon> 'a list \<Rightarrow> _) = max"
haftmann@25764
    75
haftmann@25764
    76
instance
wenzelm@53214
    77
  by default (auto simp add: inf_list_def sup_list_def min_max.sup_inf_distrib1)
haftmann@22483
    78
haftmann@25764
    79
end
haftmann@25764
    80
wenzelm@53214
    81
lemma not_less_Nil [simp]: "\<not> x < []"
wenzelm@53214
    82
  by (simp add: list_less_def)
nipkow@15737
    83
haftmann@22177
    84
lemma Nil_less_Cons [simp]: "[] < a # x"
wenzelm@53214
    85
  by (simp add: list_less_def)
nipkow@15737
    86
haftmann@22177
    87
lemma Cons_less_Cons [simp]: "a # x < b # y \<longleftrightarrow> a < b \<or> a = b \<and> x < y"
wenzelm@53214
    88
  by (simp add: list_less_def)
nipkow@15737
    89
haftmann@22177
    90
lemma le_Nil [simp]: "x \<le> [] \<longleftrightarrow> x = []"
wenzelm@53214
    91
  unfolding list_le_def by (cases x) auto
haftmann@22177
    92
haftmann@22177
    93
lemma Nil_le_Cons [simp]: "[] \<le> x"
wenzelm@53214
    94
  unfolding list_le_def by (cases x) auto
nipkow@15737
    95
haftmann@22177
    96
lemma Cons_le_Cons [simp]: "a # x \<le> b # y \<longleftrightarrow> a < b \<or> a = b \<and> x \<le> y"
wenzelm@53214
    97
  unfolding list_le_def by auto
nipkow@15737
    98
haftmann@52729
    99
instantiation list :: (order) order_bot
haftmann@37474
   100
begin
haftmann@37474
   101
wenzelm@53214
   102
definition "bot = []"
haftmann@37474
   103
wenzelm@53214
   104
instance
wenzelm@53214
   105
  by default (simp add: bot_list_def)
haftmann@37474
   106
haftmann@37474
   107
end
haftmann@37474
   108
haftmann@37474
   109
lemma less_list_code [code]:
haftmann@38857
   110
  "xs < ([]\<Colon>'a\<Colon>{equal, order} list) \<longleftrightarrow> False"
haftmann@38857
   111
  "[] < (x\<Colon>'a\<Colon>{equal, order}) # xs \<longleftrightarrow> True"
haftmann@38857
   112
  "(x\<Colon>'a\<Colon>{equal, order}) # xs < y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs < ys"
haftmann@22177
   113
  by simp_all
haftmann@22177
   114
haftmann@37474
   115
lemma less_eq_list_code [code]:
haftmann@38857
   116
  "x # xs \<le> ([]\<Colon>'a\<Colon>{equal, order} list) \<longleftrightarrow> False"
haftmann@38857
   117
  "[] \<le> (xs\<Colon>'a\<Colon>{equal, order} list) \<longleftrightarrow> True"
haftmann@38857
   118
  "(x\<Colon>'a\<Colon>{equal, order}) # xs \<le> y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs \<le> ys"
haftmann@22177
   119
  by simp_all
haftmann@22177
   120
Andreas@54597
   121
code_printing class_instance String.literal :: ord \<rightharpoonup> (Haskell) -
Andreas@54597
   122
wenzelm@17200
   123
end