The Environment
Lean 4 is a compiled language with a uniquely interactive development experience. Before writing your first program, let's understand the tools you'll use every day.
Interactive Evaluation with #eval
Unlike traditional compiled languages where you write code, compile, and run, Lean provides immediate feedback through the #eval command. This evaluates expressions directly in your editor and shows the result.
1-- Evaluate simple expressions2#eval 1 + 1 -- Output: 23#eval "Hello" ++ "!" -- Output: "Hello!"4#eval 2 ^ 10 -- Output: 102456-- Evaluate function calls7#eval String.length "Lean 4" -- Output: 68#eval List.reverse [1, 2, 3] -- Output: [3, 2, 1]The result appears in your editor's "Lean Infoview" panel (in VS Code) or inline as a comment. This makes exploration and learning incredibly fast—you never need to leave your editor.
Type Inspection with #check
While #eval runs code, #check reveals types. This is essential for understanding what you're working with.
1#check "Hello World" -- String2#check 42 -- Nat3#check true -- Bool4#check [1, 2, 3] -- List Nat56-- Check function types7#check String.length -- String → Nat8#check List.reverse -- {α : Type} → List α → List αNotice the arrow → in function types. This reads as "takes X and returns Y." For example, String → Nat means "a function that takes a String and returns a Nat."
#eval executes code and shows results. #check shows the type without executing. Use both constantly as you learn—they're your primary debugging tools.The Infoview Panel
In VS Code with the Lean 4 extension, the Infoview panel is your companion. It shows:
- Results from
#evaland#check - Type information when you hover over expressions
- Error messages with detailed explanations
- Goal state when writing proofs (covered in Tactics)
#check everywhere.Other Useful Commands
Lean provides several other inspection commands:
1-- Print the definition of something2#print Nat.add34-- Reduce an expression step by step5#reduce 2 + 267-- Search for functions by type (requires Mathlib)8-- #check @?_ : Nat → Nat → NatDeep Dive: Compiled vs Interpreted
When you run a Lean program as an executable, it compiles to efficient C code and then to native machine code. However, #eval runs in an interpreter for immediate feedback.
This means #eval is great for learning and debugging, but performance-critical code should be tested with actual compilation usinglake build.
Your First File
Create a file called Hello.lean and add:
1-- My first Lean file2#eval "Hello, Lean 4!"34-- Check some types5#check Nat6#check String → Bool78-- A simple definition (we'll cover this next)9def greeting := "Welcome to Lean!"Save the file and watch the Infoview update. You should see "Hello, Lean 4!" appear as the result of the #eval.