0

I'm using SWI prolog and was wondering how I can use setof in my source code.

I have a file: prolog_example.pl which has some facts:

stuff(hello,1,2,west).
stuff(goodbye,3,4,west).
stuff(how,5,6,north).
stuff(are,7,8,north).

canMatch(X,Y):-
    stuff(X, _, _, XZ),
    stuff(Y, _, _, YZ),
    X \= Y.

Now I want to get the set of all west and north teams in a different list. So I put:

setof(X-Y, canMatch(X,Y), ListOfMatches).

within my source code. However when I try to compile or run this in SWI-Prolog, I end up getting this error:

ERROR: prolog_example.pl:37:
    No permission to modify static procedure `setof/3'

However, if I pose the setof as a query once the file is loaded (and I take out my setof line), I get the correct answer I want. So how do I use setof in my source code?

repeat
  • 18,496
  • 4
  • 54
  • 166
Alex
  • 2,145
  • 6
  • 36
  • 72
  • 2
    You use `setof/3` in exactly the same way as any other predicate: As one of the goals in a rule. For example: `all_matches(Ls) :- setof(X-Y, ..., Ls).` – mat Nov 25 '15 at 00:58
  • Please give more information on *where* you put it! – repeat Nov 25 '15 at 06:43
  • 1
    You're getting the error because you placed a query in the fact database, and it thinks you want to redefine `setof/3`. Instead, use this query interactively at the `?- ` prompt. – Daniel Lyons Nov 25 '15 at 06:45
  • You don't make a query by asserting a fact. You need to define a predicate, such as: `foo( ListOfMatches ) :- setof(X-Y, canMatch(X,Y), ListOfMatches).` then query `foo( List ).` Or just query, `setof(X-Y, canMatch(X,Y), ListOfMatches).` directly at the Prolog REPL prompt. – lurker Nov 25 '15 at 14:36

0 Answers0