Documentation

PfsProgs25.Unit01.SimpleTerms

The Lean Context #

Simple Terms and Types #

We consider a few terms and types in Lean. These are definitions that do not involve recursion/induction or propositions.

The #check command displays the type of a term.

def two :
Equations
Instances For
    def five :
    Equations
    Instances For

      Function types #

      If α and β are types, then α → β is the type of functions from α to β.

      def cube (n : Nat) :

      The cube of a natural number.

      Equations
      Instances For
        def cubeGeneric {α : Type} [Mul α] (n : α) :
        α

        A generic function to compute the cube of a number. This defines the cube for a type α with a multiplication operation.

        Equations
        Instances For
          Equations
          Instances For
            def cube' :
            NatNat

            The cube of a natural number, using a lambda expression.

            Equations
            Instances For
              def cube'' (n : Nat) :

              The cube of a natural number, using a lambda expression with argument type specified.

              Equations
              Instances For

                Curried functions #

                Suppose we want to define the function f of two variables a and b taking value a + b + 1. We can define it as a function of one variable a returning a function of one variable b.

                def sum_of_squares :
                NatNatNat

                The function of two variables a and b taking value a * a + b * b, defined as an iterated lambda.

                Equations
                Instances For
                  def sum_of_squares' (a b : Nat) :

                  The function of two variables a and b taking value a * a + b * b, using arguments in a single pair of parentheses.

                  Equations
                  Instances For

                    The function of two variables a and b taking value a * a + b * b, using a lambda expression with two arguments.

                    Equations
                    Instances For

                      The function of two variables a and b taking value a * a + b * b, using a lambda expression with two arguments and type annotations for one. The other type is inferred.

                      Equations
                      Instances For

                        We used typeclasses above. We will discuss them later. For now, note that they are simply types or type families. However, there are plenty of type families that are not typeclasses.

                        Implicit parameters #

                        A function parameter may be implicit. This means that it is inferred by Lean from consistency of types. We can make a parameter implicit by enclosing it in curly braces {}.

                        A typeclass parameter is always implicit.

                        If we prefix a function application with @, then we must provide all implicit parameters explicitly. Conversely, we can use _ for an explicit parameter to make it implicit.

                        def doubleList {α : Type} (l : List α) :
                        List α
                        Equations
                        Instances For

                          When calling the function doubleList, we give only one argument, the list l. The type α is inferred from the type of l.

                          def doubleList' (α : Type) (l : List α) :
                          List α
                          Equations
                          Instances For

                            When calling the function doubleList', we must provide the type α explicitly.

                            When calling, we can make all parameters explicit or leave some explicit parameters to be deduced.

                            def doubleList''' {α : Type} (l : List α) :
                            List α
                            Equations
                            Instances For
                              def doubleList'' {α : Type u_1} (l : List α) :
                              {α✝ : Type} → List α
                              Equations
                              Instances For