2

I have two predicates:

foo(Y,X)
bar(Y,Z)

After running foo, How can I run bar with all possibilities of Y ?

example:

foo(Y, key) % all possibilities of Y => chat 
            %                           faq 
            %                           about
            %                           search

How can I run bar with these all possibilities ?

bar(chat, Z)
bar(faq, Z)
bar(about, Z)
bar(serach, Z)

And then store all the results of Z in a list Zs?

false
  • 10,264
  • 13
  • 101
  • 209
user1423470
  • 77
  • 2
  • 6

3 Answers3

2

foo/2 and bar/2 are already in join, and after each run of foo/2 bar/2 will be tried.

Maybe you are looking for forall(foo(Y,X), bar(Y,Z)), that run all possibilities of foo/2, and then bar/2. I.e. is required that bar/2 doesn't fail.

To understand the behaviour of forall/2, as well as other all solutions builtins, like setof/3, can be useful test with very simple builtins, with well known behaviour:

?- forall(member(X,[f,o,o]),(member(Y,[b,a,r]),writeln(X-Y))).
f-b
o-b
o-b
true.

You can see that the complete solution search of forall applies to its first argument, not the second.

HTH

CapelliC
  • 59,646
  • 5
  • 47
  • 90
0

I think you want something like this:

barOnList([], []).
barOnList([Y|Ys], [Z|Zs]) :- bar(Y, Z), barOnList(Ys, Zs).
Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • `maplist/3` would do the trick too, and if it's not provided in the implementation you use, several ones are proposed here on SO. – m09 Jun 01 '12 at 08:46
  • But, foo in not return a list.It returns only one value. Ex: foo(Y,key) return chat then I press a to show all possibilities. Afterwards, faq, about, search are also seen in terminal. – user1423470 Jun 01 '12 at 09:14
0
allZs(X, Zs) :-
    setof(Y, foo(Y, X), Ys),
    maplist(bar, Ys, Zs).

related SWI-Prolog man pages: Finding all Solutions to a Goal and library apply

Note: usually in Prolog the convention is to put intput arguments before output ones - in your first predicate that'd mean foo(X, Y) instead of foo(Y, X). Plus here it'd outline the transitivity: foo(X, Y), bar(Y, Z)..

Community
  • 1
  • 1
m09
  • 7,490
  • 3
  • 31
  • 58
  • Actually, I have store undirected graph over using foo. Ex: foo (x,y) means x and y are connected to each other. I am finding a way from neighbour of y to target. For this reason, I have write foo (Y,X ). – user1423470 Jun 01 '12 at 09:18
  • Well yeah it was a sidenote, it happens that an inversed order is justified. – m09 Jun 01 '12 at 09:22