src/HOL/Library/Mapping.thy
author haftmann
Wed, 17 Feb 2010 09:48:52 +0100
changeset 35157 73cd6f78c86d
parent 33640 0d82107dc07a
child 35194 a6c573d13385
permissions -rw-r--r--
more close integration with theory Map
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
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    31
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    32
  "is_empty m \<longleftrightarrow> dom (lookup m) = {}"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    33
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    34
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    35
  "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
    36
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    37
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
29814
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    38
  "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
    39
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    40
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
    41
  "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))"
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
definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    44
  "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    45
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    46
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    47
subsection {* Properties *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    48
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    49
lemma lookup_inject [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    50
  "lookup m = lookup n \<longleftrightarrow> m = n"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    51
  by (cases m, cases n) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    52
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    53
lemma mapping_eqI:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    54
  assumes "lookup m = lookup n"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    55
  shows "m = n"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    56
  using assms by simp
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    57
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    58
lemma lookup_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    59
  "lookup empty = Map.empty"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    60
  by (simp add: empty_def)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    61
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    62
lemma lookup_update [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    63
  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    64
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    65
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    66
lemma lookup_delete [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    67
  "lookup (delete k m) = (lookup m) (k := None)"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    68
  by (cases m) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    69
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    70
lemma lookup_tabulate [simp]:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    71
  "lookup (tabulate ks f) = (Some o f) |` set ks"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    72
  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    73
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    74
lemma lookup_bulkload [simp]:
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    75
  "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
    76
  by (simp add: bulkload_def)
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
    77
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    78
lemma update_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    79
  "update k v (update k w m) = update k v m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    80
  "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
    81
  by (rule mapping_eqI, simp add: fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    82
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    83
lemma update_delete [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    84
  "update k v (delete k m) = update k v m"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    85
  by (rule mapping_eqI) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    86
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    87
lemma delete_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    88
  "delete k (update k v m) = delete k m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    89
  "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
    90
  by (rule mapping_eqI, simp add: fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    91
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    92
lemma delete_empty [simp]:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    93
  "delete k empty = empty"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    94
  by (rule mapping_eqI) simp
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    95
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    96
lemma replace_update:
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    97
  "k \<notin> dom (lookup m) \<Longrightarrow> replace k v m = m"
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
    98
  "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
    99
  by (rule mapping_eqI, auto simp add: replace_def fun_upd_twist)+
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   100
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   101
lemma size_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   102
  "size empty = 0"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   103
  by (simp add: size_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   104
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   105
lemma size_update:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   106
  "finite (dom (lookup m)) \<Longrightarrow> size (update k v m) =
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   107
    (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
   108
  by (auto simp add: size_def insert_dom)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   109
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   110
lemma size_delete:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   111
  "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
   112
  by (simp add: size_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   113
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   114
lemma size_tabulate:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   115
  "size (tabulate ks f) = length (remdups ks)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   116
  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def)
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   117
29831
5dc920623bb1 Isar proof
haftmann
parents: 29828
diff changeset
   118
lemma bulkload_tabulate:
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   119
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   120
  by (rule mapping_eqI) (simp add: expand_fun_eq)
29826
5132da6ebca3 added bulkload
haftmann
parents: 29814
diff changeset
   121
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   122
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   123
subsection {* Some technical code lemmas *}
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   124
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   125
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   126
  "mapping_case f m = f (Mapping.lookup m)"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   127
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   128
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   129
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   130
  "mapping_rec f m = f (Mapping.lookup m)"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   131
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   132
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   133
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   134
  "Nat.size (m :: (_, _) mapping) = 0"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   135
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   136
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   137
lemma [code]:
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   138
  "mapping_size f g m = 0"
31459
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   139
  by (cases m) simp
ae39b7b2a68a added trees implementing mappings
haftmann
parents: 30663
diff changeset
   140
35157
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   141
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   142
hide (open) const empty is_empty lookup update delete keys size replace tabulate bulkload
73cd6f78c86d more close integration with theory Map
haftmann
parents: 33640
diff changeset
   143
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   144
end