Anomaly in GNU Prolog `delete/3` predicate?

こ雲淡風輕ζ 提交于 2019-12-23 12:25:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29289228/anomaly-in-gnu-prolog-delete-3-predicate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!