|
1 | 1 | Require Import init.imports. |
2 | | -Require Import UniMath.Combinatorics.Lists. |
| 2 | +Require Import Inductive.Option. |
3 | 3 |
|
4 | 4 | Section Definitions. |
5 | 5 |
|
@@ -203,7 +203,189 @@ Section Filter. |
203 | 203 |
|
204 | 204 | End Filter. |
205 | 205 |
|
| 206 | +Section Position. |
| 207 | + Definition pos {X : UU} : (list X) → nat → @option X. |
| 208 | + Proof. |
| 209 | + use list_ind; cbn beta. |
| 210 | + - exact (λ _, none). |
| 211 | + - intros xs l f n. |
| 212 | + induction n. |
| 213 | + + exact (some xs). |
| 214 | + + exact (f n). |
| 215 | + Defined. |
| 216 | + |
| 217 | + Definition elem_pos {X : UU} (x : X) (l : list X) (inn : is_in x l) : nat. |
| 218 | + Proof. |
| 219 | + revert l inn. |
| 220 | + use list_ind. |
| 221 | + - intros inn. apply fromempty; exact inn. |
| 222 | + - intros x0 xs f inn. |
| 223 | + destruct inn as [l | r]. |
| 224 | + + exact (S (f l)). |
| 225 | + + exact 0. |
| 226 | + Defined. |
| 227 | + |
| 228 | + Lemma bla {X : UU} (x : X) (l : list X) (inn : is_in x l) (n : nat) : (elem_pos x l inn) = n → (pos l n) = (some x). |
| 229 | + Proof. |
| 230 | + revert l n inn. |
| 231 | + use list_ind. |
| 232 | + - intros n inn. exact (fromempty inn). |
| 233 | + - cbn beta. intros x0 xs IHn n inn eq. |
| 234 | + destruct inn as [l | r]. |
| 235 | + + induction n. |
| 236 | + * apply fromempty. exact (negpathssx0 _ eq). |
| 237 | + * assert (elem_pos x xs l = n) by exact (invmaponpathsS _ _ eq). |
| 238 | + assert (pos (cons x0 xs) (S n) = pos xs n) by apply idpath. |
| 239 | + rewrite X1. exact (IHn n l X0). |
| 240 | + + induction n. |
| 241 | + * assert (pos (cons x0 xs) 0 = some x0) by apply idpath; rewrite -> X0; apply maponpaths. |
| 242 | + exact (pathsinv0 r). |
| 243 | + * apply fromempty. exact (negpaths0sx _ eq). |
| 244 | + Defined. |
| 245 | + |
| 246 | + Lemma poselem_posinv {X : UU} (x : X) (l : list X) (inn : is_in x l) : pos l (elem_pos x l inn) = some x. |
| 247 | + Proof. |
| 248 | + apply (bla x l inn). |
| 249 | + apply idpath. |
| 250 | + Defined. |
| 251 | + |
| 252 | +End Position. |
| 253 | + |
| 254 | +Section Append. |
| 255 | + |
| 256 | + Definition append {X : UU} : (list X) → (list X) → (list X). |
| 257 | + Proof. |
| 258 | + use list_ind; cbn beta. |
| 259 | + - exact (idfun _). |
| 260 | + - intros. exact (cons x (X0 X1)). |
| 261 | + Defined. |
| 262 | + |
| 263 | + Section AppendTests. |
| 264 | + Definition l0 : list nat := (cons 0 (cons 1 (cons 2 nil))). |
| 265 | + Definition l1 : list nat := (cons 1 (cons 2 (cons 3 nil))). |
| 266 | + Definition l2 : list nat := (cons 0 (cons 1 (cons 2 (cons 1 (cons 2 (cons 3 nil)))))). |
| 267 | + |
| 268 | + Example testappend : (append l0 l1) = l2. Proof. apply idpath. Qed. |
| 269 | + |
| 270 | + Example testappendnilleft : (append nil l1) = l1. Proof. apply idpath. Qed. |
| 271 | + |
| 272 | + Example testappendnilright: (append l2 nil) = l2. Proof. apply idpath. Qed. |
| 273 | + |
| 274 | + End AppendTests. |
| 275 | + |
| 276 | + (* Local Notation "l1 ++ l2" := (append l1 l2) (at level 60, left associativity). |
| 277 | + Lemma appendnil1 {X : UU} (l1 : list X) : (l1 ++ nil) = l1. |
| 278 | + Proof. |
| 279 | + revert l1. |
| 280 | + use list_ind; cbn beta; intros. |
| 281 | + - apply idpath. |
| 282 | + - exact (maponpaths (cons x) X0). |
| 283 | + Qed. |
| 284 | +
|
| 285 | + Lemma appendnil1inv {X : UU} (l1 : list X) : l1 = (l1 ++ nil). |
| 286 | + Proof. |
| 287 | + apply pathsinv0, appendnil1. |
| 288 | + Qed. |
| 289 | + |
| 290 | + Lemma appendnil2 {X : UU} (l1 : list X) : (nil ++ l1) = l1. |
| 291 | + Proof. apply idpath. Qed. |
| 292 | + |
| 293 | + Lemma appendnil2inv {X : UU} (l2 : list X) : l2 = (nil ++ l2). |
| 294 | + Proof. apply idpath. Qed. |
| 295 | +
|
| 296 | + Lemma appendtrans {X : UU} (l1 l2 l3 : list X) : (l1 ++ l2) ++ l3 = l1 ++ l2 ++ l3. |
| 297 | + Proof. *) |
| 298 | + |
| 299 | + Local Infix "++" := concatenate. |
| 300 | + |
| 301 | + Lemma isin_concatenate1 {X : UU} (l1 l2 : list X) (x : X) : (is_in x l1) → (is_in x (l1 ++ l2)). |
| 302 | + Proof. |
| 303 | + revert l1. |
| 304 | + use list_ind; cbn beta; intros. |
| 305 | + - apply fromempty; exact X0. |
| 306 | + - rewrite -> (concatenateStep x0 xs l2). |
| 307 | + destruct X1 as [l | r]. |
| 308 | + + left; apply X0; exact l. |
| 309 | + + right; exact r. |
| 310 | + Defined. |
| 311 | + |
| 312 | + Lemma isin_concatenate2 {X : UU} (l1 l2 : list X) (x : X) : (is_in x l2) → (is_in x (l1 ++ l2)). |
| 313 | + Proof. |
| 314 | + revert l1. |
| 315 | + use list_ind; cbn beta; intros. |
| 316 | + - rewrite -> (nil_concatenate l2); exact X0. |
| 317 | + - rewrite -> (concatenateStep x0 xs l2). left. exact (X0 X1). |
| 318 | + Defined. |
| 319 | + |
| 320 | + Lemma isin_concatenate_choice {X : UU} (l1 l2 : list X) (x : X) : (is_in x (l1 ++ l2)) → (is_in x l1) ⨿ (is_in x l2). |
| 321 | + Proof. |
| 322 | + revert l1. |
| 323 | + use list_ind; cbn beta. |
| 324 | + - rewrite -> nil_concatenate. intros inn; right; exact inn. |
| 325 | + - intros x0 xs IHl. rewrite -> (concatenateStep x0 xs l2). intros [l | r]. |
| 326 | + + induction (IHl l) as [a | a]. left; left; exact a. right; exact a. |
| 327 | + + left; right; exact r. |
| 328 | + Defined. |
| 329 | +End Append. |
| 330 | + |
| 331 | +Section CumulativeFunctions. |
| 332 | + |
| 333 | + Local Infix "++" := concatenate. |
| 334 | + Definition iscumulative {X : UU} (L : nat → list X) := ∏ (n : nat), ∃ (l : list X), (L (S n) = ((L n) ++ l)). |
| 335 | + |
| 336 | + Lemma cumulativenleqm {X : UU} (L : nat → list X) (iscum : (iscumulative L)) (m n : nat) : n ≤ m → ∃ (l : list X), L m = (L n) ++ l. |
| 337 | + Proof. |
| 338 | + intros nleqm. |
| 339 | + assert (eq : ∑ (k : nat), n + k = m). |
| 340 | + - use tpair. |
| 341 | + + exact (m - n). |
| 342 | + + cbn beta. rewrite -> (natpluscomm n (m - n)). apply (minusplusnmm _ _ nleqm). |
| 343 | + - destruct eq as [k eq]. |
| 344 | + induction eq; induction k. |
| 345 | + + rewrite (natpluscomm n 0). apply hinhpr. use (tpair _ nil). simpl. rewrite -> (concatenate_nil (L n)). apply idpath. |
| 346 | + + rewrite (natplusnsm n k); use (squash_to_prop (iscum (n + k)) (propproperty _)); intros [l eq]; clear iscum. |
| 347 | + use (squash_to_prop (IHk (natlehnplusnm _ _)) (propproperty _)); intros [l0 neq]; clear IHk; apply hinhpr. |
| 348 | + use (tpair _ (l0 ++ l)). simpl. rewrite -> eq, neq. apply assoc_concatenate. |
| 349 | + Qed. |
| 350 | + |
| 351 | + Definition cumul {X : UU} : (nat → list X) → (nat → list X). |
| 352 | + Proof. |
| 353 | + intros L n. |
| 354 | + induction n. |
| 355 | + - exact (L 0). |
| 356 | + - exact (IHn ++ (L (S n))). |
| 357 | + Defined. |
| 358 | + |
| 359 | + Lemma iscumulativecumul {X : UU} (L : nat → list X) : (iscumulative (cumul L)). |
| 360 | + Proof. |
| 361 | + intros n; induction n. |
| 362 | + - apply hinhpr. |
| 363 | + use (tpair _ (L 1) (idpath _)). |
| 364 | + - use (squash_to_prop IHn (propproperty _)); intros IH; clear IHn; apply hinhpr. |
| 365 | + use (tpair _ (L (S (S n))) (idpath _)). |
| 366 | + Defined. |
| 367 | + |
| 368 | + Lemma isinlisincumull {X : UU} (L : nat → list X) (x : X) (n : nat) : (is_in x (L n)) → (is_in x (cumul L n)). |
| 369 | + Proof. |
| 370 | + intros inn. |
| 371 | + induction n. |
| 372 | + - exact inn. |
| 373 | + - simpl. apply isin_concatenate2. exact inn. |
| 374 | + Defined. |
| 375 | + |
| 376 | + Lemma iscumulex {X : UU} (L : nat → list X) (x : X) : (∃ (n : nat), (is_in x (L n))) <-> (∃ (n : nat), (is_in x (cumul L n))). |
| 377 | + Proof. |
| 378 | + split; intros ex; use (squash_to_prop ex (propproperty _)); clear ex; intros [n inn]; apply hinhpr. |
| 379 | + - use (tpair _ n); cbn beta. |
| 380 | + apply isinlisincumull. exact inn. |
| 381 | + - induction n. |
| 382 | + + use (tpair _ 0). exact inn. |
| 383 | + + induction (isin_concatenate_choice (cumul L n) (L (S n)) x inn). |
| 384 | + * exact (IHn a). |
| 385 | + * exact ((S n),, b). |
| 386 | + Defined. |
206 | 387 |
|
| 388 | +End CumulativeFunctions. |
207 | 389 |
|
208 | 390 | Section Properties. |
209 | 391 |
|
|
0 commit comments