哪一个是相等证明?(Z = S n)?
这里的相等证明是参数p
。类型为Z = S n
。
哪个是谓词?disjointTy
函数?
是的,你说得对。
disjointTy
的目的是什么?
让我重复一下disjointTy
的定义:
disjointTy : Nat -> Type
disjointTy Z = ()
disjointTy (S k) = Void
disjointTy
的目的是作为
replace
函数所需的谓词。这一点决定了
disjointTy
的类型,即
[domain] -> Type
。由于自然数之间具有相等性,
[domain]
是
Nat
。
要理解构造方法,我们需要再次查看
replace
:
replace : (x = y) -> P x -> P y
请注意,我们已经有
p
个
Z = S n
,因此上述类型中的
x
是
Z
,
y
是
S n
。调用
replace
需要构造一个类型为
P x
的项,即在我们的情况下
P Z
。这意味着
P Z
的返回类型必须容易构造,例如,单位类型是理想的候选对象。我们已经证明了
disjointTy Z = ()
定义中的条款。当然,这不是唯一的选择,我们可以使用任何其他有人居住(非空)类型,例如
Bool
或
Nat
等。
现在,我们很容易看出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"
作为加分项,这里有一种替代的证明方法:
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
我已经使用了
False
,但也可以使用
True
——这并不重要。重要的是我们能够构建一个类型为
disjointTy Z
的术语。
无类型输出如何表示矛盾?
Void
的定义如下:
data Void : Type where
它没有构造函数!根据某些条件(例如Idris的实现正确且Idris的基本逻辑是合理的等),无论如何都无法创建此类型的术语。因此,如果某个函数声称它可以返回
Void
类型的术语,则必须有一些可疑的事情正在发生。我们的函数说:如果您给我一个
Z = S n
的证明,我将返回一个
空类型的术语。这意味着首先无法构建
Z = S n
,因为它会导致矛盾。请注意保留HTML标记,但不要写解释。