在 ACL2 中编写 select() 函数

Writing a select() function in ACL2

我正在尝试在 ACL2(特别是 ACL2s)中编写一个函数,它接受一个列表和一个自然数,并且 returns 是列表中给定索引处的项目。所以 (select (list 1 2 3) 2) 会 return 3.

这是我的代码:

;; select: List x Nat -> All
(defunc select (l n)
  :input-contract (and (listp l) (natp n))
  :output-contract t
  (if (equal 0 n)
    (first l)
    (select (rest l) (- n 1))))

我收到以下错误:

Query: Testing body contracts ... 

**Summary of Cgen/testing**
We tested 50 examples across 1 subgoals, of which 48 (48 unique) satisfied
the hypotheses, and found 1 counterexamples and 47 witnesses.

We falsified the conjecture. Here are counterexamples:
 [found in : "top"]
 -- ((L NIL) (N 0))

Test? found a counterexample.
Body contract falsified in: 
 -- (ACL2::EXTRA-INFO '(:GUARD (:BODY SELECT)) '(FIRST L))

非常感谢任何帮助!

消息对我来说似乎很清楚:您正在尝试获取空列表的第一个元素,这与您的规范冲突。

基于 this reference,似乎 first 需要一个 non-empty 列表,而 car returns nil 当您的输入是nil.

您可以使用 endp 测试明确处理 nil 案例,或者使用 car 而不是 first