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