src/HOL/Library/Nested_Environment.thy
author nipkow
Wed Aug 18 11:09:40 2004 +0200 (2004-08-18)
changeset 15140 322485b816ac
parent 15131 c69542757a4d
child 18153 a084aa91f701
permissions -rw-r--r--
import -> imports
wenzelm@10943
     1
(*  Title:      HOL/Library/Nested_Environment.thy
wenzelm@10943
     2
    ID:         $Id$
wenzelm@10943
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@10943
     4
*)
wenzelm@10943
     5
wenzelm@14706
     6
header {* Nested environments *}
wenzelm@10943
     7
nipkow@15131
     8
theory Nested_Environment
nipkow@15140
     9
imports Main
nipkow@15131
    10
begin
wenzelm@10943
    11
wenzelm@10943
    12
text {*
wenzelm@10943
    13
  Consider a partial function @{term [source] "e :: 'a => 'b option"};
wenzelm@10943
    14
  this may be understood as an \emph{environment} mapping indexes
wenzelm@10943
    15
  @{typ 'a} to optional entry values @{typ 'b} (cf.\ the basic theory
wenzelm@10948
    16
  @{text Map} of Isabelle/HOL).  This basic idea is easily generalized
wenzelm@10948
    17
  to that of a \emph{nested environment}, where entries may be either
wenzelm@10948
    18
  basic values or again proper environments.  Then each entry is
wenzelm@10948
    19
  accessed by a \emph{path}, i.e.\ a list of indexes leading to its
wenzelm@10948
    20
  position within the structure.
wenzelm@10943
    21
*}
wenzelm@10943
    22
wenzelm@10943
    23
datatype ('a, 'b, 'c) env =
wenzelm@10943
    24
    Val 'a
wenzelm@10943
    25
  | Env 'b  "'c => ('a, 'b, 'c) env option"
wenzelm@10943
    26
wenzelm@10943
    27
text {*
wenzelm@10943
    28
  \medskip In the type @{typ "('a, 'b, 'c) env"} the parameter @{typ
wenzelm@10943
    29
  'a} refers to basic values (occurring in terminal positions), type
wenzelm@10943
    30
  @{typ 'b} to values associated with proper (inner) environments, and
wenzelm@10943
    31
  type @{typ 'c} with the index type for branching.  Note that there
wenzelm@10943
    32
  is no restriction on any of these types.  In particular, arbitrary
wenzelm@10943
    33
  branching may yield rather large (transfinite) tree structures.
wenzelm@10943
    34
*}
wenzelm@10943
    35
wenzelm@10943
    36
wenzelm@10943
    37
subsection {* The lookup operation *}
wenzelm@10943
    38
wenzelm@10943
    39
text {*
wenzelm@10943
    40
  Lookup in nested environments works by following a given path of
wenzelm@10943
    41
  index elements, leading to an optional result (a terminal value or
wenzelm@10943
    42
  nested environment).  A \emph{defined position} within a nested
wenzelm@10943
    43
  environment is one where @{term lookup} at its path does not yield
wenzelm@10943
    44
  @{term None}.
wenzelm@10943
    45
*}
wenzelm@10943
    46
wenzelm@10943
    47
consts
wenzelm@10943
    48
  lookup :: "('a, 'b, 'c) env => 'c list => ('a, 'b, 'c) env option"
wenzelm@10943
    49
  lookup_option :: "('a, 'b, 'c) env option => 'c list => ('a, 'b, 'c) env option"
wenzelm@10943
    50
wenzelm@10943
    51
primrec (lookup)
wenzelm@10943
    52
  "lookup (Val a) xs = (if xs = [] then Some (Val a) else None)"
wenzelm@10943
    53
  "lookup (Env b es) xs =
wenzelm@10943
    54
    (case xs of
wenzelm@10943
    55
      [] => Some (Env b es)
wenzelm@10943
    56
    | y # ys => lookup_option (es y) ys)"
wenzelm@10943
    57
  "lookup_option None xs = None"
wenzelm@10943
    58
  "lookup_option (Some e) xs = lookup e xs"
wenzelm@10943
    59
wenzelm@10943
    60
hide const lookup_option
wenzelm@10943
    61
wenzelm@10943
    62
text {*
wenzelm@10943
    63
  \medskip The characteristic cases of @{term lookup} are expressed by
wenzelm@10943
    64
  the following equalities.
wenzelm@10943
    65
*}
wenzelm@10943
    66
wenzelm@10943
    67
theorem lookup_nil: "lookup e [] = Some e"
wenzelm@10943
    68
  by (cases e) simp_all
wenzelm@10943
    69
wenzelm@10943
    70
theorem lookup_val_cons: "lookup (Val a) (x # xs) = None"
wenzelm@10943
    71
  by simp
wenzelm@10943
    72
wenzelm@10943
    73
theorem lookup_env_cons:
wenzelm@10943
    74
  "lookup (Env b es) (x # xs) =
wenzelm@10943
    75
    (case es x of
wenzelm@10943
    76
      None => None
wenzelm@10943
    77
    | Some e => lookup e xs)"
wenzelm@10943
    78
  by (cases "es x") simp_all
wenzelm@10943
    79
wenzelm@10943
    80
lemmas lookup.simps [simp del]
wenzelm@10943
    81
  and lookup_simps [simp] = lookup_nil lookup_val_cons lookup_env_cons
wenzelm@10943
    82
wenzelm@10943
    83
theorem lookup_eq:
wenzelm@10943
    84
  "lookup env xs =
wenzelm@10943
    85
    (case xs of
wenzelm@10943
    86
      [] => Some env
wenzelm@10943
    87
    | x # xs =>
wenzelm@10943
    88
      (case env of
wenzelm@10943
    89
        Val a => None
wenzelm@10943
    90
      | Env b es =>
wenzelm@10943
    91
          (case es x of
wenzelm@10943
    92
            None => None
wenzelm@10943
    93
          | Some e => lookup e xs)))"
wenzelm@10943
    94
  by (simp split: list.split env.split)
wenzelm@10943
    95
wenzelm@10943
    96
text {*
wenzelm@10943
    97
  \medskip Displaced @{term lookup} operations, relative to a certain
wenzelm@10943
    98
  base path prefix, may be reduced as follows.  There are two cases,
wenzelm@10943
    99
  depending whether the environment actually extends far enough to
wenzelm@10943
   100
  follow the base path.
wenzelm@10943
   101
*}
wenzelm@10943
   102
wenzelm@10943
   103
theorem lookup_append_none:
wenzelm@10943
   104
  "!!env. lookup env xs = None ==> lookup env (xs @ ys) = None"
wenzelm@10943
   105
  (is "PROP ?P xs")
wenzelm@10943
   106
proof (induct xs)
wenzelm@10943
   107
  fix env :: "('a, 'b, 'c) env"
wenzelm@10943
   108
  {
wenzelm@10943
   109
    assume "lookup env [] = None"
wenzelm@10943
   110
    hence False by simp
wenzelm@10943
   111
    thus "lookup env ([] @ ys) = None" ..
wenzelm@10943
   112
  next
wenzelm@10943
   113
    fix x xs
wenzelm@10943
   114
    assume hyp: "PROP ?P xs"
wenzelm@10943
   115
    assume asm: "lookup env (x # xs) = None"
wenzelm@10943
   116
    show "lookup env ((x # xs) @ ys) = None"
wenzelm@10943
   117
    proof (cases env)
wenzelm@10943
   118
      case Val
wenzelm@10943
   119
      thus ?thesis by simp
wenzelm@10943
   120
    next
wenzelm@10943
   121
      fix b es assume env: "env = Env b es"
wenzelm@10943
   122
      show ?thesis
wenzelm@10943
   123
      proof (cases "es x")
wenzelm@10943
   124
        assume "es x = None"
wenzelm@10943
   125
        with env show ?thesis by simp
wenzelm@10943
   126
      next
wenzelm@10943
   127
        fix e assume es: "es x = Some e"
wenzelm@10943
   128
        show ?thesis
wenzelm@10943
   129
        proof (cases "lookup e xs")
wenzelm@10943
   130
          case None
wenzelm@10943
   131
          hence "lookup e (xs @ ys) = None" by (rule hyp)
wenzelm@10943
   132
          with env es show ?thesis by simp
wenzelm@10943
   133
        next
wenzelm@10943
   134
          case Some
wenzelm@10943
   135
          with asm env es have False by simp
wenzelm@10943
   136
          thus ?thesis ..
wenzelm@10943
   137
        qed
wenzelm@10943
   138
      qed
wenzelm@10943
   139
    qed
wenzelm@10943
   140
  }
wenzelm@10943
   141
qed
wenzelm@10943
   142
wenzelm@10943
   143
theorem lookup_append_some:
wenzelm@10943
   144
  "!!env e. lookup env xs = Some e ==> lookup env (xs @ ys) = lookup e ys"
wenzelm@10943
   145
  (is "PROP ?P xs")
wenzelm@10943
   146
proof (induct xs)
wenzelm@10943
   147
  fix env e :: "('a, 'b, 'c) env"
wenzelm@10943
   148
  {
wenzelm@10943
   149
    assume "lookup env [] = Some e"
wenzelm@10943
   150
    hence "env = e" by simp
wenzelm@10943
   151
    thus "lookup env ([] @ ys) = lookup e ys" by simp
wenzelm@10943
   152
  next
wenzelm@10943
   153
    fix x xs
wenzelm@10943
   154
    assume hyp: "PROP ?P xs"
wenzelm@10943
   155
    assume asm: "lookup env (x # xs) = Some e"
wenzelm@10943
   156
    show "lookup env ((x # xs) @ ys) = lookup e ys"
wenzelm@10943
   157
    proof (cases env)
wenzelm@10943
   158
      fix a assume "env = Val a"
wenzelm@10943
   159
      with asm have False by simp
wenzelm@10943
   160
      thus ?thesis ..
wenzelm@10943
   161
    next
wenzelm@10943
   162
      fix b es assume env: "env = Env b es"
wenzelm@10943
   163
      show ?thesis
wenzelm@10943
   164
      proof (cases "es x")
wenzelm@10943
   165
        assume "es x = None"
wenzelm@10943
   166
        with asm env have False by simp
wenzelm@10943
   167
        thus ?thesis ..
wenzelm@10943
   168
      next
wenzelm@10943
   169
        fix e' assume es: "es x = Some e'"
wenzelm@10943
   170
        show ?thesis
wenzelm@10943
   171
        proof (cases "lookup e' xs")
wenzelm@10943
   172
          case None
wenzelm@10943
   173
          with asm env es have False by simp
wenzelm@10943
   174
          thus ?thesis ..
wenzelm@10943
   175
        next
wenzelm@10943
   176
          case Some
wenzelm@10943
   177
          with asm env es have "lookup e' xs = Some e"
wenzelm@10943
   178
            by simp
wenzelm@10943
   179
          hence "lookup e' (xs @ ys) = lookup e ys" by (rule hyp)
wenzelm@10943
   180
          with env es show ?thesis by simp
wenzelm@10943
   181
        qed
wenzelm@10943
   182
      qed
wenzelm@10943
   183
    qed
wenzelm@10943
   184
  }
wenzelm@10943
   185
qed
wenzelm@10943
   186
wenzelm@10943
   187
text {*
wenzelm@10943
   188
  \medskip Successful @{term lookup} deeper down an environment
wenzelm@10943
   189
  structure means we are able to peek further up as well.  Note that
wenzelm@10943
   190
  this is basically just the contrapositive statement of @{thm
wenzelm@10943
   191
  [source] lookup_append_none} above.
wenzelm@10943
   192
*}
wenzelm@10943
   193
wenzelm@10943
   194
theorem lookup_some_append:
wenzelm@10943
   195
  "lookup env (xs @ ys) = Some e ==> \<exists>e. lookup env xs = Some e"
wenzelm@10943
   196
proof -
wenzelm@10943
   197
  assume "lookup env (xs @ ys) = Some e"
wenzelm@10943
   198
  hence "lookup env (xs @ ys) \<noteq> None" by simp
wenzelm@10943
   199
  hence "lookup env xs \<noteq> None"
wenzelm@10943
   200
    by (rule contrapos_nn) (simp only: lookup_append_none)
wenzelm@10943
   201
  thus ?thesis by simp
wenzelm@10943
   202
qed
wenzelm@10943
   203
wenzelm@10943
   204
text {*
wenzelm@10943
   205
  The subsequent statement describes in more detail how a successful
wenzelm@10943
   206
  @{term lookup} with a non-empty path results in a certain situation
wenzelm@10943
   207
  at any upper position.
wenzelm@10943
   208
*}
wenzelm@10943
   209
wenzelm@10943
   210
theorem lookup_some_upper: "!!env e.
wenzelm@10943
   211
  lookup env (xs @ y # ys) = Some e ==>
wenzelm@10943
   212
    \<exists>b' es' env'.
wenzelm@10943
   213
      lookup env xs = Some (Env b' es') \<and>
wenzelm@10943
   214
      es' y = Some env' \<and>
wenzelm@10943
   215
      lookup env' ys = Some e"
wenzelm@10943
   216
  (is "PROP ?P xs" is "!!env e. ?A env e xs ==> ?C env e xs")
wenzelm@11809
   217
proof (induct xs)
wenzelm@10948
   218
  fix env e let ?A = "?A env e" and ?C = "?C env e"
wenzelm@10943
   219
  {
wenzelm@10943
   220
    assume "?A []"
wenzelm@10943
   221
    hence "lookup env (y # ys) = Some e" by simp
wenzelm@10943
   222
    then obtain b' es' env' where
wenzelm@10948
   223
        env: "env = Env b' es'" and
wenzelm@10948
   224
        es': "es' y = Some env'" and
wenzelm@10948
   225
        look': "lookup env' ys = Some e"
wenzelm@10943
   226
      by (auto simp add: lookup_eq split: option.splits env.splits)
wenzelm@10943
   227
    from env have "lookup env [] = Some (Env b' es')" by simp
wenzelm@10943
   228
    with es' look' show "?C []" by blast
wenzelm@10943
   229
  next
wenzelm@10943
   230
    fix x xs
wenzelm@10943
   231
    assume hyp: "PROP ?P xs"
wenzelm@10943
   232
    assume "?A (x # xs)"
wenzelm@10943
   233
    then obtain b' es' env' where
wenzelm@10948
   234
        env: "env = Env b' es'" and
wenzelm@10948
   235
        es': "es' x = Some env'" and
wenzelm@10948
   236
        look': "lookup env' (xs @ y # ys) = Some e"
wenzelm@10943
   237
      by (auto simp add: lookup_eq split: option.splits env.splits)
wenzelm@10943
   238
    from hyp [OF look'] obtain b'' es'' env'' where
wenzelm@10948
   239
        upper': "lookup env' xs = Some (Env b'' es'')" and
wenzelm@10948
   240
        es'': "es'' y = Some env''" and
wenzelm@10948
   241
        look'': "lookup env'' ys = Some e"
wenzelm@10943
   242
      by blast
wenzelm@10943
   243
    from env es' upper' have "lookup env (x # xs) = Some (Env b'' es'')"
wenzelm@10943
   244
      by simp
wenzelm@10943
   245
    with es'' look'' show "?C (x # xs)" by blast
wenzelm@10943
   246
  }
wenzelm@10943
   247
qed
wenzelm@10943
   248
wenzelm@10943
   249
wenzelm@10943
   250
subsection {* The update operation *}
wenzelm@10943
   251
wenzelm@10943
   252
text {*
wenzelm@10943
   253
  Update at a certain position in a nested environment may either
wenzelm@10943
   254
  delete an existing entry, or overwrite an existing one.  Note that
wenzelm@10943
   255
  update at undefined positions is simple absorbed, i.e.\ the
wenzelm@10943
   256
  environment is left unchanged.
wenzelm@10943
   257
*}
wenzelm@10943
   258
wenzelm@10943
   259
consts
wenzelm@10943
   260
  update :: "'c list => ('a, 'b, 'c) env option
wenzelm@10943
   261
    => ('a, 'b, 'c) env => ('a, 'b, 'c) env"
wenzelm@10943
   262
  update_option :: "'c list => ('a, 'b, 'c) env option
wenzelm@10943
   263
    => ('a, 'b, 'c) env option => ('a, 'b, 'c) env option"
wenzelm@10943
   264
wenzelm@10943
   265
primrec (update)
wenzelm@10943
   266
  "update xs opt (Val a) =
wenzelm@10943
   267
    (if xs = [] then (case opt of None => Val a | Some e => e)
wenzelm@10943
   268
    else Val a)"
wenzelm@10943
   269
  "update xs opt (Env b es) =
wenzelm@10943
   270
    (case xs of
wenzelm@10943
   271
      [] => (case opt of None => Env b es | Some e => e)
wenzelm@10943
   272
    | y # ys => Env b (es (y := update_option ys opt (es y))))"
wenzelm@10943
   273
  "update_option xs opt None =
wenzelm@10943
   274
    (if xs = [] then opt else None)"
wenzelm@10943
   275
  "update_option xs opt (Some e) =
wenzelm@10943
   276
    (if xs = [] then opt else Some (update xs opt e))"
wenzelm@10943
   277
wenzelm@10943
   278
hide const update_option
wenzelm@10943
   279
wenzelm@10943
   280
text {*
wenzelm@10943
   281
  \medskip The characteristic cases of @{term update} are expressed by
wenzelm@10943
   282
  the following equalities.
wenzelm@10943
   283
*}
wenzelm@10943
   284
wenzelm@10943
   285
theorem update_nil_none: "update [] None env = env"
wenzelm@10943
   286
  by (cases env) simp_all
wenzelm@10943
   287
wenzelm@10943
   288
theorem update_nil_some: "update [] (Some e) env = e"
wenzelm@10943
   289
  by (cases env) simp_all
wenzelm@10943
   290
wenzelm@10943
   291
theorem update_cons_val: "update (x # xs) opt (Val a) = Val a"
wenzelm@10943
   292
  by simp
wenzelm@10943
   293
wenzelm@10943
   294
theorem update_cons_nil_env:
wenzelm@10943
   295
    "update [x] opt (Env b es) = Env b (es (x := opt))"
wenzelm@10943
   296
  by (cases "es x") simp_all
wenzelm@10943
   297
wenzelm@10943
   298
theorem update_cons_cons_env:
wenzelm@10943
   299
  "update (x # y # ys) opt (Env b es) =
wenzelm@10943
   300
    Env b (es (x :=
wenzelm@10943
   301
      (case es x of
wenzelm@10943
   302
        None => None
wenzelm@10943
   303
      | Some e => Some (update (y # ys) opt e))))"
wenzelm@10943
   304
  by (cases "es x") simp_all
wenzelm@10943
   305
wenzelm@10943
   306
lemmas update.simps [simp del]
wenzelm@10943
   307
  and update_simps [simp] = update_nil_none update_nil_some
wenzelm@10943
   308
    update_cons_val update_cons_nil_env update_cons_cons_env
wenzelm@10943
   309
wenzelm@10943
   310
lemma update_eq:
wenzelm@10943
   311
  "update xs opt env =
wenzelm@10943
   312
    (case xs of
wenzelm@10943
   313
      [] =>
wenzelm@10943
   314
        (case opt of
wenzelm@10943
   315
          None => env
wenzelm@10943
   316
        | Some e => e)
wenzelm@10943
   317
    | x # xs =>
wenzelm@10943
   318
        (case env of
wenzelm@10943
   319
          Val a => Val a
wenzelm@10943
   320
        | Env b es =>
wenzelm@10943
   321
            (case xs of
wenzelm@10943
   322
              [] => Env b (es (x := opt))
wenzelm@10943
   323
            | y # ys =>
wenzelm@10943
   324
                Env b (es (x :=
wenzelm@10943
   325
                  (case es x of
wenzelm@10943
   326
                    None => None
wenzelm@10943
   327
                  | Some e => Some (update (y # ys) opt e)))))))"
wenzelm@10943
   328
  by (simp split: list.split env.split option.split)
wenzelm@10943
   329
wenzelm@10943
   330
text {*
wenzelm@10943
   331
  \medskip The most basic correspondence of @{term lookup} and @{term
wenzelm@10943
   332
  update} states that after @{term update} at a defined position,
wenzelm@10943
   333
  subsequent @{term lookup} operations would yield the new value.
wenzelm@10943
   334
*}
wenzelm@10943
   335
wenzelm@10943
   336
theorem lookup_update_some:
wenzelm@10943
   337
  "!!env e. lookup env xs = Some e ==>
wenzelm@10943
   338
    lookup (update xs (Some env') env) xs = Some env'"
wenzelm@10943
   339
  (is "PROP ?P xs")
wenzelm@10943
   340
proof (induct xs)
wenzelm@10943
   341
  fix env e :: "('a, 'b, 'c) env"
wenzelm@10943
   342
  {
wenzelm@10943
   343
    assume "lookup env [] = Some e"
wenzelm@10943
   344
    hence "env = e" by simp
wenzelm@10943
   345
    thus "lookup (update [] (Some env') env) [] = Some env'"
wenzelm@10943
   346
      by simp
wenzelm@10943
   347
  next
wenzelm@10943
   348
    fix x xs
wenzelm@10943
   349
    assume hyp: "PROP ?P xs"
wenzelm@10943
   350
    assume asm: "lookup env (x # xs) = Some e"
wenzelm@10943
   351
    show "lookup (update (x # xs) (Some env') env) (x # xs) = Some env'"
wenzelm@10943
   352
    proof (cases env)
wenzelm@10943
   353
      fix a assume "env = Val a"
wenzelm@10943
   354
      with asm have False by simp
wenzelm@10943
   355
      thus ?thesis ..
wenzelm@10943
   356
    next
wenzelm@10943
   357
      fix b es assume env: "env = Env b es"
wenzelm@10943
   358
      show ?thesis
wenzelm@10943
   359
      proof (cases "es x")
wenzelm@10943
   360
        assume "es x = None"
wenzelm@10943
   361
        with asm env have False by simp
wenzelm@10943
   362
        thus ?thesis ..
wenzelm@10943
   363
      next
wenzelm@10943
   364
        fix e' assume es: "es x = Some e'"
wenzelm@10943
   365
        show ?thesis
wenzelm@10943
   366
        proof (cases xs)
wenzelm@10943
   367
          assume "xs = []"
wenzelm@10943
   368
          with env show ?thesis by simp
wenzelm@10943
   369
        next
wenzelm@10943
   370
          fix x' xs' assume xs: "xs = x' # xs'"
wenzelm@10943
   371
          from asm env es have "lookup e' xs = Some e" by simp
wenzelm@10943
   372
          hence "lookup (update xs (Some env') e') xs = Some env'" by (rule hyp)
wenzelm@10943
   373
          with env es xs show ?thesis by simp
wenzelm@10943
   374
        qed
wenzelm@10943
   375
      qed
wenzelm@10943
   376
    qed
wenzelm@10943
   377
  }
wenzelm@10943
   378
qed
wenzelm@10943
   379
wenzelm@10943
   380
text {*
wenzelm@10943
   381
  \medskip The properties of displaced @{term update} operations are
wenzelm@10943
   382
  analogous to those of @{term lookup} above.  There are two cases:
wenzelm@10943
   383
  below an undefined position @{term update} is absorbed altogether,
wenzelm@10943
   384
  and below a defined positions @{term update} affects subsequent
wenzelm@10943
   385
  @{term lookup} operations in the obvious way.
wenzelm@10943
   386
*}
wenzelm@10943
   387
wenzelm@10943
   388
theorem update_append_none:
wenzelm@10943
   389
  "!!env. lookup env xs = None ==> update (xs @ y # ys) opt env = env"
wenzelm@10943
   390
  (is "PROP ?P xs")
wenzelm@10943
   391
proof (induct xs)
wenzelm@10943
   392
  fix env :: "('a, 'b, 'c) env"
wenzelm@10943
   393
  {
wenzelm@10943
   394
    assume "lookup env [] = None"
wenzelm@10943
   395
    hence False by simp
wenzelm@10943
   396
    thus "update ([] @ y # ys) opt env = env" ..
wenzelm@10943
   397
  next
wenzelm@10943
   398
    fix x xs
wenzelm@10943
   399
    assume hyp: "PROP ?P xs"
wenzelm@10943
   400
    assume asm: "lookup env (x # xs) = None"
wenzelm@10943
   401
    show "update ((x # xs) @ y # ys) opt env = env"
wenzelm@10943
   402
    proof (cases env)
wenzelm@10943
   403
      fix a assume "env = Val a"
wenzelm@10943
   404
      thus ?thesis by simp
wenzelm@10943
   405
    next
wenzelm@10943
   406
      fix b es assume env: "env = Env b es"
wenzelm@10943
   407
      show ?thesis
wenzelm@10943
   408
      proof (cases "es x")
wenzelm@10943
   409
        assume es: "es x = None"
wenzelm@10943
   410
        show ?thesis
wenzelm@10943
   411
          by (cases xs) (simp_all add: es env fun_upd_idem_iff)
wenzelm@10943
   412
      next
wenzelm@10943
   413
        fix e assume es: "es x = Some e"
wenzelm@10943
   414
        show ?thesis
wenzelm@10943
   415
        proof (cases xs)
wenzelm@10943
   416
          assume "xs = []"
wenzelm@10943
   417
          with asm env es have False by simp
wenzelm@10943
   418
          thus ?thesis ..
wenzelm@10943
   419
        next
wenzelm@10943
   420
          fix x' xs' assume xs: "xs = x' # xs'"
wenzelm@10943
   421
          from asm env es have "lookup e xs = None" by simp
wenzelm@10943
   422
          hence "update (xs @ y # ys) opt e = e" by (rule hyp)
wenzelm@10943
   423
          with env es xs show "update ((x # xs) @ y # ys) opt env = env"
wenzelm@10943
   424
            by (simp add: fun_upd_idem_iff)
wenzelm@10943
   425
        qed
wenzelm@10943
   426
      qed
wenzelm@10943
   427
    qed
wenzelm@10943
   428
  }
wenzelm@10943
   429
qed
wenzelm@10943
   430
wenzelm@10943
   431
theorem update_append_some:
wenzelm@10943
   432
  "!!env e. lookup env xs = Some e ==>
wenzelm@10943
   433
    lookup (update (xs @ y # ys) opt env) xs = Some (update (y # ys) opt e)"
wenzelm@10943
   434
  (is "PROP ?P xs")
wenzelm@10943
   435
proof (induct xs)
wenzelm@10943
   436
  fix env e :: "('a, 'b, 'c) env"
wenzelm@10943
   437
  {
wenzelm@10943
   438
    assume "lookup env [] = Some e"
wenzelm@10943
   439
    hence "env = e" by simp
wenzelm@10943
   440
    thus "lookup (update ([] @ y # ys) opt env) [] = Some (update (y # ys) opt e)"
wenzelm@10943
   441
      by simp
wenzelm@10943
   442
  next
wenzelm@10943
   443
    fix x xs
wenzelm@10943
   444
    assume hyp: "PROP ?P xs"
wenzelm@10943
   445
    assume asm: "lookup env (x # xs) = Some e"
wenzelm@10943
   446
    show "lookup (update ((x # xs) @ y # ys) opt env) (x # xs)
wenzelm@10943
   447
      = Some (update (y # ys) opt e)"
wenzelm@10943
   448
    proof (cases env)
wenzelm@10943
   449
      fix a assume "env = Val a"
wenzelm@10943
   450
      with asm have False by simp
wenzelm@10943
   451
      thus ?thesis ..
wenzelm@10943
   452
    next
wenzelm@10943
   453
      fix b es assume env: "env = Env b es"
wenzelm@10943
   454
      show ?thesis
wenzelm@10943
   455
      proof (cases "es x")
wenzelm@10943
   456
        assume "es x = None"
wenzelm@10943
   457
        with asm env have False by simp
wenzelm@10943
   458
        thus ?thesis ..
wenzelm@10943
   459
      next
wenzelm@10943
   460
        fix e' assume es: "es x = Some e'"
wenzelm@10943
   461
        show ?thesis
wenzelm@10943
   462
        proof (cases xs)
wenzelm@10943
   463
          assume xs: "xs = []"
wenzelm@10943
   464
          from asm env es xs have "e = e'" by simp
wenzelm@10943
   465
          with env es xs show ?thesis by simp
wenzelm@10943
   466
        next
wenzelm@10943
   467
          fix x' xs' assume xs: "xs = x' # xs'"
wenzelm@10943
   468
          from asm env es have "lookup e' xs = Some e" by simp
wenzelm@10943
   469
          hence "lookup (update (xs @ y # ys) opt e') xs =
wenzelm@10943
   470
            Some (update (y # ys) opt e)" by (rule hyp)
wenzelm@10943
   471
          with env es xs show ?thesis by simp
wenzelm@10943
   472
        qed
wenzelm@10943
   473
      qed
wenzelm@10943
   474
    qed
wenzelm@10943
   475
  }
wenzelm@10943
   476
qed
wenzelm@10943
   477
wenzelm@10943
   478
text {*
wenzelm@10943
   479
  \medskip Apparently, @{term update} does not affect the result of
wenzelm@10943
   480
  subsequent @{term lookup} operations at independent positions, i.e.\
wenzelm@10943
   481
  in case that the paths for @{term update} and @{term lookup} fork at
wenzelm@10943
   482
  a certain point.
wenzelm@10943
   483
*}
wenzelm@10943
   484
wenzelm@10943
   485
theorem lookup_update_other:
wenzelm@10943
   486
  "!!env. y \<noteq> (z::'c) ==> lookup (update (xs @ z # zs) opt env) (xs @ y # ys) =
wenzelm@10943
   487
    lookup env (xs @ y # ys)"
wenzelm@10943
   488
  (is "PROP ?P xs")
wenzelm@10943
   489
proof (induct xs)
wenzelm@10943
   490
  fix env :: "('a, 'b, 'c) env"
wenzelm@10943
   491
  assume neq: "y \<noteq> z"
wenzelm@10943
   492
  {
wenzelm@10943
   493
    show "lookup (update ([] @ z # zs) opt env) ([] @ y # ys) =
wenzelm@10943
   494
      lookup env ([] @ y # ys)"
wenzelm@10943
   495
    proof (cases env)
wenzelm@10943
   496
      case Val
wenzelm@10943
   497
      thus ?thesis by simp
wenzelm@10943
   498
    next
wenzelm@10943
   499
      case Env
wenzelm@10943
   500
      show ?thesis
wenzelm@10943
   501
      proof (cases zs)
wenzelm@10943
   502
        case Nil
wenzelm@10943
   503
        with neq Env show ?thesis by simp
wenzelm@10943
   504
      next
wenzelm@10943
   505
        case Cons
wenzelm@10943
   506
        with neq Env show ?thesis by simp
wenzelm@10943
   507
      qed
wenzelm@10943
   508
    qed
wenzelm@10943
   509
  next
wenzelm@10943
   510
    fix x xs
wenzelm@10943
   511
    assume hyp: "PROP ?P xs"
wenzelm@10943
   512
    show "lookup (update ((x # xs) @ z # zs) opt env) ((x # xs) @ y # ys) =
wenzelm@10943
   513
      lookup env ((x # xs) @ y # ys)"
wenzelm@10943
   514
    proof (cases env)
wenzelm@10943
   515
      case Val
wenzelm@10943
   516
      thus ?thesis by simp
wenzelm@10943
   517
    next
wenzelm@10943
   518
      fix y es assume env: "env = Env y es"
wenzelm@10943
   519
      show ?thesis
wenzelm@10943
   520
      proof (cases xs)
wenzelm@10943
   521
        assume xs: "xs = []"
wenzelm@10943
   522
        show ?thesis
wenzelm@10943
   523
        proof (cases "es x")
wenzelm@10943
   524
          case None
wenzelm@10943
   525
          with env xs show ?thesis by simp
wenzelm@10943
   526
        next
wenzelm@10943
   527
          case Some
wenzelm@10943
   528
          with hyp env xs and neq show ?thesis by simp
wenzelm@10943
   529
        qed
wenzelm@10943
   530
      next
wenzelm@10943
   531
        fix x' xs' assume xs: "xs = x' # xs'"
wenzelm@10943
   532
        show ?thesis
wenzelm@10943
   533
        proof (cases "es x")
wenzelm@10943
   534
          case None
wenzelm@10943
   535
          with env xs show ?thesis by simp
wenzelm@10943
   536
        next
wenzelm@10943
   537
          case Some
wenzelm@10943
   538
          with hyp env xs neq show ?thesis by simp
wenzelm@10943
   539
        qed
wenzelm@10943
   540
      qed
wenzelm@10943
   541
    qed
wenzelm@10943
   542
  }
wenzelm@10943
   543
qed
wenzelm@10943
   544
wenzelm@10943
   545
end