src/HOL/Library/Mapping.thy
author haftmann
Wed, 17 Feb 2010 16:49:37 +0100
changeset 35194 a6c573d13385
parent 35157 73cd6f78c86d
child 36110 4ab91a42666a
permissions -rw-r--r--
added ordered_keys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     2
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     3
header {* An abstract view on maps for code generation. *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     4
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     5
theory Mapping
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
     6
imports Main
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     7
begin
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     8
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     9
subsection {* Type definition and primitive operations *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    10
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    11
datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    12
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    13
definition empty :: "('a, 'b) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    14
  "empty = Mapping (\<lambda>_. None)"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    15
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    16
primrec lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<rightharpoonup> 'b" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    17
  "lookup (Mapping f) = f"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    18
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    19
primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    20
  "update k v (Mapping f) = Mapping (f (k \<mapsto> v))"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    21
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    22
primrec delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    23
  "delete k (Mapping f) = Mapping (f (k := None))"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    24
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    25
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    26
subsection {* Derived operations *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    27
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    28
definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    29
  "keys m = dom (lookup m)"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    30
35194
a6c573d13385 added ordered_keys
haftmann
parents: 35157
diff changeset
    31
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
a6c573d13385 added ordered_keys
haftmann
parents: 35157
diff changeset
    32
  "ordered_keys m = sorted_list_of_set (keys m)"
a6c573d13385 added ordered_keys
haftmann
parents: 35157
diff changeset
    33
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    34
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    35
  "is_empty m \<longleftrightarrow> dom (lookup m) = {}"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    36
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    37
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    38
  "size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    39
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    40
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
29814
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    41
  "replace k v m = (if lookup m k = None then m else update k v m)"
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    42
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    43
definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    44
  "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    45
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    46
definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    47
  "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    48
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    49
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    50
subsection {* Properties *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    51
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    52
lemma lookup_inject [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    53
  "lookup m = lookup n \<longleftrightarrow> m = n"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    54
  by (cases m, cases n) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    55
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    56
lemma mapping_eqI:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    57
  assumes "lookup m = lookup n"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    58
  shows "m = n"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    59
  using assms by simp
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    60
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    61
lemma lookup_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    62
  "lookup empty = Map.empty"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    63
  by (simp add: empty_def)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    64
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    65
lemma lookup_update [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    66
  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    67
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    68
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    69
lemma lookup_delete [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    70
  "lookup (delete k m) = (lookup m) (k := None)"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    71
  by (cases m) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    72
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    73
lemma lookup_tabulate [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    74
  "lookup (tabulate ks f) = (Some o f) |` set ks"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    75
  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    76
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    77
lemma lookup_bulkload [simp]:
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    78
  "lookup (bulkload xs) = (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    79
  by (simp add: bulkload_def)
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    80
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    81
lemma update_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    82
  "update k v (update k w m) = update k v m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    83
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    84
  by (rule mapping_eqI, simp add: fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    85
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    86
lemma update_delete [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    87
  "update k v (delete k m) = update k v m"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    88
  by (rule mapping_eqI) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    89
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    90
lemma delete_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    91
  "delete k (update k v m) = delete k m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    92
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    93
  by (rule mapping_eqI, simp add: fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    94
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    95
lemma delete_empty [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    96
  "delete k empty = empty"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    97
  by (rule mapping_eqI) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    98
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    99
lemma replace_update:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   100
  "k \<notin> dom (lookup m) \<Longrightarrow> replace k v m = m"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   101
  "k \<in> dom (lookup m) \<Longrightarrow> replace k v m = update k v m"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   102
  by (rule mapping_eqI, auto simp add: replace_def fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   103
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   104
lemma size_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   105
  "size empty = 0"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   106
  by (simp add: size_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   107
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   108
lemma size_update:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   109
  "finite (dom (lookup m)) \<Longrightarrow> size (update k v m) =
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   110
    (if k \<in> dom (lookup m) then size m else Suc (size m))"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   111
  by (auto simp add: size_def insert_dom)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   112
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   113
lemma size_delete:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   114
  "size (delete k m) = (if k \<in> dom (lookup m) then size m - 1 else size m)"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   115
  by (simp add: size_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   116
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   117
lemma size_tabulate:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   118
  "size (tabulate ks f) = length (remdups ks)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   119
  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   120
29831
5dc920623bb1 Isar proof
haftmann
parents: 29828
diff changeset
   121
lemma bulkload_tabulate:
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   122
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   123
  by (rule mapping_eqI) (simp add: expand_fun_eq)
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   124
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   125
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   126
subsection {* Some technical code lemmas *}
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   127
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   128
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   129
  "mapping_case f m = f (Mapping.lookup m)"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   130
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   131
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   132
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   133
  "mapping_rec f m = f (Mapping.lookup m)"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   134
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   135
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   136
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   137
  "Nat.size (m :: (_, _) mapping) = 0"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   138
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   139
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   140
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   141
  "mapping_size f g m = 0"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   142
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   143
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   144
35194
a6c573d13385 added ordered_keys
haftmann
parents: 35157
diff changeset
   145
hide (open) const empty is_empty lookup update delete ordered_keys keys size replace tabulate bulkload
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   146
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   147
end