5

I would like to get a list of solutions from a rule I made in Prolog.

However the findall predicate appears to only work with one variable.

Can anyone suggest how to get around this apparent limitation?

My rule

beat(P,M,E)

What I want

L = [[P,M],[P,M],................]

What I get now

L = [P,P,P,P,.........]

or

L = [M,M,M,M,M.............]
eazar001
  • 1,572
  • 2
  • 16
  • 29
ReiiYuki
  • 365
  • 5
  • 19

1 Answers1

13

findall can work with a surprisingly flexible amount of variations. I think you want something like this:

findall([P,M], beat(P,M,E), L).
eazar001
  • 1,572
  • 2
  • 16
  • 29
  • @ReiiYuki Please, mark the answer as accepted if this is the solution you need, so other users can find this answer helpful too. – Yasel Oct 16 '16 at 15:07
  • Can you reference to the docs of this feature. – eguneys Jun 19 '22 at 00:51
  • @eguneys https://www.swi-prolog.org/pldoc/man?section=allsolutions (for swi-prolog), but this is an ISO standard for all prologs, so you can find this in any of the docs for g-prolog, sicstus, etc. – eazar001 Jun 19 '22 at 01:25
  • That doesn't mention the usage you provided as solution, like using a list as the first parameter. – eguneys Jun 19 '22 at 01:41
  • @eguneys You can't find that information in a doc on a predicate. That knowledge comes from solid understanding of what terms, compound, terms, atoms, and unification is in prolog, and ... how a findall/N template relates to that fundamental understanding. – eazar001 Jun 19 '22 at 05:03
  • @eguneys The template parameter in findall/3 can literally unify with any type of term you can imagine. There is no limit, it is strictly a function of unification and backtracking over predicates. It's so flexible you can even do something like this, which seems ostensibly nonsensical for most use-cases: findall(1, beat(P,M,E), L). In that case for every successful unification, or proof of beat(P,M,E), you add another element `1` to a list L, and the aggregated results are unified with the variable `L`. There really are no restrictions on findall templates beyond violations of Prolog itself. – eazar001 Jun 19 '22 at 05:09
  • @eguneys In other words, this is more a "feature" of the language itself rather than findall/3. – eazar001 Jun 19 '22 at 05:15