src/HOL/Library/Dlist.thy
author haftmann
Wed May 19 09:20:36 2010 +0200 (2010-05-19)
changeset 36980 1a4cc941171a
parent 36274 42bd879dc1b0
child 37022 f9681d9d1d56
permissions -rw-r--r--
added implementations of Fset.Set, Fset.Coset; do not delete code equations for relational operators on fsets
haftmann@35303
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@35303
     2
haftmann@35303
     3
header {* Lists with elements distinct as canonical example for datatype invariants *}
haftmann@35303
     4
haftmann@35303
     5
theory Dlist
haftmann@35303
     6
imports Main Fset
haftmann@35303
     7
begin
haftmann@35303
     8
haftmann@35303
     9
section {* Prelude *}
haftmann@35303
    10
haftmann@35303
    11
text {* Without canonical argument order, higher-order things tend to get confusing quite fast: *}
haftmann@35303
    12
haftmann@35303
    13
setup {* Sign.map_naming (Name_Space.add_path "List") *}
haftmann@35303
    14
haftmann@35303
    15
primrec member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
haftmann@35303
    16
    "member [] y \<longleftrightarrow> False"
haftmann@35303
    17
  | "member (x#xs) y \<longleftrightarrow> x = y \<or> member xs y"
haftmann@35303
    18
haftmann@35303
    19
lemma member_set:
haftmann@35303
    20
  "member = set"
haftmann@35303
    21
proof (rule ext)+
haftmann@35303
    22
  fix xs :: "'a list" and x :: 'a
haftmann@35303
    23
  have "member xs x \<longleftrightarrow> x \<in> set xs" by (induct xs) auto
haftmann@35303
    24
  then show "member xs x = set xs x" by (simp add: mem_def)
haftmann@35303
    25
qed
haftmann@35303
    26
haftmann@35303
    27
lemma not_set_compl:
haftmann@35303
    28
  "Not \<circ> set xs = - set xs"
haftmann@35303
    29
  by (simp add: fun_Compl_def bool_Compl_def comp_def expand_fun_eq)
haftmann@35303
    30
haftmann@35303
    31
primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@35303
    32
    "fold f [] s = s"
haftmann@35303
    33
  | "fold f (x#xs) s = fold f xs (f x s)"
haftmann@35303
    34
haftmann@35303
    35
lemma foldl_fold:
haftmann@35303
    36
  "foldl f s xs = List.fold (\<lambda>x s. f s x) xs s"
haftmann@35303
    37
  by (induct xs arbitrary: s) simp_all
haftmann@35303
    38
haftmann@35303
    39
setup {* Sign.map_naming Name_Space.parent_path *}
haftmann@35303
    40
haftmann@35303
    41
haftmann@35303
    42
section {* The type of distinct lists *}
haftmann@35303
    43
haftmann@35303
    44
typedef (open) 'a dlist = "{xs::'a list. distinct xs}"
haftmann@35303
    45
  morphisms list_of_dlist Abs_dlist
haftmann@35303
    46
proof
haftmann@35303
    47
  show "[] \<in> ?dlist" by simp
haftmann@35303
    48
qed
haftmann@35303
    49
haftmann@36274
    50
lemma dlist_ext:
haftmann@36274
    51
  assumes "list_of_dlist xs = list_of_dlist ys"
haftmann@36274
    52
  shows "xs = ys"
haftmann@36274
    53
  using assms by (simp add: list_of_dlist_inject)
haftmann@36274
    54
haftmann@36112
    55
haftmann@35303
    56
text {* Formal, totalized constructor for @{typ "'a dlist"}: *}
haftmann@35303
    57
haftmann@35303
    58
definition Dlist :: "'a list \<Rightarrow> 'a dlist" where
haftmann@35303
    59
  [code del]: "Dlist xs = Abs_dlist (remdups xs)"
haftmann@35303
    60
haftmann@35303
    61
lemma distinct_list_of_dlist [simp]:
haftmann@35303
    62
  "distinct (list_of_dlist dxs)"
haftmann@35303
    63
  using list_of_dlist [of dxs] by simp
haftmann@35303
    64
haftmann@35303
    65
lemma list_of_dlist_Dlist [simp]:
haftmann@35303
    66
  "list_of_dlist (Dlist xs) = remdups xs"
haftmann@35303
    67
  by (simp add: Dlist_def Abs_dlist_inverse)
haftmann@35303
    68
haftmann@36112
    69
lemma Dlist_list_of_dlist [simp, code abstype]:
haftmann@35303
    70
  "Dlist (list_of_dlist dxs) = dxs"
haftmann@35303
    71
  by (simp add: Dlist_def list_of_dlist_inverse distinct_remdups_id)
haftmann@35303
    72
haftmann@35303
    73
haftmann@35303
    74
text {* Fundamental operations: *}
haftmann@35303
    75
haftmann@35303
    76
definition empty :: "'a dlist" where
haftmann@35303
    77
  "empty = Dlist []"
haftmann@35303
    78
haftmann@35303
    79
definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
haftmann@35303
    80
  "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"
haftmann@35303
    81
haftmann@35303
    82
definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
haftmann@35303
    83
  "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"
haftmann@35303
    84
haftmann@35303
    85
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist" where
haftmann@35303
    86
  "map f dxs = Dlist (remdups (List.map f (list_of_dlist dxs)))"
haftmann@35303
    87
haftmann@35303
    88
definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
haftmann@35303
    89
  "filter P dxs = Dlist (List.filter P (list_of_dlist dxs))"
haftmann@35303
    90
haftmann@35303
    91
haftmann@35303
    92
text {* Derived operations: *}
haftmann@35303
    93
haftmann@35303
    94
definition null :: "'a dlist \<Rightarrow> bool" where
haftmann@35303
    95
  "null dxs = List.null (list_of_dlist dxs)"
haftmann@35303
    96
haftmann@35303
    97
definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where
haftmann@35303
    98
  "member dxs = List.member (list_of_dlist dxs)"
haftmann@35303
    99
haftmann@35303
   100
definition length :: "'a dlist \<Rightarrow> nat" where
haftmann@35303
   101
  "length dxs = List.length (list_of_dlist dxs)"
haftmann@35303
   102
haftmann@35303
   103
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@35303
   104
  "fold f dxs = List.fold f (list_of_dlist dxs)"
haftmann@35303
   105
haftmann@35303
   106
haftmann@35303
   107
section {* Executable version obeying invariant *}
haftmann@35303
   108
haftmann@35303
   109
lemma list_of_dlist_empty [simp, code abstract]:
haftmann@35303
   110
  "list_of_dlist empty = []"
haftmann@35303
   111
  by (simp add: empty_def)
haftmann@35303
   112
haftmann@35303
   113
lemma list_of_dlist_insert [simp, code abstract]:
haftmann@35303
   114
  "list_of_dlist (insert x dxs) = List.insert x (list_of_dlist dxs)"
haftmann@35303
   115
  by (simp add: insert_def)
haftmann@35303
   116
haftmann@35303
   117
lemma list_of_dlist_remove [simp, code abstract]:
haftmann@35303
   118
  "list_of_dlist (remove x dxs) = remove1 x (list_of_dlist dxs)"
haftmann@35303
   119
  by (simp add: remove_def)
haftmann@35303
   120
haftmann@35303
   121
lemma list_of_dlist_map [simp, code abstract]:
haftmann@35303
   122
  "list_of_dlist (map f dxs) = remdups (List.map f (list_of_dlist dxs))"
haftmann@35303
   123
  by (simp add: map_def)
haftmann@35303
   124
haftmann@35303
   125
lemma list_of_dlist_filter [simp, code abstract]:
haftmann@35303
   126
  "list_of_dlist (filter P dxs) = List.filter P (list_of_dlist dxs)"
haftmann@35303
   127
  by (simp add: filter_def)
haftmann@35303
   128
haftmann@35303
   129
haftmann@36980
   130
text {* Explicit executable conversion *}
haftmann@36980
   131
haftmann@36980
   132
definition dlist_of_list [simp]:
haftmann@36980
   133
  "dlist_of_list = Dlist"
haftmann@36980
   134
haftmann@36980
   135
lemma [code abstract]:
haftmann@36980
   136
  "list_of_dlist (dlist_of_list xs) = remdups xs"
haftmann@36980
   137
  by simp
haftmann@36980
   138
haftmann@36980
   139
haftmann@35303
   140
section {* Implementation of sets by distinct lists -- canonical! *}
haftmann@35303
   141
haftmann@35303
   142
definition Set :: "'a dlist \<Rightarrow> 'a fset" where
haftmann@35303
   143
  "Set dxs = Fset.Set (list_of_dlist dxs)"
haftmann@35303
   144
haftmann@35303
   145
definition Coset :: "'a dlist \<Rightarrow> 'a fset" where
haftmann@35303
   146
  "Coset dxs = Fset.Coset (list_of_dlist dxs)"
haftmann@35303
   147
haftmann@35303
   148
code_datatype Set Coset
haftmann@35303
   149
haftmann@35303
   150
declare member_code [code del]
haftmann@35303
   151
declare is_empty_Set [code del]
haftmann@35303
   152
declare empty_Set [code del]
haftmann@35303
   153
declare UNIV_Set [code del]
haftmann@35303
   154
declare insert_Set [code del]
haftmann@35303
   155
declare remove_Set [code del]
haftmann@35303
   156
declare map_Set [code del]
haftmann@35303
   157
declare filter_Set [code del]
haftmann@35303
   158
declare forall_Set [code del]
haftmann@35303
   159
declare exists_Set [code del]
haftmann@35303
   160
declare card_Set [code del]
haftmann@35303
   161
declare inter_project [code del]
haftmann@35303
   162
declare subtract_remove [code del]
haftmann@35303
   163
declare union_insert [code del]
haftmann@35303
   164
declare Infimum_inf [code del]
haftmann@35303
   165
declare Supremum_sup [code del]
haftmann@35303
   166
haftmann@35303
   167
lemma Set_Dlist [simp]:
haftmann@35303
   168
  "Set (Dlist xs) = Fset (set xs)"
haftmann@35303
   169
  by (simp add: Set_def Fset.Set_def)
haftmann@35303
   170
haftmann@35303
   171
lemma Coset_Dlist [simp]:
haftmann@35303
   172
  "Coset (Dlist xs) = Fset (- set xs)"
haftmann@35303
   173
  by (simp add: Coset_def Fset.Coset_def)
haftmann@35303
   174
haftmann@35303
   175
lemma member_Set [simp]:
haftmann@35303
   176
  "Fset.member (Set dxs) = List.member (list_of_dlist dxs)"
haftmann@35303
   177
  by (simp add: Set_def member_set)
haftmann@35303
   178
haftmann@35303
   179
lemma member_Coset [simp]:
haftmann@35303
   180
  "Fset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
haftmann@35303
   181
  by (simp add: Coset_def member_set not_set_compl)
haftmann@35303
   182
haftmann@36980
   183
lemma Set_dlist_of_list [code]:
haftmann@36980
   184
  "Fset.Set xs = Set (dlist_of_list xs)"
haftmann@36980
   185
  by simp
haftmann@36980
   186
haftmann@36980
   187
lemma Coset_dlist_of_list [code]:
haftmann@36980
   188
  "Fset.Coset xs = Coset (dlist_of_list xs)"
haftmann@36980
   189
  by simp
haftmann@36980
   190
haftmann@35303
   191
lemma is_empty_Set [code]:
haftmann@35303
   192
  "Fset.is_empty (Set dxs) \<longleftrightarrow> null dxs"
haftmann@35303
   193
  by (simp add: null_def null_empty member_set)
haftmann@35303
   194
haftmann@35303
   195
lemma bot_code [code]:
haftmann@35303
   196
  "bot = Set empty"
haftmann@35303
   197
  by (simp add: empty_def)
haftmann@35303
   198
haftmann@35303
   199
lemma top_code [code]:
haftmann@35303
   200
  "top = Coset empty"
haftmann@35303
   201
  by (simp add: empty_def)
haftmann@35303
   202
haftmann@35303
   203
lemma insert_code [code]:
haftmann@35303
   204
  "Fset.insert x (Set dxs) = Set (insert x dxs)"
haftmann@35303
   205
  "Fset.insert x (Coset dxs) = Coset (remove x dxs)"
haftmann@35303
   206
  by (simp_all add: insert_def remove_def member_set not_set_compl)
haftmann@35303
   207
haftmann@35303
   208
lemma remove_code [code]:
haftmann@35303
   209
  "Fset.remove x (Set dxs) = Set (remove x dxs)"
haftmann@35303
   210
  "Fset.remove x (Coset dxs) = Coset (insert x dxs)"
haftmann@35303
   211
  by (auto simp add: insert_def remove_def member_set not_set_compl)
haftmann@35303
   212
haftmann@35303
   213
lemma member_code [code]:
haftmann@35303
   214
  "Fset.member (Set dxs) = member dxs"
haftmann@35303
   215
  "Fset.member (Coset dxs) = Not \<circ> member dxs"
haftmann@35303
   216
  by (simp_all add: member_def)
haftmann@35303
   217
haftmann@35303
   218
lemma map_code [code]:
haftmann@35303
   219
  "Fset.map f (Set dxs) = Set (map f dxs)"
haftmann@35303
   220
  by (simp add: member_set)
haftmann@35303
   221
  
haftmann@35303
   222
lemma filter_code [code]:
haftmann@35303
   223
  "Fset.filter f (Set dxs) = Set (filter f dxs)"
haftmann@35303
   224
  by (simp add: member_set)
haftmann@35303
   225
haftmann@35303
   226
lemma forall_Set [code]:
haftmann@35303
   227
  "Fset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
haftmann@35303
   228
  by (simp add: member_set list_all_iff)
haftmann@35303
   229
haftmann@35303
   230
lemma exists_Set [code]:
haftmann@35303
   231
  "Fset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
haftmann@35303
   232
  by (simp add: member_set list_ex_iff)
haftmann@35303
   233
haftmann@35303
   234
lemma card_code [code]:
haftmann@35303
   235
  "Fset.card (Set dxs) = length dxs"
haftmann@35303
   236
  by (simp add: length_def member_set distinct_card)
haftmann@35303
   237
haftmann@35303
   238
lemma foldl_list_of_dlist:
haftmann@35303
   239
  "foldl f s (list_of_dlist dxs) = fold (\<lambda>x s. f s x) dxs s"
haftmann@35303
   240
  by (simp add: foldl_fold fold_def)
haftmann@35303
   241
haftmann@35303
   242
lemma inter_code [code]:
haftmann@35303
   243
  "inf A (Set xs) = Set (filter (Fset.member A) xs)"
haftmann@35303
   244
  "inf A (Coset xs) = fold Fset.remove xs A"
haftmann@35303
   245
  by (simp_all only: Set_def Coset_def foldl_list_of_dlist inter_project list_of_dlist_filter)
haftmann@35303
   246
haftmann@35303
   247
lemma subtract_code [code]:
haftmann@35303
   248
  "A - Set xs = fold Fset.remove xs A"
haftmann@35303
   249
  "A - Coset xs = Set (filter (Fset.member A) xs)"
haftmann@35303
   250
  by (simp_all only: Set_def Coset_def foldl_list_of_dlist subtract_remove list_of_dlist_filter)
haftmann@35303
   251
haftmann@35303
   252
lemma union_code [code]:
haftmann@35303
   253
  "sup (Set xs) A = fold Fset.insert xs A"
haftmann@35303
   254
  "sup (Coset xs) A = Coset (filter (Not \<circ> Fset.member A) xs)"
haftmann@35303
   255
  by (simp_all only: Set_def Coset_def foldl_list_of_dlist union_insert list_of_dlist_filter)
haftmann@35303
   256
haftmann@35303
   257
context complete_lattice
haftmann@35303
   258
begin
haftmann@35303
   259
haftmann@35303
   260
lemma Infimum_code [code]:
haftmann@35303
   261
  "Infimum (Set As) = fold inf As top"
haftmann@35303
   262
  by (simp only: Set_def Infimum_inf foldl_list_of_dlist inf.commute)
haftmann@35303
   263
haftmann@35303
   264
lemma Supremum_code [code]:
haftmann@35303
   265
  "Supremum (Set As) = fold sup As bot"
haftmann@35303
   266
  by (simp only: Set_def Supremum_sup foldl_list_of_dlist sup.commute)
haftmann@35303
   267
haftmann@35303
   268
end
haftmann@35303
   269
wenzelm@36176
   270
hide_const (open) member fold empty insert remove map filter null member length fold
haftmann@35303
   271
haftmann@35303
   272
end