以下Agda函数应查找列表,循环回索引溢出:
module Test where
open import Prelude.Nat
open import Prelude.List
roundIndex : {A : Set} -> Nat -> A -> List A -> A
roundIndex n x xs = go n xs where
go : {A : Set} -> Nat -> List A -> A
go (suc n) (x ∷ xs) = go n xs
go (suc n) [] = go n xs
go zero (x ∷ xs) = x
go zero [] = x
但不会编译以下隐藏错误:
/Users/v/vic/dev/agda/Test.agda:10,25-32
.A !=< .A₁ of type Set
when checking that the expression go n xs has type .A₁
似乎Agda没有正确地将隐式A
传递给go
,但明确地传递它并没有解决问题。这个错误是什么?另外,那是!=<
?
答案 0 :(得分:2)
错误是go
适用于任何A
,但您尝试使用外部x
类型返回外部xs
和A
。 go
- 绑定A
和roundIndex
- 绑定A
类型不同,因此错误。通常,绑定类型变量在转换检查期间仅等于它们自身。它不是特定于Agda的,因为你也可以在Haskell中重现相同的错误。
解决方案是删除内部类型量化:
roundIndex : {A : Set} -> Nat -> A -> List A -> A
roundIndex {A} n x xs = go n xs where
go : Nat -> List A -> A
go (suc n) (x ∷ xs) = go n xs
go (suc n) [] = go n xs
go zero (x ∷ xs) = x
go zero [] = x
现在只有一个共享A
。