src/HOL/Library/RBT_Mapping.thy
author wenzelm
Fri Jan 04 23:22:53 2019 +0100 (6 months ago)
changeset 69593 3dda49e08b9d
parent 68484 59793df7f853
permissions -rw-r--r--
isabelle update -u control_cartouches;
kuncar@49929
     1
(*  Title:      HOL/Library/RBT_Mapping.thy
kuncar@49929
     2
    Author:     Florian Haftmann and Ondrej Kuncar
kuncar@49929
     3
*)
bulwahn@43124
     4
wenzelm@60500
     5
section \<open>Implementation of mappings with Red-Black Trees\<close>
bulwahn@43124
     6
bulwahn@43124
     7
(*<*)
bulwahn@43124
     8
theory RBT_Mapping
bulwahn@43124
     9
imports RBT Mapping
bulwahn@43124
    10
begin
bulwahn@43124
    11
wenzelm@60500
    12
subsection \<open>Implementation of mappings\<close>
bulwahn@43124
    13
kuncar@56019
    14
context includes rbt.lifting begin
wenzelm@61076
    15
lift_definition Mapping :: "('a::linorder, 'b) rbt \<Rightarrow> ('a, 'b) mapping" is RBT.lookup .
kuncar@56019
    16
end
bulwahn@43124
    17
bulwahn@43124
    18
code_datatype Mapping
bulwahn@43124
    19
kuncar@56019
    20
context includes rbt.lifting begin
kuncar@56019
    21
bulwahn@43124
    22
lemma lookup_Mapping [simp, code]:
kuncar@56019
    23
  "Mapping.lookup (Mapping t) = RBT.lookup t"
kuncar@49929
    24
   by (transfer fixing: t) rule
bulwahn@43124
    25
kuncar@56019
    26
lemma empty_Mapping [code]: "Mapping.empty = Mapping RBT.empty"
kuncar@49929
    27
proof -
kuncar@49929
    28
  note RBT.empty.transfer[transfer_rule del]
kuncar@49929
    29
  show ?thesis by transfer simp
kuncar@49929
    30
qed
bulwahn@43124
    31
bulwahn@43124
    32
lemma is_empty_Mapping [code]:
kuncar@56019
    33
  "Mapping.is_empty (Mapping t) \<longleftrightarrow> RBT.is_empty t"
kuncar@49929
    34
  unfolding is_empty_def by (transfer fixing: t) simp
bulwahn@43124
    35
bulwahn@43124
    36
lemma insert_Mapping [code]:
kuncar@56019
    37
  "Mapping.update k v (Mapping t) = Mapping (RBT.insert k v t)"
kuncar@49929
    38
  by (transfer fixing: t) simp
bulwahn@43124
    39
bulwahn@43124
    40
lemma delete_Mapping [code]:
kuncar@56019
    41
  "Mapping.delete k (Mapping t) = Mapping (RBT.delete k t)"
kuncar@49929
    42
  by (transfer fixing: t) simp
bulwahn@43124
    43
bulwahn@43124
    44
lemma map_entry_Mapping [code]:
kuncar@56019
    45
  "Mapping.map_entry k f (Mapping t) = Mapping (RBT.map_entry k f t)"
wenzelm@63649
    46
  apply (transfer fixing: t)
wenzelm@63649
    47
  apply (case_tac "RBT.lookup t k")
wenzelm@63649
    48
   apply auto
wenzelm@63649
    49
  done
bulwahn@43124
    50
bulwahn@43124
    51
lemma keys_Mapping [code]:
kuncar@56019
    52
  "Mapping.keys (Mapping t) = set (RBT.keys t)"
kuncar@49929
    53
by (transfer fixing: t) (simp add: lookup_keys)
bulwahn@43124
    54
bulwahn@43124
    55
lemma ordered_keys_Mapping [code]:
kuncar@56019
    56
  "Mapping.ordered_keys (Mapping t) = RBT.keys t"
kuncar@49929
    57
unfolding ordered_keys_def 
kuncar@49929
    58
by (transfer fixing: t) (auto simp add: lookup_keys intro: sorted_distinct_set_unique)
bulwahn@43124
    59
bulwahn@43124
    60
lemma Mapping_size_card_keys: (*FIXME*)
bulwahn@43124
    61
  "Mapping.size m = card (Mapping.keys m)"
kuncar@49929
    62
unfolding size_def by transfer simp
bulwahn@43124
    63
bulwahn@43124
    64
lemma size_Mapping [code]:
kuncar@56019
    65
  "Mapping.size (Mapping t) = length (RBT.keys t)"
kuncar@49929
    66
unfolding size_def
kuncar@49929
    67
by (transfer fixing: t) (simp add: lookup_keys distinct_card)
bulwahn@43124
    68
kuncar@49929
    69
context
kuncar@49929
    70
  notes RBT.bulkload.transfer[transfer_rule del]
kuncar@49929
    71
begin
wenzelm@60373
    72
wenzelm@60373
    73
lemma tabulate_Mapping [code]:
wenzelm@60373
    74
  "Mapping.tabulate ks f = Mapping (RBT.bulkload (List.map (\<lambda>k. (k, f k)) ks))"
wenzelm@60373
    75
by transfer (simp add: map_of_map_restrict)
wenzelm@60373
    76
wenzelm@60373
    77
lemma bulkload_Mapping [code]:
wenzelm@60373
    78
  "Mapping.bulkload vs = Mapping (RBT.bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
wenzelm@60373
    79
by transfer (simp add: map_of_map_restrict fun_eq_iff)
wenzelm@60373
    80
kuncar@49929
    81
end
bulwahn@43124
    82
eberlm@63194
    83
lemma map_values_Mapping [code]: 
eberlm@63194
    84
  "Mapping.map_values f (Mapping t) = Mapping (RBT.map f t)"
eberlm@63194
    85
  by (transfer fixing: t) (auto simp: fun_eq_iff)
eberlm@63194
    86
eberlm@63194
    87
lemma filter_Mapping [code]: 
eberlm@63194
    88
  "Mapping.filter P (Mapping t) = Mapping (RBT.filter P t)"
eberlm@63194
    89
  by (transfer' fixing: P t) (simp add: RBT.lookup_filter fun_eq_iff)
eberlm@63194
    90
eberlm@63194
    91
lemma combine_with_key_Mapping [code]:
eberlm@63194
    92
  "Mapping.combine_with_key f (Mapping t1) (Mapping t2) =
eberlm@63194
    93
     Mapping (RBT.combine_with_key f t1 t2)"
eberlm@63194
    94
  by (transfer fixing: f t1 t2) (simp_all add: fun_eq_iff)
eberlm@63194
    95
eberlm@63194
    96
lemma combine_Mapping [code]:
eberlm@63194
    97
  "Mapping.combine f (Mapping t1) (Mapping t2) =
eberlm@63194
    98
     Mapping (RBT.combine f t1 t2)"
eberlm@63194
    99
  by (transfer fixing: f t1 t2) (simp_all add: fun_eq_iff)
eberlm@63194
   100
bulwahn@43124
   101
lemma equal_Mapping [code]:
kuncar@56019
   102
  "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> RBT.entries t1 = RBT.entries t2"
haftmann@51161
   103
  by (transfer fixing: t1 t2) (simp add: entries_lookup)
bulwahn@43124
   104
bulwahn@43124
   105
lemma [code nbe]:
bulwahn@43124
   106
  "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
bulwahn@43124
   107
  by (fact equal_refl)
bulwahn@43124
   108
kuncar@56019
   109
end
bulwahn@43124
   110
bulwahn@43124
   111
(*>*)
bulwahn@43124
   112
wenzelm@60500
   113
text \<open>
bulwahn@43124
   114
  This theory defines abstract red-black trees as an efficient
bulwahn@43124
   115
  representation of finite maps, backed by the implementation
wenzelm@69593
   116
  in \<^theory>\<open>HOL-Library.RBT_Impl\<close>.
wenzelm@60500
   117
\<close>
bulwahn@43124
   118
wenzelm@60500
   119
subsection \<open>Data type and invariant\<close>
bulwahn@43124
   120
wenzelm@60500
   121
text \<open>
wenzelm@69593
   122
  The type \<^typ>\<open>('k, 'v) RBT_Impl.rbt\<close> denotes red-black trees with
wenzelm@69593
   123
  keys of type \<^typ>\<open>'k\<close> and values of type \<^typ>\<open>'v\<close>. To function
wenzelm@61585
   124
  properly, the key type musorted belong to the \<open>linorder\<close>
bulwahn@43124
   125
  class.
bulwahn@43124
   126
wenzelm@69593
   127
  A value \<^term>\<open>t\<close> of this type is a valid red-black tree if it
wenzelm@69593
   128
  satisfies the invariant \<open>is_rbt t\<close>.  The abstract type \<^typ>\<open>('k, 'v) rbt\<close> always obeys this invariant, and for this reason you
wenzelm@69593
   129
  should only use this in our application.  Going back to \<^typ>\<open>('k,
wenzelm@69593
   130
  'v) RBT_Impl.rbt\<close> may be necessary in proofs if not yet proven
bulwahn@43124
   131
  properties about the operations must be established.
bulwahn@43124
   132
wenzelm@69593
   133
  The interpretation function \<^const>\<open>RBT.lookup\<close> returns the partial
bulwahn@43124
   134
  map represented by a red-black tree:
bulwahn@43124
   135
  @{term_type[display] "RBT.lookup"}
bulwahn@43124
   136
bulwahn@43124
   137
  This function should be used for reasoning about the semantics of the RBT
bulwahn@43124
   138
  operations. Furthermore, it implements the lookup functionality for
bulwahn@43124
   139
  the data structure: It is executable and the lookup is performed in
bulwahn@43124
   140
  $O(\log n)$.  
wenzelm@60500
   141
\<close>
bulwahn@43124
   142
wenzelm@60500
   143
subsection \<open>Operations\<close>
bulwahn@43124
   144
wenzelm@60500
   145
text \<open>
bulwahn@43124
   146
  Currently, the following operations are supported:
bulwahn@43124
   147
bulwahn@43124
   148
  @{term_type [display] "RBT.empty"}
bulwahn@43124
   149
  Returns the empty tree. $O(1)$
bulwahn@43124
   150
bulwahn@43124
   151
  @{term_type [display] "RBT.insert"}
bulwahn@43124
   152
  Updates the map at a given position. $O(\log n)$
bulwahn@43124
   153
bulwahn@43124
   154
  @{term_type [display] "RBT.delete"}
bulwahn@43124
   155
  Deletes a map entry at a given position. $O(\log n)$
bulwahn@43124
   156
bulwahn@43124
   157
  @{term_type [display] "RBT.entries"}
bulwahn@43124
   158
  Return a corresponding key-value list for a tree.
bulwahn@43124
   159
bulwahn@43124
   160
  @{term_type [display] "RBT.bulkload"}
bulwahn@43124
   161
  Builds a tree from a key-value list.
bulwahn@43124
   162
bulwahn@43124
   163
  @{term_type [display] "RBT.map_entry"}
bulwahn@43124
   164
  Maps a single entry in a tree.
bulwahn@43124
   165
bulwahn@43124
   166
  @{term_type [display] "RBT.map"}
bulwahn@43124
   167
  Maps all values in a tree. $O(n)$
bulwahn@43124
   168
bulwahn@43124
   169
  @{term_type [display] "RBT.fold"}
bulwahn@43124
   170
  Folds over all entries in a tree. $O(n)$
wenzelm@60500
   171
\<close>
bulwahn@43124
   172
bulwahn@43124
   173
wenzelm@60500
   174
subsection \<open>Invariant preservation\<close>
bulwahn@43124
   175
wenzelm@60500
   176
text \<open>
bulwahn@43124
   177
  \noindent
wenzelm@61585
   178
  @{thm Empty_is_rbt}\hfill(\<open>Empty_is_rbt\<close>)
bulwahn@43124
   179
bulwahn@43124
   180
  \noindent
wenzelm@61585
   181
  @{thm rbt_insert_is_rbt}\hfill(\<open>rbt_insert_is_rbt\<close>)
bulwahn@43124
   182
bulwahn@43124
   183
  \noindent
wenzelm@61585
   184
  @{thm rbt_delete_is_rbt}\hfill(\<open>delete_is_rbt\<close>)
bulwahn@43124
   185
bulwahn@43124
   186
  \noindent
wenzelm@61585
   187
  @{thm rbt_bulkload_is_rbt}\hfill(\<open>bulkload_is_rbt\<close>)
bulwahn@43124
   188
bulwahn@43124
   189
  \noindent
wenzelm@61585
   190
  @{thm rbt_map_entry_is_rbt}\hfill(\<open>map_entry_is_rbt\<close>)
bulwahn@43124
   191
bulwahn@43124
   192
  \noindent
wenzelm@61585
   193
  @{thm map_is_rbt}\hfill(\<open>map_is_rbt\<close>)
bulwahn@43124
   194
bulwahn@43124
   195
  \noindent
wenzelm@61585
   196
  @{thm rbt_union_is_rbt}\hfill(\<open>union_is_rbt\<close>)
wenzelm@60500
   197
\<close>
bulwahn@43124
   198
bulwahn@43124
   199
wenzelm@60500
   200
subsection \<open>Map Semantics\<close>
bulwahn@43124
   201
wenzelm@60500
   202
text \<open>
bulwahn@43124
   203
  \noindent
wenzelm@61585
   204
  \underline{\<open>lookup_empty\<close>}
bulwahn@43124
   205
  @{thm [display] lookup_empty}
bulwahn@43124
   206
  \vspace{1ex}
bulwahn@43124
   207
bulwahn@43124
   208
  \noindent
wenzelm@61585
   209
  \underline{\<open>lookup_insert\<close>}
bulwahn@43124
   210
  @{thm [display] lookup_insert}
bulwahn@43124
   211
  \vspace{1ex}
bulwahn@43124
   212
bulwahn@43124
   213
  \noindent
wenzelm@61585
   214
  \underline{\<open>lookup_delete\<close>}
bulwahn@43124
   215
  @{thm [display] lookup_delete}
bulwahn@43124
   216
  \vspace{1ex}
bulwahn@43124
   217
bulwahn@43124
   218
  \noindent
wenzelm@61585
   219
  \underline{\<open>lookup_bulkload\<close>}
bulwahn@43124
   220
  @{thm [display] lookup_bulkload}
bulwahn@43124
   221
  \vspace{1ex}
bulwahn@43124
   222
bulwahn@43124
   223
  \noindent
wenzelm@61585
   224
  \underline{\<open>lookup_map\<close>}
bulwahn@43124
   225
  @{thm [display] lookup_map}
bulwahn@43124
   226
  \vspace{1ex}
wenzelm@60500
   227
\<close>
bulwahn@43124
   228
nipkow@62390
   229
end