src/HOL/Library/Mapping.thy
author haftmann
Fri, 06 Feb 2009 09:05:19 +0100
changeset 29814 15344c0899e1
parent 29708 e40b70d38909
child 29826 5132da6ebca3
permissions -rw-r--r--
added replace operation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Mapping.thy
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     3
*)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     4
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     5
header {* An abstract view on maps for code generation. *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     6
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     7
theory Mapping
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     8
imports Map
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
     9
begin
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    10
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    11
subsection {* Type definition and primitive operations *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    12
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    13
datatype ('a, 'b) map = Map "'a \<rightharpoonup> 'b"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    14
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    15
definition empty :: "('a, 'b) map" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    16
  "empty = Map (\<lambda>_. None)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    17
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    18
primrec lookup :: "('a, 'b) map \<Rightarrow> 'a \<rightharpoonup> 'b" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    19
  "lookup (Map f) = f"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    20
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    21
primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    22
  "update k v (Map f) = Map (f (k \<mapsto> v))"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    23
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    24
primrec delete :: "'a \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    25
  "delete k (Map f) = Map (f (k := None))"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    26
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    27
primrec keys :: "('a, 'b) map \<Rightarrow> 'a set" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    28
  "keys (Map f) = dom f"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    29
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    30
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    31
subsection {* Derived operations *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    32
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    33
definition size :: "('a, 'b) map \<Rightarrow> nat" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    34
  "size m = (if finite (keys m) then card (keys m) else 0)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    35
29814
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    36
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    37
  "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
    38
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    39
definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) map" where
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    40
  "tabulate ks f = Map (map_of (map (\<lambda>k. (k, f k)) ks))"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    41
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    42
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    43
subsection {* Properties *}
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    44
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    45
lemma lookup_inject:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    46
  "lookup m = lookup n \<longleftrightarrow> m = n"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    47
  by (cases m, cases n) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    48
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    49
lemma lookup_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    50
  "lookup empty = Map.empty"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    51
  by (simp add: empty_def)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    52
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    53
lemma lookup_update [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    54
  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    55
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    56
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    57
lemma lookup_delete:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    58
  "lookup (delete k m) k = None"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    59
  "k \<noteq> l \<Longrightarrow> lookup (delete k m) l = lookup m l"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    60
  by (cases m, simp)+
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    61
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    62
lemma lookup_tabulate:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    63
  "lookup (tabulate ks f) = (Some o f) |` set ks"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    64
  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    65
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    66
lemma update_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    67
  "update k v (update k w m) = update k v m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    68
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    69
  by (cases m, simp add: expand_fun_eq)+
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    70
29814
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    71
lemma replace_update:
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    72
  "lookup m k = None \<Longrightarrow> replace k v m = m"
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    73
  "lookup m k \<noteq> None \<Longrightarrow> replace k v m = update k v m"
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    74
  by (auto simp add: replace_def)
15344c0899e1 added replace operation
haftmann
parents: 29708
diff changeset
    75
29708
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    76
lemma delete_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    77
  "delete k empty = empty"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    78
  by (simp add: empty_def)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    79
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    80
lemma delete_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    81
  "delete k (update k v m) = delete k m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    82
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    83
  by (cases m, simp add: expand_fun_eq)+
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    84
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    85
lemma update_delete [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    86
  "update k v (delete k m) = update k v m"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    87
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    88
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    89
lemma keys_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    90
  "keys empty = {}"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    91
  unfolding empty_def by simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    92
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    93
lemma keys_update [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    94
  "keys (update k v m) = insert k (keys m)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    95
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    96
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    97
lemma keys_delete [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    98
  "keys (delete k m) = keys m - {k}"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
    99
  by (cases m) simp
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   100
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   101
lemma keys_tabulate [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   102
  "keys (tabulate ks f) = set ks"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   103
  by (auto simp add: tabulate_def dest: map_of_SomeD intro!: weak_map_of_SomeI)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   104
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   105
lemma size_empty [simp]:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   106
  "size empty = 0"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   107
  by (simp add: size_def keys_empty)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   108
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   109
lemma size_update:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   110
  "finite (keys m) \<Longrightarrow> size (update k v m) =
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   111
    (if k \<in> keys m then size m else Suc (size m))"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   112
  by (simp add: size_def keys_update)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   113
    (auto simp only: card_insert card_Suc_Diff1)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   114
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   115
lemma size_delete:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   116
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   117
  by (simp add: size_def keys_delete)
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   118
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   119
lemma size_tabulate:
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   120
  "size (tabulate ks f) = length (remdups ks)"
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   121
  by (simp add: size_def keys_tabulate distinct_card [of "remdups ks", symmetric])
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   122
e40b70d38909 added Mapping.thy to Library
haftmann
parents:
diff changeset
   123
end