43141
|
1 |
header "Small-Step Semantics of Commands"
|
|
2 |
|
|
3 |
theory Small_Step imports Star Big_Step begin
|
|
4 |
|
|
5 |
subsection "The transition relation"
|
|
6 |
|
|
7 |
inductive
|
|
8 |
small_step :: "com * state \<Rightarrow> com * state \<Rightarrow> bool" (infix "\<rightarrow>" 55)
|
|
9 |
where
|
|
10 |
Assign: "(x ::= a, s) \<rightarrow> (SKIP, s(x := aval a s))" |
|
|
11 |
|
|
12 |
Semi1: "(SKIP;c\<^isub>2,s) \<rightarrow> (c\<^isub>2,s)" |
|
|
13 |
Semi2: "(c\<^isub>1,s) \<rightarrow> (c\<^isub>1',s') \<Longrightarrow> (c\<^isub>1;c\<^isub>2,s) \<rightarrow> (c\<^isub>1';c\<^isub>2,s')" |
|
|
14 |
|
|
15 |
IfTrue: "bval b s \<Longrightarrow> (IF b THEN c\<^isub>1 ELSE c\<^isub>2,s) \<rightarrow> (c\<^isub>1,s)" |
|
|
16 |
IfFalse: "\<not>bval b s \<Longrightarrow> (IF b THEN c\<^isub>1 ELSE c\<^isub>2,s) \<rightarrow> (c\<^isub>2,s)" |
|
|
17 |
|
|
18 |
While: "(WHILE b DO c,s) \<rightarrow> (IF b THEN c; WHILE b DO c ELSE SKIP,s)"
|
|
19 |
|
|
20 |
|
|
21 |
abbreviation small_steps :: "com * state \<Rightarrow> com * state \<Rightarrow> bool" (infix "\<rightarrow>*" 55)
|
|
22 |
where "x \<rightarrow>* y == star small_step x y"
|
|
23 |
|
|
24 |
subsection{* Executability *}
|
|
25 |
|
|
26 |
code_pred small_step .
|
|
27 |
|
|
28 |
values "{(c',map t [''x'',''y'',''z'']) |c' t.
|
|
29 |
(''x'' ::= V ''z''; ''y'' ::= V ''x'',
|
44036
|
30 |
<''x'' := 3, ''y'' := 7, ''z'' := 5>) \<rightarrow>* (c',t)}"
|
43141
|
31 |
|
|
32 |
|
|
33 |
subsection{* Proof infrastructure *}
|
|
34 |
|
|
35 |
subsubsection{* Induction rules *}
|
|
36 |
|
|
37 |
text{* The default induction rule @{thm[source] small_step.induct} only works
|
|
38 |
for lemmas of the form @{text"a \<rightarrow> b \<Longrightarrow> \<dots>"} where @{text a} and @{text b} are
|
|
39 |
not already pairs @{text"(DUMMY,DUMMY)"}. We can generate a suitable variant
|
|
40 |
of @{thm[source] small_step.induct} for pairs by ``splitting'' the arguments
|
|
41 |
@{text"\<rightarrow>"} into pairs: *}
|
|
42 |
lemmas small_step_induct = small_step.induct[split_format(complete)]
|
|
43 |
|
|
44 |
|
|
45 |
subsubsection{* Proof automation *}
|
|
46 |
|
|
47 |
declare small_step.intros[simp,intro]
|
|
48 |
|
|
49 |
text{* Rule inversion: *}
|
|
50 |
|
|
51 |
inductive_cases SkipE[elim!]: "(SKIP,s) \<rightarrow> ct"
|
|
52 |
thm SkipE
|
|
53 |
inductive_cases AssignE[elim!]: "(x::=a,s) \<rightarrow> ct"
|
|
54 |
thm AssignE
|
|
55 |
inductive_cases SemiE[elim]: "(c1;c2,s) \<rightarrow> ct"
|
|
56 |
thm SemiE
|
|
57 |
inductive_cases IfE[elim!]: "(IF b THEN c1 ELSE c2,s) \<rightarrow> ct"
|
|
58 |
inductive_cases WhileE[elim]: "(WHILE b DO c, s) \<rightarrow> ct"
|
|
59 |
|
|
60 |
|
|
61 |
text{* A simple property: *}
|
|
62 |
lemma deterministic:
|
|
63 |
"cs \<rightarrow> cs' \<Longrightarrow> cs \<rightarrow> cs'' \<Longrightarrow> cs'' = cs'"
|
45015
|
64 |
apply(induction arbitrary: cs'' rule: small_step.induct)
|
43141
|
65 |
apply blast+
|
|
66 |
done
|
|
67 |
|
|
68 |
|
|
69 |
subsection "Equivalence with big-step semantics"
|
|
70 |
|
|
71 |
lemma star_semi2: "(c1,s) \<rightarrow>* (c1',s') \<Longrightarrow> (c1;c2,s) \<rightarrow>* (c1';c2,s')"
|
45015
|
72 |
proof(induction rule: star_induct)
|
43141
|
73 |
case refl thus ?case by simp
|
|
74 |
next
|
|
75 |
case step
|
|
76 |
thus ?case by (metis Semi2 star.step)
|
|
77 |
qed
|
|
78 |
|
|
79 |
lemma semi_comp:
|
|
80 |
"\<lbrakk> (c1,s1) \<rightarrow>* (SKIP,s2); (c2,s2) \<rightarrow>* (SKIP,s3) \<rbrakk>
|
|
81 |
\<Longrightarrow> (c1;c2, s1) \<rightarrow>* (SKIP,s3)"
|
|
82 |
by(blast intro: star.step star_semi2 star_trans)
|
|
83 |
|
|
84 |
text{* The following proof corresponds to one on the board where one would
|
45218
|
85 |
show chains of @{text "\<rightarrow>"} and @{text "\<rightarrow>*"} steps. *}
|
43141
|
86 |
|
|
87 |
lemma big_to_small:
|
|
88 |
"cs \<Rightarrow> t \<Longrightarrow> cs \<rightarrow>* (SKIP,t)"
|
45015
|
89 |
proof (induction rule: big_step.induct)
|
43141
|
90 |
fix s show "(SKIP,s) \<rightarrow>* (SKIP,s)" by simp
|
|
91 |
next
|
|
92 |
fix x a s show "(x ::= a,s) \<rightarrow>* (SKIP, s(x := aval a s))" by auto
|
|
93 |
next
|
|
94 |
fix c1 c2 s1 s2 s3
|
|
95 |
assume "(c1,s1) \<rightarrow>* (SKIP,s2)" and "(c2,s2) \<rightarrow>* (SKIP,s3)"
|
|
96 |
thus "(c1;c2, s1) \<rightarrow>* (SKIP,s3)" by (rule semi_comp)
|
|
97 |
next
|
|
98 |
fix s::state and b c0 c1 t
|
|
99 |
assume "bval b s"
|
|
100 |
hence "(IF b THEN c0 ELSE c1,s) \<rightarrow> (c0,s)" by simp
|
45218
|
101 |
moreover assume "(c0,s) \<rightarrow>* (SKIP,t)"
|
|
102 |
ultimately
|
|
103 |
show "(IF b THEN c0 ELSE c1,s) \<rightarrow>* (SKIP,t)" by (metis star.simps)
|
43141
|
104 |
next
|
|
105 |
fix s::state and b c0 c1 t
|
|
106 |
assume "\<not>bval b s"
|
|
107 |
hence "(IF b THEN c0 ELSE c1,s) \<rightarrow> (c1,s)" by simp
|
45218
|
108 |
moreover assume "(c1,s) \<rightarrow>* (SKIP,t)"
|
|
109 |
ultimately
|
|
110 |
show "(IF b THEN c0 ELSE c1,s) \<rightarrow>* (SKIP,t)" by (metis star.simps)
|
43141
|
111 |
next
|
|
112 |
fix b c and s::state
|
|
113 |
assume b: "\<not>bval b s"
|
|
114 |
let ?if = "IF b THEN c; WHILE b DO c ELSE SKIP"
|
|
115 |
have "(WHILE b DO c,s) \<rightarrow> (?if, s)" by blast
|
45218
|
116 |
moreover have "(?if,s) \<rightarrow> (SKIP, s)" by (simp add: b)
|
45265
|
117 |
ultimately show "(WHILE b DO c,s) \<rightarrow>* (SKIP,s)" by(metis star.refl star.step)
|
43141
|
118 |
next
|
|
119 |
fix b c s s' t
|
|
120 |
let ?w = "WHILE b DO c"
|
|
121 |
let ?if = "IF b THEN c; ?w ELSE SKIP"
|
|
122 |
assume w: "(?w,s') \<rightarrow>* (SKIP,t)"
|
|
123 |
assume c: "(c,s) \<rightarrow>* (SKIP,s')"
|
|
124 |
assume b: "bval b s"
|
|
125 |
have "(?w,s) \<rightarrow> (?if, s)" by blast
|
45218
|
126 |
moreover have "(?if, s) \<rightarrow> (c; ?w, s)" by (simp add: b)
|
|
127 |
moreover have "(c; ?w,s) \<rightarrow>* (SKIP,t)" by(rule semi_comp[OF c w])
|
|
128 |
ultimately show "(WHILE b DO c,s) \<rightarrow>* (SKIP,t)" by (metis star.simps)
|
43141
|
129 |
qed
|
|
130 |
|
|
131 |
text{* Each case of the induction can be proved automatically: *}
|
|
132 |
lemma "cs \<Rightarrow> t \<Longrightarrow> cs \<rightarrow>* (SKIP,t)"
|
45015
|
133 |
proof (induction rule: big_step.induct)
|
43141
|
134 |
case Skip show ?case by blast
|
|
135 |
next
|
|
136 |
case Assign show ?case by blast
|
|
137 |
next
|
|
138 |
case Semi thus ?case by (blast intro: semi_comp)
|
|
139 |
next
|
45265
|
140 |
case IfTrue thus ?case by (blast intro: star.step)
|
43141
|
141 |
next
|
45265
|
142 |
case IfFalse thus ?case by (blast intro: star.step)
|
43141
|
143 |
next
|
|
144 |
case WhileFalse thus ?case
|
45265
|
145 |
by (metis star.step star_step1 small_step.IfFalse small_step.While)
|
43141
|
146 |
next
|
|
147 |
case WhileTrue
|
|
148 |
thus ?case
|
45265
|
149 |
by(metis While semi_comp small_step.IfTrue star.step[of small_step])
|
43141
|
150 |
qed
|
|
151 |
|
|
152 |
lemma small1_big_continue:
|
|
153 |
"cs \<rightarrow> cs' \<Longrightarrow> cs' \<Rightarrow> t \<Longrightarrow> cs \<Rightarrow> t"
|
45015
|
154 |
apply (induction arbitrary: t rule: small_step.induct)
|
43141
|
155 |
apply auto
|
|
156 |
done
|
|
157 |
|
|
158 |
lemma small_big_continue:
|
|
159 |
"cs \<rightarrow>* cs' \<Longrightarrow> cs' \<Rightarrow> t \<Longrightarrow> cs \<Rightarrow> t"
|
45015
|
160 |
apply (induction rule: star.induct)
|
43141
|
161 |
apply (auto intro: small1_big_continue)
|
|
162 |
done
|
|
163 |
|
|
164 |
lemma small_to_big: "cs \<rightarrow>* (SKIP,t) \<Longrightarrow> cs \<Rightarrow> t"
|
|
165 |
by (metis small_big_continue Skip)
|
|
166 |
|
|
167 |
text {*
|
|
168 |
Finally, the equivalence theorem:
|
|
169 |
*}
|
|
170 |
theorem big_iff_small:
|
|
171 |
"cs \<Rightarrow> t = cs \<rightarrow>* (SKIP,t)"
|
|
172 |
by(metis big_to_small small_to_big)
|
|
173 |
|
|
174 |
|
|
175 |
subsection "Final configurations and infinite reductions"
|
|
176 |
|
|
177 |
definition "final cs \<longleftrightarrow> \<not>(EX cs'. cs \<rightarrow> cs')"
|
|
178 |
|
|
179 |
lemma finalD: "final (c,s) \<Longrightarrow> c = SKIP"
|
|
180 |
apply(simp add: final_def)
|
45015
|
181 |
apply(induction c)
|
43141
|
182 |
apply blast+
|
|
183 |
done
|
|
184 |
|
|
185 |
lemma final_iff_SKIP: "final (c,s) = (c = SKIP)"
|
|
186 |
by (metis SkipE finalD final_def)
|
|
187 |
|
|
188 |
text{* Now we can show that @{text"\<Rightarrow>"} yields a final state iff @{text"\<rightarrow>"}
|
|
189 |
terminates: *}
|
|
190 |
|
|
191 |
lemma big_iff_small_termination:
|
|
192 |
"(EX t. cs \<Rightarrow> t) \<longleftrightarrow> (EX cs'. cs \<rightarrow>* cs' \<and> final cs')"
|
|
193 |
by(simp add: big_iff_small final_iff_SKIP)
|
|
194 |
|
|
195 |
text{* This is the same as saying that the absence of a big step result is
|
|
196 |
equivalent with absence of a terminating small step sequence, i.e.\ with
|
|
197 |
nontermination. Since @{text"\<rightarrow>"} is determininistic, there is no difference
|
|
198 |
between may and must terminate. *}
|
|
199 |
|
|
200 |
end
|