我具有以下功能:
natListLast : List ℕ → ℕ
natListLast nats = v.last (v.fromList nats)
我目前收到此错误:
l.foldr (λ _ → suc) 0 nats != suc _n_126 of type ℕ
when checking that the expression fromList nats has type
Vec ℕ (1 + _n_126)
我想知道当last
返回v.fromList nats
时如何在v.fromList
上调用Vec A (length xs)
。如何告诉编译器length xs
等于1 + _n_126
?
谢谢!
我也尝试这样做:
natListLast : List ℕ → ℕ
natListLast nats = v.last {l.length nats} (v.fromList nats)
由于last
具有以下签名:
last : ∀ {n} → Vec A (1 + n) → A
我认为我可以将nat的长度作为last
的隐式参数传入,但出现此错误:
ℕ !=< Level
when checking that the inferred type of an application
ℕ
matches the expected type
Level
答案 0 :(得分:2)
如何告诉编译器长度xs等于1 + _n_126?
仔细考虑这个问题。 您如何分辨length xs
等于1 + _n_126
?如果xs = []
进而length xs = 0
又该怎么办?答案是你做不到,所以不要指望阿格达也这样做!
要解决此问题,您需要在xs
上进行模式匹配以确保它不是空的:
natListLast : List ℕ → ℕ
natListLast nats@(x ∷ xs) = v.last (v.fromList nats)
natListLast [] = {!!}
第二种情况下,我将由您自己决定返回什么。
对于第二个问题,您需要检查last
的完整类型(在Emacs中使用C-c C-d
):
{A.a : Agda.Primitive.Level} {A : Set A.a} {n : ℕ} →
v.Vec A (suc n) → A
如您所见,在长度n
之前有两个隐藏的参数(它们已由automatic variable generalization插入)。要直接传递自变量n
而不传递前两个自变量,可以命名自变量,例如last {n = l.length nats}
。