Documentation

PfsProgs25.Unit09.Trees

Inductive types and trees #

As we have seen, inductive types are given by constructors. To fully understand these, we must understand:

Since the constructs give elements of T, they must be (dependent) functions with codomain T, or (dependent) functions with codomain (dependent) functions with codomain T, etc. Thus, possible types of constructors are:

Parameterized inductive types #

We also have parametrized inductive types like lists, where we are defining a family of types List α for each α. In this case, the constructors can depend on the parameter α.

To be precise, a family of types is one of:

Indexed inductive types #

This is a more subtle notion. We will return to this later. We have already seen an example of this: MyNat.le is an indexed inductive type.

Trees #

We see various kinds of trees, giving various inductive types.

inductive BinTree (α : Type) :
Instances For
    instance instInhabitedBinTree {a✝ : Type} [Inhabited a✝] :
    Equations
    instance instReprBinTree {α✝ : Type} [Repr α✝] :
    Repr (BinTree α✝)
    Equations
    • instReprBinTree = { reprPrec := reprBinTree✝ }
    Equations
    Instances For
      Equations
      Instances For
        def BinTree.toList {α : Type} :
        BinTree αList α
        Equations
        Instances For
          Equations
          Instances For
            noncomputable def BinTree.sum' :
            Equations
            Instances For
              theorem BinTree.sum'_leaf (a : ) :
              (BinTree.leaf a).sum' = a
              theorem BinTree.sum'_node (t₁ t₂ : BinTree ) :
              (t₁.node t₂).sum' = t₁.sum' + t₂.sum'
              inductive BinTree' (α : Type) :
              Instances For
                instance instInhabitedBinTree' {a✝ : Type} [Inhabited a✝] :
                Equations
                instance instReprBinTree' {α✝ : Type} [Repr α✝] :
                Repr (BinTree' α✝)
                Equations
                • instReprBinTree' = { reprPrec := reprBinTree'✝ }
                Equations
                Instances For
                  inductive BoolTree (α : Type) :
                  Instances For
                    Equations
                    Instances For
                      def BoolTree.toList {α : Type} :
                      BoolTree αList α
                      Equations
                      Instances For
                        def BoolTree.toBinTree {α : Type} :
                        BoolTree αBinTree α
                        Equations
                        Instances For
                          def BoolTree.ofBinTree {α : Type} :
                          BinTree αBoolTree α
                          Equations
                          Instances For
                            inductive ListTree (α : Type) :
                            Instances For
                              instance instInhabitedListTree {a✝ : Type} :
                              Equations
                              instance instReprListTree {α✝ : Type} [Repr α✝] :
                              Repr (ListTree α✝)
                              Equations
                              • instReprListTree = { reprPrec := reprListTree✝ }
                              @[irreducible]
                              def ListTree.toList {α : Type} :
                              ListTree αList α
                              Equations
                              Instances For

                                Lean is pragmatic and allows imperative programming. We can define the sum of a list. Technically, this is in a monad.

                                def listSumNat (l : List ) :
                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  def listSumNat' (l : List ) :
                                  Equations
                                  Instances For
                                    Equations
                                    Instances For
                                      Equations
                                      Instances For
                                        def listTill :
                                        Equations
                                        Instances For
                                          Equations
                                          Instances For
                                            inductive FinTree (α : Type) :
                                            Instances For
                                              def FinTree.ofBinTree {α : Type} :
                                              BinTree αFinTree α
                                              Equations
                                              Instances For
                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  inductive CantorTree.T :
                                                  Instances For

                                                    The idea of Cantor's theorem is to show that we can define p: TBool such that p (node p) ≠ p p. If not for the positivity condition, we could define p (node x) = ¬ x (node x).

                                                    Equations
                                                    Instances For