| 1 |
From Coq Require Import Morphisms. |
| 2 |
From Coq Require Import List. |
| 3 |
Require Import Crypto.Util.Bool. |
| 4 |
Import ListNotations. Open Scope bool_scope. |
| 5 |
|
| 6 |
Lemma fold_left_orb_true ls |
| 7 |
: List.fold_left orb ls true = true. |
| 8 |
Proof. induction ls as [|?? IHls]; [ reflexivity | assumption ]. Qed. |
| 9 |
Lemma fold_left_orb_pull ls v |
| 10 |
: List.fold_left orb ls v = orb v (List.fold_left orb ls false). |
| 11 |
Proof. destruct v; [ apply fold_left_orb_true | reflexivity ]. Qed. |
| 12 |
|
| 13 |
Lemma nth_error_true_fold_left_orb_true {n ls b} |
| 14 |
: nth_error ls n = Some true -> fold_left orb ls b = true. |
| 15 |
Proof. |
| 16 |
revert ls b; induction n, ls as [|[] ?]; cbn; try congruence; intro b'; destruct b'; cbn [orb]; try congruence; auto using fold_left_orb_true. |
| 17 |
Qed. |
| 18 |
|
| 19 |
Lemma fold_left_orb_true_or_ind_gen {ls b} (P Q : bool -> Prop) |
| 20 |
(H : forall b, P b -> Q b) |
| 21 |
: Q b -> fold_right (fun b res => P b -> res) (Q (fold_left orb ls b)) ls. |
| 22 |
Proof. |
| 23 |
revert Q H b; induction ls; cbn; eauto; intros. |
| 24 |
apply IHls; eauto. |
| 25 |
destruct b; cbn; eauto. |
| 26 |
Qed. |
| 27 |
|
| 28 |
Lemma fold_left_orb_true_or_ind {ls b} (P : bool -> Prop) |
| 29 |
: fold_right (fun b res => P b -> res) (P b -> P (fold_left orb ls b)) ls. |
| 30 |
Proof. |
| 31 |
apply @fold_left_orb_true_or_ind_gen with (P:=P) (Q:=fun v => _ -> P v); auto. |
| 32 |
Qed. |
| 33 |
|
| 34 |
Ltac induction_fold_left_orb_true ls b := |
| 35 |
let fold_right_u := (eval cbv [fold_right] in (@fold_right)) in |
| 36 |
let lem := (eval cbv beta iota in |
| 37 |
(@id (forall P : bool -> Prop, fold_right_u _ _ (fun b' res => P b' -> res) (P b -> P (fold_left orb ls b)) ls) |
| 38 |
(@fold_left_orb_true_or_ind ls b))) in |
| 39 |
induction (fold_left orb ls b) using lem. |
| 40 |
|
| 41 |
Fixpoint fold_andb_map {A B} (f : A -> B -> bool) (ls1 : list A) (ls2 : list B) |
| 42 |
: bool |
| 43 |
:= match ls1, ls2 with |
| 44 |
| nil, nil => true |
| 45 |
| nil, _ => false |
| 46 |
| cons x xs, cons y ys => andb (f x y) (@fold_andb_map A B f xs ys) |
| 47 |
| cons _ _, _ => false |
| 48 |
end. |
| 49 |
Lemma fold_andb_map_map {A B C} f g ls1 ls2 |
| 50 |
: @fold_andb_map A B f ls1 (@List.map C _ g ls2) |
| 51 |
= fold_andb_map (fun a b => f a (g b)) ls1 ls2. |
| 52 |
Proof. revert ls1 ls2; induction ls1, ls2; cbn; congruence. Qed. |
| 53 |
|
| 54 |
Lemma fold_andb_map_map1 {A B C} f g ls1 ls2 |
| 55 |
: @fold_andb_map A B f (@List.map C _ g ls1) ls2 |
| 56 |
= fold_andb_map (fun a b => f (g a) b) ls1 ls2. |
| 57 |
Proof. revert ls1 ls2; induction ls1, ls2; cbn; congruence. Qed. |
| 58 |
|
| 59 |
Lemma fold_andb_map_length A B f ls1 ls2 |
| 60 |
(H : @fold_andb_map A B f ls1 ls2 = true) |
| 61 |
: length ls1 = length ls2. |
| 62 |
Proof. |
| 63 |
revert ls1 ls2 H; induction ls1, ls2; cbn; intros; |
| 64 |
rewrite ?Bool.andb_true_iff in *; |
| 65 |
f_equal; try congruence; intuition auto. |
| 66 |
Qed. |
| 67 |
|
| 68 |
Global Instance fold_andb_map_Proper {A B} |
| 69 |
: Proper (pointwise_relation _ (pointwise_relation _ eq) ==> eq ==> eq ==> eq) (@fold_andb_map A B). |
| 70 |
Proof. |
| 71 |
unfold pointwise_relation. |
| 72 |
intros f g H ls1 y ? ls2 z ?; subst y z. |
| 73 |
revert ls2; induction ls1, ls2; cbn; try reflexivity. |
| 74 |
apply f_equal2; eauto. |
| 75 |
Qed. |
| 76 |
|
| 77 |
Lemma fold_andb_map_iff A B R ls1 ls2 |
| 78 |
: (@fold_andb_map A B R ls1 ls2 = true) |
| 79 |
<-> (length ls1 = length ls2 |
| 80 |
/\ (forall v, List.In v (List.combine ls1 ls2) -> R (fst v) (snd v) = true)). |
| 81 |
Proof. |
| 82 |
revert ls2; induction ls1 as [|x xs IHxs], ls2 as [|y ys]; cbn; try solve [ intuition (congruence || auto) ]; []. |
| 83 |
rewrite Bool.andb_true_iff, IHxs. |
| 84 |
split; intros [H0 H1]; split; auto; |
| 85 |
intuition (congruence || (subst; auto)). |
| 86 |
apply (H1 (_, _)); auto. |
| 87 |
Qed. |
| 88 |
|
| 89 |
Lemma fold_andb_map_snoc A B f x xs y ys |
| 90 |
: @fold_andb_map A B f (xs ++ [x]) (ys ++ [y]) = @fold_andb_map A B f xs ys && f x y. |
| 91 |
Proof. |
| 92 |
clear. |
| 93 |
revert ys; induction xs as [|x' xs'], ys as [|y' ys']; cbn; |
| 94 |
rewrite ?Bool.andb_true_r, ?Bool.andb_false_r; |
| 95 |
try (destruct ys'; cbn; rewrite Bool.andb_false_r); |
| 96 |
try (destruct xs'; cbn; rewrite Bool.andb_false_r); |
| 97 |
try reflexivity. |
| 98 |
rewrite IHxs', Bool.andb_assoc; reflexivity. |
| 99 |
Qed. |
| 100 |
|
| 101 |
Module Thunked. |
| 102 |
Import Bool.Thunked. |
| 103 |
Lemma fold_left_orb_true ls b |
| 104 |
: b tt = Datatypes.true -> List.fold_left orb ls b tt = Datatypes.true. |
| 105 |
Proof. |
| 106 |
revert b; induction ls as [|?? IHls]; cbn; auto. |
| 107 |
intros ? H; apply IHls; cbv [orb]; rewrite H; reflexivity. |
| 108 |
Qed. |
| 109 |
Lemma nth_error_true_fold_left_orb_true {n ls b} |
| 110 |
: option_map (fun f => f tt) (nth_error ls n) = Some Datatypes.true -> fold_left orb ls b tt = Datatypes.true. |
| 111 |
Proof. |
| 112 |
revert ls b; induction n, ls as [|v ?]; cbn; auto. |
| 113 |
all: intros b H; inversion H; auto using fold_left_orb_true. |
| 114 |
etransitivity; [ apply fold_left_orb_true; cbv [orb] | congruence ]. |
| 115 |
edestruct b; cbn; congruence. |
| 116 |
Qed. |
| 117 |
|
| 118 |
Lemma fold_left_orb_true_or_ind_gen {ls b} (P Q : Datatypes.bool -> Prop) |
| 119 |
(H : forall b, P b -> Q b) |
| 120 |
: Q (b tt) -> fold_right (fun b res => P (b tt) -> res) (Q (fold_left orb ls b tt)) ls. |
| 121 |
Proof. |
| 122 |
revert Q H b; induction ls; cbn; eauto; intros. |
| 123 |
apply IHls; eauto; cbv [orb]. |
| 124 |
destruct b; cbn; eauto. |
| 125 |
Qed. |
| 126 |
|
| 127 |
Lemma fold_left_orb_true_or_ind {ls b} (P : Datatypes.bool -> Prop) |
| 128 |
: fold_right (fun b res => P (b tt) -> res) (P (b tt) -> P (fold_left orb ls b tt)) ls. |
| 129 |
Proof. |
| 130 |
apply @fold_left_orb_true_or_ind_gen with (P:=P) (Q:=fun v => _ -> P v); auto. |
| 131 |
Qed. |
| 132 |
|
| 133 |
Ltac induction_fold_left_orb_true ls b := |
| 134 |
let fold_right_u := (eval cbv [fold_right] in (@fold_right)) in |
| 135 |
let lem := (eval cbv beta iota in |
| 136 |
(@id (forall P : Datatypes.bool -> Prop, fold_right_u _ _ (fun b' res => P (b' tt) -> res) (P (b tt) -> P (fold_left orb ls b tt)) ls) |
| 137 |
(@fold_left_orb_true_or_ind ls b))) in |
| 138 |
induction (fold_left orb ls b tt) using lem. |
| 139 |
End Thunked. |