GNU Prolog“delete/3"谓词中的异常? [英] Anomaly in GNU Prolog `delete/3` predicate?

查看:51
本文介绍了GNU Prolog“delete/3"谓词中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 GNU Prolog(版本 1.4.2)的行为不一致 delete/3 谓词:

<代码>|?- 删除([a,b,c], b, R).R = [a,c]是的|?- 删除([(a,_), (b,_), (c,_)], (b,_), R).R = [(a,_),(b,_),(c,_)]是的|?- 成员( (b,_), [(a,_), (b,_), (c,_)] ).真的 ?;不|?- 选择((b,_), [(a,_), (b,_), (c,_)], R).R = [(a,_),(c,_)] ?;不|?-

上述所有结果都是我预期的,除了delete([(a,_), (b,_), (c,_)], (b,_), R)..如果您在 SWI Prolog 中运行相同的查询集,例如,delete([(a,_), (b,_), (c,_)], (b,_), R). 产生,R = [(a,_), (c,_)] 正如我所期望的.

我的问题是这是否是基于 delete/3 谓词的某些特定解释"所预期的,或者它可能是 GNU Prolog 中的一个错误?

解决方案

delete 这个名称非常缺乏描述性.这是否意味着删除所有相等元素或所有匹配元素?如果是后者,我们正在删除的元素与列表元素之间的统一是否在移动到下一个列表元素时进行或被撤消?

从您上面的跟踪,特别是第二个查询,似乎 delete/3 使用相等而不是统一(如您所知,匿名变量的每次出现都是不同的变量).文档证实了这一点:

http://www.gprolog.org/manual/gprolog.html#sec213>

确实没有错误,因为对于 delete/3 谓词的唯一语义没有达成共识.至多,有一个共识是库谓词的坏名字.只是一个在任何 Prolog 库中都应该避免的谓词名称.也就是说,与旧代码的兼容性通常会导致它的存在.

I found an inconsistency in behavior in the GNU Prolog (version 1.4.2) delete/3 predicate:

| ?- delete([a,b,c], b, R).

R = [a,c]

yes
| ?- delete([(a,_), (b,_), (c,_)], (b,_), R).

R = [(a,_),(b,_),(c,_)]

yes
| ?- member( (b,_), [(a,_), (b,_), (c,_)] ).

true ? ;

no
| ?- select((b,_), [(a,_), (b,_), (c,_)], R).

R = [(a,_),(c,_)] ? ;

no
| ?-

All of the above results I expected, except for that of, delete([(a,_), (b,_), (c,_)], (b,_), R).. If you run the same set of queries in SWI Prolog, for example, the delete([(a,_), (b,_), (c,_)], (b,_), R). yields, R = [(a,_), (c,_)] as I would expect.

My question is whether this is expected based upon some specific "interpretation" of the delete/3 predicate, or is it perhaps a bug in GNU Prolog?

解决方案

The name delete is terribly non-descriptive. Does it mean deleting all equal elements or all matching elements? If the later, does the unification between the element we're deleting and an element of the list carries when moving to the next list element or is undone?

From your trace above, in particular the second query, it seems that delete/3 is using equality instead of unification (as, as you know, each occurrence of an anonymous variable is a different variable). The documentation confirms it:

http://www.gprolog.org/manual/gprolog.html#sec213

There's really no bug as there's no consensus on a unique semantics for a delete/3predicate. At most, there's a consensus that's a bad name for a library predicate. Just a predicate name that should be avoided in any Prolog library. That said, compatibility with old code often results in its presence.

这篇关于GNU Prolog“delete/3"谓词中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆