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