src/HOL/Library/RBT_Set.thy
author wenzelm
Thu Jun 25 23:33:47 2015 +0200 (2015-06-25)
changeset 60580 7e741e22d7fc
parent 60500 903bb1495239
child 60679 ade12ef2773c
permissions -rw-r--r--
tuned proofs;
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
kuncar@48623
    21
definition Set :: "('a\<Colon>linorder, unit) rbt \<Rightarrow> 'a set" 
kuncar@56019
    22
  where "Set t = {x . RBT.lookup t x = Some ()}"
kuncar@48623
    23
kuncar@48623
    24
definition Coset :: "('a\<Colon>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]:
kuncar@48623
    88
  "setsum = setsum" ..
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"
kuncar@48623
   276
using assms
kuncar@48623
   277
  apply (induction t)
kuncar@48623
   278
  apply simp
kuncar@48623
   279
  apply (case_tac "t1 = rbt.Empty")
kuncar@48623
   280
  apply simp_all
kuncar@48623
   281
done
kuncar@48623
   282
kuncar@48623
   283
lemma rbt_min_opt_in_set: 
kuncar@48623
   284
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   285
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   286
  shows "rbt_min_opt t \<in> set (RBT_Impl.keys t)"
kuncar@48623
   287
using assms by (induction t rule: rbt_min_opt.induct) (auto)
kuncar@48623
   288
kuncar@48623
   289
lemma rbt_min_opt_is_min:
kuncar@48623
   290
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   291
  assumes "rbt_sorted t"
kuncar@48623
   292
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   293
  shows "\<And>y. y \<in> set (RBT_Impl.keys t) \<Longrightarrow> y \<ge> rbt_min_opt t"
kuncar@48623
   294
using assms 
kuncar@48623
   295
proof (induction t rule: rbt_min_opt_induct)
kuncar@48623
   296
  case empty
wenzelm@60580
   297
  then show ?case by simp
kuncar@48623
   298
next
kuncar@48623
   299
  case left_empty
wenzelm@60580
   300
  then show ?case by (auto intro: key_le_right simp del: rbt_sorted.simps)
kuncar@48623
   301
next
kuncar@48623
   302
  case (left_non_empty c t1 k v t2 y)
wenzelm@60580
   303
  then consider "y = k" | "y \<in> set (RBT_Impl.keys t1)" | "y \<in> set (RBT_Impl.keys t2)"
wenzelm@60580
   304
    by auto
wenzelm@60580
   305
  then show ?case 
wenzelm@60580
   306
  proof cases
wenzelm@60580
   307
    case 1
wenzelm@60580
   308
    with left_non_empty show ?thesis
wenzelm@60580
   309
      by (auto simp add: rbt_min_opt_Branch intro: left_le_key rbt_min_opt_in_set)
wenzelm@60580
   310
  next
wenzelm@60580
   311
    case 2
wenzelm@60580
   312
    with left_non_empty show ?thesis
wenzelm@60580
   313
      by (auto simp add: rbt_min_opt_Branch)
wenzelm@60580
   314
  next 
wenzelm@60580
   315
    case y: 3
wenzelm@60580
   316
    have "rbt_min_opt t1 \<le> k"
wenzelm@60580
   317
      using left_non_empty by (simp add: left_le_key rbt_min_opt_in_set)
wenzelm@60580
   318
    moreover have "k \<le> y"
wenzelm@60580
   319
      using left_non_empty y by (simp add: key_le_right)
wenzelm@60580
   320
    ultimately show ?thesis
wenzelm@60580
   321
      using left_non_empty y by (simp add: rbt_min_opt_Branch)
wenzelm@60580
   322
  qed
kuncar@48623
   323
qed
kuncar@48623
   324
kuncar@48623
   325
lemma rbt_min_eq_rbt_min_opt:
kuncar@48623
   326
  assumes "t \<noteq> RBT_Impl.Empty"
kuncar@48623
   327
  assumes "is_rbt t"
kuncar@48623
   328
  shows "rbt_min t = rbt_min_opt t"
kuncar@48623
   329
proof -
haftmann@51489
   330
  from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
haftmann@51489
   331
  with assms show ?thesis
haftmann@51489
   332
    by (simp add: rbt_min_def rbt_fold1_keys_def rbt_min_opt_is_min
haftmann@51540
   333
      Min.set_eq_fold [symmetric] Min_eqI rbt_min_opt_in_set)
kuncar@48623
   334
qed
kuncar@48623
   335
kuncar@48623
   336
(* maximum *)
kuncar@48623
   337
kuncar@48623
   338
definition rbt_max :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
kuncar@48623
   339
  where "rbt_max t = rbt_fold1_keys max t"
kuncar@48623
   340
kuncar@48623
   341
lemma fold_max_triv:
kuncar@48623
   342
  fixes k :: "_ :: linorder"
kuncar@48623
   343
  shows "(\<forall>x\<in>set xs. x \<le> k) \<Longrightarrow> List.fold max xs k = k" 
kuncar@48623
   344
by (induct xs) (auto simp add: max_def)
kuncar@48623
   345
kuncar@48623
   346
lemma fold_max_rev_eq:
kuncar@48623
   347
  fixes xs :: "('a :: linorder) list"
kuncar@48623
   348
  assumes "xs \<noteq> []"
kuncar@48623
   349
  shows "List.fold max (tl xs) (hd xs) = List.fold max (tl (rev xs)) (hd (rev xs))" 
haftmann@51540
   350
  using assms by (simp add: Max.set_eq_fold [symmetric])
kuncar@48623
   351
kuncar@48623
   352
lemma rbt_max_simps:
kuncar@48623
   353
  assumes "is_rbt (Branch c lt k v RBT_Impl.Empty)" 
kuncar@48623
   354
  shows "rbt_max (Branch c lt k v RBT_Impl.Empty) = k"
kuncar@48623
   355
proof -
kuncar@48623
   356
  have "List.fold max (tl (rev(RBT_Impl.keys lt @ [k]))) (hd (rev(RBT_Impl.keys lt @ [k]))) = k"
kuncar@48623
   357
    using assms by (auto intro!: fold_max_triv dest!: left_le_key is_rbt_rbt_sorted)
kuncar@48623
   358
  then show ?thesis by (auto simp add: rbt_max_def rbt_fold1_keys_def fold_max_rev_eq)
kuncar@48623
   359
qed
kuncar@48623
   360
kuncar@48623
   361
fun rbt_max_opt where
kuncar@48623
   362
  "rbt_max_opt (Branch c lt k v RBT_Impl.Empty) = k" |
kuncar@48623
   363
  "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
   364
kuncar@48623
   365
lemma rbt_max_opt_Branch:
kuncar@48623
   366
  "t2 \<noteq> rbt.Empty \<Longrightarrow> rbt_max_opt (Branch c t1 k () t2) = rbt_max_opt t2" 
kuncar@48623
   367
by (cases t2) auto
kuncar@48623
   368
kuncar@48623
   369
lemma rbt_max_opt_induct [case_names empty right_empty right_non_empty]:
kuncar@48623
   370
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   371
  assumes "P rbt.Empty"
kuncar@48623
   372
  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
   373
  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
   374
  shows "P t"
kuncar@48623
   375
using assms
kuncar@48623
   376
  apply (induction t)
kuncar@48623
   377
  apply simp
kuncar@48623
   378
  apply (case_tac "t2 = rbt.Empty")
kuncar@48623
   379
  apply simp_all
kuncar@48623
   380
done
kuncar@48623
   381
kuncar@48623
   382
lemma rbt_max_opt_in_set: 
kuncar@48623
   383
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   384
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   385
  shows "rbt_max_opt t \<in> set (RBT_Impl.keys t)"
kuncar@48623
   386
using assms by (induction t rule: rbt_max_opt.induct) (auto)
kuncar@48623
   387
kuncar@48623
   388
lemma rbt_max_opt_is_max:
kuncar@48623
   389
  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
kuncar@48623
   390
  assumes "rbt_sorted t"
kuncar@48623
   391
  assumes "t \<noteq> rbt.Empty"
kuncar@48623
   392
  shows "\<And>y. y \<in> set (RBT_Impl.keys t) \<Longrightarrow> y \<le> rbt_max_opt t"
kuncar@48623
   393
using assms 
kuncar@48623
   394
proof (induction t rule: rbt_max_opt_induct)
kuncar@48623
   395
  case empty
wenzelm@60580
   396
  then show ?case by simp
kuncar@48623
   397
next
kuncar@48623
   398
  case right_empty
wenzelm@60580
   399
  then show ?case by (auto intro: left_le_key simp del: rbt_sorted.simps)
kuncar@48623
   400
next
kuncar@48623
   401
  case (right_non_empty c t1 k v t2 y)
wenzelm@60580
   402
  then consider "y = k" | "y \<in> set (RBT_Impl.keys t2)" | "y \<in> set (RBT_Impl.keys t1)"
wenzelm@60580
   403
    by auto
wenzelm@60580
   404
  then show ?case 
wenzelm@60580
   405
  proof cases
wenzelm@60580
   406
    case 1
wenzelm@60580
   407
    with right_non_empty show ?thesis
wenzelm@60580
   408
      by (auto simp add: rbt_max_opt_Branch intro: key_le_right rbt_max_opt_in_set)
wenzelm@60580
   409
  next
wenzelm@60580
   410
    case 2
wenzelm@60580
   411
    with right_non_empty show ?thesis
wenzelm@60580
   412
      by (auto simp add: rbt_max_opt_Branch)
wenzelm@60580
   413
  next 
wenzelm@60580
   414
    case y: 3
wenzelm@60580
   415
    have "rbt_max_opt t2 \<ge> k"
wenzelm@60580
   416
      using right_non_empty by (simp add: key_le_right rbt_max_opt_in_set)
wenzelm@60580
   417
    moreover have "y \<le> k"
wenzelm@60580
   418
      using right_non_empty y by (simp add: left_le_key)
wenzelm@60580
   419
    ultimately show ?thesis
wenzelm@60580
   420
      using right_non_empty by (simp add: rbt_max_opt_Branch)
wenzelm@60580
   421
  qed
kuncar@48623
   422
qed
kuncar@48623
   423
kuncar@48623
   424
lemma rbt_max_eq_rbt_max_opt:
kuncar@48623
   425
  assumes "t \<noteq> RBT_Impl.Empty"
kuncar@48623
   426
  assumes "is_rbt t"
kuncar@48623
   427
  shows "rbt_max t = rbt_max_opt t"
kuncar@48623
   428
proof -
haftmann@51489
   429
  from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
haftmann@51489
   430
  with assms show ?thesis
haftmann@51489
   431
    by (simp add: rbt_max_def rbt_fold1_keys_def rbt_max_opt_is_max
haftmann@51540
   432
      Max.set_eq_fold [symmetric] Max_eqI rbt_max_opt_in_set)
kuncar@48623
   433
qed
kuncar@48623
   434
kuncar@48623
   435
kuncar@48623
   436
(** abstract **)
kuncar@48623
   437
kuncar@56019
   438
context includes rbt.lifting begin
kuncar@48623
   439
lift_definition fold1_keys :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> 'a"
kuncar@55565
   440
  is rbt_fold1_keys .
kuncar@48623
   441
kuncar@48623
   442
lemma fold1_keys_def_alt:
kuncar@56019
   443
  "fold1_keys f t = List.fold f (tl (RBT.keys t)) (hd (RBT.keys t))"
kuncar@48623
   444
  by transfer (simp add: rbt_fold1_keys_def)
kuncar@48623
   445
kuncar@48623
   446
lemma finite_fold1_fold1_keys:
haftmann@51489
   447
  assumes "semilattice f"
kuncar@56019
   448
  assumes "\<not> RBT.is_empty t"
haftmann@51489
   449
  shows "semilattice_set.F f (Set t) = fold1_keys f t"
kuncar@48623
   450
proof -
wenzelm@60500
   451
  from \<open>semilattice f\<close> interpret semilattice_set f by (rule semilattice_set.intro)
kuncar@48623
   452
  show ?thesis using assms 
haftmann@51489
   453
    by (auto simp: fold1_keys_def_alt set_keys fold_def_alt non_empty_keys set_eq_fold [symmetric])
kuncar@48623
   454
qed
kuncar@48623
   455
kuncar@48623
   456
(* minimum *)
kuncar@48623
   457
kuncar@55565
   458
lift_definition r_min :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min .
kuncar@48623
   459
kuncar@55565
   460
lift_definition r_min_opt :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min_opt .
kuncar@48623
   461
kuncar@48623
   462
lemma r_min_alt_def: "r_min t = fold1_keys min t"
kuncar@48623
   463
by transfer (simp add: rbt_min_def)
kuncar@48623
   464
kuncar@48623
   465
lemma r_min_eq_r_min_opt:
kuncar@56019
   466
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   467
  shows "r_min t = r_min_opt t"
kuncar@48623
   468
using assms unfolding is_empty_empty by transfer (auto intro: rbt_min_eq_rbt_min_opt)
kuncar@48623
   469
kuncar@48623
   470
lemma fold_keys_min_top_eq:
kuncar@48623
   471
  fixes t :: "('a :: {linorder, bounded_lattice_top}, unit) rbt"
kuncar@56019
   472
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   473
  shows "fold_keys min t top = fold1_keys min t"
kuncar@48623
   474
proof -
kuncar@48623
   475
  have *: "\<And>t. RBT_Impl.keys t \<noteq> [] \<Longrightarrow> List.fold min (RBT_Impl.keys t) top = 
kuncar@48623
   476
    List.fold min (hd(RBT_Impl.keys t) # tl(RBT_Impl.keys t)) top"
kuncar@48623
   477
    by (simp add: hd_Cons_tl[symmetric])
kuncar@48623
   478
  { fix x :: "_ :: {linorder, bounded_lattice_top}" and xs
kuncar@48623
   479
    have "List.fold min (x#xs) top = List.fold min xs x"
kuncar@48623
   480
    by (simp add: inf_min[symmetric])
kuncar@48623
   481
  } note ** = this
kuncar@48623
   482
  show ?thesis using assms
kuncar@48623
   483
    unfolding fold_keys_def_alt fold1_keys_def_alt is_empty_empty
kuncar@48623
   484
    apply transfer 
kuncar@48623
   485
    apply (case_tac t) 
kuncar@48623
   486
    apply simp 
kuncar@48623
   487
    apply (subst *)
kuncar@48623
   488
    apply simp
kuncar@48623
   489
    apply (subst **)
kuncar@48623
   490
    apply simp
kuncar@48623
   491
  done
kuncar@48623
   492
qed
kuncar@48623
   493
kuncar@48623
   494
(* maximum *)
kuncar@48623
   495
kuncar@55565
   496
lift_definition r_max :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_max .
kuncar@48623
   497
kuncar@55565
   498
lift_definition r_max_opt :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_max_opt .
kuncar@48623
   499
kuncar@48623
   500
lemma r_max_alt_def: "r_max t = fold1_keys max t"
kuncar@48623
   501
by transfer (simp add: rbt_max_def)
kuncar@48623
   502
kuncar@48623
   503
lemma r_max_eq_r_max_opt:
kuncar@56019
   504
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   505
  shows "r_max t = r_max_opt t"
kuncar@48623
   506
using assms unfolding is_empty_empty by transfer (auto intro: rbt_max_eq_rbt_max_opt)
kuncar@48623
   507
kuncar@48623
   508
lemma fold_keys_max_bot_eq:
kuncar@48623
   509
  fixes t :: "('a :: {linorder, bounded_lattice_bot}, unit) rbt"
kuncar@56019
   510
  assumes "\<not> (RBT.is_empty t)"
kuncar@48623
   511
  shows "fold_keys max t bot = fold1_keys max t"
kuncar@48623
   512
proof -
kuncar@48623
   513
  have *: "\<And>t. RBT_Impl.keys t \<noteq> [] \<Longrightarrow> List.fold max (RBT_Impl.keys t) bot = 
kuncar@48623
   514
    List.fold max (hd(RBT_Impl.keys t) # tl(RBT_Impl.keys t)) bot"
kuncar@48623
   515
    by (simp add: hd_Cons_tl[symmetric])
kuncar@48623
   516
  { fix x :: "_ :: {linorder, bounded_lattice_bot}" and xs
kuncar@48623
   517
    have "List.fold max (x#xs) bot = List.fold max xs x"
kuncar@48623
   518
    by (simp add: sup_max[symmetric])
kuncar@48623
   519
  } note ** = this
kuncar@48623
   520
  show ?thesis using assms
kuncar@48623
   521
    unfolding fold_keys_def_alt fold1_keys_def_alt is_empty_empty
kuncar@48623
   522
    apply transfer 
kuncar@48623
   523
    apply (case_tac t) 
kuncar@48623
   524
    apply simp 
kuncar@48623
   525
    apply (subst *)
kuncar@48623
   526
    apply simp
kuncar@48623
   527
    apply (subst **)
kuncar@48623
   528
    apply simp
kuncar@48623
   529
  done
kuncar@48623
   530
qed
kuncar@48623
   531
kuncar@56019
   532
end
kuncar@48623
   533
wenzelm@60500
   534
section \<open>Code equations\<close>
kuncar@48623
   535
kuncar@48623
   536
code_datatype Set Coset
kuncar@48623
   537
blanchet@57816
   538
declare list.set[code] (* needed? *)
kuncar@50996
   539
kuncar@48623
   540
lemma empty_Set [code]:
kuncar@48623
   541
  "Set.empty = Set RBT.empty"
kuncar@48623
   542
by (auto simp: Set_def)
kuncar@48623
   543
kuncar@48623
   544
lemma UNIV_Coset [code]:
kuncar@48623
   545
  "UNIV = Coset RBT.empty"
kuncar@48623
   546
by (auto simp: Set_def)
kuncar@48623
   547
kuncar@48623
   548
lemma is_empty_Set [code]:
kuncar@48623
   549
  "Set.is_empty (Set t) = RBT.is_empty t"
kuncar@48623
   550
  unfolding Set.is_empty_def by (auto simp: fun_eq_iff Set_def intro: lookup_empty_empty[THEN iffD1])
kuncar@48623
   551
kuncar@48623
   552
lemma compl_code [code]:
kuncar@48623
   553
  "- Set xs = Coset xs"
kuncar@48623
   554
  "- Coset xs = Set xs"
kuncar@48623
   555
by (simp_all add: Set_def)
kuncar@48623
   556
kuncar@48623
   557
lemma member_code [code]:
kuncar@48623
   558
  "x \<in> (Set t) = (RBT.lookup t x = Some ())"
kuncar@48623
   559
  "x \<in> (Coset t) = (RBT.lookup t x = None)"
kuncar@48623
   560
by (simp_all add: Set_def)
kuncar@48623
   561
kuncar@48623
   562
lemma insert_code [code]:
kuncar@48623
   563
  "Set.insert x (Set t) = Set (RBT.insert x () t)"
kuncar@48623
   564
  "Set.insert x (Coset t) = Coset (RBT.delete x t)"
kuncar@48623
   565
by (auto simp: Set_def)
kuncar@48623
   566
kuncar@48623
   567
lemma remove_code [code]:
kuncar@48623
   568
  "Set.remove x (Set t) = Set (RBT.delete x t)"
kuncar@48623
   569
  "Set.remove x (Coset t) = Coset (RBT.insert x () t)"
kuncar@48623
   570
by (auto simp: Set_def)
kuncar@48623
   571
kuncar@48623
   572
lemma union_Set [code]:
kuncar@48623
   573
  "Set t \<union> A = fold_keys Set.insert t A"
kuncar@48623
   574
proof -
kuncar@48623
   575
  interpret comp_fun_idem Set.insert
kuncar@48623
   576
    by (fact comp_fun_idem_insert)
wenzelm@60500
   577
  from finite_fold_fold_keys[OF \<open>comp_fun_commute Set.insert\<close>]
kuncar@48623
   578
  show ?thesis by (auto simp add: union_fold_insert)
kuncar@48623
   579
qed
kuncar@48623
   580
kuncar@48623
   581
lemma inter_Set [code]:
kuncar@48623
   582
  "A \<inter> Set t = rbt_filter (\<lambda>k. k \<in> A) t"
kuncar@49758
   583
by (simp add: inter_Set_filter Set_filter_rbt_filter)
kuncar@48623
   584
kuncar@48623
   585
lemma minus_Set [code]:
kuncar@48623
   586
  "A - Set t = fold_keys Set.remove t A"
kuncar@48623
   587
proof -
kuncar@48623
   588
  interpret comp_fun_idem Set.remove
kuncar@48623
   589
    by (fact comp_fun_idem_remove)
wenzelm@60500
   590
  from finite_fold_fold_keys[OF \<open>comp_fun_commute Set.remove\<close>]
kuncar@48623
   591
  show ?thesis by (auto simp add: minus_fold_remove)
kuncar@48623
   592
qed
kuncar@48623
   593
kuncar@48623
   594
lemma union_Coset [code]:
kuncar@48623
   595
  "Coset t \<union> A = - rbt_filter (\<lambda>k. k \<notin> A) t"
kuncar@48623
   596
proof -
kuncar@48623
   597
  have *: "\<And>A B. (-A \<union> B) = -(-B \<inter> A)" by blast
kuncar@48623
   598
  show ?thesis by (simp del: boolean_algebra_class.compl_inf add: * inter_Set)
kuncar@48623
   599
qed
kuncar@48623
   600
 
kuncar@48623
   601
lemma union_Set_Set [code]:
kuncar@56019
   602
  "Set t1 \<union> Set t2 = Set (RBT.union t1 t2)"
kuncar@48623
   603
by (auto simp add: lookup_union map_add_Some_iff Set_def)
kuncar@48623
   604
kuncar@48623
   605
lemma inter_Coset [code]:
kuncar@48623
   606
  "A \<inter> Coset t = fold_keys Set.remove t A"
kuncar@48623
   607
by (simp add: Diff_eq [symmetric] minus_Set)
kuncar@48623
   608
kuncar@48623
   609
lemma inter_Coset_Coset [code]:
kuncar@56019
   610
  "Coset t1 \<inter> Coset t2 = Coset (RBT.union t1 t2)"
kuncar@48623
   611
by (auto simp add: lookup_union map_add_Some_iff Set_def)
kuncar@48623
   612
kuncar@48623
   613
lemma minus_Coset [code]:
kuncar@48623
   614
  "A - Coset t = rbt_filter (\<lambda>k. k \<in> A) t"
kuncar@48623
   615
by (simp add: inter_Set[simplified Int_commute])
kuncar@48623
   616
kuncar@49757
   617
lemma filter_Set [code]:
kuncar@49757
   618
  "Set.filter P (Set t) = (rbt_filter P t)"
kuncar@49758
   619
by (auto simp add: Set_filter_rbt_filter)
kuncar@48623
   620
kuncar@48623
   621
lemma image_Set [code]:
kuncar@48623
   622
  "image f (Set t) = fold_keys (\<lambda>k A. Set.insert (f k) A) t {}"
kuncar@48623
   623
proof -
kuncar@48623
   624
  have "comp_fun_commute (\<lambda>k. Set.insert (f k))" by default auto
kuncar@48623
   625
  then show ?thesis by (auto simp add: image_fold_insert intro!: finite_fold_fold_keys)
kuncar@48623
   626
qed
kuncar@48623
   627
kuncar@48623
   628
lemma Ball_Set [code]:
kuncar@56019
   629
  "Ball (Set t) P \<longleftrightarrow> RBT.foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t True"
kuncar@48623
   630
proof -
kuncar@48623
   631
  have "comp_fun_commute (\<lambda>k s. s \<and> P k)" by default auto
kuncar@48623
   632
  then show ?thesis 
kuncar@48623
   633
    by (simp add: foldi_fold_conj[symmetric] Ball_fold finite_fold_fold_keys)
kuncar@48623
   634
qed
kuncar@48623
   635
kuncar@48623
   636
lemma Bex_Set [code]:
kuncar@56019
   637
  "Bex (Set t) P \<longleftrightarrow> RBT.foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t False"
kuncar@48623
   638
proof -
kuncar@48623
   639
  have "comp_fun_commute (\<lambda>k s. s \<or> P k)" by default auto
kuncar@48623
   640
  then show ?thesis 
kuncar@48623
   641
    by (simp add: foldi_fold_disj[symmetric] Bex_fold finite_fold_fold_keys)
kuncar@48623
   642
qed
kuncar@48623
   643
kuncar@48623
   644
lemma subset_code [code]:
kuncar@48623
   645
  "Set t \<le> B \<longleftrightarrow> (\<forall>x\<in>Set t. x \<in> B)"
kuncar@48623
   646
  "A \<le> Coset t \<longleftrightarrow> (\<forall>y\<in>Set t. y \<notin> A)"
kuncar@48623
   647
by auto
kuncar@48623
   648
kuncar@48623
   649
lemma subset_Coset_empty_Set_empty [code]:
kuncar@56019
   650
  "Coset t1 \<le> Set t2 \<longleftrightarrow> (case (RBT.impl_of t1, RBT.impl_of t2) of 
kuncar@48623
   651
    (rbt.Empty, rbt.Empty) => False |
Andreas@53745
   652
    (_, _) => Code.abort (STR ''non_empty_trees'') (\<lambda>_. Coset t1 \<le> Set t2))"
kuncar@48623
   653
proof -
kuncar@56019
   654
  have *: "\<And>t. RBT.impl_of t = rbt.Empty \<Longrightarrow> t = RBT rbt.Empty"
kuncar@48623
   655
    by (subst(asm) RBT_inverse[symmetric]) (auto simp: impl_of_inject)
kuncar@56519
   656
  have **: "eq_onp is_rbt rbt.Empty rbt.Empty" unfolding eq_onp_def by simp
kuncar@48623
   657
  show ?thesis  
Andreas@53745
   658
    by (auto simp: Set_def lookup.abs_eq[OF **] dest!: * split: rbt.split)
kuncar@48623
   659
qed
kuncar@48623
   660
wenzelm@60500
   661
text \<open>A frequent case -- avoid intermediate sets\<close>
kuncar@48623
   662
lemma [code_unfold]:
kuncar@56019
   663
  "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
   664
by (simp add: subset_code Ball_Set)
kuncar@48623
   665
kuncar@48623
   666
lemma card_Set [code]:
kuncar@48623
   667
  "card (Set t) = fold_keys (\<lambda>_ n. n + 1) t 0"
haftmann@51489
   668
  by (auto simp add: card.eq_fold intro: finite_fold_fold_keys comp_fun_commute_const)
kuncar@48623
   669
kuncar@48623
   670
lemma setsum_Set [code]:
kuncar@48623
   671
  "setsum f (Set xs) = fold_keys (plus o f) xs 0"
kuncar@48623
   672
proof -
haftmann@57514
   673
  have "comp_fun_commute (\<lambda>x. op + (f x))" by default (auto simp: ac_simps)
kuncar@48623
   674
  then show ?thesis 
haftmann@51489
   675
    by (auto simp add: setsum.eq_fold finite_fold_fold_keys o_def)
kuncar@48623
   676
qed
kuncar@48623
   677
kuncar@48623
   678
lemma the_elem_set [code]:
kuncar@48623
   679
  fixes t :: "('a :: linorder, unit) rbt"
kuncar@56019
   680
  shows "the_elem (Set t) = (case RBT.impl_of t of 
kuncar@48623
   681
    (Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty) \<Rightarrow> x
Andreas@53745
   682
    | _ \<Rightarrow> Code.abort (STR ''not_a_singleton_tree'') (\<lambda>_. the_elem (Set t)))"
kuncar@48623
   683
proof -
kuncar@48623
   684
  {
kuncar@48623
   685
    fix x :: "'a :: linorder"
kuncar@48623
   686
    let ?t = "Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty" 
kuncar@48623
   687
    have *:"?t \<in> {t. is_rbt t}" unfolding is_rbt_def by auto
kuncar@56519
   688
    then have **:"eq_onp is_rbt ?t ?t" unfolding eq_onp_def by auto
kuncar@48623
   689
kuncar@56019
   690
    have "RBT.impl_of t = ?t \<Longrightarrow> the_elem (Set t) = x" 
kuncar@48623
   691
      by (subst(asm) RBT_inverse[symmetric, OF *])
kuncar@48623
   692
        (auto simp: Set_def the_elem_def lookup.abs_eq[OF **] impl_of_inject)
kuncar@48623
   693
  }
Andreas@53745
   694
  then show ?thesis
kuncar@48623
   695
    by(auto split: rbt.split unit.split color.split)
kuncar@48623
   696
qed
kuncar@48623
   697
kuncar@48623
   698
lemma Pow_Set [code]:
kuncar@48623
   699
  "Pow (Set t) = fold_keys (\<lambda>x A. A \<union> Set.insert x ` A) t {{}}"
kuncar@48623
   700
by (simp add: Pow_fold finite_fold_fold_keys[OF comp_fun_commute_Pow_fold])
kuncar@48623
   701
kuncar@48623
   702
lemma product_Set [code]:
kuncar@48623
   703
  "Product_Type.product (Set t1) (Set t2) = 
kuncar@48623
   704
    fold_keys (\<lambda>x A. fold_keys (\<lambda>y. Set.insert (x, y)) t2 A) t1 {}"
kuncar@48623
   705
proof -
kuncar@48623
   706
  have *:"\<And>x. comp_fun_commute (\<lambda>y. Set.insert (x, y))" by default auto
kuncar@48623
   707
  show ?thesis using finite_fold_fold_keys[OF comp_fun_commute_product_fold, of "Set t2" "{}" "t1"]  
kuncar@48623
   708
    by (simp add: product_fold Product_Type.product_def finite_fold_fold_keys[OF *])
kuncar@48623
   709
qed
kuncar@48623
   710
kuncar@48623
   711
lemma Id_on_Set [code]:
kuncar@48623
   712
  "Id_on (Set t) =  fold_keys (\<lambda>x. Set.insert (x, x)) t {}"
kuncar@48623
   713
proof -
kuncar@48623
   714
  have "comp_fun_commute (\<lambda>x. Set.insert (x, x))" by default auto
kuncar@48623
   715
  then show ?thesis
kuncar@48623
   716
    by (auto simp add: Id_on_fold intro!: finite_fold_fold_keys)
kuncar@48623
   717
qed
kuncar@48623
   718
kuncar@48623
   719
lemma Image_Set [code]:
kuncar@48623
   720
  "(Set t) `` S = fold_keys (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) t {}"
kuncar@48623
   721
by (auto simp add: Image_fold finite_fold_fold_keys[OF comp_fun_commute_Image_fold])
kuncar@48623
   722
kuncar@48623
   723
lemma trancl_set_ntrancl [code]:
kuncar@48623
   724
  "trancl (Set t) = ntrancl (card (Set t) - 1) (Set t)"
kuncar@48623
   725
by (simp add: finite_trancl_ntranl)
kuncar@48623
   726
kuncar@48623
   727
lemma relcomp_Set[code]:
kuncar@48623
   728
  "(Set t1) O (Set t2) = fold_keys 
kuncar@48623
   729
    (\<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
   730
proof -
kuncar@48623
   731
  interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
kuncar@48623
   732
  have *: "\<And>x y. comp_fun_commute (\<lambda>(w, z) A'. if y = w then Set.insert (x, z) A' else A')"
kuncar@48623
   733
    by default (auto simp add: fun_eq_iff)
kuncar@48623
   734
  show ?thesis using finite_fold_fold_keys[OF comp_fun_commute_relcomp_fold, of "Set t2" "{}" t1]
kuncar@48623
   735
    by (simp add: relcomp_fold finite_fold_fold_keys[OF *])
kuncar@48623
   736
qed
kuncar@48623
   737
kuncar@48623
   738
lemma wf_set [code]:
kuncar@48623
   739
  "wf (Set t) = acyclic (Set t)"
kuncar@48623
   740
by (simp add: wf_iff_acyclic_if_finite)
kuncar@48623
   741
kuncar@48623
   742
lemma Min_fin_set_fold [code]:
Andreas@53745
   743
  "Min (Set t) = 
kuncar@56019
   744
  (if RBT.is_empty t
Andreas@53745
   745
   then Code.abort (STR ''not_non_empty_tree'') (\<lambda>_. Min (Set t))
Andreas@53745
   746
   else r_min_opt t)"
kuncar@48623
   747
proof -
haftmann@51489
   748
  have *: "semilattice (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
haftmann@51489
   749
  with finite_fold1_fold1_keys [OF *, folded Min_def]
kuncar@48623
   750
  show ?thesis
Andreas@53745
   751
    by (simp add: r_min_alt_def r_min_eq_r_min_opt [symmetric])  
kuncar@48623
   752
qed
kuncar@48623
   753
kuncar@48623
   754
lemma Inf_fin_set_fold [code]:
kuncar@48623
   755
  "Inf_fin (Set t) = Min (Set t)"
kuncar@48623
   756
by (simp add: inf_min Inf_fin_def Min_def)
kuncar@48623
   757
kuncar@48623
   758
lemma Inf_Set_fold:
kuncar@48623
   759
  fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
kuncar@56019
   760
  shows "Inf (Set t) = (if RBT.is_empty t then top else r_min_opt t)"
kuncar@48623
   761
proof -
kuncar@48623
   762
  have "comp_fun_commute (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" by default (simp add: fun_eq_iff ac_simps)
kuncar@56019
   763
  then have "t \<noteq> RBT.empty \<Longrightarrow> Finite_Set.fold min top (Set t) = fold1_keys min t"
kuncar@48623
   764
    by (simp add: finite_fold_fold_keys fold_keys_min_top_eq)
kuncar@48623
   765
  then show ?thesis 
kuncar@48623
   766
    by (auto simp add: Inf_fold_inf inf_min empty_Set[symmetric] r_min_eq_r_min_opt[symmetric] r_min_alt_def)
kuncar@48623
   767
qed
kuncar@48623
   768
kuncar@48623
   769
definition Inf' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a" where [code del]: "Inf' x = Inf x"
kuncar@48623
   770
declare Inf'_def[symmetric, code_unfold]
kuncar@48623
   771
declare Inf_Set_fold[folded Inf'_def, code]
kuncar@48623
   772
haftmann@56212
   773
lemma INF_Set_fold [code]:
hoelzl@54263
   774
  fixes f :: "_ \<Rightarrow> 'a::complete_lattice"
haftmann@56218
   775
  shows "INFIMUM (Set t) f = fold_keys (inf \<circ> f) t top"
kuncar@48623
   776
proof -
kuncar@48623
   777
  have "comp_fun_commute ((inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a) \<circ> f)" 
kuncar@48623
   778
    by default (auto simp add: fun_eq_iff ac_simps)
kuncar@48623
   779
  then show ?thesis
kuncar@48623
   780
    by (auto simp: INF_fold_inf finite_fold_fold_keys)
kuncar@48623
   781
qed
kuncar@48623
   782
kuncar@48623
   783
lemma Max_fin_set_fold [code]:
Andreas@53745
   784
  "Max (Set t) = 
kuncar@56019
   785
  (if RBT.is_empty t
Andreas@53745
   786
   then Code.abort (STR ''not_non_empty_tree'') (\<lambda>_. Max (Set t))
Andreas@53745
   787
   else r_max_opt t)"
kuncar@48623
   788
proof -
haftmann@51489
   789
  have *: "semilattice (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
haftmann@51489
   790
  with finite_fold1_fold1_keys [OF *, folded Max_def]
kuncar@48623
   791
  show ?thesis
Andreas@53745
   792
    by (simp add: r_max_alt_def r_max_eq_r_max_opt [symmetric])  
kuncar@48623
   793
qed
kuncar@48623
   794
kuncar@48623
   795
lemma Sup_fin_set_fold [code]:
kuncar@48623
   796
  "Sup_fin (Set t) = Max (Set t)"
kuncar@48623
   797
by (simp add: sup_max Sup_fin_def Max_def)
kuncar@48623
   798
kuncar@48623
   799
lemma Sup_Set_fold:
kuncar@48623
   800
  fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
kuncar@56019
   801
  shows "Sup (Set t) = (if RBT.is_empty t then bot else r_max_opt t)"
kuncar@48623
   802
proof -
kuncar@48623
   803
  have "comp_fun_commute (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" by default (simp add: fun_eq_iff ac_simps)
kuncar@56019
   804
  then have "t \<noteq> RBT.empty \<Longrightarrow> Finite_Set.fold max bot (Set t) = fold1_keys max t"
kuncar@48623
   805
    by (simp add: finite_fold_fold_keys fold_keys_max_bot_eq)
kuncar@48623
   806
  then show ?thesis 
kuncar@48623
   807
    by (auto simp add: Sup_fold_sup sup_max empty_Set[symmetric] r_max_eq_r_max_opt[symmetric] r_max_alt_def)
kuncar@48623
   808
qed
kuncar@48623
   809
kuncar@48623
   810
definition Sup' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a" where [code del]: "Sup' x = Sup x"
kuncar@48623
   811
declare Sup'_def[symmetric, code_unfold]
kuncar@48623
   812
declare Sup_Set_fold[folded Sup'_def, code]
kuncar@48623
   813
haftmann@56212
   814
lemma SUP_Set_fold [code]:
hoelzl@54263
   815
  fixes f :: "_ \<Rightarrow> 'a::complete_lattice"
haftmann@56218
   816
  shows "SUPREMUM (Set t) f = fold_keys (sup \<circ> f) t bot"
kuncar@48623
   817
proof -
kuncar@48623
   818
  have "comp_fun_commute ((sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a) \<circ> f)" 
kuncar@48623
   819
    by default (auto simp add: fun_eq_iff ac_simps)
kuncar@48623
   820
  then show ?thesis
kuncar@48623
   821
    by (auto simp: SUP_fold_sup finite_fold_fold_keys)
kuncar@48623
   822
qed
kuncar@48623
   823
kuncar@48623
   824
lemma sorted_list_set[code]:
kuncar@56019
   825
  "sorted_list_of_set (Set t) = RBT.keys t"
kuncar@48623
   826
by (auto simp add: set_keys intro: sorted_distinct_set_unique) 
kuncar@48623
   827
nipkow@53955
   828
lemma Bleast_code [code]:
kuncar@56019
   829
 "Bleast (Set t) P = (case filter P (RBT.keys t) of
nipkow@53955
   830
    x#xs \<Rightarrow> x |
nipkow@53955
   831
    [] \<Rightarrow> abort_Bleast (Set t) P)"
kuncar@56019
   832
proof (cases "filter P (RBT.keys t)")
nipkow@53955
   833
  case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def)
nipkow@53955
   834
next
nipkow@53955
   835
  case (Cons x ys)
nipkow@53955
   836
  have "(LEAST x. x \<in> Set t \<and> P x) = x"
nipkow@53955
   837
  proof (rule Least_equality)
nipkow@53955
   838
    show "x \<in> Set t \<and> P x" using Cons[symmetric]
nipkow@53955
   839
      by(auto simp add: set_keys Cons_eq_filter_iff)
nipkow@53955
   840
    next
nipkow@53955
   841
      fix y assume "y : Set t \<and> P y"
nipkow@53955
   842
      then show "x \<le> y" using Cons[symmetric]
nipkow@53955
   843
        by(auto simp add: set_keys Cons_eq_filter_iff)
nipkow@53955
   844
          (metis sorted_Cons sorted_append sorted_keys)
nipkow@53955
   845
  qed
nipkow@53955
   846
  thus ?thesis using Cons by (simp add: Bleast_def)
nipkow@53955
   847
qed
nipkow@53955
   848
kuncar@48623
   849
hide_const (open) RBT_Set.Set RBT_Set.Coset
kuncar@48623
   850
kuncar@48623
   851
end