'如果'在序言中?
有没有办法在序言中执行 if 操作,例如,如果变量为 0,则执行一些操作(将文本写入终端)。甚至不需要 else,但我找不到 if 的任何文档。
Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn't even needed, but I can't find any documentation of if.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的,ISO Prolog 中有这样一个控制结构,称为
->
。您可以这样使用它:这是一个使用 else-if 子句链的示例:
请注意,如果省略 else-子句,则条件失败将意味着整个 if 语句将失败。因此,我建议始终包含 else 子句(即使它只是
true
)。Yes, there is such a control construct in ISO Prolog, called
->
. You use it like this:Here is an example that uses a chain of else-if-clauses:
Note that if you omit the else-clause, the condition failing will mean that the whole if-statement will fail. Therefore, I recommend always including the else-clause (even if it is just
true
).标准的序言谓词将执行此操作。
如果您使用 5 调用它,则评估结果为 true;如果您使用其他任何值运行它,则评估结果为失败(返回 false)。对于不等于,您使用 \=
从技术上讲,它并不统一,但它与不等于类似。
Learn Prolog Now 是一个学习 Prolog 的好网站。
编辑:
添加另一个例子。
A standard prolog predicate will do this.
will evaluate to true if you call it with 5 and fail(return false) if you run it with anything else. For not equal you use \=
Technically it is does not unify, but it is similar to not equal.
Learn Prolog Now is a good website for learning prolog.
Edit:
To add another example.
Prolog 谓词“统一” -
因此,在命令式语言中,我会写
在 Prolog 中,当您理解两种风格时,我会写
,实际上会更清晰。
“我对 foo 为 5 时的特殊情况感到非常满意”
“当 foo 不是 5 时,我对正常情况很满意”
Prolog predicates 'unify' -
So, in an imperative langauge I'd write
In Prolog I'd write
which, when you understand both styles, is actually a lot clearer.
"I'm bazoo for the special case when foo is 5"
"I'm bazoo for the normal case when foo isn't 5"
首先,让我们回顾一些经典的一阶逻辑:
我们怎样才能像 Prolog 中的 that 一样表达“if-then-else”?
我们来看下面的具体例子:
我们可以匹配上面的模式(“If P then Q else R”)如果...
P
是list_member([1,2],X)
,non_P
是non_member([1,2],X)
,Q
为X=2
,R
为X=4
。为了以纯粹的方式表达列表(非)成员身份,我们定义:
让我们看看在 Prolog 中表达“if-then-else”的不同方式!
(P,Q;non_P,R)
(P -> Q ; R)
(P *-> Q ; R)
(初步)摘要:
(P,Q ; non_P,R)
是正确的,但需要non_P
的离散实现。(P -> Q ; R)
在实例化不足时丢失声明性语义。(P *-> Q ; R)
比(P -> Q ; R)
的不完整程度“更少”,但仍然有类似的问题。< /p>幸运的是,我们还有其他选择:
输入逻辑单调控制结构
if_/3
!我们可以使用
if_/3
与具体化列表成员谓词memberd_t/3
< /a> 像这样:正确性得分 5/5。效率得分 4/5。
First, let's recall some classical first order logic:
How can we express "if-then-else" like that in Prolog?
Let's take the following concrete example:
We can match above pattern ("If P then Q else R") if ...
P
islist_member([1,2],X)
,non_P
isnon_member([1,2],X)
,Q
isX=2
, andR
isX=4
.To express list (non-)membership in a pure way, we define:
Let's check out different ways of expressing "if-then-else" in Prolog!
(P,Q ; non_P,R)
Correctness score 5/5. Efficiency score 3/5.
(P -> Q ; R)
Correctness score 2/5. Efficiency score 2/5.
(P *-> Q ; R)
Correctness score 3/5. Efficiency score 1/5.
(Preliminary) summary:
(P,Q ; non_P,R)
is correct, but needs a discrete implementation ofnon_P
.(P -> Q ; R)
loses declarative semantics when instantiation is insufficient.(P *-> Q ; R)
is "less" incomplete than(P -> Q ; R)
, but still has similar woes.Luckily for us, there are alternatives:
Enter the logically monotone control construct
if_/3
!We can use
if_/3
together with the reified list-membership predicatememberd_t/3
like so:Correctness score 5/5. Efficiency score 4/5.
我发现这对于在规则中使用 if 语句很有帮助。
感谢 http://cs.union.edu/~striegnk /learn-prolog-now/html/node89.html
I found this helpful for using an if statement in a rule.
Thanks to http://cs.union.edu/~striegnk/learn-prolog-now/html/node89.html
在 Prolog 中表达 if-then-else 之类的内容本质上有三种不同的方式。要比较它们,请考虑
char_class/2
。对于a
和b
,类应为ab
,对于所有其他术语,类应为other
。人们可能会像这样笨拙地写这个:为了更紧凑地写东西,需要一个 if-then-else 结构。 Prolog 有一个内置的答案:
虽然这个答案很合理,但它并不完整。仅给出了
( Ch = a ; Ch = b )
的第一个答案。其他答案都被砍掉了。确实不太相关。更好的构造,通常称为“软剪切”(不要相信这个名字,剪切就是剪切就是剪切),给出了稍微更好的结果(这是在 YAP 中):
或者,SICStus 有
if/3
具有非常相似的语义:所以最后一个答案仍然被抑制。现在输入 Scryer 的
library(reif)
,SICStus,YAP< /a> 和 SWI。安装它并说:请注意,所有
if_/3
都被编译为疯狂嵌套的 if-then-else,其在 YAP 6.3.4 中扩展为:
There are essentially three different ways how to express something like if-then-else in Prolog. To compare them consider
char_class/2
. Fora
andb
the class should beab
andother
for all other terms. One could write this clumsily like so:To write things more compactly, an if-then-else construct is needed. Prolog has a built-in one:
While this answer is sound, it is incomplete. Just the first answer from
( Ch = a ; Ch = b )
is given. The other answers are chopped away. Not very relational, indeed.A better construct, often called a "soft cut" (don't believe the name, a cut is a cut is a cut), gives slightly better results (this is in YAP):
Alternatively, SICStus has
if/3
with very similar semantics:So the last answer is still suppressed. Now enter
library(reif)
for Scryer, SICStus, YAP, and SWI. Install it and say:Note that all the
if_/3
is compiled away to a wildly nested if-then-else forwhich expands in YAP 6.3.4 to:
最好的办法是使用所谓的
cuts
,它具有符号!
。以上是条件函数的基本结构。
举个例子,这是 max 函数:
我建议阅读更多有关剪切的文档,但一般来说它们就像断点。
例如:如果第一个 max 函数返回真值,则不会验证第二个函数。
PS:我对 Prolog 相当陌生,但这就是我发现的。
The best thing to do is to use the so-called
cuts
, which has the symbol!
.The above is the basic structure of a condition function.
To exemplify, here's the
max
function:I suggest reading more documentation on cuts, but in general they are like breakpoints.
Ex.: In case the first
max
function returns a true value, the second function is not verified.PS: I'm fairly new to Prolog, but this is what I've found out.
Prolog 程序实际上是“if”的大条件,“then”打印“目标已达到”,“else”打印“未找到解决方案”。
A, B表示“A为真且B为真”,如果“A”不可到达,大多数prolog系统不会尝试满足“B”(即
X=3,写('X is 3'),nl
当 X=3 时将打印 'X is 3',如果 X=2 将不执行任何操作)。Prolog program actually is big condition for "if" with "then" which prints "Goal is reached" and "else" which prints "No sloutions was found".
A, B
means "A is true and B is true", most of prolog systems will not try to satisfy "B" if "A" is not reachable (i.e.X=3, write('X is 3'),nl
will print 'X is 3' when X=3, and will do nothing if X=2).else 部分为必填项
The else part is required
您应该阅读立即学习Prolog!第 10.2 章使用 Cut。这提供了一个示例:
max(X,Y,Z) :- X =< Y,!,Y = Z。
可以说,
Z
等于Y
IF!
为真(它总是如此) ) ANDX
是<= Y
。You should read Learn Prolog Now! Chapter 10.2 Using Cut. This provides an example:
max(X,Y,Z) :- X =< Y,!, Y = Z.
to be said,
Z
is equal toY
IF!
is true (which it always is) ANDX
is<= Y
.