【发布时间】:2016-04-21 15:33:27
【问题描述】:
我正在尝试编写一个函数,该函数接受自然数列表并返回其中不同元素的数量作为输出。例如,如果我有列表 [1,2,2,4,1],我的函数 DifElem 应该输出“3”。我已经尝试了很多东西,我得到的最接近的是:
Fixpoint DifElem (l : list nat) : nat :=
match l with
| [] => 0
| m::tm =>
let n := listWidth tm in
if (~ In m tm) then S n else n
end.
我的逻辑是这样的:如果 m 不在列表的尾部,则向计数器加一。如果是,不要添加到计数器中,所以我只会数一次:当它最后一次出现时。我得到错误:
Error: The term "~ In m tm" has type "Prop"
which is not a (co-)inductive type.
In 是 Coq 的 list 标准库的一部分 Coq.Lists.List. 它在那里定义为:
Fixpoint In (a:A) (l:list A) : Prop :=
match l with
| [] => False
| b :: m => b = a \/ In a m
end.
我认为我不太了解如何在定义中使用 If then 语句,Coq 的文档并没有提供足够的帮助。
我还尝试使用同一个库中的nodup 进行此定义:
Definition Width (A : list nat ) := length (nodup ( A ) ).
在这种情况下,我得到的错误是:
The term "A" has type "list nat" while it is expected to have
type "forall x y : ?A0, {x = y} + {x <> y}".
而且我对这里发生的事情感到很困惑。感谢您为解决此问题提供的帮助。
【问题讨论】:
标签: coq