Lean4 Foundations
Introduction to Lean 4
What is Lean 4?
Lean 4 is a special kind of tool. It's two things at once: a functional programming language and an interactive theorem prover. This means you can use it to write software, much like you would with Python or JavaScript. But you can also use it to write mathematical proofs that a computer can check for correctness.
As a functional programming language, Lean treats computation as the evaluation of mathematical functions. This approach avoids changing state and mutable data, which can make programs more predictable and easier to reason about.
As a theorem prover, Lean acts like an extremely meticulous assistant for mathematicians and computer scientists. You state a mathematical claim (a theorem) and then write out the logical steps to prove it. Lean checks every single step to ensure it's valid. If you make a mistake, it tells you immediately. This makes it possible to build large, complex proofs with a high degree of confidence that they are correct.
Essentially, Lean 4 lets you write programs and formally prove they work as intended, all within the same environment.
Getting Set Up
To get started with Lean, you'll need a couple of things: the Lean toolchain itself and a code editor called Visual Studio Code (VS Code). The best way to install Lean is using elan, its official version manager.
First, open a terminal (on macOS or Linux) or PowerShell (on Windows). Then, run the appropriate command for your system. This will download and run the elan installer, which sets up Lean for you.
# For macOS or Linux
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh
If you're on Windows, use this command in PowerShell:
# For Windows (in PowerShell)
powershell -c "irm https://raw.githubusercontent.com/leanprover/elan/master/elan-init.ps1 -outfile elan-init.ps1; ./elan-init.ps1"
After the installer finishes, you'll need to restart your terminal or PowerShell for the changes to take effect. Next, download and install Visual Studio Code from its official website. Once it's installed, open it, go to the Extensions view (the icon with four squares on the side), and search for lean4. Install the official Lean 4 extension published by the Lean Community.
Your First Lean Project
Lean projects are managed by a build tool called lake. To create a new project, navigate to a directory where you want to store your work and run the following command in your terminal:
lake new my_first_project
This creates a new folder called my_first_project with all the necessary files to get started. Now, open this folder in VS Code. You can do this from your terminal:
cd my_first_project
code .
Inside VS Code, you'll see a few files. The main one to look at is MyFirstProject.lean. This is where you'll write your Lean code. Let's replace the default content with a simple theorem.
def hello := "world"
-- This is a simple theorem stating that 1 + 1 = 2.
theorem one_plus_one_is_two : 1 + 1 = 2 :=
rfl -- The proof is 'by reflexivity'
As soon as you open this .lean file, the Lean 4 extension will start working in the background. Now, let's explore the interactive environment.
The Interactive Environment
The most powerful feature of using Lean in VS Code is the immediate feedback. Click your cursor on the line with the theorem keyword. You should see a window pop up called the "Lean Infoview".
The Infoview shows you your current progress on a proof. At the start of our simple theorem, it will show one goal:
⊢ 1 + 1 = 2
The ⊢ symbol can be read as "we need to prove." So, the goal is to prove that 1 + 1 = 2 is true.
Now, move your cursor to the end of the rfl line. You should see the message "Goals accomplished! 🎉". The term rfl is a tactic that proves things are equal by definition. Since Lean knows that 1 + 1 simplifies to 2, rfl is enough to complete the proof.
Try changing the theorem to something false, like 1 + 1 = 3. You'll immediately see a red squiggly line under rfl, and the Infoview will show an error. This instant feedback is what makes Lean so effective for both programming and writing proofs.
What are the two primary functions of the Lean 4 tool?
Which command-line tool is used to create a new Lean project?
