# mathlibdocumentation

data.equiv.encodable.basic

# Encodable types #

This file defines encodable (constructively countable) types as a typeclass. This is used to provide explicit encode/decode functions from and to ℕ, with the information that those functions are inverses of each other. The difference with denumerable is that finite types are encodable. For infinite types, encodable and denumerable agree.

## Main declarations #

• encodable α: States that there exists an explicit encoding function encode : α → ℕ with a partial inverse decode : ℕ → option α.
• decode₂: Version of decode that is equal to none outside of the range of encode. Useful as we do not require this in the definition of decode.
• ulower α: Any encodable type has an equivalent type living in the lowest universe, namely a subtype of ℕ. ulower α finds it.

## Implementation notes #

The point of asking for an explicit partial inverse decode : ℕ → option α to encode : α → ℕ is to make the range of encode decidable even when the finiteness of α is not.

@[class]
structure encodable (α : Type u_1) :
Type u_1
• encode : α →
• decode :
• encodek : ∀ (a : α),

Constructively countable type. Made from an explicit injection encode : α → ℕ and a partial inverse decode : ℕ → option α. Note that finite types are countable. See denumerable if you wish to enforce infiniteness.

Instances
theorem encodable.encode_injective {α : Type u_1} [encodable α] :
@[simp]
theorem encodable.encode_inj {α : Type u_1} [encodable α] {a b : α} :
a = b
theorem encodable.surjective_decode_iget (α : Type u_1) [encodable α] [inhabited α] :
def encodable.decidable_eq_of_encodable (α : Type u_1) [encodable α] :

An encodable type has decidable equality. Not set as an instance because this is usually not the best way to infer decidability.

Equations
def encodable.of_left_injection {α : Type u_1} {β : Type u_2} [encodable α] (f : β → α) (finv : α → ) (linv : ∀ (b : β), finv (f b) = some b) :

If α is encodable and there is an injection f : β → α, then β is encodable as well.

Equations
def encodable.of_left_inverse {α : Type u_1} {β : Type u_2} [encodable α] (f : β → α) (finv : α → β) (linv : ∀ (b : β), finv (f b) = b) :

If α is encodable and f : β → α is invertible, then β is encodable as well.

Equations
def encodable.of_equiv {β : Type u_2} (α : Type u_1) [encodable α] (e : β α) :

Encodability is preserved by equivalence.

Equations
• = _
@[simp]
theorem encodable.encode_of_equiv {α : Type u_1} {β : Type u_2} [encodable α] (e : β α) (b : β) :
@[simp]
theorem encodable.decode_of_equiv {α : Type u_1} {β : Type u_2} [encodable α] (e : β α) (n : ) :
@[protected, instance]
Equations
@[simp]
theorem encodable.encode_nat (n : ) :
@[simp]
theorem encodable.decode_nat (n : ) :
@[protected, instance]
Equations
@[protected, instance]
Equations
@[simp]
theorem encodable.encode_star  :
@[simp]
@[simp]
theorem encodable.decode_unit_succ (n : ) :
@[protected, instance]
def encodable.option {α : Type u_1} [h : encodable α] :

If α is encodable, then so is option α.

Equations
@[simp]
theorem encodable.encode_none {α : Type u_1} [encodable α] :
@[simp]
theorem encodable.encode_some {α : Type u_1} [encodable α] (a : α) :
@[simp]
theorem encodable.decode_option_zero {α : Type u_1} [encodable α] :
0 =
@[simp]
theorem encodable.decode_option_succ {α : Type u_1} [encodable α] (n : ) :
n.succ =
def encodable.decode₂ (α : Type u_1) [encodable α] (n : ) :

Failsafe variant of decode. decode₂ α n returns the preimage of n under encode if it exists, and returns none if it doesn't. This requirement could be imposed directly on decode but is not to help make the definition easier to use.

Equations
theorem encodable.mem_decode₂' {α : Type u_1} [encodable α] {n : } {a : α} :
a a
theorem encodable.mem_decode₂ {α : Type u_1} [encodable α] {n : } {a : α} :
a
theorem encodable.decode₂_eq_some {α : Type u_1} [encodable α] {n : } {a : α} :
= some a
@[simp]
theorem encodable.decode₂_encode {α : Type u_1} [encodable α] (a : α) :
theorem encodable.decode₂_ne_none_iff {α : Type u_1} [encodable α] {n : } :
theorem encodable.decode₂_is_partial_inv {α : Type u_1} [encodable α] :
theorem encodable.decode₂_inj {α : Type u_1} [encodable α] {n : } {a₁ a₂ : α} (h₁ : a₁ ) (h₂ : a₂ ) :
a₁ = a₂
theorem encodable.encodek₂ {α : Type u_1} [encodable α] (a : α) :

The encoding function has decidable range.

Equations
def encodable.equiv_range_encode (α : Type u_1) [encodable α] :

An encodable type is equivalent to the range of its encoding function.

Equations
def unique.encodable {α : Type u_1} [unique α] :

A type with unique element is encodable. This is not an instance to avoid diamonds.

Equations
def encodable.encode_sum {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] :
α β

Explicit encoding function for the sum of two encodable types.

Equations
def encodable.decode_sum {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] (n : ) :
option β)

Explicit decoding function for the sum of two encodable types.

Equations
• = encodable.decode_sum._match_1 n.bodd_div2
• encodable.decode_sum._match_1 (tt, m) =
• encodable.decode_sum._match_1 (ff, m) =
@[protected, instance]
def encodable.sum {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] :
encodable β)

If α and β are encodable, then so is their sum.

Equations
@[simp]
theorem encodable.encode_inl {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] (a : α) :
@[simp]
theorem encodable.encode_inr {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] (b : β) :
@[simp]
theorem encodable.decode_sum_val {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] (n : ) :
@[protected, instance]
Equations
@[simp]
theorem encodable.encode_tt  :
@[simp]
theorem encodable.encode_ff  :
@[simp]
theorem encodable.decode_zero  :
@[simp]
theorem encodable.decode_one  :
theorem encodable.decode_ge_two (n : ) (h : 2 n) :
@[protected, instance]
noncomputable def encodable.Prop  :
Equations
def encodable.encode_sigma {α : Type u_1} {γ : α → Type u_3} [encodable α] [Π (a : α), encodable (γ a)] :

Explicit encoding function for sigma γ

Equations
def encodable.decode_sigma {α : Type u_1} {γ : α → Type u_3} [encodable α] [Π (a : α), encodable (γ a)] (n : ) :

Explicit decoding function for sigma γ

Equations
@[protected, instance]
def encodable.sigma {α : Type u_1} {γ : α → Type u_3} [encodable α] [Π (a : α), encodable (γ a)] :
Equations
@[simp]
theorem encodable.decode_sigma_val {α : Type u_1} {γ : α → Type u_3} [encodable α] [Π (a : α), encodable (γ a)] (n : ) :
n = (nat.unpair n).fst).bind (λ (a : α), (encodable.decode (γ a) (nat.unpair n).snd))
@[simp]
theorem encodable.encode_sigma_val {α : Type u_1} {γ : α → Type u_3} [encodable α] [Π (a : α), encodable (γ a)] (a : α) (b : γ a) :
@[protected, instance]
def encodable.prod {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] :
encodable × β)

If α and β are encodable, then so is their product.

Equations
@[simp]
theorem encodable.decode_prod_val {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] (n : ) :
encodable.decode × β) n = (nat.unpair n).fst).bind (λ (a : α), (nat.unpair n).snd))
@[simp]
theorem encodable.encode_prod_val {α : Type u_1} {β : Type u_2} [encodable α] [encodable β] (a : α) (b : β) :
def encodable.encode_subtype {α : Type u_1} {P : α → Prop} [encA : encodable α] :
{a // P a}

Explicit encoding function for a decidable subtype of an encodable type

Equations
def encodable.decode_subtype {α : Type u_1} {P : α → Prop} [encA : encodable α] [decP : decidable_pred P] (v : ) :
option {a // P a}

Explicit decoding function for a decidable subtype of an encodable type

Equations
@[protected, instance]
def encodable.subtype {α : Type u_1} {P : α → Prop} [encA : encodable α] [decP : decidable_pred P] :
encodable {a // P a}

A decidable subtype of an encodable type is encodable.

Equations
theorem encodable.subtype.encode_eq {α : Type u_1} {P : α → Prop} [encA : encodable α] [decP : decidable_pred P] (a : subtype P) :
@[protected, instance]
def encodable.fin (n : ) :
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
def encodable.ulift {α : Type u_1} [encodable α] :

The lift of an encodable type is encodable.

Equations
@[protected, instance]
def encodable.plift {α : Type u_1} [encodable α] :

The lift of an encodable type is encodable.

Equations
noncomputable def encodable.of_inj {α : Type u_1} {β : Type u_2} [encodable β] (f : α → β) (hf : function.injective f) :

If β is encodable and there is an injection f : α → β, then α is encodable as well.

Equations
@[protected, instance]
def ulower.decidable_eq (α : Type u_1) [encodable α] :
def ulower (α : Type u_1) [encodable α] :
Type

ulower α : Type is an equivalent type in the lowest universe, given encodable α.

Equations
@[protected, instance]
def ulower.encodable (α : Type u_1) [encodable α] :
def ulower.equiv (α : Type u_1) [encodable α] :
α

The equivalence between the encodable type α and ulower α : Type.

Equations
def ulower.down {α : Type u_1} [encodable α] (a : α) :

Lowers an a : α into ulower α.

Equations
@[protected, instance]
def ulower.inhabited {α : Type u_1} [encodable α] [inhabited α] :
Equations
def ulower.up {α : Type u_1} [encodable α] (a : ulower α) :
α

Lifts an a : ulower α into α.

Equations
@[simp]
theorem ulower.down_up {α : Type u_1} [encodable α] {a : ulower α} :
= a
@[simp]
theorem ulower.up_down {α : Type u_1} [encodable α] {a : α} :
@[simp]
theorem ulower.up_eq_up {α : Type u_1} [encodable α] {a b : ulower α} :
a.up = b.up a = b
@[simp]
theorem ulower.down_eq_down {α : Type u_1} [encodable α] {a b : α} :
a = b
@[protected, ext]
theorem ulower.ext {α : Type u_1} [encodable α] {a b : ulower α} :
a.up = b.upa = b
def encodable.choose_x {α : Type u_1} {p : α → Prop} [encodable α] (h : ∃ (x : α), p x) :
{a // p a}

Constructive choice function for a decidable subtype of an encodable type.

Equations
def encodable.choose {α : Type u_1} {p : α → Prop} [encodable α] (h : ∃ (x : α), p x) :
α

Constructive choice function for a decidable predicate over an encodable type.

Equations
theorem encodable.choose_spec {α : Type u_1} {p : α → Prop} [encodable α] (h : ∃ (x : α), p x) :
p
theorem encodable.axiom_of_choice {α : Type u_1} {β : α → Type u_2} {R : Π (x : α), β x → Prop} [Π (a : α), encodable (β a)] [Π (x : α) (y : β x), decidable (R x y)] (H : ∀ (x : α), ∃ (y : β x), R x y) :
∃ (f : Π (a : α), β a), ∀ (x : α), R x (f x)
theorem encodable.skolem {α : Type u_1} {β : α → Type u_2} {P : Π (x : α), β x → Prop} [c : Π (a : α), encodable (β a)] [d : Π (x : α) (y : β x), decidable (P x y)] :
(∀ (x : α), ∃ (y : β x), P x y) ∃ (f : Π (a : α), β a), ∀ (x : α), P x (f x)
def encodable.encode' (α : Type u_1) [encodable α] :
α

The encode function, viewed as an embedding.

Equations
@[protected, instance]
def encodable.order.preimage.is_trans {α : Type u_1} [encodable α] :
@[protected, instance]
def encodable.order.preimage.is_antisymm {α : Type u_1} [encodable α] :
@[protected, instance]
def encodable.order.preimage.is_total {α : Type u_1} [encodable α] :
@[protected]
noncomputable def directed.sequence {α : Type u_1} {β : Type u_2} [encodable α] [inhabited α] {r : β → β → Prop} (f : α → β) (hf : f) :
→ α

Given a directed r function f : α → β defined on an encodable inhabited type, construct a noncomputable sequence such that r (f (x n)) (f (x (n + 1))) and r (f a) (f (x (encode a + 1)).

Equations
• (n + 1) = let p : α := n in directed.sequence._match_1 f hf p n)
• 0 = default
• directed.sequence._match_1 f hf p (some a) =
• directed.sequence._match_1 f hf p none =
theorem directed.sequence_mono_nat {α : Type u_1} {β : Type u_2} [encodable α] [inhabited α] {r : β → β → Prop} {f : α → β} (hf : f) (n : ) :
r (f hf n)) (f hf (n + 1)))
theorem directed.rel_sequence {α : Type u_1} {β : Type u_2} [encodable α] [inhabited α] {r : β → β → Prop} {f : α → β} (hf : f) (a : α) :
r (f a) (f hf + 1)))
theorem directed.sequence_mono {α : Type u_1} {β : Type u_2} [encodable α] [inhabited α] [preorder β] {f : α → β} (hf : f) :
monotone (f hf)
theorem directed.le_sequence {α : Type u_1} {β : Type u_2} [encodable α] [inhabited α] [preorder β] {f : α → β} (hf : f) (a : α) :
f a f hf + 1))
def quotient.rep {α : Type u_1} {s : setoid α} [encodable α] (q : quotient s) :
α

Representative of an equivalence class. This is a computable version of quot.out for a setoid on an encodable type.

Equations
theorem quotient.rep_spec {α : Type u_1} {s : setoid α} [encodable α] (q : quotient s) :
def encodable_quotient {α : Type u_1} {s : setoid α} [encodable α] :

The quotient of an encodable space by a decidable equivalence relation is encodable.

Equations