syntax - Use of conditionals in Prolog -


i confused how conditionals used in prolog. while example 1 case conditional, example 2 shows case says newpos = exit on other side of -> operator. checking if newpos equal exit or case value exit being assigned newpos? shouldn't is used assign values in prolog?

sorry if 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 more detailed @ prolog -> operator, see what's meaning of prolog operator '->'. examples covered below.

example 1:

current = b(_,cost,newpos), 

unifies current term, b(_, cost, newpos). unficiation in prolog not same assignment. in unification, prolog attempt match 2 arguments of =/2 possibly instantiating variables on either side achieve unification. if, example, current, cost , newpos uninstantiated, current instantiated b(_, cost, newpos) (using same variables, cost , newpos. if, later on, cost instantiated with, say, 10, current 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 ->, is, in effect:

(   exit=none ->  backtrack_path(current,visited,rpath) ;   (   exit=b(5,5)     ->  backtrack_path(current,visited,rpath),         ...     ),     ... ) 

exit = none attempt unify exit , atom, none. succeed if exit uninstantiated (and instantiate exit none), or can succeed if exit instantiated none. fail if exit instantiated term doesn't match none.

if exit = none succeeds, first backtrack_path(current, visited, rpath) called. if fails, attempt made unify exit b(5,5). if prior exit = none failed, got point because exit unified didn't match none, , succeed exit = b(5,5) if unified term looks like, b(x, y). otherwise, fail. if succeeds, then, again, backtrack_path(current, visited, rpath) called.

example 2:

current = b(_,cost,newpos), ( exit=none -> backtrack_path(current,visited,rpath) ; otherwise -> newpos = exit,              backtrack_path(current,visited,rpath) 

, has highest precedence, followed ->, followed ;. effectively:

current = b(_,cost,newpos), (   exit=none ->  backtrack_path(current,visited,rpath) ;   (   otherwise     ->  (   newpos = exit,             backtrack_path(current,visited,rpath),             ...         ),         ...     ),     ...  ) 

see discussion above unification. if exit = none succeeds, backtrack_path(current, visited, rpath) called. otherwise, then otherwise call done (note if otherwise block of code, operator precedence can affect grouping show above, i'm assuming otherwise block has precedence on following ->).

if otherwise succeeds, prolog attempts newpos = exit, , if succeeds, move on call, backtrack_path(current, visited, rpath). if newpos = exit unification fails, then, in case, lacking "else" or "or" (;) type of expression, might backtrack way current = b(_, cost, newpos). backtracks depends upon other code have in clause (it's bit hypothetically presented, anything).

regarding is/2

is/2 used (a) evaluate numeric expression second argument, , (b) unify result of expression evaluation first argument. here examples, showing contrast =/2 (unification):

| ?- x = + b.  x = a+b  yes 

here, x unified term, a + b. x instantiated term a+b (which term, '+'(a, b))

| ?- = 2, b = 3, x = + b.  = 2 b = 3 x = 2+3  yes 

x unified term, a + b. a unified 2 (and a instantiated value 2), , b unified 3. x unified 2+3 (or `'+'(2,3)).

| ?- = 2, b = 3, x + b.  = 2 b = 3 x = 5  yes 

the expression on right hand side of is (the second argument is/2) evaluated yielding 5. x instantiated 5.

| ?- = 2, x + b. uncaught exception: error(instantiation_error,(is)/2) 

b isn't instantiated, expression, a + b cannot evaluated, is/2 fails due instantiation error.

| ?- = 2, b = 3, z = 5, z + b.  = 2 b = 3 z = 5  yes 

a, b, , z instantiated numeric values, , z + b succeeds since a + b evaluates 5, unifiable z has value 5.

| ?- = 2, b = 3, z = 4, z + b.  no 

a, b, , z instantiated numeric values, , z + b fails since a + b evaluates 5, not unifiable z has value 4.

| ?- = 2, b = 3, x = + b, z x.  = 2 b = 3 x = 2+3 z = 5  yes 

x unified term, a + b, term 2 + 3 since a has been instantiated 2, , b 3. z unified evaluation of expression x (which 2 + 3) , has value 5.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -