Debian Codegraph @

src/Bedrock/Group/Loops.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 Require Import Rupicola.Lib.ControlFlow.DownTo.
4 Require Import Crypto.Util.Loops.
5 Require Import Crypto.Util.ListUtil.
6 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
7
8 (** Helper lemmas for proving equivalence between different loop formats
9 (e.g. rupicola's [downto] and Util/Loops.v's [while] **)
10
11 Section DownToWhile.
12 Lemma downto'_while
13 {state} (count : nat)
14 (step : state -> nat -> state) :
15 forall start init,
16 downto' init start count step =
17 fst (while (fun '(_, i) => (i >=? Z.of_nat start)%Z)
18 (fun '(s, i) => (step s (Z.to_nat i), Z.pred i))
19 count (init, Z.pred (Z.of_nat count))).
20 Proof.
21 induction count; intros.
22 { destruct start; [ reflexivity | ].
23 cbn; autorewrite with push_skipn; reflexivity. }
24 { rewrite Nat2Z.inj_succ, Z.pred_succ.
25 cbn [while]; break_match;
26 LtbToLt.Z.ltb_to_lt;
27 match goal with
28 | H : (Z.of_nat _ < Z.of_nat _)%Z |- _ =>
29 apply Nat2Z.inj_lt in H
30 | H : (Z.of_nat _ >= Z.of_nat _)%Z |- _ =>
31 apply Nat2Z.inj_ge in H
32 end; [ | ].
33 { rewrite <-IHcount, Nat2Z.id.
34 cbv [downto']. rewrite seq_snoc.
35 autorewrite with natsimplify push_skipn distr_length.
36 rewrite rev_app_distr, fold_left_app.
37 reflexivity. }
38 { cbv [downto']. autorewrite with push_skipn.
39 reflexivity. } }
40 Qed.
41
42 Lemma downto_while
43 {state} (count : nat)
44 (step : state -> nat -> state) :
45 forall init : state,
46 downto init count step =
47 fst (while (fun '(_, i) => (i >=? 0)%Z)
48 (fun '(s, i) => (step s (Z.to_nat i), Z.pred i))
49 count (init, Z.pred (Z.of_nat count))).
50 Proof. apply downto'_while. Qed.
51 End DownToWhile.