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