src/HOL/Map.thy
author nipkow
Wed May 14 10:22:09 2003 +0200 (2003-05-14)
changeset 14025 d9b155757dc8
parent 13937 e9d57517c9b1
child 14026 c031a330a03f
permissions -rw-r--r--
*** empty log message ***
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
webertj@13908
    11
theory Map = List:
nipkow@3981
    12
webertj@13908
    13
types ('a,'b) "~=>" = "'a => 'b option" (infixr 0)
nipkow@3981
    14
nipkow@3981
    15
consts
oheimb@5300
    16
chg_map	:: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
nipkow@14025
    17
map_add:: "('a ~=> 'b) => ('a ~=> 'b) => ('a ~=> 'b)" (infixl "++" 100)
oheimb@5300
    18
dom	:: "('a ~=> 'b) => 'a set"
oheimb@5300
    19
ran	:: "('a ~=> 'b) => 'b set"
oheimb@5300
    20
map_of	:: "('a * 'b)list => 'a ~=> 'b"
oheimb@5300
    21
map_upds:: "('a ~=> 'b) => 'a list => 'b list => 
nipkow@13910
    22
	    ('a ~=> 'b)"		 ("_/'(_[|->]_/')" [900,0,0]900)
nipkow@13910
    23
map_le  :: "('a ~=> 'b) => ('a ~=> 'b) => bool" (infix "\<subseteq>\<^sub>m" 50)
nipkow@13910
    24
oheimb@5300
    25
syntax
nipkow@13890
    26
empty	::  "'a ~=> 'b"
oheimb@5300
    27
map_upd	:: "('a ~=> 'b) => 'a => 'b => ('a ~=> 'b)"
nipkow@13910
    28
					 ("_/'(_/|->_')"   [900,0,0]900)
nipkow@3981
    29
wenzelm@12114
    30
syntax (xsymbols)
webertj@13908
    31
  "~=>"     :: "[type, type] => type"    (infixr "\<leadsto>" 0)
oheimb@5300
    32
  map_upd   :: "('a ~=> 'b) => 'a      => 'b      => ('a ~=> 'b)"
webertj@13908
    33
					  ("_/'(_/\<mapsto>/_')"  [900,0,0]900)
oheimb@5300
    34
  map_upds  :: "('a ~=> 'b) => 'a list => 'b list => ('a ~=> 'b)"
webertj@13908
    35
				         ("_/'(_/[\<mapsto>]/_')" [900,0,0]900)
oheimb@5300
    36
oheimb@5300
    37
translations
nipkow@13890
    38
  "empty"    => "_K None"
nipkow@13890
    39
  "empty"    <= "%x. None"
oheimb@5300
    40
oheimb@5300
    41
  "m(a|->b)" == "m(a:=Some b)"
nipkow@3981
    42
nipkow@3981
    43
defs
webertj@13908
    44
chg_map_def:  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
nipkow@3981
    45
nipkow@14025
    46
map_add_def: "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
nipkow@14025
    47
nipkow@14025
    48
map_upds_def: "m(xs [|->] ys) == m ++ map_of (rev(zip xs ys))"
nipkow@3981
    49
webertj@13908
    50
dom_def: "dom(m) == {a. m a ~= None}"
nipkow@14025
    51
ran_def: "ran(m) == {b. EX a. m a = Some b}"
nipkow@3981
    52
nipkow@13910
    53
map_le_def: "m1 \<subseteq>\<^sub>m m2  ==  ALL a : dom m1. m1 a = m2 a"
nipkow@13910
    54
berghofe@5183
    55
primrec
berghofe@5183
    56
  "map_of [] = empty"
oheimb@5300
    57
  "map_of (p#ps) = (map_of ps)(fst p |-> snd p)"
oheimb@5300
    58
webertj@13908
    59
nipkow@13937
    60
subsection {* empty *}
webertj@13908
    61
nipkow@13910
    62
lemma empty_upd_none[simp]: "empty(x := None) = empty"
webertj@13908
    63
apply (rule ext)
webertj@13908
    64
apply (simp (no_asm))
webertj@13908
    65
done
nipkow@13910
    66
webertj@13908
    67
webertj@13908
    68
(* FIXME: what is this sum_case nonsense?? *)
nipkow@13910
    69
lemma sum_case_empty_empty[simp]: "sum_case empty empty = empty"
webertj@13908
    70
apply (rule ext)
webertj@13908
    71
apply (simp (no_asm) split add: sum.split)
webertj@13908
    72
done
webertj@13908
    73
nipkow@13937
    74
subsection {* map\_upd *}
webertj@13908
    75
webertj@13908
    76
lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
webertj@13908
    77
apply (rule ext)
webertj@13908
    78
apply (simp (no_asm_simp))
webertj@13908
    79
done
webertj@13908
    80
nipkow@13910
    81
lemma map_upd_nonempty[simp]: "t(k|->x) ~= empty"
webertj@13908
    82
apply safe
webertj@13908
    83
apply (drule_tac x = "k" in fun_cong)
webertj@13908
    84
apply (simp (no_asm_use))
webertj@13908
    85
done
webertj@13908
    86
webertj@13908
    87
lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))"
webertj@13908
    88
apply (unfold image_def)
webertj@13908
    89
apply (simp (no_asm_use) add: full_SetCompr_eq)
webertj@13908
    90
apply (rule finite_subset)
webertj@13908
    91
prefer 2 apply (assumption)
webertj@13908
    92
apply auto
webertj@13908
    93
done
webertj@13908
    94
webertj@13908
    95
webertj@13908
    96
(* FIXME: what is this sum_case nonsense?? *)
nipkow@13937
    97
subsection {* sum\_case and empty/map\_upd *}
webertj@13908
    98
nipkow@13910
    99
lemma sum_case_map_upd_empty[simp]:
nipkow@13910
   100
 "sum_case (m(k|->y)) empty =  (sum_case m empty)(Inl k|->y)"
webertj@13908
   101
apply (rule ext)
webertj@13908
   102
apply (simp (no_asm) split add: sum.split)
webertj@13908
   103
done
webertj@13908
   104
nipkow@13910
   105
lemma sum_case_empty_map_upd[simp]:
nipkow@13910
   106
 "sum_case empty (m(k|->y)) =  (sum_case empty m)(Inr k|->y)"
webertj@13908
   107
apply (rule ext)
webertj@13908
   108
apply (simp (no_asm) split add: sum.split)
webertj@13908
   109
done
webertj@13908
   110
nipkow@13910
   111
lemma sum_case_map_upd_map_upd[simp]:
nipkow@13910
   112
 "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
webertj@13908
   113
apply (rule ext)
webertj@13908
   114
apply (simp (no_asm) split add: sum.split)
webertj@13908
   115
done
webertj@13908
   116
webertj@13908
   117
nipkow@13937
   118
subsection {* chg\_map *}
webertj@13908
   119
nipkow@13910
   120
lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
webertj@13908
   121
apply (unfold chg_map_def)
webertj@13908
   122
apply auto
webertj@13908
   123
done
webertj@13908
   124
nipkow@13910
   125
lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
webertj@13908
   126
apply (unfold chg_map_def)
webertj@13908
   127
apply auto
webertj@13908
   128
done
webertj@13908
   129
webertj@13908
   130
nipkow@13937
   131
subsection {* map\_of *}
webertj@13908
   132
webertj@13908
   133
lemma map_of_SomeD [rule_format (no_asm)]: "map_of xs k = Some y --> (k,y):set xs"
webertj@13908
   134
apply (induct_tac "xs")
webertj@13908
   135
apply  auto
webertj@13908
   136
done
webertj@13908
   137
webertj@13908
   138
lemma map_of_mapk_SomeI [rule_format (no_asm)]: "inj f ==> map_of t k = Some x -->  
webertj@13908
   139
   map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
webertj@13908
   140
apply (induct_tac "t")
webertj@13908
   141
apply  (auto simp add: inj_eq)
webertj@13908
   142
done
webertj@13908
   143
webertj@13908
   144
lemma weak_map_of_SomeI [rule_format (no_asm)]: "(k, x) : set l --> (? x. map_of l k = Some x)"
webertj@13908
   145
apply (induct_tac "l")
webertj@13908
   146
apply  auto
webertj@13908
   147
done
webertj@13908
   148
webertj@13908
   149
lemma map_of_filter_in: 
webertj@13908
   150
"[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z"
webertj@13908
   151
apply (rule mp)
webertj@13908
   152
prefer 2 apply (assumption)
webertj@13908
   153
apply (erule thin_rl)
webertj@13908
   154
apply (induct_tac "xs")
webertj@13908
   155
apply  auto
webertj@13908
   156
done
webertj@13908
   157
webertj@13908
   158
lemma finite_range_map_of: "finite (range (map_of l))"
webertj@13908
   159
apply (induct_tac "l")
webertj@13908
   160
apply  (simp_all (no_asm) add: image_constant)
webertj@13908
   161
apply (rule finite_subset)
webertj@13908
   162
prefer 2 apply (assumption)
webertj@13908
   163
apply auto
webertj@13908
   164
done
webertj@13908
   165
webertj@13908
   166
lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
webertj@13908
   167
apply (induct_tac "xs")
webertj@13908
   168
apply auto
webertj@13908
   169
done
webertj@13908
   170
webertj@13908
   171
nipkow@13937
   172
subsection {* option\_map related *}
webertj@13908
   173
nipkow@13910
   174
lemma option_map_o_empty[simp]: "option_map f o empty = empty"
webertj@13908
   175
apply (rule ext)
webertj@13908
   176
apply (simp (no_asm))
webertj@13908
   177
done
webertj@13908
   178
nipkow@13910
   179
lemma option_map_o_map_upd[simp]:
nipkow@13910
   180
 "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
webertj@13908
   181
apply (rule ext)
webertj@13908
   182
apply (simp (no_asm))
webertj@13908
   183
done
webertj@13908
   184
webertj@13908
   185
nipkow@13937
   186
subsection {* ++ *}
webertj@13908
   187
nipkow@14025
   188
lemma map_add_empty[simp]: "m ++ empty = m"
nipkow@14025
   189
apply (unfold map_add_def)
webertj@13908
   190
apply (simp (no_asm))
webertj@13908
   191
done
webertj@13908
   192
nipkow@14025
   193
lemma empty_map_add[simp]: "empty ++ m = m"
nipkow@14025
   194
apply (unfold map_add_def)
webertj@13908
   195
apply (rule ext)
webertj@13908
   196
apply (simp split add: option.split)
webertj@13908
   197
done
webertj@13908
   198
nipkow@14025
   199
lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3"
nipkow@14025
   200
apply(rule ext)
nipkow@14025
   201
apply(simp add: map_add_def split:option.split)
nipkow@14025
   202
done
nipkow@14025
   203
nipkow@14025
   204
lemma map_add_Some_iff: 
webertj@13908
   205
 "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
nipkow@14025
   206
apply (unfold map_add_def)
webertj@13908
   207
apply (simp (no_asm) split add: option.split)
webertj@13908
   208
done
webertj@13908
   209
nipkow@14025
   210
lemmas map_add_SomeD = map_add_Some_iff [THEN iffD1, standard]
nipkow@14025
   211
declare map_add_SomeD [dest!]
webertj@13908
   212
nipkow@14025
   213
lemma map_add_find_right[simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
nipkow@14025
   214
apply (subst map_add_Some_iff)
webertj@13908
   215
apply fast
webertj@13908
   216
done
webertj@13908
   217
nipkow@14025
   218
lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)"
nipkow@14025
   219
apply (unfold map_add_def)
webertj@13908
   220
apply (simp (no_asm) split add: option.split)
webertj@13908
   221
done
webertj@13908
   222
nipkow@14025
   223
lemma map_add_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)"
nipkow@14025
   224
apply (unfold map_add_def)
webertj@13908
   225
apply (rule ext)
webertj@13908
   226
apply auto
webertj@13908
   227
done
webertj@13908
   228
nipkow@14025
   229
lemma map_of_append[simp]: "map_of (xs@ys) = map_of ys ++ map_of xs"
nipkow@14025
   230
apply (unfold map_add_def)
webertj@13908
   231
apply (induct_tac "xs")
webertj@13908
   232
apply (simp (no_asm))
webertj@13908
   233
apply (rule ext)
webertj@13908
   234
apply (simp (no_asm_simp) split add: option.split)
webertj@13908
   235
done
webertj@13908
   236
webertj@13908
   237
declare fun_upd_apply [simp del]
nipkow@14025
   238
lemma finite_range_map_of_map_add:
nipkow@14025
   239
 "finite (range f) ==> finite (range (f ++ map_of l))"
webertj@13908
   240
apply (induct_tac "l")
webertj@13908
   241
apply  auto
webertj@13908
   242
apply (erule finite_range_updI)
webertj@13908
   243
done
webertj@13908
   244
declare fun_upd_apply [simp]
webertj@13908
   245
webertj@13908
   246
nipkow@14025
   247
subsection {* map\_upds *}
nipkow@14025
   248
nipkow@14025
   249
lemma map_upds_Nil1[simp]: "m([] [|->] bs) = m"
nipkow@14025
   250
by(simp add:map_upds_def)
nipkow@14025
   251
nipkow@14025
   252
lemma map_upds_Nil2[simp]: "m(as [|->] []) = m"
nipkow@14025
   253
by(simp add:map_upds_def)
nipkow@14025
   254
nipkow@14025
   255
lemma map_upds_Cons[simp]: "m(a#as [|->] b#bs) = (m(a|->b))(as[|->]bs)"
nipkow@14025
   256
by(simp add:map_upds_def)
nipkow@14025
   257
nipkow@14025
   258
nipkow@14025
   259
lemma map_upd_upds_conv_if: "!!x y ys f.
nipkow@14025
   260
 (f(x|->y))(xs [|->] ys) =
nipkow@14025
   261
 (if x : set(take (length ys) xs) then f(xs [|->] ys)
nipkow@14025
   262
                                  else (f(xs [|->] ys))(x|->y))"
nipkow@14025
   263
apply(induct xs)
nipkow@14025
   264
 apply simp
nipkow@14025
   265
apply(case_tac ys)
nipkow@14025
   266
 apply(auto split:split_if simp:fun_upd_twist)
nipkow@14025
   267
done
nipkow@14025
   268
nipkow@14025
   269
lemma map_upds_twist [simp]:
nipkow@14025
   270
 "a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)"
nipkow@14025
   271
apply(insert set_take_subset)
nipkow@14025
   272
apply (fastsimp simp add: map_upd_upds_conv_if)
nipkow@14025
   273
done
nipkow@14025
   274
nipkow@14025
   275
lemma map_upds_apply_nontin[simp]:
nipkow@14025
   276
 "!!ys. x ~: set xs ==> (f(xs[|->]ys)) x = f x"
nipkow@14025
   277
apply(induct xs)
nipkow@14025
   278
 apply simp
nipkow@14025
   279
apply(case_tac ys)
nipkow@14025
   280
 apply(auto simp: map_upd_upds_conv_if)
nipkow@14025
   281
done
nipkow@14025
   282
nipkow@13937
   283
subsection {* dom *}
webertj@13908
   284
webertj@13908
   285
lemma domI: "m a = Some b ==> a : dom m"
webertj@13908
   286
apply (unfold dom_def)
webertj@13908
   287
apply auto
webertj@13908
   288
done
webertj@13908
   289
webertj@13908
   290
lemma domD: "a : dom m ==> ? b. m a = Some b"
webertj@13908
   291
apply (unfold dom_def)
webertj@13908
   292
apply auto
webertj@13908
   293
done
webertj@13908
   294
nipkow@13910
   295
lemma domIff[iff]: "(a : dom m) = (m a ~= None)"
webertj@13908
   296
apply (unfold dom_def)
webertj@13908
   297
apply auto
webertj@13908
   298
done
webertj@13908
   299
declare domIff [simp del]
webertj@13908
   300
nipkow@13910
   301
lemma dom_empty[simp]: "dom empty = {}"
webertj@13908
   302
apply (unfold dom_def)
webertj@13908
   303
apply (simp (no_asm))
webertj@13908
   304
done
webertj@13908
   305
nipkow@13910
   306
lemma dom_fun_upd[simp]:
nipkow@13910
   307
 "dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))"
nipkow@13910
   308
by (simp add:dom_def) blast
webertj@13908
   309
nipkow@13937
   310
lemma dom_map_of: "dom(map_of xys) = {x. \<exists>y. (x,y) : set xys}"
nipkow@13937
   311
apply(induct xys)
nipkow@13937
   312
apply(auto simp del:fun_upd_apply)
nipkow@13937
   313
done
nipkow@13937
   314
webertj@13908
   315
lemma finite_dom_map_of: "finite (dom (map_of l))"
webertj@13908
   316
apply (unfold dom_def)
webertj@13908
   317
apply (induct_tac "l")
webertj@13908
   318
apply (auto simp add: insert_Collect [symmetric])
webertj@13908
   319
done
webertj@13908
   320
nipkow@14025
   321
lemma dom_map_upds[simp]:
nipkow@14025
   322
 "!!m ys. dom(m(xs[|->]ys)) = set(take (length ys) xs) Un dom m"
nipkow@14025
   323
apply(induct xs)
nipkow@14025
   324
 apply simp
nipkow@14025
   325
apply(case_tac ys)
nipkow@14025
   326
 apply auto
nipkow@14025
   327
done
nipkow@13910
   328
nipkow@14025
   329
lemma dom_map_add[simp]: "dom(m++n) = dom n Un dom m"
webertj@13908
   330
apply (unfold dom_def)
webertj@13908
   331
apply auto
webertj@13908
   332
done
nipkow@13910
   333
nipkow@13910
   334
lemma dom_overwrite[simp]:
nipkow@13910
   335
 "dom(f(g|A)) = (dom f  - {a. a : A - dom g}) Un {a. a : A Int dom g}"
nipkow@13910
   336
by(auto simp add: dom_def overwrite_def)
webertj@13908
   337
nipkow@13937
   338
subsection {* ran *}
webertj@13908
   339
nipkow@13910
   340
lemma ran_empty[simp]: "ran empty = {}"
webertj@13908
   341
apply (unfold ran_def)
webertj@13908
   342
apply (simp (no_asm))
webertj@13908
   343
done
webertj@13908
   344
nipkow@13910
   345
lemma ran_map_upd[simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
webertj@13908
   346
apply (unfold ran_def)
webertj@13908
   347
apply auto
webertj@13908
   348
apply (subgoal_tac "~ (aa = a) ")
webertj@13908
   349
apply auto
webertj@13908
   350
done
nipkow@13910
   351
nipkow@13937
   352
subsection {* map\_le *}
nipkow@13910
   353
kleing@13912
   354
lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g"
nipkow@13910
   355
by(simp add:map_le_def)
nipkow@13910
   356
nipkow@13910
   357
lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)"
nipkow@13910
   358
by(fastsimp simp add:map_le_def)
nipkow@13910
   359
nipkow@13910
   360
lemma map_le_upds[simp]:
nipkow@13910
   361
 "!!f g bs. f \<subseteq>\<^sub>m g ==> f(as [|->] bs) \<subseteq>\<^sub>m g(as [|->] bs)"
nipkow@14025
   362
apply(induct as)
nipkow@14025
   363
 apply simp
nipkow@14025
   364
apply(case_tac bs)
nipkow@14025
   365
 apply auto
nipkow@14025
   366
done
webertj@13908
   367
nipkow@3981
   368
end