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