-
Notifications
You must be signed in to change notification settings - Fork 0
/
TitleCaseExample.v
executable file
·372 lines (341 loc) · 11.4 KB
/
TitleCaseExample.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
Require Import Coq.Program.Syntax.
Require Export ZArith.
Require Import Coq.Program.Basics.
Require Import SetoidTactics.
Require Import SetoidClass.
Require Import Coq.Strings.String.
Require Import Coq.Strings.Ascii.
Require Import Coq.Lists.List.
Require Import HDI.Util.
Require Import HDI.Syntax.
Require Import HDI.Heap.
Require Import HDI.OpSem.
Require Import HDI.Bisimulation.
Require HDI.HoareDoubles.
Require HDI.HoareDoublesI.
Local Open Scope Z.
Local Open Scope stmt.
Local Open Scope char.
Local Open Scope bool.
Section Implementation.
CoFixpoint parseWS a : stmt:=
x <- read a;
if x =? zero then
ret 1 (* Title Case *)
else if x =? " " then
parseWS (1+a)
else if (negb (is_letterZ x)) || (is_upperZ x) then
parseLetters (1+a)
else
ret 0 (* not type case *)
with parseLetters (a:Z) :=
x <- read a;
if x =? zero then
ret 1 (* Title Case *)
else if x =? " " then
parseWS (1+a)
else if negb (is_letterZ x) || is_lowerZ x then
parseLetters (1+a)
else
ret 0 (* cAps in woRd *).
Definition isTitleCase := parseWS.
Lemma unfold_parseWS: forall a,
parseWS a =
x <- read a;
if x =? zero then
ret 1 (* Title Case *)
else if x =? " " then
parseWS (1+a)
else if (negb (is_letterZ x)) || (is_upperZ x) then
parseLetters (1+a)
else
ret 0 (* not type case *).
Proof. intros; rewrite (unfold_stmt_eq (parseWS a)); reflexivity. Qed.
Lemma unfold_parseLetters: forall a,
parseLetters a =
x <- read a;
if x =? zero then
ret 1 (* Title Case *)
else if x =? " " then
parseWS (1+a)
else if negb (is_letterZ x) || is_lowerZ x then
parseLetters (1+a)
else
ret 0 (* cAps in woRd *).
Proof. intros; rewrite (unfold_stmt_eq (parseLetters a)); reflexivity. Qed.
End Implementation.
Section Verification.
Fixpoint is_title_case_ (in_word: bool) (s: string) {struct s} : bool :=
match s with
| String x s' =>
if ascii_dec x zero then
true (* Title Case *)
else if ascii_dec x " " then
is_title_case_ false s'
else if in_word then
if negb (is_letter x) || is_lower x then
is_title_case_ true s'
else
false (* cAps in woRd *)
else
let check_word x s :=
if (negb (is_letter x)) || (is_upper x) then
is_title_case_ true s'
else
false (* not type case *)
in
check_word x s'
| empty => true (* Title Case *)
end.
Definition is_title_case s := is_title_case_ false s.
Goal is_title_case "" = true. reflexivity. Abort.
Goal is_title_case " " = true. reflexivity. Abort.
Goal is_title_case "67" = true. reflexivity. Abort.
Goal is_title_case "A" = true. reflexivity. Abort.
Goal is_title_case "z" = false. reflexivity. Abort.
Goal is_title_case "Hello World" = true. reflexivity. Abort.
Goal is_title_case "Hello world" = false. reflexivity. Abort.
Goal is_title_case "hello World" = false. reflexivity. Abort.
Goal is_title_case "HeLlo World" = false. reflexivity. Abort.
Goal is_title_case " Hello World " = true. reflexivity. Abort.
Goal is_title_case "8ello World" = true. reflexivity. Abort.
Goal is_title_case "8ellO World" = false. reflexivity. Abort.
Lemma is_title_case_in_word: forall s,
is_title_case_ true s = is_title_case (String "A" s).
Proof. intros; reflexivity. Qed.
Section HD.
Import HoareDoubles.
Lemma isTitleCase_ok: forall a s F k,
|-{{a |->0 s && F}} k (if is_title_case s then 1 else 0) ->
|-{{a |->0 s && F}} isTitleCase a >>= k.
Proof.
Abort.
End HD.
Import HoareDoublesI.
Require Import CoInduction.
Definition tc_ret s := if is_title_case s then 1 else 0.
Lemma parseLetters_ind_ok: forall (I: predicate heap -> stmt -> Prop) a s F k,
I ||= {{a |->0 s && F}} k (tc_ret ("A"++s)%string) ->
(forall a s0 s' F,
append s0 s' = s ->
I ||= {{a |->0 s' && F}} k (tc_ret s') ->
I ||- {{a |->0 s' && F}} (parseWS a >>= k)) ->
I ||- {{a |->0 s && F}} parseLetters a >>= k.
Proof.
intros I a s F k.
intros Hsafe_k Hsafe_sws.
unfold tc_ret in Hsafe_k; simpl in Hsafe_k.
rewrite <-is_title_case_in_word in Hsafe_k.
revert a F Hsafe_k.
induction s; simpl; intros.
* rewrite unfold_parseLetters.
step.
mcase_eq.
step; auto.
*
(* x <- parseLetters a; k x *)
rewrite unfold_parseLetters.
(* x <- act (read a); *)
step.
(* (if x =? Z_of_ascii zero *)
mcase_eq.
(* then ret 1 *)
{ apply Z_of_ascii_inv in H; subst a.
step; auto.
}
(* else if x =? Z_of_ascii " " *)
destruct (ascii_dec a zero); subst.
congruence.
destruct (ascii_dec a " "); subst.
(* then parseWS (1 + a) *)
{ rewrite Z.eqb_refl.
rewrite str0_cons, (inter_comm ((1+a0) |->0 _)), <-!inter_assoc in Hsafe_k |- *.
apply hd'_safe.
eapply (Hsafe_sws (1+a0) " "%string s); auto.
}
rewrite (proj2 (Z.eqb_neq _ _)); [ | apply Z_of_ascii_neq; auto ].
(* else if negb (is_letterZ x) || is_lowerZ x *)
rewrite is_letterZ_of_ascii, is_lowerZ_of_ascii.
rewrite is_letter_eq in Hsafe_k |- *.
rewrite Bool.negb_orb, Bool.orb_andb_distrib_l, negb_orb_cancel_l in Hsafe_k |- *.
rewrite Bool.andb_true_r in Hsafe_k |- *.
mcase_eq.
(* then parseLetters (1 + a) *)
{ rewrite str0_cons, (inter_comm ((1+a0) |->0 _)), <-!inter_assoc in Hsafe_k |- *.
apply hd'_safe.
apply IHs; intros; auto.
eapply Hsafe_sws with (s0:=String a s0); auto.
subst s.
reflexivity.
}
(* else ret 0 >>= k *)
step; assumption.
Qed.
Lemma parseWS_ind_ok: forall I a s F k,
I ||= {{a |->0 s && F}} k (tc_ret s) ->
I ||- {{a |->0 s && F}} parseWS a >>= k.
Proof.
intros I a s F k Hsafe_k.
unfold tc_ret, is_title_case in Hsafe_k.
revert Hsafe_k.
remember (append EmptyString s) as s1.
revert Heqs1.
generalize EmptyString as s0.
revert a s F.
induction s1; simpl; intros; subst.
* rewrite unfold_parseWS.
destruct s0, s; try solve [inversion Heqs1].
step.
mcase_eq.
step; auto.
*
(* x <- parseWS a; k x *)
rewrite unfold_parseWS.
(* x <- read a *)
step.
(* if x =? Z_of_ascii zero *)
mcase_eq.
(* then ret 1 *)
{ step; auto.
repeat mcase_eq in *.
destruct s0; inversion Heqs1; subst; eauto.
apply Z_of_ascii_inv in H; subst; auto.
apply Z_of_ascii_inv in H; subst; auto.
simpl in H0; discriminate.
}
(* else if x =? " " *)
destruct s.
compute in H; exfalso; eauto.
rewrite str0_cons, (inter_comm ((1+a0) |->0 _)), <-!inter_assoc in Hsafe_k |- *.
rewrite is_letterZ_of_ascii, is_upperZ_of_ascii.
unfold is_title_case_ in Hsafe_k.
fold is_title_case_ in Hsafe_k.
destruct (ascii_dec a1 zero); subst.
congruence.
destruct (ascii_dec a1 " "); subst.
(* then parseWS (1+a) *)
{ rewrite Z.eqb_refl.
apply hd'_safe; eauto.
destruct s0; inversion Heqs1; subst; eauto.
eapply IHs1 with (s1:=""%string); auto.
eapply IHs1 with (s2:= (s0++" ")%string); auto.
clear.
induction s0; simpl in *; congruence.
}
(* else if (negb (is_letterZ x)) || (is_upperZ x) *)
rewrite (proj2 (Z.eqb_neq _ _)); [ | apply Z_of_ascii_neq; auto ].
repeat mcase_eq.
(* then parseLetters (1+a) *)
apply hd'_safe, parseLetters_ind_ok; intros; subst; eauto.
destruct s0; inversion Heqs1; subst; eauto.
eapply IHs1 with (s1:=(s0++String a1 s2)%string); eauto.
clear; induction s0; simpl in *; congruence.
(* else ret 0 (* not type case *) *)
step; auto.
Qed.
Lemma parseLetters_ok: forall (I0 I: predicate heap -> stmt -> Prop) a s F k,
I0 ||= {{a |->0 s && F}} k (tc_ret ("A"++s)%string) ->
incl_inv I0 I ->
(forall a s F,
I0 ||= {{a |->0 s && F}} k (tc_ret s) ->
I ||= {{a |->0 s && F}} parseWS a >>= k) ->
I ||- {{a |->0 s && F}} parseLetters a >>= k.
Proof.
intros I0 I a s F k.
intros Hsafe_k HI0 Hsafe_sws.
unfold tc_ret in Hsafe_k; simpl in Hsafe_k.
rewrite <-is_title_case_in_word in Hsafe_k.
revert a s F Hsafe_k.
hd_coind.
(* x <- parseLetters a; k x *)
rewrite unfold_parseLetters.
(* x <- act (read a); *)
step.
(* if x =? Z_of_ascii zero *)
mcase_eq.
(* then ret 1 *)
{ rewrite HI0, H0 in Hsafe_k.
destruct s; simpl in *.
step; auto.
apply Z_of_ascii_inv in H1; subst.
simpl in *.
step; auto.
}
destruct s.
compute in H1; congruence.
(* else if x =? Z_of_ascii " " *)
unfold is_title_case in Hsafe_k.
simpl in Hsafe_k.
destruct (ascii_dec a0 zero); subst.
congruence.
destruct (ascii_dec a0 " "); subst.
(* then parseWS (1 + a) *)
{ rewrite Z.eqb_refl.
rewrite str0_cons, (inter_comm ((1+a) |->0 _)), <-!inter_assoc in Hsafe_k |- *.
rewrite <-H0.
apply Hsafe_sws; assumption.
}
rewrite (proj2 (Z.eqb_neq _ _)); [ | apply Z_of_ascii_neq; auto ].
(* else if negb (is_letterZ x) || is_lowerZ x *)
rewrite is_letterZ_of_ascii, is_lowerZ_of_ascii.
rewrite is_letter_eq in Hsafe_k |- *.
rewrite Bool.negb_orb, Bool.orb_andb_distrib_l, negb_orb_cancel_l in Hsafe_k |- *.
rewrite Bool.andb_true_r in Hsafe_k |- *.
mcase_eq.
(* then parseLetters (1 + a) *)
rewrite str0_cons, (inter_comm ((1+a) |->0 _)), <-!inter_assoc in Hsafe_k |- *.
eauto.
(* else ret 0 >>= k *)
rewrite <-H0, <-HI0.
step; assumption.
Qed.
Lemma parseWS_ok: forall I a s F k,
I ||= {{a |->0 s && F}} k (tc_ret s) ->
I ||- {{a |->0 s && F}} parseWS a >>= k.
Proof.
intros I a s F k Hsafe_k.
unfold tc_ret, is_title_case in Hsafe_k.
revert a s F Hsafe_k.
hd_coind.
(* x <- parseWS a; k x *)
rewrite unfold_parseWS.
(* x <- read a *)
step.
(* if x =? Z_of_ascii zero *)
mcase_eq.
(* then ret 1 *)
{ rewrite H0 in Hsafe_k.
destruct s; simpl in *.
step; auto.
apply Z_of_ascii_inv in H1; subst.
simpl in *.
step; auto.
}
destruct s.
compute in H1; congruence.
(* else if x =? " " *)
rewrite str0_cons, (inter_comm ((1+a) |->0 _)), <-!inter_assoc in Hsafe_k |- *.
simpl (is_title_case_ _ _) in Hsafe_k.
rewrite is_letterZ_of_ascii, is_upperZ_of_ascii.
destruct (ascii_dec a0 zero); subst.
congruence.
destruct (ascii_dec a0 " "); subst.
(* then parseWS (1+a) *)
rewrite Z.eqb_refl; auto.
(* else if (negb (is_letterZ x)) || (is_upperZ x) *)
rewrite (proj2 (Z.eqb_neq _ _)); [ | apply Z_of_ascii_neq; auto ].
repeat mcase_eq.
(* then parseLetters (1+a) *)
apply hd'_safe, parseLetters_ok with (I0:=I); auto.
(* else ret 0 (* not type case *) *)
rewrite <-H0; step; assumption.
Qed.
Lemma isTitleCase_ok: forall I a s F k,
I ||= {{a |->0 s && F}} k (if is_title_case s then 1 else 0) ->
I ||- {{a |->0 s && F}} isTitleCase a >>= k.
Proof.
intros I a s F k Hsafe_k.
unfold isTitleCase.
apply parseWS_ok; assumption.
Qed.
End Verification.