Agda stdlib Vec fromList 具有任意长度的列表

Agda stdlib Vec fromList of list with arbitrary length

我正在尝试在任意长度的 Data.List 上使用 Data.Vec fromList

natListToVec : {n : ℕ} → List ℕ → Vec ℕ n
natListToVec nats = v.fromList nats

但出现错误:

l.foldr (λ _ → suc) 0 nats != n of type ℕ
when checking that the expression fromList nats has type Vec ℕ n

当我尝试使用已知长度的 List 时,我没有遇到任何问题:

ListWith2ToVec : v.fromList (2 l.∷ l.[]) ≡ 2 v.∷ v.[]
ListWith2ToVec = refl

我猜问题出在这种类型签名上:

{n : ℕ} → List ℕ → Vec ℕ n

我没有指定 n 必须是 List ℕ 的长度。

我该怎么做?

谢谢!

您可以简单地在类型签名中使用 length 函数。您还必须命名类型列表的参数以引用它,如下所示:

natListToVec : (xs : List ℕ) → Vec ℕ (length xs)
[...]