src/HOL/Library/Mapping.thy
author haftmann
Fri, 21 May 2010 15:22:37 +0200
changeset 37052 80dd92673fca
parent 37026 7e8979a155ae
child 37107 1535aa1c943a
permissions -rw-r--r--
more lemmas about mappings, in particular 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
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
     9
lemma remove1_idem: (*FIXME move to List.thy*)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    10
  assumes "x \<notin> set xs"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    11
  shows "remove1 x xs = xs"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    12
  using assms by (induct xs) simp_all
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    13
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    14
lemma remove1_insort [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    15
  "remove1 x (insort x xs) = xs"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    16
  by (induct xs) simp_all
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    17
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    18
lemma sorted_list_of_set_remove:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    19
  assumes "finite A"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    20
  shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    21
proof (cases "x \<in> A")
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    22
  case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    23
  with False show ?thesis by (simp add: remove1_idem)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    24
next
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    25
  case True then obtain B where A: "A = insert x B" by (rule Set.set_insert)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    26
  with assms show ?thesis by simp
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    27
qed
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    28
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    29
lemma sorted_list_of_set_range [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    30
  "sorted_list_of_set {m..<n} = [m..<n]"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    31
  by (rule sorted_distinct_set_unique) simp_all
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    32
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    33
subsection {* Type definition and primitive operations *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    34
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    35
datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    36
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    37
definition empty :: "('a, 'b) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    38
  "empty = Mapping (\<lambda>_. None)"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    39
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    40
primrec lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<rightharpoonup> 'b" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    41
  "lookup (Mapping f) = f"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    42
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    43
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
    44
  "update k v (Mapping f) = Mapping (f (k \<mapsto> v))"
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
primrec delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    47
  "delete k (Mapping f) = Mapping (f (k := None))"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    48
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    49
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    50
subsection {* Derived operations *}
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
definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    53
  "keys m = dom (lookup m)"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    54
35194
a6c573d13385 added ordered_keys
haftmann
parents: 35157
diff changeset
    55
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    56
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
35194
a6c573d13385 added ordered_keys
haftmann
parents: 35157
diff changeset
    57
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    58
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    59
  "is_empty m \<longleftrightarrow> keys m = {}"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    60
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    61
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    62
  "size m = (if finite (keys m) then card (keys m) else 0)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    63
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    64
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    65
  "replace k v m = (if k \<in> keys m then update k v m else m)"
29814
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    66
37026
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    67
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    68
  "default k v m = (if k \<in> keys m then m else update k v m)"
37026
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    69
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    70
definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    71
  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    72
    | Some v \<Rightarrow> update k (f v) m)" 
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    73
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    74
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    75
  "map_default k v f m = map_entry k f (default k v m)" 
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
    76
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    77
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
    78
  "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))"
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    79
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    80
definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    81
  "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    82
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    83
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    84
subsection {* Properties *}
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 lookup_inject [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    87
  "lookup m = lookup n \<longleftrightarrow> m = n"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    88
  by (cases m, cases n) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    89
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    90
lemma mapping_eqI:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    91
  assumes "lookup m = lookup n"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    92
  shows "m = n"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    93
  using assms by simp
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    94
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    95
lemma keys_is_none_lookup [code_inline]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    96
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    97
  by (auto simp add: keys_def is_none_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
    98
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    99
lemma lookup_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   100
  "lookup empty = Map.empty"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   101
  by (simp add: empty_def)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   102
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   103
lemma lookup_update [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   104
  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   105
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   106
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   107
lemma lookup_delete [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   108
  "lookup (delete k m) = (lookup m) (k := None)"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   109
  by (cases m) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   110
37026
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   111
lemma lookup_map_entry [simp]:
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   112
  "lookup (map_entry k f m) = (lookup m) (k := Option.map f (lookup m k))"
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   113
  by (cases "lookup m k") (simp_all add: map_entry_def expand_fun_eq)
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   114
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   115
lemma lookup_tabulate [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   116
  "lookup (tabulate ks f) = (Some o f) |` set ks"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   117
  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   118
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   119
lemma lookup_bulkload [simp]:
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   120
  "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
   121
  by (simp add: bulkload_def)
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   122
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   123
lemma update_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   124
  "update k v (update k w m) = update k v m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   125
  "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
   126
  by (rule mapping_eqI, simp add: fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   127
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   128
lemma update_delete [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   129
  "update k v (delete k m) = update k v m"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   130
  by (rule mapping_eqI) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   131
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   132
lemma delete_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   133
  "delete k (update k v m) = delete k m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   134
  "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
   135
  by (rule mapping_eqI, simp add: fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   136
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   137
lemma delete_empty [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   138
  "delete k empty = empty"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   139
  by (rule mapping_eqI) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   140
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   141
lemma replace_update:
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   142
  "k \<notin> keys m \<Longrightarrow> replace k v m = m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   143
  "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   144
  by (rule mapping_eqI) (auto simp add: replace_def fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   145
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   146
lemma size_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   147
  "size empty = 0"
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   148
  by (simp add: size_def keys_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   149
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   150
lemma size_update:
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   151
  "finite (keys m) \<Longrightarrow> size (update k v m) =
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   152
    (if k \<in> keys m then size m else Suc (size m))"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   153
  by (auto simp add: size_def insert_dom keys_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   154
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   155
lemma size_delete:
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   156
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   157
  by (simp add: size_def keys_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   158
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   159
lemma size_tabulate [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   160
  "size (tabulate ks f) = length (remdups ks)"
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   161
  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def keys_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   162
29831
5dc920623bb1 Isar proof
haftmann
parents: 29828
diff changeset
   163
lemma bulkload_tabulate:
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   164
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   165
  by (rule mapping_eqI) (simp add: expand_fun_eq)
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   166
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   167
lemma is_empty_empty: (*FIXME*)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   168
  "is_empty m \<longleftrightarrow> m = Mapping Map.empty"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   169
  by (cases m) (simp add: is_empty_def keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   170
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   171
lemma is_empty_empty' [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   172
  "is_empty empty"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   173
  by (simp add: is_empty_empty empty_def) 
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   174
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   175
lemma is_empty_update [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   176
  "\<not> is_empty (update k v m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   177
  by (cases m) (simp add: is_empty_empty)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   178
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   179
lemma is_empty_delete:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   180
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   181
  by (cases m) (auto simp add: is_empty_empty keys_def dom_eq_empty_conv [symmetric] simp del: dom_eq_empty_conv)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   182
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   183
lemma is_empty_replace [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   184
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   185
  by (auto simp add: replace_def) (simp add: is_empty_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   186
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   187
lemma is_empty_default [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   188
  "\<not> is_empty (default k v m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   189
  by (auto simp add: default_def) (simp add: is_empty_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   190
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   191
lemma is_empty_map_entry [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   192
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   193
  by (cases "lookup m k")
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   194
    (auto simp add: map_entry_def, simp add: is_empty_empty)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   195
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   196
lemma is_empty_map_default [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   197
  "\<not> is_empty (map_default k v f m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   198
  by (simp add: map_default_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   199
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   200
lemma keys_empty [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   201
  "keys empty = {}"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   202
  by (simp add: keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   203
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   204
lemma keys_update [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   205
  "keys (update k v m) = insert k (keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   206
  by (simp add: keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   207
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   208
lemma keys_delete [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   209
  "keys (delete k m) = keys m - {k}"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   210
  by (simp add: keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   211
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   212
lemma keys_replace [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   213
  "keys (replace k v m) = keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   214
  by (auto simp add: keys_def replace_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   215
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   216
lemma keys_default [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   217
  "keys (default k v m) = insert k (keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   218
  by (auto simp add: keys_def default_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   219
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   220
lemma keys_map_entry [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   221
  "keys (map_entry k f m) = keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   222
  by (auto simp add: keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   223
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   224
lemma keys_map_default [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   225
  "keys (map_default k v f m) = insert k (keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   226
  by (simp add: map_default_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   227
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   228
lemma keys_tabulate [simp]:
37026
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   229
  "keys (tabulate ks f) = set ks"
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   230
  by (simp add: tabulate_def keys_def map_of_map_restrict o_def)
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   231
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   232
lemma keys_bulkload [simp]:
37026
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   233
  "keys (bulkload xs) = {0..<length xs}"
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   234
  by (simp add: keys_tabulate bulkload_tabulate)
7e8979a155ae operations default, map_entry, map_default; more lemmas
haftmann
parents: 36176
diff changeset
   235
37052
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   236
lemma distinct_ordered_keys [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   237
  "distinct (ordered_keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   238
  by (simp add: ordered_keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   239
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   240
lemma ordered_keys_infinite [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   241
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   242
  by (simp add: ordered_keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   243
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   244
lemma ordered_keys_empty [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   245
  "ordered_keys empty = []"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   246
  by (simp add: ordered_keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   247
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   248
lemma ordered_keys_update [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   249
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   250
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   251
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   252
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   253
lemma ordered_keys_delete [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   254
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   255
proof (cases "finite (keys m)")
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   256
  case False then show ?thesis by simp
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   257
next
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   258
  case True note fin = True
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   259
  show ?thesis
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   260
  proof (cases "k \<in> keys m")
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   261
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   262
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   263
  next
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   264
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   265
  qed
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   266
qed
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   267
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   268
lemma ordered_keys_replace [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   269
  "ordered_keys (replace k v m) = ordered_keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   270
  by (simp add: replace_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   271
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   272
lemma ordered_keys_default [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   273
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   274
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   275
  by (simp_all add: default_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   276
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   277
lemma ordered_keys_map_entry [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   278
  "ordered_keys (map_entry k f m) = ordered_keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   279
  by (simp add: ordered_keys_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   280
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   281
lemma ordered_keys_map_default [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   282
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   283
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   284
  by (simp_all add: map_default_def)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   285
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   286
lemma ordered_keys_tabulate [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   287
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   288
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   289
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   290
lemma ordered_keys_bulkload [simp]:
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   291
  "ordered_keys (bulkload ks) = [0..<length ks]"
80dd92673fca more lemmas about mappings, in particular keys
haftmann
parents: 37026
diff changeset
   292
  by (simp add: ordered_keys_def)
36110
4ab91a42666a lemma is_empty_empty
haftmann
parents: 35194
diff changeset
   293
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   294
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   295
subsection {* Some technical code lemmas *}
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   296
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   297
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   298
  "mapping_case f m = f (Mapping.lookup m)"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   299
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   300
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   301
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   302
  "mapping_rec f m = f (Mapping.lookup m)"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   303
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   304
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   305
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   306
  "Nat.size (m :: (_, _) mapping) = 0"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   307
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   308
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   309
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   310
  "mapping_size f g m = 0"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   311
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   312
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   313
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36110
diff changeset
   314
hide_const (open) 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
   315
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   316
end