| 1 |
From Coq Require Import Lia. |
| 2 |
From Coq Require Import List. |
| 3 |
Require Import Crypto.Util.ErrorT. |
| 4 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 5 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 6 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 7 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 8 |
Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy. |
| 9 |
Require Import Crypto.Util.ListUtil. |
| 10 |
Require Import Crypto.Util.NatUtil. |
| 11 |
Require Import Crypto.Util.ListUtil.NthExt. |
| 12 |
Require Import Crypto.Util.Option. |
| 13 |
Require Import Crypto.Util.Notations. |
| 14 |
|
| 15 |
Module ErrorT. |
| 16 |
Module List. |
| 17 |
Fixpoint bind_list {ErrT A B} (v : list (ErrorT ErrT A)) (f : list A -> ErrorT ErrT B) : ErrorT ErrT B |
| 18 |
:= match v with |
| 19 |
| nil => f nil |
| 20 |
| x :: xs => (x <- x; bind_list xs (fun xs => f (x :: xs))) |
| 21 |
end%error%list. |
| 22 |
|
| 23 |
Definition lift {ErrT A} (ls : list (ErrorT ErrT A)) : ErrorT ErrT (list A) |
| 24 |
:= fold_right (fun x xs => x <- x; xs <- xs; Success (x :: xs))%error |
| 25 |
(Success nil) |
| 26 |
ls. |
| 27 |
|
| 28 |
Lemma bind_list_cps_id {ErrT A B} v f |
| 29 |
: @bind_list ErrT A B v f = (v <- bind_list v Success; f v)%error. |
| 30 |
Proof. |
| 31 |
revert B f; induction v as [|[x|] xs IHxs]; cbn; try reflexivity. |
| 32 |
intros; etransitivity; rewrite IHxs; [ | reflexivity ]; edestruct bind_list; reflexivity. |
| 33 |
Qed. |
| 34 |
|
| 35 |
Lemma eq_bind_list_lift {ErrT A} (ls : list (ErrorT ErrT A)) |
| 36 |
: lift ls = bind_list ls Success. |
| 37 |
Proof. |
| 38 |
cbv [lift]; induction ls as [|[x|] xs IHxs]; cbn; try reflexivity. |
| 39 |
rewrite IHxs; etransitivity; [ | rewrite bind_list_cps_id; reflexivity ]. |
| 40 |
reflexivity. |
| 41 |
Qed. |
| 42 |
|
| 43 |
Lemma lift_Success_nth_error_all {ErrT A} ol l (H : @lift ErrT A ol = Success l) |
| 44 |
: forall i a, nth_error l i = Some a -> nth_error ol i = Some (Success a). |
| 45 |
Proof. |
| 46 |
cbv [lift] in *; revert dependent l; induction ol as [|x xs IHxs], l as [|y ys], i as [|i]; cbn in *; try congruence; |
| 47 |
intros; inversion_option; subst; destruct_head' ErrorT; cbn in *; try congruence. |
| 48 |
all: edestruct fold_right; cbn in *; inversion_ErrorT; specialize (IHxs _ eq_refl). |
| 49 |
all: match goal with H : cons _ _ = cons _ _ |- _ => inversion H; clear H; subst end. |
| 50 |
all: auto. |
| 51 |
Qed. |
| 52 |
|
| 53 |
Lemma lift_Success_Forall2_iff {ErrT A} ol l |
| 54 |
: @lift ErrT A ol = Success l |
| 55 |
<-> Forall2 (fun a b => a = Success b) ol l. |
| 56 |
Proof. |
| 57 |
split; cbv [lift] in *; revert dependent l; induction ol as [|x xs IH], l as [|y ys]; try specialize (IH ys); cbn [fold_right]. |
| 58 |
all: repeat first [ solve [ eauto ] |
| 59 |
| progress intros |
| 60 |
| progress subst |
| 61 |
| progress inversion_ErrorT |
| 62 |
| progress inversion_list |
| 63 |
| progress invlist Forall2 |
| 64 |
| progress specialize_by_assumption |
| 65 |
| match goal with |
| 66 |
| [ H : ErrorT.bind ?x ?y = _ |- _ ] |
| 67 |
=> destruct x eqn:?; cbn [ErrorT.bind] in H |
| 68 |
| [ H : ?x = _ |- context[?x] ] => rewrite H |
| 69 |
end ]. |
| 70 |
Qed. |
| 71 |
|
| 72 |
Lemma lift_Success_nth_error_all_iff {ErrT A} ol l |
| 73 |
: @lift ErrT A ol = Success l |
| 74 |
<-> (List.length ol = List.length l |
| 75 |
/\ forall i a, nth_error l i = Some a -> nth_error ol i = Some (Success a)). |
| 76 |
Proof. |
| 77 |
rewrite lift_Success_Forall2_iff. |
| 78 |
split; revert dependent l; induction ol as [|x xs IH], l as [|y ys]; |
| 79 |
try specialize (IH ys). |
| 80 |
all: repeat first [ constructor |
| 81 |
| progress subst |
| 82 |
| progress invlist Forall2 |
| 83 |
| progress destruct_head'_and |
| 84 |
| progress inversion_option |
| 85 |
| progress cbn [List.length nth_error] in * |
| 86 |
| progress specialize_by_assumption |
| 87 |
| progress intros |
| 88 |
| progress inversion_nat_eq |
| 89 |
| congruence |
| 90 |
| progress specialize_under_binders_by apply conj |
| 91 |
| progress specialize_under_binders_by eassumption |
| 92 |
| match goal with |
| 93 |
| [ H : nth_error nil ?i = _ |- _ ] => is_var i; destruct i |
| 94 |
| [ H : nth_error (cons _ _) ?i = _ |- _ ] => is_var i; destruct i |
| 95 |
| [ H : forall i a, nth_error (cons _ _) i = _ -> _ |- _ ] |
| 96 |
=> pose proof (H 0 _ eq_refl); specialize (fun i => H (S i)) |
| 97 |
end ]. |
| 98 |
Qed. |
| 99 |
|
| 100 |
Lemma lift_Error_nth_error_iff {ErrT A} ol |
| 101 |
: (exists e, @lift ErrT A ol = Error e) <-> exists j e, nth_error ol j = Some (Error e). |
| 102 |
Proof. |
| 103 |
cbv [lift]; induction ol as [|[x|] xs IHxs]. |
| 104 |
all: repeat first [ progress cbn in * |
| 105 |
| apply conj |
| 106 |
| congruence |
| 107 |
| progress intros |
| 108 |
| progress destruct_head'_ex |
| 109 |
| progress split_iff |
| 110 |
| eexists (S _); cbn; now eauto |
| 111 |
| exists 0; cbn; now eauto |
| 112 |
| solve [ eauto ] |
| 113 |
| progress specialize_by eauto |
| 114 |
| progress inversion_ErrorT |
| 115 |
| progress subst |
| 116 |
| match goal with |
| 117 |
| [ H : nth_error nil ?i = _ |- _ ] => is_var i; destruct i |
| 118 |
| [ H : nth_error (cons _ _) ?i = _ |- _ ] => is_var i; destruct i |
| 119 |
| [ H : ?x = ?x -> _ |- _ ] => specialize (H eq_refl) |
| 120 |
end |
| 121 |
| match goal with |
| 122 |
| [ H : context[@fold_right ?A ?B ?f ?init ?ls] |- _ ] => destruct (@fold_right A B f init ls) |
| 123 |
end ]. |
| 124 |
Qed. |
| 125 |
|
| 126 |
Module Export Notations. |
| 127 |
Notation "A <-- X ; B" := (bind_list X (fun A => B%error)) : error_scope. |
| 128 |
End Notations. |
| 129 |
End List. |
| 130 |
End ErrorT. |
| 131 |
|
| 132 |
Export ErrorT.List.Notations. |
| 133 |
|
| 134 |
Lemma fold_right_ErrorT_seq ErrT A B f init ls v |
| 135 |
: List.fold_right (fun x y => x <- x; y <- y; Success (f x y))%error (Success init) ls = Success v |
| 136 |
<-> exists ls', List.map (@Success ErrT _) ls' = ls |
| 137 |
/\ @List.fold_right A B f init ls' = v. |
| 138 |
Proof. |
| 139 |
revert v; induction ls as [|x xs IHxs]; cbn; intros; |
| 140 |
repeat first [ apply conj |
| 141 |
| progress intros |
| 142 |
| progress subst |
| 143 |
| progress cbn in * |
| 144 |
| solve [ try (exists nil); cbn; auto ] |
| 145 |
| congruence |
| 146 |
| match goal with |
| 147 |
| [ x : ErrorT _ _ |- _ ] => destruct x |
| 148 |
| [ H : ex _ |- _ ] => destruct H |
| 149 |
| [ H : and _ _ |- _ ] => destruct H |
| 150 |
| [ H : Success _ = Success _ |- _ ] => inversion H; clear H |
| 151 |
| [ H : cons _ _ = cons _ _ |- _ ] => inversion H; clear H |
| 152 |
| [ H : List.map _ ?x = nil |- _ ] => is_var x; destruct x |
| 153 |
| [ H : List.map _ ?x = cons _ _ |- _ ] => is_var x; destruct x |
| 154 |
| [ H : forall v, iff _ _ |- _ ] |
| 155 |
=> pose proof (fun v => proj1 (H v)); pose proof (fun v => proj2 (H v)); clear H |
| 156 |
| [ |- iff _ _ ] => split |
| 157 |
| [ |- context[ErrorT.bind ?x ?y] ] => destruct x eqn:? |
| 158 |
| [ H : context[ErrorT.bind ?x ?y] |- _ ] => destruct x eqn:? |
| 159 |
| [ H : forall v, _ = _ -> _ |- _ ] => specialize (H _ ltac:(eassumption || reflexivity)) |
| 160 |
| [ H : forall v, (exists y, _ = _ /\ _ = _) -> _ |- _ ] => specialize (H _ ltac:(eexists; split; reflexivity)) |
| 161 |
| [ |- exists ls', List.map ?f ls' = (?f ?x :: List.map ?f ?xs)%list /\ _ ] |
| 162 |
=> exists (cons x xs) |
| 163 |
end ]. |
| 164 |
Qed. |