- Mathport uses matli4 to move tactics.
- Mathlib4 has syntax definitions ofr every single tactic that exists in
mathlib. - These only exist as syntax so far. We need to port this.
- The goal for this week is to have as many as possible knocked off.
§ Macro
- A tactic that expands to another tactic.
- Example:
_tactic. This expands into({}), which shows you the current state. -
macro_rulesneed a piece ofSyntax, and it expands into another tactic.
-- | do the iff.rfl as well.macro_rules | `(tactic| rfl => `(tactic| exact Iff.rfl)- Closed syntax categories:
syntax rcasesPatLo := .... - Open syntax categories:
syntax X.
§ How to collate info?
- Use
macroto define syntax + macro - Use
elabto define syntax + elaborator together. - Add command to list all places where something was extended.
- Add information into docstrings.
-
match_target.
§ Mapsto arrow
-
(x: α) \mapsto e: maybe annoying to parse. -
\lambda (x: \alpha) \mapsto e: easy to parse, but mathematicians don't know what lambda is.
§ ext tactic
- implemented as a macro tactic, which uses an
elabtactic.
§ colGt
-
syntax "ext" - Lean4 is whitespace sensitive, like python.
colGtsays that we can have the following syntax on a column that is greater than the current line.
ext x -- parsed as part of `x`. y -- parsed as part of `y`.z -- is not parsed as part of `x y`.- If in parens, we don't need colGt, because we want to allow something like:
ext (x y) -- should parse. § ppSpace
- Used when pretty printing a tactic.
§ Scoped syntax
-
scoped syntax "ext_or_skip: ... - This declares a syntax that is valid only for the current section/namespace.
- Trailing percent
ext_proof%is an indicator that it is a term macro / term elaboration. - Protected: identifier cannot appear without prefixing namespaces.
§ Trivia
-
{..}pattern matches on a struct.
§ Tactic development: Trace
- Create a new file
Mathlib/Tactic/Trace.lean - Move the syntax line from
./Mathlib/Mathport/Syntax.leanintoMathib/Tactic/Trace.lean. - On adding a file, add it to
Mathlib.lean. So we addimport Mathlib.Tactic.Trace - We also want to re-import the syntax into
Mathlib/Mathport/Syntax.lean. - We have now moved
traceinto its own file and hooked into the build system and extension. - The first thing to do is to find out what the tactic even does.
- Go to
traceat the mathlib docs.
-- Tactic.leanimport Leanopen Lean Meta Elabsyntax (name := trace) "trace " term : tacticelab "foo" : tactic => do -- `TacticM Unit` expected logInfo "hi"open Lean Meta Elab Tactic ~=open Leanopen Lean.Metaopen Lean.Elabopen Lean.Elab.Tactic- TacticM is a
MonadRef, which is aware of source spans to report the errors. so we can write:
elab "foo" : tactic => do logInfo "hi"- We can use
withRefto control the current source span where errors are reported.
elab tk:"foo" val:term : tactic => do withRef tk (logInfo val)- We want to evaluate the
val:term, because otherwise, it literally prints the syntax tree for things like(2 + (by trivial)). - Use
elabTermto elaborate the syntax into a term. -
set_option trace.Elab.definition true in ...which printso ut the declarations that are being sent to the kernel. -
elabissyntax+elab_rulestogether, just likemacroissyntax+macro_rulestogether. - Create a test file
test/trace.lean. Import the tactic, and write some examples. - Recompile, and check that the test works.
- How do we check that our port works?
§ Reducible
To clarify, @ [reducible ] marks the definition as reducible for typeclass inference specifically. By default typeclass inference avoids reducing because it would make the search very expensive.