src/HOL/IMP/Big_Step.thy
author haftmann
Wed, 25 Dec 2013 17:39:06 +0100
changeset 54863 82acc20ded73
parent 54192 a5eec4263b3a
child 59451 86be42bb5174
permissions -rw-r--r--
prefer more canonical names for lemmas on min/max
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     1
(* Author: Gerwin Klein, Tobias Nipkow *)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     2
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     3
theory Big_Step imports Com begin
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     4
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     5
subsection "Big-Step Semantics of Commands"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     6
52370
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
     7
text {*
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
     8
The big-step semantics is a straight-forward inductive definition
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
     9
with concrete syntax. Note that the first paramenter is a tuple,
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    10
so the syntax becomes @{text "(c,s) \<Rightarrow> s'"}.
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    11
*}
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    12
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
    13
text_raw{*\snip{BigStepdef}{0}{1}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    14
inductive
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    15
  big_step :: "com \<times> state \<Rightarrow> state \<Rightarrow> bool" (infix "\<Rightarrow>" 55)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    16
where
52391
e65dedce4a17 adjust layout for book
kleing
parents: 52386
diff changeset
    17
Skip: "(SKIP,s) \<Rightarrow> s" |
e65dedce4a17 adjust layout for book
kleing
parents: 52386
diff changeset
    18
Assign: "(x ::= a,s) \<Rightarrow> s(x := aval a s)" |
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
    19
Seq: "\<lbrakk> (c\<^sub>1,s\<^sub>1) \<Rightarrow> s\<^sub>2;  (c\<^sub>2,s\<^sub>2) \<Rightarrow> s\<^sub>3 \<rbrakk> \<Longrightarrow> (c\<^sub>1;;c\<^sub>2, s\<^sub>1) \<Rightarrow> s\<^sub>3" |
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
    20
IfTrue: "\<lbrakk> bval b s;  (c\<^sub>1,s) \<Rightarrow> t \<rbrakk> \<Longrightarrow> (IF b THEN c\<^sub>1 ELSE c\<^sub>2, s) \<Rightarrow> t" |
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
    21
IfFalse: "\<lbrakk> \<not>bval b s;  (c\<^sub>2,s) \<Rightarrow> t \<rbrakk> \<Longrightarrow> (IF b THEN c\<^sub>1 ELSE c\<^sub>2, s) \<Rightarrow> t" |
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    22
WhileFalse: "\<not>bval b s \<Longrightarrow> (WHILE b DO c,s) \<Rightarrow> s" |
52391
e65dedce4a17 adjust layout for book
kleing
parents: 52386
diff changeset
    23
WhileTrue:
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
    24
"\<lbrakk> bval b s\<^sub>1;  (c,s\<^sub>1) \<Rightarrow> s\<^sub>2;  (WHILE b DO c, s\<^sub>2) \<Rightarrow> s\<^sub>3 \<rbrakk> 
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
    25
\<Longrightarrow> (WHILE b DO c, s\<^sub>1) \<Rightarrow> s\<^sub>3"
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
    26
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    27
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
    28
text_raw{*\snip{BigStepEx}{1}{2}{% *}
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
    29
schematic_lemma ex: "(''x'' ::= N 5;; ''y'' ::= V ''x'', s) \<Rightarrow> ?t"
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45324
diff changeset
    30
apply(rule Seq)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    31
apply(rule Assign)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    32
apply simp
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    33
apply(rule Assign)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    34
done
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
    35
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    36
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    37
thm ex[simplified]
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    38
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    39
text{* We want to execute the big-step rules: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    40
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    41
code_pred big_step .
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    42
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    43
text{* For inductive definitions we need command
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    44
       \texttt{values} instead of \texttt{value}. *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    45
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44070
diff changeset
    46
values "{t. (SKIP, \<lambda>_. 0) \<Rightarrow> t}"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    47
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    48
text{* We need to translate the result state into a list
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    49
to display it. *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    50
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43158
diff changeset
    51
values "{map t [''x''] |t. (SKIP, <''x'' := 42>) \<Rightarrow> t}"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    52
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43158
diff changeset
    53
values "{map t [''x''] |t. (''x'' ::= N 2, <''x'' := 42>) \<Rightarrow> t}"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    54
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    55
values "{map t [''x'',''y''] |t.
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    56
  (WHILE Less (V ''x'') (V ''y'') DO (''x'' ::= Plus (V ''x'') (N 5)),
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43158
diff changeset
    57
   <''x'' := 0, ''y'' := 13>) \<Rightarrow> t}"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    58
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    59
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    60
text{* Proof automation: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    61
52370
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    62
text {* The introduction rules are good for automatically
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    63
construction small program executions. The recursive cases
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    64
may require backtracking, so we declare the set as unsafe
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    65
intro rules. *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    66
declare big_step.intros [intro]
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    67
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    68
text{* The standard induction rule 
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    69
@{thm [display] big_step.induct [no_vars]} *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    70
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    71
thm big_step.induct
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    72
52370
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    73
text{*
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    74
This induction schema is almost perfect for our purposes, but
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    75
our trick for reusing the tuple syntax means that the induction
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    76
schema has two parameters instead of the @{text c}, @{text s},
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    77
and @{text s'} that we are likely to encounter. Splitting
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    78
the tuple parameter fixes this:
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    79
*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    80
lemmas big_step_induct = big_step.induct[split_format(complete)]
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    81
thm big_step_induct
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    82
text {*
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    83
@{thm [display] big_step_induct [no_vars]}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    84
*}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    85
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    86
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    87
subsection "Rule inversion"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    88
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    89
text{* What can we deduce from @{prop "(SKIP,s) \<Rightarrow> t"} ?
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    90
That @{prop "s = t"}. This is how we can automatically prove it: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    91
51513
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
    92
inductive_cases SkipE[elim!]: "(SKIP,s) \<Rightarrow> t"
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
    93
thm SkipE
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    94
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    95
text{* This is an \emph{elimination rule}. The [elim] attribute tells auto,
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    96
blast and friends (but not simp!) to use it automatically; [elim!] means that
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    97
it is applied eagerly.
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    98
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    99
Similarly for the other commands: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   100
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   101
inductive_cases AssignE[elim!]: "(x ::= a,s) \<Rightarrow> t"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   102
thm AssignE
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   103
inductive_cases SeqE[elim!]: "(c1;;c2,s1) \<Rightarrow> s3"
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45324
diff changeset
   104
thm SeqE
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   105
inductive_cases IfE[elim!]: "(IF b THEN c1 ELSE c2,s) \<Rightarrow> t"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   106
thm IfE
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   107
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   108
inductive_cases WhileE[elim]: "(WHILE b DO c,s) \<Rightarrow> t"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   109
thm WhileE
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   110
text{* Only [elim]: [elim!] would not terminate. *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   111
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   112
text{* An automatic example: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   113
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   114
lemma "(IF b THEN SKIP ELSE SKIP, s) \<Rightarrow> t \<Longrightarrow> t = s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   115
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   116
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   117
text{* Rule inversion by hand via the ``cases'' method: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   118
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   119
lemma assumes "(IF b THEN SKIP ELSE SKIP, s) \<Rightarrow> t"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   120
shows "t = s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   121
proof-
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   122
  from assms show ?thesis
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   123
  proof cases  --"inverting assms"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   124
    case IfTrue thm IfTrue
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   125
    thus ?thesis by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   126
  next
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   127
    case IfFalse thus ?thesis by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   128
  qed
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   129
qed
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   130
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents: 44036
diff changeset
   131
(* Using rule inversion to prove simplification rules: *)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents: 44036
diff changeset
   132
lemma assign_simp:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents: 44036
diff changeset
   133
  "(x ::= a,s) \<Rightarrow> s' \<longleftrightarrow> (s' = s(x := aval a s))"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents: 44036
diff changeset
   134
  by auto
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   135
52376
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   136
text {* An example combining rule inversion and derivations *}
52399
nipkow
parents: 52391
diff changeset
   137
lemma Seq_assoc:
52376
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   138
  "(c1;; c2;; c3, s) \<Rightarrow> s' \<longleftrightarrow> (c1;; (c2;; c3), s) \<Rightarrow> s'"
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   139
proof
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   140
  assume "(c1;; c2;; c3, s) \<Rightarrow> s'"
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   141
  then obtain s1 s2 where
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   142
    c1: "(c1, s) \<Rightarrow> s1" and
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   143
    c2: "(c2, s1) \<Rightarrow> s2" and
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   144
    c3: "(c3, s2) \<Rightarrow> s'" by auto
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   145
  from c2 c3
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   146
  have "(c2;; c3, s1) \<Rightarrow> s'" by (rule Seq)
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   147
  with c1
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   148
  show "(c1;; (c2;; c3), s) \<Rightarrow> s'" by (rule Seq)
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   149
next
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   150
  -- "The other direction is analogous"
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   151
  assume "(c1;; (c2;; c3), s) \<Rightarrow> s'"
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   152
  thus "(c1;; c2;; c3, s) \<Rightarrow> s'" by auto
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   153
qed
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   154
90fd1922f45f another example lemma
kleing
parents: 52372
diff changeset
   155
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   156
subsection "Command Equivalence"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   157
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   158
text {*
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   159
  We call two statements @{text c} and @{text c'} equivalent wrt.\ the
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   160
  big-step semantics when \emph{@{text c} started in @{text s} terminates
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   161
  in @{text s'} iff @{text c'} started in the same @{text s} also terminates
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   162
  in the same @{text s'}}. Formally:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   163
*}
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
   164
text_raw{*\snip{BigStepEquiv}{0}{1}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   165
abbreviation
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   166
  equiv_c :: "com \<Rightarrow> com \<Rightarrow> bool" (infix "\<sim>" 50) where
52372
02dfb6bb487a prefer xsymbol for book
kleing
parents: 52370
diff changeset
   167
  "c \<sim> c' \<equiv> (\<forall>s t. (c,s) \<Rightarrow> t  =  (c',s) \<Rightarrow> t)"
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
   168
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   169
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   170
text {*
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   171
Warning: @{text"\<sim>"} is the symbol written \verb!\ < s i m >! (without spaces).
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   172
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   173
  As an example, we show that loop unfolding is an equivalence
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   174
  transformation on programs:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   175
*}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   176
lemma unfold_while:
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   177
  "(WHILE b DO c) \<sim> (IF b THEN c;; WHILE b DO c ELSE SKIP)" (is "?w \<sim> ?iw")
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   178
proof -
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   179
  -- "to show the equivalence, we look at the derivation tree for"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   180
  -- "each side and from that construct a derivation tree for the other side"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   181
  { fix s t assume "(?w, s) \<Rightarrow> t"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   182
    -- "as a first thing we note that, if @{text b} is @{text False} in state @{text s},"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   183
    -- "then both statements do nothing:"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   184
    { assume "\<not>bval b s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   185
      hence "t = s" using `(?w,s) \<Rightarrow> t` by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   186
      hence "(?iw, s) \<Rightarrow> t" using `\<not>bval b s` by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   187
    }
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   188
    moreover
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   189
    -- "on the other hand, if @{text b} is @{text True} in state @{text s},"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   190
    -- {* then only the @{text WhileTrue} rule can have been used to derive @{text "(?w, s) \<Rightarrow> t"} *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   191
    { assume "bval b s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   192
      with `(?w, s) \<Rightarrow> t` obtain s' where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   193
        "(c, s) \<Rightarrow> s'" and "(?w, s') \<Rightarrow> t" by auto
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   194
      -- "now we can build a derivation tree for the @{text IF}"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   195
      -- "first, the body of the True-branch:"
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   196
      hence "(c;; ?w, s) \<Rightarrow> t" by (rule Seq)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   197
      -- "then the whole @{text IF}"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   198
      with `bval b s` have "(?iw, s) \<Rightarrow> t" by (rule IfTrue)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   199
    }
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   200
    ultimately
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   201
    -- "both cases together give us what we want:"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   202
    have "(?iw, s) \<Rightarrow> t" by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   203
  }
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   204
  moreover
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   205
  -- "now the other direction:"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   206
  { fix s t assume "(?iw, s) \<Rightarrow> t"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   207
    -- "again, if @{text b} is @{text False} in state @{text s}, then the False-branch"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   208
    -- "of the @{text IF} is executed, and both statements do nothing:"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   209
    { assume "\<not>bval b s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   210
      hence "s = t" using `(?iw, s) \<Rightarrow> t` by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   211
      hence "(?w, s) \<Rightarrow> t" using `\<not>bval b s` by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   212
    }
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   213
    moreover
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   214
    -- "on the other hand, if @{text b} is @{text True} in state @{text s},"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   215
    -- {* then this time only the @{text IfTrue} rule can have be used *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   216
    { assume "bval b s"
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   217
      with `(?iw, s) \<Rightarrow> t` have "(c;; ?w, s) \<Rightarrow> t" by auto
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45324
diff changeset
   218
      -- "and for this, only the Seq-rule is applicable:"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   219
      then obtain s' where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   220
        "(c, s) \<Rightarrow> s'" and "(?w, s') \<Rightarrow> t" by auto
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   221
      -- "with this information, we can build a derivation tree for the @{text WHILE}"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   222
      with `bval b s`
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   223
      have "(?w, s) \<Rightarrow> t" by (rule WhileTrue)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   224
    }
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   225
    ultimately
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   226
    -- "both cases together again give us what we want:"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   227
    have "(?w, s) \<Rightarrow> t" by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   228
  }
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   229
  ultimately
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   230
  show ?thesis by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   231
qed
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   232
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   233
text {* Luckily, such lengthy proofs are seldom necessary.  Isabelle can
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   234
prove many such facts automatically.  *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   235
45321
b227989b6ee6 more IMP text fragments
kleing
parents: 45015
diff changeset
   236
lemma while_unfold:
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   237
  "(WHILE b DO c) \<sim> (IF b THEN c;; WHILE b DO c ELSE SKIP)"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   238
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   239
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   240
lemma triv_if:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   241
  "(IF b THEN c ELSE c) \<sim> c"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   242
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   243
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   244
lemma commute_if:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   245
  "(IF b1 THEN (IF b2 THEN c11 ELSE c12) ELSE c2) 
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   246
   \<sim> 
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   247
   (IF b2 THEN (IF b1 THEN c11 ELSE c2) ELSE (IF b1 THEN c12 ELSE c2))"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   248
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   249
51513
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   250
lemma sim_while_cong_aux:
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   251
  "(WHILE b DO c,s) \<Rightarrow> t  \<Longrightarrow> c \<sim> c' \<Longrightarrow>  (WHILE b DO c',s) \<Rightarrow> t"
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   252
apply(induction "WHILE b DO c" s t arbitrary: b c rule: big_step_induct)
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   253
 apply blast
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   254
apply blast
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   255
done
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   256
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   257
lemma sim_while_cong: "c \<sim> c' \<Longrightarrow> WHILE b DO c \<sim> WHILE b DO c'"
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   258
by (metis sim_while_cong_aux)
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   259
52021
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   260
text {* Command equivalence is an equivalence relation, i.e.\ it is
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   261
reflexive, symmetric, and transitive. Because we used an abbreviation
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   262
above, Isabelle derives this automatically. *}
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   263
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   264
lemma sim_refl:  "c \<sim> c" by simp
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   265
lemma sim_sym:   "(c \<sim> c') = (c' \<sim> c)" by auto
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   266
lemma sim_trans: "c \<sim> c' \<Longrightarrow> c' \<sim> c'' \<Longrightarrow> c \<sim> c''" by auto
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   267
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   268
subsection "Execution is deterministic"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   269
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   270
text {* This proof is automatic. *}
54192
nipkow
parents: 53015
diff changeset
   271
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   272
theorem big_step_determ: "\<lbrakk> (c,s) \<Rightarrow> t; (c,s) \<Rightarrow> u \<rbrakk> \<Longrightarrow> u = t"
45321
b227989b6ee6 more IMP text fragments
kleing
parents: 45015
diff changeset
   273
  by (induction arbitrary: u rule: big_step.induct) blast+
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   274
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   275
text {*
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   276
  This is the proof as you might present it in a lecture. The remaining
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   277
  cases are simple enough to be proved automatically:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   278
*}
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
   279
text_raw{*\snip{BigStepDetLong}{0}{2}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   280
theorem
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   281
  "(c,s) \<Rightarrow> t  \<Longrightarrow>  (c,s) \<Rightarrow> t'  \<Longrightarrow>  t' = t"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44923
diff changeset
   282
proof (induction arbitrary: t' rule: big_step.induct)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   283
  -- "the only interesting case, @{text WhileTrue}:"
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   284
  fix b c s s\<^sub>1 t t'
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   285
  -- "The assumptions of the rule:"
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   286
  assume "bval b s" and "(c,s) \<Rightarrow> s\<^sub>1" and "(WHILE b DO c,s\<^sub>1) \<Rightarrow> t"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   287
  -- {* Ind.Hyp; note the @{text"\<And>"} because of arbitrary: *}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   288
  assume IHc: "\<And>t'. (c,s) \<Rightarrow> t' \<Longrightarrow> t' = s\<^sub>1"
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   289
  assume IHw: "\<And>t'. (WHILE b DO c,s\<^sub>1) \<Rightarrow> t' \<Longrightarrow> t' = t"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   290
  -- "Premise of implication:"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   291
  assume "(WHILE b DO c,s) \<Rightarrow> t'"
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   292
  with `bval b s` obtain s\<^sub>1' where
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   293
      c: "(c,s) \<Rightarrow> s\<^sub>1'" and
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   294
      w: "(WHILE b DO c,s\<^sub>1') \<Rightarrow> t'"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   295
    by auto
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   296
  from c IHc have "s\<^sub>1' = s\<^sub>1" by blast
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   297
  with w IHw show "t' = t" by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   298
qed blast+ -- "prove the rest automatically"
49191
3601bf546775 tuned latex
nipkow
parents: 47818
diff changeset
   299
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   300
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   301
end