λleanlab

Lean examples

Read the source and explanations, then open a file in the editor to run it. These examples use the included core library.

Getting started

  • Welcome

    Run a program, check a proof, and find your way around leanlab.

  • Prove 2 + 2 = 4

    Compute an answer, then prove the equality.

  • Curry–Howard correspondence

    A proposition is a type; a proof is a term of that type.

  • Hello, world

    Define a function and print its result from main.

  • Values & types

    Evaluate expressions with #eval and inspect their types with #check.

  • Writing functions

    Functions take inputs and return values. Application uses spaces, so parentheses group expressions.

Programs

  • Fibonacci

    Turn the mathematical definition of Fibonacci numbers into a recursive program.

  • Lists & pipelines

    Build a small data pipeline with immutable lists and higher-order functions.

  • Pattern matching

    Describe a small set of possibilities and handle each one explicitly.

  • Structures

    Group named fields into your own data type, then write a function that uses it.

  • Handling missing values

    Option makes missing values explicit, so the compiler helps you handle both outcomes.

  • Factorial

    Lean checks that ordinary recursive definitions terminate.

  • Parsing with error messages

    Use Except and do to return a value or an explanation of failure.

  • Arrays and bounds

    Build an array and access an element with a checked bound.

  • Type classes and instances

    Write one function whose behavior depends on the input type.

Proofs

Classic mathematics

Number theory

Counting & logic

Algebra & fields

Cryptography

Advanced Lean