[mercury-users] Equivalent of Prolog setof with free variable

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Jan 26 02:53:58 AEDT 2012


Hi,

On Tue, 24 Jan 2012, Jeff Thompson wrote:

> Hello.  What is the preferred way in Mercury to implement the equivalent of 
> setof with a free variable.  For example, Prolog:
>
> person(bob, accounting).
> person(alice, accounting).
> person(fred, sales).
>
> departmentMembers(Department, People) :-
>  setof(Person, person(Person, Department), People).
>
> Calling departmentMembers returns multiple times with results for the free 
> variable:
> Department = accounting,
> People = [alice, bob] ;
>
> Department = sales,
> People = [fred].
>
> What is the preferred way to get this in Mercury?

As Michael has mentioned, you will need to use the all-solutions
predicates from the standard library's solutions module.

A possible Mercury implementation of the above is:

     :- module foo.
     :- interface.
     :- pred main(io::di, io::uo) is det.
     :- implementation.

     :- import_module list.
     :- import_module set.
     :- import_module solutions.
     :- import_module string.

     :- type person ---> alice ; bob ; fred.

     :- type department ---> accounting ; sales.

     :- pred departmentMembers({department, list(person)}::out) is multi.

     departmentMembers({Department, People}) :-
        person(_, Department),
        solutions((pred(P::out) is multi :- person(P, Department)), People).

     :- pred person(person, department).
     :- mode person(out, in) is multi.
     :- mode person(out, out) is multi.

     person(bob, accounting).
     person(alice, accounting).
     person(fred, sales).

And since Mercury doesn't have an interactive top-level:

     main(!IO) :-
         solutions(departmentMembers, Solns),
         io.write_list(Solns, "\n\n", output_soln, !IO),
         io.nl(!IO).

     :- pred output_soln({department, list(person)}::in, io::di, io::uo) is det.

     output_soln({Department, People}, !IO) :-
         io.format("Department = %s,\nPeople = %s",
 	    [s(string(Department)), s(string(People))], !IO).

Julien.
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list