Documentation

PfsProgs25.Unit00.Egs

Examples of programs and in Lean #

We see our first examples of programs and proofs in Lean. A major goal is to illustrate functional programming, where we use recursions for loops and avoid mutable variables.

We define recursively summing to n and prove a theorem about it.

def sumToN (n : ) :

The function sumToN computes the sum of the first n natural numbers.

Equations
Instances For
    theorem sumToN_eq (n : ) :
    2 * sumToN n = n * (n + 1)

    The theorem sumToN_eq states that the sum of the first n natural numbers is n * (n + 1) / 2.

    @[irreducible]
    def sumToN' (n : ) :
    Equations
    Instances For

      Lean allows imperative programming, but it is not recommended. Here is an example of a program that uses a mutable variable.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For