Debian Codegraph @

src/Util/ListUtil/Filter.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import Arith.
3 From Coq Require Import List.
4 From Coq Require Import Morphisms.
5 Require Export Crypto.Util.FixCoqMistakes.
6 Import ListNotations.
7 Local Set Implicit Arguments.
8 Local Open Scope list_scope.
9
10 Module List.
11 Lemma filter_Forall_eq A f ls : @Forall A (fun x => f x = true) ls -> filter f ls = ls.
12 Proof.
13 induction 1; cbn; [ reflexivity | destruct f ]; try congruence.
14 Qed.
15
16 Lemma filter_length_le A f ls : List.length (@filter A f ls) <= List.length ls.
17 Proof. induction ls; cbn; [ reflexivity | destruct f ]; cbn; auto with arith. Qed.
18
19 Lemma filter_eq_length_eq A f ls : List.length (filter f ls) = List.length ls -> @filter A f ls = ls.
20 Proof.
21 induction ls; cbn; [ reflexivity | destruct f ]; cbn; intro; try apply f_equal; eauto with arith.
22 pose proof (filter_length_le f ls).
23 lia.
24 Qed.
25
26 Lemma Forall2_filter_same A B P f g lsA lsB
27 : @Forall2 A B (fun a b => match f a, g b with
28 | true, true => P a b
29 | false, false => True
30 | true, false | false, true => False
31 end)
32 lsA lsB
33 -> Forall2 P (filter f lsA) (filter g lsB).
34 Proof.
35 induction 1; cbn; [ constructor | ].
36 destruct f, g; try constructor; auto; try (exfalso; assumption).
37 Qed.
38
39 #[export] Instance filter_Proper {A} : Proper (pointwise_relation _ eq ==> eq ==> eq) (@List.filter A).
40 Proof.
41 cbv [pointwise_relation]; intros f g Hfg ls ls' ?; subst ls'.
42 induction ls as [|l ls IHls]; cbn [filter]; rewrite ?IHls, ?Hfg; reflexivity.
43 Qed.
44
45 #[export] Instance Proper_filter_eq {A} : Proper ((eq ==> eq) ==> eq ==> eq) (@filter A).
46 Proof. repeat intro; subst; apply filter_Proper; repeat intro; eauto. Qed.
47 End List.
48
49 Export (hints) List.