1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
(** Example by Nicky Vazou, unfinished *)
Require Import Arith.
Require Import Coq.Classes.DecidableClass.
Require Import Coq.Program.Wf.
Require Import List Lia.
Require Import PeanoNat.
Require Import Program.
From Equations Require Import Equations.
Import ListNotations.
Set Keyed Unification.
Class Associative {T: Type} (op: T -> T -> T) :=
{
associativity: forall x y z, op x (op y z) = op (op x y) z;
}.
Class Monoid (T: Type) :=
MkMonoid {
unit: T;
op: T -> T -> T;
monoid_associative: Associative op;
monoid_left_identity: forall x, op unit x = x;
monoid_right_identity: forall x, op x unit = x;
}.
#[export]
Instance app_Associative: forall T, Associative (@app T).
Proof.
intro T.
constructor.
induction x.
{ reflexivity. }
{ simpl. congruence. }
Defined.
#[export]
Instance list_Monoid: forall T, Monoid (list T).
Proof.
intro T.
apply MkMonoid with (unit := []) (op := @app T).
{ auto with typeclass_instances. }
{ reflexivity. }
{ induction x.
{ reflexivity. }
{ simpl. congruence. }
}
Defined.
Notation "a ** b" := (op a b) (at level 50).
Class MonoidMorphism
{Tn Tm: Type}
`{Mn: Monoid Tn} `{Mm: Monoid Tm}
(f: Tn -> Tm)
:=
{
morphism_unit: f unit = unit;
morphism_op: forall x y, f (x ** y) = f x ** f y;
}.
Class ChunkableMonoid (T: Type) `{Monoid T} :=
MkChunkableMonoid {
length: T -> nat;
drop: nat -> T -> T;
take: nat -> T -> T;
drop_spec:
forall i x,
length (drop i x) = length x - i;
take_spec:
forall i x,
i <= length x ->
length (take i x) = i;
take_drop_spec:
forall i x, x = take i x ** drop i x;
}.
Fixpoint list_take {T: Type} i (l: list T) :=
match i, l with
| 0, _ => []
| _, [] => []
| S i, h::t => h :: list_take i t
end.
Fixpoint list_drop {T: Type} i (l: list T) :=
match i, l with
| 0, _ => l
| _, [] => []
| S i, h::t => list_drop i t
end.
Ltac intuition_solver ::= auto with core arith datatypes solve_subterm.
#[export]
Instance list_ChunkableMonoid: forall T, ChunkableMonoid (list T).
Proof.
intro T.
apply MkChunkableMonoid
with (length := @List.length T) (drop := list_drop) (take := list_take);
intros.
{ generalize dependent x.
induction i, x; intros; intuition.
}
{ generalize dependent x.
induction i, x; intros; intuition.
simpl in *.
rewrite IHi; intuition.
}
{ generalize dependent x.
induction i, x; intros; intuition.
simpl in *.
rewrite <- IHi.
reflexivity.
}
Defined.
Section Chunk.
Context{T : Type} `{M : ChunkableMonoid T}.
Set Program Mode.
Equations? chunk (i: { i : nat | i > 0 }) (x : T) : list T by wf (length x) lt :=
chunk i x with dec (length x <=? i) :=
{ | left _ => [x] ;
| right p => take i x :: chunk i (drop i x) }.
Proof. apply leb_complete_conv in p. rewrite drop_spec. lia. Qed.
End Chunk.
Theorem if_flip_helper {B: Type} {b: bool}
(C E: true = b -> B) (D F: false = b -> B):
(forall (r: true = b), C r = E r) ->
(forall (r: false = b), D r = F r) ->
(if b as an return an = b -> B then C else D) eq_refl =
(if b as an return an = b -> B then E else F) eq_refl.
Proof.
intros.
destruct b.
apply H.
apply H0.
Qed.
(* Transparent chunk.
Eval compute in (chunk (exist _ 3 _) [0; 1; 2; 3; 4; 5; 6; 7; 8; 9]). *)
(*
= [[0; 1; 2]; [3; 4; 5]; [6; 7; 8]; [9]]
: list (list nat)
*)
Section mconcat.
Context {M : Type} `{Monoid M}.
Equations mconcat (l: list M): M :=
mconcat [] := unit;
mconcat (cons x xs) := x ** mconcat xs.
End mconcat.
Transparent mconcat.
Theorem morphism_distribution:
forall {M N: Type}
`{MM: Monoid M} `{MN: Monoid N}
`{CMM: @ChunkableMonoid N MN}
(f: N -> M)
`{MMf: @MonoidMorphism _ _ MN MM f},
forall i x,
f x = mconcat (map f (chunk i x)).
Proof.
intros.
funelim (chunk i x).
{ simpl. simp mconcat. now rewrite monoid_right_identity. }
simpl. simp mconcat.
rewrite <- H; auto.
rewrite <- morphism_op.
now rewrite <- take_drop_spec.
Qed.
Lemma length_list_drop: forall {T: Type} i (x: list T),
i < Datatypes.length x ->
Datatypes.length (list_drop i x) = Datatypes.length x - i.
Proof.
intros.
generalize dependent i.
induction x; destruct i; simpl; intros.
{ reflexivity. }
{ reflexivity. }
{ reflexivity. }
{ apply IHx. intuition. }
Qed.
Lemma length_chunk_base:
forall {T: Type} I (x: list T),
let i := proj1_sig I in
i > 1 ->
Datatypes.length x <= i ->
Datatypes.length (chunk I x) = 1.
Proof.
intros; subst i.
funelim (chunk I x). reflexivity.
simpl.
apply leb_correct in H1. rewrite p in H1. discriminate.
Qed.
Ltac feed H :=
match type of H with
| ?foo -> _ =>
let FOO := fresh in
assert foo as FOO; [|specialize (H FOO); clear FOO]
end.
Lemma length_chunk_lt:
forall {T: Type} I (x: list T),
let i := proj1_sig I in
i > 1 ->
Datatypes.length x > i ->
Datatypes.length (chunk I x) < Datatypes.length x.
Proof.
intros; subst i.
funelim (chunk I x).
simpl. lia.
simpl.
specialize (H H0).
revert H. unfold drop. simpl.
pose proof (drop_spec (` i) x). simpl in H.
rewrite H by lia. clear H.
simp chunk. clear Heq. destruct dec. simp chunk; simpl; intros; try lia. intros.
feed H.
clear H. apply leb_complete_conv in e.
pose proof (drop_spec (` i) x). rewrite H in e; try lia;
unfold length in *; simpl in *; lia.
lia.
Qed.
Section pmconcat.
Context {M : Type} `{ChunkableMonoid M}.
Equations? pmconcat (I : { i : nat | i > 0 }) (x : list M) : M by wf (length x) lt :=
pmconcat i x with dec ((` i <=? 1) || (length x <=? ` i))%bool => {
| left H => mconcat x ;
| right Hd => pmconcat i (map mconcat (chunk i x)) }.
Proof. clear pmconcat.
rewrite map_length.
rewrite Bool.orb_false_iff in Hd.
destruct Hd. apply leb_complete_conv in H2. apply leb_complete_conv in H3.
apply length_chunk_lt; simpl; auto.
Qed. (* 0.264s from 1.571s *)
End pmconcat.
#[export] Instance mconcat_mon T : MonoidMorphism (@mconcat (list T) _).
Next Obligation.
Proof.
funelim (mconcat x). reflexivity.
simpl. rewrite H. now rewrite <- app_assoc.
Qed.
Theorem concatEquivalence: forall {T: Type} i (x: list (list T)),
pmconcat i x = mconcat x.
Proof.
intros.
funelim (pmconcat i x).
reflexivity.
rewrite H. now rewrite <- (morphism_distribution mconcat).
Qed.
|