Debian Codegraph @

src/Util/Loops.v

raw
1 From Coq Require Import Lia.
2
3 Module Import core.
4 Section Loops.
5 Context {A B : Type} (body : A -> A + B).
6
7 (* the fuel parameter is only present to allow defining a loop
8 without proving its termination. The loop body does not have
9 access to the amount of remaining fuel, and thus increasing fuel
10 beyond termination cannot change the behavior. fuel counts full
11 loops -- the one that executes "break" is not included *)
12
13 Fixpoint loop (fuel : nat) (s : A) {struct fuel} : A + B :=
14 let s := body s in
15 match s with
16 | inl a =>
17 match fuel with
18 | O => inl a
19 | S fuel' => loop fuel' a
20 end
21 | inr b => inr b
22 end.
23
24
25 Context (body_cps : A -> forall T, (A + B -> T) -> T).
26
27 Fixpoint loop_cps (fuel : nat) (s : A) {struct fuel} : forall T, (A + B -> T) -> T :=
28 body_cps s _ (fun s =>
29 match s with
30 | inl a =>
31 match fuel with
32 | O => fun _ k => k (inl a)
33 | S fuel' => loop_cps fuel' a
34 end
35 | inr b => fun _ k => k (inr b)
36 end).
37
38 Context (body_cps_ok : forall s {R} f, body_cps s R f = f (body s)).
39 Lemma loop_cps_ok n s {R} f : loop_cps n s R f = f (loop n s).
40 Proof.
41 revert f; revert R; revert s; induction n;
42 repeat match goal with
43 | _ => progress intros
44 | _ => progress cbv [cpsreturn cpscall] in *
45 | _ => progress cbn
46 | _ => progress rewrite ?body_cps_ok
47 | _ => progress rewrite ?IHn
48 | |- context [body s] => destruct (body s) eqn:?
49 | _ => reflexivity
50 end.
51 Qed.
52
53 Context (body_cps2 : A -> forall {R}, (A -> R) -> (B -> R) -> R).
54 Fixpoint loop_cps2 (fuel : nat) (s : A) {R} (timeout:A->R) (ret:B->R) {struct fuel} : R :=
55 body_cps2 s R
56 (fun a =>
57 match fuel with
58 | O => timeout a
59 | S fuel' => @loop_cps2 fuel' a R timeout ret
60 end)
61 (fun b => ret b).
62
63 Context (body_cps2_ok : forall s {R} continue break,
64 body_cps2 s R continue break =
65 match body s with
66 | inl a => continue a
67 | inr b => break b
68 end).
69 Lemma loop_cps2_ok n s {R} (timeout ret : _ -> R) :
70 @loop_cps2 n s R timeout ret =
71 match loop n s with
72 | inl a => timeout a
73 | inr b => ret b
74 end.
75 Proof.
76 revert timeout; revert ret; revert R; revert s; induction n;
77 repeat match goal with
78 | _ => progress intros
79 | _ => progress cbv [cpsreturn cpscall] in *
80 | _ => progress cbn
81 | _ => progress rewrite ?body_cps2_ok
82 | _ => progress rewrite ?IHn
83 | _ => progress subst
84 | |- context [body s] => destruct (body s) eqn:?
85 | _ => reflexivity
86 end.
87 Qed.
88
89 Local Lemma loop_fuel_0 s : loop 0 s = body s.
90 Proof. cbv; destruct (body s); reflexivity. Qed.
91
92 Local Lemma loop_fuel_S_first n s : loop (S n) s =
93 match body s with
94 | inl a => loop n a
95 | inr b => inr b
96 end.
97 Proof. reflexivity. Qed.
98
99 Local Lemma loop_fuel_S_last n s : loop (S n) s =
100 match loop n s with
101 | inl a => body a
102 | inr b => loop n s
103 end.
104 Proof.
105 revert s; induction n; cbn; intros s.
106 { repeat destruct (body _); reflexivity. }
107 { destruct (body s); cbn; rewrite <-?IHn; reflexivity. }
108 Qed.
109
110 Local Lemma loop_fuel_S_stable n s b (H : loop n s = inr b) : loop (S n) s = inr b.
111 Proof.
112 revert H; revert b; revert s; induction n; intros ? ? H.
113 { cbn [loop nat_rect] in *; destruct (body s); congruence. }
114 { rewrite loop_fuel_S_last; destruct (loop (S n) s); congruence. }
115 Qed.
116
117 Local Lemma loop_fuel_add_stable n m s b (H : loop n s = inr b) : loop (m+n) s = inr b.
118 Proof.
119 induction m; intros.
120 { rewrite PeanoNat.Nat.add_0_l. assumption. }
121 { rewrite PeanoNat.Nat.add_succ_l.
122 erewrite loop_fuel_S_stable; eauto. }
123 Qed.
124
125 Lemma loop_fuel_irrelevant n m s bn bm
126 (Hn : loop n s = inr bn)
127 (Hm : loop m s = inr bm)
128 : bn = bm.
129 Proof.
130 destruct (Compare_dec.le_le_S_dec n m) as [H|H];
131 destruct (PeanoNat.Nat.le_exists_sub _ _ H) as [d [? _]]; subst.
132 { erewrite loop_fuel_add_stable in Hm by eassumption; congruence. }
133 { erewrite loop_fuel_add_stable in Hn.
134 { inversion Hn. reflexivity. }
135 { erewrite loop_fuel_S_stable by eassumption. congruence. } }
136 Qed.
137
138 Local Lemma by_invariant_fuel' (inv:_->Prop) measure P f s0
139 (init : inv s0 /\ measure s0 <= f)
140 (step : forall s, inv s -> match body s with
141 | inl s' => inv s' /\ measure s' < measure s
142 | inr s' => P s'
143 end)
144 : match loop f s0 with
145 | inl a => False
146 | inr s => P s
147 end.
148 Proof.
149 revert dependent s0; induction f; intros.
150 { specialize (step s0 (proj1 init)); cbv. destruct (body _); [lia|assumption]. }
151 { rewrite loop_fuel_S_first.
152 specialize (step s0 (proj1 init)); destruct (body s0); [|assumption].
153 destruct step.
154 exact (IHf a ltac:(split; (assumption || lia))). }
155 Qed.
156
157 Lemma by_invariant_fuel (inv:_->Prop) measure P f s0
158 (init : inv s0 /\ measure s0 <= f)
159 (step : forall s, inv s -> match body s with
160 | inl s' => inv s' /\ measure s' < measure s
161 | inr s' => P s'
162 end)
163 : exists b, loop f s0 = inr b /\ P b.
164 Proof.
165 pose proof (by_invariant_fuel' inv measure P f s0);
166 destruct (loop f s0); [exfalso|]; eauto.
167 Qed.
168
169 Lemma by_invariant (inv:_->Prop) measure P s0
170 (init : inv s0)
171 (step : forall s, inv s -> match body s with
172 | inl s' => inv s' /\ measure s' < measure s
173 | inr s' => P s'
174 end)
175 : exists b, loop (measure s0) s0 = inr b /\ P b.
176 Proof. eapply by_invariant_fuel; eauto. Qed.
177
178 (* Completeness proof *)
179
180 Definition iterations_required fuel s : option nat :=
181 nat_rect _ None
182 (fun n r =>
183 match r with
184 | Some _ => r
185 | None =>
186 match loop n s with
187 | inl a => None
188 | inr b => Some n
189 end
190 end
191 ) fuel.
192
193 Lemma iterations_required_correct fuel s :
194 (forall m, iterations_required fuel s = Some m ->
195 m < fuel /\
196 exists b, forall n, (n < m -> exists a, loop n s = inl a) /\ (m <= n -> loop n s = inr b))
197 /\
198 (iterations_required fuel s = None -> forall n, n < fuel -> exists a, loop n s = inl a).
199 Proof.
200 induction fuel; intros.
201 { cbn. split; [congruence|intros; lia]. }
202 { change (iterations_required (S fuel) s)
203 with (match iterations_required fuel s with
204 | None => match loop fuel s with
205 | inl _ => None
206 | inr _ => Some fuel
207 end
208 | Some _ => iterations_required fuel s
209 end) in *.
210 destruct (iterations_required fuel s) in *.
211 { split; intros ? H; [ inversion H; subst | congruence ].
212 destruct (proj1 IHfuel _ eq_refl); split; [lia|assumption]. }
213 { destruct (loop fuel s) eqn:HSf; split; intros; try congruence.
214 { destruct (PeanoNat.Nat.eq_dec n fuel); subst; eauto; [].
215 eapply IHfuel; lia || congruence. }
216 { split; match goal with H:Some _=Some _|-_=>inversion H end; [lia|].
217 exists b; intros; split; intros.
218 { eapply IHfuel; congruence || lia. }
219 { destruct (PeanoNat.Nat.le_exists_sub m n ltac:(assumption)) as [?[]]; subst.
220 eauto using loop_fuel_add_stable. } } } }
221 Qed.
222
223 Lemma iterations_required_step fuel s s' n
224 (Hs : iterations_required fuel s = Some (S n))
225 (Hstep : body s = inl s')
226 : iterations_required fuel s' = Some n.
227 Proof.
228 eapply iterations_required_correct in Hs.
229 destruct Hs as [Hn [b Hs]].
230 pose proof (proj2 (Hs (S n)) ltac:(lia)) as H.
231 rewrite loop_fuel_S_first, Hstep in H.
232 destruct (iterations_required fuel s') as [x|] eqn:Hs' in *; [f_equal|exfalso].
233 { eapply iterations_required_correct in Hs'; destruct Hs' as [Hx Hs'].
234 destruct Hs' as [b' Hs'].
235 destruct (Compare_dec.le_lt_dec n x) as [Hc|Hc].
236 { destruct (Compare_dec.le_lt_dec x n) as [Hc'|Hc']; try lia; [].
237 destruct (proj1 (Hs' n) Hc'); congruence. }
238 { destruct (proj1 (Hs (S x)) ltac:(lia)) as [? HX].
239 rewrite loop_fuel_S_first, Hstep in HX.
240 pose proof (proj2 (Hs' x) ltac:(lia)).
241 congruence. } }
242 { eapply iterations_required_correct in Hs'; [|exact Hn].
243 destruct Hs' as [? Hs']; rewrite loop_fuel_S_last, H in Hs'; congruence. }
244 Qed.
245
246 Local Lemma invariant_complete (P:_->Prop) f s0 b (H:loop f s0 = inr b) (HP:P b)
247 : exists inv measure,
248 (inv s0 /\ measure s0 <= f)
249 /\ forall s, inv s -> match body s with
250 | inl s' => inv s' /\ measure s' < measure s
251 | inr s' => P s'
252 end.
253 Proof.
254 set (measure s := match iterations_required (S f) s with None => 0 | Some n => n end).
255 exists (fun s => match loop (measure s) s with
256 | inl a => False
257 | inr r => r = b end).
258 exists (measure); split; [ |repeat match goal with |- _ /\ _ => split end..].
259 { cbv [measure].
260 destruct (iterations_required (S f) s0) eqn:Hs0;
261 eapply iterations_required_correct in Hs0;
262 [ .. | exact (ltac:(lia):f <S f)]; [|destruct Hs0; congruence].
263 destruct Hs0 as [? [? Hs0]]; split; [|lia].
264 pose proof (proj2 (Hs0 n) ltac:(lia)) as HH; rewrite HH.
265 exact (loop_fuel_irrelevant _ _ _ _ _ HH H). }
266 { intros s Hinv; destruct (body s) as [s'|c] eqn:Hstep.
267 { destruct (loop (measure s) s) eqn:Hs; [contradiction|subst].
268 cbv [measure] in *.
269 destruct (iterations_required (S f) s) eqn:Hs' in *; try destruct n;
270 try (rewrite loop_fuel_0 in Hs; congruence); [].
271 pose proof (iterations_required_step _ _ s' _ Hs' Hstep) as HA.
272 rewrite HA.
273 edestruct (proj1 (iterations_required_correct _ _) _ HA) as [? [? [? HE']]].
274 pose proof (HE' ltac:(constructor)) as HE; clear HE'.
275 split; [|lia].
276 rewrite loop_fuel_S_first, Hstep in Hs.
277 destruct (loop n s'); congruence. }
278 { destruct (loop (measure s) s) eqn:Hs; [contradiction|].
279 assert (HH: loop 1 s = inr c) by (cbn; rewrite Hstep; reflexivity).
280 rewrite (loop_fuel_irrelevant _ _ _ _ _ HH Hs); congruence. } }
281 Qed.
282
283 Lemma invariant_iff P f s0 :
284 (exists b, loop f s0 = inr b /\ P b)
285 <->
286 (exists inv measure,
287 (inv s0 /\ measure s0 <= f)
288 /\ forall s, inv s -> match body s with
289 | inl s' => inv s' /\ measure s' < measure s
290 | inr s' => P s'
291 end).
292 Proof.
293 split; [intros [? []] | intros [? [? []]] ];
294 eauto using invariant_complete, by_invariant_fuel.
295 Qed.
296 End Loops.
297
298 Global Arguments loop_cps_ok {A B body body_cps}.
299 Global Arguments loop_cps2_ok {A B body body_cps2}.
300 Global Arguments by_invariant_fuel {A B body} inv measure P.
301 Global Arguments by_invariant {A B body} inv measure P.
302 Global Arguments invariant_iff {A B body} P f s0.
303 Global Arguments iterations_required_correct {A B body} fuel s.
304 End core.
305
306 Module default.
307 Section Default.
308 Context {A B} (default : B) (body : A -> A + B).
309 Definition loop fuel s : B :=
310 match loop body fuel s with
311 | inl s => default
312 | inr s => s
313 end.
314
315 Lemma by_invariant_fuel inv measure (P:_->Prop) f s0
316 (init : inv s0 /\ measure s0 <= f)
317 (step: forall s, inv s -> match body s with
318 | inl s' => inv s' /\ measure s' < measure s
319 | inr s' => P s'
320 end)
321 : P (loop f s0).
322 Proof.
323 edestruct (by_invariant_fuel (body:=body) inv measure P f s0) as [x [HA HB]]; eauto; [].
324 apply (f_equal (fun r : A + B => match r with inl s => default | inr s => s end)) in HA.
325 cbv [loop]; destruct (core.loop body f s0); congruence.
326 Qed.
327
328 Lemma by_invariant (inv:_->Prop) measure P s0
329 (init : inv s0)
330 (step: forall s, inv s -> match body s with
331 | inl s' => inv s' /\ measure s' < measure s
332 | inr s' => P s'
333 end)
334 : P (loop (measure s0) s0).
335 Proof. eapply by_invariant_fuel; eauto. Qed.
336 End Default.
337 Global Arguments by_invariant_fuel {A B default body} inv measure P.
338 Global Arguments by_invariant {A B default body} inv measure P.
339 End default.
340
341 Module silent.
342 Section Silent.
343 Context {state} (body : state -> state + state).
344 Definition loop fuel s : state :=
345 match loop body fuel s with
346 | inl s => s
347 | inr s => s
348 end.
349
350 Lemma by_invariant_fuel inv measure (P:_->Prop) f s0
351 (init : inv s0 /\ measure s0 <= f)
352 (step: forall s, inv s -> match body s with
353 | inl s' => inv s' /\ measure s' < measure s
354 | inr s' => P s'
355 end)
356 : P (loop f s0).
357 Proof.
358 edestruct (by_invariant_fuel (body:=body) inv measure P f s0) as [x [A B]]; eauto; [].
359 apply (f_equal (fun r : state + state => match r with inl s => s | inr s => s end)) in A.
360 cbv [loop]; destruct (core.loop body f s0); congruence.
361 Qed.
362
363 Lemma by_invariant (inv:_->Prop) measure P s0
364 (init : inv s0)
365 (step: forall s, inv s -> match body s with
366 | inl s' => inv s' /\ measure s' < measure s
367 | inr s' => P s'
368 end)
369 : P (loop (measure s0) s0).
370 Proof. eapply by_invariant_fuel; eauto. Qed.
371 End Silent.
372
373 Global Arguments by_invariant_fuel {state body} inv measure P.
374 Global Arguments by_invariant {state body} inv measure P.
375 End silent.
376
377 Module while.
378 Section While.
379 Context {state}
380 (test : state -> bool)
381 (body : state -> state).
382
383 Fixpoint while f s :=
384 if test s
385 then
386 let s := body s in
387 match f with
388 | O => s
389 | S f => while f s
390 end
391 else s.
392
393 Local Definition lbody := fun s => if test s then inl (body s) else inr s.
394
395 Lemma eq_loop f s : while f s = silent.loop lbody f s.
396 Proof.
397 revert s; induction f; intros s;
398 repeat match goal with
399 | _ => progress cbn in *
400 | _ => progress cbv [silent.loop lbody] in *
401 | _ => rewrite IHf
402 | |- context[test s] => destruct (test s)
403 | _ => congruence
404 end.
405 Qed.
406
407 Lemma by_invariant_fuel inv measure P f s0
408 (init : inv s0 /\ measure s0 <= f)
409 (step : forall s, inv s -> if test s
410 then inv (body s) /\ measure (body s) < measure s
411 else P s)
412 : P (while f s0).
413 Proof.
414 rewrite eq_loop.
415 eapply silent.by_invariant_fuel; eauto; []; intros s H; cbv [lbody].
416 specialize (step s H); destruct (test s); eauto.
417 Qed.
418
419 Lemma by_invariant (inv:_->Prop) measure P s0
420 (init : inv s0)
421 (step : forall s, inv s -> if test s
422 then inv (body s) /\ measure (body s) < measure s
423 else P s)
424 : P (while (measure s0) s0).
425 Proof. eapply by_invariant_fuel; eauto. Qed.
426
427 Context (body_cps : state -> forall T, (state -> T) -> T).
428
429 Fixpoint while_cps f s : forall T, (state -> T) -> T :=
430 if test s
431 then
432 body_cps s _ (fun s =>
433 match f with
434 | O => fun _ k => k s
435 | S f =>while_cps f s
436 end)
437 else fun _ k => k s.
438
439 Context (body_cps_ok : forall s {R} f, body_cps s R f = f (body s)).
440 Lemma loop_cps_ok n s {R} f : while_cps n s R f = f (while n s).
441 Proof.
442 revert s; induction n; intros s;
443 repeat match goal with
444 | _ => progress intros
445 | _ => progress cbv [cpsreturn cpscall] in *
446 | _ => progress cbn
447 | _ => progress rewrite ?body_cps_ok
448 | _ => progress rewrite ?IHn
449 | |- context[test s] => destruct (test s)
450 | _ => reflexivity
451 end.
452 Qed.
453 End While.
454 Global Arguments by_invariant_fuel {state test body} inv measure P.
455 Global Arguments by_invariant {state test body} inv measure P.
456
457 Section TwoLoops.
458 Context {state1 state2 : Type}
459 (test1 : state1 -> bool) (body1 : state1 -> state1)
460 (test2 : state2 -> bool) (body2 : state2 -> state2).
461
462 Lemma preservation
463 (R : state1 -> state2 -> Prop)
464 (Htest: forall s1 s2, R s1 s2 -> test1 s1 = test2 s2)
465 (Hbody: forall s1 s2, test2 s2 = true -> R s1 s2 -> R (body1 s1) (body2 s2)) :
466 forall fuel init1 init2,
467 R init1 init2 ->
468 R (while test1 body1 fuel init1) (while test2 body2 fuel init2).
469 Proof.
470 induction fuel; intros; cbn [while];
471 erewrite Htest by eauto; case_eq (test2 init2); auto.
472 Qed.
473 End TwoLoops.
474 End while.
475 Notation while := while.while.
476
477 Definition for2 {state} (test : state -> bool) (increment body : state -> state)
478 := while test (fun s => let s := body s in increment s).
479
480 Definition for3 {state} init test increment body fuel :=
481 @for2 state test increment body fuel init.