关于:synthInstanceFailed
Type 类 是 Lean 和许多其他 编程语言用于处理重载操作。处理特定的代码 重载操作是一个类型类的instance;决定对于给定的情况使用哪个实例 重载操作称为合成实例。
例如,当 Lean 遇到表达式 x + y 时,其中 x 和 y 都
有类型 Int,有必要查找它应该如何添加两个整数并查找
结果类型是什么。这被描述为合成类型类的实例
HAdd Int Int t 对于某些类型 t。
许多合成类型类实例失败的原因是使用了错误的二进制文件 操作。成功和失败并不总是那么简单,因为有些情况是 根据其他实例定义,并且 Lean 必须递归搜索以找到适当的实例。 可以 检查 Lean 的实例合成,并且这个 有助于诊断类型类实例合成的棘手故障。
示例
Using the Wrong Binary Operation
Arguments Have the Wrong Type
Missing Type Class Instance
inductive MyColor where
| chartreuse | sienna | thistle
def forceColor (oc : Option MyColor) :=
oc.get!
inductive MyColor where
| chartreuse | sienna | thistle
deriving Inhabited
def forceColor (oc : Option MyColor) :=
oc.get!
Type 类综合可能会失败,因为只需要提供类型类的实例。
对于 Repr、BEq、ToJson 等类型类,通常会发生这种情况
Inhabited。 Lean 通常可以 自动生成
带有 deriving 关键字的类型类 在定义类型时或使用独立类型时
Lean.Parser.Command.deriving : commandderiving 命令。