名称 Nat.bdiv 未定义,但 Int.bdiv 存在。
查找字段 bdiv 时,不会考虑从 Nat 到 Int 的强制转换:
example (n : Nat) := n.Invalid field `bdiv`: The environment does not contain `Nat.bdiv`, so it is not possible to project the field `bdiv` from an expression
n
of type `Nat`bdiv 2
Invalid field `bdiv`: The environment does not contain `Nat.bdiv`, so it is not possible to project the field `bdiv` from an expression
n
of type `Nat`
这是因为只有当期望类型与推断类型不同时才会插入强制转换,而广义字段是根据点号前项的推断类型解析的。
添加类型标注可以触发强制转换;此外,它还会使整个标注项的推断类型成为 Int,从而找到函数 Int.bdiv。
example (n : Nat) := (n : Int).bdiv 2