Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix B: Toplevel Declarations

Every Lean file is a sequence of toplevel declarations. These are the building blocks of every program and proof. This appendix provides a quick reference for all declaration types, with links to detailed explanations in the main text.

Definitions and Proofs

DeclarationPurposeExample
defDefine a value or functionBasics
theoremState and prove a proposition (opaque)Basics, Proving
lemmaSame as theoremProving
exampleAnonymous proof (not saved)Type Theory
abbrevTransparent abbreviationBasics
opaqueHide implementationProofs
axiomUnproven assumptionProofs

The distinction between def and theorem matters for performance. Lean marks theorem proofs as opaque, meaning they are never unfolded during type checking. This keeps proof terms from bloating computations. Use def for values you need to compute with and theorem for propositions you need to prove.

Type Declarations

DeclarationPurposeExample
inductiveDefine type with constructorsData Structures
structureSingle-constructor with fieldsData Structures
classType class interfacePolymorphism
instanceType class implementationPolymorphism
mutualMutually recursive definitionsDependent Types

Organization

DeclarationPurposeExample
importLoad another moduleBasics
variableAuto-add to definitionsBasics
namespaceGroup under prefixBasics
sectionScope for variablesBasics
openBring names into scopeBasics
universeDeclare universe levelsType Theory
attributeAttach metadataPolymorphism
exportRe-export from namespaceBasics
notationCustom syntaxDependent Types
set_optionConfigure compilerType Theory

Interactive Commands

CommandPurposeExample
#evalEvaluate and printBasics
#checkDisplay typeBasics
#printPrint declaration infoBasics
#reduceReduce to normal formBasics

These commands are prefixed with # to distinguish them from regular declarations. They produce output but do not contribute to the compiled program. Use them liberally during development to inspect types and evaluate expressions.