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.
Example 1
Current = b(_,Cost,NewPos),
( Exit=none -> backtrack_path(Current,Visited,RPath)
; Exit=b(5,5) -> backtrack_path(Current,Visited,RPath)
Example 2
Current = b(_,Cost,NewPos),
( Exit=none -> backtrack_path(Current,Visited,RPath)
; otherwise -> NewPos = Exit,
backtrack_path(Current,Visited,RPath)
For a more detailed look at the Prolog
->
operator, see What's the meaning of Prolog operator '->'. Your examples are covered below.Example 1:
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
andNewPos
are all uninstantiated, thenCurrent
will be instantiated withb(_, Cost, NewPos)
(using the same variables,Cost
andNewPos
. If, later on,Cost
is instantiated with, say,10
, thenCurrent
will also become,b(_, 10, NewPos)
, in effect.;
has lower precedence than->
, so this is, in effect:Exit = none
will attempt to unifyExit
and the atom,none
. It will succeed ifExit
is uninstantiated (and will then instantiateExit
withnone
), or can succeed ifExit
is already instantiated asnone
. It will fail ifExit
is instantiated with any term that doesn't matchnone
.If
Exit = none
succeeds, then the firstbacktrack_path(Current, Visited, RPath)
is called. If it fails, then an attempt is made to unifyExit
withb(5,5)
. If the priorExit = none
failed, then we got to this point becauseExit
was already unified with something that didn't matchnone
, and it will succeedExit = 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.Example 2:
,
has highest precedence, followed by->
, followed by;
. So this is effectively:See the discussion above about unification. If
Exit = none
succeeds, thenbacktrack_path(Current, Visited, RPath)
is called. Otherwise, then thenotherwise
call is done (NOTE that ifotherwise
is a block of code, then operator precedence can affect the grouping I show above, so I'm assumingotherwise
is a block which has precedence over the following->
).If
otherwise
succeeds, then Prolog attemptsNewPos = Exit
, and if that succeeds, it will move on to call,backtrack_path(Current, Visited, RPath)
. If theNewPos = Exit
unification fails, then, in this case, lacking an "else" or "OR" (;
) type of expression, it might backtrack all the way toCurrent = 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).Regarding
is/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):Here,
X
is unified with the term,A + B
. SoX
is now instantiated with the termA+B
(which is the term,'+'(A, B)
)X
is unified with the term,A + B
.A
is unified with2
(and soA
is instantiated with the value2
), andB
is unified with3
. SoX
is unified with2+3
(or `'+'(2,3)).The expression on the right hand side of
is
(the second argument tois/2
) is evaluated yielding5
. ThenX
is instantiated to5
.B
isn't instantiated, so the expression,A + B
cannot be evaluated, sois/2
fails due to an instantiation error.A
,B
, andZ
are all instantiated to numeric values, andZ is A + B
succeeds sinceA + B
evaluates to5
, which is unifiable withZ
which also has the value5
.A
,B
, andZ
are all instantiated to numeric values, andZ is A + B
fails sinceA + B
evaluates to5
, which is not unifiable withZ
which has the value4
.X
is unified with the term,A + B
, which is the term2 + 3
sinceA
has been instantiated with2
, andB
with3
.Z
is unified with the evaluation of the expressionX
(which is2 + 3
) and so has the value5
.