src/HOL/IMP/Natural.thy
author haftmann
Mon Jan 30 08:20:56 2006 +0100 (2006-01-30)
changeset 18851 9502ce541f01
parent 18372 2bffdf62fe7f
child 19796 d86e7b1fc472
permissions -rw-r--r--
adaptions to codegen_package
kleing@12431
     1
(*  Title:        HOL/IMP/Natural.thy
kleing@12431
     2
    ID:           $Id$
kleing@12431
     3
    Author:       Tobias Nipkow & Robert Sandner, TUM
kleing@12431
     4
    Isar Version: Gerwin Klein, 2001
kleing@12431
     5
    Copyright     1996 TUM
nipkow@1700
     6
*)
nipkow@1700
     7
kleing@12431
     8
header "Natural Semantics of Commands"
kleing@12431
     9
haftmann@16417
    10
theory Natural imports Com begin
kleing@12431
    11
kleing@12431
    12
subsection "Execution of commands"
nipkow@1700
    13
wenzelm@18372
    14
consts  evalc   :: "(com \<times> state \<times> state) set"
wenzelm@12546
    15
syntax "_evalc" :: "[com,state,state] \<Rightarrow> bool" ("<_,_>/ -c-> _" [0,0,60] 60)
nipkow@1700
    16
kleing@12431
    17
syntax (xsymbols)
wenzelm@12546
    18
  "_evalc" :: "[com,state,state] \<Rightarrow> bool" ("\<langle>_,_\<rangle>/ \<longrightarrow>\<^sub>c _" [0,0,60] 60)
nipkow@1700
    19
kleing@14565
    20
syntax (HTML output)
kleing@14565
    21
  "_evalc" :: "[com,state,state] \<Rightarrow> bool" ("\<langle>_,_\<rangle>/ \<longrightarrow>\<^sub>c _" [0,0,60] 60)
kleing@14565
    22
kleing@12431
    23
text {*
wenzelm@18372
    24
  We write @{text "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'"} for \emph{Statement @{text c}, started
kleing@12431
    25
  in state @{text s}, terminates in state @{text s'}}. Formally,
kleing@12431
    26
  @{text "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'"} is just another form of saying \emph{the tuple
kleing@12431
    27
  @{text "(c,s,s')"} is part of the relation @{text evalc}}:
kleing@12431
    28
*}
kleing@12431
    29
translations  "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'" == "(c,s,s') \<in> evalc"
nipkow@1700
    30
kleing@12431
    31
constdefs
kleing@12431
    32
  update :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a \<Rightarrow> 'b)" ("_/[_ ::= /_]" [900,0,0] 900)
kleing@12431
    33
  "update == fun_upd"
kleing@12431
    34
kleing@12431
    35
syntax (xsymbols)
kleing@12431
    36
  update :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a \<Rightarrow> 'b)" ("_/[_ \<mapsto> /_]" [900,0,0] 900)
kleing@12431
    37
kleing@12431
    38
text {*
kleing@12431
    39
  The big-step execution relation @{text evalc} is defined inductively:
kleing@12431
    40
*}
paulson@1789
    41
inductive evalc
wenzelm@18372
    42
  intros
kleing@12431
    43
  Skip:    "\<langle>\<SKIP>,s\<rangle> \<longrightarrow>\<^sub>c s"
kleing@12431
    44
  Assign:  "\<langle>x :== a,s\<rangle> \<longrightarrow>\<^sub>c s[x\<mapsto>a s]"
kleing@12431
    45
kleing@12431
    46
  Semi:    "\<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c s'' \<Longrightarrow> \<langle>c1,s''\<rangle> \<longrightarrow>\<^sub>c s' \<Longrightarrow> \<langle>c0; c1, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
    47
kleing@12431
    48
  IfTrue:  "b s \<Longrightarrow> \<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c s' \<Longrightarrow> \<langle>\<IF> b \<THEN> c0 \<ELSE> c1, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
    49
  IfFalse: "\<not>b s \<Longrightarrow> \<langle>c1,s\<rangle> \<longrightarrow>\<^sub>c s' \<Longrightarrow> \<langle>\<IF> b \<THEN> c0 \<ELSE> c1, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
    50
kleing@12431
    51
  WhileFalse: "\<not>b s \<Longrightarrow> \<langle>\<WHILE> b \<DO> c,s\<rangle> \<longrightarrow>\<^sub>c s"
wenzelm@18372
    52
  WhileTrue:  "b s \<Longrightarrow> \<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'' \<Longrightarrow> \<langle>\<WHILE> b \<DO> c, s''\<rangle> \<longrightarrow>\<^sub>c s'
kleing@12431
    53
               \<Longrightarrow> \<langle>\<WHILE> b \<DO> c, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
    54
kleing@12431
    55
lemmas evalc.intros [intro] -- "use those rules in automatic proofs"
kleing@12431
    56
kleing@12431
    57
text {*
kleing@12431
    58
The induction principle induced by this definition looks like this:
kleing@12431
    59
kleing@12431
    60
@{thm [display] evalc.induct [no_vars]}
kleing@12431
    61
kleing@12431
    62
wenzelm@18372
    63
(@{text "\<And>"} and @{text "\<Longrightarrow>"} are Isabelle's
kleing@12431
    64
  meta symbols for @{text "\<forall>"} and @{text "\<longrightarrow>"})
kleing@12431
    65
*}
kleing@12431
    66
kleing@12431
    67
kleing@12431
    68
text {*
kleing@12431
    69
  The rules of @{text evalc} are syntax directed, i.e.~for each
kleing@12431
    70
  syntactic category there is always only one rule applicable. That
kleing@12431
    71
  means we can use the rules in both directions. The proofs for this
kleing@12431
    72
  are all the same: one direction is trivial, the other one is shown
wenzelm@18372
    73
  by using the @{text evalc} rules backwards:
kleing@12431
    74
*}
wenzelm@18372
    75
lemma skip:
kleing@12431
    76
  "\<langle>\<SKIP>,s\<rangle> \<longrightarrow>\<^sub>c s' = (s' = s)"
kleing@12431
    77
  by (rule, erule evalc.elims) auto
kleing@12431
    78
wenzelm@18372
    79
lemma assign:
wenzelm@18372
    80
  "\<langle>x :== a,s\<rangle> \<longrightarrow>\<^sub>c s' = (s' = s[x\<mapsto>a s])"
kleing@12431
    81
  by (rule, erule evalc.elims) auto
kleing@12431
    82
wenzelm@18372
    83
lemma semi:
kleing@12431
    84
  "\<langle>c0; c1, s\<rangle> \<longrightarrow>\<^sub>c s' = (\<exists>s''. \<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c s'' \<and> \<langle>c1,s''\<rangle> \<longrightarrow>\<^sub>c s')"
kleing@12431
    85
  by (rule, erule evalc.elims) auto
nipkow@1700
    86
wenzelm@18372
    87
lemma ifTrue:
wenzelm@18372
    88
  "b s \<Longrightarrow> \<langle>\<IF> b \<THEN> c0 \<ELSE> c1, s\<rangle> \<longrightarrow>\<^sub>c s' = \<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
    89
  by (rule, erule evalc.elims) auto
kleing@12431
    90
wenzelm@18372
    91
lemma ifFalse:
kleing@12431
    92
  "\<not>b s \<Longrightarrow> \<langle>\<IF> b \<THEN> c0 \<ELSE> c1, s\<rangle> \<longrightarrow>\<^sub>c s' = \<langle>c1,s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
    93
  by (rule, erule evalc.elims) auto
kleing@12431
    94
wenzelm@18372
    95
lemma whileFalse:
kleing@12431
    96
  "\<not> b s \<Longrightarrow> \<langle>\<WHILE> b \<DO> c,s\<rangle> \<longrightarrow>\<^sub>c s' = (s' = s)"
wenzelm@18372
    97
  by (rule, erule evalc.elims) auto
kleing@12431
    98
wenzelm@18372
    99
lemma whileTrue:
wenzelm@18372
   100
  "b s \<Longrightarrow>
wenzelm@18372
   101
  \<langle>\<WHILE> b \<DO> c, s\<rangle> \<longrightarrow>\<^sub>c s' =
kleing@12431
   102
  (\<exists>s''. \<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'' \<and> \<langle>\<WHILE> b \<DO> c, s''\<rangle> \<longrightarrow>\<^sub>c s')"
kleing@12431
   103
  by (rule, erule evalc.elims) auto
kleing@12431
   104
kleing@12431
   105
text "Again, Isabelle may use these rules in automatic proofs:"
kleing@12431
   106
lemmas evalc_cases [simp] = skip assign ifTrue ifFalse whileFalse semi whileTrue
kleing@12431
   107
kleing@12431
   108
subsection "Equivalence of statements"
nipkow@1700
   109
kleing@12431
   110
text {*
kleing@12431
   111
  We call two statements @{text c} and @{text c'} equivalent wrt.~the
kleing@12431
   112
  big-step semantics when \emph{@{text c} started in @{text s} terminates
kleing@12431
   113
  in @{text s'} iff @{text c'} started in the same @{text s} also terminates
kleing@12431
   114
  in the same @{text s'}}. Formally:
wenzelm@18372
   115
*}
kleing@12431
   116
constdefs
kleing@12431
   117
  equiv_c :: "com \<Rightarrow> com \<Rightarrow> bool" ("_ \<sim> _")
kleing@12431
   118
  "c \<sim> c' \<equiv> \<forall>s s'. \<langle>c, s\<rangle> \<longrightarrow>\<^sub>c s' = \<langle>c', s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
   119
kleing@12431
   120
text {*
kleing@12431
   121
  Proof rules telling Isabelle to unfold the definition
kleing@12431
   122
  if there is something to be proved about equivalent
wenzelm@18372
   123
  statements: *}
kleing@12431
   124
lemma equivI [intro!]:
kleing@12431
   125
  "(\<And>s s'. \<langle>c, s\<rangle> \<longrightarrow>\<^sub>c s' = \<langle>c', s\<rangle> \<longrightarrow>\<^sub>c s') \<Longrightarrow> c \<sim> c'"
kleing@12431
   126
  by (unfold equiv_c_def) blast
kleing@12431
   127
kleing@12431
   128
lemma equivD1:
kleing@12431
   129
  "c \<sim> c' \<Longrightarrow> \<langle>c, s\<rangle> \<longrightarrow>\<^sub>c s' \<Longrightarrow> \<langle>c', s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
   130
  by (unfold equiv_c_def) blast
kleing@12431
   131
kleing@12431
   132
lemma equivD2:
kleing@12431
   133
  "c \<sim> c' \<Longrightarrow> \<langle>c', s\<rangle> \<longrightarrow>\<^sub>c s' \<Longrightarrow> \<langle>c, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
   134
  by (unfold equiv_c_def) blast
nipkow@1700
   135
kleing@12431
   136
text {*
kleing@12431
   137
  As an example, we show that loop unfolding is an equivalence
kleing@12431
   138
  transformation on programs:
kleing@12431
   139
*}
kleing@12431
   140
lemma unfold_while:
kleing@12431
   141
  "(\<WHILE> b \<DO> c) \<sim> (\<IF> b \<THEN> c; \<WHILE> b \<DO> c \<ELSE> \<SKIP>)" (is "?w \<sim> ?if")
kleing@12431
   142
proof -
kleing@12431
   143
  -- "to show the equivalence, we look at the derivation tree for"
wenzelm@18372
   144
  -- "each side and from that construct a derivation tree for the other side"
kleing@12431
   145
  { fix s s' assume w: "\<langle>?w, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
   146
    -- "as a first thing we note that, if @{text b} is @{text False} in state @{text s},"
kleing@12431
   147
    -- "then both statements do nothing:"
kleing@12431
   148
    hence "\<not>b s \<Longrightarrow> s = s'" by simp
kleing@12431
   149
    hence "\<not>b s \<Longrightarrow> \<langle>?if, s\<rangle> \<longrightarrow>\<^sub>c s'" by simp
kleing@12431
   150
    moreover
kleing@12431
   151
    -- "on the other hand, if @{text b} is @{text True} in state @{text s},"
kleing@12431
   152
    -- {* then only the @{text WhileTrue} rule can have been used to derive @{text "\<langle>?w, s\<rangle> \<longrightarrow>\<^sub>c s'"} *}
kleing@12431
   153
    { assume b: "b s"
kleing@12431
   154
      with w obtain s'' where
kleing@12431
   155
        "\<langle>c, s\<rangle> \<longrightarrow>\<^sub>c s''" and "\<langle>?w, s''\<rangle> \<longrightarrow>\<^sub>c s'" by (cases set: evalc) auto
kleing@12431
   156
      -- "now we can build a derivation tree for the @{text \<IF>}"
kleing@12431
   157
      -- "first, the body of the True-branch:"
kleing@12431
   158
      hence "\<langle>c; ?w, s\<rangle> \<longrightarrow>\<^sub>c s'" by (rule Semi)
kleing@12431
   159
      -- "then the whole @{text \<IF>}"
kleing@12431
   160
      with b have "\<langle>?if, s\<rangle> \<longrightarrow>\<^sub>c s'" by (rule IfTrue)
kleing@12431
   161
    }
wenzelm@18372
   162
    ultimately
wenzelm@18372
   163
    -- "both cases together give us what we want:"
kleing@12431
   164
    have "\<langle>?if, s\<rangle> \<longrightarrow>\<^sub>c s'" by blast
kleing@12431
   165
  }
kleing@12431
   166
  moreover
kleing@12431
   167
  -- "now the other direction:"
kleing@12431
   168
  { fix s s' assume if: "\<langle>?if, s\<rangle> \<longrightarrow>\<^sub>c s'"
kleing@12431
   169
    -- "again, if @{text b} is @{text False} in state @{text s}, then the False-branch"
kleing@12431
   170
    -- "of the @{text \<IF>} is executed, and both statements do nothing:"
kleing@12431
   171
    hence "\<not>b s \<Longrightarrow> s = s'" by simp
kleing@12431
   172
    hence "\<not>b s \<Longrightarrow> \<langle>?w, s\<rangle> \<longrightarrow>\<^sub>c s'" by simp
kleing@12431
   173
    moreover
kleing@12431
   174
    -- "on the other hand, if @{text b} is @{text True} in state @{text s},"
kleing@12431
   175
    -- {* then this time only the @{text IfTrue} rule can have be used *}
kleing@12431
   176
    { assume b: "b s"
kleing@12431
   177
      with if have "\<langle>c; ?w, s\<rangle> \<longrightarrow>\<^sub>c s'" by (cases set: evalc) auto
kleing@12431
   178
      -- "and for this, only the Semi-rule is applicable:"
kleing@12431
   179
      then obtain s'' where
kleing@12431
   180
        "\<langle>c, s\<rangle> \<longrightarrow>\<^sub>c s''" and "\<langle>?w, s''\<rangle> \<longrightarrow>\<^sub>c s'" by (cases set: evalc) auto
kleing@12431
   181
      -- "with this information, we can build a derivation tree for the @{text \<WHILE>}"
kleing@12431
   182
      with b
wenzelm@18372
   183
      have "\<langle>?w, s\<rangle> \<longrightarrow>\<^sub>c s'" by (rule WhileTrue)
kleing@12431
   184
    }
wenzelm@18372
   185
    ultimately
kleing@12431
   186
    -- "both cases together again give us what we want:"
kleing@12431
   187
    have "\<langle>?w, s\<rangle> \<longrightarrow>\<^sub>c s'" by blast
kleing@12431
   188
  }
kleing@12431
   189
  ultimately
kleing@12431
   190
  show ?thesis by blast
kleing@12431
   191
qed
kleing@12431
   192
kleing@12431
   193
kleing@12431
   194
subsection "Execution is deterministic"
nipkow@1700
   195
kleing@12431
   196
text {*
kleing@12431
   197
The following proof presents all the details:
kleing@12431
   198
*}
wenzelm@18372
   199
theorem com_det:
wenzelm@18372
   200
  assumes "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c t" and "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   201
  shows "u = t"
wenzelm@18372
   202
  using prems
wenzelm@18372
   203
proof (induct fixing: u set: evalc)
wenzelm@18372
   204
  fix s u assume "\<langle>\<SKIP>,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   205
  thus "u = s" by simp
wenzelm@18372
   206
next
wenzelm@18372
   207
  fix a s x u assume "\<langle>x :== a,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   208
  thus "u = s[x \<mapsto> a s]" by simp
wenzelm@18372
   209
next
wenzelm@18372
   210
  fix c0 c1 s s1 s2 u
wenzelm@18372
   211
  assume IH0: "\<And>u. \<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s2"
wenzelm@18372
   212
  assume IH1: "\<And>u. \<langle>c1,s2\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s1"
kleing@12431
   213
wenzelm@18372
   214
  assume "\<langle>c0;c1, s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   215
  then obtain s' where
kleing@12431
   216
      c0: "\<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c s'" and
wenzelm@18372
   217
      c1: "\<langle>c1,s'\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   218
    by auto
kleing@12431
   219
wenzelm@18372
   220
  from c0 IH0 have "s'=s2" by blast
wenzelm@18372
   221
  with c1 IH1 show "u=s1" by blast
wenzelm@18372
   222
next
wenzelm@18372
   223
  fix b c0 c1 s s1 u
wenzelm@18372
   224
  assume IH: "\<And>u. \<langle>c0,s\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s1"
kleing@12431
   225
wenzelm@18372
   226
  assume "b s" and "\<langle>\<IF> b \<THEN> c0 \<ELSE> c1,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   227
  hence "\<langle>c0, s\<rangle> \<longrightarrow>\<^sub>c u" by simp
wenzelm@18372
   228
  with IH show "u = s1" by blast
wenzelm@18372
   229
next
wenzelm@18372
   230
  fix b c0 c1 s s1 u
wenzelm@18372
   231
  assume IH: "\<And>u. \<langle>c1,s\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s1"
nipkow@1700
   232
wenzelm@18372
   233
  assume "\<not>b s" and "\<langle>\<IF> b \<THEN> c0 \<ELSE> c1,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   234
  hence "\<langle>c1, s\<rangle> \<longrightarrow>\<^sub>c u" by simp
wenzelm@18372
   235
  with IH show "u = s1" by blast
wenzelm@18372
   236
next
wenzelm@18372
   237
  fix b c s u
wenzelm@18372
   238
  assume "\<not>b s" and "\<langle>\<WHILE> b \<DO> c,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   239
  thus "u = s" by simp
wenzelm@18372
   240
next
wenzelm@18372
   241
  fix b c s s1 s2 u
wenzelm@18372
   242
  assume "IH\<^sub>c": "\<And>u. \<langle>c,s\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s2"
wenzelm@18372
   243
  assume "IH\<^sub>w": "\<And>u. \<langle>\<WHILE> b \<DO> c,s2\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s1"
wenzelm@18372
   244
wenzelm@18372
   245
  assume "b s" and "\<langle>\<WHILE> b \<DO> c,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   246
  then obtain s' where
kleing@12431
   247
      c: "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'" and
wenzelm@18372
   248
      w: "\<langle>\<WHILE> b \<DO> c,s'\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   249
    by auto
wenzelm@18372
   250
wenzelm@18372
   251
  from c "IH\<^sub>c" have "s' = s2" by blast
wenzelm@18372
   252
  with w "IH\<^sub>w" show "u = s1" by blast
kleing@12431
   253
qed
kleing@12431
   254
nipkow@1700
   255
kleing@12431
   256
text {*
kleing@12431
   257
  This is the proof as you might present it in a lecture. The remaining
wenzelm@18372
   258
  cases are simple enough to be proved automatically:
wenzelm@18372
   259
*}
wenzelm@18372
   260
theorem
wenzelm@18372
   261
  assumes "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c t" and "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   262
  shows "u = t"
wenzelm@18372
   263
  using prems
wenzelm@18372
   264
proof (induct fixing: u)
wenzelm@18372
   265
  -- "the simple @{text \<SKIP>} case for demonstration:"
wenzelm@18372
   266
  fix s u assume "\<langle>\<SKIP>,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   267
  thus "u = s" by simp
wenzelm@18372
   268
next
wenzelm@18372
   269
  -- "and the only really interesting case, @{text \<WHILE>}:"
wenzelm@18372
   270
  fix b c s s1 s2 u
wenzelm@18372
   271
  assume "IH\<^sub>c": "\<And>u. \<langle>c,s\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s2"
wenzelm@18372
   272
  assume "IH\<^sub>w": "\<And>u. \<langle>\<WHILE> b \<DO> c,s2\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> u = s1"
wenzelm@18372
   273
wenzelm@18372
   274
  assume "b s" and "\<langle>\<WHILE> b \<DO> c,s\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   275
  then obtain s' where
kleing@12431
   276
      c: "\<langle>c,s\<rangle> \<longrightarrow>\<^sub>c s'" and
kleing@12431
   277
      w: "\<langle>\<WHILE> b \<DO> c,s'\<rangle> \<longrightarrow>\<^sub>c u"
wenzelm@18372
   278
    by auto
wenzelm@18372
   279
wenzelm@18372
   280
  from c "IH\<^sub>c" have "s' = s2" by blast
wenzelm@18372
   281
  with w "IH\<^sub>w" show "u = s1" by blast
wenzelm@18372
   282
qed (best dest: evalc_cases [THEN iffD1])+ -- "prove the rest automatically"
nipkow@1700
   283
nipkow@1700
   284
end