3

Please see this code snipped:

...,
findall(X, predicate(Input1, X), XS),
XS \== [],
!,
member(X, XS),
...

That will find all solutions for predicate(_, X) in XS, cut, then "iterate" over the X in XS.

Is it possible to replace findall/3 in here? Most likely I won't be interested in all solutions for X. I need to know if it is satisfiable for Input1, and if so, continue with its solutions.


Please notice that this is, of course, not what I am looking for:

predicate(Input1, X),     % Uses other values for Input1

Neither is this:

!, predicate(Input1, X),  % I could need another value for Input1

And esp. not this:

predicate(Input1, X), !,  % I want further solutions for X
false
  • 10,264
  • 13
  • 101
  • 209
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • 1
    *Why* isn't `predicate(Input1,X)` what you're looking for? Usually with Prolog if you don't want all the answers you simply don't generate them all in a list. If you want a subset of the answers, you make a more selective predicate and then use that from `findall/3` et. al. Why aren't these approaches acceptable for you? – Daniel Lyons Apr 26 '13 at 16:16
  • @DanielLyons in my actual code `predicate(...)` is `call(Fun, ...)`, Fun comes from `member(Fun, (foo, bar, baz, qux))`. If `bar(Input1, X)` has a solution, then I do not care for `bar(Input1, X)` or `qux(Input1, X)`. With findall my code works well, but I do not think that it is the best™ implementation for my problem. :-) – Kijewski Apr 26 '13 at 16:24
  • 1
    I would expect `once(predicate(...))` would do what you want, or else extracting the part with the difficulties to its own predicate and then using `once` around that. But I am having trouble understanding your problem in the abstract. – Daniel Lyons Apr 26 '13 at 16:28
  • 1
    do you mean that you need all the solutions for the first `Input1` value that succeeds? – Will Ness Apr 26 '13 at 16:41

1 Answers1

2

How about this:

predicate(Input1,X), !, (Y=X ; predicate(Input1,Y), Y \= X).

I think that's very similar (not to say, exactly the same as) the soft cut, predicate(Input1,X) *-> .... It is also CONDa from the "Reasoned Schemer" book. I think.

Actually, this is not exactly the same as what you wanted. The following is closer, I think:

predicate(Input1,_), !, predicate(Input1,X).
Community
  • 1
  • 1
Will Ness
  • 70,110
  • 9
  • 98
  • 181