Re: [eclipse-clp-users] how to solve this problem w/o non-logical variable?

From: Joachim Schimpf <joachim.schimpf_at_monash.edu>
Date: Sun, 12 Sep 2010 17:52:14 +1000
Ulrich Scholz wrote:
> Dear all,
> 
> for the following problem I only can come up with a solution that uses a
> non-logical variable.  I there a better way?
> 
> Consider I want to find all solutions of a predicate p.  At one point, there
> is the choice between q and r, i.e.
> 
> p :- q.
> p :- r.
> 
>>From studying the problem, I know that r can only have a solution if q had
> (at least one) solution itself.  In other words, if q fails without ever
> succeeding then r will fail, too.
> 
> The only way I see to prevent r from being tried is to set a non-logical
> flag if q succeeds and let r fail in case the flag is unset.  
> 
> Of course, unsuccessfully trying r is so expensive that preventing it is
> worth the effort.

If I understand the problem correctly, then you could write

  p :- \+ \+ q, ( q ; r ).

or, equivalently

  p :- ( \+ q -> false ; q ; r ).

Of course, this would only make sense if detecting
unsatisfiability of q is cheap.


Your non-logical solution is probably best here.  Make sure
you use a handle-based feature (to ensure reentrancy), e.g.

  p :-
     shelf_create(sol(false), Shelf),
     (
         p,
         shelf_set(Shelf, 1, true)    % set solution flag
     ;
         shelf_get(Shelf, 1, true),   % fail if p has no solution
         r
     ).



-- Joachim
Received on Sun Sep 12 2010 - 07:52:20 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET