Joh*_*iao 5 theorem-proving idris
我正在阅读伊德里斯教程。而且我无法理解下面的代码。
disjoint : (n : Nat) -> Z = S n -> Void
disjoint n p = replace {P = disjointTy} p ()
where
disjointTy : Nat -> Type
disjointTy Z = ()
disjointTy (S k) = Void
Run Code Online (Sandbox Code Playgroud)
到目前为止,我发现......是
Void
空类型,用于证明某些事情是不可能的。
替换:(x = y) -> P x -> P y 替换使用等式证明来转换谓词。
我的问题是:
哪一个是平等证明?(Z = S n)?
哪一个是谓词?功能disjointTy
?
目的是什么disjointTy
?disjointTy Z = () 是否意味着 Z 位于一个类型“land”() 中且 (S k) 位于另一块土地中Void
?
Void 输出以什么方式表示矛盾?
诗。我所知道的证明是“凡事不匹配则为假”。或者“找到一件矛盾的事情”......
哪一个是平等证明?(Z = S n)?
这里的参数p
就是等式证明。p
有类型Z = S n
.
哪一个是谓词?功能
disjointTy
?
是的你是对的。
目的是什么
disjointTy
?
让我重复一下这里的定义disjointTy
:
disjointTy : Nat -> Type
disjointTy Z = ()
disjointTy (S k) = Void
Run Code Online (Sandbox Code Playgroud)
的目的disjointTy
是谓词replace
函数需要。这种考虑决定了 的类型disjointTy
,即。[domain] -> Type
。由于自然数之间相等,因此[domain]
是Nat
。
为了了解身体是如何构造的,我们需要再看replace
一次:
replace : (x = y) -> P x -> P y
Run Code Online (Sandbox Code Playgroud)
回想一下,我们有p
of Z = S n
,所以x
从上面的类型可以看出 isZ
和y
is S n
。要调用,replace
我们需要构造一个类型为 的术语P x
,即P Z
在我们的例子中。这意味着类型P Z
返回必须易于构造,例如单元类型是理想的候选者。我们有 的disjointTy Z = ()
定义的合理子句disjointTy
。当然,这不是唯一的选择,我们可以使用任何其他有人居住(非空)类型,例如Bool
orNat
等。
第二个子句中的返回值disjointTy
现在很明显了——我们想要replace
返回一个Void
类型的值,所以P (S n)
必须是Void
。
接下来,我们disjointTy
像这样使用:
replace {P = disjointTy} p ()
^ ^ ^
| | |
| | the value of `()` type
| |
| proof term of Z = S n
|
we are saying "this is the predicate"
Run Code Online (Sandbox Code Playgroud)
作为奖励,这里有一个替代证明:
disjoint : (n : Nat) -> Z = S n -> Void
disjoint n p = replace {P = disjointTy} p False
where
disjointTy : Nat -> Type
disjointTy Z = Bool
disjointTy (S k) = Void
Run Code Online (Sandbox Code Playgroud)
我用过False
,但本来可以用True
——没关系。重要的是我们构建类型术语的能力disjointTy Z
。
Void 输出以什么方式表示矛盾?
Void
定义如下:
data Void : Type where
Run Code Online (Sandbox Code Playgroud)
它没有构造函数!无论如何,都无法创建这种类型的术语(在某些条件下:比如 Idris 的实现是正确的,并且 Idris 的底层逻辑是健全的,等等)。因此,如果某个函数声称它可以返回一个类型项,Void
那么一定有什么可疑之处。我们的函数说:如果你给我一个 的证明Z = S n
,我将返回一个空类型的项。这种方式Z = S n
一开始就不能被构造,因为它会导致矛盾。