Equivalences involving list
-like types #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
This file defines some additional constructive equivalences using encodable
and the pairing
function on ℕ
.
Explicit encoding function for list α
Equations
- encodable.encode_list (a :: l) = (nat.mkpair (encodable.encode a) (encodable.encode_list l)).succ
- encodable.encode_list list.nil = 0
Explicit decoding function for list α
Equations
- encodable.decode_list v.succ = encodable.decode_list._match_1 v (λ (v₁ v₂ : ℕ) (h : (v₁, v₂).snd ≤ v) (this : v₂ < v.succ), encodable.decode_list v₂) (nat.unpair v) _
- encodable.decode_list 0 = option.some list.nil
- encodable.decode_list._match_1 v _f_1 (v₁, v₂) h = have this : v₂ < v.succ, from _, (λ (_x : α) (_y : list α), _x :: _y) <$> encodable.decode α v₁ <*> _f_1 v₁ v₂ h this
If α
is encodable, then so is list α
. This uses the mkpair
and unpair
functions from
data.nat.pairing
.
Equations
- list.encodable = {encode := encodable.encode_list _inst_1, decode := encodable.decode_list _inst_1, encodek := _}
Explicit encoding function for multiset α
Equations
- encodable.encode_multiset s = encodable.encode (multiset.sort enle s)
Explicit decoding function for multiset α
Equations
- encodable.decode_multiset n = coe <$> encodable.decode (list α) n
If α
is encodable, then so is multiset α
.
Equations
- multiset.encodable = {encode := encodable.encode_multiset _inst_1, decode := encodable.decode_multiset _inst_1, encodek := _}
A listable type with decidable equality is encodable.
Equations
- encodable.encodable_of_list l H = {encode := λ (a : α), list.index_of a l, decode := l.nth, encodek := _}
A finite type is encodable. Because the encoding is not unique, we wrap it in trunc
to
preserve computability.
Equations
- fintype.trunc_encodable α = quot.rec_on_subsingleton finset.univ.val (λ (l : list α) (H : ∀ (x : α), x ∈ quot.mk setoid.r l), trunc.mk (encodable.encodable_of_list l H)) finset.mem_univ
A noncomputable way to arbitrarily choose an ordering on a finite type.
It is not made into a global instance, since it involves an arbitrary choice.
This can be locally made into an instance with local attribute [instance] fintype.to_encodable
.
Equations
If α
is encodable, then so is finset α
.
When α
is finite and β
is encodable, α → β
is encodable too. Because the encoding is not
unique, we wrap it in trunc
to preserve computability.
Equations
- encodable.fintype_arrow α β = trunc.map (λ (f : α ≃ fin (fintype.card α)), encodable.of_equiv (fin (fintype.card α) → β) (f.arrow_congr (equiv.refl β))) (fintype.trunc_equiv_fin α)
When α
is finite and all π a
are encodable, Π a, π a
is encodable too. Because the
encoding is not unique, we wrap it in trunc
to preserve computability.
Equations
- encodable.fintype_pi α π = (fintype.trunc_encodable α).bind (λ (a : encodable α), (encodable.fintype_arrow α (Σ (a : α), π a)).bind (λ (f : encodable (α → (Σ (a : α), π a))), trunc.mk (encodable.of_equiv {a // ∀ (i : α), (a i).fst = i} (equiv.pi_equiv_subtype_sigma α π))))
The elements of a fintype
as a sorted list.
Equations
An encodable fintype
is equivalent to the same size fin
.
Equations
- encodable.fintype_equiv_fin = (list.nodup.nth_le_equiv_of_forall_mem_list (encodable.sorted_univ α) _ encodable.mem_sorted_univ).symm.trans (equiv.cast encodable.fintype_equiv_fin._proof_1)
Outputs the list of differences of the input list, that is
lower [a₁, a₂, ...] n = [a₁ - n, a₂ - a₁, ...]
Equations
- denumerable.lower (m :: l) n = (m - n) :: denumerable.lower l m
- denumerable.lower list.nil n = list.nil
Outputs the list of partial sums of the input list, that is
raise [a₁, a₂, ...] n = [n + a₁, n + a₁ + a₂, ...]
Equations
- denumerable.raise (m :: l) n = (m + n) :: denumerable.raise l (m + n)
- denumerable.raise list.nil n = list.nil
raise l n
is an non-decreasing sequence.
If α
is denumerable, then so is multiset α
. Warning: this is not the same encoding as used
in multiset.encodable
.
Equations
- denumerable.multiset = denumerable.mk' {to_fun := λ (s : multiset α), encodable.encode (denumerable.lower (multiset.sort has_le.le (multiset.map encodable.encode s)) 0), inv_fun := λ (n : ℕ), multiset.map (denumerable.of_nat α) ↑(denumerable.raise (denumerable.of_nat (list ℕ) n) 0), left_inv := _, right_inv := _}
Outputs the list of differences minus one of the input list, that is
lower' [a₁, a₂, a₃, ...] n = [a₁ - n, a₂ - a₁ - 1, a₃ - a₂ - 1, ...]
.
Equations
- denumerable.lower' (m :: l) n = (m - n) :: denumerable.lower' l (m + 1)
- denumerable.lower' list.nil n = list.nil
Outputs the list of partial sums plus one of the input list, that is
raise [a₁, a₂, a₃, ...] n = [n + a₁, n + a₁ + a₂ + 1, n + a₁ + a₂ + a₃ + 2, ...]
. Adding one each
time ensures the elements are distinct.
Equations
- denumerable.raise' (m :: l) n = (m + n) :: denumerable.raise' l (m + n + 1)
- denumerable.raise' list.nil n = list.nil
raise' l n
is a strictly increasing sequence.
Makes raise' l n
into a finset. Elements are distinct thanks to raise'_sorted
.
Equations
- denumerable.raise'_finset l n = {val := ↑(denumerable.raise' l n), nodup := _}
If α
is denumerable, then so is finset α
. Warning: this is not the same encoding as used
in finset.encodable
.
Equations
- denumerable.finset = denumerable.mk' {to_fun := λ (s : finset α), encodable.encode (denumerable.lower' (finset.sort has_le.le (finset.map (denumerable.eqv α).to_embedding s)) 0), inv_fun := λ (n : ℕ), finset.map (denumerable.eqv α).symm.to_embedding (denumerable.raise'_finset (denumerable.of_nat (list ℕ) n) 0), left_inv := _, right_inv := _}
The type lists on unit is canonically equivalent to the natural numbers.
Equations
- equiv.list_unit_equiv = {to_fun := list.length unit, inv_fun := λ (n : ℕ), list.replicate n unit.star(), left_inv := equiv.list_unit_equiv._proof_1, right_inv := equiv.list_unit_equiv._proof_2}
list ℕ
is equivalent to ℕ
.
Equations
If α
is equivalent to ℕ
, then list α
is equivalent to α
.