[mercury-users] Re: can one_solution/2 and unsorted_solutions/2 be det?

Andrew Bromage bromage at cs.mu.oz.au
Wed Oct 29 08:12:27 AEDT 1997


G'day.

Fergus Henderson wrote:

> Bart Demoen, you wrote:
> > 
> > (my code is Prolog rather than Mercury, because it doesn't matter)
> > 
> > Don and I seem in favour of a predicate like once/1 that could have
> > been implemented in Prolog as
> > 
> > 	once(Goal) :- Goal, !.
> 
> Why are you in favour of such a predicate?
> Please show us a program in which it would be useful.

There's one program I did recently in which it would be pragmatically
useful.  I was writing a sample solution for the mastermind player
assignment that we gave to students in the "Frontiers" subject here.

My solution used a number of heuristics, but if they failed (say, the
number of potential solutions was too large to handle the O(n^2)
algorithms I was using), the program decided that any potential
solution was good enough.  Thankfully, spotting a potential solution
is a relatively cheap operation, and so you can do something like:

potential_solution(Constraints, Solution) :-
	generate_solution(Solution),
	satisfies(Constraints, Solution).

So how to pick an arbitrary solution without generating the whole list
of solutions?  We could use committed choice nondeterminism, however,
the driver program that your player would be tested with (hi Tom!) had
its main being det.  Changing the driver is not an option here (though
lobbying the marker probably is).

(In the end, I did just generate the whole list then pick off a random
element.)

Cheers,
Andrew Bromage



More information about the users mailing list