Documentation

PfsProgs25.Unit06.Answer

Inductive Types #

The main type construction in Lean is the inductive type. We will see how to define inductive types and how to define functions by recursion on these types.

We have already seen one special case of inductive types: structures. In ShortAnswer, we saw another special case, namely enumerated types. We will now see a third special case, namely non-recursive inductive types. Finally, we will see the general case, namely recursive inductive types.

inductive Answer :

The Answer type is a non-recursive inductive type, with constructors a short answer or a short answer with reason.

Instances For

    An example of a term in the Answer type.

    Equations
    Instances For

      The short answer to an answer.

      Equations
      Instances For

        The dot notation.

        inductive LongAnswer :

        A LongAnswer is either a ShortAnswer or a LongAnswer with a reason.

        Instances For

          An example of a term in the LongAnswer type.

          Equations
          Instances For

            An example of a term in the LongAnswer type.

            Equations
            Instances For

              An example of a term in the LongAnswer type.

              Equations
              Instances For

                The short answer to a long answer. Lean has convenient notation for pattern matching

                Equations
                Instances For

                  The short answer to a long answer. Expanded definition

                  Equations
                  Instances For

                    The long answer from a short answer.

                    Equations
                    Instances For

                      Adding a reason to a long answer.

                      Equations
                      • ans.justify reason = ans.because reason
                      Instances For

                        An example of a term in the LongAnswer type.

                        Equations
                        Instances For

                          An example of a term in the LongAnswer type.

                          Equations
                          Instances For

                            An example of a term in the LongAnswer type.

                            Equations
                            Instances For
                              inductive EmptyAnswer :

                              There are no terms of this type.

                              Instances For

                                A function from EmptyAnswer to α for any type α. Such a function can only exist if EmptyAnswer has no terms.

                                Equations
                                Instances For
                                  noncomputable def EmptyAnswer.mapToType' (α : Type) :

                                  A function from EmptyAnswer to α for any type α. Such a function can only exist if EmptyAnswer has no terms.

                                  Equations
                                  Instances For