Documentation

Mathlib.Logic.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 #

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 Encodable (α : Type u_1) :
Type u_1

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.

  • encode : α

    Encoding from Type α to ℕ

  • decode : Option α

    Decoding from ℕ to Option α

  • encodek (a : α) : decode (encode a) = some a

    Invariant relationship between encoding and decoding

Instances
    @[simp]
    theorem Encodable.encode_inj {α : Type u_1} [Encodable α] {a b : α} :
    encode a = encode b a = b
    @[instance 400]
    instance Encodable.countable {α : Type u_1} [Encodable α] :
    @[deprecated Encodable.surjective_decode_getD (since := "2026-01-05")]

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

    Equations
      Instances For
        def Encodable.ofLeftInjection {α : Type u_1} {β : Type u_2} [Encodable α] (f : βα) (finv : αOption β) (linv : ∀ (b : β), finv (f b) = some b) :

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

        Equations
          Instances For
            def Encodable.ofLeftInverse {α : 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
              Instances For
                def Encodable.ofEquiv {β : Type u_2} (α : Type u_3) [Encodable α] (e : β α) :

                Encodability is preserved by equivalence.

                Equations
                  Instances For
                    theorem Encodable.encode_ofEquiv {α : Type u_3} {β : Type u_4} [Encodable α] (e : β α) (b : β) :
                    encode b = encode (e b)
                    theorem Encodable.decode_ofEquiv {α : Type u_3} {β : Type u_4} [Encodable α] (e : β α) (n : ) :
                    @[instance 100]
                    instance IsEmpty.toEncodable {α : Type u_1} [IsEmpty α] :
                    Equations
                      instance Option.encodable {α : Type u_3} [h : Encodable α] :

                      If α is encodable, then so is Option α.

                      Equations
                        @[simp]
                        theorem Encodable.encode_some {α : Type u_1} [Encodable α] (a : α) :
                        def Encodable.decode₂ (α : Type u_3) [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
                          Instances For
                            theorem Encodable.mem_decode₂' {α : Type u_1} [Encodable α] {n : } {a : α} :
                            theorem Encodable.mem_decode₂ {α : Type u_1} [Encodable α] {n : } {a : α} :
                            a decode₂ α n encode a = n
                            theorem Encodable.decode₂_eq_some {α : Type u_1} [Encodable α] {n : } {a : α} :
                            decode₂ α n = some a encode a = n
                            @[simp]
                            theorem Encodable.decode₂_encode {α : Type u_1} [Encodable α] (a : α) :
                            theorem Encodable.decode₂_inj {α : Type u_1} [Encodable α] {n : } {a₁ a₂ : α} (h₁ : a₁ decode₂ α n) (h₂ : a₂ decode₂ α n) :
                            a₁ = a₂
                            theorem Encodable.encodek₂ {α : Type u_1} [Encodable α] (a : α) :

                            The encoding function has decidable range.

                            Equations
                              Instances For

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

                                Equations
                                  Instances For
                                    def Unique.encodable {α : Type u_1} [Unique α] :

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

                                    Equations
                                      Instances For
                                        def Encodable.encodeSum {α : Type u_1} {β : Type u_2} [Encodable α] [Encodable β] :
                                        α β

                                        Explicit encoding function for the sum of two encodable types.

                                        Equations
                                          Instances For
                                            def Encodable.decodeSum {α : Type u_1} {β : Type u_2} [Encodable α] [Encodable β] (n : ) :
                                            Option (α β)

                                            Explicit decoding function for the sum of two encodable types.

                                            Equations
                                              Instances For
                                                instance Sum.encodable {α : 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 : β) :
                                                  encode (Sum.inr b) = 2 * encode b + 1
                                                  @[simp]
                                                  theorem Encodable.decode_sum_val {α : Type u_1} {β : Type u_2} [Encodable α] [Encodable β] (n : ) :
                                                  noncomputable instance Prop.encodable :
                                                  Equations
                                                    def Encodable.encodeSigma {α : Type u_1} {γ : αType u_3} [Encodable α] [(a : α) → Encodable (γ a)] :
                                                    Sigma γ

                                                    Explicit encoding function for Sigma γ

                                                    Equations
                                                      Instances For
                                                        def Encodable.decodeSigma {α : Type u_1} {γ : αType u_3} [Encodable α] [(a : α) → Encodable (γ a)] (n : ) :

                                                        Explicit decoding function for Sigma γ

                                                        Equations
                                                          Instances For
                                                            instance Sigma.encodable {α : 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 : ) :
                                                              decode n = (decode (Nat.unpair n).1).bind fun (a : α) => Option.map (Sigma.mk a) (decode (Nat.unpair n).2)
                                                              @[simp]
                                                              theorem Encodable.encode_sigma_val {α : Type u_1} {γ : αType u_3} [Encodable α] [(a : α) → Encodable (γ a)] (a : α) (b : γ a) :
                                                              instance Encodable.Prod.encodable {α : 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 : ) :
                                                                decode n = (decode (Nat.unpair n).1).bind fun (a : α) => Option.map (Prod.mk a) (decode (Nat.unpair n).2)
                                                                @[simp]
                                                                theorem Encodable.encode_prod_val {α : Type u_1} {β : Type u_2} [Encodable α] [Encodable β] (a : α) (b : β) :
                                                                def Encodable.encodeSubtype {α : Type u_1} {P : αProp} [encA : Encodable α] :
                                                                { a : α // P a }

                                                                Explicit encoding function for a decidable subtype of an encodable type

                                                                Equations
                                                                  Instances For
                                                                    def Encodable.decodeSubtype {α : Type u_1} {P : αProp} [encA : Encodable α] [decP : DecidablePred P] (v : ) :
                                                                    Option { a : α // P a }

                                                                    Explicit decoding function for a decidable subtype of an encodable type

                                                                    Equations
                                                                      Instances For
                                                                        instance Subtype.encodable {α : Type u_1} {P : αProp} [encA : Encodable α] [decP : DecidablePred 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 : DecidablePred P] (a : Subtype P) :
                                                                          encode a = encode a
                                                                          instance Fin.encodable (n : ) :
                                                                          Equations

                                                                            The lift of an encodable type is encodable

                                                                            Equations
                                                                              instance PLift.encodable {α : Type u_1} [Encodable α] :

                                                                              The lift of an encodable type is encodable.

                                                                              Equations
                                                                                noncomputable def Encodable.ofInj {α : 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
                                                                                  Instances For
                                                                                    noncomputable def Encodable.ofCountable (α : Type u_3) [Countable α] :

                                                                                    If α is countable, then it has a (non-canonical) Encodable structure.

                                                                                    Equations
                                                                                      Instances For
                                                                                        def ULower (α : Type u_1) [Encodable α] :

                                                                                        ULower α : Type is an equivalent type in the lowest universe, given Encodable α.

                                                                                        Equations
                                                                                          Instances For
                                                                                            instance instEncodableULower {α : Type u_1} [Encodable α] :
                                                                                            Equations
                                                                                              def ULower.equiv (α : Type u_1) [Encodable α] :
                                                                                              α ULower α

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

                                                                                              Equations
                                                                                                Instances For
                                                                                                  def ULower.down {α : Type u_1} [Encodable α] (a : α) :

                                                                                                  Lowers an a : α into ULower α.

                                                                                                  Equations
                                                                                                    Instances For
                                                                                                      instance ULower.instInhabited {α : Type u_1} [Encodable α] [Inhabited α] :
                                                                                                      Equations
                                                                                                        def ULower.up {α : Type u_1} [Encodable α] (a : ULower α) :
                                                                                                        α

                                                                                                        Lifts an a : ULower α into α.

                                                                                                        Equations
                                                                                                          Instances For
                                                                                                            @[simp]
                                                                                                            theorem ULower.down_up {α : Type u_1} [Encodable α] {a : ULower α} :
                                                                                                            down a.up = a
                                                                                                            @[simp]
                                                                                                            theorem ULower.up_down {α : Type u_1} [Encodable α] {a : α} :
                                                                                                            (down a).up = 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 : α} :
                                                                                                            down a = down b a = b
                                                                                                            theorem ULower.ext {α : Type u_1} [Encodable α] {a b : ULower α} :
                                                                                                            a.up = b.upa = b
                                                                                                            theorem ULower.ext_iff {α : Type u_1} [Encodable α] {a b : ULower α} :
                                                                                                            a = b a.up = b.up
                                                                                                            def Encodable.chooseX {α : Type u_1} {p : αProp} [Encodable α] [DecidablePred p] (h : ∃ (x : α), p x) :
                                                                                                            { a : α // p a }

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

                                                                                                            Equations
                                                                                                              Instances For
                                                                                                                def Encodable.choose {α : Type u_1} {p : αProp} [Encodable α] [DecidablePred p] (h : ∃ (x : α), p x) :
                                                                                                                α

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

                                                                                                                Equations
                                                                                                                  Instances For
                                                                                                                    theorem Encodable.choose_spec {α : Type u_1} {p : αProp} [Encodable α] [DecidablePred p] (h : ∃ (x : α), p x) :
                                                                                                                    p (choose h)
                                                                                                                    theorem Encodable.axiom_of_choice {α : Type u_1} {β : αType u_2} {R : (x : α) → β xProp} [(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)

                                                                                                                    A constructive version of Classical.axiom_of_choice for Encodable types.

                                                                                                                    theorem Encodable.skolem {α : Type u_1} {β : αType u_2} {P : (x : α) → β xProp} [(a : α) → Encodable (β a)] [(x : α) → (y : β x) → Decidable (P x y)] :
                                                                                                                    (∀ (x : α), ∃ (y : β x), P x y) ∃ (f : (a : α) → β a), ∀ (x : α), P x (f x)

                                                                                                                    A constructive version of Classical.skolem for Encodable types.

                                                                                                                    def Encodable.encode' (α : Type u_1) [Encodable α] :
                                                                                                                    α

                                                                                                                    The encode function, viewed as an embedding.

                                                                                                                    Equations
                                                                                                                      Instances For
                                                                                                                        noncomputable def Directed.sequence {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] {r : ββProp} (f : αβ) (hf : Directed r 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
                                                                                                                          Instances For
                                                                                                                            theorem Directed.sequence_mono_nat {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] {r : ββProp} {f : αβ} (hf : Directed r f) (n : ) :
                                                                                                                            r (f (Directed.sequence f hf n)) (f (Directed.sequence f hf (n + 1)))
                                                                                                                            theorem Directed.rel_sequence {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] {r : ββProp} {f : αβ} (hf : Directed r f) (a : α) :
                                                                                                                            r (f a) (f (Directed.sequence f hf (Encodable.encode a + 1)))
                                                                                                                            theorem Directed.sequence_mono {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] [Preorder β] {f : αβ} (hf : Directed (fun (x1 x2 : β) => x1 x2) f) :
                                                                                                                            theorem Directed.le_sequence {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] [Preorder β] {f : αβ} (hf : Directed (fun (x1 x2 : β) => x1 x2) f) (a : α) :
                                                                                                                            theorem Directed.sequence_anti {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] [Preorder β] {f : αβ} (hf : Directed (fun (x1 x2 : β) => x1 x2) f) :
                                                                                                                            theorem Directed.sequence_le {α : Type u_1} {β : Type u_2} [Encodable α] [Inhabited α] [Preorder β] {f : αβ} (hf : Directed (fun (x1 x2 : β) => x1 x2) f) (a : α) :
                                                                                                                            def Quotient.rep {α : Type u_1} {s : Setoid α} [DecidableRel fun (x1 x2 : α) => x1 x2] [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
                                                                                                                              Instances For
                                                                                                                                theorem Quotient.rep_spec {α : Type u_1} {s : Setoid α} [DecidableRel fun (x1 x2 : α) => x1 x2] [Encodable α] (q : Quotient s) :
                                                                                                                                def encodableQuotient {α : Type u_1} {s : Setoid α} [DecidableRel fun (x1 x2 : α) => x1 x2] [Encodable α] :

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

                                                                                                                                Equations
                                                                                                                                  Instances For