| 1 |
From Coq Require Import Lia. |
| 2 |
From Coq Require Import Arith. |
| 3 |
From Coq Require Import Permutation. |
| 4 |
From Coq Require Import List. |
| 5 |
Require Import Crypto.Util.ListUtil.Partition. |
| 6 |
Require Import Crypto.Util.ListUtil.StdlibCompat. |
| 7 |
Require Export Crypto.Util.FixCoqMistakes. |
| 8 |
Import ListNotations. |
| 9 |
Local Open Scope list_scope. |
| 10 |
|
| 11 |
Module List. |
| 12 |
Definition groupAllBy' {A} (f : A -> A -> bool) |
| 13 |
:= fix groupAllBy' (ls : list A) (fuel : nat) : list (list A) |
| 14 |
:= match fuel, ls with |
| 15 |
| (O | S _), [] => [] |
| 16 |
| O, ls => [ls] |
| 17 |
| S fuel, x :: xs |
| 18 |
=> let '(xs, ys) := partition (f x) xs in |
| 19 |
(x :: xs) :: groupAllBy' ys fuel |
| 20 |
end. |
| 21 |
|
| 22 |
Definition groupAllBy {A} (f : A -> A -> bool) (ls : list A) : list (list A) |
| 23 |
:= groupAllBy' f ls (List.length ls). |
| 24 |
|
| 25 |
Lemma groupAllBy'_eq_fuel' A f ls fuel fuel' |
| 26 |
(H : List.length ls <= fuel <= fuel') |
| 27 |
: @groupAllBy' A f ls fuel = @groupAllBy' A f ls fuel'. |
| 28 |
Proof using Type. |
| 29 |
revert fuel' fuel ls H. |
| 30 |
induction fuel' as [|fuel' IH], fuel as [|fuel], ls as [|x xs]; |
| 31 |
try specialize (IH fuel); cbn [List.length groupAllBy']; try lia; try reflexivity. |
| 32 |
destruct (partition (f x) xs) as [xs' ys] eqn:Hp. |
| 33 |
pose proof (@partition_length _ _ _ _ _ Hp). |
| 34 |
intros; f_equal; apply IH; lia. |
| 35 |
Qed. |
| 36 |
|
| 37 |
Lemma groupAllBy'_eq_fuel A f ls fuel fuel' |
| 38 |
(H : List.length ls <= fuel) |
| 39 |
(H' : List.length ls <= fuel') |
| 40 |
: @groupAllBy' A f ls fuel = @groupAllBy' A f ls fuel'. |
| 41 |
Proof using Type. |
| 42 |
destruct (le_gt_dec fuel fuel'). |
| 43 |
all: (idtac + symmetry); apply groupAllBy'_eq_fuel'; lia. |
| 44 |
Qed. |
| 45 |
|
| 46 |
Lemma expand_groupAllBy {A f ls} |
| 47 |
: @groupAllBy A f ls = match ls with |
| 48 |
| [] => [] |
| 49 |
| x :: xs |
| 50 |
=> let '(xs, ys) := partition (f x) xs in |
| 51 |
(x :: xs) :: groupAllBy f ys |
| 52 |
end. |
| 53 |
Proof using Type. |
| 54 |
induction ls as [|x xs IH]; cbn; try reflexivity. |
| 55 |
cbv [groupAllBy]. |
| 56 |
pose proof (partition_length (f x) xs) as H. |
| 57 |
destruct partition; specialize (H _ _ eq_refl). |
| 58 |
f_equal. |
| 59 |
apply groupAllBy'_eq_fuel; lia. |
| 60 |
Qed. |
| 61 |
|
| 62 |
Lemma concat_groupAllBy {A} (f : A -> A -> bool) ls |
| 63 |
: Permutation (List.concat (groupAllBy f ls)) ls. |
| 64 |
Proof using Type. |
| 65 |
remember (List.length ls) as n eqn:H. |
| 66 |
revert ls H. |
| 67 |
induction n as [n IH] using lt_wf_ind; intros ls H'; subst. |
| 68 |
destruct ls as [|x xs]. |
| 69 |
{ constructor. } |
| 70 |
{ rewrite expand_groupAllBy. |
| 71 |
destruct (partition (f x) xs) as [xs' ys] eqn:H. |
| 72 |
pose proof H as H'. |
| 73 |
apply partition_length in H'. |
| 74 |
apply List.partition_permutation in H; cbn [concat List.app]. |
| 75 |
rewrite IH; [ | try reflexivity .. ]; [ | cbn [List.length]; lia .. ]. |
| 76 |
constructor; assumption. } |
| 77 |
Qed. |
| 78 |
|
| 79 |
Lemma Forall_groupAllBy_full A f ls |
| 80 |
: list_rect (fun _ => Prop) |
| 81 |
True |
| 82 |
(fun xs xss P |
| 83 |
=> match xs with |
| 84 |
| [] => False |
| 85 |
| x :: xs => Forall (fun x' => f x x' = true) xs |
| 86 |
/\ Forall (Forall (fun y => f x y = false)) xss |
| 87 |
end |
| 88 |
/\ P) |
| 89 |
(@groupAllBy A f ls). |
| 90 |
Proof. |
| 91 |
remember (List.length ls) as n eqn:H. |
| 92 |
revert ls H. |
| 93 |
induction n as [n IH] using lt_wf_ind; intros ls H'; subst. |
| 94 |
destruct ls as [|x xs]. |
| 95 |
{ constructor. } |
| 96 |
{ rewrite expand_groupAllBy. |
| 97 |
destruct (partition (f x) xs) as [xs' ys] eqn:H. |
| 98 |
pose proof H as H'. |
| 99 |
apply partition_length in H'. |
| 100 |
apply List.Forall_partition in H; cbn [concat List.app]. |
| 101 |
cbn [list_rect]. |
| 102 |
constructor; |
| 103 |
[ split; rewrite <- ?Forall_concat, ?concat_groupAllBy; apply H |
| 104 |
| eapply IH; [ | reflexivity ] ]. |
| 105 |
cbn [List.length]. |
| 106 |
lia. } |
| 107 |
Qed. |
| 108 |
|
| 109 |
Lemma Forall_groupAllBy A f ls |
| 110 |
: Forall (fun xs => match xs with |
| 111 |
| [] => False |
| 112 |
| x :: xs => Forall (fun x' => f x x' = true) xs |
| 113 |
end) |
| 114 |
(@groupAllBy A f ls). |
| 115 |
Proof. |
| 116 |
generalize (@Forall_groupAllBy_full A f ls). |
| 117 |
induction (groupAllBy f ls) as [|[] xs IH]; cbn; constructor; intuition. |
| 118 |
Qed. |
| 119 |
End List. |