CoeTailαβ is for coercions that can only appear at the end of a
sequence of coercions. That is, α can be further coerced via Coeσα and
CoeHeadτσ instances but β will only be the expected type of the expression.
CoeT is the core typeclass which is invoked by Lean to resolve a type error.
It can also be triggered explicitly with the notation ↑x or by double type
ascription ((x:α):β).
A CoeT chain has the grammar CoeHead?CoeOut*Coe*CoeTail?|CoeDep.
The resulting value of type β. The input x:α is a parameter to
the type class, so the value of type β may possibly depend on additional
typeclasses on x.
CoeDepα(x:α)β is a typeclass for dependent coercions, that is, the type β
can depend on x (or rather, the value of x is available to typeclass search
so an instance that relates β to x is allowed).
Dependent coercions do not participate in the transitive chaining process of
regular coercions: they must exactly match the type mismatch on both sides.
The resulting value of type β. The input x:α is a parameter to
the type class, so the value of type β may possibly depend on additional
typeclasses on x.
term::= ...
|`↑x` represents a coercion, which converts `x` of type `α` to type `β`, using
typeclasses to resolve a suitable conversion function. You can often leave the
`↑` off entirely, since coercion is triggered implicitly whenever there is a
type error, but in ambiguous cases it can be useful to use `↑` to disambiguate
between e.g. `↑x + ↑y` and `↑(x + y)`.
↑term
可以使用前缀运算符 coeNotation : term`↑x` represents a coercion, which converts `x` of type `α` to type `β`, using
typeclasses to resolve a suitable conversion function. You can often leave the
`↑` off entirely, since coercion is triggered implicitly whenever there is a
type error, but in ambiguous cases it can be useful to use `↑` to disambiguate
between e.g. `↑x + ↑y` and `↑(x + y)`.
↑ 显式放置强制转换。
与使用嵌套 类型归属 不同,用于放置强制转换的 coeNotation : term`↑x` represents a coercion, which converts `x` of type `α` to type `β`, using
typeclasses to resolve a suitable conversion function. You can often leave the
`↑` off entirely, since coercion is triggered implicitly whenever there is a
type error, but in ambiguous cases it can be useful to use `↑` to disambiguate
between e.g. `↑x + ↑y` and `↑(x + y)`.
↑ 语法不需要显式编写所涉及的类型。
适当的 CoeHead、CoeOut、Coe 或 CoeTail 实例足以导致插入所需的强制。
但是,强制的实现应使用 coe 属性注册为强制。
这会导致 Lean 显示 coeNotation : term`↑x` represents a coercion, which converts `x` of type `α` to type `β`, using
typeclasses to resolve a suitable conversion function. You can often leave the
`↑` off entirely, since coercion is triggered implicitly whenever there is a
type error, but in ambiguous cases it can be useful to use `↑` to disambiguate
between e.g. `↑x + ↑y` and `↑(x + y)`.
↑ 运算符的强制转换的使用情况。
它还导致 norm_cast策略将强制转换视为强制转换,而不是普通函数。
attributeCoercion Declarations
attr::= ...
|The `@[coe]` attribute on a function (which should also appear in a
`instance : Coe A B := ⟨myFn⟩` declaration) allows the delaborator to show
applications of this function as `↑` when printing expressions.
coe
The @[coe] attribute on a function (which should also appear in a
instance:CoeAB:=⟨myFn⟩ declaration) allows the delaborator to show
applications of this function as ↑ when printing expressions.
The canonical homomorphism Nat→R. In most use cases, the target type will have a (semi)ring
structure, and this homomorphism should be a (semi)ring homomorphism.
NatCast and IntCast exist to allow different libraries with their own types that can be notated
as natural numbers to have consistent simp normal forms without needing to create coercion
simplification sets that are aware of all combinations. Libraries should make it easy to work with
NatCast where possible. For instance, in Mathlib there will be such a homomorphism (and thus a
NatCastR instance) whenever R is an additive monoid with a 1.
The canonical homomorphism Nat→R. In most use cases, the target type will have a (semi)ring
structure, and this homomorphism should be a (semi)ring homomorphism.
NatCast and IntCast exist to allow different libraries with their own types that can be notated
as natural numbers to have consistent simp normal forms without needing to create coercion
simplification sets that are aware of all combinations. Libraries should make it easy to work with
NatCast where possible. For instance, in Mathlib there will be such a homomorphism (and thus a
NatCastR instance) whenever R is an additive monoid with a 1.
The canonical homomorphism Int→R. In most use cases, the target type will have a ring structure,
and this homomorphism should be a ring homomorphism.
IntCast and NatCast exist to allow different libraries with their own types that can be notated
as natural numbers to have consistent simp normal forms without needing to create coercion
simplification sets that are aware of all combinations. Libraries should make it easy to work with
IntCast where possible. For instance, in Mathlib there will be such a homomorphism (and thus an
IntCastR instance) whenever R is an additive group with a 1.
The canonical homomorphism Int→R. In most use cases, the target type will have a ring structure,
and this homomorphism should be a ring homomorphism.
IntCast and NatCast exist to allow different libraries with their own types that can be notated
as natural numbers to have consistent simp normal forms without needing to create coercion
simplification sets that are aware of all combinations. Libraries should make it easy to work with
IntCast where possible. For instance, in Mathlib there will be such a homomorphism (and thus an
IntCastR instance) whenever R is an additive group with a 1.