src/HOL/Map.thy
author wenzelm
Fri Nov 17 02:20:03 2006 +0100 (2006-11-17)
changeset 21404 eb85850d3eb7
parent 21210 c17fd2df4e9e
child 22230 bdec4a82f385
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
nipkow@3981
     1
(*  Title:      HOL/Map.thy
nipkow@3981
     2
    ID:         $Id$
nipkow@3981
     3
    Author:     Tobias Nipkow, based on a theory by David von Oheimb
webertj@13908
     4
    Copyright   1997-2003 TU Muenchen
nipkow@3981
     5
nipkow@3981
     6
The datatype of `maps' (written ~=>); strongly resembles maps in VDM.
nipkow@3981
     7
*)
nipkow@3981
     8
nipkow@13914
     9
header {* Maps *}
nipkow@13914
    10
nipkow@15131
    11
theory Map
nipkow@15140
    12
imports List
nipkow@15131
    13
begin
nipkow@3981
    14
wenzelm@20800
    15
types ('a,'b) "~=>" = "'a => 'b option"  (infixr 0)
oheimb@14100
    16
translations (type) "a ~=> b " <= (type) "a => b option"
nipkow@3981
    17
wenzelm@19656
    18
syntax (xsymbols)
wenzelm@20800
    19
  "~=>" :: "[type, type] => type"  (infixr "\<rightharpoonup>" 0)
wenzelm@19656
    20
nipkow@19378
    21
abbreviation
wenzelm@21404
    22
  empty :: "'a ~=> 'b" where
nipkow@19378
    23
  "empty == %x. None"
nipkow@19378
    24
wenzelm@19656
    25
definition
wenzelm@21404
    26
  map_comp :: "('b ~=> 'c)  => ('a ~=> 'b) => ('a ~=> 'c)"  (infixl "o'_m" 55) where
wenzelm@20800
    27
  "f o_m g = (\<lambda>k. case g k of None \<Rightarrow> None | Some v \<Rightarrow> f v)"
nipkow@19378
    28
wenzelm@21210
    29
notation (xsymbols)
wenzelm@19656
    30
  map_comp  (infixl "\<circ>\<^sub>m" 55)
wenzelm@19656
    31
wenzelm@20800
    32
definition
wenzelm@21404
    33
  map_add :: "('a ~=> 'b) => ('a ~=> 'b) => ('a ~=> 'b)"  (infixl "++" 100) where
wenzelm@20800
    34
  "m1 ++ m2 = (\<lambda>x. case m2 x of None => m1 x | Some y => Some y)"
wenzelm@20800
    35
wenzelm@21404
    36
definition
wenzelm@21404
    37
  restrict_map :: "('a ~=> 'b) => 'a set => ('a ~=> 'b)"  (infixl "|`"  110) where
wenzelm@20800
    38
  "m|`A = (\<lambda>x. if x : A then m x else None)"
nipkow@13910
    39
wenzelm@21210
    40
notation (latex output)
wenzelm@19656
    41
  restrict_map  ("_\<restriction>\<^bsub>_\<^esub>" [111,110] 110)
wenzelm@19656
    42
wenzelm@20800
    43
definition
wenzelm@21404
    44
  dom :: "('a ~=> 'b) => 'a set" where
wenzelm@20800
    45
  "dom m = {a. m a ~= None}"
wenzelm@20800
    46
wenzelm@21404
    47
definition
wenzelm@21404
    48
  ran :: "('a ~=> 'b) => 'b set" where
wenzelm@20800
    49
  "ran m = {b. EX a. m a = Some b}"
wenzelm@20800
    50
wenzelm@21404
    51
definition
wenzelm@21404
    52
  map_le :: "('a ~=> 'b) => ('a ~=> 'b) => bool"  (infix "\<subseteq>\<^sub>m" 50) where
wenzelm@20800
    53
  "(m\<^isub>1 \<subseteq>\<^sub>m m\<^isub>2) = (\<forall>a \<in> dom m\<^isub>1. m\<^isub>1 a = m\<^isub>2 a)"
wenzelm@20800
    54
wenzelm@20800
    55
consts
wenzelm@20800
    56
  map_of :: "('a * 'b) list => 'a ~=> 'b"
wenzelm@20800
    57
  map_upds :: "('a ~=> 'b) => 'a list => 'b list => ('a ~=> 'b)"
wenzelm@20800
    58
nipkow@14180
    59
nonterminals
nipkow@14180
    60
  maplets maplet
nipkow@14180
    61
oheimb@5300
    62
syntax
nipkow@14180
    63
  "_maplet"  :: "['a, 'a] => maplet"             ("_ /|->/ _")
nipkow@14180
    64
  "_maplets" :: "['a, 'a] => maplet"             ("_ /[|->]/ _")
nipkow@14180
    65
  ""         :: "maplet => maplets"             ("_")
nipkow@14180
    66
  "_Maplets" :: "[maplet, maplets] => maplets" ("_,/ _")
nipkow@14180
    67
  "_MapUpd"  :: "['a ~=> 'b, maplets] => 'a ~=> 'b" ("_/'(_')" [900,0]900)
nipkow@14180
    68
  "_Map"     :: "maplets => 'a ~=> 'b"            ("(1[_])")
nipkow@3981
    69
wenzelm@12114
    70
syntax (xsymbols)
nipkow@14180
    71
  "_maplet"  :: "['a, 'a] => maplet"             ("_ /\<mapsto>/ _")
nipkow@14180
    72
  "_maplets" :: "['a, 'a] => maplet"             ("_ /[\<mapsto>]/ _")
nipkow@14180
    73
oheimb@5300
    74
translations
nipkow@14180
    75
  "_MapUpd m (_Maplets xy ms)"  == "_MapUpd (_MapUpd m xy) ms"
nipkow@14180
    76
  "_MapUpd m (_maplet  x y)"    == "m(x:=Some y)"
nipkow@14180
    77
  "_MapUpd m (_maplets x y)"    == "map_upds m x y"
wenzelm@19947
    78
  "_Map ms"                     == "_MapUpd (CONST empty) ms"
nipkow@14180
    79
  "_Map (_Maplets ms1 ms2)"     <= "_MapUpd (_Map ms1) ms2"
nipkow@14180
    80
  "_Maplets ms1 (_Maplets ms2 ms3)" <= "_Maplets (_Maplets ms1 ms2) ms3"
nipkow@14180
    81
berghofe@5183
    82
primrec
berghofe@5183
    83
  "map_of [] = empty"
oheimb@5300
    84
  "map_of (p#ps) = (map_of ps)(fst p |-> snd p)"
oheimb@5300
    85
wenzelm@20800
    86
defs
wenzelm@20800
    87
  map_upds_def: "m(xs [|->] ys) == m ++ map_of (rev(zip xs ys))"
wenzelm@20800
    88
nipkow@19323
    89
(* special purpose constants that should be defined somewhere else and
nipkow@19323
    90
whose syntax is a bit odd as well:
nipkow@19323
    91
nipkow@19323
    92
 "@chg_map" :: "('a ~=> 'b) => 'a => ('b => 'b) => ('a ~=> 'b)"
wenzelm@20800
    93
                                          ("_/'(_/\<mapsto>\<lambda>_. _')"  [900,0,0,0] 900)
nipkow@19323
    94
  "m(x\<mapsto>\<lambda>y. f)" == "chg_map (\<lambda>y. f) x m"
nipkow@19323
    95
wenzelm@20800
    96
map_upd_s::"('a ~=> 'b) => 'a set => 'b =>
wenzelm@20800
    97
            ('a ~=> 'b)"                         ("_/'(_{|->}_/')" [900,0,0]900)
wenzelm@20800
    98
map_subst::"('a ~=> 'b) => 'b => 'b =>
wenzelm@20800
    99
            ('a ~=> 'b)"                         ("_/'(_~>_/')"    [900,0,0]900)
nipkow@19323
   100
nipkow@19323
   101
map_upd_s_def: "m(as{|->}b) == %x. if x : as then Some b else m x"
nipkow@19323
   102
map_subst_def: "m(a~>b)     == %x. if m x = Some a then Some b else m x"
nipkow@19323
   103
nipkow@19323
   104
  map_upd_s  :: "('a ~=> 'b) => 'a set => 'b => ('a ~=> 'b)"
wenzelm@20800
   105
                                                 ("_/'(_/{\<mapsto>}/_')" [900,0,0]900)
wenzelm@20800
   106
  map_subst :: "('a ~=> 'b) => 'b => 'b =>
wenzelm@20800
   107
                ('a ~=> 'b)"                     ("_/'(_\<leadsto>_/')"    [900,0,0]900)
nipkow@19323
   108
nipkow@19323
   109
nipkow@19323
   110
subsection {* @{term [source] map_upd_s} *}
nipkow@19323
   111
wenzelm@20800
   112
lemma map_upd_s_apply [simp]:
nipkow@19323
   113
  "(m(as{|->}b)) x = (if x : as then Some b else m x)"
nipkow@19323
   114
by (simp add: map_upd_s_def)
nipkow@19323
   115
wenzelm@20800
   116
lemma map_subst_apply [simp]:
wenzelm@20800
   117
  "(m(a~>b)) x = (if m x = Some a then Some b else m x)"
nipkow@19323
   118
by (simp add: map_subst_def)
nipkow@19323
   119
nipkow@19323
   120
*)
webertj@13908
   121
wenzelm@20800
   122
wenzelm@17399
   123
subsection {* @{term [source] empty} *}
webertj@13908
   124
wenzelm@20800
   125
lemma empty_upd_none [simp]: "empty(x := None) = empty"
wenzelm@20800
   126
  by (rule ext) simp
webertj@13908
   127
webertj@13908
   128
(* FIXME: what is this sum_case nonsense?? *)
nipkow@13910
   129
lemma sum_case_empty_empty[simp]: "sum_case empty empty = empty"
wenzelm@20800
   130
  by (rule ext) (simp split: sum.split)
wenzelm@20800
   131
webertj@13908
   132
wenzelm@17399
   133
subsection {* @{term [source] map_upd} *}
webertj@13908
   134
webertj@13908
   135
lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
wenzelm@20800
   136
  by (rule ext) simp
webertj@13908
   137
wenzelm@20800
   138
lemma map_upd_nonempty [simp]: "t(k|->x) ~= empty"
wenzelm@20800
   139
proof
wenzelm@20800
   140
  assume "t(k \<mapsto> x) = empty"
wenzelm@20800
   141
  then have "(t(k \<mapsto> x)) k = None" by simp
wenzelm@20800
   142
  then show False by simp
wenzelm@20800
   143
qed
webertj@13908
   144
wenzelm@20800
   145
lemma map_upd_eqD1:
wenzelm@20800
   146
  assumes "m(a\<mapsto>x) = n(a\<mapsto>y)"
wenzelm@20800
   147
  shows "x = y"
wenzelm@20800
   148
proof -
wenzelm@20800
   149
  from prems have "(m(a\<mapsto>x)) a = (n(a\<mapsto>y)) a" by simp
wenzelm@20800
   150
  then show ?thesis by simp
wenzelm@20800
   151
qed
oheimb@14100
   152
wenzelm@20800
   153
lemma map_upd_Some_unfold:
wenzelm@20800
   154
    "((m(a|->b)) x = Some y) = (x = a \<and> b = y \<or> x \<noteq> a \<and> m x = Some y)"
wenzelm@20800
   155
  by auto
oheimb@14100
   156
wenzelm@20800
   157
lemma image_map_upd [simp]: "x \<notin> A \<Longrightarrow> m(x \<mapsto> y) ` A = m ` A"
wenzelm@20800
   158
  by auto
nipkow@15303
   159
webertj@13908
   160
lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))"
wenzelm@20800
   161
  unfolding image_def
wenzelm@20800
   162
  apply (simp (no_asm_use) add: full_SetCompr_eq)
wenzelm@20800
   163
  apply (rule finite_subset)
wenzelm@20800
   164
   prefer 2 apply assumption
wenzelm@20800
   165
  apply auto
wenzelm@20800
   166
  done
webertj@13908
   167
webertj@13908
   168
webertj@13908
   169
(* FIXME: what is this sum_case nonsense?? *)
wenzelm@17399
   170
subsection {* @{term [source] sum_case} and @{term [source] empty}/@{term [source] map_upd} *}
webertj@13908
   171
wenzelm@20800
   172
lemma sum_case_map_upd_empty [simp]:
wenzelm@20800
   173
    "sum_case (m(k|->y)) empty = (sum_case m empty)(Inl k|->y)"
wenzelm@20800
   174
  by (rule ext) (simp split: sum.split)
webertj@13908
   175
wenzelm@20800
   176
lemma sum_case_empty_map_upd [simp]:
wenzelm@20800
   177
    "sum_case empty (m(k|->y)) = (sum_case empty m)(Inr k|->y)"
wenzelm@20800
   178
  by (rule ext) (simp split: sum.split)
webertj@13908
   179
wenzelm@20800
   180
lemma sum_case_map_upd_map_upd [simp]:
wenzelm@20800
   181
    "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
wenzelm@20800
   182
  by (rule ext) (simp split: sum.split)
webertj@13908
   183
webertj@13908
   184
wenzelm@17399
   185
subsection {* @{term [source] map_of} *}
webertj@13908
   186
nipkow@15304
   187
lemma map_of_eq_None_iff:
wenzelm@20800
   188
    "(map_of xys x = None) = (x \<notin> fst ` (set xys))"
wenzelm@20800
   189
  by (induct xys) simp_all
nipkow@15304
   190
nipkow@15304
   191
lemma map_of_is_SomeD:
wenzelm@20800
   192
    "map_of xys x = Some y \<Longrightarrow> (x,y) \<in> set xys"
wenzelm@20800
   193
  apply (induct xys)
wenzelm@20800
   194
   apply simp
wenzelm@20800
   195
  apply (clarsimp split: if_splits)
wenzelm@20800
   196
  done
nipkow@15304
   197
wenzelm@20800
   198
lemma map_of_eq_Some_iff [simp]:
wenzelm@20800
   199
    "distinct(map fst xys) \<Longrightarrow> (map_of xys x = Some y) = ((x,y) \<in> set xys)"
wenzelm@20800
   200
  apply (induct xys)
wenzelm@20800
   201
   apply simp
wenzelm@20800
   202
  apply (auto simp: map_of_eq_None_iff [symmetric])
wenzelm@20800
   203
  done
nipkow@15304
   204
wenzelm@20800
   205
lemma Some_eq_map_of_iff [simp]:
wenzelm@20800
   206
    "distinct(map fst xys) \<Longrightarrow> (Some y = map_of xys x) = ((x,y) \<in> set xys)"
wenzelm@20800
   207
  by (auto simp del:map_of_eq_Some_iff simp add: map_of_eq_Some_iff [symmetric])
nipkow@15304
   208
paulson@17724
   209
lemma map_of_is_SomeI [simp]: "\<lbrakk> distinct(map fst xys); (x,y) \<in> set xys \<rbrakk>
wenzelm@20800
   210
    \<Longrightarrow> map_of xys x = Some y"
wenzelm@20800
   211
  apply (induct xys)
wenzelm@20800
   212
   apply simp
wenzelm@20800
   213
  apply force
wenzelm@20800
   214
  done
nipkow@15304
   215
wenzelm@20800
   216
lemma map_of_zip_is_None [simp]:
wenzelm@20800
   217
    "length xs = length ys \<Longrightarrow> (map_of (zip xs ys) x = None) = (x \<notin> set xs)"
wenzelm@20800
   218
  by (induct rule: list_induct2) simp_all
nipkow@15110
   219
nipkow@15110
   220
lemma finite_range_map_of: "finite (range (map_of xys))"
wenzelm@20800
   221
  apply (induct xys)
wenzelm@20800
   222
   apply (simp_all add: image_constant)
wenzelm@20800
   223
  apply (rule finite_subset)
wenzelm@20800
   224
   prefer 2 apply assumption
wenzelm@20800
   225
  apply auto
wenzelm@20800
   226
  done
nipkow@15110
   227
wenzelm@20800
   228
lemma map_of_SomeD: "map_of xs k = Some y \<Longrightarrow> (k, y) \<in> set xs"
wenzelm@20800
   229
  by (induct xs) (simp, atomize (full), auto)
webertj@13908
   230
wenzelm@20800
   231
lemma map_of_mapk_SomeI:
wenzelm@20800
   232
  assumes "inj f"
wenzelm@20800
   233
  shows "map_of t k = Some x ==>
wenzelm@20800
   234
    map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
wenzelm@20800
   235
  by (induct t) (auto simp add: `inj f` inj_eq)
webertj@13908
   236
wenzelm@20800
   237
lemma weak_map_of_SomeI: "(k, x) : set l ==> \<exists>x. map_of l k = Some x"
wenzelm@20800
   238
  by (induct l) auto
webertj@13908
   239
wenzelm@20800
   240
lemma map_of_filter_in:
wenzelm@20800
   241
  assumes 1: "map_of xs k = Some z"
wenzelm@20800
   242
    and 2: "P k z"
wenzelm@20800
   243
  shows "map_of (filter (split P) xs) k = Some z"
wenzelm@20800
   244
  using 1 by (induct xs) (insert 2, auto)
webertj@13908
   245
webertj@13908
   246
lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
wenzelm@20800
   247
  by (induct xs) auto
webertj@13908
   248
webertj@13908
   249
wenzelm@17399
   250
subsection {* @{term [source] option_map} related *}
webertj@13908
   251
wenzelm@20800
   252
lemma option_map_o_empty [simp]: "option_map f o empty = empty"
wenzelm@20800
   253
  by (rule ext) simp
webertj@13908
   254
wenzelm@20800
   255
lemma option_map_o_map_upd [simp]:
wenzelm@20800
   256
    "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
wenzelm@20800
   257
  by (rule ext) simp
wenzelm@20800
   258
webertj@13908
   259
wenzelm@17399
   260
subsection {* @{term [source] map_comp} related *}
schirmer@17391
   261
wenzelm@20800
   262
lemma map_comp_empty [simp]:
wenzelm@20800
   263
    "m \<circ>\<^sub>m empty = empty"
wenzelm@20800
   264
    "empty \<circ>\<^sub>m m = empty"
schirmer@17391
   265
  by (auto simp add: map_comp_def intro: ext split: option.splits)
schirmer@17391
   266
wenzelm@20800
   267
lemma map_comp_simps [simp]:
wenzelm@20800
   268
    "m2 k = None \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = None"
wenzelm@20800
   269
    "m2 k = Some k' \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = m1 k'"
schirmer@17391
   270
  by (auto simp add: map_comp_def)
schirmer@17391
   271
schirmer@17391
   272
lemma map_comp_Some_iff:
wenzelm@20800
   273
    "((m1 \<circ>\<^sub>m m2) k = Some v) = (\<exists>k'. m2 k = Some k' \<and> m1 k' = Some v)"
schirmer@17391
   274
  by (auto simp add: map_comp_def split: option.splits)
schirmer@17391
   275
schirmer@17391
   276
lemma map_comp_None_iff:
wenzelm@20800
   277
    "((m1 \<circ>\<^sub>m m2) k = None) = (m2 k = None \<or> (\<exists>k'. m2 k = Some k' \<and> m1 k' = None)) "
schirmer@17391
   278
  by (auto simp add: map_comp_def split: option.splits)
webertj@13908
   279
wenzelm@20800
   280
oheimb@14100
   281
subsection {* @{text "++"} *}
webertj@13908
   282
nipkow@14025
   283
lemma map_add_empty[simp]: "m ++ empty = m"
wenzelm@20800
   284
  unfolding map_add_def by simp
webertj@13908
   285
nipkow@14025
   286
lemma empty_map_add[simp]: "empty ++ m = m"
wenzelm@20800
   287
  unfolding map_add_def by (rule ext) (simp split: option.split)
webertj@13908
   288
nipkow@14025
   289
lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3"
wenzelm@20800
   290
  unfolding map_add_def by (rule ext) (simp add: map_add_def split: option.split)
wenzelm@20800
   291
wenzelm@20800
   292
lemma map_add_Some_iff:
wenzelm@20800
   293
    "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
wenzelm@20800
   294
  unfolding map_add_def by (simp split: option.split)
nipkow@14025
   295
wenzelm@20800
   296
lemma map_add_SomeD [dest!]:
wenzelm@20800
   297
    "(m ++ n) k = Some x \<Longrightarrow> n k = Some x \<or> n k = None \<and> m k = Some x"
wenzelm@20800
   298
  by (rule map_add_Some_iff [THEN iffD1])
webertj@13908
   299
wenzelm@20800
   300
lemma map_add_find_right [simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
wenzelm@20800
   301
  by (subst map_add_Some_iff) fast
webertj@13908
   302
nipkow@14025
   303
lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)"
wenzelm@20800
   304
  unfolding map_add_def by (simp split: option.split)
webertj@13908
   305
nipkow@14025
   306
lemma map_add_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)"
wenzelm@20800
   307
  unfolding map_add_def by (rule ext) simp
webertj@13908
   308
nipkow@14186
   309
lemma map_add_upds[simp]: "m1 ++ (m2(xs[\<mapsto>]ys)) = (m1++m2)(xs[\<mapsto>]ys)"
wenzelm@20800
   310
  by (simp add: map_upds_def)
nipkow@14186
   311
wenzelm@20800
   312
lemma map_of_append[simp]: "map_of (xs @ ys) = map_of ys ++ map_of xs"
wenzelm@20800
   313
  unfolding map_add_def
wenzelm@20800
   314
  apply (induct xs)
wenzelm@20800
   315
   apply simp
wenzelm@20800
   316
  apply (rule ext)
wenzelm@20800
   317
  apply (simp split add: option.split)
wenzelm@20800
   318
  done
webertj@13908
   319
nipkow@14025
   320
lemma finite_range_map_of_map_add:
wenzelm@20800
   321
  "finite (range f) ==> finite (range (f ++ map_of l))"
wenzelm@20800
   322
  apply (induct l)
wenzelm@20800
   323
   apply (auto simp del: fun_upd_apply)
wenzelm@20800
   324
  apply (erule finite_range_updI)
wenzelm@20800
   325
  done
webertj@13908
   326
wenzelm@20800
   327
lemma inj_on_map_add_dom [iff]:
wenzelm@20800
   328
    "inj_on (m ++ m') (dom m') = inj_on m' (dom m')"
wenzelm@20800
   329
  unfolding map_add_def dom_def inj_on_def
wenzelm@20800
   330
  by (fastsimp split: option.splits)
wenzelm@20800
   331
nipkow@15304
   332
wenzelm@17399
   333
subsection {* @{term [source] restrict_map} *}
oheimb@14100
   334
wenzelm@20800
   335
lemma restrict_map_to_empty [simp]: "m|`{} = empty"
wenzelm@20800
   336
  by (simp add: restrict_map_def)
nipkow@14186
   337
wenzelm@20800
   338
lemma restrict_map_empty [simp]: "empty|`D = empty"
wenzelm@20800
   339
  by (simp add: restrict_map_def)
nipkow@14186
   340
nipkow@15693
   341
lemma restrict_in [simp]: "x \<in> A \<Longrightarrow> (m|`A) x = m x"
wenzelm@20800
   342
  by (simp add: restrict_map_def)
oheimb@14100
   343
nipkow@15693
   344
lemma restrict_out [simp]: "x \<notin> A \<Longrightarrow> (m|`A) x = None"
wenzelm@20800
   345
  by (simp add: restrict_map_def)
oheimb@14100
   346
nipkow@15693
   347
lemma ran_restrictD: "y \<in> ran (m|`A) \<Longrightarrow> \<exists>x\<in>A. m x = Some y"
wenzelm@20800
   348
  by (auto simp: restrict_map_def ran_def split: split_if_asm)
oheimb@14100
   349
nipkow@15693
   350
lemma dom_restrict [simp]: "dom (m|`A) = dom m \<inter> A"
wenzelm@20800
   351
  by (auto simp: restrict_map_def dom_def split: split_if_asm)
oheimb@14100
   352
nipkow@15693
   353
lemma restrict_upd_same [simp]: "m(x\<mapsto>y)|`(-{x}) = m|`(-{x})"
wenzelm@20800
   354
  by (rule ext) (auto simp: restrict_map_def)
oheimb@14100
   355
nipkow@15693
   356
lemma restrict_restrict [simp]: "m|`A|`B = m|`(A\<inter>B)"
wenzelm@20800
   357
  by (rule ext) (auto simp: restrict_map_def)
oheimb@14100
   358
wenzelm@20800
   359
lemma restrict_fun_upd [simp]:
wenzelm@20800
   360
    "m(x := y)|`D = (if x \<in> D then (m|`(D-{x}))(x := y) else m|`D)"
wenzelm@20800
   361
  by (simp add: restrict_map_def expand_fun_eq)
nipkow@14186
   362
wenzelm@20800
   363
lemma fun_upd_None_restrict [simp]:
wenzelm@20800
   364
    "(m|`D)(x := None) = (if x:D then m|`(D - {x}) else m|`D)"
wenzelm@20800
   365
  by (simp add: restrict_map_def expand_fun_eq)
nipkow@14186
   366
wenzelm@20800
   367
lemma fun_upd_restrict: "(m|`D)(x := y) = (m|`(D-{x}))(x := y)"
wenzelm@20800
   368
  by (simp add: restrict_map_def expand_fun_eq)
nipkow@14186
   369
wenzelm@20800
   370
lemma fun_upd_restrict_conv [simp]:
wenzelm@20800
   371
    "x \<in> D \<Longrightarrow> (m|`D)(x := y) = (m|`(D-{x}))(x := y)"
wenzelm@20800
   372
  by (simp add: restrict_map_def expand_fun_eq)
nipkow@14186
   373
oheimb@14100
   374
wenzelm@17399
   375
subsection {* @{term [source] map_upds} *}
nipkow@14025
   376
wenzelm@20800
   377
lemma map_upds_Nil1 [simp]: "m([] [|->] bs) = m"
wenzelm@20800
   378
  by (simp add: map_upds_def)
nipkow@14025
   379
wenzelm@20800
   380
lemma map_upds_Nil2 [simp]: "m(as [|->] []) = m"
wenzelm@20800
   381
  by (simp add:map_upds_def)
wenzelm@20800
   382
wenzelm@20800
   383
lemma map_upds_Cons [simp]: "m(a#as [|->] b#bs) = (m(a|->b))(as[|->]bs)"
wenzelm@20800
   384
  by (simp add:map_upds_def)
nipkow@14025
   385
wenzelm@20800
   386
lemma map_upds_append1 [simp]: "\<And>ys m. size xs < size ys \<Longrightarrow>
wenzelm@20800
   387
    m(xs@[x] [\<mapsto>] ys) = m(xs [\<mapsto>] ys)(x \<mapsto> ys!size xs)"
wenzelm@20800
   388
  apply(induct xs)
wenzelm@20800
   389
   apply (clarsimp simp add: neq_Nil_conv)
wenzelm@20800
   390
  apply (case_tac ys)
wenzelm@20800
   391
   apply simp
wenzelm@20800
   392
  apply simp
wenzelm@20800
   393
  done
nipkow@14187
   394
wenzelm@20800
   395
lemma map_upds_list_update2_drop [simp]:
wenzelm@20800
   396
  "\<lbrakk>size xs \<le> i; i < size ys\<rbrakk>
wenzelm@20800
   397
    \<Longrightarrow> m(xs[\<mapsto>]ys[i:=y]) = m(xs[\<mapsto>]ys)"
wenzelm@20800
   398
  apply (induct xs arbitrary: m ys i)
wenzelm@20800
   399
   apply simp
wenzelm@20800
   400
  apply (case_tac ys)
wenzelm@20800
   401
   apply simp
wenzelm@20800
   402
  apply (simp split: nat.split)
wenzelm@20800
   403
  done
nipkow@14025
   404
wenzelm@20800
   405
lemma map_upd_upds_conv_if:
wenzelm@20800
   406
  "(f(x|->y))(xs [|->] ys) =
wenzelm@20800
   407
   (if x : set(take (length ys) xs) then f(xs [|->] ys)
wenzelm@20800
   408
                                    else (f(xs [|->] ys))(x|->y))"
wenzelm@20800
   409
  apply (induct xs arbitrary: x y ys f)
wenzelm@20800
   410
   apply simp
wenzelm@20800
   411
  apply (case_tac ys)
wenzelm@20800
   412
   apply (auto split: split_if simp: fun_upd_twist)
wenzelm@20800
   413
  done
nipkow@14025
   414
nipkow@14025
   415
lemma map_upds_twist [simp]:
wenzelm@20800
   416
    "a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)"
wenzelm@20800
   417
  using set_take_subset by (fastsimp simp add: map_upd_upds_conv_if)
nipkow@14025
   418
wenzelm@20800
   419
lemma map_upds_apply_nontin [simp]:
wenzelm@20800
   420
    "x ~: set xs ==> (f(xs[|->]ys)) x = f x"
wenzelm@20800
   421
  apply (induct xs arbitrary: ys)
wenzelm@20800
   422
   apply simp
wenzelm@20800
   423
  apply (case_tac ys)
wenzelm@20800
   424
   apply (auto simp: map_upd_upds_conv_if)
wenzelm@20800
   425
  done
nipkow@14025
   426
wenzelm@20800
   427
lemma fun_upds_append_drop [simp]:
wenzelm@20800
   428
    "size xs = size ys \<Longrightarrow> m(xs@zs[\<mapsto>]ys) = m(xs[\<mapsto>]ys)"
wenzelm@20800
   429
  apply (induct xs arbitrary: m ys)
wenzelm@20800
   430
   apply simp
wenzelm@20800
   431
  apply (case_tac ys)
wenzelm@20800
   432
   apply simp_all
wenzelm@20800
   433
  done
nipkow@14300
   434
wenzelm@20800
   435
lemma fun_upds_append2_drop [simp]:
wenzelm@20800
   436
    "size xs = size ys \<Longrightarrow> m(xs[\<mapsto>]ys@zs) = m(xs[\<mapsto>]ys)"
wenzelm@20800
   437
  apply (induct xs arbitrary: m ys)
wenzelm@20800
   438
   apply simp
wenzelm@20800
   439
  apply (case_tac ys)
wenzelm@20800
   440
   apply simp_all
wenzelm@20800
   441
  done
nipkow@14300
   442
nipkow@14300
   443
wenzelm@20800
   444
lemma restrict_map_upds[simp]:
wenzelm@20800
   445
  "\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk>
wenzelm@20800
   446
    \<Longrightarrow> m(xs [\<mapsto>] ys)|`D = (m|`(D - set xs))(xs [\<mapsto>] ys)"
wenzelm@20800
   447
  apply (induct xs arbitrary: m ys)
wenzelm@20800
   448
   apply simp
wenzelm@20800
   449
  apply (case_tac ys)
wenzelm@20800
   450
   apply simp
wenzelm@20800
   451
  apply (simp add: Diff_insert [symmetric] insert_absorb)
wenzelm@20800
   452
  apply (simp add: map_upd_upds_conv_if)
wenzelm@20800
   453
  done
nipkow@14186
   454
nipkow@14186
   455
wenzelm@17399
   456
subsection {* @{term [source] dom} *}
webertj@13908
   457
webertj@13908
   458
lemma domI: "m a = Some b ==> a : dom m"
wenzelm@20800
   459
  unfolding dom_def by simp
oheimb@14100
   460
(* declare domI [intro]? *)
webertj@13908
   461
paulson@15369
   462
lemma domD: "a : dom m ==> \<exists>b. m a = Some b"
wenzelm@20800
   463
  by (cases "m a") (auto simp add: dom_def)
webertj@13908
   464
wenzelm@20800
   465
lemma domIff [iff, simp del]: "(a : dom m) = (m a ~= None)"
wenzelm@20800
   466
  unfolding dom_def by simp
webertj@13908
   467
wenzelm@20800
   468
lemma dom_empty [simp]: "dom empty = {}"
wenzelm@20800
   469
  unfolding dom_def by simp
webertj@13908
   470
wenzelm@20800
   471
lemma dom_fun_upd [simp]:
wenzelm@20800
   472
    "dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))"
wenzelm@20800
   473
  unfolding dom_def by auto
webertj@13908
   474
nipkow@13937
   475
lemma dom_map_of: "dom(map_of xys) = {x. \<exists>y. (x,y) : set xys}"
wenzelm@20800
   476
  by (induct xys) (auto simp del: fun_upd_apply)
nipkow@13937
   477
nipkow@15304
   478
lemma dom_map_of_conv_image_fst:
wenzelm@20800
   479
    "dom(map_of xys) = fst ` (set xys)"
wenzelm@20800
   480
  unfolding dom_map_of by force
nipkow@15304
   481
wenzelm@20800
   482
lemma dom_map_of_zip [simp]: "[| length xs = length ys; distinct xs |] ==>
wenzelm@20800
   483
    dom(map_of(zip xs ys)) = set xs"
wenzelm@20800
   484
  by (induct rule: list_induct2) simp_all
nipkow@15110
   485
webertj@13908
   486
lemma finite_dom_map_of: "finite (dom (map_of l))"
wenzelm@20800
   487
  unfolding dom_def
wenzelm@20800
   488
  by (induct l) (auto simp add: insert_Collect [symmetric])
webertj@13908
   489
wenzelm@20800
   490
lemma dom_map_upds [simp]:
wenzelm@20800
   491
    "dom(m(xs[|->]ys)) = set(take (length ys) xs) Un dom m"
wenzelm@20800
   492
  apply (induct xs arbitrary: m ys)
wenzelm@20800
   493
   apply simp
wenzelm@20800
   494
  apply (case_tac ys)
wenzelm@20800
   495
   apply auto
wenzelm@20800
   496
  done
nipkow@13910
   497
wenzelm@20800
   498
lemma dom_map_add [simp]: "dom(m++n) = dom n Un dom m"
wenzelm@20800
   499
  unfolding dom_def by auto
nipkow@13910
   500
wenzelm@20800
   501
lemma dom_override_on [simp]:
wenzelm@20800
   502
  "dom(override_on f g A) =
wenzelm@20800
   503
    (dom f  - {a. a : A - dom g}) Un {a. a : A Int dom g}"
wenzelm@20800
   504
  unfolding dom_def override_on_def by auto
webertj@13908
   505
nipkow@14027
   506
lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1++m2 = m2++m1"
wenzelm@20800
   507
  by (rule ext) (force simp: map_add_def dom_def split: option.split)
wenzelm@20800
   508
nipkow@14027
   509
wenzelm@17399
   510
subsection {* @{term [source] ran} *}
oheimb@14100
   511
wenzelm@20800
   512
lemma ranI: "m a = Some b ==> b : ran m"
wenzelm@20800
   513
  unfolding ran_def by auto
oheimb@14100
   514
(* declare ranI [intro]? *)
webertj@13908
   515
wenzelm@20800
   516
lemma ran_empty [simp]: "ran empty = {}"
wenzelm@20800
   517
  unfolding ran_def by simp
webertj@13908
   518
wenzelm@20800
   519
lemma ran_map_upd [simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
wenzelm@20800
   520
  unfolding ran_def
wenzelm@20800
   521
  apply auto
wenzelm@20800
   522
  apply (subgoal_tac "aa ~= a")
wenzelm@20800
   523
   apply auto
wenzelm@20800
   524
  done
wenzelm@20800
   525
nipkow@13910
   526
oheimb@14100
   527
subsection {* @{text "map_le"} *}
nipkow@13910
   528
kleing@13912
   529
lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g"
wenzelm@20800
   530
  by (simp add: map_le_def)
nipkow@13910
   531
paulson@17724
   532
lemma upd_None_map_le [simp]: "f(x := None) \<subseteq>\<^sub>m f"
wenzelm@20800
   533
  by (force simp add: map_le_def)
nipkow@14187
   534
nipkow@13910
   535
lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)"
wenzelm@20800
   536
  by (fastsimp simp add: map_le_def)
nipkow@13910
   537
paulson@17724
   538
lemma map_le_imp_upd_le [simp]: "m1 \<subseteq>\<^sub>m m2 \<Longrightarrow> m1(x := None) \<subseteq>\<^sub>m m2(x \<mapsto> y)"
wenzelm@20800
   539
  by (force simp add: map_le_def)
nipkow@14187
   540
wenzelm@20800
   541
lemma map_le_upds [simp]:
wenzelm@20800
   542
    "f \<subseteq>\<^sub>m g ==> f(as [|->] bs) \<subseteq>\<^sub>m g(as [|->] bs)"
wenzelm@20800
   543
  apply (induct as arbitrary: f g bs)
wenzelm@20800
   544
   apply simp
wenzelm@20800
   545
  apply (case_tac bs)
wenzelm@20800
   546
   apply auto
wenzelm@20800
   547
  done
webertj@13908
   548
webertj@14033
   549
lemma map_le_implies_dom_le: "(f \<subseteq>\<^sub>m g) \<Longrightarrow> (dom f \<subseteq> dom g)"
webertj@14033
   550
  by (fastsimp simp add: map_le_def dom_def)
webertj@14033
   551
webertj@14033
   552
lemma map_le_refl [simp]: "f \<subseteq>\<^sub>m f"
webertj@14033
   553
  by (simp add: map_le_def)
webertj@14033
   554
nipkow@14187
   555
lemma map_le_trans[trans]: "\<lbrakk> m1 \<subseteq>\<^sub>m m2; m2 \<subseteq>\<^sub>m m3\<rbrakk> \<Longrightarrow> m1 \<subseteq>\<^sub>m m3"
paulson@18447
   556
  by (auto simp add: map_le_def dom_def)
webertj@14033
   557
webertj@14033
   558
lemma map_le_antisym: "\<lbrakk> f \<subseteq>\<^sub>m g; g \<subseteq>\<^sub>m f \<rbrakk> \<Longrightarrow> f = g"
wenzelm@20800
   559
  unfolding map_le_def
webertj@14033
   560
  apply (rule ext)
paulson@14208
   561
  apply (case_tac "x \<in> dom f", simp)
paulson@14208
   562
  apply (case_tac "x \<in> dom g", simp, fastsimp)
wenzelm@20800
   563
  done
webertj@14033
   564
webertj@14033
   565
lemma map_le_map_add [simp]: "f \<subseteq>\<^sub>m (g ++ f)"
nipkow@18576
   566
  by (fastsimp simp add: map_le_def)
webertj@14033
   567
nipkow@15304
   568
lemma map_le_iff_map_add_commute: "(f \<subseteq>\<^sub>m f ++ g) = (f++g = g++f)"
wenzelm@20800
   569
  by (fastsimp simp add: map_add_def map_le_def expand_fun_eq split: option.splits)
nipkow@15304
   570
nipkow@15303
   571
lemma map_add_le_mapE: "f++g \<subseteq>\<^sub>m h \<Longrightarrow> g \<subseteq>\<^sub>m h"
wenzelm@20800
   572
  by (fastsimp simp add: map_le_def map_add_def dom_def)
nipkow@15303
   573
nipkow@15303
   574
lemma map_add_le_mapI: "\<lbrakk> f \<subseteq>\<^sub>m h; g \<subseteq>\<^sub>m h; f \<subseteq>\<^sub>m f++g \<rbrakk> \<Longrightarrow> f++g \<subseteq>\<^sub>m h"
wenzelm@20800
   575
  by (clarsimp simp add: map_le_def map_add_def dom_def split: option.splits)
nipkow@15303
   576
nipkow@3981
   577
end