[m-rev.] diff: avoid impure code in extras/trailed_update/samples/interpreter.m
Fergus Henderson
fjh at cs.mu.OZ.AU
Sun Jan 19 01:02:59 AEDT 2003
Estimated hours taken: 0.75
Branches: main
extras/trailed_update/samples/interpreter.m:
Use `unsafe_promise_unique' rather than impure code.
This is a cleaner solution. (We don't want to be
recommending the use of `impure' in sample code
when better alternatives are available!)
Workspace: /home/ceres/fjh/mercury
Index: extras/trailed_update/samples/interpreter.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/trailed_update/samples/interpreter.m,v
retrieving revision 1.5
diff -u -d -r1.5 interpreter.m
--- extras/trailed_update/samples/interpreter.m 13 Apr 2000 10:05:34 -0000 1.5
+++ extras/trailed_update/samples/interpreter.m 5 Nov 2002 06:27:22 -0000
@@ -74,34 +74,27 @@
:- pred print_solutions(varset, map(var, my_var(S)), my_term(S),
store(S), database, io__state, io__state).
-:- mode print_solutions(in, in, in, mdi, in, di, uo) is det.
+:- mode print_solutions(in, in, in, mdi, in, di, uo) is cc_multi.
-/***
-% Alas, the following code gets a (spurious) unique mode error,
+% The call to unsafe_promise_unique here is needed because without it,
+% the following code gets a (spurious) unique mode error,
% because the compiler thinks that `Store0' has inst `ground'
% rather than `mostly_unique' when it is passed as a curried
% argument of a higher-order term. The compiler doesn't know
% that unsorted_aggregate will only call its higher-order argument
% once per forward execution.
%
-% Instead, we use the impure code below.
+% It might be nicer to use do_while rather than unsorted_aggregate,
+% so that we can prompt the user after each solution to see if they
+% want to see the next solution.
%
print_solutions(VarSet, VarMap, MyGoal, Store0, Database) -->
unsorted_aggregate(
- solve(Database, MyGoal, Store0),
+ (pred(Store::muo) is nondet :-
+ solve(Database, MyGoal, unsafe_promise_unique(Store0),
+ Store)),
write_solution(VarSet, VarMap, MyGoal)),
io__write_string("No (more) solutions.\n").
-***/
-
-:- pragma promise_pure(print_solutions/7).
-print_solutions(VarSet, VarMap, MyGoal, Store0, Database) -->
- (
- { solve(Database, MyGoal, Store0, Store1) },
- { impure write_solution(VarSet, VarMap, MyGoal, Store1) },
- { fail }
- ;
- io__write_string("No (more) solutions.\n")
- ).
:- pred write_solution(varset, map(var, my_var(S)), my_term(S), store(S),
io__state, io__state).
@@ -114,18 +107,6 @@
{ my_term_to_term(MyGoal, Goal, VarSet0, VarSet, VarMap0, _VarMap,
Store0, _Store) },
term_io__write_term_nl(VarSet, Goal).
-
-:- impure pred write_solution(varset, map(var, my_var(S)), my_term(S),
- store(S)).
-:- mode write_solution(in, in, in, mdi) is det.
-
-write_solution(VarSet0, VarToMyVarMap, MyGoal, Store0) :-
- map__keys(VarToMyVarMap, Vars),
- map__values(VarToMyVarMap, MyVars),
- map__from_corresponding_lists(MyVars, Vars, VarMap0),
- my_term_to_term(MyGoal, Goal, VarSet0, VarSet, VarMap0, _VarMap,
- Store0, _Store),
- impure unsafe_perform_io(term_io__write_term_nl(VarSet, Goal)).
%-----------------------------------------------------------------------------%
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | 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