“怎么做?”;有";在idris教程第11页第3.4.4节中工作?

“怎么做?”;有";在idris教程第11页第3.4.4节中工作?,idris,Idris,以下是教程中的示例,为简单起见稍作修改: data Vect : Nat -> (b:Type) -> Type where Nil : Vect Z a (::) : a -> Vect k a -> Vect (S k) a data Elem : a -> Vect n a -> Type where Here : {x:a} -> {xs:Vect n a} -> Elem x (x :: xs) There : {x,y

以下是教程中的示例,为简单起见稍作修改:

data Vect : Nat -> (b:Type) -> Type where
  Nil : Vect Z a
  (::) : a -> Vect k a -> Vect (S k) a

data Elem : a -> Vect n a -> Type where
  Here : {x:a} -> {xs:Vect n a} -> Elem x (x :: xs)
  There : {x,y:a} -> {xs:Vect n a} -> Elem x xs -> Elem x (y :: xs)

testVec : Vect 4 Int
testVec = 3 :: 4 :: 5 :: 6 :: Nil
inVect : Elem 4 testVec
inVect = There Here
我无法理解
那里的
如何验证值是否正确。
据我所知,
那里的
就像一个函数,所以它需要 此处
类型的元素
,当填充孔时,对应于
Elem 3 testVec
,然后将
testVec
的第一个值设置为
y
,其余值设置为
xs
。但是,由于,
y
没有在任何地方使用,因此我希望除了此之外不会造成任何限制

然而,当我尝试

inVectBroken : Elem 2 testVec
inVectBroken = There Here
我得到一个错误:

When checking an application of constructor There:
Type mismatch between
        Elem x (x :: xs) (Type of Here)
and
        Elem 2 [4, 5, 6] (Expected type)

Specifically:
        Type mismatch between
                2
        and
                4
有人能给我解释一下上面的代码是如何(神奇地)将
限制在
Vect
的尾部的吗?

这里4(x::xs)
证明了4是
(x::xs)
,所以
x=4
那里
需要证明
elem4xs
中的某个地方是4,因此证明
elem4(y::xs)
,即4仍然在扩展列表中的某个地方。这就是
y
的位置。不管实际是什么,它只允许将证明扩展到更大的列表。例如:

in1 : Elem 4 (4 :: Nil)
in1 = Here

in2 : Elem 4 (3 :: 4 :: Nil)
in2 = There in1

in3 : Elem 4 (8 :: 4 :: Nil)
in3 = There in1

根据类型,您看到的不是元素4在整个证明过程中发生了变化,而是列表发生了变化。

那里的
构造函数确实忽略了向量的头部,因为您必须提供元素位于尾部的证明,即
Elem x xs
参数。您的错误消息是您无法在此处构造
2[4,5,6]