src/HOL/Library/RBT_Set.thy
author nipkow
Mon Oct 17 11:46:22 2016 +0200 (2016-10-17)
changeset 64267 b9a1486e79be
parent 63649 e690d6f2185b
child 64272 f76b6dda2e56
permissions -rw-r--r--
setsum -> sum
kuncar@48623
     1
(*  Title:      HOL/Library/RBT_Set.thy
kuncar@48623
     2
    Author:     Ondrej Kuncar
kuncar@48623
     3
*)
kuncar@48623
     4
wenzelm@60500
     5
section \<open>Implementation of sets using RBT trees\<close>
kuncar@48623
     6
kuncar@48623
     7
theory RBT_Set
haftmann@51115
     8
imports RBT Product_Lexorder
kuncar@48623
     9
begin
kuncar@48623
    10
kuncar@48623
    11
(*
kuncar@48623
    12
  Users should be aware that by including this file all code equations
blanchet@58301
    13
  outside of List.thy using 'a list as an implementation of sets cannot be
kuncar@48623
    14
  used for code generation. If such equations are not needed, they can be
kuncar@48623
    15
  deleted from the code generator. Otherwise, a user has to provide their 
kuncar@48623
    16
  own equations using RBT trees. 
kuncar@48623
    17
*)
kuncar@48623
    18
wenzelm@60500
    19
section \<open>Definition of code datatype constructors\<close>
kuncar@48623
    20
wenzelm@61076
    21
definition Set :: "('a::linorder, unit) rbt \<Rightarrow> 'a set" 
kuncar@56019
    22
  where "Set t = {x . RBT.lookup t x = Some ()}"
kuncar@48623
    23
wenzelm@61076
    24
definition Coset :: "('a::linorder, unit) rbt \<Rightarrow> 'a set" 
kuncar@48623
    25
  where [simp]: "Coset t = - Set t"
kuncar@48623
    26
kuncar@48623
    27
wenzelm@60500
    28
section \<open>Deletion of already existing code equations\<close>
kuncar@48623
    29
kuncar@48623
    30
lemma [code, code del]:
kuncar@48623
    31
  "Set.empty = Set.empty" ..
kuncar@48623
    32
kuncar@48623
    33
lemma [code, code del]:
kuncar@48623
    34
  "Set.is_empty = Set.is_empty" ..
kuncar@48623
    35
kuncar@48623
    36
lemma [code, code del]:
kuncar@48623
    37
  "uminus_set_inst.uminus_set = uminus_set_inst.uminus_set" ..
kuncar@48623
    38
kuncar@48623
    39
lemma [code, code del]:
kuncar@48623
    40
  "Set.member = Set.member" ..
kuncar@48623
    41
kuncar@48623
    42
lemma [code, code del]:
kuncar@48623
    43
  "Set.insert = Set.insert" ..
kuncar@48623
    44
kuncar@48623
    45
lemma [code, code del]:
kuncar@48623
    46
  "Set.remove = Set.remove" ..
kuncar@48623
    47
kuncar@48623
    48
lemma [code, code del]:
kuncar@48623
    49
  "UNIV = UNIV" ..
kuncar@48623
    50
kuncar@48623
    51
lemma [code, code del]:
kuncar@49757
    52
  "Set.filter = Set.filter" ..
kuncar@48623
    53
kuncar@48623
    54
lemma [code, code del]:
kuncar@48623
    55
  "image = image" ..
kuncar@48623
    56
kuncar@48623
    57
lemma [code, code del]:
kuncar@48623
    58
  "Set.subset_eq = Set.subset_eq" ..
kuncar@48623
    59
kuncar@48623
    60
lemma [code, code del]:
kuncar@48623
    61
  "Ball = Ball" ..
kuncar@48623
    62
kuncar@48623
    63
lemma [code, code del]:
kuncar@48623
    64
  "Bex = Bex" ..
kuncar@48623
    65
haftmann@49948
    66
lemma [code, code del]:
haftmann@49948
    67
  "can_select = can_select" ..
haftmann@49948
    68
kuncar@48623
    69
lemma [code, code del]:
kuncar@48623
    70
  "Set.union = Set.union" ..
kuncar@48623
    71
kuncar@48623
    72
lemma [code, code del]:
kuncar@48623
    73
  "minus_set_inst.minus_set = minus_set_inst.minus_set" ..
kuncar@48623
    74
kuncar@48623
    75
lemma [code, code del]:
kuncar@48623
    76
  "Set.inter = Set.inter" ..
kuncar@48623
    77
kuncar@48623
    78
lemma [code, code del]:
kuncar@48623
    79
  "card = card" ..
kuncar@48623
    80
kuncar@48623
    81
lemma [code, code del]:
kuncar@48623
    82
  "the_elem = the_elem" ..
kuncar@48623
    83
kuncar@48623
    84
lemma [code, code del]:
kuncar@48623
    85
  "Pow = Pow" ..
kuncar@48623
    86
kuncar@48623
    87
lemma [code, code del]:
nipkow@64267
    88
  "sum = sum" ..
kuncar@48623
    89
kuncar@48623
    90
lemma [code, code del]:
haftmann@58368
    91
  "setprod = setprod" ..
haftmann@58368
    92
haftmann@58368
    93
lemma [code, code del]:
kuncar@48623
    94
  "Product_Type.product = Product_Type.product"  ..
kuncar@48623
    95
kuncar@48623
    96
lemma [code, code del]:
kuncar@48623
    97
  "Id_on = Id_on" ..
kuncar@48623
    98
kuncar@48623
    99
lemma [code, code del]:
kuncar@48623
   100
  "Image = Image" ..
kuncar@48623
   101
kuncar@48623
   102
lemma [code, code del]:
kuncar@48623
   103
  "trancl = trancl" ..
kuncar@48623
   104
kuncar@48623
   105
lemma [code, code del]:
kuncar@48623
   106
  "relcomp = relcomp" ..
kuncar@48623
   107
kuncar@48623
   108
lemma [code, code del]:
kuncar@48623
   109
  "wf = wf" ..
kuncar@48623
   110
kuncar@48623
   111
lemma [code, code del]:
kuncar@48623
   112
  "Min = Min" ..
kuncar@48623
   113
kuncar@48623
   114
lemma [code, code del]:
kuncar@48623
   115
  "Inf_fin = Inf_fin" ..
kuncar@48623
   116
kuncar@48623
   117
lemma [code, code del]:
haftmann@56218
   118
  "INFIMUM = INFIMUM" ..
kuncar@48623
   119
kuncar@48623
   120
lemma [code, code del]:
kuncar@48623
   121
  "Max = Max" ..
kuncar@48623
   122
kuncar@48623
   123
lemma [code, code del]:
kuncar@48623
   124
  "Sup_fin = Sup_fin" ..
kuncar@48623
   125
kuncar@48623
   126
lemma [code, code del]:
haftmann@56218
   127
  "SUPREMUM = SUPREMUM" ..
kuncar@48623
   128
kuncar@48623
   129
lemma [code, code del]:
kuncar@48623
   130
  "(Inf :: 'a set set \<Rightarrow> 'a set) = Inf" ..
kuncar@48623
   131
kuncar@48623
   132
lemma [code, code del]:
kuncar@48623
   133
  "(Sup :: 'a set set \<Rightarrow> 'a set) = Sup" ..
kuncar@48623
   134
kuncar@48623
   135
lemma [code, code del]:
kuncar@48623
   136
  "sorted_list_of_set = sorted_list_of_set" ..
kuncar@48623
   137
kuncar@48623
   138
lemma [code, code del]: 
kuncar@48623
   139
  "List.map_project = List.map_project" ..
kuncar@48623
   140
nipkow@53955
   141
lemma [code, code del]: 
nipkow@53955
   142
  "List.Bleast = List.Bleast" ..
nipkow@53955
   143
wenzelm@60500
   144
section \<open>Lemmas\<close>
kuncar@48623
   145
kuncar@48623
   146
wenzelm@60500
   147
subsection \<open>Auxiliary lemmas\<close>
kuncar@48623
   148
kuncar@48623
   149
lemma [simp]: "x \<noteq> Some () \<longleftrightarrow> x = None"
kuncar@48623
   150
by (auto simp: not_Some_eq[THEN iffD1])
kuncar@48623
   151
kuncar@56019
   152
lemma Set_set_keys: "Set x = dom (RBT.lookup x)" 
kuncar@49928
   153
by (auto simp: Set_def)
kuncar@49928
   154
kuncar@48623
   155
lemma finite_Set [simp, intro!]: "finite (Set x)"
kuncar@49928
   156
by (simp add: Set_set_keys)
kuncar@48623
   157
kuncar@56019
   158
lemma set_keys: "Set t = set(RBT.keys t)"
kuncar@49928
   159
by (simp add: Set_set_keys lookup_keys)
kuncar@48623
   160
wenzelm@60500
   161
subsection \<open>fold and filter\<close>
kuncar@48623
   162
kuncar@48623
   163
lemma finite_fold_rbt_fold_eq:
kuncar@48623
   164
  assumes "comp_fun_commute f" 
kuncar@56019
   165
  shows "Finite_Set.fold f A (set (RBT.entries t)) = RBT.fold (curry f) t A"
kuncar@48623
   166
proof -
kuncar@56019
   167
  have *: "remdups (RBT.entries t) = RBT.entries t"
kuncar@48623
   168
    using distinct_entries distinct_map by (auto intro: distinct_remdups_id)
kuncar@48623
   169
  show ?thesis using assms by (auto simp: fold_def_alt comp_fun_commute.fold_set_fold_remdups *)
kuncar@48623
   170
qed
kuncar@48623
   171
kuncar@48623
   172
definition fold_keys :: "('a :: linorder \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, _) rbt \<Rightarrow> 'b \<Rightarrow> 'b" 
kuncar@56019
   173
  where [code_unfold]:"fold_keys f t A = RBT.fold (\<lambda>k _ t. f k t) t A"
kuncar@48623
   174
kuncar@48623
   175
lemma fold_keys_def_alt:
kuncar@56019
   176
  "fold_keys f t s = List.fold f (RBT.keys t) s"
kuncar@48623
   177
by (auto simp: fold_map o_def split_def fold_def_alt keys_def_alt fold_keys_def)
kuncar@48623
   178
kuncar@48623
   179
lemma finite_fold_fold_keys:
kuncar@48623
   180
  assumes "comp_fun_commute f"
kuncar@48623
   181
  shows "Finite_Set.fold f A (Set t) = fold_keys f t A"
kuncar@48623
   182
using assms
kuncar@48623
   183
proof -
kuncar@48623
   184
  interpret comp_fun_commute f by fact
kuncar@56019
   185
  have "set (RBT.keys t) = fst ` (set (RBT.entries t))" by (auto simp: fst_eq_Domain keys_entries)
kuncar@56019
   186
  moreover have "inj_on fst (set (RBT.entries t))" using distinct_entries distinct_map by auto
kuncar@48623
   187
  ultimately show ?thesis 
kuncar@48623
   188
    by (auto simp add: set_keys fold_keys_def curry_def fold_image finite_fold_rbt_fold_eq 
kuncar@48623
   189
      comp_comp_fun_commute)
kuncar@48623
   190
qed
kuncar@48623
   191
kuncar@48623
   192
definition rbt_filter :: "('a :: linorder \<Rightarrow> bool) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'a set" where
kuncar@56019
   193
  "rbt_filter P t = RBT.fold (\<lambda>k _ A'. if P k then Set.insert k A' else A') t {}"
kuncar@48623
   194
kuncar@49758
   195
lemma Set_filter_rbt_filter:
kuncar@49758
   196
  "Set.filter P (Set t) = rbt_filter P t"
kuncar@49758
   197
by (simp add: fold_keys_def Set_filter_fold rbt_filter_def 
kuncar@48623
   198
  finite_fold_fold_keys[OF comp_fun_commute_filter_fold])
kuncar@48623
   199
kuncar@48623
   200
wenzelm@60500
   201
subsection \<open>foldi and Ball\<close>
kuncar@48623
   202
kuncar@48623
   203
lemma Ball_False: "RBT_Impl.fold (\<lambda>k v s. s \<and> P k) t False = False"
kuncar@48623
   204
by (induction t) auto
kuncar@48623
   205
kuncar@48623
   206
lemma rbt_foldi_fold_conj: 
kuncar@48623
   207
  "RBT_Impl.foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t val = RBT_Impl.fold (\<lambda>k v s. s \<and> P k) t val"
kuncar@48623
   208
proof (induction t arbitrary: val) 
kuncar@48623
   209
  case (Branch c t1) then show ?case
kuncar@48623
   210
    by (cases "RBT_Impl.fold (\<lambda>k v s. s \<and> P k) t1 True") (simp_all add: Ball_False) 
kuncar@48623
   211
qed simp
kuncar@48623
   212
kuncar@56019
   213
lemma foldi_fold_conj: "RBT.foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t val = fold_keys (\<lambda>k s. s \<and> P k) t val"
kuncar@56019
   214
unfolding fold_keys_def including rbt.lifting by transfer (rule rbt_foldi_fold_conj)
kuncar@48623
   215
kuncar@48623
   216
wenzelm@60500
   217
subsection \<open>foldi and Bex\<close>
kuncar@48623
   218
kuncar@48623
   219
lemma Bex_True: "RBT_Impl.fold (\<lambda>k v s. s \<or> P k) t True = True"
kuncar@48623
   220
by (induction t) auto
kuncar@48623
   221
kuncar@48623
   222
lemma rbt_foldi_fold_disj: 
kuncar@48623
   223
  "RBT_Impl.foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t val = RBT_Impl.fold (\<lambda>k v s. s \<or> P k) t val"
kuncar@48623
   224
proof (induction t arbitrary: val) 
kuncar@48623
   225
  case (Branch c t1) then show ?case
kuncar@48623
   226
    by (cases "RBT_Impl.fold (\<lambda>k v s. s \<or> P k) t1 False") (simp_all add: Bex_True) 
kuncar@48623
   227
qed simp
kuncar@48623
   228
kuncar@56019
   229
lemma foldi_fold_disj: "RBT.foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t val = fold_keys (\<lambda>k s. s \<or> P k) t val"
kuncar@56019
   230
unfolding fold_keys_def including rbt.lifting by transfer (rule rbt_foldi_fold_disj)
kuncar@48623
   231
kuncar@48623
   232
wenzelm@60500
   233
subsection \<open>folding over non empty trees and selecting the minimal and maximal element\<close>
kuncar@48623
   234
kuncar@48623
   235
(** concrete **)
kuncar@48623
   236
kuncar@48623
   237
(* The concrete part is here because it's probably not general enough to be moved to RBT_Impl *)
kuncar@48623
   238
kuncar@48623
   239
definition rbt_fold1_keys :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a::linorder, 'b) RBT_Impl.rbt \<Rightarrow> 'a" 
kuncar@48623
   240
  where "rbt_fold1_keys f t = List.fold f (tl(RBT_Impl.keys t)) (hd(RBT_Impl.keys t))"
kuncar@48623
   241
kuncar@48623
   242
(* minimum *)
kuncar@48623
   243
kuncar@48623
   244
definition rbt_min :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
kuncar@48623
   245
  where "rbt_min t = rbt_fold1_keys min t"
kuncar@48623
   246
kuncar@48623
   247
lemma key_le_right: "rbt_sorted (Branch c lt k v rt) \<Longrightarrow> (\<And>x. x \<in>set (RBT_Impl.keys rt) \<Longrightarrow> k \<le> x)"
kuncar@48623
   248
by  (auto simp: rbt_greater_prop less_imp_le)
kuncar@48623
   249
kuncar@48623
   250
lemma left_le_key: "rbt_sorted (Branch c lt k v rt) \<Longrightarrow> (\<And>x. x \<in>set (RBT_Impl.keys lt) \<Longrightarrow> x \<le> k)"
kuncar@48623
   251
by (auto simp: rbt_less_prop less_imp_le)
kuncar@48623
   252
kuncar@48623
   253
lemma fold_min_triv:
kuncar@48623
   254
  fixes k :: "_ :: linorder"
kuncar@48623
   255
  shows "(\<forall>x\<in>set xs. k \<le> x) \<Longrightarrow> List.fold min xs k = k" 
kuncar@48623
   256
by (induct xs) (auto simp add: min_def)
kuncar@48623
   257
kuncar@48623
   258
lemma rbt_min_simps:
kuncar@48623
   259
  "is_rbt (Branch c RBT_Impl.Empty k v rt) \<Longrightarrow> rbt_min (Branch c RBT_Impl.Empty k v rt) = k"
kuncar@48623
   260
by (auto intro: fold_min_triv dest: key_le_right is_rbt_rbt_sorted simp: rbt_fold1_keys_def rbt_min_def)
kuncar@48623
   261
kuncar@48623
   262
fun rbt_min_opt where
kuncar@48623
   263
  "rbt_min_opt (Branch c RBT_Impl.Empty k v rt) = k" |
kuncar@48623
   264
  "rbt_min_opt (Branch c (Branch lc llc lk lv lrt) k v rt) = rbt_min_opt (Branch lc llc lk lv lrt)"
kuncar@48623
   265
kuncar@48623
   266
lemma rbt_min_opt_Branch:
kuncar@48623
   267
  "t1 \<noteq> rbt.Empty \<Longrightarrow> rbt_min_opt (Branch c t1 k () t2) = rbt_min_opt t1" 
kuncar@48623
   268
by (cases t1) auto
kuncar@48623
   269
kuncar@48623
   270
lemma rbt_min_opt_induct [case_names empty left_empty left_non_empty]:
kuncar@48623
   271
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   272
  assumes "P rbt.Empty"
kuncar@48623
   273
  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t1 = rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
kuncar@48623
   274
  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t1 \<noteq> rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
kuncar@48623
   275
  shows "P t"
wenzelm@63649
   276
  using assms
wenzelm@63649
   277
proof (induct t)
wenzelm@63649
   278
  case Empty
wenzelm@63649
   279
  then show ?case by simp
wenzelm@63649
   280
next
wenzelm@63649
   281
  case (Branch x1 t1 x3 x4 t2)
wenzelm@63649
   282
  then show ?case by (cases "t1 = rbt.Empty") simp_all
wenzelm@63649
   283
qed
kuncar@48623
   284
kuncar@48623
   285
lemma rbt_min_opt_in_set: 
kuncar@48623
   286
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   287
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   288
  shows "rbt_min_opt t \<in> set (RBT_Impl.keys t)"
kuncar@48623
   289
using assms by (induction t rule: rbt_min_opt.induct) (auto)
kuncar@48623
   290
kuncar@48623
   291
lemma rbt_min_opt_is_min:
kuncar@48623
   292
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   293
  assumes "rbt_sorted t"
kuncar@48623
   294
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   295
  shows "\<And>y. y \<in> set (RBT_Impl.keys t) \<Longrightarrow> y \<ge> rbt_min_opt t"
kuncar@48623
   296
using assms 
kuncar@48623
   297
proof (induction t rule: rbt_min_opt_induct)
kuncar@48623
   298
  case empty
wenzelm@60580
   299
  then show ?case by simp
kuncar@48623
   300
next
kuncar@48623
   301
  case left_empty
wenzelm@60580
   302
  then show ?case by (auto intro: key_le_right simp del: rbt_sorted.simps)
kuncar@48623
   303
next
kuncar@48623
   304
  case (left_non_empty c t1 k v t2 y)
wenzelm@60580
   305
  then consider "y = k" | "y \<in> set (RBT_Impl.keys t1)" | "y \<in> set (RBT_Impl.keys t2)"
wenzelm@60580
   306
    by auto
wenzelm@60580
   307
  then show ?case 
wenzelm@60580
   308
  proof cases
wenzelm@60580
   309
    case 1
wenzelm@60580
   310
    with left_non_empty show ?thesis
wenzelm@60580
   311
      by (auto simp add: rbt_min_opt_Branch intro: left_le_key rbt_min_opt_in_set)
wenzelm@60580
   312
  next
wenzelm@60580
   313
    case 2
wenzelm@60580
   314
    with left_non_empty show ?thesis
wenzelm@60580
   315
      by (auto simp add: rbt_min_opt_Branch)
wenzelm@60580
   316
  next 
wenzelm@60580
   317
    case y: 3
wenzelm@60580
   318
    have "rbt_min_opt t1 \<le> k"
wenzelm@60580
   319
      using left_non_empty by (simp add: left_le_key rbt_min_opt_in_set)
wenzelm@60580
   320
    moreover have "k \<le> y"
wenzelm@60580
   321
      using left_non_empty y by (simp add: key_le_right)
wenzelm@60580
   322
    ultimately show ?thesis
wenzelm@60580
   323
      using left_non_empty y by (simp add: rbt_min_opt_Branch)
wenzelm@60580
   324
  qed
kuncar@48623
   325
qed
kuncar@48623
   326
kuncar@48623
   327
lemma rbt_min_eq_rbt_min_opt:
kuncar@48623
   328
  assumes "t \<noteq> RBT_Impl.Empty"
kuncar@48623
   329
  assumes "is_rbt t"
kuncar@48623
   330
  shows "rbt_min t = rbt_min_opt t"
kuncar@48623
   331
proof -
haftmann@51489
   332
  from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
haftmann@51489
   333
  with assms show ?thesis
haftmann@51489
   334
    by (simp add: rbt_min_def rbt_fold1_keys_def rbt_min_opt_is_min
haftmann@51540
   335
      Min.set_eq_fold [symmetric] Min_eqI rbt_min_opt_in_set)
kuncar@48623
   336
qed
kuncar@48623
   337
kuncar@48623
   338
(* maximum *)
kuncar@48623
   339
kuncar@48623
   340
definition rbt_max :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
kuncar@48623
   341
  where "rbt_max t = rbt_fold1_keys max t"
kuncar@48623
   342
kuncar@48623
   343
lemma fold_max_triv:
kuncar@48623
   344
  fixes k :: "_ :: linorder"
kuncar@48623
   345
  shows "(\<forall>x\<in>set xs. x \<le> k) \<Longrightarrow> List.fold max xs k = k" 
kuncar@48623
   346
by (induct xs) (auto simp add: max_def)
kuncar@48623
   347
kuncar@48623
   348
lemma fold_max_rev_eq:
kuncar@48623
   349
  fixes xs :: "('a :: linorder) list"
kuncar@48623
   350
  assumes "xs \<noteq> []"
kuncar@48623
   351
  shows "List.fold max (tl xs) (hd xs) = List.fold max (tl (rev xs)) (hd (rev xs))" 
haftmann@51540
   352
  using assms by (simp add: Max.set_eq_fold [symmetric])
kuncar@48623
   353
kuncar@48623
   354
lemma rbt_max_simps:
kuncar@48623
   355
  assumes "is_rbt (Branch c lt k v RBT_Impl.Empty)" 
kuncar@48623
   356
  shows "rbt_max (Branch c lt k v RBT_Impl.Empty) = k"
kuncar@48623
   357
proof -
kuncar@48623
   358
  have "List.fold max (tl (rev(RBT_Impl.keys lt @ [k]))) (hd (rev(RBT_Impl.keys lt @ [k]))) = k"
kuncar@48623
   359
    using assms by (auto intro!: fold_max_triv dest!: left_le_key is_rbt_rbt_sorted)
kuncar@48623
   360
  then show ?thesis by (auto simp add: rbt_max_def rbt_fold1_keys_def fold_max_rev_eq)
kuncar@48623
   361
qed
kuncar@48623
   362
kuncar@48623
   363
fun rbt_max_opt where
kuncar@48623
   364
  "rbt_max_opt (Branch c lt k v RBT_Impl.Empty) = k" |
kuncar@48623
   365
  "rbt_max_opt (Branch c lt k v (Branch rc rlc rk rv rrt)) = rbt_max_opt (Branch rc rlc rk rv rrt)"
kuncar@48623
   366
kuncar@48623
   367
lemma rbt_max_opt_Branch:
kuncar@48623
   368
  "t2 \<noteq> rbt.Empty \<Longrightarrow> rbt_max_opt (Branch c t1 k () t2) = rbt_max_opt t2" 
kuncar@48623
   369
by (cases t2) auto
kuncar@48623
   370
kuncar@48623
   371
lemma rbt_max_opt_induct [case_names empty right_empty right_non_empty]:
kuncar@48623
   372
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   373
  assumes "P rbt.Empty"
kuncar@48623
   374
  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t2 = rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
kuncar@48623
   375
  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t2 \<noteq> rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
kuncar@48623
   376
  shows "P t"
wenzelm@63649
   377
  using assms
wenzelm@63649
   378
proof (induct t)
wenzelm@63649
   379
  case Empty
wenzelm@63649
   380
  then show ?case by simp
wenzelm@63649
   381
next
wenzelm@63649
   382
  case (Branch x1 t1 x3 x4 t2)
wenzelm@63649
   383
  then show ?case by (cases "t2 = rbt.Empty") simp_all
wenzelm@63649
   384
qed
kuncar@48623
   385
kuncar@48623
   386
lemma rbt_max_opt_in_set: 
kuncar@48623
   387
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   388
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   389
  shows "rbt_max_opt t \<in> set (RBT_Impl.keys t)"
kuncar@48623
   390
using assms by (induction t rule: rbt_max_opt.induct) (auto)
kuncar@48623
   391
kuncar@48623
   392
lemma rbt_max_opt_is_max:
kuncar@48623
   393
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   394
  assumes "rbt_sorted t"
kuncar@48623
   395
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   396
  shows "\<And>y. y \<in> set (RBT_Impl.keys t) \<Longrightarrow> y \<le> rbt_max_opt t"
kuncar@48623
   397
using assms 
kuncar@48623
   398
proof (induction t rule: rbt_max_opt_induct)
kuncar@48623
   399
  case empty
wenzelm@60580
   400
  then show ?case by simp
kuncar@48623
   401
next
kuncar@48623
   402
  case right_empty
wenzelm@60580
   403
  then show ?case by (auto intro: left_le_key simp del: rbt_sorted.simps)
kuncar@48623
   404
next
kuncar@48623
   405
  case (right_non_empty c t1 k v t2 y)
wenzelm@60580
   406
  then consider "y = k" | "y \<in> set (RBT_Impl.keys t2)" | "y \<in> set (RBT_Impl.keys t1)"
wenzelm@60580
   407
    by auto
wenzelm@60580
   408
  then show ?case 
wenzelm@60580
   409
  proof cases
wenzelm@60580
   410
    case 1
wenzelm@60580
   411
    with right_non_empty show ?thesis
wenzelm@60580
   412
      by (auto simp add: rbt_max_opt_Branch intro: key_le_right rbt_max_opt_in_set)
wenzelm@60580
   413
  next
wenzelm@60580
   414
    case 2
wenzelm@60580
   415
    with right_non_empty show ?thesis
wenzelm@60580
   416
      by (auto simp add: rbt_max_opt_Branch)
wenzelm@60580
   417
  next 
wenzelm@60580
   418
    case y: 3
wenzelm@60580
   419
    have "rbt_max_opt t2 \<ge> k"
wenzelm@60580
   420
      using right_non_empty by (simp add: key_le_right rbt_max_opt_in_set)
wenzelm@60580
   421
    moreover have "y \<le> k"
wenzelm@60580
   422
      using right_non_empty y by (simp add: left_le_key)
wenzelm@60580
   423
    ultimately show ?thesis
wenzelm@60580
   424
      using right_non_empty by (simp add: rbt_max_opt_Branch)
wenzelm@60580
   425
  qed
kuncar@48623
   426
qed
kuncar@48623
   427
kuncar@48623
   428
lemma rbt_max_eq_rbt_max_opt:
kuncar@48623
   429
  assumes "t \<noteq> RBT_Impl.Empty"
kuncar@48623
   430
  assumes "is_rbt t"
kuncar@48623
   431
  shows "rbt_max t = rbt_max_opt t"
kuncar@48623
   432
proof -
haftmann@51489
   433
  from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
haftmann@51489
   434
  with assms show ?thesis
haftmann@51489
   435
    by (simp add: rbt_max_def rbt_fold1_keys_def rbt_max_opt_is_max
haftmann@51540
   436
      Max.set_eq_fold [symmetric] Max_eqI rbt_max_opt_in_set)
kuncar@48623
   437
qed
kuncar@48623
   438
kuncar@48623
   439
kuncar@48623
   440
(** abstract **)
kuncar@48623
   441
kuncar@56019
   442
context includes rbt.lifting begin
kuncar@48623
   443
lift_definition fold1_keys :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> 'a"
kuncar@55565
   444
  is rbt_fold1_keys .
kuncar@48623
   445
kuncar@48623
   446
lemma fold1_keys_def_alt:
kuncar@56019
   447
  "fold1_keys f t = List.fold f (tl (RBT.keys t)) (hd (RBT.keys t))"
kuncar@48623
   448
  by transfer (simp add: rbt_fold1_keys_def)
kuncar@48623
   449
kuncar@48623
   450
lemma finite_fold1_fold1_keys:
haftmann@51489
   451
  assumes "semilattice f"
kuncar@56019
   452
  assumes "\<not> RBT.is_empty t"
haftmann@51489
   453
  shows "semilattice_set.F f (Set t) = fold1_keys f t"
kuncar@48623
   454
proof -
wenzelm@60500
   455
  from \<open>semilattice f\<close> interpret semilattice_set f by (rule semilattice_set.intro)
kuncar@48623
   456
  show ?thesis using assms 
haftmann@51489
   457
    by (auto simp: fold1_keys_def_alt set_keys fold_def_alt non_empty_keys set_eq_fold [symmetric])
kuncar@48623
   458
qed
kuncar@48623
   459
kuncar@48623
   460
(* minimum *)
kuncar@48623
   461
kuncar@55565
   462
lift_definition r_min :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min .
kuncar@48623
   463
kuncar@55565
   464
lift_definition r_min_opt :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min_opt .
kuncar@48623
   465
kuncar@48623
   466
lemma r_min_alt_def: "r_min t = fold1_keys min t"
kuncar@48623
   467
by transfer (simp add: rbt_min_def)
kuncar@48623
   468
kuncar@48623
   469
lemma r_min_eq_r_min_opt:
kuncar@56019
   470
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   471
  shows "r_min t = r_min_opt t"
kuncar@48623
   472
using assms unfolding is_empty_empty by transfer (auto intro: rbt_min_eq_rbt_min_opt)
kuncar@48623
   473
kuncar@48623
   474
lemma fold_keys_min_top_eq:
wenzelm@63649
   475
  fixes t :: "('a::{linorder,bounded_lattice_top}, unit) rbt"
kuncar@56019
   476
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   477
  shows "fold_keys min t top = fold1_keys min t"
kuncar@48623
   478
proof -
kuncar@48623
   479
  have *: "\<And>t. RBT_Impl.keys t \<noteq> [] \<Longrightarrow> List.fold min (RBT_Impl.keys t) top = 
wenzelm@63649
   480
      List.fold min (hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t)) top"
kuncar@48623
   481
    by (simp add: hd_Cons_tl[symmetric])
wenzelm@63649
   482
  have **: "List.fold min (x # xs) top = List.fold min xs x" for x :: 'a and xs
kuncar@48623
   483
    by (simp add: inf_min[symmetric])
wenzelm@63649
   484
  show ?thesis
wenzelm@63649
   485
    using assms
kuncar@48623
   486
    unfolding fold_keys_def_alt fold1_keys_def_alt is_empty_empty
kuncar@48623
   487
    apply transfer 
kuncar@48623
   488
    apply (case_tac t) 
wenzelm@63649
   489
     apply simp 
kuncar@48623
   490
    apply (subst *)
wenzelm@63649
   491
     apply simp
kuncar@48623
   492
    apply (subst **)
kuncar@48623
   493
    apply simp
wenzelm@63649
   494
    done
kuncar@48623
   495
qed
kuncar@48623
   496
kuncar@48623
   497
(* maximum *)
kuncar@48623
   498
kuncar@55565
   499
lift_definition r_max :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_max .
kuncar@48623
   500
kuncar@55565
   501
lift_definition r_max_opt :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_max_opt .
kuncar@48623
   502
kuncar@48623
   503
lemma r_max_alt_def: "r_max t = fold1_keys max t"
kuncar@48623
   504
by transfer (simp add: rbt_max_def)
kuncar@48623
   505
kuncar@48623
   506
lemma r_max_eq_r_max_opt:
kuncar@56019
   507
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   508
  shows "r_max t = r_max_opt t"
kuncar@48623
   509
using assms unfolding is_empty_empty by transfer (auto intro: rbt_max_eq_rbt_max_opt)
kuncar@48623
   510
kuncar@48623
   511
lemma fold_keys_max_bot_eq:
wenzelm@63649
   512
  fixes t :: "('a::{linorder,bounded_lattice_bot}, unit) rbt"
kuncar@56019
   513
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   514
  shows "fold_keys max t bot = fold1_keys max t"
kuncar@48623
   515
proof -
kuncar@48623
   516
  have *: "\<And>t. RBT_Impl.keys t \<noteq> [] \<Longrightarrow> List.fold max (RBT_Impl.keys t) bot = 
wenzelm@63649
   517
      List.fold max (hd(RBT_Impl.keys t) # tl(RBT_Impl.keys t)) bot"
kuncar@48623
   518
    by (simp add: hd_Cons_tl[symmetric])
wenzelm@63649
   519
  have **: "List.fold max (x # xs) bot = List.fold max xs x" for x :: 'a and xs
kuncar@48623
   520
    by (simp add: sup_max[symmetric])
wenzelm@63649
   521
  show ?thesis
wenzelm@63649
   522
    using assms
kuncar@48623
   523
    unfolding fold_keys_def_alt fold1_keys_def_alt is_empty_empty
kuncar@48623
   524
    apply transfer 
kuncar@48623
   525
    apply (case_tac t) 
wenzelm@63649
   526
     apply simp 
kuncar@48623
   527
    apply (subst *)
wenzelm@63649
   528
     apply simp
kuncar@48623
   529
    apply (subst **)
kuncar@48623
   530
    apply simp
wenzelm@63649
   531
    done
kuncar@48623
   532
qed
kuncar@48623
   533
kuncar@56019
   534
end
kuncar@48623
   535
wenzelm@60500
   536
section \<open>Code equations\<close>
kuncar@48623
   537
kuncar@48623
   538
code_datatype Set Coset
kuncar@48623
   539
blanchet@57816
   540
declare list.set[code] (* needed? *)
kuncar@50996
   541
kuncar@48623
   542
lemma empty_Set [code]:
kuncar@48623
   543
  "Set.empty = Set RBT.empty"
kuncar@48623
   544
by (auto simp: Set_def)
kuncar@48623
   545
kuncar@48623
   546
lemma UNIV_Coset [code]:
kuncar@48623
   547
  "UNIV = Coset RBT.empty"
kuncar@48623
   548
by (auto simp: Set_def)
kuncar@48623
   549
kuncar@48623
   550
lemma is_empty_Set [code]:
kuncar@48623
   551
  "Set.is_empty (Set t) = RBT.is_empty t"
kuncar@48623
   552
  unfolding Set.is_empty_def by (auto simp: fun_eq_iff Set_def intro: lookup_empty_empty[THEN iffD1])
kuncar@48623
   553
kuncar@48623
   554
lemma compl_code [code]:
kuncar@48623
   555
  "- Set xs = Coset xs"
kuncar@48623
   556
  "- Coset xs = Set xs"
kuncar@48623
   557
by (simp_all add: Set_def)
kuncar@48623
   558
kuncar@48623
   559
lemma member_code [code]:
kuncar@48623
   560
  "x \<in> (Set t) = (RBT.lookup t x = Some ())"
kuncar@48623
   561
  "x \<in> (Coset t) = (RBT.lookup t x = None)"
kuncar@48623
   562
by (simp_all add: Set_def)
kuncar@48623
   563
kuncar@48623
   564
lemma insert_code [code]:
kuncar@48623
   565
  "Set.insert x (Set t) = Set (RBT.insert x () t)"
kuncar@48623
   566
  "Set.insert x (Coset t) = Coset (RBT.delete x t)"
kuncar@48623
   567
by (auto simp: Set_def)
kuncar@48623
   568
kuncar@48623
   569
lemma remove_code [code]:
kuncar@48623
   570
  "Set.remove x (Set t) = Set (RBT.delete x t)"
kuncar@48623
   571
  "Set.remove x (Coset t) = Coset (RBT.insert x () t)"
kuncar@48623
   572
by (auto simp: Set_def)
kuncar@48623
   573
kuncar@48623
   574
lemma union_Set [code]:
kuncar@48623
   575
  "Set t \<union> A = fold_keys Set.insert t A"
kuncar@48623
   576
proof -
kuncar@48623
   577
  interpret comp_fun_idem Set.insert
kuncar@48623
   578
    by (fact comp_fun_idem_insert)
wenzelm@60500
   579
  from finite_fold_fold_keys[OF \<open>comp_fun_commute Set.insert\<close>]
kuncar@48623
   580
  show ?thesis by (auto simp add: union_fold_insert)
kuncar@48623
   581
qed
kuncar@48623
   582
kuncar@48623
   583
lemma inter_Set [code]:
kuncar@48623
   584
  "A \<inter> Set t = rbt_filter (\<lambda>k. k \<in> A) t"
kuncar@49758
   585
by (simp add: inter_Set_filter Set_filter_rbt_filter)
kuncar@48623
   586
kuncar@48623
   587
lemma minus_Set [code]:
kuncar@48623
   588
  "A - Set t = fold_keys Set.remove t A"
kuncar@48623
   589
proof -
kuncar@48623
   590
  interpret comp_fun_idem Set.remove
kuncar@48623
   591
    by (fact comp_fun_idem_remove)
wenzelm@60500
   592
  from finite_fold_fold_keys[OF \<open>comp_fun_commute Set.remove\<close>]
kuncar@48623
   593
  show ?thesis by (auto simp add: minus_fold_remove)
kuncar@48623
   594
qed
kuncar@48623
   595
kuncar@48623
   596
lemma union_Coset [code]:
kuncar@48623
   597
  "Coset t \<union> A = - rbt_filter (\<lambda>k. k \<notin> A) t"
kuncar@48623
   598
proof -
kuncar@48623
   599
  have *: "\<And>A B. (-A \<union> B) = -(-B \<inter> A)" by blast
kuncar@48623
   600
  show ?thesis by (simp del: boolean_algebra_class.compl_inf add: * inter_Set)
kuncar@48623
   601
qed
kuncar@48623
   602
 
kuncar@48623
   603
lemma union_Set_Set [code]:
kuncar@56019
   604
  "Set t1 \<union> Set t2 = Set (RBT.union t1 t2)"
kuncar@48623
   605
by (auto simp add: lookup_union map_add_Some_iff Set_def)
kuncar@48623
   606
kuncar@48623
   607
lemma inter_Coset [code]:
kuncar@48623
   608
  "A \<inter> Coset t = fold_keys Set.remove t A"
kuncar@48623
   609
by (simp add: Diff_eq [symmetric] minus_Set)
kuncar@48623
   610
kuncar@48623
   611
lemma inter_Coset_Coset [code]:
kuncar@56019
   612
  "Coset t1 \<inter> Coset t2 = Coset (RBT.union t1 t2)"
kuncar@48623
   613
by (auto simp add: lookup_union map_add_Some_iff Set_def)
kuncar@48623
   614
kuncar@48623
   615
lemma minus_Coset [code]:
kuncar@48623
   616
  "A - Coset t = rbt_filter (\<lambda>k. k \<in> A) t"
kuncar@48623
   617
by (simp add: inter_Set[simplified Int_commute])
kuncar@48623
   618
kuncar@49757
   619
lemma filter_Set [code]:
kuncar@49757
   620
  "Set.filter P (Set t) = (rbt_filter P t)"
kuncar@49758
   621
by (auto simp add: Set_filter_rbt_filter)
kuncar@48623
   622
kuncar@48623
   623
lemma image_Set [code]:
kuncar@48623
   624
  "image f (Set t) = fold_keys (\<lambda>k A. Set.insert (f k) A) t {}"
kuncar@48623
   625
proof -
wenzelm@60679
   626
  have "comp_fun_commute (\<lambda>k. Set.insert (f k))"
wenzelm@60679
   627
    by standard auto
wenzelm@60679
   628
  then show ?thesis
wenzelm@60679
   629
    by (auto simp add: image_fold_insert intro!: finite_fold_fold_keys)
kuncar@48623
   630
qed
kuncar@48623
   631
kuncar@48623
   632
lemma Ball_Set [code]:
kuncar@56019
   633
  "Ball (Set t) P \<longleftrightarrow> RBT.foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t True"
kuncar@48623
   634
proof -
wenzelm@60679
   635
  have "comp_fun_commute (\<lambda>k s. s \<and> P k)"
wenzelm@60679
   636
    by standard auto
kuncar@48623
   637
  then show ?thesis 
kuncar@48623
   638
    by (simp add: foldi_fold_conj[symmetric] Ball_fold finite_fold_fold_keys)
kuncar@48623
   639
qed
kuncar@48623
   640
kuncar@48623
   641
lemma Bex_Set [code]:
kuncar@56019
   642
  "Bex (Set t) P \<longleftrightarrow> RBT.foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t False"
kuncar@48623
   643
proof -
wenzelm@60679
   644
  have "comp_fun_commute (\<lambda>k s. s \<or> P k)"
wenzelm@60679
   645
    by standard auto
kuncar@48623
   646
  then show ?thesis 
kuncar@48623
   647
    by (simp add: foldi_fold_disj[symmetric] Bex_fold finite_fold_fold_keys)
kuncar@48623
   648
qed
kuncar@48623
   649
kuncar@48623
   650
lemma subset_code [code]:
kuncar@48623
   651
  "Set t \<le> B \<longleftrightarrow> (\<forall>x\<in>Set t. x \<in> B)"
kuncar@48623
   652
  "A \<le> Coset t \<longleftrightarrow> (\<forall>y\<in>Set t. y \<notin> A)"
kuncar@48623
   653
by auto
kuncar@48623
   654
kuncar@48623
   655
lemma subset_Coset_empty_Set_empty [code]:
kuncar@56019
   656
  "Coset t1 \<le> Set t2 \<longleftrightarrow> (case (RBT.impl_of t1, RBT.impl_of t2) of 
kuncar@48623
   657
    (rbt.Empty, rbt.Empty) => False |
Andreas@53745
   658
    (_, _) => Code.abort (STR ''non_empty_trees'') (\<lambda>_. Coset t1 \<le> Set t2))"
kuncar@48623
   659
proof -
kuncar@56019
   660
  have *: "\<And>t. RBT.impl_of t = rbt.Empty \<Longrightarrow> t = RBT rbt.Empty"
kuncar@48623
   661
    by (subst(asm) RBT_inverse[symmetric]) (auto simp: impl_of_inject)
kuncar@56519
   662
  have **: "eq_onp is_rbt rbt.Empty rbt.Empty" unfolding eq_onp_def by simp
kuncar@48623
   663
  show ?thesis  
Andreas@53745
   664
    by (auto simp: Set_def lookup.abs_eq[OF **] dest!: * split: rbt.split)
kuncar@48623
   665
qed
kuncar@48623
   666
wenzelm@60500
   667
text \<open>A frequent case -- avoid intermediate sets\<close>
kuncar@48623
   668
lemma [code_unfold]:
kuncar@56019
   669
  "Set t1 \<subseteq> Set t2 \<longleftrightarrow> RBT.foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> k \<in> Set t2) t1 True"
kuncar@48623
   670
by (simp add: subset_code Ball_Set)
kuncar@48623
   671
kuncar@48623
   672
lemma card_Set [code]:
kuncar@48623
   673
  "card (Set t) = fold_keys (\<lambda>_ n. n + 1) t 0"
haftmann@51489
   674
  by (auto simp add: card.eq_fold intro: finite_fold_fold_keys comp_fun_commute_const)
kuncar@48623
   675
nipkow@64267
   676
lemma sum_Set [code]:
nipkow@64267
   677
  "sum f (Set xs) = fold_keys (plus o f) xs 0"
kuncar@48623
   678
proof -
wenzelm@60679
   679
  have "comp_fun_commute (\<lambda>x. op + (f x))"
wenzelm@60679
   680
    by standard (auto simp: ac_simps)
kuncar@48623
   681
  then show ?thesis 
nipkow@64267
   682
    by (auto simp add: sum.eq_fold finite_fold_fold_keys o_def)
kuncar@48623
   683
qed
kuncar@48623
   684
kuncar@48623
   685
lemma the_elem_set [code]:
kuncar@48623
   686
  fixes t :: "('a :: linorder, unit) rbt"
kuncar@56019
   687
  shows "the_elem (Set t) = (case RBT.impl_of t of 
kuncar@48623
   688
    (Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty) \<Rightarrow> x
Andreas@53745
   689
    | _ \<Rightarrow> Code.abort (STR ''not_a_singleton_tree'') (\<lambda>_. the_elem (Set t)))"
kuncar@48623
   690
proof -
kuncar@48623
   691
  {
kuncar@48623
   692
    fix x :: "'a :: linorder"
kuncar@48623
   693
    let ?t = "Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty" 
kuncar@48623
   694
    have *:"?t \<in> {t. is_rbt t}" unfolding is_rbt_def by auto
kuncar@56519
   695
    then have **:"eq_onp is_rbt ?t ?t" unfolding eq_onp_def by auto
kuncar@48623
   696
kuncar@56019
   697
    have "RBT.impl_of t = ?t \<Longrightarrow> the_elem (Set t) = x" 
kuncar@48623
   698
      by (subst(asm) RBT_inverse[symmetric, OF *])
kuncar@48623
   699
        (auto simp: Set_def the_elem_def lookup.abs_eq[OF **] impl_of_inject)
kuncar@48623
   700
  }
Andreas@53745
   701
  then show ?thesis
kuncar@48623
   702
    by(auto split: rbt.split unit.split color.split)
kuncar@48623
   703
qed
kuncar@48623
   704
wenzelm@60679
   705
lemma Pow_Set [code]: "Pow (Set t) = fold_keys (\<lambda>x A. A \<union> Set.insert x ` A) t {{}}"
wenzelm@60679
   706
  by (simp add: Pow_fold finite_fold_fold_keys[OF comp_fun_commute_Pow_fold])
kuncar@48623
   707
kuncar@48623
   708
lemma product_Set [code]:
kuncar@48623
   709
  "Product_Type.product (Set t1) (Set t2) = 
kuncar@48623
   710
    fold_keys (\<lambda>x A. fold_keys (\<lambda>y. Set.insert (x, y)) t2 A) t1 {}"
kuncar@48623
   711
proof -
wenzelm@60679
   712
  have *: "comp_fun_commute (\<lambda>y. Set.insert (x, y))" for x
wenzelm@60679
   713
    by standard auto
kuncar@48623
   714
  show ?thesis using finite_fold_fold_keys[OF comp_fun_commute_product_fold, of "Set t2" "{}" "t1"]  
kuncar@48623
   715
    by (simp add: product_fold Product_Type.product_def finite_fold_fold_keys[OF *])
kuncar@48623
   716
qed
kuncar@48623
   717
wenzelm@60679
   718
lemma Id_on_Set [code]: "Id_on (Set t) =  fold_keys (\<lambda>x. Set.insert (x, x)) t {}"
kuncar@48623
   719
proof -
wenzelm@60679
   720
  have "comp_fun_commute (\<lambda>x. Set.insert (x, x))"
wenzelm@60679
   721
    by standard auto
kuncar@48623
   722
  then show ?thesis
kuncar@48623
   723
    by (auto simp add: Id_on_fold intro!: finite_fold_fold_keys)
kuncar@48623
   724
qed
kuncar@48623
   725
kuncar@48623
   726
lemma Image_Set [code]:
kuncar@48623
   727
  "(Set t) `` S = fold_keys (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) t {}"
kuncar@48623
   728
by (auto simp add: Image_fold finite_fold_fold_keys[OF comp_fun_commute_Image_fold])
kuncar@48623
   729
kuncar@48623
   730
lemma trancl_set_ntrancl [code]:
kuncar@48623
   731
  "trancl (Set t) = ntrancl (card (Set t) - 1) (Set t)"
kuncar@48623
   732
by (simp add: finite_trancl_ntranl)
kuncar@48623
   733
kuncar@48623
   734
lemma relcomp_Set[code]:
kuncar@48623
   735
  "(Set t1) O (Set t2) = fold_keys 
kuncar@48623
   736
    (\<lambda>(x,y) A. fold_keys (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') t2 A) t1 {}"
kuncar@48623
   737
proof -
wenzelm@60679
   738
  interpret comp_fun_idem Set.insert
wenzelm@60679
   739
    by (fact comp_fun_idem_insert)
kuncar@48623
   740
  have *: "\<And>x y. comp_fun_commute (\<lambda>(w, z) A'. if y = w then Set.insert (x, z) A' else A')"
wenzelm@60679
   741
    by standard (auto simp add: fun_eq_iff)
wenzelm@60679
   742
  show ?thesis
wenzelm@60679
   743
    using finite_fold_fold_keys[OF comp_fun_commute_relcomp_fold, of "Set t2" "{}" t1]
kuncar@48623
   744
    by (simp add: relcomp_fold finite_fold_fold_keys[OF *])
kuncar@48623
   745
qed
kuncar@48623
   746
kuncar@48623
   747
lemma wf_set [code]:
kuncar@48623
   748
  "wf (Set t) = acyclic (Set t)"
kuncar@48623
   749
by (simp add: wf_iff_acyclic_if_finite)
kuncar@48623
   750
kuncar@48623
   751
lemma Min_fin_set_fold [code]:
Andreas@53745
   752
  "Min (Set t) = 
kuncar@56019
   753
  (if RBT.is_empty t
Andreas@53745
   754
   then Code.abort (STR ''not_non_empty_tree'') (\<lambda>_. Min (Set t))
Andreas@53745
   755
   else r_min_opt t)"
kuncar@48623
   756
proof -
haftmann@51489
   757
  have *: "semilattice (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
haftmann@51489
   758
  with finite_fold1_fold1_keys [OF *, folded Min_def]
kuncar@48623
   759
  show ?thesis
Andreas@53745
   760
    by (simp add: r_min_alt_def r_min_eq_r_min_opt [symmetric])  
kuncar@48623
   761
qed
kuncar@48623
   762
kuncar@48623
   763
lemma Inf_fin_set_fold [code]:
kuncar@48623
   764
  "Inf_fin (Set t) = Min (Set t)"
kuncar@48623
   765
by (simp add: inf_min Inf_fin_def Min_def)
kuncar@48623
   766
kuncar@48623
   767
lemma Inf_Set_fold:
kuncar@48623
   768
  fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
kuncar@56019
   769
  shows "Inf (Set t) = (if RBT.is_empty t then top else r_min_opt t)"
kuncar@48623
   770
proof -
wenzelm@60679
   771
  have "comp_fun_commute (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)"
wenzelm@60679
   772
    by standard (simp add: fun_eq_iff ac_simps)
kuncar@56019
   773
  then have "t \<noteq> RBT.empty \<Longrightarrow> Finite_Set.fold min top (Set t) = fold1_keys min t"
kuncar@48623
   774
    by (simp add: finite_fold_fold_keys fold_keys_min_top_eq)
kuncar@48623
   775
  then show ?thesis 
wenzelm@60679
   776
    by (auto simp add: Inf_fold_inf inf_min empty_Set[symmetric]
wenzelm@60679
   777
      r_min_eq_r_min_opt[symmetric] r_min_alt_def)
kuncar@48623
   778
qed
kuncar@48623
   779
kuncar@48623
   780
definition Inf' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a" where [code del]: "Inf' x = Inf x"
kuncar@48623
   781
declare Inf'_def[symmetric, code_unfold]
kuncar@48623
   782
declare Inf_Set_fold[folded Inf'_def, code]
kuncar@48623
   783
haftmann@56212
   784
lemma INF_Set_fold [code]:
hoelzl@54263
   785
  fixes f :: "_ \<Rightarrow> 'a::complete_lattice"
haftmann@56218
   786
  shows "INFIMUM (Set t) f = fold_keys (inf \<circ> f) t top"
kuncar@48623
   787
proof -
kuncar@48623
   788
  have "comp_fun_commute ((inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a) \<circ> f)" 
wenzelm@60679
   789
    by standard (auto simp add: fun_eq_iff ac_simps)
kuncar@48623
   790
  then show ?thesis
kuncar@48623
   791
    by (auto simp: INF_fold_inf finite_fold_fold_keys)
kuncar@48623
   792
qed
kuncar@48623
   793
kuncar@48623
   794
lemma Max_fin_set_fold [code]:
Andreas@53745
   795
  "Max (Set t) = 
kuncar@56019
   796
  (if RBT.is_empty t
Andreas@53745
   797
   then Code.abort (STR ''not_non_empty_tree'') (\<lambda>_. Max (Set t))
Andreas@53745
   798
   else r_max_opt t)"
kuncar@48623
   799
proof -
haftmann@51489
   800
  have *: "semilattice (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
haftmann@51489
   801
  with finite_fold1_fold1_keys [OF *, folded Max_def]
kuncar@48623
   802
  show ?thesis
Andreas@53745
   803
    by (simp add: r_max_alt_def r_max_eq_r_max_opt [symmetric])  
kuncar@48623
   804
qed
kuncar@48623
   805
kuncar@48623
   806
lemma Sup_fin_set_fold [code]:
kuncar@48623
   807
  "Sup_fin (Set t) = Max (Set t)"
kuncar@48623
   808
by (simp add: sup_max Sup_fin_def Max_def)
kuncar@48623
   809
kuncar@48623
   810
lemma Sup_Set_fold:
kuncar@48623
   811
  fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
kuncar@56019
   812
  shows "Sup (Set t) = (if RBT.is_empty t then bot else r_max_opt t)"
kuncar@48623
   813
proof -
wenzelm@60679
   814
  have "comp_fun_commute (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)"
wenzelm@60679
   815
    by standard (simp add: fun_eq_iff ac_simps)
kuncar@56019
   816
  then have "t \<noteq> RBT.empty \<Longrightarrow> Finite_Set.fold max bot (Set t) = fold1_keys max t"
kuncar@48623
   817
    by (simp add: finite_fold_fold_keys fold_keys_max_bot_eq)
kuncar@48623
   818
  then show ?thesis 
wenzelm@60679
   819
    by (auto simp add: Sup_fold_sup sup_max empty_Set[symmetric]
wenzelm@60679
   820
      r_max_eq_r_max_opt[symmetric] r_max_alt_def)
kuncar@48623
   821
qed
kuncar@48623
   822
wenzelm@60679
   823
definition Sup' :: "'a :: {linorder,complete_lattice} set \<Rightarrow> 'a"
wenzelm@60679
   824
  where [code del]: "Sup' x = Sup x"
kuncar@48623
   825
declare Sup'_def[symmetric, code_unfold]
kuncar@48623
   826
declare Sup_Set_fold[folded Sup'_def, code]
kuncar@48623
   827
haftmann@56212
   828
lemma SUP_Set_fold [code]:
hoelzl@54263
   829
  fixes f :: "_ \<Rightarrow> 'a::complete_lattice"
haftmann@56218
   830
  shows "SUPREMUM (Set t) f = fold_keys (sup \<circ> f) t bot"
kuncar@48623
   831
proof -
kuncar@48623
   832
  have "comp_fun_commute ((sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a) \<circ> f)" 
wenzelm@60679
   833
    by standard (auto simp add: fun_eq_iff ac_simps)
kuncar@48623
   834
  then show ?thesis
kuncar@48623
   835
    by (auto simp: SUP_fold_sup finite_fold_fold_keys)
kuncar@48623
   836
qed
kuncar@48623
   837
wenzelm@60679
   838
lemma sorted_list_set[code]: "sorted_list_of_set (Set t) = RBT.keys t"
wenzelm@60679
   839
  by (auto simp add: set_keys intro: sorted_distinct_set_unique) 
kuncar@48623
   840
nipkow@53955
   841
lemma Bleast_code [code]:
wenzelm@60679
   842
  "Bleast (Set t) P =
eberlm@63194
   843
    (case List.filter P (RBT.keys t) of
wenzelm@60679
   844
      x # xs \<Rightarrow> x
wenzelm@60679
   845
    | [] \<Rightarrow> abort_Bleast (Set t) P)"
eberlm@63194
   846
proof (cases "List.filter P (RBT.keys t)")
wenzelm@60679
   847
  case Nil
wenzelm@60679
   848
  thus ?thesis by (simp add: Bleast_def abort_Bleast_def)
nipkow@53955
   849
next
nipkow@53955
   850
  case (Cons x ys)
nipkow@53955
   851
  have "(LEAST x. x \<in> Set t \<and> P x) = x"
nipkow@53955
   852
  proof (rule Least_equality)
wenzelm@60679
   853
    show "x \<in> Set t \<and> P x"
wenzelm@60679
   854
      using Cons[symmetric]
wenzelm@60679
   855
      by (auto simp add: set_keys Cons_eq_filter_iff)
nipkow@53955
   856
    next
wenzelm@60679
   857
      fix y
wenzelm@60679
   858
      assume "y \<in> Set t \<and> P y"
wenzelm@60679
   859
      then show "x \<le> y"
wenzelm@60679
   860
        using Cons[symmetric]
nipkow@53955
   861
        by(auto simp add: set_keys Cons_eq_filter_iff)
nipkow@53955
   862
          (metis sorted_Cons sorted_append sorted_keys)
nipkow@53955
   863
  qed
nipkow@53955
   864
  thus ?thesis using Cons by (simp add: Bleast_def)
nipkow@53955
   865
qed
nipkow@53955
   866
kuncar@48623
   867
hide_const (open) RBT_Set.Set RBT_Set.Coset
kuncar@48623
   868
kuncar@48623
   869
end