src/HOL/Library/Dlist.thy
author haftmann
Wed Aug 18 11:55:27 2010 +0200 (2010-08-18)
changeset 38512 ed4703b416ed
parent 37765 26bdfb7b680b
child 38857 97775f3e8722
permissions -rw-r--r--
added equality instantiation
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@37473
     6
imports Main Fset
haftmann@35303
     7
begin
haftmann@35303
     8
haftmann@35303
     9
section {* The type of distinct lists *}
haftmann@35303
    10
haftmann@35303
    11
typedef (open) 'a dlist = "{xs::'a list. distinct xs}"
haftmann@35303
    12
  morphisms list_of_dlist Abs_dlist
haftmann@35303
    13
proof
haftmann@35303
    14
  show "[] \<in> ?dlist" by simp
haftmann@35303
    15
qed
haftmann@35303
    16
haftmann@36274
    17
lemma dlist_ext:
haftmann@38512
    18
  assumes "list_of_dlist dxs = list_of_dlist dys"
haftmann@38512
    19
  shows "dxs = dys"
haftmann@36274
    20
  using assms by (simp add: list_of_dlist_inject)
haftmann@36274
    21
haftmann@36112
    22
haftmann@35303
    23
text {* Formal, totalized constructor for @{typ "'a dlist"}: *}
haftmann@35303
    24
haftmann@35303
    25
definition Dlist :: "'a list \<Rightarrow> 'a dlist" where
haftmann@37765
    26
  "Dlist xs = Abs_dlist (remdups xs)"
haftmann@35303
    27
haftmann@35303
    28
lemma distinct_list_of_dlist [simp]:
haftmann@35303
    29
  "distinct (list_of_dlist dxs)"
haftmann@35303
    30
  using list_of_dlist [of dxs] by simp
haftmann@35303
    31
haftmann@35303
    32
lemma list_of_dlist_Dlist [simp]:
haftmann@35303
    33
  "list_of_dlist (Dlist xs) = remdups xs"
haftmann@35303
    34
  by (simp add: Dlist_def Abs_dlist_inverse)
haftmann@35303
    35
haftmann@36112
    36
lemma Dlist_list_of_dlist [simp, code abstype]:
haftmann@35303
    37
  "Dlist (list_of_dlist dxs) = dxs"
haftmann@35303
    38
  by (simp add: Dlist_def list_of_dlist_inverse distinct_remdups_id)
haftmann@35303
    39
haftmann@35303
    40
haftmann@35303
    41
text {* Fundamental operations: *}
haftmann@35303
    42
haftmann@35303
    43
definition empty :: "'a dlist" where
haftmann@35303
    44
  "empty = Dlist []"
haftmann@35303
    45
haftmann@35303
    46
definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
haftmann@35303
    47
  "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"
haftmann@35303
    48
haftmann@35303
    49
definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
haftmann@35303
    50
  "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"
haftmann@35303
    51
haftmann@35303
    52
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist" where
haftmann@35303
    53
  "map f dxs = Dlist (remdups (List.map f (list_of_dlist dxs)))"
haftmann@35303
    54
haftmann@35303
    55
definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
haftmann@35303
    56
  "filter P dxs = Dlist (List.filter P (list_of_dlist dxs))"
haftmann@35303
    57
haftmann@35303
    58
haftmann@35303
    59
text {* Derived operations: *}
haftmann@35303
    60
haftmann@35303
    61
definition null :: "'a dlist \<Rightarrow> bool" where
haftmann@35303
    62
  "null dxs = List.null (list_of_dlist dxs)"
haftmann@35303
    63
haftmann@35303
    64
definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where
haftmann@35303
    65
  "member dxs = List.member (list_of_dlist dxs)"
haftmann@35303
    66
haftmann@35303
    67
definition length :: "'a dlist \<Rightarrow> nat" where
haftmann@35303
    68
  "length dxs = List.length (list_of_dlist dxs)"
haftmann@35303
    69
haftmann@35303
    70
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@37022
    71
  "fold f dxs = More_List.fold f (list_of_dlist dxs)"
haftmann@37022
    72
haftmann@37022
    73
definition foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@37022
    74
  "foldr f dxs = List.foldr f (list_of_dlist dxs)"
haftmann@35303
    75
haftmann@35303
    76
haftmann@35303
    77
section {* Executable version obeying invariant *}
haftmann@35303
    78
haftmann@35303
    79
lemma list_of_dlist_empty [simp, code abstract]:
haftmann@35303
    80
  "list_of_dlist empty = []"
haftmann@35303
    81
  by (simp add: empty_def)
haftmann@35303
    82
haftmann@35303
    83
lemma list_of_dlist_insert [simp, code abstract]:
haftmann@35303
    84
  "list_of_dlist (insert x dxs) = List.insert x (list_of_dlist dxs)"
haftmann@35303
    85
  by (simp add: insert_def)
haftmann@35303
    86
haftmann@35303
    87
lemma list_of_dlist_remove [simp, code abstract]:
haftmann@35303
    88
  "list_of_dlist (remove x dxs) = remove1 x (list_of_dlist dxs)"
haftmann@35303
    89
  by (simp add: remove_def)
haftmann@35303
    90
haftmann@35303
    91
lemma list_of_dlist_map [simp, code abstract]:
haftmann@35303
    92
  "list_of_dlist (map f dxs) = remdups (List.map f (list_of_dlist dxs))"
haftmann@35303
    93
  by (simp add: map_def)
haftmann@35303
    94
haftmann@35303
    95
lemma list_of_dlist_filter [simp, code abstract]:
haftmann@35303
    96
  "list_of_dlist (filter P dxs) = List.filter P (list_of_dlist dxs)"
haftmann@35303
    97
  by (simp add: filter_def)
haftmann@35303
    98
haftmann@35303
    99
haftmann@36980
   100
text {* Explicit executable conversion *}
haftmann@36980
   101
haftmann@36980
   102
definition dlist_of_list [simp]:
haftmann@36980
   103
  "dlist_of_list = Dlist"
haftmann@36980
   104
haftmann@36980
   105
lemma [code abstract]:
haftmann@36980
   106
  "list_of_dlist (dlist_of_list xs) = remdups xs"
haftmann@36980
   107
  by simp
haftmann@36980
   108
haftmann@36980
   109
haftmann@38512
   110
text {* Equality *}
haftmann@38512
   111
haftmann@38512
   112
instantiation dlist :: (eq) eq
haftmann@38512
   113
begin
haftmann@38512
   114
haftmann@38512
   115
definition "HOL.eq dxs dys \<longleftrightarrow> HOL.eq (list_of_dlist dxs) (list_of_dlist dys)"
haftmann@38512
   116
haftmann@38512
   117
instance proof
haftmann@38512
   118
qed (simp add: eq_dlist_def eq list_of_dlist_inject)
haftmann@38512
   119
haftmann@38512
   120
end
haftmann@38512
   121
haftmann@38512
   122
haftmann@37106
   123
section {* Induction principle and case distinction *}
haftmann@37106
   124
haftmann@37106
   125
lemma dlist_induct [case_names empty insert, induct type: dlist]:
haftmann@37106
   126
  assumes empty: "P empty"
haftmann@37106
   127
  assumes insrt: "\<And>x dxs. \<not> member dxs x \<Longrightarrow> P dxs \<Longrightarrow> P (insert x dxs)"
haftmann@37106
   128
  shows "P dxs"
haftmann@37106
   129
proof (cases dxs)
haftmann@37106
   130
  case (Abs_dlist xs)
haftmann@37106
   131
  then have "distinct xs" and dxs: "dxs = Dlist xs" by (simp_all add: Dlist_def distinct_remdups_id)
haftmann@37106
   132
  from `distinct xs` have "P (Dlist xs)"
haftmann@37106
   133
  proof (induct xs rule: distinct_induct)
haftmann@37106
   134
    case Nil from empty show ?case by (simp add: empty_def)
haftmann@37106
   135
  next
haftmann@37106
   136
    case (insert x xs)
haftmann@37106
   137
    then have "\<not> member (Dlist xs) x" and "P (Dlist xs)"
haftmann@37595
   138
      by (simp_all add: member_def List.member_def)
haftmann@37106
   139
    with insrt have "P (insert x (Dlist xs))" .
haftmann@37106
   140
    with insert show ?case by (simp add: insert_def distinct_remdups_id)
haftmann@37106
   141
  qed
haftmann@37106
   142
  with dxs show "P dxs" by simp
haftmann@37106
   143
qed
haftmann@37106
   144
haftmann@37106
   145
lemma dlist_case [case_names empty insert, cases type: dlist]:
haftmann@37106
   146
  assumes empty: "dxs = empty \<Longrightarrow> P"
haftmann@37106
   147
  assumes insert: "\<And>x dys. \<not> member dys x \<Longrightarrow> dxs = insert x dys \<Longrightarrow> P"
haftmann@37106
   148
  shows P
haftmann@37106
   149
proof (cases dxs)
haftmann@37106
   150
  case (Abs_dlist xs)
haftmann@37106
   151
  then have dxs: "dxs = Dlist xs" and distinct: "distinct xs"
haftmann@37106
   152
    by (simp_all add: Dlist_def distinct_remdups_id)
haftmann@37106
   153
  show P proof (cases xs)
haftmann@37106
   154
    case Nil with dxs have "dxs = empty" by (simp add: empty_def) 
haftmann@37106
   155
    with empty show P .
haftmann@37106
   156
  next
haftmann@37106
   157
    case (Cons x xs)
haftmann@37106
   158
    with dxs distinct have "\<not> member (Dlist xs) x"
haftmann@37106
   159
      and "dxs = insert x (Dlist xs)"
haftmann@37595
   160
      by (simp_all add: member_def List.member_def insert_def distinct_remdups_id)
haftmann@37106
   161
    with insert show P .
haftmann@37106
   162
  qed
haftmann@37106
   163
qed
haftmann@37106
   164
haftmann@37106
   165
haftmann@35303
   166
section {* Implementation of sets by distinct lists -- canonical! *}
haftmann@35303
   167
haftmann@35303
   168
definition Set :: "'a dlist \<Rightarrow> 'a fset" where
haftmann@35303
   169
  "Set dxs = Fset.Set (list_of_dlist dxs)"
haftmann@35303
   170
haftmann@35303
   171
definition Coset :: "'a dlist \<Rightarrow> 'a fset" where
haftmann@35303
   172
  "Coset dxs = Fset.Coset (list_of_dlist dxs)"
haftmann@35303
   173
haftmann@35303
   174
code_datatype Set Coset
haftmann@35303
   175
haftmann@35303
   176
declare member_code [code del]
haftmann@35303
   177
declare is_empty_Set [code del]
haftmann@35303
   178
declare empty_Set [code del]
haftmann@35303
   179
declare UNIV_Set [code del]
haftmann@35303
   180
declare insert_Set [code del]
haftmann@35303
   181
declare remove_Set [code del]
haftmann@37029
   182
declare compl_Set [code del]
haftmann@37029
   183
declare compl_Coset [code del]
haftmann@35303
   184
declare map_Set [code del]
haftmann@35303
   185
declare filter_Set [code del]
haftmann@35303
   186
declare forall_Set [code del]
haftmann@35303
   187
declare exists_Set [code del]
haftmann@35303
   188
declare card_Set [code del]
haftmann@35303
   189
declare inter_project [code del]
haftmann@35303
   190
declare subtract_remove [code del]
haftmann@35303
   191
declare union_insert [code del]
haftmann@35303
   192
declare Infimum_inf [code del]
haftmann@35303
   193
declare Supremum_sup [code del]
haftmann@35303
   194
haftmann@35303
   195
lemma Set_Dlist [simp]:
haftmann@35303
   196
  "Set (Dlist xs) = Fset (set xs)"
haftmann@37473
   197
  by (rule fset_eqI) (simp add: Set_def)
haftmann@35303
   198
haftmann@35303
   199
lemma Coset_Dlist [simp]:
haftmann@35303
   200
  "Coset (Dlist xs) = Fset (- set xs)"
haftmann@37473
   201
  by (rule fset_eqI) (simp add: Coset_def)
haftmann@35303
   202
haftmann@35303
   203
lemma member_Set [simp]:
haftmann@35303
   204
  "Fset.member (Set dxs) = List.member (list_of_dlist dxs)"
haftmann@35303
   205
  by (simp add: Set_def member_set)
haftmann@35303
   206
haftmann@35303
   207
lemma member_Coset [simp]:
haftmann@35303
   208
  "Fset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
haftmann@35303
   209
  by (simp add: Coset_def member_set not_set_compl)
haftmann@35303
   210
haftmann@36980
   211
lemma Set_dlist_of_list [code]:
haftmann@36980
   212
  "Fset.Set xs = Set (dlist_of_list xs)"
haftmann@37473
   213
  by (rule fset_eqI) simp
haftmann@36980
   214
haftmann@36980
   215
lemma Coset_dlist_of_list [code]:
haftmann@36980
   216
  "Fset.Coset xs = Coset (dlist_of_list xs)"
haftmann@37473
   217
  by (rule fset_eqI) simp
haftmann@36980
   218
haftmann@35303
   219
lemma is_empty_Set [code]:
haftmann@35303
   220
  "Fset.is_empty (Set dxs) \<longleftrightarrow> null dxs"
haftmann@37595
   221
  by (simp add: null_def List.null_def member_set)
haftmann@35303
   222
haftmann@35303
   223
lemma bot_code [code]:
haftmann@35303
   224
  "bot = Set empty"
haftmann@35303
   225
  by (simp add: empty_def)
haftmann@35303
   226
haftmann@35303
   227
lemma top_code [code]:
haftmann@35303
   228
  "top = Coset empty"
haftmann@35303
   229
  by (simp add: empty_def)
haftmann@35303
   230
haftmann@35303
   231
lemma insert_code [code]:
haftmann@35303
   232
  "Fset.insert x (Set dxs) = Set (insert x dxs)"
haftmann@35303
   233
  "Fset.insert x (Coset dxs) = Coset (remove x dxs)"
haftmann@35303
   234
  by (simp_all add: insert_def remove_def member_set not_set_compl)
haftmann@35303
   235
haftmann@35303
   236
lemma remove_code [code]:
haftmann@35303
   237
  "Fset.remove x (Set dxs) = Set (remove x dxs)"
haftmann@35303
   238
  "Fset.remove x (Coset dxs) = Coset (insert x dxs)"
haftmann@35303
   239
  by (auto simp add: insert_def remove_def member_set not_set_compl)
haftmann@35303
   240
haftmann@35303
   241
lemma member_code [code]:
haftmann@35303
   242
  "Fset.member (Set dxs) = member dxs"
haftmann@35303
   243
  "Fset.member (Coset dxs) = Not \<circ> member dxs"
haftmann@35303
   244
  by (simp_all add: member_def)
haftmann@35303
   245
haftmann@37029
   246
lemma compl_code [code]:
haftmann@37029
   247
  "- Set dxs = Coset dxs"
haftmann@37029
   248
  "- Coset dxs = Set dxs"
haftmann@37473
   249
  by (rule fset_eqI, simp add: member_set not_set_compl)+
haftmann@37029
   250
haftmann@35303
   251
lemma map_code [code]:
haftmann@35303
   252
  "Fset.map f (Set dxs) = Set (map f dxs)"
haftmann@37473
   253
  by (rule fset_eqI) (simp add: member_set)
haftmann@35303
   254
  
haftmann@35303
   255
lemma filter_code [code]:
haftmann@35303
   256
  "Fset.filter f (Set dxs) = Set (filter f dxs)"
haftmann@37473
   257
  by (rule fset_eqI) (simp add: member_set)
haftmann@35303
   258
haftmann@35303
   259
lemma forall_Set [code]:
haftmann@35303
   260
  "Fset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
haftmann@35303
   261
  by (simp add: member_set list_all_iff)
haftmann@35303
   262
haftmann@35303
   263
lemma exists_Set [code]:
haftmann@35303
   264
  "Fset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
haftmann@35303
   265
  by (simp add: member_set list_ex_iff)
haftmann@35303
   266
haftmann@35303
   267
lemma card_code [code]:
haftmann@35303
   268
  "Fset.card (Set dxs) = length dxs"
haftmann@35303
   269
  by (simp add: length_def member_set distinct_card)
haftmann@35303
   270
haftmann@35303
   271
lemma inter_code [code]:
haftmann@35303
   272
  "inf A (Set xs) = Set (filter (Fset.member A) xs)"
haftmann@37022
   273
  "inf A (Coset xs) = foldr Fset.remove xs A"
haftmann@37022
   274
  by (simp_all only: Set_def Coset_def foldr_def inter_project list_of_dlist_filter)
haftmann@35303
   275
haftmann@35303
   276
lemma subtract_code [code]:
haftmann@37022
   277
  "A - Set xs = foldr Fset.remove xs A"
haftmann@35303
   278
  "A - Coset xs = Set (filter (Fset.member A) xs)"
haftmann@37022
   279
  by (simp_all only: Set_def Coset_def foldr_def subtract_remove list_of_dlist_filter)
haftmann@35303
   280
haftmann@35303
   281
lemma union_code [code]:
haftmann@37022
   282
  "sup (Set xs) A = foldr Fset.insert xs A"
haftmann@35303
   283
  "sup (Coset xs) A = Coset (filter (Not \<circ> Fset.member A) xs)"
haftmann@37022
   284
  by (simp_all only: Set_def Coset_def foldr_def union_insert list_of_dlist_filter)
haftmann@35303
   285
haftmann@35303
   286
context complete_lattice
haftmann@35303
   287
begin
haftmann@35303
   288
haftmann@35303
   289
lemma Infimum_code [code]:
haftmann@37022
   290
  "Infimum (Set As) = foldr inf As top"
haftmann@37022
   291
  by (simp only: Set_def Infimum_inf foldr_def inf.commute)
haftmann@35303
   292
haftmann@35303
   293
lemma Supremum_code [code]:
haftmann@37022
   294
  "Supremum (Set As) = foldr sup As bot"
haftmann@37022
   295
  by (simp only: Set_def Supremum_sup foldr_def sup.commute)
haftmann@35303
   296
haftmann@35303
   297
end
haftmann@35303
   298
haftmann@38512
   299
haftmann@37022
   300
hide_const (open) member fold foldr empty insert remove map filter null member length fold
haftmann@35303
   301
haftmann@35303
   302
end