src/HOL/Library/Executable_Set.thy
author bulwahn
Fri Oct 21 11:17:14 2011 +0200 (2011-10-21)
changeset 45231 d85a2fdc586c
parent 45186 645c6cac779e
child 45975 5e78c499a7ff
permissions -rw-r--r--
replacing code_inline by code_unfold, removing obsolete code_unfold, code_inline del now that the ancient code generator is removed
haftmann@34020
     1
(*  Title:      HOL/Library/Executable_Set.thy
haftmann@34020
     2
    Author:     Stefan Berghofer, TU Muenchen
haftmann@33947
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@33947
     4
*)
haftmann@33947
     5
haftmann@33947
     6
header {* A crude implementation of finite sets by lists -- avoid using this at any cost! *}
haftmann@33947
     7
haftmann@34020
     8
theory Executable_Set
haftmann@37024
     9
imports More_Set
haftmann@33947
    10
begin
haftmann@33947
    11
haftmann@39141
    12
text {*
haftmann@39141
    13
  This is just an ad-hoc hack which will rarely give you what you want.
haftmann@39141
    14
  For the moment, whenever you need executable sets, consider using
haftmann@43363
    15
  type @{text Cset.set} from theory @{text Cset}.
haftmann@39141
    16
*}
haftmann@39141
    17
haftmann@33947
    18
declare mem_def [code del]
haftmann@33947
    19
declare Collect_def [code del]
haftmann@33947
    20
declare insert_code [code del]
haftmann@33947
    21
declare vimage_code [code del]
haftmann@33947
    22
haftmann@33947
    23
subsection {* Set representation *}
haftmann@33947
    24
haftmann@33947
    25
setup {*
haftmann@33947
    26
  Code.add_type_cmd "set"
haftmann@33947
    27
*}
haftmann@33947
    28
haftmann@33947
    29
definition Set :: "'a list \<Rightarrow> 'a set" where
haftmann@33947
    30
  [simp]: "Set = set"
haftmann@33947
    31
haftmann@33947
    32
definition Coset :: "'a list \<Rightarrow> 'a set" where
haftmann@33947
    33
  [simp]: "Coset xs = - set xs"
haftmann@33947
    34
haftmann@33947
    35
setup {*
haftmann@33947
    36
  Code.add_signature_cmd ("Set", "'a list \<Rightarrow> 'a set")
haftmann@33947
    37
  #> Code.add_signature_cmd ("Coset", "'a list \<Rightarrow> 'a set")
haftmann@33947
    38
  #> Code.add_signature_cmd ("set", "'a list \<Rightarrow> 'a set")
haftmann@33947
    39
  #> Code.add_signature_cmd ("op \<in>", "'a \<Rightarrow> 'a set \<Rightarrow> bool")
haftmann@33947
    40
*}
haftmann@33947
    41
haftmann@33947
    42
code_datatype Set Coset
haftmann@33947
    43
haftmann@33947
    44
haftmann@33947
    45
subsection {* Basic operations *}
haftmann@33947
    46
haftmann@33947
    47
lemma [code]:
haftmann@33947
    48
  "set xs = Set (remdups xs)"
haftmann@33947
    49
  by simp
haftmann@33947
    50
haftmann@33947
    51
lemma [code]:
haftmann@37595
    52
  "x \<in> Set xs \<longleftrightarrow> List.member xs x"
haftmann@37595
    53
  "x \<in> Coset xs \<longleftrightarrow> \<not> List.member xs x"
haftmann@37595
    54
  by (simp_all add: member_def)
haftmann@33947
    55
haftmann@33947
    56
definition is_empty :: "'a set \<Rightarrow> bool" where
haftmann@33947
    57
  [simp]: "is_empty A \<longleftrightarrow> A = {}"
haftmann@33947
    58
haftmann@34020
    59
lemma [code_unfold]:
haftmann@33947
    60
  "A = {} \<longleftrightarrow> is_empty A"
haftmann@33947
    61
  by simp
haftmann@33947
    62
haftmann@33947
    63
definition empty :: "'a set" where
haftmann@33947
    64
  [simp]: "empty = {}"
haftmann@33947
    65
haftmann@34020
    66
lemma [code_unfold]:
haftmann@33947
    67
  "{} = empty"
haftmann@33947
    68
  by simp
haftmann@33947
    69
bulwahn@45231
    70
lemma
haftmann@34020
    71
  "empty = Set []"
haftmann@34020
    72
  by simp -- {* Otherwise @{text \<eta>}-expansion produces funny things. *}
haftmann@34020
    73
haftmann@33947
    74
setup {*
haftmann@33947
    75
  Code.add_signature_cmd ("is_empty", "'a set \<Rightarrow> bool")
haftmann@33947
    76
  #> Code.add_signature_cmd ("empty", "'a set")
haftmann@33947
    77
  #> Code.add_signature_cmd ("insert", "'a \<Rightarrow> 'a set \<Rightarrow> 'a set")
haftmann@37024
    78
  #> Code.add_signature_cmd ("More_Set.remove", "'a \<Rightarrow> 'a set \<Rightarrow> 'a set")
haftmann@33947
    79
  #> Code.add_signature_cmd ("image", "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set")
haftmann@37024
    80
  #> Code.add_signature_cmd ("More_Set.project", "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set")
haftmann@33947
    81
  #> Code.add_signature_cmd ("Ball", "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool")
haftmann@33947
    82
  #> Code.add_signature_cmd ("Bex", "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool")
haftmann@33947
    83
  #> Code.add_signature_cmd ("card", "'a set \<Rightarrow> nat")
haftmann@33947
    84
*}
haftmann@33947
    85
haftmann@33947
    86
lemma is_empty_Set [code]:
haftmann@37595
    87
  "is_empty (Set xs) \<longleftrightarrow> List.null xs"
haftmann@37595
    88
  by (simp add: null_def)
haftmann@33947
    89
haftmann@33947
    90
lemma empty_Set [code]:
haftmann@33947
    91
  "empty = Set []"
haftmann@33947
    92
  by simp
haftmann@33947
    93
haftmann@33947
    94
lemma insert_Set [code]:
haftmann@34980
    95
  "insert x (Set xs) = Set (List.insert x xs)"
haftmann@34980
    96
  "insert x (Coset xs) = Coset (removeAll x xs)"
haftmann@45012
    97
  by simp_all
haftmann@33947
    98
haftmann@33947
    99
lemma remove_Set [code]:
haftmann@34980
   100
  "remove x (Set xs) = Set (removeAll x xs)"
haftmann@34980
   101
  "remove x (Coset xs) = Coset (List.insert x xs)"
haftmann@45012
   102
  by (auto simp add: remove_def)
haftmann@33947
   103
haftmann@33947
   104
lemma image_Set [code]:
haftmann@33947
   105
  "image f (Set xs) = Set (remdups (map f xs))"
haftmann@33947
   106
  by simp
haftmann@33947
   107
haftmann@33947
   108
lemma project_Set [code]:
haftmann@33947
   109
  "project P (Set xs) = Set (filter P xs)"
haftmann@33947
   110
  by (simp add: project_set)
haftmann@33947
   111
haftmann@33947
   112
lemma Ball_Set [code]:
haftmann@33947
   113
  "Ball (Set xs) P \<longleftrightarrow> list_all P xs"
haftmann@37595
   114
  by (simp add: list_all_iff)
haftmann@33947
   115
haftmann@33947
   116
lemma Bex_Set [code]:
haftmann@33947
   117
  "Bex (Set xs) P \<longleftrightarrow> list_ex P xs"
haftmann@37595
   118
  by (simp add: list_ex_iff)
haftmann@33947
   119
bulwahn@45120
   120
lemma
bulwahn@45120
   121
  [code, code del]: "card S = card S" ..
bulwahn@45120
   122
haftmann@33947
   123
lemma card_Set [code]:
haftmann@33947
   124
  "card (Set xs) = length (remdups xs)"
haftmann@33947
   125
proof -
haftmann@33947
   126
  have "card (set (remdups xs)) = length (remdups xs)"
haftmann@33947
   127
    by (rule distinct_card) simp
haftmann@33947
   128
  then show ?thesis by simp
haftmann@33947
   129
qed
haftmann@33947
   130
haftmann@33947
   131
haftmann@33947
   132
subsection {* Derived operations *}
haftmann@33947
   133
haftmann@33947
   134
definition set_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@33947
   135
  [simp]: "set_eq = op ="
haftmann@33947
   136
haftmann@34020
   137
lemma [code_unfold]:
haftmann@33947
   138
  "op = = set_eq"
haftmann@33947
   139
  by simp
haftmann@33947
   140
haftmann@33947
   141
definition subset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@33947
   142
  [simp]: "subset_eq = op \<subseteq>"
haftmann@33947
   143
haftmann@34020
   144
lemma [code_unfold]:
haftmann@33947
   145
  "op \<subseteq> = subset_eq"
haftmann@33947
   146
  by simp
haftmann@33947
   147
haftmann@33947
   148
definition subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@33947
   149
  [simp]: "subset = op \<subset>"
haftmann@33947
   150
haftmann@34020
   151
lemma [code_unfold]:
haftmann@33947
   152
  "op \<subset> = subset"
haftmann@33947
   153
  by simp
haftmann@33947
   154
haftmann@33947
   155
setup {*
haftmann@33947
   156
  Code.add_signature_cmd ("set_eq", "'a set \<Rightarrow> 'a set \<Rightarrow> bool")
haftmann@33947
   157
  #> Code.add_signature_cmd ("subset_eq", "'a set \<Rightarrow> 'a set \<Rightarrow> bool")
haftmann@33947
   158
  #> Code.add_signature_cmd ("subset", "'a set \<Rightarrow> 'a set \<Rightarrow> bool")
haftmann@33947
   159
*}
haftmann@33947
   160
haftmann@33947
   161
lemma set_eq_subset_eq [code]:
haftmann@33947
   162
  "set_eq A B \<longleftrightarrow> subset_eq A B \<and> subset_eq B A"
haftmann@33947
   163
  by auto
haftmann@33947
   164
haftmann@33947
   165
lemma subset_eq_forall [code]:
haftmann@33947
   166
  "subset_eq A B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
haftmann@33947
   167
  by (simp add: subset_eq)
haftmann@33947
   168
haftmann@33947
   169
lemma subset_subset_eq [code]:
haftmann@33947
   170
  "subset A B \<longleftrightarrow> subset_eq A B \<and> \<not> subset_eq B A"
haftmann@33947
   171
  by (simp add: subset)
haftmann@33947
   172
haftmann@33947
   173
haftmann@33947
   174
subsection {* Functorial operations *}
haftmann@33947
   175
haftmann@33947
   176
definition inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@33947
   177
  [simp]: "inter = op \<inter>"
haftmann@33947
   178
haftmann@34020
   179
lemma [code_unfold]:
haftmann@33947
   180
  "op \<inter> = inter"
haftmann@33947
   181
  by simp
haftmann@33947
   182
haftmann@33947
   183
definition subtract :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@33947
   184
  [simp]: "subtract A B = B - A"
haftmann@33947
   185
haftmann@34020
   186
lemma [code_unfold]:
haftmann@33947
   187
  "B - A = subtract A B"
haftmann@33947
   188
  by simp
haftmann@33947
   189
haftmann@33947
   190
definition union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@33947
   191
  [simp]: "union = op \<union>"
haftmann@33947
   192
haftmann@34020
   193
lemma [code_unfold]:
haftmann@33947
   194
  "op \<union> = union"
haftmann@33947
   195
  by simp
haftmann@33947
   196
haftmann@33947
   197
definition Inf :: "'a::complete_lattice set \<Rightarrow> 'a" where
haftmann@44860
   198
  [simp]: "Inf = Complete_Lattices.Inf"
haftmann@33947
   199
haftmann@34020
   200
lemma [code_unfold]:
haftmann@44860
   201
  "Complete_Lattices.Inf = Inf"
haftmann@33947
   202
  by simp
haftmann@33947
   203
haftmann@33947
   204
definition Sup :: "'a::complete_lattice set \<Rightarrow> 'a" where
haftmann@44860
   205
  [simp]: "Sup = Complete_Lattices.Sup"
haftmann@33947
   206
haftmann@34020
   207
lemma [code_unfold]:
haftmann@44860
   208
  "Complete_Lattices.Sup = Sup"
haftmann@33947
   209
  by simp
haftmann@33947
   210
haftmann@33947
   211
definition Inter :: "'a set set \<Rightarrow> 'a set" where
haftmann@33947
   212
  [simp]: "Inter = Inf"
haftmann@33947
   213
haftmann@34020
   214
lemma [code_unfold]:
haftmann@33947
   215
  "Inf = Inter"
haftmann@33947
   216
  by simp
haftmann@33947
   217
haftmann@33947
   218
definition Union :: "'a set set \<Rightarrow> 'a set" where
haftmann@33947
   219
  [simp]: "Union = Sup"
haftmann@33947
   220
haftmann@34020
   221
lemma [code_unfold]:
haftmann@33947
   222
  "Sup = Union"
haftmann@33947
   223
  by simp
haftmann@33947
   224
haftmann@33947
   225
setup {*
haftmann@33947
   226
  Code.add_signature_cmd ("inter", "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set")
haftmann@33947
   227
  #> Code.add_signature_cmd ("subtract", "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set")
haftmann@33947
   228
  #> Code.add_signature_cmd ("union", "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set")
haftmann@33947
   229
  #> Code.add_signature_cmd ("Inf", "'a set \<Rightarrow> 'a")
haftmann@33947
   230
  #> Code.add_signature_cmd ("Sup", "'a set \<Rightarrow> 'a")
haftmann@33947
   231
  #> Code.add_signature_cmd ("Inter", "'a set set \<Rightarrow> 'a set")
haftmann@33947
   232
  #> Code.add_signature_cmd ("Union", "'a set set \<Rightarrow> 'a set")
haftmann@33947
   233
*}
haftmann@33947
   234
haftmann@33947
   235
lemma inter_project [code]:
haftmann@33947
   236
  "inter A (Set xs) = Set (List.filter (\<lambda>x. x \<in> A) xs)"
haftmann@37023
   237
  "inter A (Coset xs) = foldr remove xs A"
haftmann@37023
   238
  by (simp add: inter project_def) (simp add: Diff_eq [symmetric] minus_set_foldr)
haftmann@33947
   239
haftmann@33947
   240
lemma subtract_remove [code]:
haftmann@37023
   241
  "subtract (Set xs) A = foldr remove xs A"
haftmann@33947
   242
  "subtract (Coset xs) A = Set (List.filter (\<lambda>x. x \<in> A) xs)"
haftmann@37023
   243
  by (auto simp add: minus_set_foldr)
haftmann@33947
   244
haftmann@33947
   245
lemma union_insert [code]:
haftmann@37023
   246
  "union (Set xs) A = foldr insert xs A"
haftmann@33947
   247
  "union (Coset xs) A = Coset (List.filter (\<lambda>x. x \<notin> A) xs)"
haftmann@37023
   248
  by (auto simp add: union_set_foldr)
haftmann@33947
   249
haftmann@33947
   250
lemma Inf_inf [code]:
haftmann@37023
   251
  "Inf (Set xs) = foldr inf xs (top :: 'a::complete_lattice)"
haftmann@33947
   252
  "Inf (Coset []) = (bot :: 'a::complete_lattice)"
haftmann@45012
   253
  by (simp_all add: Inf_set_foldr)
haftmann@33947
   254
haftmann@33947
   255
lemma Sup_sup [code]:
haftmann@37023
   256
  "Sup (Set xs) = foldr sup xs (bot :: 'a::complete_lattice)"
haftmann@33947
   257
  "Sup (Coset []) = (top :: 'a::complete_lattice)"
haftmann@45012
   258
  by (simp_all add: Sup_set_foldr)
haftmann@33947
   259
haftmann@33947
   260
lemma Inter_inter [code]:
haftmann@37023
   261
  "Inter (Set xs) = foldr inter xs (Coset [])"
haftmann@33947
   262
  "Inter (Coset []) = empty"
haftmann@33947
   263
  unfolding Inter_def Inf_inf by simp_all
haftmann@33947
   264
haftmann@33947
   265
lemma Union_union [code]:
haftmann@37023
   266
  "Union (Set xs) = foldr union xs empty"
haftmann@33947
   267
  "Union (Coset []) = Coset []"
haftmann@33947
   268
  unfolding Union_def Sup_sup by simp_all
haftmann@33947
   269
wenzelm@36176
   270
hide_const (open) is_empty empty remove
haftmann@33947
   271
  set_eq subset_eq subset inter union subtract Inf Sup Inter Union
haftmann@33947
   272
haftmann@38304
   273
haftmann@38304
   274
subsection {* Operations on relations *}
haftmann@38304
   275
haftmann@38304
   276
text {* Initially contributed by Tjark Weber. *}
haftmann@38304
   277
haftmann@45012
   278
lemma [code]:
haftmann@38304
   279
  "Domain r = fst ` r"
haftmann@45012
   280
  by (fact Domain_fst)
haftmann@38304
   281
haftmann@45012
   282
lemma [code]:
haftmann@38304
   283
  "Range r = snd ` r"
haftmann@45012
   284
  by (fact Range_snd)
haftmann@38304
   285
haftmann@45012
   286
lemma [code]:
haftmann@45012
   287
  "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
haftmann@45012
   288
  by (fact trans_join)
haftmann@45012
   289
haftmann@45012
   290
lemma [code]:
haftmann@45012
   291
  "irrefl r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<noteq> y)"
haftmann@45012
   292
  by (fact irrefl_distinct)
haftmann@38304
   293
haftmann@45012
   294
lemma [code]:
haftmann@45012
   295
  "acyclic r \<longleftrightarrow> irrefl (r^+)"
haftmann@45012
   296
  by (fact acyclic_irrefl)
haftmann@38304
   297
haftmann@45012
   298
lemma [code]:
haftmann@45012
   299
  "More_Set.product (Set xs) (Set ys) = Set [(x, y). x \<leftarrow> xs, y \<leftarrow> ys]"
haftmann@45012
   300
  by (unfold Set_def) (fact product_code)
haftmann@38304
   301
haftmann@45012
   302
lemma [code]:
haftmann@45012
   303
  "Id_on (Set xs) = Set [(x, x). x \<leftarrow> xs]"
haftmann@45012
   304
  by (unfold Set_def) (fact Id_on_set)
haftmann@38304
   305
haftmann@45012
   306
lemma [code]:
haftmann@45012
   307
  "Set xys O Set yzs = Set ([(fst xy, snd yz). xy \<leftarrow> xys, yz \<leftarrow> yzs, snd xy = fst yz])"
haftmann@45012
   308
  by (unfold Set_def) (fact set_rel_comp)
haftmann@38304
   309
haftmann@45012
   310
lemma [code]:
haftmann@38304
   311
  "wf (Set xs) = acyclic (Set xs)"
haftmann@45012
   312
  by (unfold Set_def) (fact wf_set)
haftmann@38304
   313
haftmann@33947
   314
end