Lean4 Cheat-sheet
Extracting information
-
Extract the goal:
Lean.Elab.Tactic.getMainGoalUse as
let goal ← Lean.Elab.Tactic.getMainGoal -
Extract the declaration out of a metavariable:
mvarId.getDeclwhenmvarId : Lean.MVarIdis in context. For instance,mvarIdcould be the goal extracted usinggetMainGoal -
Extract the type of a metavariable:
mvarId.getTypewhenmvarId : Lean.MVarIdis in context. -
Extract the type of the main goal:
Lean.Elab.Tactic.getMainTargetUse as
let goal_type ← Lean.Elab.Tactic.getMainTargetAchieves the same as
let goal ← Lean.Elab.Tactic.getMainGoal
let goal_type ← goal.getType
-
Extract local context:
Lean.MonadLCtx.getLCtxUse as
let lctx ← Lean.MonadLCtx.getLCtx -
Extract the name of a declaration:
Lean.LocalDecl.userName ldeclwhenldecl : Lean.LocalDeclis in context -
Extract the type of an expression:
Lean.Meta.inferType exprwhenexpr : Lean.Expris an expression in contextUse as
let expr_type ← Lean.Meta.inferType expr
Playing around with expressions
-
Convert a declaration into an expression:
Lean.LocalDecl.toExprUse as
ldecl.toExpr, whenldecl : Lean.LocalDeclis in contextFor instance,
ldeclcould belet ldecl ← Lean.MonadLCtx.getLCtx -
Check whether two expressions are definitionally equal:
Lean.Meta.isDefEq ex1 ex2whenex1 ex2 : Lean.Exprare in context. Returns aLean.MetaM Bool -
Close a goal:
Lean.Elab.Tactic.closeMainGoal exprwhenexpr : Lean.Expris in context
Further commands
- meta-sorry:
Lean.Elab.admitGoal goal, whengoal : Lean.MVarIdis the current goal
Printing and errors
-
Print a "permanent" message in normal usage:
Lean.logInfo f!"Hi, I will print\n{Syntax}" -
Print a message while debugging:
dbg_trace f!"1) goal: {Syntax_that_will_be_interpreted}". -
Throw an error:
Lean.Meta.throwTacticEx name mvar message_datawherename : Lean.Nameis the name of a tactic andmvarcontains error data.Use as
Lean.Meta.throwTacticExtac goal (m!"unable to find matching hypothesis of type ({goal_type})")where them!formatting builds aMessageData` for better printing of terms
TODO: Add?
- Lean.LocalContext.forM
- Lean.LocalContext.findDeclM?