Prolog中条件的使用 [英] Use of conditionals in Prolog

查看:40
本文介绍了Prolog中条件的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 prolog 中如何使用条件感到困惑.虽然示例 1 是条件语句的情况,示例 2 显示了在 NewPos = Exit 的另一侧显示的情况>-> 运算符.是否检查 NewPos 是否等于 Exit 或者值 Exit 是否被分配给 NewPos>?不应该使用 is 在序言中分配值吗?

I am confused about how conditionals are used in prolog. While Example 1 is the case for a conditional, Example 2 shows a case where it says NewPos = Exit on the other side of the -> operator. Is it checking if NewPos is equal to Exit or is it the case that the value Exit is being assigned to NewPos? Shouldn't an is be used to assign values in prolog?

抱歉,这是一个非常基本的语法问题.

Sorry if this is a very basic syntax question.

示例 1

Current = b(_,Cost,NewPos),
( Exit=none   -> backtrack_path(Current,Visited,RPath)
; Exit=b(5,5) -> backtrack_path(Current,Visited,RPath)

示例 2

Current = b(_,Cost,NewPos),
( Exit=none -> backtrack_path(Current,Visited,RPath)
; otherwise -> NewPos = Exit,
             backtrack_path(Current,Visited,RPath)

推荐答案

要更详细地了解 Prolog -> 运算符,请参阅 Prolog 运算符 '->' 的含义是什么.您的示例如下所示.

For a more detailed look at the Prolog -> operator, see What's the meaning of Prolog operator '->'. Your examples are covered below.

示例 1:

Current = b(_,Cost,NewPos),

Current 与术语 b(_, Cost, NewPos) 统一起来.Prolog 中的Unficiation与赋值相同.在统一中,Prolog 将尝试匹配 =/2 的两个参数,可能在任一侧实例化变量以实现统一.例如,如果 CurrentCostNewPos 都未实例化,则 Current 将使用 实例化>b(_, Cost, NewPos)(使用相同变量,CostNewPos.如果稍后,Cost 被实例化为,例如,10,那么 Current 也会变成,b(_, 10, NewPos),实际上.

Unifies Current with the term, b(_, Cost, NewPos). Unficiation in Prolog is not the same as assignment. In unification, Prolog will attempt to match the two arguments of =/2 possibly instantiating variables on either side to achieve the unification. If, for example, Current, Cost and NewPos are all uninstantiated, then Current will be instantiated with b(_, Cost, NewPos) (using the same variables, Cost and NewPos. If, later on, Cost is instantiated with, say, 10, then Current will also become, b(_, 10, NewPos), in effect.

( Exit=none   -> backtrack_path(Current,Visited,RPath)
; Exit=b(5,5) -> backtrack_path(Current,Visited,RPath)

; 具有较低的优先级->,所以实际上是:

; has lower precedence than ->, so this is, in effect:

(   Exit=none
->  backtrack_path(Current,Visited,RPath)
;   (   Exit=b(5,5)
    ->  backtrack_path(Current,Visited,RPath),
        ...
    ),
    ...
)

Exit = none 将尝试统一 Exit 和原子,none.如果 Exit 未实例化(然后使用 none 实例化 Exit),它将成功,或者如果 Exit 可以成功已经实例化为 none.如果 Exit 被任何不匹配 none 的术语实例化,它将会失败.

Exit = none will attempt to unify Exit and the atom, none. It will succeed if Exit is uninstantiated (and will then instantiate Exit with none), or can succeed if Exit is already instantiated as none. It will fail if Exit is instantiated with any term that doesn't match none.

如果 Exit = none 成功,则调用第一个 backtrack_path(Current, Visited, RPath).如果失败,则尝试将 Exitb(5,5) 统一.如果之前的 Exit = none 失败了,那么我们就到了这一点,因为 Exit 已经与不匹配 none 的东西统一了,并且只有当它已经与一个看起来像 b(X, Y) 的术语统一时,它才会成功 Exit = b(5,5).否则,它也会失败.如果成功,则再次调用 backtrack_path(Current, Visited, RPath).

If Exit = none succeeds, then the first backtrack_path(Current, Visited, RPath) is called. If it fails, then an attempt is made to unify Exit with b(5,5). If the prior Exit = none failed, then we got to this point because Exit was already unified with something that didn't match none, and it will succeed Exit = b(5,5) only if it was already unified with a term that looks like, b(X, Y). Otherwise, it will also fail. If it succeeds, then, again, backtrack_path(Current, Visited, RPath) will be called.

示例 2:

Current = b(_,Cost,NewPos),
( Exit=none -> backtrack_path(Current,Visited,RPath)
; otherwise -> NewPos = Exit,
             backtrack_path(Current,Visited,RPath)

, 具有最高优先级,其次是->,然后是;.所以这是有效的:

, has highest precedence, followed by ->, followed by ;. So this is effectively:

Current = b(_,Cost,NewPos),
(   Exit=none
->  backtrack_path(Current,Visited,RPath)
;   (   otherwise
    ->  (   NewPos = Exit,
            backtrack_path(Current,Visited,RPath),
            ...
        ),
        ...
    ),
    ...
 )

参见上面关于统一的讨论.如果 Exit = none 成功,则调用 backtrack_path(Current, Visited, RPath).否则,则 otherwise 调用完成(注意,如果 otherwise 是一个代码块,那么运算符优先级会影响我上面显示的分组,所以我假设 otherwise 是一个优先于以下 -> 的块.

See the discussion above about unification. If Exit = none succeeds, then backtrack_path(Current, Visited, RPath) is called. Otherwise, then then otherwise call is done (NOTE that if otherwise is a block of code, then operator precedence can affect the grouping I show above, so I'm assuming otherwise is a block which has precedence over the following ->).

如果否则成功,那么Prolog尝试NewPos = Exit,如果成功,它会继续调用,backtrack_path(Current, Visited, RPath).如果 NewPos = Exit 统一失败,那么在这种情况下,缺少else"或OR"(;)类型的表达式,它可能会一路回溯到 Current = b(_, Cost, NewPos).它回溯到哪里完全取决于您在此子句中的其他代码(它有点假设性地呈现,所以它可以是任何东西).

If otherwise succeeds, then Prolog attempts NewPos = Exit, and if that succeeds, it will move on to call, backtrack_path(Current, Visited, RPath). If the NewPos = Exit unification fails, then, in this case, lacking an "else" or "OR" (;) type of expression, it might backtrack all the way to Current = b(_, Cost, NewPos). Where it backtracks to depends completely upon what other code you have in this clause (it's a bit hypothetically presented, so it could be anything).

关于is/2

is/2 用于 (a) 评估数值表达式作为其第二个参数,以及 (b) 将表达式评估的结果与第一个参数统一.以下是一些示例,也显示了与 =/2(统一)的对比:

is/2 is used to (a) evaluate a numeric expression as its second argument, and (b) unify the result of the expression evaluation with the first argument. Here are some examples, also showing the contrast with =/2 (unification):

| ?- X = A + B.

X = A+B

yes

在这里,X 与术语 A + B 统一.所以 X 现在用术语 A+B 实例化(也就是术语,'+'(A, B))

Here, X is unified with the term, A + B. So X is now instantiated with the term A+B (which is the term, '+'(A, B))

| ?- A = 2, B = 3, X = A + B.

A = 2
B = 3
X = 2+3

yes

X 与术语 A + B 统一.A2 统一(因此 A实例化,值为 2),B3统一.所以X2+3(或`'+'(2,3))统一.

X is unified with the term, A + B. A is unified with 2 (and so A is instantiated with the value 2), and B is unified with 3. So X is unified with 2+3 (or `'+'(2,3)).

| ?- A = 2, B = 3, X is A + B.

A = 2
B = 3
X = 5

yes

is 右侧的表达式(is/2 的第二个参数)被计算为 5.然后将X实例化为5.

The expression on the right hand side of is (the second argument to is/2) is evaluated yielding 5. Then X is instantiated to 5.

| ?- A = 2, X is A + B.
uncaught exception: error(instantiation_error,(is)/2)

B 未实例化,因此无法计算表达式 A + B,因此 is/2 由于实例化而失败错误.

B isn't instantiated, so the expression, A + B cannot be evaluated, so is/2 fails due to an instantiation error.

| ?- A = 2, B = 3, Z = 5, Z is A + B.

A = 2
B = 3
Z = 5

yes

ABZ都实例化为数值,Z为A+B成功,因为 A + B 的计算结果为 5,这与 Z 统一,后者的值也为 5.

A, B, and Z are all instantiated to numeric values, and Z is A + B succeeds since A + B evaluates to 5, which is unifiable with Z which also has the value 5.

| ?- A = 2, B = 3, Z = 4, Z is A + B.

no

ABZ都实例化为数值,Z为A+B失败,因为 A + B 的计算结果为 5,这与值为 4Z 无法统一.

A, B, and Z are all instantiated to numeric values, and Z is A + B fails since A + B evaluates to 5, which is not unifiable with Z which has the value 4.

| ?- A = 2, B = 3, X = A + B, Z is X.

A = 2
B = 3
X = 2+3
Z = 5

yes

X 与术语 A + B 统一,从 A 开始就是术语 2 + 3> 已经用 2 实例化,B3 实例化.Z 与表达式 X(即 2 + 3)的求值统一,因此具有值 5.

X is unified with the term, A + B, which is the term 2 + 3 since A has been instantiated with 2, and B with 3. Z is unified with the evaluation of the expression X (which is 2 + 3) and so has the value 5.

这篇关于Prolog中条件的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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