如何删除列表中具有特定属性的所有元素?

时间:2013-03-08 11:40:40

标签: isabelle

我希望匹配并删除l :: 'a list中与谓词P :: ('a => bool)匹配的元素

完成此类任务的最佳方法是什么?我怎样才能找到可能对我有帮助的现有功能?

2 个答案:

答案 0 :(得分:1)

短篇小说:使用find_consts

长篇故事:

这是解决此类问题的方法。

Main中,有List.dropWhile

List.dropWhile :: "('a => bool) => 'a list => 'a list"

但是,它只从一开始就删除了。这可能不是预期的功能。

value "List.dropWhile (λ x. x = ''c'') [''c'', ''c'', ''d'']"
"[''d'']"

value "List.dropWhile (λ x. x = ''c'') [''d'', ''c'', ''c'']"
"[''d'', ''c'', ''c'']"

手动方法

我们可以自己编写一个删除所有事件的函数

fun dropAll :: "('a => bool) => 'a list => 'a list" where
    "dropAll P [] = []"
  | "dropAll P (x # xs) = (if P x then dropAll P xs else x # (dropAll P xs))"

搜索图书馆

但是,此功能相当于使用¬ P

进行过滤

我们如何找到这样的库函数?

如果我们知道我们想要做的签名,我们可以使用find_consts

find_consts "('a ⇒ bool) ⇒ 'a list ⇒ 'a list"

它返回Main的3个函数,并带有该签名:List.dropWhileList.filterList.takeWhile

现在,让我们说明我们不需要dropAll,但可以对filter执行相同操作。

lemma "dropAll P l = filter (λ x. ¬ P x) l"
  apply(induction l)
  by simp_all

建议不要自己实现dropAll之类的内容,而应使用过滤器。因此,filter证明的所有引理都是可用的。

<强>提示

提示:我们可以使用方便的列表理解语法来编写例如过滤表达式

lemma "filter (λ x. ¬ P x) l = [x ← l. ¬ P x]" by simp 

答案 1 :(得分:1)

找到您希望存在的函数的一种方法是Isabelle文档中的文档What's in Main。它简要概述了Isabelle / HOL理论Main提供的主要类型,功能和语法。

如果您查看该文档中的List部分,您会发现函数filter似乎具有正确的类型。