3
下面是本教程中,稍加修改簡單的例子:「有」在idris教程,第11頁,第3.4.4節中如何工作?
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
我不明白怎麼There
驗證值是正確的。 據我所知,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
可以給我這樣的人解釋一下上面的代碼是如何工作的限制There
到Vect
尾(神奇?) ?
'There'的構造函數不會忽略向量的頭部,因爲您必須提供元素在尾部內的證明,即'Elem x xs'參數。你的錯誤信息是你不能構建一個'This 2 [4,5,6]'。 – Lee