The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V01. The language of Mathlib

Mathlib.Util.Notation3

64 declarations. Mathlib docs · Source

The notation3 macro, simulating Lean 3's notation.

Syntaxes supporting notation3

Expression matching

A more complicated part of notation3 is the delaborator generator. While notation relies on generating app unexpanders, we instead generate a delaborator directly so that we can control how binders are formatted (we want to be able to know the types of binders, whether a lambda is a constant function, and whether it is Prop-valued, which are not things we can answer once we pass to app unexpanders).

The notation3 command

scoped[ns] support

def Mathlib.Notation3.MatchState.ctorIdx E1 #

Mathlib.Notation3.MatchState → ℕ

theorem Mathlib.Notation3.MatchState.mk.injEq E0 #

∀ (vars : Std.HashMap Lean.Name (Lean.SubExpr × Lean.LocalContext × Lean.LocalInstances))
  (scopeState : Option (Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized)))
  (foldState : Std.HashMap Lean.Name (Array Lean.Term))
  (vars_1 : Std.HashMap Lean.Name (Lean.SubExpr × Lean.LocalContext × Lean.LocalInstances))
  (scopeState_1 : Option (Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized)))
  (foldState_1 : Std.HashMap Lean.Name (Array Lean.Term)),
  ({ vars := vars, scopeState := scopeState, foldState := foldState } =
      { vars := vars_1, scopeState := scopeState_1, foldState := foldState_1 }) =
    (vars = vars_1 ∧ scopeState = scopeState_1 ∧ foldState = foldState_1)

theorem Mathlib.Notation3.DelabKey.other.injEq E0 #

∀ (key key_1 : Lean.Name),
  (Mathlib.Notation3.DelabKey.other key = Mathlib.Notation3.DelabKey.other key_1) = (key = key_1)

theorem Mathlib.Notation3.DelabKey.other.inj E1 #

∀ {key key_1 : Lean.Name}, Mathlib.Notation3.DelabKey.other key = Mathlib.Notation3.DelabKey.other key_1 → key = key_1

abbrev Mathlib.Notation3.DelabKey.ctorElimType E1 #

{motive : Mathlib.Notation3.DelabKey → Sort u} → ℕ → Sort (max 1 u)

def Mathlib.Notation3.DelabKey.ctorIdx E1 #

Mathlib.Notation3.DelabKey → ℕ

theorem Mathlib.Notation3.MatchState.mk.inj E1 #

∀ {vars : Std.HashMap Lean.Name (Lean.SubExpr × Lean.LocalContext × Lean.LocalInstances)}
  {scopeState : Option (Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized))}
  {foldState : Std.HashMap Lean.Name (Array Lean.Term)}
  {vars_1 : Std.HashMap Lean.Name (Lean.SubExpr × Lean.LocalContext × Lean.LocalInstances)}
  {scopeState_1 : Option (Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized))}
  {foldState_1 : Std.HashMap Lean.Name (Array Lean.Term)},
  { vars := vars, scopeState := scopeState, foldState := foldState } =
      { vars := vars_1, scopeState := scopeState_1, foldState := foldState_1 } →
    vars = vars_1 ∧ scopeState = scopeState_1 ∧ foldState = foldState_1

theorem Mathlib.Notation3.DelabKey.app.injEq E0 #

∀ (const : Option Lean.Name) (arity : ℕ) (const_1 : Option Lean.Name) (arity_1 : ℕ),
  (Mathlib.Notation3.DelabKey.app const arity = Mathlib.Notation3.DelabKey.app const_1 arity_1) =
    (const = const_1 ∧ arity = arity_1)

def Mathlib.Notation3.BoundValueType.ctorIdx E1 #

Mathlib.Notation3.BoundValueType → ℕ

theorem Mathlib.Notation3.DelabKey.app.inj E1 #

∀ {const : Option Lean.Name} {arity : ℕ} {const_1 : Option Lean.Name} {arity_1 : ℕ},
  Mathlib.Notation3.DelabKey.app const arity = Mathlib.Notation3.DelabKey.app const_1 arity_1 →
    const = const_1 ∧ arity = arity_1

abbrev Mathlib.Notation3.BoundValueType.ctorElimType E1 #

{motive : Mathlib.Notation3.BoundValueType → Sort u} → ℕ → Sort (max 1 u)

def Mathlib.Notation3.«termExpand_binders%(_=>_)_,_» E1 #

Lean.ParserDescr

Expands binders into nested combinators. For example, the familiar exists is given by: expand_binders% (p => Exists p) x y : Nat, x < y which expands to the same expression as ∃ x y : Nat, x < y

def Mathlib.Notation3.expandFoldl E1 #

Lean.ParserDescr

def Mathlib.Notation3.expandFoldr E1 #

Lean.ParserDescr

def Mathlib.Notation3.foldKind E1 #

Lean.ParserDescr

Keywording indicating whether to use a left- or right-fold.

def Mathlib.Notation3.bindersItem E1 #

Lean.ParserDescr

notation3 argument matching extBinders.

def Mathlib.Notation3.foldAction E1 #

Lean.ParserDescr

notation3 argument simulating a Lean 3 fold notation.

def Mathlib.Notation3.identOptScoped E1 #

Lean.ParserDescr

notation3 argument binding a name.

def Mathlib.Notation3.notation3Item E1 #

Lean.ParserDescr

notation3 argument.

structure Mathlib.Notation3.MatchState E1 #

Type

The dynamic state of a Matcher.

ctor Mathlib.Notation3.MatchState.mk E0 #

Std.HashMap Lean.Name (Lean.SubExpr × Lean.LocalContext × Lean.LocalInstances) →
  Option (Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized)) →
    Std.HashMap Lean.Name (Array Lean.Term) → Mathlib.Notation3.MatchState

abbrev Mathlib.Notation3.MatchState.vars E1 #

Mathlib.Notation3.MatchState → Std.HashMap Lean.Name (Lean.SubExpr × Lean.LocalContext × Lean.LocalInstances)

This stores the assignments of variables to subexpressions (and their contexts) that have been found so far during the course of the matching algorithm. We store the contexts since we need to delaborate expressions after we leave scoping constructs.

abbrev Mathlib.Notation3.MatchState.scopeState E1 #

Mathlib.Notation3.MatchState → Option (Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized))

The binders accumulated while matching a scoped expression.

abbrev Mathlib.Notation3.MatchState.foldState E1 #

Mathlib.Notation3.MatchState → Std.HashMap Lean.Name (Array Lean.Term)

The arrays of delaborated Terms accumulated while matching foldl and foldr expressions. For foldl, the arrays are stored in reverse order.

def Mathlib.Notation3.Matcher E1 #

Type

A matcher is a delaboration function that transforms MatchStates.

instance Mathlib.Notation3.instInhabitedMatcher E0 #

Inhabited Mathlib.Notation3.Matcher

def Mathlib.Notation3.MatchState.empty E1 #

Mathlib.Notation3.MatchState

The initial state.

def Mathlib.Notation3.MatchState.withVar E1 #

{α : Type} →
  Mathlib.Notation3.MatchState →
    Lean.Name → Lean.PrettyPrinter.Delaborator.DelabM α → Lean.PrettyPrinter.Delaborator.DelabM α

Evaluate f with the given variable's value as the SubExpr and within that subexpression's saved context. Fails if the variable has no value.

def Mathlib.Notation3.MatchState.delabVar E1 #

Mathlib.Notation3.MatchState →
  Lean.Name → optParam (Option Lean.Expr) none → Lean.PrettyPrinter.Delaborator.DelabM Lean.Term

Delaborate the given variable's value. Fails if the variable has no value. If checkNot is provided, then checks that the expression being delaborated is not the given one (this is used to prevent infinite loops).

def Mathlib.Notation3.MatchState.captureSubexpr E1 #

Mathlib.Notation3.MatchState → Lean.Name → Lean.PrettyPrinter.Delaborator.DelabM Mathlib.Notation3.MatchState

Assign a variable to the current SubExpr, capturing the local context.

def Mathlib.Notation3.MatchState.getFoldArray E1 #

Mathlib.Notation3.MatchState → Lean.Name → Array Lean.Term

Get the accumulated array of delaborated terms for a given foldr/foldl. Returns #[] if nothing has been pushed yet.

def Mathlib.Notation3.MatchState.getBinders E1 #

Mathlib.Notation3.MatchState → Array (Lean.TSyntax `Batteries.ExtendedBinder.extBinderParenthesized)

Get the accumulated array of delaborated terms for a given foldr/foldl. Returns #[] if nothing has been pushed yet.

def Mathlib.Notation3.MatchState.pushFold E1 #

Mathlib.Notation3.MatchState → Lean.Name → Lean.Term → Mathlib.Notation3.MatchState

Push a delaborated term onto a foldr/foldl array.

def Mathlib.Notation3.matchVar E1 #

Lean.Name → Mathlib.Notation3.Matcher

Matcher that assigns the current SubExpr into the match state; if a value already exists, then it checks for equality.

def Mathlib.Notation3.matchExpr E1 #

(Lean.Expr → Bool) → Mathlib.Notation3.Matcher

Matcher for an expression satisfying a given predicate.

def Mathlib.Notation3.matchFVar E1 #

Lean.Name → Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher

Matcher for Expr.fvar. It checks that the user name agrees and that the type of the expression is matched by matchTy.

def Mathlib.Notation3.matchTypeOf E1 #

Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher

Matcher that checks that the type of the expression is matched by matchTy.

def Mathlib.Notation3.natLitMatcher E1 #

ℕ → Mathlib.Notation3.Matcher

Matches raw Nat literals.

def Mathlib.Notation3.matchApp E1 #

Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher

Matches applications.

def Mathlib.Notation3.matchForall E1 #

Mathlib.Notation3.Matcher → (Lean.Expr → Mathlib.Notation3.Matcher) → Mathlib.Notation3.Matcher

Matches pi types. The name n should be unique, and matchBody should use n as the userName of its fvar.

def Mathlib.Notation3.matchLambda E1 #

Mathlib.Notation3.Matcher → (Lean.Expr → Mathlib.Notation3.Matcher) → Mathlib.Notation3.Matcher

Matches lambdas. The matchBody takes the fvar introduced when visiting the body.

def Mathlib.Notation3.setupLCtx E1 #

Lean.LocalContext → Array Lean.Name → Lean.MetaM (Lean.LocalContext × Std.HashMap Lean.FVarId Lean.Name)

Adds all the names in boundNames to the local context with types that are fresh metavariables. This is used for example when initializing p in (scoped p => ...) when elaborating ....

def Mathlib.Notation3.isType' E1 #

Lean.Expr → Bool

Like Expr.isType, but uses logic that normalizes the universe level. Mirrors the core Sort delaborator logic.

inductive Mathlib.Notation3.DelabKey E1 #

Type

Represents a key to use when registering the delab attribute for a delaborator. We use this to handle overapplication.

ctor Mathlib.Notation3.DelabKey.app E0 #

Option Lean.Name → ℕ → Mathlib.Notation3.DelabKey

The key app.const or app with a specific arity.

ctor Mathlib.Notation3.DelabKey.other E0 #

Lean.Name → Mathlib.Notation3.DelabKey

def Mathlib.Notation3.instReprDelabKey.repr E1 #

Mathlib.Notation3.DelabKey → ℕ → Std.Format

instance Mathlib.Notation3.instReprDelabKey E0 #

Repr Mathlib.Notation3.DelabKey

def Mathlib.Notation3.DelabKey.key E1 #

Mathlib.Notation3.DelabKey → Lean.Name

Turns the DelabKey into a key that the delab attribute accepts.

opaque Mathlib.Notation3.exprToMatcher E1 #

Std.HashMap Lean.FVarId Lean.Name →
  Std.HashMap Lean.FVarId Lean.Term →
    Lean.Expr → OptionT Lean.Elab.TermElabM (List Mathlib.Notation3.DelabKey × Lean.Term)

Given an expression, generate a matcher for it. The boundFVars hash map records which state variables certain fvars correspond to. The localFVars hash map records which local variable the matcher should use for an exact expression match.

If it succeeds generating a matcher, returns

  1. a list of keys that should be used for the delab attribute when defining the elaborator
  2. a Term that represents a Matcher for the given expression e.

def Mathlib.Notation3.mkExprMatcher E1 #

Lean.Term → Array Lean.Name → OptionT Lean.Elab.TermElabM (List Mathlib.Notation3.DelabKey × Lean.Term)

Returns a Term that represents a Matcher for the given pattern stx. The boundNames set determines which identifiers are variables in the pattern. Fails in the OptionT sense if it comes across something it's unable to handle.

Also returns constant names that could serve as a key for a delaborator. For example, if it's for a function f, then app.f.

def Mathlib.Notation3.matchScoped E1 #

Lean.Name → Lean.Name → Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher

Matcher for processing scoped syntax. Assumes the expression to be matched against is in the lit variable.

Runs smatcher, extracts the resulting scopeId variable, processes this value (which must be a lambda) to produce a binder, and loops.

def Mathlib.Notation3.mkScopedMatcher E1 #

Lean.Name →
  Lean.Name → Lean.Term → Array Lean.Name → OptionT Lean.Elab.TermElabM (List Mathlib.Notation3.DelabKey × Lean.Term)

Create a Term that represents a matcher for scoped notation. Fails in the OptionT sense if a matcher couldn't be constructed. Also returns a delaborator key like in mkExprMatcher. Reminder: $lit:ident : (scoped $scopedId:ident => $scopedTerm:Term)

opaque Mathlib.Notation3.matchFoldl E1 #

Lean.Name → Lean.Name → Lean.Name → Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher → Mathlib.Notation3.Matcher

Matcher for expressions produced by foldl.

def Mathlib.Notation3.mkFoldlMatcher E1 #

Lean.Name →
  Lean.Name →
    Lean.Name →
      Lean.Term →
        Lean.Term → Array Lean.Name → OptionT Lean.Elab.TermElabM (List Mathlib.Notation3.DelabKey × Lean.Term)

Create a Term that represents a matcher for foldl notation. Reminder: ( lit ","* => foldl (x y => scopedTerm) init)

def Mathlib.Notation3.mkFoldrMatcher E1 #

Lean.Name →
  Lean.Name →
    Lean.Name →
      Lean.Term →
        Lean.Term → Array Lean.Name → OptionT Lean.Elab.TermElabM (List Mathlib.Notation3.DelabKey × Lean.Term)

Create a Term that represents a matcher for foldr notation. Reminder: ( lit ","* => foldr (x y => scopedTerm) init)

inductive Mathlib.Notation3.BoundValueType E1 #

Type

Used when processing different kinds of variables when building the final delaborator.

ctor Mathlib.Notation3.BoundValueType.normal E0 #

Mathlib.Notation3.BoundValueType

A normal variable, delaborate its expression.

ctor Mathlib.Notation3.BoundValueType.foldl E0 #

Mathlib.Notation3.BoundValueType

A fold variable, use the fold state (but reverse the array).

ctor Mathlib.Notation3.BoundValueType.foldr E0 #

Mathlib.Notation3.BoundValueType

A fold variable, use the fold state (do not reverse the array).

def Mathlib.Notation3.prettyPrintOpt E1 #

Lean.ParserDescr

def Mathlib.Notation3.getPrettyPrintOpt E1 #

Option (Lean.TSyntax `Mathlib.Notation3.prettyPrintOpt) → Bool

Interpret a prettyPrintOpt. The default value is true.

def Mathlib.Notation3.withHeadRefIfTagAppFns E1 #

Lean.PrettyPrinter.Delaborator.Delab → Lean.PrettyPrinter.Delaborator.Delab

If pp.tagAppFns is true and the head of the current expression is a constant, then delaborates the head and uses it for the ref. This causes tokens inside the syntax to refer to this constant. A consequence is that docgen will linkify the tokens.

def Mathlib.Notation3.notation3 E1 #

Lean.ParserDescr

notation3 declares notation using Lean-3-style syntax.

Examples:

notation3 "∀ᶠ " (...) " in " f ", " r:(scoped p => Filter.eventually p f) => r
notation3 "MyList[" (x", "* => foldr (a b => MyList.cons a b) MyList.nil) "]" => x

By default notation is unable to mention any variables defined using variable, but local notation3 is able to use such local variables.

Use notation3 (prettyPrint := false) to keep the command from generating a pretty printer for the notation.

This command can be used in mathlib4 but it has an uncertain future and was created primarily for backward compatibility.