nondet. in conditionals 
Author Message
 nondet. in conditionals

Hi there:

Consider a predicate such as member(X,[1,2,3,4]),X==2 ->
write(a);write(b).  It has exactly one solution that outputs 'a'.
However, what I'm looking for are 4 solutions b,a,b,b.
One problem is the implicit cut in this kind of conditionals. Replacing
it by a version with no cuts, e.g., if/3 in Sicstus or Eclipse, yields
the same solution 'a'.
The obvious solution is to remove the member statement from the
condition, i.e.,
  member(X,[1,2,3,4]), (X==2->write(a);write(b)).
I can't use this one, however, since the member predicate actually is an
arbitrary predicate that may fail, resulting in a failure of the whole
expression.
The second solution is ugly:
Statements (C1,...,Cn->T;E) are replaced by
if(C1,if(C2,if(...,if(Cn,T,E),E),E),...,E)

I'm looking for a solution that is more elegant/direct/efficient.

Any hints will be appreciated.

Thanks,
Alex



Tue, 03 Jun 2003 04:17:59 GMT  
 nondet. in conditionals

Quote:

> Hi there:

Hi

not much help but anyway :

(response not written in textual order)

Quote:
> Consider a predicate such as member(X,[1,2,3,4]),X==2 ->
> write(a);write(b).  It has exactly one solution that outputs 'a'.
> However, what I'm looking for are 4 solutions b,a,b,b.
> One problem is the implicit cut in this kind of conditionals. Replacing
> it by a version with no cuts, e.g., if/3 in Sicstus or Eclipse, yields
> the same solution 'a'.
> The obvious solution is to remove the member statement from the
> condition, i.e.,
>   member(X,[1,2,3,4]), (X==2->write(a);write(b)).
> I can't use this one, however, since the member predicate actually is an
> arbitrary predicate that may fail, resulting in a failure of the whole
> expression.

how , arbitrary ?
 is it a totally unknown predicate argument ?
 or can you enforce some structure/invariants on it ?

maybe you can use findall/setof/bagof and test
specifically for null list case (corresponding to failure)

Quote:
> The second solution is ugly:
> Statements (C1,...,Cn->T;E) are replaced by
> if(C1,if(C2,if(...,if(Cn,T,E),E),E),...,E)

hmm , ... (maybe a solution)

(i haven't looked close that the above transformation really works but if so)

maybe one can put C1,...,Cn in a list
and make a higher-order-predicate which transforms and executes in just
that way :
(that way some of the ugliness would go away)

Statements (C1,...,Cn->T;E) are replaced by
nondet_multi_list_if( [C1,...,Cn] ,T ,E )

%predicate with too long name
nondet_multi_list_if( Cs ,T ,E ) :-
        nondet_multi_list_if_transform( Cs ,T ,E ,Pred ),
        call( Pred ).

nondet_multi_list_if_transform( [C   ] ,T ,E ,if(C,T   ,E) ).
nondet_multi_list_if_transform( [C|Cs] ,T ,E ,if(C,Pred,E) ):-
        is_cons(Cs),
        nondet_multi_list_if_transform( Cs ,T ,E ,Pred ).

cons([X|Xs],X,Xs).

is_cons([_|_]).

then you could do

:- op(1101,xfy,=->). %one step higher than ; (is this ISO compliant ?)

Cs =-> T ; E :-
        !,
        nondet_multi_list_if( Cs ,T ,E ).
Cs =-> T :-
        nondet_multi_list_if( Cs ,T ,true ).

and you could instead write :

[C1,...,Cn] =-> T ; E
or
[ member( X ,[1,2,3,4] ) ,X==2 ] =-> write( a ) ; write( b )

or something like that
(i have *not* tested this so beware !)

Quote:
> I'm looking for a solution that is more elegant/direct/efficient.

> Any hints will be appreciated.

> Thanks,
> Alex

i think in mercury (/= prolog  but  ~= prolog with type,mode,determinism system),
the test (A) in the if-else-construct (A->B;C) can succeed many times.

maybe this is not exactly what you need ?

(perhaps one could translate that construct from mercury to prolog
 , but i doubt it)

otherwise you can try to rewrite the problem/program so that the nondet
generator is not in the test ?
(like you say , with member it's easy , but maybe it can be done in your
 program too ?)

end of rant

md9slj
Stefan Lj



Sat, 07 Jun 2003 07:26:34 GMT  
 nondet. in conditionals

Quote:
> [...]
> > I can't use this one, however, since the member predicate actually is an
> > arbitrary predicate that may fail, resulting in a failure of the whole
> > expression.

> how , arbitrary ?
>  is it a totally unknown predicate argument ?
>  or can you enforce some structure/invariants on it ?

First of all, thanks for your response! The predicate(s) in the conditional part are
generated code from preconditions of transitions in specifications of reactive
systems (in a CASE tool called AutoFocus,
http://autofocus.informatik.tu-muenchen.de) Since these preconditions may involve
arbitrary (recursive) function definitions that have to be flattened, I can't
actually say a lot about their structure.

Quote:
> maybe you can use findall/setof/bagof and test
> specifically for null list case (corresponding to failure)

Well, no. The point of the above mentioned translation is the generation of test
cases where I need the decisions once at a time.

Quote:
> [...]
> [C1,...,Cn] =-> T ; E
> or
> [ member( X ,[1,2,3,4] ) ,X==2 ] =-> write( a ) ; write( b )

> or something like that
> (i have *not* tested this so beware !)

Okay. You're right it's more elegant, but I doubt it's more efficient (more
predicate calls involved). You might argue that for generated code elegance is not
exactly an issue (and I'd agree) whereas efficiency sure is. The question indeed
addressed both issues, elegance and efficiency.

Quote:
> i think in mercury (/= prolog  but  ~= prolog with type,mode,determinism system),
> the test (A) in the if-else-construct (A->B;C) can succeed many times.

> maybe this is not exactly what you need ?

yes it is. Additionally, even if A fails a couple of times, I'd like to get the C
answer in these cases.

Quote:
> (perhaps one could translate that construct from mercury to prolog
>  , but i doubt it)

I'll have a look at this.

Quote:
> otherwise you can try to rewrite the problem/program so that the nondet
> generator is not in the test ?
> (like you say , with member it's easy , but maybe it can be done in your
>  program too ?)

see above - it's generated code.

Thanks again,
Alex



Sat, 07 Jun 2003 17:45:31 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Does conditional COPY still look for member?

2. Does conditional COPY still look for member?

3. Column filling (conditional)

4. Conditional compiles on any build *BUT* debug?

5. conditionals and `in'

6. Help with conditional selection of fields...

7. conditional column total?

8. Printing conditional bold

9. Field Printing conditional

10. Conditional cancel of delete request

11. Conditional Compilation

12. conditional order on child file

 

 
Powered by phpBB® Forum Software