src/HOL/Library/RBT_Mapping.thy
author wenzelm
Sun Nov 02 17:20:45 2014 +0100 (2014-11-02)
changeset 58881 b9556a055632
parent 56019 682bba24e474
child 60373 68eb60fd22a6
permissions -rw-r--r--
modernized header;
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@58881
     5
section {* Implementation of mappings with Red-Black Trees *}
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
bulwahn@43124
    12
subsection {* Implementation of mappings *}
bulwahn@43124
    13
kuncar@56019
    14
context includes rbt.lifting begin
kuncar@56019
    15
lift_definition Mapping :: "('a\<Colon>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)"
kuncar@56019
    46
  apply (transfer fixing: t) by (case_tac "RBT.lookup t k") auto
bulwahn@43124
    47
bulwahn@43124
    48
lemma keys_Mapping [code]:
kuncar@56019
    49
  "Mapping.keys (Mapping t) = set (RBT.keys t)"
kuncar@49929
    50
by (transfer fixing: t) (simp add: lookup_keys)
bulwahn@43124
    51
bulwahn@43124
    52
lemma ordered_keys_Mapping [code]:
kuncar@56019
    53
  "Mapping.ordered_keys (Mapping t) = RBT.keys t"
kuncar@49929
    54
unfolding ordered_keys_def 
kuncar@49929
    55
by (transfer fixing: t) (auto simp add: lookup_keys intro: sorted_distinct_set_unique)
bulwahn@43124
    56
bulwahn@43124
    57
lemma Mapping_size_card_keys: (*FIXME*)
bulwahn@43124
    58
  "Mapping.size m = card (Mapping.keys m)"
kuncar@49929
    59
unfolding size_def by transfer simp
bulwahn@43124
    60
bulwahn@43124
    61
lemma size_Mapping [code]:
kuncar@56019
    62
  "Mapping.size (Mapping t) = length (RBT.keys t)"
kuncar@49929
    63
unfolding size_def
kuncar@49929
    64
by (transfer fixing: t) (simp add: lookup_keys distinct_card)
bulwahn@43124
    65
kuncar@49929
    66
context
kuncar@49929
    67
  notes RBT.bulkload.transfer[transfer_rule del]
kuncar@49929
    68
begin
kuncar@49929
    69
  lemma tabulate_Mapping [code]:
kuncar@56019
    70
    "Mapping.tabulate ks f = Mapping (RBT.bulkload (List.map (\<lambda>k. (k, f k)) ks))"
kuncar@49929
    71
  by transfer (simp add: map_of_map_restrict)
kuncar@49929
    72
  
kuncar@49929
    73
  lemma bulkload_Mapping [code]:
kuncar@56019
    74
    "Mapping.bulkload vs = Mapping (RBT.bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
kuncar@49929
    75
  by transfer (simp add: map_of_map_restrict fun_eq_iff)
kuncar@49929
    76
end
bulwahn@43124
    77
bulwahn@43124
    78
lemma equal_Mapping [code]:
kuncar@56019
    79
  "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> RBT.entries t1 = RBT.entries t2"
haftmann@51161
    80
  by (transfer fixing: t1 t2) (simp add: entries_lookup)
bulwahn@43124
    81
bulwahn@43124
    82
lemma [code nbe]:
bulwahn@43124
    83
  "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
bulwahn@43124
    84
  by (fact equal_refl)
bulwahn@43124
    85
kuncar@56019
    86
end
bulwahn@43124
    87
bulwahn@43124
    88
(*>*)
bulwahn@43124
    89
bulwahn@43124
    90
text {* 
bulwahn@43124
    91
  This theory defines abstract red-black trees as an efficient
bulwahn@43124
    92
  representation of finite maps, backed by the implementation
bulwahn@43124
    93
  in @{theory RBT_Impl}.
bulwahn@43124
    94
*}
bulwahn@43124
    95
bulwahn@43124
    96
subsection {* Data type and invariant *}
bulwahn@43124
    97
bulwahn@43124
    98
text {*
bulwahn@43124
    99
  The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with
bulwahn@43124
   100
  keys of type @{typ "'k"} and values of type @{typ "'v"}. To function
bulwahn@43124
   101
  properly, the key type musorted belong to the @{text "linorder"}
bulwahn@43124
   102
  class.
bulwahn@43124
   103
bulwahn@43124
   104
  A value @{term t} of this type is a valid red-black tree if it
bulwahn@43124
   105
  satisfies the invariant @{text "is_rbt t"}.  The abstract type @{typ
bulwahn@43124
   106
  "('k, 'v) rbt"} always obeys this invariant, and for this reason you
bulwahn@43124
   107
  should only use this in our application.  Going back to @{typ "('k,
bulwahn@43124
   108
  'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven
bulwahn@43124
   109
  properties about the operations must be established.
bulwahn@43124
   110
bulwahn@43124
   111
  The interpretation function @{const "RBT.lookup"} returns the partial
bulwahn@43124
   112
  map represented by a red-black tree:
bulwahn@43124
   113
  @{term_type[display] "RBT.lookup"}
bulwahn@43124
   114
bulwahn@43124
   115
  This function should be used for reasoning about the semantics of the RBT
bulwahn@43124
   116
  operations. Furthermore, it implements the lookup functionality for
bulwahn@43124
   117
  the data structure: It is executable and the lookup is performed in
bulwahn@43124
   118
  $O(\log n)$.  
bulwahn@43124
   119
*}
bulwahn@43124
   120
bulwahn@43124
   121
subsection {* Operations *}
bulwahn@43124
   122
bulwahn@43124
   123
text {*
bulwahn@43124
   124
  Currently, the following operations are supported:
bulwahn@43124
   125
bulwahn@43124
   126
  @{term_type [display] "RBT.empty"}
bulwahn@43124
   127
  Returns the empty tree. $O(1)$
bulwahn@43124
   128
bulwahn@43124
   129
  @{term_type [display] "RBT.insert"}
bulwahn@43124
   130
  Updates the map at a given position. $O(\log n)$
bulwahn@43124
   131
bulwahn@43124
   132
  @{term_type [display] "RBT.delete"}
bulwahn@43124
   133
  Deletes a map entry at a given position. $O(\log n)$
bulwahn@43124
   134
bulwahn@43124
   135
  @{term_type [display] "RBT.entries"}
bulwahn@43124
   136
  Return a corresponding key-value list for a tree.
bulwahn@43124
   137
bulwahn@43124
   138
  @{term_type [display] "RBT.bulkload"}
bulwahn@43124
   139
  Builds a tree from a key-value list.
bulwahn@43124
   140
bulwahn@43124
   141
  @{term_type [display] "RBT.map_entry"}
bulwahn@43124
   142
  Maps a single entry in a tree.
bulwahn@43124
   143
bulwahn@43124
   144
  @{term_type [display] "RBT.map"}
bulwahn@43124
   145
  Maps all values in a tree. $O(n)$
bulwahn@43124
   146
bulwahn@43124
   147
  @{term_type [display] "RBT.fold"}
bulwahn@43124
   148
  Folds over all entries in a tree. $O(n)$
bulwahn@43124
   149
*}
bulwahn@43124
   150
bulwahn@43124
   151
bulwahn@43124
   152
subsection {* Invariant preservation *}
bulwahn@43124
   153
bulwahn@43124
   154
text {*
bulwahn@43124
   155
  \noindent
bulwahn@43124
   156
  @{thm Empty_is_rbt}\hfill(@{text "Empty_is_rbt"})
bulwahn@43124
   157
bulwahn@43124
   158
  \noindent
Andreas@47450
   159
  @{thm rbt_insert_is_rbt}\hfill(@{text "rbt_insert_is_rbt"})
bulwahn@43124
   160
bulwahn@43124
   161
  \noindent
Andreas@47450
   162
  @{thm rbt_delete_is_rbt}\hfill(@{text "delete_is_rbt"})
bulwahn@43124
   163
bulwahn@43124
   164
  \noindent
Andreas@47450
   165
  @{thm rbt_bulkload_is_rbt}\hfill(@{text "bulkload_is_rbt"})
bulwahn@43124
   166
bulwahn@43124
   167
  \noindent
Andreas@47450
   168
  @{thm rbt_map_entry_is_rbt}\hfill(@{text "map_entry_is_rbt"})
bulwahn@43124
   169
bulwahn@43124
   170
  \noindent
bulwahn@43124
   171
  @{thm map_is_rbt}\hfill(@{text "map_is_rbt"})
bulwahn@43124
   172
bulwahn@43124
   173
  \noindent
Andreas@47450
   174
  @{thm rbt_union_is_rbt}\hfill(@{text "union_is_rbt"})
bulwahn@43124
   175
*}
bulwahn@43124
   176
bulwahn@43124
   177
bulwahn@43124
   178
subsection {* Map Semantics *}
bulwahn@43124
   179
bulwahn@43124
   180
text {*
bulwahn@43124
   181
  \noindent
bulwahn@43124
   182
  \underline{@{text "lookup_empty"}}
bulwahn@43124
   183
  @{thm [display] lookup_empty}
bulwahn@43124
   184
  \vspace{1ex}
bulwahn@43124
   185
bulwahn@43124
   186
  \noindent
bulwahn@43124
   187
  \underline{@{text "lookup_insert"}}
bulwahn@43124
   188
  @{thm [display] lookup_insert}
bulwahn@43124
   189
  \vspace{1ex}
bulwahn@43124
   190
bulwahn@43124
   191
  \noindent
bulwahn@43124
   192
  \underline{@{text "lookup_delete"}}
bulwahn@43124
   193
  @{thm [display] lookup_delete}
bulwahn@43124
   194
  \vspace{1ex}
bulwahn@43124
   195
bulwahn@43124
   196
  \noindent
bulwahn@43124
   197
  \underline{@{text "lookup_bulkload"}}
bulwahn@43124
   198
  @{thm [display] lookup_bulkload}
bulwahn@43124
   199
  \vspace{1ex}
bulwahn@43124
   200
bulwahn@43124
   201
  \noindent
bulwahn@43124
   202
  \underline{@{text "lookup_map"}}
bulwahn@43124
   203
  @{thm [display] lookup_map}
bulwahn@43124
   204
  \vspace{1ex}
bulwahn@43124
   205
*}
bulwahn@43124
   206
bulwahn@43124
   207
end