Skip to content

Commit 2adba78

Browse files
committed
Defined List Enumerability.
1 parent 3e56e19 commit 2adba78

File tree

4 files changed

+262
-3
lines changed

4 files changed

+262
-3
lines changed

code/Enumerability/EnumerableTypes.v

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Require Import Enumerability.EnumerablePredicates.
33
Require Import Decidability.DecidablePredicates.
44
Require Import Inductive.Option.
55
Require Import Inductive.Predicates.
6-
Require Import util.NaturalEmbedding.
6+
Require Import util.NaturalEmbedding.
7+
Require Import Inductive.ListProperties.
78

89
Section Definitions.
910

@@ -265,3 +266,62 @@ Section ClosureProperties.
265266
Qed.
266267

267268
End ClosureProperties.
269+
270+
Section ListEnumerability.
271+
Definition islistenumerator (X : UU) (L : nat → list X) := ∏ (x : X), ∃ (n : nat), (is_in x (L n)).
272+
273+
Definition listenumerator (X : UU) := ∑ (L : nat → list X), (islistenumerator X L).
274+
275+
Definition islistenumerable (X : UU) := ishinh (listenumerator X).
276+
277+
Lemma enumeratortolistenumerator (X : UU) (E : enumerator X) : (listenumerator X).
278+
Proof.
279+
destruct E as [E isenum].
280+
use tpair. (**TODO: to replace with make_enumerator? **)
281+
- intros n.
282+
induction (E n).
283+
+ exact (cons a nil).
284+
+ exact nil.
285+
- intros x.
286+
use squash_to_prop.
287+
+ exact (hfiber E (some x)).
288+
+ exact (isenum x).
289+
+ apply propproperty.
290+
+ intros [n nfib]; clear isenum; apply hinhpr.
291+
use tpair.
292+
* exact n.
293+
* cbn beta. rewrite -> nfib. right; apply idpath.
294+
Defined.
295+
296+
Lemma listenumeratortoenumerator (X : UU) (L : listenumerator X) : (enumerator X).
297+
Proof.
298+
destruct L as [L islstenum].
299+
use tpair.
300+
- intros n.
301+
destruct (unembed n) as [m1 m2]; clear n.
302+
exact (pos (L m1) m2).
303+
- intros x.
304+
use squash_to_prop.
305+
+ exact (∑ (n : nat), (is_in x (L n))).
306+
+ exact (islstenum x).
307+
+ apply propproperty.
308+
+ intros [n inn]; apply hinhpr.
309+
use tpair.
310+
* exact (embed (n,, (elem_pos x (L n) inn))).
311+
* cbn beta; rewrite -> unembedinv; simpl.
312+
apply poselem_posinv.
313+
Defined.
314+
315+
Lemma weqisenumerableislistenumerable (X : UU) : (isenumerable X) ≃ (islistenumerable X).
316+
Proof.
317+
use weqiff.
318+
- split; intros x; use (squash_to_prop x (propproperty _)); intros enum; apply hinhpr.
319+
+ exact (enumeratortolistenumerator X enum).
320+
+ exact (listenumeratortoenumerator X enum).
321+
- apply propproperty.
322+
- apply propproperty.
323+
Qed.
324+
325+
326+
327+
End ListEnumerability.

code/Inductive/ListProperties.v

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Require Import init.imports.
2-
Require Import UniMath.Combinatorics.Lists.
2+
Require Import Inductive.Option.
33

44
Section Definitions.
55

@@ -203,7 +203,189 @@ Section Filter.
203203

204204
End Filter.
205205

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.
206387

388+
End CumulativeFunctions.
207389

208390
Section Properties.
209391

code/init/imports.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Require Export UniMath.Foundations.All.
2+
Require Export UniMath.Combinatorics.Lists.

code/util/NaturalEmbedding.v

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ Section EmbedNaturals.
9999

100100
Lemma embedinv (n : nat) : (embed (unembed n)) = n.
101101
Proof.
102-
(*TODO*)
102+
assert (eq : ∏ (m : nat × nat), unembed n = m → n = embed m).
103+
- admit.
104+
- rewrite <- (eq (unembed n)); apply idpath; apply idpath.
103105
Admitted.
104106

105107
Lemma unembedinv (n : nat × nat) : (unembed (embed n)) = n.
@@ -110,3 +112,17 @@ Section EmbedNaturals.
110112

111113

112114
End EmbedNaturals.
115+
116+
Lemma nat_ind_geq_n (n : nat) (P : nat → UU) : (P n) → (∏ (k : nat), (P (n + k) → (P (S (n + k))))) → (∏ (m : nat), (n ≤ m) → (P (m))).
117+
Proof.
118+
intros.
119+
assert (∑ (k : nat), n + k = m).
120+
- use tpair.
121+
+ exact (m - n).
122+
+ cbn beta. rewrite -> (natpluscomm n (m - n)). exact (minusplusnmm m n X1).
123+
- destruct X2 as [k eq].
124+
induction eq.
125+
induction k.
126+
+ rewrite -> (natpluscomm n 0); simpl. exact X.
127+
+ rewrite -> (natplusnsm n k); simpl. apply (X0 k), (IHk (natlehnplusnm _ _)).
128+
Defined.

0 commit comments

Comments
 (0)