src/HOL/Map.thy
author nipkow
Wed May 14 10:33:52 2003 +0200 (2003-05-14)
changeset 14026 c031a330a03f
parent 14025 d9b155757dc8
child 14027 68d247b7b14b
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@14026
   204
lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1++m2 = m2++m1"
nipkow@14026
   205
apply(rule ext)
nipkow@14026
   206
apply(fastsimp simp:map_add_def split:option.split)
nipkow@14026
   207
done
nipkow@14026
   208
nipkow@14025
   209
lemma map_add_Some_iff: 
webertj@13908
   210
 "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
nipkow@14025
   211
apply (unfold map_add_def)
webertj@13908
   212
apply (simp (no_asm) split add: option.split)
webertj@13908
   213
done
webertj@13908
   214
nipkow@14025
   215
lemmas map_add_SomeD = map_add_Some_iff [THEN iffD1, standard]
nipkow@14025
   216
declare map_add_SomeD [dest!]
webertj@13908
   217
nipkow@14025
   218
lemma map_add_find_right[simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
nipkow@14025
   219
apply (subst map_add_Some_iff)
webertj@13908
   220
apply fast
webertj@13908
   221
done
webertj@13908
   222
nipkow@14025
   223
lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)"
nipkow@14025
   224
apply (unfold map_add_def)
webertj@13908
   225
apply (simp (no_asm) split add: option.split)
webertj@13908
   226
done
webertj@13908
   227
nipkow@14025
   228
lemma map_add_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)"
nipkow@14025
   229
apply (unfold map_add_def)
webertj@13908
   230
apply (rule ext)
webertj@13908
   231
apply auto
webertj@13908
   232
done
webertj@13908
   233
nipkow@14025
   234
lemma map_of_append[simp]: "map_of (xs@ys) = map_of ys ++ map_of xs"
nipkow@14025
   235
apply (unfold map_add_def)
webertj@13908
   236
apply (induct_tac "xs")
webertj@13908
   237
apply (simp (no_asm))
webertj@13908
   238
apply (rule ext)
webertj@13908
   239
apply (simp (no_asm_simp) split add: option.split)
webertj@13908
   240
done
webertj@13908
   241
webertj@13908
   242
declare fun_upd_apply [simp del]
nipkow@14025
   243
lemma finite_range_map_of_map_add:
nipkow@14025
   244
 "finite (range f) ==> finite (range (f ++ map_of l))"
webertj@13908
   245
apply (induct_tac "l")
webertj@13908
   246
apply  auto
webertj@13908
   247
apply (erule finite_range_updI)
webertj@13908
   248
done
webertj@13908
   249
declare fun_upd_apply [simp]
webertj@13908
   250
webertj@13908
   251
nipkow@14025
   252
subsection {* map\_upds *}
nipkow@14025
   253
nipkow@14025
   254
lemma map_upds_Nil1[simp]: "m([] [|->] bs) = m"
nipkow@14025
   255
by(simp add:map_upds_def)
nipkow@14025
   256
nipkow@14025
   257
lemma map_upds_Nil2[simp]: "m(as [|->] []) = m"
nipkow@14025
   258
by(simp add:map_upds_def)
nipkow@14025
   259
nipkow@14025
   260
lemma map_upds_Cons[simp]: "m(a#as [|->] b#bs) = (m(a|->b))(as[|->]bs)"
nipkow@14025
   261
by(simp add:map_upds_def)
nipkow@14025
   262
nipkow@14025
   263
nipkow@14025
   264
lemma map_upd_upds_conv_if: "!!x y ys f.
nipkow@14025
   265
 (f(x|->y))(xs [|->] ys) =
nipkow@14025
   266
 (if x : set(take (length ys) xs) then f(xs [|->] ys)
nipkow@14025
   267
                                  else (f(xs [|->] ys))(x|->y))"
nipkow@14025
   268
apply(induct xs)
nipkow@14025
   269
 apply simp
nipkow@14025
   270
apply(case_tac ys)
nipkow@14025
   271
 apply(auto split:split_if simp:fun_upd_twist)
nipkow@14025
   272
done
nipkow@14025
   273
nipkow@14025
   274
lemma map_upds_twist [simp]:
nipkow@14025
   275
 "a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)"
nipkow@14025
   276
apply(insert set_take_subset)
nipkow@14025
   277
apply (fastsimp simp add: map_upd_upds_conv_if)
nipkow@14025
   278
done
nipkow@14025
   279
nipkow@14025
   280
lemma map_upds_apply_nontin[simp]:
nipkow@14025
   281
 "!!ys. x ~: set xs ==> (f(xs[|->]ys)) x = f x"
nipkow@14025
   282
apply(induct xs)
nipkow@14025
   283
 apply simp
nipkow@14025
   284
apply(case_tac ys)
nipkow@14025
   285
 apply(auto simp: map_upd_upds_conv_if)
nipkow@14025
   286
done
nipkow@14025
   287
nipkow@13937
   288
subsection {* dom *}
webertj@13908
   289
webertj@13908
   290
lemma domI: "m a = Some b ==> a : dom m"
webertj@13908
   291
apply (unfold dom_def)
webertj@13908
   292
apply auto
webertj@13908
   293
done
webertj@13908
   294
webertj@13908
   295
lemma domD: "a : dom m ==> ? b. m a = Some b"
webertj@13908
   296
apply (unfold dom_def)
webertj@13908
   297
apply auto
webertj@13908
   298
done
webertj@13908
   299
nipkow@13910
   300
lemma domIff[iff]: "(a : dom m) = (m a ~= None)"
webertj@13908
   301
apply (unfold dom_def)
webertj@13908
   302
apply auto
webertj@13908
   303
done
webertj@13908
   304
declare domIff [simp del]
webertj@13908
   305
nipkow@13910
   306
lemma dom_empty[simp]: "dom empty = {}"
webertj@13908
   307
apply (unfold dom_def)
webertj@13908
   308
apply (simp (no_asm))
webertj@13908
   309
done
webertj@13908
   310
nipkow@13910
   311
lemma dom_fun_upd[simp]:
nipkow@13910
   312
 "dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))"
nipkow@13910
   313
by (simp add:dom_def) blast
webertj@13908
   314
nipkow@13937
   315
lemma dom_map_of: "dom(map_of xys) = {x. \<exists>y. (x,y) : set xys}"
nipkow@13937
   316
apply(induct xys)
nipkow@13937
   317
apply(auto simp del:fun_upd_apply)
nipkow@13937
   318
done
nipkow@13937
   319
webertj@13908
   320
lemma finite_dom_map_of: "finite (dom (map_of l))"
webertj@13908
   321
apply (unfold dom_def)
webertj@13908
   322
apply (induct_tac "l")
webertj@13908
   323
apply (auto simp add: insert_Collect [symmetric])
webertj@13908
   324
done
webertj@13908
   325
nipkow@14025
   326
lemma dom_map_upds[simp]:
nipkow@14025
   327
 "!!m ys. dom(m(xs[|->]ys)) = set(take (length ys) xs) Un dom m"
nipkow@14025
   328
apply(induct xs)
nipkow@14025
   329
 apply simp
nipkow@14025
   330
apply(case_tac ys)
nipkow@14025
   331
 apply auto
nipkow@14025
   332
done
nipkow@13910
   333
nipkow@14025
   334
lemma dom_map_add[simp]: "dom(m++n) = dom n Un dom m"
webertj@13908
   335
apply (unfold dom_def)
webertj@13908
   336
apply auto
webertj@13908
   337
done
nipkow@13910
   338
nipkow@13910
   339
lemma dom_overwrite[simp]:
nipkow@13910
   340
 "dom(f(g|A)) = (dom f  - {a. a : A - dom g}) Un {a. a : A Int dom g}"
nipkow@13910
   341
by(auto simp add: dom_def overwrite_def)
webertj@13908
   342
nipkow@13937
   343
subsection {* ran *}
webertj@13908
   344
nipkow@13910
   345
lemma ran_empty[simp]: "ran empty = {}"
webertj@13908
   346
apply (unfold ran_def)
webertj@13908
   347
apply (simp (no_asm))
webertj@13908
   348
done
webertj@13908
   349
nipkow@13910
   350
lemma ran_map_upd[simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
webertj@13908
   351
apply (unfold ran_def)
webertj@13908
   352
apply auto
webertj@13908
   353
apply (subgoal_tac "~ (aa = a) ")
webertj@13908
   354
apply auto
webertj@13908
   355
done
nipkow@13910
   356
nipkow@13937
   357
subsection {* map\_le *}
nipkow@13910
   358
kleing@13912
   359
lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g"
nipkow@13910
   360
by(simp add:map_le_def)
nipkow@13910
   361
nipkow@13910
   362
lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)"
nipkow@13910
   363
by(fastsimp simp add:map_le_def)
nipkow@13910
   364
nipkow@13910
   365
lemma map_le_upds[simp]:
nipkow@13910
   366
 "!!f g bs. f \<subseteq>\<^sub>m g ==> f(as [|->] bs) \<subseteq>\<^sub>m g(as [|->] bs)"
nipkow@14025
   367
apply(induct as)
nipkow@14025
   368
 apply simp
nipkow@14025
   369
apply(case_tac bs)
nipkow@14025
   370
 apply auto
nipkow@14025
   371
done
webertj@13908
   372
nipkow@3981
   373
end