Ask Lean about types

Use #check to read a type, and tell data apart from statements.

9 minBeginner#checkType and Proppartial applicationreading answers
0/10 completed in this course

Every expression in Lean has a type, and #check is how you ask for it. It elaborates the expression and prints expression : type. Nothing is run. #eval is the other half of the pair: it runs the expression and prints the value, which only works when the result is something Lean knows how to display.

A function comes back described the way it was declared. #check double answers double (n : Nat) : Nat, naming the parameter. Apply it and the answer changes: #check double 3 answers double 3 : Nat, because the argument is supplied and what is left is a number.

Types are expressions too, so you can ask about them. #check Nat answers Nat : Type. #check (2 + 2 = 4) answers 2 + 2 = 4 : Prop. That distinction carries the rest of the Academy: Type classifies data, Prop classifies statements, and a value whose type is a Prop is a proof of it.

This is also the fastest way to understand an error. When two things do not fit, ask for the type of each piece on its own. The mismatch is usually obvious once both answers are in front of you.

Worked example

#check 42              -- 42 : Nat
#check "Lean"          -- "Lean" : String
#check true            -- Bool.true : Bool

#check greet           -- greet (name : String) : String
#eval greet "Lean"     -- "Hello, Lean"

#check Nat             -- Nat : Type
#check Prop            -- Prop : Type
#check (2 + 2 = 4)     -- 2 + 2 = 4 : Prop

Look at #check true: Lean answers Bool.true, the constructor’s full name, not the text you typed. #check reports what the expression elaborates to. Your exercise asks the three kinds of question about double: the function itself, the function applied to something, and a claim about the result.

Takeaway

Ask Lean instead of guessing. #check reports a type, #eval computes a value, and Type and Prop tell data and statements apart.

Your exercise

Ask Lean for the type of double, the type of double 3, and the type of the claim double 3 = 6.

Suggested steps
  1. Ask for the type of the function itself
  2. Ask for the type of the function applied to 3
  3. Ask for the type of the claim double 3 = 6

These marks only recognize text. Another correct proof may use different steps; Lean checks whether it works.

Exercise.lean
1def double (n : Nat) : Nat := n + n
2def greet (name : String) : String := "Hello, " ++ name
given
Loading editor…

This exercise is judged by what Lean prints. Write the commands, press Check, then read the Output tab.

ReadyLn 1, Col 1Lean 4
Draft saved in this browser
Stuck?
Recall and apply

Knowledge check

Answer without looking back, then check your reasoning.

0of 3
correct
Question 1 of 3What does #check double 3 report?
Question 2 of 3What is the difference between Type and Prop?
Question 3 of 3Which command runs the expression?