src/HOL/IMP/Big_Step.thy
author wenzelm
Tue, 16 Jan 2018 09:30:00 +0100
changeset 67443 3abf6a722518
parent 67406 23307fd33906
child 68776 403dd13cf6e9
permissions -rw-r--r--
standardized towards new-style formal comments: isabelle update_comments;
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
     7
text \<open>
52370
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
     8
The big-step semantics is a straight-forward inductive definition
59451
86be42bb5174 spelling error
hoelzl
parents: 54192
diff changeset
     9
with concrete syntax. Note that the first parameter is a tuple,
52370
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    10
so the syntax becomes @{text "(c,s) \<Rightarrow> s'"}.
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    11
\<close>
52370
ec46a485bf60 some comments on syntax and automation setup
kleing
parents: 52046
diff changeset
    12
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    13
text_raw\<open>\snip{BigStepdef}{0}{1}{%\<close>
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"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    26
text_raw\<open>}%endsnip\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    27
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    28
text_raw\<open>\snip{BigStepEx}{1}{2}{%\<close>
61337
4645502c3c64 fewer aliases for toplevel theorem statements;
wenzelm
parents: 59451
diff changeset
    29
schematic_goal 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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    35
text_raw\<open>}%endsnip\<close>
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    39
text\<open>We want to execute the big-step rules:\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    43
text\<open>For inductive definitions we need command
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    44
       \texttt{values} instead of \texttt{value}.\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    48
text\<open>We need to translate the result state into a list
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    49
to display it.\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    60
text\<open>Proof automation:\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    61
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    62
text \<open>The introduction rules are good for automatically
52370
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    65
intro rules.\<close>
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    68
text\<open>The standard induction rule 
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    69
@{thm [display] big_step.induct [no_vars]}\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    73
text\<open>
52370
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:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    79
\<close>
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    82
text \<open>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    83
@{thm [display] big_step_induct [no_vars]}
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    84
\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    89
text\<open>What can we deduce from @{prop "(SKIP,s) \<Rightarrow> t"} ?
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    90
That @{prop "s = t"}. This is how we can automatically prove it:\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    95
text\<open>This is an \emph{elimination rule}. The [elim] attribute tells auto,
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
    99
Similarly for the other commands:\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   110
text\<open>Only [elim]: [elim!] would not terminate.\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   111
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   112
text\<open>An automatic example:\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   117
text\<open>Rule inversion by hand via the ``cases'' method:\<close>
43141
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
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   123
  proof cases  \<comment> \<open>inverting assms\<close>
43141
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   136
text \<open>An example combining rule inversion and derivations\<close>
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
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   150
  \<comment> \<open>The other direction is analogous\<close>
52376
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   158
text \<open>
43141
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:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   163
\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   164
text_raw\<open>\snip{BigStepEquiv}{0}{1}{%\<close>
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)"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   168
text_raw\<open>}%endsnip\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   169
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   170
text \<open>
43141
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:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   175
\<close>
43141
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 -
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   179
  \<comment> \<open>to show the equivalence, we look at the derivation tree for\<close>
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   180
  \<comment> \<open>each side and from that construct a derivation tree for the other side\<close>
67019
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   181
  have "(?iw, s) \<Rightarrow> t" if assm: "(?w, s) \<Rightarrow> t" for s t
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   182
  proof -
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   183
    from assm show ?thesis
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   184
    proof cases \<comment> \<open>rule inversion on \<open>(?w, s) \<Rightarrow> t\<close>\<close>
64530
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   185
      case WhileFalse
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   186
      thus ?thesis by blast
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   187
    next
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   188
      case WhileTrue
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   189
      from \<open>bval b s\<close> \<open>(?w, s) \<Rightarrow> t\<close> obtain s' where
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   190
        "(c, s) \<Rightarrow> s'" and "(?w, s') \<Rightarrow> t" by auto
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   191
      \<comment> \<open>now we can build a derivation tree for the @{text IF}\<close>
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   192
      \<comment> \<open>first, the body of the True-branch:\<close>
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   193
      hence "(c;; ?w, s) \<Rightarrow> t" by (rule Seq)
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   194
      \<comment> \<open>then the whole @{text IF}\<close>
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   195
      with \<open>bval b s\<close> show ?thesis by (rule IfTrue)
64530
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   196
    qed
67019
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   197
  qed
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   198
  moreover
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   199
  \<comment> \<open>now the other direction:\<close>
67019
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   200
  have "(?w, s) \<Rightarrow> t" if assm: "(?iw, s) \<Rightarrow> t" for s t
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   201
  proof -
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   202
    from assm show ?thesis
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   203
    proof cases \<comment> \<open>rule inversion on \<open>(?iw, s) \<Rightarrow> t\<close>\<close>
64530
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   204
      case IfFalse
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   205
      hence "s = t" using \<open>(?iw, s) \<Rightarrow> t\<close> by blast
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   206
      thus ?thesis using \<open>\<not>bval b s\<close> by blast
64530
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   207
    next
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   208
      case IfTrue
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   209
      \<comment> \<open>and for this, only the Seq-rule is applicable:\<close>
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   210
      from \<open>(c;; ?w, s) \<Rightarrow> t\<close> obtain s' where
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   211
        "(c, s) \<Rightarrow> s'" and "(?w, s') \<Rightarrow> t" by auto
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   212
      \<comment> \<open>with this information, we can build a derivation tree for @{text WHILE}\<close>
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   213
      with \<open>bval b s\<close> show ?thesis by (rule WhileTrue)
64530
a720c3911308 tuned proof
nipkow
parents: 61337
diff changeset
   214
    qed
67019
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 64530
diff changeset
   215
  qed
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   216
  ultimately
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   217
  show ?thesis by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   218
qed
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   219
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   220
text \<open>Luckily, such lengthy proofs are seldom necessary.  Isabelle can
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   221
prove many such facts automatically.\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   222
45321
b227989b6ee6 more IMP text fragments
kleing
parents: 45015
diff changeset
   223
lemma while_unfold:
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 52021
diff changeset
   224
  "(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
   225
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   226
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   227
lemma triv_if:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   228
  "(IF b THEN c ELSE c) \<sim> c"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   229
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   230
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   231
lemma commute_if:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   232
  "(IF b1 THEN (IF b2 THEN c11 ELSE c12) ELSE c2) 
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   233
   \<sim> 
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   234
   (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
   235
by blast
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   236
51513
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   237
lemma sim_while_cong_aux:
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   238
  "(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
   239
apply(induction "WHILE b DO c" s t arbitrary: b c rule: big_step_induct)
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   240
 apply blast
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   241
apply blast
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   242
done
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   243
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   244
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
   245
by (metis sim_while_cong_aux)
7a2912282391 added lemmas
nipkow
parents: 50054
diff changeset
   246
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   247
text \<open>Command equivalence is an equivalence relation, i.e.\ it is
52021
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   248
reflexive, symmetric, and transitive. Because we used an abbreviation
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   249
above, Isabelle derives this automatically.\<close>
52021
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   250
59963cda805a explicitly state equivalence relation for sim; tweak syntax of sem_equiv
kleing
parents: 51513
diff changeset
   251
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
   252
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
   253
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
   254
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   255
subsection "Execution is deterministic"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   256
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   257
text \<open>This proof is automatic.\<close>
54192
nipkow
parents: 53015
diff changeset
   258
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   259
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
   260
  by (induction arbitrary: u rule: big_step.induct) blast+
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   261
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   262
text \<open>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   263
  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
   264
  cases are simple enough to be proved automatically:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   265
\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   266
text_raw\<open>\snip{BigStepDetLong}{0}{2}{%\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   267
theorem
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   268
  "(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
   269
proof (induction arbitrary: t' rule: big_step.induct)
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   270
  \<comment> \<open>the only interesting case, @{text WhileTrue}:\<close>
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   271
  fix b c s s\<^sub>1 t t'
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   272
  \<comment> \<open>The assumptions of the rule:\<close>
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   273
  assume "bval b s" and "(c,s) \<Rightarrow> s\<^sub>1" and "(WHILE b DO c,s\<^sub>1) \<Rightarrow> t"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   274
  \<comment> \<open>Ind.Hyp; note the @{text"\<And>"} because of arbitrary:\<close>
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   275
  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
   276
  assume IHw: "\<And>t'. (WHILE b DO c,s\<^sub>1) \<Rightarrow> t' \<Longrightarrow> t' = t"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   277
  \<comment> \<open>Premise of implication:\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   278
  assume "(WHILE b DO c,s) \<Rightarrow> t'"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   279
  with \<open>bval b s\<close> obtain s\<^sub>1' where
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   280
      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
   281
      w: "(WHILE b DO c,s\<^sub>1') \<Rightarrow> t'"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   282
    by auto
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52399
diff changeset
   283
  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
   284
  with w IHw show "t' = t" by blast
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
   285
qed blast+ \<comment> \<open>prove the rest automatically\<close>
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67071
diff changeset
   286
text_raw\<open>}%endsnip\<close>
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   287
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   288
end