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