Errors You May See
These came up while I wrote the programs in this book.
A reserved word as a name
1 def recall := 1
1 error: unexpected token 'recall'; expected identifier
recall is a reserved word in Lean. Pick another name.
A line that starts with a minus sign
1 def g : Int :=
2 let a := (5 : Int)
3 -1 * a
Lean reads this as (5 : Int) - 1 * a and then reports that a term is missing. Put the factors in another order, a * -1, or wrap the line in parentheses.
A field of an unknown type
1 let avgdl := (docs.map (·.length)).sum.toFloat
In the BM25 program this line gave the error Type of x is not known; cannot resolve field length. Naming the function in full fixed it: docs.map List.length.
A missing default value
arr[i]! must return some value when the index is out of range, before the program stops. Lean needs an Inhabited instance for the element type. If you see failed to synthesize instance of type class Inhabited, add deriving Inhabited to your type.
An import after code
1 def main : IO Unit := do
2 IO.println "hi"
3
4 import Std.Data.HashMap
1 error: invalid 'import' command, it must be used in the beginning of the file
Every import must come before the first declaration. Move it to the top of the file.
An identifier from a missing import
1 def empty : Std.HashMap String Nat := ∅
1 error(lean.unknownIdentifier): Unknown identifier `Std.HashMap`
Lean’s core library is small. Types from Std need an import line; add import Std.Data.HashMap.
A Nat where a Float is expected
1 def average (total n : Nat) : Float := total / n
1 error: Type mismatch
2 total / n
3 has type
4 Nat
5 but is expected to have type
6 Float
Both operands are Nat, so / returns a Nat. Convert one side: total.toFloat / n.toFloat.
A missing match case
1 inductive Color where
2 | red | green | blue
3
4 def name : Color → String
5 | .red => "red"
1 error: Missing cases:
2 Color.green
3 Color.blue
Add the two cases. Lean names every constructor you left out.
A subtraction that floors at zero
1 #eval 2 - 5 -- 0
No error. Subtraction on Nat stops at zero, and division drops the remainder. Use Int or Rat when negative values matter, and write the type ascription.
Summary
| Message | Cause | Fix |
|---|---|---|
unexpected token 'recall'; expected identifier |
reserved word as a name | choose another name |
term missing after a line that starts with -
|
the minus reads as subtraction | reorder factors or add parentheses |
Type of x is not known; cannot resolve field length |
dot call on a function whose parameter type is unknown | name the function in full |
failed to synthesize instance ... Inhabited |
arr[i]! needs a default value |
add deriving Inhabited
|
invalid 'import' command |
import after code | move imports to the top |
Unknown identifier Std.HashMap |
missing import | add the import line |
Type mismatch ... Nat but Float |
mixed arithmetic | convert with .toFloat
|
Missing cases: ... |
incomplete match | add the missing constructors |
Reading an Error
Lean prints file, line, and column, then the message. Read the end of the message first: it names the expected type, the found type, or the missing case. The caret points at the term Lean rejected, which is often one argument rather than the whole function. Use #check on the name and #eval on a small input. A type ascription such as (x : Float) often fixes a message about implicit arguments, because it gives Lean the type it could not infer. Many messages point at dot notation or implicit parameters rather than at logic.
An error at check time means the file did not compile, so lean --run never starts. An exception at run time, such as arr[i]! out of range or a failed process, prints and exits. try and catch handle the second kind only.