Thanks to both Paul and Julien. I ended up using most of Julien's solution but wrote main this way:<br>main(!IO) :-<br> io.format("DOG + ANT = CAT\n", [], !IO),<br> solutions(<br> (pred({DOG, ANT, CAT}::out) is nondet :- dogAntCat(DOG, ANT, CAT)),
<br> Solutions<br> ),<br> ( if<br> Solutions = []<br> then<br> io.format("No Result.", [], !IO)<br> else<br> io.write_list(Solutions, "\n", writeDogAntCat, !IO),
<br> io.nl(!IO)<br> ).<br><br>So I could keep my old dogAntCat definition. It just felt better to have the option of flipping one of the arguments to an in argument at some point.<br><br>I have a question about reordering predicates. The example had this to do that dogAntCat predicate:
<br> D0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], C0 = 0,<br> list.delete(D0, G, D1),<br> list.delete(D1, T, D2),<br> T = (G + T + C0) mod 10, C1 = (G + T + C0) / 10,<br> list.delete(D2, O, D3),<br> list.delete(D3, N, D4),
<br> A = (O + N + C1) mod 10, A\=0, C2 = (O + N + C1) / 10,<br> list.delete(D4, D, D5),<br> list.delete(D5, A, D6),<br> C = (D + A + C2), D \= 0, 0 = (D + A + C2) / 10,<br> list.delete(D6, C, _),<br> DOG = 100 * D + 10 * O + G,
<br> ANT = 100 * A + 10 * N + T,<br> CAT = 100 * C + 10 * A + T.<br><br>is it equivalent to do:<br> D0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], C0 = 0,<br> list.delete(D0, G, D1),<br> list.delete(D1, T, D2),<br>
list.delete(D2, O, D3),<br> list.delete(D3, N, D4),<br> list.delete(D4, D, D5),<br> list.delete(D5, A, D6),<br> list.delete(D6, C, _),<br> A = (O + N + C1) mod 10, A\=0, C2 = (O + N + C1) / 10,<br> T = (G + T + C0) mod 10, C1 = (G + T + C0) / 10,
<br> C = (D + A + C2), D \= 0, 0 = (D + A + C2) / 10,<br> DOG = 100 * D + 10 * O + G,<br> ANT = 100 * A + 10 * N + T,<br> CAT = 100 * C + 10 * A + T.<br><br>It feels like it would be, but I'm not sure if the compiler does infer that kind of thing.
<br><br><div><span class="gmail_quote">On 18/07/07, <b class="gmail_sendername">Julien Fischer</b> <<a href="mailto:juliensf@csse.unimelb.edu.au">juliensf@csse.unimelb.edu.au</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Hi,<br><br>On Wed, 18 Jul 2007, Nikolas Everett wrote:<br><br>> I'm sure you get this a lot, but how can I ask Mercury to do something with<br>> all the solutions that it finds for some predicate? At this point I'd just
<br>> like to print out all of my solutions, but I'm sure there are better things<br>> to do then that.<br>><br>> I've started with one of the programs in the Ralph Becket's tutorial and<br>> produced the below. I've messed around a little with the solutions module,
<br>> but haven't gotten any where.<br><br>The directory samples/solutions in the Mercury distribution contains<br>exaqmple of how to use the predicates in the solutions module.<br><br>There is also a brief discussion of the predicates in this module in the
<br>Prolog-to-Mercury transition guide, which is available here.<br><br><<a href="http://www.cs.mu.oz.au/research/mercury/information/doc-latest/mercury_trans_guide/All_002dsolutions.html#All_002dsolutions">http://www.cs.mu.oz.au/research/mercury/information/doc-latest/mercury_trans_guide/All_002dsolutions.html#All_002dsolutions
</a>><br><br><br>> :- module first.<br>> :- interface.<br>> :- import_module io.<br>><br>> :- pred main(io::di, io::uo) is cc_multi.<br>><br>> :- implementation.<br>> :- import_module int, list, string.
<br>><br>> main(!IO) :-<br>> io.format("DOG + ANT = CAT\n", [], !IO),<br>> ( if<br>> dogAntCat(DOG, ANT, CAT)<br>> then<br>> io.format("%d + %d = %d\n", [i(DOG), i(ANT), i(CAT)], !IO)
<br>> else<br>> io.format("No result.", [], !IO)<br>> ).<br>><br>> :- pred dogAntCat(int::out, int::out, int::out) is nondet.<br>> dogAntCat(DOG, ANT, CAT) :-<br>> ( if<br>> D0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], C0 = 0,
<br>> pick(D0, G, D1),<br>> pick(D1, T, D2),<br>> T = (G + T + C0) mod 10, C1 = (G + T + C0) / 10,<br>> pick(D2, O, D3),<br>> pick(D3, N, D4),<br>> A = (O + N + C1) mod 10, A\=0, C2 = (O + N + C1) / 10,
<br>> pick(D4, D, D5),<br>> pick(D5, A, D6),<br>> C = (D + A + C2), D \= 0, 0 = (D + A + C2) / 10,<br>> pick(D6, C, _)<br>> then<br>> DOG = 100 * D + 10 * O + G,<br>> ANT = 100 * A + 10 * N + T,
<br>> CAT = 100 * C + 10 * A + T<br>> else<br>> fail<br>> ).<br><br>The if-then-else in the body of this clause is redundant.<br><br>> :- pred pick(list(int)::in, int::out, list(int)::out) is nondet.
<br>> pick([X | Xs], X, Xs).<br>> pick([X | Xs], Y, [X | Zs]) :- pick(Xs, Y, Zs).<br><br>This predicate is available in the standard library in the list module<br>under the name list.delete/3.<br><br>A version of the above program that writes out all solutions using
<br>the predicate solutions/2 is:<br><br>:- module first.<br>:- interface.<br>:- import_module io.<br><br>:- pred main(io::di, io::uo) is det.<br><br>:- implementation.<br>:- import_module int.<br>:- import_module list.<br>
:- import_module solutions.<br>:- import_module string.<br><br>main(!IO) :-<br> io.format("DOG + ANT = CAT\n", [], !IO),<br> solutions(dogAntCat, Solutions),<br> (<br> Solutions = [],<br>
io.format("No result.", [], !IO)<br> ;<br> Solutions = [_ | _],<br> io.write_list(Solutions, "\n", write_solution, !IO),<br> io.nl(!IO)<br> ).<br><br>:- pred dogAntCat({int, int, int}::out) is nondet.
<br><br>dogAntCat({DOG, ANT, CAT}) :-<br> D0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], C0 = 0,<br> list.delete(D0, G, D1),<br> list.delete(D1, T, D2),<br> T = (G + T + C0) mod 10, C1 = (G + T + C0) / 10,<br> list.delete
(D2, O, D3),<br> list.delete(D3, N, D4),<br> A = (O + N + C1) mod 10, A \= 0, C2 = (O + N + C1) / 10,<br> list.delete(D4, D, D5),<br> list.delete(D5, A, D6),<br> C = (D + A + C2), D \= 0, 0 = (D + A + C2) / 10,
<br> list.delete(D6, C, _),<br> DOG = 100 * D + 10 * O + G,<br> ANT = 100 * A + 10 * N + T,<br> CAT = 100 * C + 10 * A + T.<br><br>:- pred write_solution({int, int, int}::in, io::di, io::uo) is det.<br><br>
write_solution({DOG, ANT, CAT}, !IO) :-<br> io.format("%d + %d = %d", [i(DOG), i(ANT), i(CAT)], !IO).<br><br>Cheers,<br>Julien.<br>--------------------------------------------------------------------------<br>
mercury-users mailing list<br>Post messages to: <a href="mailto:mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>Administrative Queries: <a href="mailto:owner-mercury-users@csse.unimelb.edu.au">
owner-mercury-users@csse.unimelb.edu.au</a><br>Subscriptions: <a href="mailto:mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a><br>--------------------------------------------------------------------------
<br></blockquote></div><br>