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