Debian Codegraph @

src/Util/Option.v

raw
1 From Coq Require Import Morphisms.
2 From Coq Require Import Relation_Definitions.
3 Require Import Crypto.Util.Tactics.BreakMatch.
4 Require Import Crypto.Util.Tactics.DestructHead.
5 Require Import Crypto.Util.Notations.
6
7 Scheme Equality for option.
8 Arguments option_beq {_} _ _ _.
9
10 Definition option_beq_hetero {A B} (AB_beq : A -> B -> bool) (x : option A) (y : option B) : bool
11 := match x, y with
12 | Some x, Some y => AB_beq x y
13 | None, None => true
14 | Some _, _
15 | None, _
16 => false
17 end.
18
19 (** In general, [lift : M (option A) -> option (M A)]. This is a bit
20 confusing for [option] because [M = option]. We want to return
21 [None] if the thing contained in the [M (option A)] is [None], and
22 say [Some] otherwise. *)
23 Definition lift {A} (x : option (option A)) : option (option A)
24 := match x with
25 | Some None => None (* the contained thing is bad/not present *)
26 | Some (Some x) => Some (Some x)
27 | None => Some None
28 end.
29
30 Notation map := option_map (only parsing). (* so we have [Option.map] *)
31
32 Definition map2 {A B C} (f : A -> B -> C) (v1 : option A) (v2 : option B) : option C
33 := match v1, v2 with
34 | None, _
35 | _, None
36 => None
37 | Some v1, Some v2 => Some (f v1 v2)
38 end.
39
40 Definition bind {A B} (v : option A) (f : A -> option B) : option B
41 := match v with
42 | Some v => f v
43 | None => None
44 end.
45
46 Definition bind2 {A B C} (v1 : option A) (v2 : option B) (f : A -> B -> option C) : option C
47 := match v1, v2 with
48 | None, _
49 | _, None
50 => None
51 | Some x1, Some x2 => f x1 x2
52 end.
53 Global Arguments bind2 {A B C} !v1 !v2 / f.
54
55 Definition sequence {A} (v1 v2 : option A) : option A
56 := match v1 with
57 | Some v => Some v
58 | None => v2
59 end.
60 Definition sequence_return {A} (v1 : option A) (v2 : A) : A
61 := match v1 with
62 | Some v => v
63 | None => v2
64 end.
65 Global Arguments sequence {A} !v1 v2.
66 Global Arguments sequence_return {A} !v1 v2.
67 Notation or_else := sequence (only parsing).
68 (* After OCaml's [value] *)
69 Notation value := sequence_return (only parsing).
70 Notation get_default := sequence_return (only parsing).
71
72 Module Export Notations.
73 Declare Scope option_scope.
74 Delimit Scope option_scope with option.
75 Bind Scope option_scope with option.
76
77 Notation "'olet' x .. y <- X ; B" := (bind X (fun x => .. (fun y => B%option) .. )) : option_scope.
78 Notation "A <- X ; B" := (bind X (fun A => B%option)) : option_scope.
79 (*Notation "A , A' <- X , X' ; B" := (bind2 X X' (fun A A' => B%option)) : option_scope.*)
80 Infix ";;" := sequence : option_scope.
81 Infix ";;;" := sequence_return : option_scope.
82 End Notations.
83 Local Open Scope option_scope.
84
85 Definition combine {A B} (x : option A) (y : option B) : option (A * B)
86 := match x, y with
87 | Some x, Some y => Some (x, y)
88 | _, _ => None
89 end.
90
91 Section Relations.
92 Definition option_eq {A B} eq (x : option A) (y : option B) :=
93 match x with
94 | None => y = None
95 | Some ax => match y with
96 | None => False
97 | Some ay => eq ax ay
98 end
99 end.
100
101 Local Ltac t :=
102 cbv; repeat (break_match || intro || intuition congruence ||
103 solve [ apply reflexivity
104 | apply symmetry; eassumption
105 | eapply transitivity; eassumption
106 | eauto ] ).
107
108 Global Instance Reflexive_option_eq {T} {R} {Reflexive_R:@Reflexive T R}
109 : Reflexive (option_eq R) | 1. Proof. t. Qed.
110
111 Lemma option_eq_sym {A B} {R1 R2 : _ -> _ -> Prop} (HR : forall v1 v2, R1 v1 v2 -> R2 v2 v1)
112 : forall v1 v2, @option_eq A B R1 v1 v2 -> option_eq R2 v2 v1.
113 Proof. t. Qed.
114
115 Lemma option_eq_trans {A B C} {R1 R2 R3 : _ -> _ -> Prop}
116 (HR : forall v1 v2 v3, R1 v1 v2 -> R2 v2 v3 -> R3 v1 v3)
117 : forall v1 v2 v3, @option_eq A B R1 v1 v2 -> @option_eq B C R2 v2 v3 -> @option_eq A C R3 v1 v3.
118 Proof. t. Qed.
119
120 Global Instance Transitive_option_eq {T} {R} {Transitive_R:@Transitive T R}
121 : Transitive (option_eq R) | 1 := option_eq_trans Transitive_R.
122
123 Global Instance Symmetric_option_eq {T} {R} {Symmetric_R:@Symmetric T R}
124 : Symmetric (option_eq R) | 1 := option_eq_sym Symmetric_R.
125
126 Global Instance Equivalence_option_eq {T} {R} {Equivalence_R:@Equivalence T R}
127 : Equivalence (option_eq R). Proof. split; exact _. Qed.
128 End Relations.
129
130 Lemma option_bl_hetero {A B} {AB_beq : A -> B -> bool} {AB_R : A -> B -> Prop}
131 (AB_bl : forall x y, AB_beq x y = true -> AB_R x y)
132 {x y}
133 : option_beq_hetero AB_beq x y = true -> option_eq AB_R x y.
134 Proof using Type.
135 destruct x, y; cbn in *; eauto; congruence.
136 Qed.
137
138 Lemma option_lb_hetero {A B} {AB_beq : A -> B -> bool} {AB_R : A -> B -> Prop}
139 (AB_lb : forall x y, AB_R x y -> AB_beq x y = true)
140 {x y}
141 : option_eq AB_R x y -> option_beq_hetero AB_beq x y = true.
142 Proof using Type.
143 destruct x, y; cbn in *; eauto; intuition congruence.
144 Qed.
145
146 Lemma option_beq_hetero_uniform {A : Type} A_beq {x y}
147 : option_beq_hetero A_beq x y = @option_beq A A_beq x y.
148 Proof. destruct x, y; reflexivity. Qed.
149
150 Lemma option_bl_hetero_eq {A}
151 {A_beq : A -> A -> bool}
152 (A_bl : forall x y, A_beq x y = true -> x = y)
153 {x y}
154 : option_beq_hetero A_beq x y = true -> x = y.
155 Proof using Type. rewrite option_beq_hetero_uniform; now apply internal_option_dec_bl. Qed.
156
157 Lemma option_lb_hetero_eq {A}
158 {A_beq : A -> A -> bool}
159 (A_lb : forall x y, x = y -> A_beq x y = true)
160 {x y}
161 : x = y -> option_beq_hetero A_beq x y = true.
162 Proof using Type. rewrite option_beq_hetero_uniform; now apply internal_option_dec_lb. Qed.
163
164 Global Instance bind_Proper {A B}
165 : Proper (eq ==> (pointwise_relation _ eq) ==> eq) (@bind A B).
166 Proof.
167 cbv [respectful bind pointwise_relation Proper]; intros; subst; break_innermost_match; auto.
168 Qed.
169
170 Global Instance bind_Proper_pointwise_option_eq {A B RB}
171 : Proper (eq ==> (pointwise_relation _ (option_eq RB)) ==> option_eq RB) (@bind A B) | 90.
172 Proof.
173 cbv [respectful bind pointwise_relation Proper]; intros; subst; break_innermost_match; cbn [option_eq]; auto.
174 Qed.
175
176 Lemma bind_Proper_option_eq_hetero {A A' B B'} {RA RB : _ -> _ -> Prop}
177 a a' (HA : option_eq RA a a') b b' (HB : forall a a', RA a a' -> option_eq RB (b a) (b' a'))
178 : option_eq RB (@bind A B a b) (@bind A' B' a' b').
179 Proof.
180 cbv [bind].
181 destruct a as [a|], a' as [a'|]; try (reflexivity || congruence || exfalso; assumption).
182 cbn [option_eq] in *; auto.
183 Qed.
184
185 Global Instance bind_Proper_option_eq {A B RA RB}
186 : Proper (option_eq RA ==> (RA ==> option_eq RB) ==> option_eq RB) (@bind A B) | 100.
187 Proof.
188 cbv [Proper respectful]; eapply bind_Proper_option_eq_hetero.
189 Qed.
190
191 Global Instance Proper_option_rect_nd_changebody
192 {A B:Type} {RB:relation B} {a:option A}
193 : Proper (pointwise_relation _ RB ==> RB ==> RB) (fun S N => option_rect (fun _ => B) S N a).
194 Proof. cbv; repeat (intro || break_match); intuition. Qed.
195
196 (* FIXME: there used to be a typeclass resolution hint here, something like
197 Hint Extern 1 (Proper _ (@option_rect ?A (fun _ => ?B))) => exact (@Proper_option_rect_nd_changebody A B _ _) : typeclass_instances.
198 but I could not get it working after generalizing [RB] from [Logic.eq] ~ andreser *)
199
200 Global Instance Proper_option_rect_nd_changevalue
201 {A B RA RB} some {Proper_some: Proper (RA==>RB) some}
202 : Proper (RB ==> option_eq RA ==> RB) (@option_rect A (fun _ => B) some).
203 Proof. cbv; repeat (intro || break_match || f_equiv || intuition congruence). Qed.
204
205 Lemma bind_zero_l {A B} f : @bind A B None f = None.
206 Proof. reflexivity. Qed.
207 Lemma bind_zero_r {A B} v : @bind A B v (fun _ => None) = None.
208 Proof. destruct v; reflexivity. Qed.
209 Lemma bind_zero_r_ext {A B} v f : (forall v, f v = None) -> @bind A B v f = None.
210 Proof. destruct v; cbn; auto. Qed.
211
212 Lemma option_rect_false_returns_true_iff
213 {T} {R} {reflexiveR:Reflexive R}
214 (f:T->bool) {Proper_f:Proper(R==>eq)f} (o:option T) :
215 option_rect (fun _ => bool) f false o = true <-> exists s:T, option_eq R o (Some s) /\ f s = true.
216 Proof.
217 unfold option_rect; break_match; repeat intuition (destruct_head ex; eauto);
218 solve [ congruence
219 | repeat esplit; simpl; easy
220 | match goal with [H : f _ = true |- f _ = true ] =>
221 solve [rewrite <- H; eauto] end ].
222 Qed.
223
224 Lemma option_rect_false_returns_true_iff_eq
225 {T} (f:T->bool) (o:option T) :
226 option_rect (fun _ => bool) f false o = true <-> exists s:T, Logic.eq o (Some s) /\ f s = true.
227 Proof. unfold option_rect; break_match; repeat intuition (destruct_head ex; eauto); congruence. Qed.
228
229 Lemma option_rect_option_map : forall {A B C} (f:A->B) some none v,
230 option_rect (fun _ => C) (fun x => some (f x)) none v = option_rect (fun _ => C) some none (option_map f v).
231 Proof.
232 destruct v; reflexivity.
233 Qed.
234
235 Lemma option_map_map : forall {A B C} (f:A->B) (g:B->C) v,
236 option_map g (option_map f v) = option_map (fun v => g (f v)) v.
237 Proof.
238 destruct v; reflexivity.
239 Qed.
240
241 Lemma option_rect_function {A B C S' N' v} f
242 : f (option_rect (fun _ : option A => option B) S' N' v)
243 = option_rect (fun _ : option A => C) (fun x => f (S' x)) (f N') v.
244 Proof. destruct v; reflexivity. Qed.
245
246 (*
247 Ltac commute_option_rect_Let_In := (* pull let binders out side of option_rect pattern matching *)
248 idtac;
249 lazymatch goal with
250 | [ |- ?LHS = option_rect ?P ?S ?N (Let_In ?x ?f) ]
251 => (* we want to just do a [change] here, but unification is stupid, so we have to tell it what to unfold in what order *)
252 cut (LHS = Let_In x (fun y => option_rect P S N (f y))); cbv beta;
253 [ set_evars;
254 let H := fresh in
255 intro H;
256 rewrite H;
257 clear;
258 abstract (cbv [Let_In]; reflexivity)
259 | ]
260 end.
261 *)
262
263 Ltac replace_option_match_with_option_rect :=
264 idtac;
265 lazymatch goal with
266 | [ |- _ = ?RHS :> ?T ]
267 => lazymatch RHS with
268 | match ?a with None => ?N | Some x => @?S x end
269 => replace RHS with (option_rect (fun _ => T) S N a) by (destruct a; reflexivity)
270 end
271 end.
272
273 Ltac simpl_option_rect := (* deal with [option_rect _ _ _ None] and [option_rect _ _ _ (Some _)] *)
274 repeat match goal with
275 | [ |- context[option_rect ?P ?S ?N None] ]
276 => change (option_rect P S N None) with N
277 | [ |- context[option_rect ?P ?S ?N (Some ?x) ] ]
278 => change (option_rect P S N (Some x)) with (S x); cbv beta
279 end.
280
281 Definition option_leq_to_eq {A} (x y : option A) : x = y -> option_eq eq x y.
282 Proof. destruct x; intro; subst; simpl; reflexivity. Defined.
283
284 Definition option_eq_to_leq {A} (x y : option A) : option_eq eq x y -> x = y.
285 Proof.
286 destruct x, y; simpl;
287 try solve [ intros []
288 | apply f_equal
289 | reflexivity
290 | apply eq_sym ].
291 Defined.
292
293 Lemma option_leq_to_eq_to_leq {A x y} v : @option_eq_to_leq A x y (@option_leq_to_eq A x y v) = v.
294 Proof.
295 destruct x; subst; simpl; reflexivity.
296 Qed.
297
298 Lemma option_eq_to_leq_to_eq {A x y} v : @option_leq_to_eq A x y (@option_eq_to_leq A x y v) = v.
299 Proof.
300 compute in *.
301 repeat first [ progress subst
302 | progress break_innermost_match_step
303 | reflexivity ].
304 Qed.
305
306 Lemma UIP_None {A} (p q : @None A = @None A) : p = q.
307 Proof.
308 rewrite <- (option_leq_to_eq_to_leq p), <- (option_leq_to_eq_to_leq q); simpl; reflexivity.
309 Qed.
310
311 Definition is_None {A} (x : option A) : bool
312 := match x with
313 | Some _ => false
314 | None => true
315 end.
316
317 Definition is_Some {A} (x : option A) : bool
318 := match x with
319 | Some _ => true
320 | None => false
321 end.
322
323 Lemma is_None_eq_None_iff {A x} : @is_None A x = true <-> x = None.
324 Proof. destruct x; cbv; split; congruence. Qed.
325
326 Definition invert_Some {A : Type} (x : option A) : match x with
327 | Some _ => A
328 | None => unit
329 end
330 := match x with
331 | Some x' => x'
332 | None => tt
333 end.
334
335 Lemma invert_eq_Some {A x y} (p : Some x = Some y) : { pf : x = y | @option_eq_to_leq A (Some x) (Some y) pf = p }.
336 Proof.
337 refine (exist _ _ (option_leq_to_eq_to_leq _)).
338 Qed.
339
340 Definition always_invert_Some {A} (x : option A) {pf : x <> None} : A
341 := match x return x <> None -> A with
342 | Some v => fun _ => v
343 | None => fun pf => False_rect _ (pf eq_refl)
344 end pf.
345
346 Lemma push_always_invert_Some' {A B} (f : A -> B) (x : option A)
347 (pf : x <> None)
348 (pf' : option_map f x <> None)
349 : f (@always_invert_Some _ x pf) = @always_invert_Some _ (option_map f x) pf'.
350 Proof.
351 destruct x; [ reflexivity | congruence ].
352 Qed.
353
354 Definition pull_always_invert_Some {A B} (f : A -> B) (x : option A)
355 (pf : option_map f x <> None)
356 : f (@always_invert_Some _ x (fun H => pf (f_equal (option_map f) H)))
357 = @always_invert_Some _ (option_map f x) pf
358 := push_always_invert_Some' f x _ pf.
359
360 Lemma option_map_neq_None_iff {A B} (f : A -> B) x
361 : x <> None <-> option_map f x <> None.
362 Proof. destruct x; cbn in *; split; congruence. Qed.
363
364 Definition push_always_invert_Some {A B} (f : A -> B) (x : option A)
365 (pf : x <> None)
366 : f (@always_invert_Some _ x pf)
367 = @always_invert_Some _ (option_map f x)
368 (proj1 (option_map_neq_None_iff f x) pf)
369 := push_always_invert_Some' f x pf _.
370
371 Definition always_invert_Some_bind' {A B} (x : option A) (f : A -> option B)
372 pf pf' pf''
373 : @always_invert_Some _ (bind x f) pf
374 = @always_invert_Some _ (f (@always_invert_Some _ x pf')) pf''.
375 Proof.
376 destruct x as [x|]; cbn in *; [ destruct (f x); cbn in * | ];
377 congruence.
378 Qed.
379
380 Lemma bind_neq_None_iff {A B} (x : option A) (f : A -> option B)
381 : (bind x f <> None) <-> (x <> None /\ forall pf, f (@always_invert_Some _ x pf) <> None).
382 Proof.
383 destruct x as [x|]; cbn; [ destruct (f x); cbn | ]; intuition congruence.
384 Qed.
385
386 Lemma bind_neq_None_iff' {A B} (x : option A) (f : A -> option B)
387 : (bind x f <> None) <-> (exists pf : x <> None, f (@always_invert_Some _ x pf) <> None).
388 Proof.
389 destruct x as [x|]; cbn; [ destruct (f x); cbn | ];
390 split; intros; destruct_head'_ex; try unshelve econstructor;
391 congruence.
392 Qed.
393
394 Definition push_always_invert_Some_bind {A B} (x : option A) (f : A -> option B)
395 pf
396 (pf' := proj1 (proj1 (bind_neq_None_iff x f) pf))
397 (pf'' := proj2 (proj1 (bind_neq_None_iff x f) pf) pf')
398 : @always_invert_Some _ (bind x f) pf
399 = @always_invert_Some _ (f (@always_invert_Some _ x pf')) pf''
400 := always_invert_Some_bind' x f _ _ _.
401
402 Definition pull_always_invert_Some_bind {A B} (x : option A) (f : A -> option B)
403 pf pf'
404 (pf'' := proj2 (bind_neq_None_iff' x f) (ex_intro _ pf pf'))
405 : @always_invert_Some _ (f (@always_invert_Some _ x pf)) pf'
406 = @always_invert_Some _ (bind x f) pf''
407 := eq_sym (always_invert_Some_bind' x f _ _ _).
408
409
410 Ltac inversion_option_step :=
411 match goal with
412 | [ H : Some _ = Some _ |- _ ] => apply option_leq_to_eq in H; unfold option_eq in H
413 | [ H : Some _ = Some _ |- _ ]
414 => let H' := fresh in
415 rename H into H';
416 destruct (invert_eq_Some H') as [H ?]; subst H'
417 | [ H : None = Some _ |- _ ] => solve [ inversion H ]
418 | [ H : Some _ = None |- _ ] => solve [ inversion H ]
419 | [ H : None = None |- _ ] => clear H
420 | [ H : None = None |- _ ]
421 => assert (eq_refl = H) by apply UIP_None; subst H
422 end.
423
424 Ltac inversion_option := repeat inversion_option_step.