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
Currentwith 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=/2possibly instantiating variables on either side to achieve the unification. If, for example,Current,CostandNewPosare all uninstantiated, thenCurrentwill be instantiated withb(_, Cost, NewPos)(using the same variables,CostandNewPos. If, later on,Costis instantiated with, say,10, thenCurrentwill also become,b(_, 10, NewPos), in effect.;has lower precedence than->, so this is, in effect:Exit = nonewill attempt to unifyExitand the atom,none. It will succeed ifExitis uninstantiated (and will then instantiateExitwithnone), or can succeed ifExitis already instantiated asnone. It will fail ifExitis instantiated with any term that doesn't matchnone.If
Exit = nonesucceeds, then the firstbacktrack_path(Current, Visited, RPath)is called. If it fails, then an attempt is made to unifyExitwithb(5,5). If the priorExit = nonefailed, then we got to this point becauseExitwas 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 = nonesucceeds, thenbacktrack_path(Current, Visited, RPath)is called. Otherwise, then thenotherwisecall is done (NOTE that ifotherwiseis a block of code, then operator precedence can affect the grouping I show above, so I'm assumingotherwiseis a block which has precedence over the following->).If
otherwisesucceeds, then Prolog attemptsNewPos = Exit, and if that succeeds, it will move on to call,backtrack_path(Current, Visited, RPath). If theNewPos = Exitunification 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/2is/2is 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,
Xis unified with the term,A + B. SoXis now instantiated with the termA+B(which is the term,'+'(A, B))Xis unified with the term,A + B.Ais unified with2(and soAis instantiated with the value2), andBis unified with3. SoXis unified with2+3(or `'+'(2,3)).The expression on the right hand side of
is(the second argument tois/2) is evaluated yielding5. ThenXis instantiated to5.Bisn't instantiated, so the expression,A + Bcannot be evaluated, sois/2fails due to an instantiation error.A,B, andZare all instantiated to numeric values, andZ is A + Bsucceeds sinceA + Bevaluates to5, which is unifiable withZwhich also has the value5.A,B, andZare all instantiated to numeric values, andZ is A + Bfails sinceA + Bevaluates to5, which is not unifiable withZwhich has the value4.Xis unified with the term,A + B, which is the term2 + 3sinceAhas been instantiated with2, andBwith3.Zis unified with the evaluation of the expressionX(which is2 + 3) and so has the value5.