【问题标题】:Writing a select() function in ACL2在 ACL2 中编写 select() 函数
【发布时间】:2016-01-25 18:25:04
【问题描述】:

我正在尝试在 ACL2(特别是 ACL2s)中编写一个函数,该函数接受一个列表和一个自然数,并返回列表中给定索引处的项目。所以 (select (list 1 2 3) 2) 将返回 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))

非常感谢任何帮助!

【问题讨论】:

    标签: functional-programming lisp acl2


    【解决方案1】:

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

    基于this reference,似乎first 需要一个非空列表,而car 在您的输入为nil 时返回nil

    要么使用 endp 测试明确地处理 nil 案例,要么使用 car 而不是 first

    【讨论】:

    • 使用endp 成功了!我曾假设first 会接受一个非空列表,但我错了。谢谢!
    猜你喜欢
    • 2023-03-16
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多