[m-rev.] diff: add n_solutions.m to samples

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Jul 26 10:51:08 AEST 2004


Estimated hours taken: 0.5
Branches: main

samples/solutions/n_solutions.m:
	New file, showing how to compute the first N solutions of a
	nondeterministic predicate.

Workspace: /home/jupiter/fjh/ws-jupiter/mercury
Index: samples/solutions/n_solutions.m
===================================================================
RCS file: samples/solutions/n_solutions.m
diff -N samples/solutions/n_solutions.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ samples/solutions/n_solutions.m	29 Feb 2004 04:52:32 -0000
@@ -0,0 +1,40 @@
+% An example program to illustrate the use of the `do_while'
+% predicate in Mercury.  This program calls a nondeterministic
+% predicate hello/1, and prints the first N solutions it finds
+% (in this case for N = 2).
+%
+% Note that in the standard "commutative" semantics, the order of
+% solutions is unspecified.  If you want to force the order of
+% evaluation, then you would need to use the "strict sequential semantics"
+% (enabled by the `--strict-sequential' option to the Mercury compiler).
+
+% This source file is hereby placed in the public domain.  -fjh (the author).
+
+:- module n_solutions.
+:- interface.
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is cc_multi.
+
+:- implementation.
+:- import_module std_util, bool, int.
+
+main(!IO) :-
+	N = 2,
+	do_while(hello, get_next, {!.IO, N}, {!:IO, _}),
+	print("Done\n", !IO).
+
+:- pred hello(string::out) is multi.
+
+hello("Hello, world\n").
+hello("Good day, world\n").
+hello("Greetings, world\n").
+
+:- pred get_next(string::in, bool::out, {io, int}::di, {io, int}::uo)
+	is det.
+
+get_next(String, More, {IO0, Max0}, {IO, Max}) :-
+	print(String, IO0, IO),
+	More = (Max0 > 1 -> yes ; no),
+	Max = Max0 - 1.
+

-- 
Fergus Henderson                    |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list