src/HOL/Library/RBT_Mapping.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 63649 e690d6f2185b
child 68484 59793df7f853
permissions -rw-r--r--
executable domain membership checks
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
bulwahn@43124
   116
  in @{theory RBT_Impl}.
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>
bulwahn@43124
   122
  The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with
bulwahn@43124
   123
  keys of type @{typ "'k"} and values of type @{typ "'v"}. To function
wenzelm@61585
   124
  properly, the key type musorted belong to the \<open>linorder\<close>
bulwahn@43124
   125
  class.
bulwahn@43124
   126
bulwahn@43124
   127
  A value @{term t} of this type is a valid red-black tree if it
wenzelm@61585
   128
  satisfies the invariant \<open>is_rbt t\<close>.  The abstract type @{typ
bulwahn@43124
   129
  "('k, 'v) rbt"} always obeys this invariant, and for this reason you
bulwahn@43124
   130
  should only use this in our application.  Going back to @{typ "('k,
bulwahn@43124
   131
  'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven
bulwahn@43124
   132
  properties about the operations must be established.
bulwahn@43124
   133
bulwahn@43124
   134
  The interpretation function @{const "RBT.lookup"} returns the partial
bulwahn@43124
   135
  map represented by a red-black tree:
bulwahn@43124
   136
  @{term_type[display] "RBT.lookup"}
bulwahn@43124
   137
bulwahn@43124
   138
  This function should be used for reasoning about the semantics of the RBT
bulwahn@43124
   139
  operations. Furthermore, it implements the lookup functionality for
bulwahn@43124
   140
  the data structure: It is executable and the lookup is performed in
bulwahn@43124
   141
  $O(\log n)$.  
wenzelm@60500
   142
\<close>
bulwahn@43124
   143
wenzelm@60500
   144
subsection \<open>Operations\<close>
bulwahn@43124
   145
wenzelm@60500
   146
text \<open>
bulwahn@43124
   147
  Currently, the following operations are supported:
bulwahn@43124
   148
bulwahn@43124
   149
  @{term_type [display] "RBT.empty"}
bulwahn@43124
   150
  Returns the empty tree. $O(1)$
bulwahn@43124
   151
bulwahn@43124
   152
  @{term_type [display] "RBT.insert"}
bulwahn@43124
   153
  Updates the map at a given position. $O(\log n)$
bulwahn@43124
   154
bulwahn@43124
   155
  @{term_type [display] "RBT.delete"}
bulwahn@43124
   156
  Deletes a map entry at a given position. $O(\log n)$
bulwahn@43124
   157
bulwahn@43124
   158
  @{term_type [display] "RBT.entries"}
bulwahn@43124
   159
  Return a corresponding key-value list for a tree.
bulwahn@43124
   160
bulwahn@43124
   161
  @{term_type [display] "RBT.bulkload"}
bulwahn@43124
   162
  Builds a tree from a key-value list.
bulwahn@43124
   163
bulwahn@43124
   164
  @{term_type [display] "RBT.map_entry"}
bulwahn@43124
   165
  Maps a single entry in a tree.
bulwahn@43124
   166
bulwahn@43124
   167
  @{term_type [display] "RBT.map"}
bulwahn@43124
   168
  Maps all values in a tree. $O(n)$
bulwahn@43124
   169
bulwahn@43124
   170
  @{term_type [display] "RBT.fold"}
bulwahn@43124
   171
  Folds over all entries in a tree. $O(n)$
wenzelm@60500
   172
\<close>
bulwahn@43124
   173
bulwahn@43124
   174
wenzelm@60500
   175
subsection \<open>Invariant preservation\<close>
bulwahn@43124
   176
wenzelm@60500
   177
text \<open>
bulwahn@43124
   178
  \noindent
wenzelm@61585
   179
  @{thm Empty_is_rbt}\hfill(\<open>Empty_is_rbt\<close>)
bulwahn@43124
   180
bulwahn@43124
   181
  \noindent
wenzelm@61585
   182
  @{thm rbt_insert_is_rbt}\hfill(\<open>rbt_insert_is_rbt\<close>)
bulwahn@43124
   183
bulwahn@43124
   184
  \noindent
wenzelm@61585
   185
  @{thm rbt_delete_is_rbt}\hfill(\<open>delete_is_rbt\<close>)
bulwahn@43124
   186
bulwahn@43124
   187
  \noindent
wenzelm@61585
   188
  @{thm rbt_bulkload_is_rbt}\hfill(\<open>bulkload_is_rbt\<close>)
bulwahn@43124
   189
bulwahn@43124
   190
  \noindent
wenzelm@61585
   191
  @{thm rbt_map_entry_is_rbt}\hfill(\<open>map_entry_is_rbt\<close>)
bulwahn@43124
   192
bulwahn@43124
   193
  \noindent
wenzelm@61585
   194
  @{thm map_is_rbt}\hfill(\<open>map_is_rbt\<close>)
bulwahn@43124
   195
bulwahn@43124
   196
  \noindent
wenzelm@61585
   197
  @{thm rbt_union_is_rbt}\hfill(\<open>union_is_rbt\<close>)
wenzelm@60500
   198
\<close>
bulwahn@43124
   199
bulwahn@43124
   200
wenzelm@60500
   201
subsection \<open>Map Semantics\<close>
bulwahn@43124
   202
wenzelm@60500
   203
text \<open>
bulwahn@43124
   204
  \noindent
wenzelm@61585
   205
  \underline{\<open>lookup_empty\<close>}
bulwahn@43124
   206
  @{thm [display] lookup_empty}
bulwahn@43124
   207
  \vspace{1ex}
bulwahn@43124
   208
bulwahn@43124
   209
  \noindent
wenzelm@61585
   210
  \underline{\<open>lookup_insert\<close>}
bulwahn@43124
   211
  @{thm [display] lookup_insert}
bulwahn@43124
   212
  \vspace{1ex}
bulwahn@43124
   213
bulwahn@43124
   214
  \noindent
wenzelm@61585
   215
  \underline{\<open>lookup_delete\<close>}
bulwahn@43124
   216
  @{thm [display] lookup_delete}
bulwahn@43124
   217
  \vspace{1ex}
bulwahn@43124
   218
bulwahn@43124
   219
  \noindent
wenzelm@61585
   220
  \underline{\<open>lookup_bulkload\<close>}
bulwahn@43124
   221
  @{thm [display] lookup_bulkload}
bulwahn@43124
   222
  \vspace{1ex}
bulwahn@43124
   223
bulwahn@43124
   224
  \noindent
wenzelm@61585
   225
  \underline{\<open>lookup_map\<close>}
bulwahn@43124
   226
  @{thm [display] lookup_map}
bulwahn@43124
   227
  \vspace{1ex}
wenzelm@60500
   228
\<close>
bulwahn@43124
   229
nipkow@62390
   230
end