josh singer wrote: > > Just a clarification of the documentation. > > According to the findall/3 bip documentation (5.4): > > findall(?Term, +Goal, ?List) > > ... The variables appearing in Term should not appear anywhere else in the > clause except within Goal. ... > > Firstly, does this mean, for example, that the following code: > > .... > findall(result(X, Y, Z), do_stuff(X, Y, Z), [result(X, Y, Z)]), > do_something_else(X, Y, Z). > > should really be written: > > .... > findall(result(GX, GY, GZ), do_stuff(GX, GY, GZ), [result(X, Y, > Z)]), > do_something_else(X, Y, Z). > > ? What the documentation says is ok for plain Prolog, but in the presence of constraints it can actually make perfect sense to have global variables in Term, e.g. ?- X :: 1..5, findall(X, (X :: 0..9, indomain(X)), L). X = X{[1..5]} L = [1, 2, 3, 4, 5] If you don't share the X, you'll lose the constraints on it and get more solutions: ?- X :: 1..5, findall(GX, (GX :: 0..9, indomain(GX)), L). X = X{[1..5]} GX = GX L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Yes (0.00s cpu) > > Secondly, if we went ahead with the first pattern, what would happen? It's no problem, and, as I said, even more correct in the presence of constraints. The real question is: what are you trying to achieve with this strange use of findall? Fail if there is more than one solution? That could be extremely inefficient when there are many solutions, e.g. ?- findall(X, between(1,1000000,1,X), [X]). No (8.42s cpu) Should be done with a special-purpose unique_solution predicate, or we could specially detect the findall(+,+,+) mode. unique_solution(Vars, Goal) :- shelf_create(s(none), Shelf), ( call(Goal), ( shelf_get(Shelf, 1, none) -> shelf_set(Shelf, 1, sol(Vars)) % first solution ; ! % second solution ), fail ; shelf_get(Shelf, 1, sol(Vars)) % fail if no solution ). -- Joachim Schimpf / phone: +44 20 7594 8187 IC-Parc / mailto:J.Schimpf@imperial.ac.uk Imperial College London / http://www.icparc.ic.ac.uk/eclipseReceived on Sat May 24 18:39:55 2003
This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:08:22 PM GMT GMT