Module 1: Why Lean·Lesson 2 of 16·3 min read

Set Up Lean 4 and Talk to It With #eval and #check

The problem: you cannot learn a language you cannot run

Reading about Lean only gets you so far. The fastest way to learn it is to type something, see what Lean says back, and adjust. So before any proofs, you need a place where Lean runs and answers you instantly.

This lesson gets you there two ways: a zero install option in the browser, and a proper local install for real projects. Then you will write your first file using two commands you will use constantly, #eval and #check.

Option one: the online playground

Go to https://live.lean-lang.org. You get an editor on the left and Lean's responses on the right. Nothing to install, nothing to configure.

This is perfect for the first few lessons and for every "Try this" exercise in this course. Its limit is that it is a single scratch file. For anything bigger, install Lean locally.

Option two: install Lean on your machine

Lean versions change often, and different projects pin different versions. A small tool called elan handles this for you. It installs lean and lake, Lean's build tool, and picks the right Lean version for each project by reading a file named lean-toolchain.

The official recommendation on lean-lang.org is to install VS Code, then install the Lean 4 extension from inside VS Code, and follow its setup guide. The extension installs elan for you and gives you live error messages as you type.

If you prefer the terminal, the elan README gives this command for Linux and macOS:

bash
curl https://elan.lean-lang.org/elan-init.sh -sSf | sh

Windows users have a PowerShell version in the same README. After installing, open a new terminal and check it worked:

bash
lean --version

Your first file: #eval

Create a file called First.lean and type:

lean
#eval 1 + 2
#eval "Hello, " ++ "Lean"
#check 1 + 2
#check "Hello"

Lines starting with # are commands for you, the human. They do not define anything. They ask Lean a question and show the answer in the editor, or in the terminal if you run lean First.lean.

#eval runs an expression and prints the result. Lean prints:

lean
-- 3
-- "Hello, Lean"

Your first file: #check

#check does not run anything. It asks Lean what type an expression has. For the last two lines above, Lean prints:

lean
-- 1 + 2 : Nat
-- "Hello" : String

Read the colon as "has type". So 1 + 2 : Nat means "1 + 2 is a natural number". You will lean on #check constantly, because in Lean the type of a thing tells you almost everything about it, including, later, what a proof proves.

Animation: Each line of the file gets an answer: #eval runs the expression, #check shows its type.

Defining something

Now define your own function with def:

lean
def greeting (name : String) : String :=
  "Hello, " ++ name ++ "!"

#eval greeting "Lean"
#check greeting

Lean prints:

lean
-- "Hello, Lean!"
-- greeting (name : String) : String

The #check output tells you greeting takes a String called name and returns a String. The next lessons explain this syntax properly. For now, notice that you got feedback without writing any tests.

The red squiggle

Now make a mistake on purpose:

lean
#eval "five" + 1

In VS Code or the playground, a red squiggle appears under the expression. The message starts with:

lean
-- error: failed to synthesize instance of type class
--   HAdd String Nat ?m.1

That looks scary, but it says something simple. HAdd is how Lean spells "addition between two types". Lean looked for a way to add a String and a Nat and found none. It refuses to guess what you meant.

Get used to this loop: write, see the squiggle, read the message, fix. It is the same loop you will use for proofs, where a red squiggle means a step does not follow.

Starting a real project with lake new

For anything beyond a single file, let Lake create a project:

bash
lake new hello
cd hello
lake build
lake exe hello

With Lean 4.36.0, lake new hello creates Main.lean, Hello.lean, a folder Hello/ containing Basic.lean, a lakefile.toml describing the project, a README.md, and a lean-toolchain file that pins the Lean version. Running lake exe hello builds the program and prints:

bash
Hello, world!

Open the folder in VS Code and the extension will pick up everything automatically. Each .lean file you add inside it can use #eval and #check just like your scratch file.

Try this

Paste this into https://live.lean-lang.org and predict each answer before you look:

lean
#eval 10 * 10
#check 10 * 10
#eval "Lean" ++ " " ++ "4"
#check "Lean" ++ " " ++ "4"

Then try #eval 10 + "ten" and read the error.

Sources

Try it yourselfEasy

Write and run your first definitions

Define `shout` so that it adds an exclamation mark to the end of a string, and `area` for the area of a rectangle with natural number sides. Then run the two #eval lines and check that the output matches the comments. Until you fill in the sorry holes, the #eval lines show an error saying the expression depends on sorry; that is expected.

def shout (s : String) : String :=
  sorry

def area (w h : Nat) : Nat :=
  sorry

#eval shout "Lean"   -- "Lean!"
#eval area 3 4       -- 12

example : area 3 4 = 12 := by
  sorry
Open in the Lean web editor

The official Lean editor runs in your browser and checks the file on Lean's server. Replace every sorry until no errors or warnings are left, then mark it solved.

The worked solution unlocks after your first attempt.

Saved in this browser. Pass the quiz while signed in to count it for the certificate.

Check what you learned

Answer all 3 questions. You need at least 2 right to pass.

1. What does #check do?

2. What is elan for?

3. Why does #eval "five" + 1 fail?