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