How to get Prolog to explain your result beyond the true statement -


i have following facts , rules:

  flight(sea,msp).   flight(msp,jfk).   route(a,b) :- flight(a,b).   route(b,a) :- flight(a,b).   route(a,c) :- flight(a,b) , flight(b,c).  

when query route(sea,jfk) result true wish explination:

sea-->msp-->jfk way can tell not it's true how it's true.

you keep track of nodes in graph you've visited. need anyway, need detect cycles in graph lest fall rabbit hole of infinite recursion.

and in prolog, use helper methods carry state around 1 or more arguments. used convention have "public" predicate — route/3 invokes "private" worker predicate having same name higher arity, route/4. ought you:

route( , b , r  ) :- % find route r b   route(a,b,[],r)      % - invoking worker, seeding list of visited nodes empty list   .                    % easy!  route(b,b,v,r) :-    % we've arrived @ destination (b) when origination node same destination node.   reverse([b|v],r)   % - reverse list of visited nodes routing.   .                  % route(a,b,v,r) :-    % otherwise...   flight(a,t) ,      % - if there's edge out of current node (a) ,   \+ member(t,v) ,   % - as-yet unvisited node...   route(t,b,[a|v],r) % - go visit node, marking current node visited.   .                  % easy! 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -