Stage 01 · Step 3 of 50

Make a function.

Core Lean5 minutesLean 4.36.0

01The idea

A recipe takes ingredients and produces something. A function takes an input and returns an output.

Lemmi · Your guide for this idea
Lemmi pointing

Naming the recipe lets you use it again.

02In maths

The function double sends a natural number n to 2n.

Picture it

A function is a machine with a nameTop: the number 7 drops into a machine named double. Inside the machine the recipe reads 2 * n, where n is the input slot. Out comes 14. Bottom: how Lean checks double 7 = 14. It opens the definition, so double 7 becomes 2 * 7, then computes 14, which matches the right-hand side.7INPUTn = 7double(the name)2 * n(the recipe)14OUTPUTHOW LEAN CHECKS double 7 = 14double 7open it2 * 7compute14

Swipe sideways to see the whole picture.

A function is a machine with a name and a recipe. The parameter n is the slot where the input goes, so double 7 runs the recipe with 7 in the slot. To check double 7 = 14, rfl opens the machine, works out 2 * 7, and sees 14 on both sides.

03In Lean

This code comes directly from the file checked by Lean.

E03.leanTry it live (opens official Lean editor)
def double (n : Nat) : Nat := 2 * n

#eval double 7

example : double 7 = 14 := by
  rfl

A definition gives a name to an expression. A parameter names an input while we describe the function.

The parentheses declare the input and its type. The next type describes the output. The expression after := supplies the definition.

Applying a function means giving it an argument. The evaluation command applies double to one specific number.

The final claim records the result as an equality. Lean opens the definition of double to check it.

04Step by step

Choose a line to see Lean’s recorded goal state after that line.

After line 5
⊢ double 7 = 14
After line 5: example : double 7 = 14 := by
⊢ double 7 = 14
After line 6: rfl
no goals

05Lean says

Captured from Lean 4.36.0
14
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Define triple so it returns three times its input.

  2. A little stretch

    Define addThenDouble: add one to the input, then double the result.

Exercises/E03.leanTry it live (opens official Lean editor)
def triple (n : Nat) : Nat := sorry

def addThenDouble (n : Nat) : Nat := sorry

08A quick check

1. What does def introduce?

Read the answer and explanation

A named definition. A definition assigns a name to an expression.

2. What changes when double gets a different input?

Read the answer and explanation

The argument used in its body. The function body uses the supplied argument in place of its parameter.

One more idea in your toolkit.

Take your time. Progress is yours to mark.