src/HOL/Map.thy
author webertj
Fri Apr 11 23:11:13 2003 +0200 (2003-04-11)
changeset 13908 4bdfa9f77254
parent 13890 90611b4e0054
child 13909 a5247a49c85e
permissions -rw-r--r--
Map.ML integrated into Map.thy
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 => 
oheimb@5300
    20
	    ('a ~=> 'b)"			 ("_/'(_[|->]_/')" [900,0,0]900)
oheimb@5300
    21
syntax
nipkow@13890
    22
empty	::  "'a ~=> 'b"
oheimb@5300
    23
map_upd	:: "('a ~=> 'b) => 'a => 'b => ('a ~=> 'b)"
oheimb@5300
    24
					         ("_/'(_/|->_')"   [900,0,0]900)
nipkow@3981
    25
wenzelm@12114
    26
syntax (xsymbols)
webertj@13908
    27
  "~=>"     :: "[type, type] => type"    (infixr "\<leadsto>" 0)
oheimb@5300
    28
  map_upd   :: "('a ~=> 'b) => 'a      => 'b      => ('a ~=> 'b)"
webertj@13908
    29
					  ("_/'(_/\<mapsto>/_')"  [900,0,0]900)
oheimb@5300
    30
  map_upds  :: "('a ~=> 'b) => 'a list => 'b list => ('a ~=> 'b)"
webertj@13908
    31
				         ("_/'(_/[\<mapsto>]/_')" [900,0,0]900)
oheimb@5300
    32
oheimb@5300
    33
translations
nipkow@13890
    34
  "empty"    => "_K None"
nipkow@13890
    35
  "empty"    <= "%x. None"
oheimb@5300
    36
oheimb@5300
    37
  "m(a|->b)" == "m(a:=Some b)"
nipkow@3981
    38
nipkow@3981
    39
defs
nipkow@3981
    40
webertj@13908
    41
chg_map_def:  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
nipkow@3981
    42
webertj@13908
    43
override_def: "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
nipkow@3981
    44
webertj@13908
    45
dom_def: "dom(m) == {a. m a ~= None}"
webertj@13908
    46
ran_def: "ran(m) == {b. ? a. m a = Some b}"
nipkow@3981
    47
berghofe@5183
    48
primrec
berghofe@5183
    49
  "map_of [] = empty"
oheimb@5300
    50
  "map_of (p#ps) = (map_of ps)(fst p |-> snd p)"
oheimb@5300
    51
oheimb@5300
    52
primrec "t([]  [|->]bs) = t"
oheimb@5300
    53
        "t(a#as[|->]bs) = t(a|->hd bs)(as[|->]tl bs)"
nipkow@3981
    54
webertj@13908
    55
webertj@13908
    56
section "empty"
webertj@13908
    57
webertj@13908
    58
lemma empty_upd_none: "empty(x := None) = empty"
webertj@13908
    59
apply (rule ext)
webertj@13908
    60
apply (simp (no_asm))
webertj@13908
    61
done
webertj@13908
    62
declare empty_upd_none [simp]
webertj@13908
    63
webertj@13908
    64
(* FIXME: what is this sum_case nonsense?? *)
webertj@13908
    65
lemma sum_case_empty_empty: "sum_case empty empty = empty"
webertj@13908
    66
apply (rule ext)
webertj@13908
    67
apply (simp (no_asm) split add: sum.split)
webertj@13908
    68
done
webertj@13908
    69
declare sum_case_empty_empty [simp]
webertj@13908
    70
webertj@13908
    71
webertj@13908
    72
section "map_upd"
webertj@13908
    73
webertj@13908
    74
lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
webertj@13908
    75
apply (rule ext)
webertj@13908
    76
apply (simp (no_asm_simp))
webertj@13908
    77
done
webertj@13908
    78
webertj@13908
    79
lemma map_upd_nonempty: "t(k|->x) ~= empty"
webertj@13908
    80
apply safe
webertj@13908
    81
apply (drule_tac x = "k" in fun_cong)
webertj@13908
    82
apply (simp (no_asm_use))
webertj@13908
    83
done
webertj@13908
    84
declare map_upd_nonempty [simp]
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@13908
    96
section "sum_case and empty/map_upd"
webertj@13908
    97
webertj@13908
    98
lemma sum_case_map_upd_empty: "sum_case (m(k|->y)) empty =  (sum_case m empty)(Inl k|->y)"
webertj@13908
    99
apply (rule ext)
webertj@13908
   100
apply (simp (no_asm) split add: sum.split)
webertj@13908
   101
done
webertj@13908
   102
declare sum_case_map_upd_empty [simp]
webertj@13908
   103
webertj@13908
   104
lemma sum_case_empty_map_upd: "sum_case empty (m(k|->y)) =  (sum_case empty m)(Inr k|->y)"
webertj@13908
   105
apply (rule ext)
webertj@13908
   106
apply (simp (no_asm) split add: sum.split)
webertj@13908
   107
done
webertj@13908
   108
declare sum_case_empty_map_upd [simp]
webertj@13908
   109
webertj@13908
   110
lemma sum_case_map_upd_map_upd: "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
webertj@13908
   111
apply (rule ext)
webertj@13908
   112
apply (simp (no_asm) split add: sum.split)
webertj@13908
   113
done
webertj@13908
   114
declare sum_case_map_upd_map_upd [simp]
webertj@13908
   115
webertj@13908
   116
webertj@13908
   117
section "map_upds"
webertj@13908
   118
webertj@13908
   119
lemma map_upds_twist [rule_format (no_asm)]: "a ~: set as --> (!m bs. (m(a|->b)(as[|->]bs)) = (m(as[|->]bs)(a|->b)))"
webertj@13908
   120
apply (induct_tac "as")
webertj@13908
   121
apply  (auto simp del: fun_upd_apply)
webertj@13908
   122
apply (drule spec)+
webertj@13908
   123
apply (rotate_tac -1)
webertj@13908
   124
apply (erule subst)
webertj@13908
   125
apply (erule fun_upd_twist [THEN subst])
webertj@13908
   126
apply (rule refl)
webertj@13908
   127
done
webertj@13908
   128
declare map_upds_twist [simp]
webertj@13908
   129
webertj@13908
   130
webertj@13908
   131
section "chg_map"
webertj@13908
   132
webertj@13908
   133
lemma chg_map_new: "m a = None   ==> chg_map f a m = m"
webertj@13908
   134
apply (unfold chg_map_def)
webertj@13908
   135
apply auto
webertj@13908
   136
done
webertj@13908
   137
webertj@13908
   138
lemma chg_map_upd: "m a = Some b ==> chg_map f a m = m(a|->f b)"
webertj@13908
   139
apply (unfold chg_map_def)
webertj@13908
   140
apply auto
webertj@13908
   141
done
webertj@13908
   142
webertj@13908
   143
declare chg_map_new [simp] chg_map_upd [simp]
webertj@13908
   144
webertj@13908
   145
webertj@13908
   146
section "map_of"
webertj@13908
   147
webertj@13908
   148
lemma map_of_SomeD [rule_format (no_asm)]: "map_of xs k = Some y --> (k,y):set xs"
webertj@13908
   149
apply (induct_tac "xs")
webertj@13908
   150
apply  auto
webertj@13908
   151
done
webertj@13908
   152
webertj@13908
   153
lemma map_of_mapk_SomeI [rule_format (no_asm)]: "inj f ==> map_of t k = Some x -->  
webertj@13908
   154
   map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
webertj@13908
   155
apply (induct_tac "t")
webertj@13908
   156
apply  (auto simp add: inj_eq)
webertj@13908
   157
done
webertj@13908
   158
webertj@13908
   159
lemma weak_map_of_SomeI [rule_format (no_asm)]: "(k, x) : set l --> (? x. map_of l k = Some x)"
webertj@13908
   160
apply (induct_tac "l")
webertj@13908
   161
apply  auto
webertj@13908
   162
done
webertj@13908
   163
webertj@13908
   164
lemma map_of_filter_in: 
webertj@13908
   165
"[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z"
webertj@13908
   166
apply (rule mp)
webertj@13908
   167
prefer 2 apply (assumption)
webertj@13908
   168
apply (erule thin_rl)
webertj@13908
   169
apply (induct_tac "xs")
webertj@13908
   170
apply  auto
webertj@13908
   171
done
webertj@13908
   172
webertj@13908
   173
lemma finite_range_map_of: "finite (range (map_of l))"
webertj@13908
   174
apply (induct_tac "l")
webertj@13908
   175
apply  (simp_all (no_asm) add: image_constant)
webertj@13908
   176
apply (rule finite_subset)
webertj@13908
   177
prefer 2 apply (assumption)
webertj@13908
   178
apply auto
webertj@13908
   179
done
webertj@13908
   180
webertj@13908
   181
lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
webertj@13908
   182
apply (induct_tac "xs")
webertj@13908
   183
apply auto
webertj@13908
   184
done
webertj@13908
   185
webertj@13908
   186
webertj@13908
   187
section "option_map related"
webertj@13908
   188
webertj@13908
   189
lemma option_map_o_empty: "option_map f o empty = empty"
webertj@13908
   190
apply (rule ext)
webertj@13908
   191
apply (simp (no_asm))
webertj@13908
   192
done
webertj@13908
   193
webertj@13908
   194
lemma option_map_o_map_upd: "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
webertj@13908
   195
apply (rule ext)
webertj@13908
   196
apply (simp (no_asm))
webertj@13908
   197
done
webertj@13908
   198
webertj@13908
   199
declare option_map_o_empty [simp] option_map_o_map_upd [simp]
webertj@13908
   200
webertj@13908
   201
webertj@13908
   202
section "++"
webertj@13908
   203
webertj@13908
   204
lemma override_empty: "m ++ empty = m"
webertj@13908
   205
apply (unfold override_def)
webertj@13908
   206
apply (simp (no_asm))
webertj@13908
   207
done
webertj@13908
   208
declare override_empty [simp]
webertj@13908
   209
webertj@13908
   210
lemma empty_override: "empty ++ m = m"
webertj@13908
   211
apply (unfold override_def)
webertj@13908
   212
apply (rule ext)
webertj@13908
   213
apply (simp split add: option.split)
webertj@13908
   214
done
webertj@13908
   215
declare empty_override [simp]
webertj@13908
   216
webertj@13908
   217
lemma override_Some_iff [rule_format (no_asm)]: 
webertj@13908
   218
 "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
webertj@13908
   219
apply (unfold override_def)
webertj@13908
   220
apply (simp (no_asm) split add: option.split)
webertj@13908
   221
done
webertj@13908
   222
webertj@13908
   223
lemmas override_SomeD = override_Some_iff [THEN iffD1, standard]
webertj@13908
   224
declare override_SomeD [dest!]
webertj@13908
   225
webertj@13908
   226
lemma override_find_right: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
webertj@13908
   227
apply (subst override_Some_iff)
webertj@13908
   228
apply fast
webertj@13908
   229
done
webertj@13908
   230
declare override_find_right [simp]
webertj@13908
   231
webertj@13908
   232
lemma override_None: "((m ++ n) k = None) = (n k = None & m k = None)"
webertj@13908
   233
apply (unfold override_def)
webertj@13908
   234
apply (simp (no_asm) split add: option.split)
webertj@13908
   235
done
webertj@13908
   236
declare override_None [iff]
webertj@13908
   237
webertj@13908
   238
lemma override_upd: "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
declare override_upd [simp]
webertj@13908
   244
webertj@13908
   245
lemma map_of_override: "map_of ys ++ map_of xs = map_of (xs@ys)"
webertj@13908
   246
apply (unfold override_def)
webertj@13908
   247
apply (rule sym)
webertj@13908
   248
apply (induct_tac "xs")
webertj@13908
   249
apply (simp (no_asm))
webertj@13908
   250
apply (rule ext)
webertj@13908
   251
apply (simp (no_asm_simp) split add: option.split)
webertj@13908
   252
done
webertj@13908
   253
declare map_of_override [simp]
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@13908
   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
webertj@13908
   276
lemma domIff: "(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 [iff]
webertj@13908
   281
declare domIff [simp del]
webertj@13908
   282
webertj@13908
   283
lemma dom_empty: "dom empty = {}"
webertj@13908
   284
apply (unfold dom_def)
webertj@13908
   285
apply (simp (no_asm))
webertj@13908
   286
done
webertj@13908
   287
declare dom_empty [simp]
webertj@13908
   288
webertj@13908
   289
lemma dom_map_upd: "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
webertj@13908
   294
declare dom_map_upd [simp]
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
webertj@13908
   302
lemma dom_override: "dom(m++n) = dom n Un dom m"
webertj@13908
   303
apply (unfold dom_def)
webertj@13908
   304
apply auto
webertj@13908
   305
done
webertj@13908
   306
declare dom_override [simp]
webertj@13908
   307
webertj@13908
   308
section "ran"
webertj@13908
   309
webertj@13908
   310
lemma ran_empty: "ran empty = {}"
webertj@13908
   311
apply (unfold ran_def)
webertj@13908
   312
apply (simp (no_asm))
webertj@13908
   313
done
webertj@13908
   314
declare ran_empty [simp]
webertj@13908
   315
webertj@13908
   316
lemma ran_empty': "ran (%u. None) = {}"
webertj@13908
   317
apply (unfold ran_def)
webertj@13908
   318
apply auto
webertj@13908
   319
done
webertj@13908
   320
declare ran_empty' [simp]
webertj@13908
   321
webertj@13908
   322
lemma ran_map_upd: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
webertj@13908
   323
apply (unfold ran_def)
webertj@13908
   324
apply auto
webertj@13908
   325
apply (subgoal_tac "~ (aa = a) ")
webertj@13908
   326
apply auto
webertj@13908
   327
done
webertj@13908
   328
declare ran_map_upd [simp]
webertj@13908
   329
nipkow@3981
   330
end