Agda stdlib Vec在对列表执行fromList之后最后调用

时间:2020-11-11 00:40:29

标签: list vector agda

我具有以下功能:

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

1 个答案:

答案 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}

相关问题