Debian Codegraph @

src/Util/ListUtil/Partition.v

raw
1 From Coq Require Import Permutation.
2 From Coq Require Import List.
3 Require Export Crypto.Util.FixCoqMistakes.
4 Import ListNotations.
5 Local Open Scope list_scope.
6
7 Module List.
8 Lemma partition_permutation A f (ls : list A) xs ys
9 (H : @partition A f ls = (xs, ys))
10 : Permutation (xs ++ ys) ls.
11 Proof.
12 revert xs ys H.
13 induction ls as [|x ls IH]; cbn [partition]; intros xs ys H; inversion H; subst; clear H.
14 { constructor. }
15 { destruct (partition f ls); specialize (IH _ _ eq_refl).
16 destruct f; match goal with H : pair _ _ = pair _ _ |- _ => inversion H; clear H end.
17 all: subst; cbn.
18 { constructor; assumption. }
19 { rewrite <- IH.
20 cbn.
21 rewrite Permutation_app_comm; cbn; rewrite Permutation_app_comm.
22 reflexivity. } }
23 Qed.
24
25 Lemma Forall_partition A f ls xs ys
26 (H : @partition A f ls = (xs, ys))
27 : Forall (fun x => f x = true) xs /\ Forall (fun x => f x = false) ys.
28 Proof.
29 revert xs ys H.
30 induction ls as [|x ls IH]; cbn [partition]; intros xs ys H; inversion H; subst; clear H; repeat constructor; destruct partition, (f x) eqn:Hf.
31 all: match goal with H : pair _ _ = pair _ _ |- _ => inversion H; clear H end; subst.
32 all: specialize (IH _ _ eq_refl).
33 all: destruct IH.
34 all: repeat first [ constructor | assumption ].
35 Qed.
36
37 Lemma partition_eq_filter A f (ls : list A) xs ys
38 (H : @partition A f ls = (xs, ys))
39 : filter f ls = xs /\ filter (fun x => negb (f x)) ls = ys.
40 Proof.
41 revert xs ys H.
42 induction ls as [|x ls IH]; cbn [partition filter]; intros xs ys H; inversion H; subst; clear H.
43 { repeat constructor. }
44 { destruct (partition f ls); specialize (IH _ _ eq_refl).
45 destruct IH; subst.
46 destruct f; match goal with H : pair _ _ = pair _ _ |- _ => inversion H; clear H end.
47 all: subst; cbn; repeat constructor. }
48 Qed.
49 End List.