[m-rev.] diff: fix extras/trailed_update/samples/interpreter

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Feb 19 17:51:17 AEDT 2003


Estimated hours taken: 1
Branches: main

Fix a problem that broke extras/trailed_update/samples/interpreter.

extras/trailed_update/samples/interpreter.m:
	Use an assoc_list rather than a map when mapping from store__mutvars.
	This is needed because store__mutvars (and other reference types)
	can only be tested for equality, not compared.  That changed
	after my previous patch to add a new TYPE_CTOR_REP for references.
	We need to disallow compare/3 on reference types because in *.agc
	grades, their addresses might change after a garbage collection,
	and the changes might not preserve the ordering.

extras/trailed_update/samples/interpreter.exp:
	Update to reflect the above change to interpreter.m.

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.6
diff -u -d -r1.6 interpreter.m
--- extras/trailed_update/samples/interpreter.m	18 Jan 2003 14:03:08 -0000	1.6
+++ extras/trailed_update/samples/interpreter.m	19 Feb 2003 06:42:35 -0000
@@ -27,7 +27,7 @@
 
 :- implementation.
 :- import_module list, string, term, varset, term_io, require, std_util.
-:- import_module store, tr_store, map, multi_map.
+:- import_module store, tr_store, map, multi_map, assoc_list.
 :- import_module unsafe.
 
 main -->
@@ -103,7 +103,7 @@
 write_solution(VarSet0, VarToMyVarMap, MyGoal, Store0) -->
 	{ map__keys(VarToMyVarMap, Vars) },
 	{ map__values(VarToMyVarMap, MyVars) },
-	{ map__from_corresponding_lists(MyVars, Vars, VarMap0) },
+	{ assoc_list__from_corresponding_lists(MyVars, Vars, VarMap0) },
 	{ my_term_to_term(MyGoal, Goal, VarSet0, VarSet, VarMap0, _VarMap,
 			Store0, _Store) },
 	term_io__write_term_nl(VarSet, Goal).
@@ -233,7 +233,7 @@
 
 my_term_to_term(MyTerm, Term) -->
 	{ varset__init(VarSet0) },
-	{ map__init(VarMap0) },
+	{ VarMap0 = [] },
 	my_term_to_term(MyTerm, Term, VarSet0, _VarSet, VarMap0, _VarMap).
 
 :- pred my_term_to_term_list(list(my_term(S)), list(term), store(S), store(S)).
@@ -241,12 +241,19 @@
 
 my_term_to_term_list(MyTerms, Terms) -->
 	{ varset__init(VarSet0) },
-	{ map__init(VarMap0) },
+	{ VarMap0 = [] },
 	my_term_to_term_list(MyTerms, Terms,
 		VarSet0, _VarSet, VarMap0, _VarMap).
 
+% note that we need to use an assoc_list here rather than a map,
+% because store mutvars can only be tested for equality, not compared
+% (this in turn is because in implementations which use copying GC,
+% the relative addresses of different mutvars might change after
+% a garbage collection).
+
 :- pred my_term_to_term(my_term(S), term, varset, varset,
-		map(my_var(S), var), map(my_var(S), var), store(S), store(S)).
+		assoc_list(my_var(S), var), assoc_list(my_var(S), var),
+		store(S), store(S)).
 :- mode my_term_to_term(in, out, in, out, in, out, mdi, muo) is det.
 
 my_term_to_term(var(MyVar), variable(Var), VarSet0, VarSet, VarMap0, VarMap)
@@ -256,13 +263,13 @@
 	% if so, use its corresponding Var,
 	% otherwise, create a fresh Var and insert it into the VarMap
 	%
-	( { map__search(VarMap0, MyVar, Var1) } ->
+	( { assoc_list__search(VarMap0, MyVar, Var1) } ->
 		{ Var = Var1 },
 		{ VarSet1 = VarSet0 },
 		{ VarMap1 = VarMap0 }
 	;
 		{ varset__new_var(VarSet0, Var, VarSet1) },
-		{ map__det_insert(VarMap0, MyVar, Var, VarMap1) }
+		{ VarMap1 = [MyVar - Var | VarMap0] }
 	),
 	%
 	% check whether MyVar is bound;
@@ -285,8 +292,8 @@
 	{ context_init(Context) },
 	my_term_to_term_list(Args0, Args, VarSet0, VarSet, VarMap0, VarMap).
 
-:- pred my_term_to_term_list(list(my_term(S)), list(term),
-		varset, varset, map(my_var(S), var), map(my_var(S), var),
+:- pred my_term_to_term_list(list(my_term(S)), list(term), varset, varset,
+		assoc_list(my_var(S), var), assoc_list(my_var(S), var),
 		store(S), store(S)).
 :- mode my_term_to_term_list(in, out, in, out, in, out, mdi, muo) is det.
Index: extras/trailed_update/samples/interpreter.exp
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/trailed_update/samples/interpreter.exp,v
retrieving revision 1.4
diff -u -d -r1.4 interpreter.exp
--- extras/trailed_update/samples/interpreter.exp	22 Jan 2003 03:00:57 -0000	1.4
+++ extras/trailed_update/samples/interpreter.exp	19 Feb 2003 06:43:26 -0000
@@ -16,7 +16,7 @@
 :- module interpreter.
 :- implementation.
 :- import_module unsafe.
-:- import_module store, tr_store, map, multi_map.
+:- import_module store, tr_store, map, multi_map, assoc_list.
 :- import_module list, string, term, varset, term_io, require, std_util.
 :- import_module io.
 :- mode database_lookup_pred_clause(in, in, out) is nondet.
@@ -60,8 +60,8 @@
 :- pred unify_list(list(my_term(_2)), list(my_term(_2)), store(_2), store(_2)).
 :- pred unify(my_term(_2), my_term(_2), store(_2), store(_2)).
 :- pred solve(database, my_term(_2), store(_2), store(_2)).
-:- pred my_term_to_term_list(list(my_term(_2)), list(term), varset, varset, map(my_var(_2), var), map(my_var(_2), var), store(_2), store(_2)).
-:- pred my_term_to_term(my_term(_2), term, varset, varset, map(my_var(_2), var), map(my_var(_2), var), store(_2), store(_2)).
+:- pred my_term_to_term_list(list(my_term(_2)), list(term), varset, varset, assoc_list(my_var(_2), var), assoc_list(my_var(_2), var), store(_2), store(_2)).
+:- pred my_term_to_term(my_term(_2), term, varset, varset, assoc_list(my_var(_2), var), assoc_list(my_var(_2), var), store(_2), store(_2)).
 :- pred my_term_to_term_list(list(my_term(_2)), list(term), store(_2), store(_2)).
 :- pred my_term_to_term(my_term(_2), term, store(_2), store(_2)).
 :- pred term_to_my_term_list(list(term), list(my_term(_2)), map(var, my_var(_2)), map(var, my_var(_2)), store(_2), store(_2)).
@@ -89,7 +89,7 @@
 consult_until_eof_2(term(_3, _4), _5, _6) --> { database_assert_clause(_5, _3, _4, _7) }, consult_until_eof(_7, _6).
 consult_until_eof_2(error(_3, _4), _5, _6) --> io__write_string("Error reading term at line "), io__write_int(_4), io__write_string(" of standard input: "), io__write_string(_3), io__write_string("\n"), consult_until_eof(_5, _6).
 consult_until_eof_2(eof, _3, _3) --> [].
-my_term_to_term(_3, _4) --> { varset__init(_5) }, { map__init(_6) }, my_term_to_term(_3, _4, _5, _7, _6, _8).
+my_term_to_term(_3, _4) --> { varset__init(_5) }, { _6 = [] }, my_term_to_term(_3, _4, _5, _7, _6, _8).
 consult_list([_3 | _4], _5, _6) --> consult(_3, _5, _7), consult_list(_4, _7, _6).
 consult_list([], _3, _3) --> [].
 consult(_3, _4, _5) --> io__write_string("Consulting file `"), io__write_string(_3), io__write_string("\'...\n"), io__see(_3, _6), ({ _6 = ok } -> consult_until_eof(_4, _5), io__seen ; io__write_string("Error opening file `"), io__write_string(_3), io__write_string("\' for input.\n"), { _5 = _4 }).
@@ -104,12 +104,12 @@
 main_loop_2(term(_3, _4), _5) --> { store__new(_6) }, { map__init(_7) }, { term_to_my_term(_4, _8, _7, _9, _6, _10) }, print_solutions(_3, _9, _8, _10, _5), main_loop(_5).
 main_loop_2(error(_3, _4), _5) --> io__write_string("Error reading term at line "), io__write_int(_4), io__write_string(" of standard input: "), io__write_string(_3), io__write_string("\n"), main_loop(_5).
 main_loop_2(eof, _3) --> [].
-my_term_to_term_list(_3, _4) --> { varset__init(_5) }, { map__init(_6) }, my_term_to_term_list(_3, _4, _5, _7, _6, _8).
+my_term_to_term_list(_3, _4) --> { varset__init(_5) }, { _6 = [] }, my_term_to_term_list(_3, _4, _5, _7, _6, _8).
 not_occurs(functor(_3, _4), _5) --> not_occurs_list(_4, _5).
 not_occurs(var(_3), _4) --> { _3 \= _4 }, tr_store__get_mutvar(_3, _5), ({ _5 = free } -> [] ; not_occurs(_5, _4)).
 my_term_to_term(functor(_3, _4), functor(_3, _5, _6), _7, _8, _9, _10) --> { context_init(_6) }, my_term_to_term_list(_4, _5, _7, _8, _9, _10).
 my_term_to_term(free, variable(_3), _4, _5, _6, _6) --> { varset__new_var(_4, _3, _5) }, { error("my_term_to_term: unexpected free var") }.
-my_term_to_term(var(_3), variable(_4), _5, _6, _7, _8) --> ({ map__search(_7, _3, _9) } -> { _4 = _9 }, { _10 = _5 }, { _11 = _7 } ; { varset__new_var(_5, _4, _10) }, { map__det_insert(_7, _3, _4, _11) }), tr_store__get_mutvar(_3, _12), ({ _12 \= free } -> my_term_to_term(_12, _13, _10, _14, _11, _8), { varset__bind_var(_14, _4, _13, _6) } ; { _8 = _11 }, { _6 = _10 }).
+my_term_to_term(var(_3), variable(_4), _5, _6, _7, _8) --> ({ assoc_list__search(_7, _3, _9) } -> { _4 = _9 }, { _10 = _5 }, { _11 = _7 } ; { varset__new_var(_5, _4, _10) }, { _11 = [_3 - _4 | _7] }), tr_store__get_mutvar(_3, _12), ({ _12 \= free } -> my_term_to_term(_12, _13, _10, _14, _11, _8), { varset__bind_var(_14, _4, _13, _6) } ; { _8 = _11 }, { _6 = _10 }).
 my_term_to_term_list([_3 | _4], [_5 | _6], _7, _8, _9, _10) --> my_term_to_term(_3, _5, _7, _11, _9, _12), my_term_to_term_list(_4, _6, _11, _8, _12, _10).
 my_term_to_term_list([], [], _3, _3, _4, _4) --> [].
 not_occurs_list([_3 | _4], _5) --> not_occurs(_3, _5), not_occurs_list(_4, _5).
@@ -131,6 +131,6 @@
 term_to_my_term_list([], [], _3, _3) --> [].
 unify_list([_3 | _4], [_5 | _6]) --> unify(_3, _5), unify_list(_4, _6).
 unify_list([], []) --> [].
-write_solution(_3, _4, _5, _6) --> { map__keys(_4, _7) }, { map__values(_4, _8) }, { map__from_corresponding_lists(_8, _7, _9) }, { my_term_to_term(_5, _10, _3, _11, _9, _12, _6, _13) }, term_io__write_term_nl(_11, _10).
+write_solution(_3, _4, _5, _6) --> { map__keys(_4, _7) }, { map__values(_4, _8) }, { assoc_list__from_corresponding_lists(_8, _7, _9) }, { my_term_to_term(_5, _10, _3, _11, _9, _12, _6, _13) }, term_io__write_term_nl(_11, _10).
 No (more) solutions.
 ?- 
 
-- 
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