[mercury-users] some_of_solutions/2

Dave Slutzkin dave_slutzkin at yahoo.com
Thu Apr 4 14:26:06 AEST 2002


Hi all.

I'm in the middle of writing a compiler (in flex and bison) from a simple
functional language to Mercury.  (So I'm writing in C a compiler from my
language into Mercury, which will be compiled back into C...  Sounds a bit
silly...)

I'm having a problem trying to write a predicate which should be reasonably
simple.

% some_of_solutions is true when
%  L is a list of some of the solutions of Pred (including the full list)
%
% can be passed an unbound list in the second instance and will then bind
%  that list the full list of all solutions of Pred
:- pred some_of_solutions(pred(T),list(T)).
:- mode some_of_solutions(pred(out) is multi,in) is semidet.
:- mode some_of_solutions(pred(out) is multi,out) is det.
some_of_solutions(Pred,L) :-
    solutions(Pred,Sol) ,
    (
     L = Sol
     -> true
     ; list__sublist(Sol,L)
    ).

with the explained semantics.

I basically need to pass this function a list which is either bound or free. 
Going through the above code - if the list passed in can be unified with the
Solutions list, then it was free (or it is already the full list). 
Otherwise, check to make sure the (already bound) list is a sublist of the
solutions.

But of course, I can't bind a non-local variable in the condition of an
if-then-else.

This code is logical -
some_of_solutions(Pred,L) :-
    solutions(Pred,Sol) ,
    list__sublist(Sol,L).

But list__sublist doesn't have a mode (in,out) - presumably because that
would be multi and quite inefficient.  And anyway, I need the full sublist in
this case - just one of many possible sublists.  So this doesn't work.

I began to try writing a predicate 'fullsublist' which would have both modes
(in,out) and (in,in) - it would check to ensure that a list was _any_ sub
list in the in,in mode, and would produce a full sublist in the in,out mode. 
But this proved tricky.

If I could check a variable's bound/unbound status at run-time I could make
all these problems go away.  But I can't find any mention of this in the
docs.  Can anyone see a simple solution to this relatively simple problem?

thanks,

Dave Slutzkin.

http://www.sold.com.au - SOLD.com.au Auctions
- 1,000s of Bargains!
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list