Debian Codegraph @

src/Util/OptionList.v

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