问题标签 [prolog-dif]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
recursion - 在 Prolog 中查找每个 X 列表的程序
我开始学习Prolog。该程序尝试获取给定元素的所有出现:
但这是错误:
prolog - Reification of term equality/inequality
Pure Prolog programs that distinguish between the equality and inequality of terms in a clean manner suffer from execution inefficiencies ; even when all terms of relevance are ground.
A recent example on SO is this answer. All answers and all failures are correct in this definition. Consider:
While the program is flawless from a declarative viewpoint, its direct execution on current systems like B, SICStus, SWI, YAP is unnecessarily inefficient. For the following goal, a choicepoint is left open for each element of the list.
This can be observed by using a sufficiently large list of a
s as follows. You might need to adapt the I
such that the list can still be represented ; in SWI this would mean that
1mo the I
must be small enough to prevent a resource error for the global stack like the following:
2do the I
must be large enough to provoke a resource error for the local stack:
To overcome this problem and still retain the nice declarative properties some comparison predicate is needed.
How should this comparison predicate be defined?
Here is such a possible definition:
Edit: Maybe the argument order should be reversed similar to the ISO built-in compare/3
(link links to draft only).
An efficient implementation of it would handle the fast determinate cases first:
Edit: it is not clear to me whether or not X \= Y
is a suitable guard in the presence of constraints. Without constraints, ?=(X, Y)
or X \= Y
are the same.
Example
As suggested by @user1638891, here is an example how one might use such a primitive. The original code by mats was:
Which can be rewritten to something like:
Please note that SWI's second-argument indexing is only activated, after you enter a query like occurrences(_,[],_)
. Also, SWI need the inherently nonmonotonic if-then-else, since it does not index on (;)/2
– disjunction. SICStus does so, but has only first argument indexing. So it leaves one (1) choice-point open (at the end with []
).
prolog - 使用 \==/2 或 diff/2
如果我想确保两个变量不会实例化为同一个术语,那么首选的方法是什么?
假设我需要在图中找到有向边,并且节点不能对自身有边:
(这里的边是 a -> c,b -> a,但不是c -> c)
以下作品:
这也有效[swi-prolog]:
这显然不起作用(因为 A 和 B 都没有被实例化?):
我想我对第一个解决方案的问题是,对于更复杂的谓词,在失败node
之前可能会发生许多不必要的统一。另一方面,在一个库中,这表明它不适合在如此简单的情况下使用(尽管它具有我似乎正在寻找的确切功能)edge
。dif
prolog - 序言中的保护条款?
它们存在吗?它们是如何实施的?
SWI-Prolog的协程谓词(freeze
,when
等dif
)具有警卫的功能。它们如何适应首选的 Prolog 编程风格?
我对逻辑编程非常陌生(使用 Prolog 和全部),并且对它不是纯粹的声明性这一事实感到有些困惑,即使在非常简单的情况下也需要程序考虑(请参阅有关使用\==
ordif
的这个问题)。我错过了什么重要的东西吗?
prolog - Prolog 中的递归如何从内部工作。一个例子
我这里有一个小脚本,它将元素列表转换为集合。例如列表 [1,1,2,3] -> 设置 [1,2,3]。有人可以逐步向我解释这些程序中发生了什么吗?你能用我的 [1,1,2,3] -> [1,2,3] 例子吗?
prolog - 强制 Prolog 选择变量的唯一值
好的,我是 Prolog 的新手,所以如果这是一件微不足道的事情,请原谅,但我似乎找不到合适的优雅答案。我正在尝试在learnprolognow.org上进行练习,练习 2.4(填字游戏)。
该练习提供了以下事实:
我想出的解决每个单词的填字游戏位置的解决方案是这样的:
V1a
其中toV1g
等是每个单词的字符,而toV1bH1b
是V3fH3f
填字游戏中单词之间的共同字符。
该解决方案似乎有效,但结果是产生重复值,第一个结果是:
我怎样才能强制 Prolog 拥有V1 \= V2 \= V3 \= H1 \= H2 \= H3
?如果我一个一个地做,我将需要 120 个排列,所以必须有一个更快的方法,这是一个初学者练习,所以我一定会遗漏一些东西。
我发现了这个类似的问题,但是提供的答案似乎很复杂,我希望有一个更简单的方法。我在 Ubuntu 上使用 swi-prolog,以防万一。
谢谢。
prolog - 为序言表达式设置一些顺序
我的 Prolog 程序中有一条规则,blanket(X,Y)
它检查 X 是否在某种 Y 集合中,其中包括:
- Y的父母
- Y的孩子们
- Y的共同父母
我将其定义如下:
但是,这并不像我预期的那样工作。它正确地识别了 X 的父母、孩子和共同父母(如果有的话),但它将自己列为父母和共同父母,这是我不想要的。是否可以设置某种顺序,以便different(Y,X)
首先独立评估?当然,我尝试了类似 :different(Y,X),(parent(Y,X);child(Y,X);coparent(Y,X)).
的方法,但这会产生语法错误,因为我仍然对这种语言非常不熟悉。
任何建议将不胜感激。
编辑:这里是child
,different
和coparent
关系:
为了完整性。
prolog - 跟踪一个简单的序言代码以完全理解 CUT (!)
〃〃〃
首先,我在不使用 cut 的情况下编写了这段代码,然后我尝试将 cut 运算符放在每个谓词之后,直到我得到完美的答案,但实际上我不明白使用 cut 后它是如何工作的。我知道 cut 运算符停止 prolog 进行匹配,但是我无法正确使用它,所以我想帮助跟踪这段代码,这段代码只是删除了列表中所有出现的元素。
prolog - 从列表中删除元素
我知道这个问题已经被问过了,但我只想问一下我的具体实现。我编写这个函数只是为了练习 Prolog 并更好地理解 Prolog。这是我所拥有的:
New
这个想法是,如果我要删除的元素不等于,我会将一个元素添加到一个名为的新列表中H
。据我了解,我的代码无法正常工作,因为我的代码在到达 where 元素时停止Ele \= H
。任何想法如何解决这一问题?
例如,del(5, [3,4,5,6,7], X)
将返回 false。
另外,有没有更好的解决方案?将列表中的每个元素继续添加到新列表似乎是一个糟糕的解决方案,因为这对于大列表来说会很慢。我宁愿只保留当前列表中的元素,找到匹配Ele
的元素,删除该元素,然后返回列表。
list - 替换 sicstus prolog 中的列表元素
我正在编写一个 Prolog 谓词,它接受参数(A1,A2,L1,L2),如果 L1 中所有出现的 A1 都已更改为 L2 中的 A2,则成功。
IE:
这是我写的:
现在,这可行,但似乎有点不雅。有没有更优雅的解决方案?
谢谢。