Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!news.mathworks.com!uunet!newsflash.concordia.ca!CC.UMontreal.CA!IRO.UMontreal.CA!tarau
From: tarau@IRO.UMontreal.CA (Paul Tarau)
Subject: Re: Novice prolog questions and thanks
Message-ID: <D3uG6r.B2q@IRO.UMontreal.CA>
Sender: news@IRO.UMontreal.CA
Organization: Universite de Montreal, Canada
References: <Pine.SOL.3.91.950209143630.11319A-100000@bingsun1> <3hf9m4$grt@news.irisa.fr>
Date: Sat, 11 Feb 1995 16:23:14 GMT
Lines: 48

In article <3hf9m4$grt@news.irisa.fr> ridoux@irisa.fr writes:
>In article <Pine.SOL.3.91.950209143630.11319A-100000@bingsun1>, Lars Kellogg-Stedman <consp01@bingsuns.cc.binghamton.edu> writes:
>...
>|> 	(a) Can I get from point X to point Z?
>|> 	(b) What is a path (shortest path?) from point X to point Z?
>|> 
>|> This almost works:
>|> 
>|> 	pathfrom(p1,p2).
>|> 	pathfrom(p2,p3).
>|> 	pathfrom(p3,p4).
>|> 	pathfrom(p4,p5).
>|> 
>|> 	pathfrom(X,Z):-pathfrom(X,Y),pathfrom(Y,Z).
>|> 
>|> However, if there are any loops in the path, this fails miserably, and 
>|> even this linear p1->p2->...->p5 example generates an infinite loop (try 
>|> pathfrom(p1,WHERE).  It sort of drops off the end...).
>|> 
>...
>
>You may either change (almost) nothing and use a memoing Prolog interpreter 
>(like XSB -see the FAQ), or indeed implement a graph traversal algorithm.
>
>Olivier Ridoux
>

For systems featuring or having the ability to implement some
form of linear implication (Lolli, LambdaProlog-MALI or BinProlog)
you can avoid looping in graph traversal algorithms by `consuming'
visited nodes through the use of linear implication ( -: in BinProlog,
-o in Lolli ).

Here is a CUT & PASTE from the BinProlog 3.30 distribution (netsurf with Mosaic
for file progs/lgraph.pl on ftp://clement.info.umoncton.ca/BinProlog):

path(X,X,[X]).
path(X,Z,[X|Xs]):-linked(X,Y),path(Y,Z,Xs).

linked(X,Y):-c(X,Ys),member(Y,Ys).

go(Xs):-
  c(1,[2,3]) -: c(2,[1,4]) -: c(3,[1,5]) -: c(4,[1,5]) -:
  path(1,5,Xs).


Paul Tarau

