diff: use_module

Simon TAYLOR stayl at students.cs.mu.oz.au
Fri Jun 27 18:57:17 AEST 1997


Hi Fergus,

Could you please review this.

Simon.


Estimated hours taken: 13 

Implemented a :- use_module directive. This is the same as 
:- import_module, except all uses of the imported items
must be explicitly module qualified.

:- use_module is implemented by ensuring that unqualified versions
of items only get added to the HLDS symbol tables if they were imported
using import_module.

Indirectly imported items (from `.int2' files) and items declared in `.opt'
files are treated as if they were imported with use_module, since all uses
of them should be module qualified. 

compiler/module_qual.m
	Keep two sets of type, mode and inst ids, those which can
	be used without qualifiers and those which can't.

	Renamed some predicates which no longer have unique names since
	'__' became a synonym for ':'.

	Made mq_info_set_module_used check whether the current item is in
	the interface, rather than relying on its caller to do the check.

	Removed init_mq_info_module, since make_hlds.m now uses the 
	mq_info built during the module qualification pass.

compiler/prog_data.m
	Add a boolean argument to the `imported' pseudo-declaration which
	is yes if the following items must be module qualified.

compiler/make_hlds.m
	Keep with the import_status whether current item was imported
	using a :- use_module directive.

	Use the mq_info structure passed in instead of building a new one.

	Ensure unqualified versions of constructors only get added to the
	cons_table if they can be used without qualification.

compiler/hlds_module.m
	Added an extra boolean argument to predicate_table_insert which
	is yes if calls to the predicate being inserted must be explicitly
	module qualified.

	Only add predicates to the name and name-arity indices if they
	can be used without qualifiers.

	Changed the structure of the module-name-arity index, so that 
	lookups can be made without an arity, such as when type-checking 
	module qualified higher-order predicate constants. This does not 
	change the interface to the module_name_arity index.

	Factored out some common code in predicate_table_insert which
	applies to both predicates and functions.

compiler/hlds_pred.m
	Removed the opt_decl import_status. It isn't needed any more
	since all uses of items declared in .opt files must now be 
	module qualified.

	Added some documentation about when the clauses_info is valid.

compiler/intermod.m
	Ensure that predicate and function calls in the `.opt' file are 
	module qualified. Use use_module instead of import_module in 
	`.opt' files.

compiler/modules.m
	Handle use_module directives. 

	Report an error if both use_module and import_module declarations
	exist for the same module.

compiler/mercury_compile.m
	Collect inter-module optimization information before module 
	qualification, since it can't cause conflicts any more. This means
	that the mq_info structure built in module_qual.m can be reused in
	make_hlds.m, instead of building a new one.

compiler/prog_out.m
	Add a predicate prog_out__write_module_list, which was moved
	here from module_qual.m.

compiler/typecheck.m
	Removed code to check that predicates declared in `.opt' files
	were being used appropriately, since this is now handled by 
	use_module.

compiler/*.m
	Added missing imports, mostly for prog_data and term.

NEWS
compiler/notes/todo.html
doc/reference_manual.texi
	Document `:- use_module'.	

tests/valid/intermod_test.m
tests/valid/intermod_test2.m
	Regression test for a bug in inter-module optimization 
	that this fixes.

tests/invalid/errors.m
tests/invalid/errors2.m
	Test cases.

Index: NEWS
===================================================================
RCS file: /home/staff/zs/imp/mercury/NEWS,v
retrieving revision 1.54
diff -u -r1.54 NEWS
--- NEWS	1997/06/19 07:13:13	1.54
+++ NEWS	1997/06/27 07:36:09
@@ -33,6 +33,11 @@
 
   You can use just plain `write_string' rather than `io__write_string'.
 
+* There is a new `:- use_module' directive.
+
+  This is the same as `:- import_module', except all uses of the imported
+  items must be explicitly module qualified.
+
   More changes to the module system are expected in the future,
   including changing the module qualifier operator to `.'
   (currently either `:' or `__' can be used as module qualifiers).
@@ -51,7 +56,7 @@
   The `--intermodule-optimization' option enables cross-module inlining
   and cross-module specialization of higher-order predicates.
   Also `--intermod-unused-args' enables cross-module elimination of
-  unused arguments.
+  unused input arguments.
 
 * We've continued to improve the quality of the code we generate.
 
Index: compiler/arg_info.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/arg_info.m,v
retrieving revision 1.21
diff -u -r1.21 arg_info.m
--- arg_info.m	1997/04/07 05:39:02	1.21
+++ arg_info.m	1997/06/16 01:46:17
@@ -22,7 +22,7 @@
 
 :- module arg_info.
 :- interface. 
-:- import_module hlds_module, llds, globals.
+:- import_module hlds_module, llds, globals, prog_data.
 
 :- pred generate_arg_info(module_info, args_method, module_info).
 :- mode generate_arg_info(in, in, out) is det.
Index: compiler/bytecode.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/bytecode.m,v
retrieving revision 1.27
diff -u -r1.27 bytecode.m
--- bytecode.m	1997/06/05 06:53:58	1.27
+++ bytecode.m	1997/06/16 01:47:08
@@ -14,7 +14,7 @@
 
 :- interface.
 
-:- import_module hlds_data, llds, tree.
+:- import_module hlds_data, prog_data, llds, tree.
 :- import_module list, std_util, io.
 
 :- type byte_tree	==	tree(list(byte_code)).
Index: compiler/call_gen.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/call_gen.m,v
retrieving revision 1.111
diff -u -r1.111 call_gen.m
--- call_gen.m	1997/06/05 05:47:49	1.111
+++ call_gen.m	1997/06/16 01:47:22
@@ -19,6 +19,7 @@
 :- interface.
 
 :- import_module hlds_pred, llds, code_info.
+:- import_module term.
 
 :- pred call_gen__generate_higher_order_call(code_model, var, list(var),
 			list(type), list(mode), determinism, hlds_goal_info,
Index: compiler/code_aux.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/code_aux.m,v
retrieving revision 1.47
diff -u -r1.47 code_aux.m
--- code_aux.m	1997/05/21 02:13:12	1.47
+++ code_aux.m	1997/06/16 01:48:01
@@ -52,7 +52,7 @@
 
 :- implementation.
 
-:- import_module hlds_module, llds_out, type_util.
+:- import_module hlds_module, llds, llds_out, type_util.
 :- import_module bool, string, set, term, std_util, assoc_list, require.
 :- import_module list, map.
 
Index: compiler/code_exprn.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/code_exprn.m,v
retrieving revision 1.49
diff -u -r1.49 code_exprn.m
--- code_exprn.m	1997/05/21 02:13:13	1.49
+++ code_exprn.m	1997/06/16 01:48:49
@@ -31,7 +31,7 @@
 
 :- interface.
 
-:- import_module llds, list, varset, assoc_list, options.
+:- import_module hlds_goal, llds, list, varset, assoc_list, options.
 
 :- type exprn_info.
 
Index: compiler/code_info.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/code_info.m,v
retrieving revision 1.205
diff -u -r1.205 code_info.m
--- code_info.m	1997/06/11 05:13:43	1.205
+++ code_info.m	1997/06/16 01:49:42
@@ -41,7 +41,7 @@
 
 :- import_module hlds_module, hlds_data, code_util.
 :- import_module code_exprn, set, varset, term, stack, prog_data.
-:- import_module type_util, mode_util, options.
+:- import_module continuation_info, type_util, mode_util, options.
 :- import_module string, require, char, list, map, bimap, tree, int.
 
 %---------------------------------------------------------------------------%
Index: compiler/continuation_info.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/continuation_info.m,v
retrieving revision 1.1
diff -u -r1.1 continuation_info.m
--- continuation_info.m	1997/05/27 03:06:21	1.1
+++ continuation_info.m	1997/06/16 01:50:32
@@ -51,8 +51,8 @@
 
 :- implementation.
 
-:- import_module llds.
-:- import_module map, list, assoc_list, std_util.
+:- import_module llds, prog_data.
+:- import_module map, list, assoc_list, std_util, term.
 
 	% The continuation_info data structure 
 
Index: compiler/cse_detection.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/cse_detection.m,v
retrieving revision 1.43
diff -u -r1.43 cse_detection.m
--- cse_detection.m	1997/05/08 06:46:50	1.43
+++ cse_detection.m	1997/06/16 01:51:14
@@ -35,7 +35,7 @@
 
 :- import_module hlds_goal, hlds_data, options, globals, goal_util, hlds_out.
 :- import_module modes, mode_util, make_hlds, quantification, instmap.
-:- import_module switch_detection, det_util.
+:- import_module prog_data, switch_detection, det_util.
 
 :- import_module int, bool, list, map, set, std_util, require, term, varset.
 
Index: compiler/delay_info.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/delay_info.m,v
retrieving revision 1.11
diff -u -r1.11 delay_info.m
--- delay_info.m	1997/02/23 06:05:49	1.11
+++ delay_info.m	1997/06/16 01:52:07
@@ -20,7 +20,8 @@
 
 :- interface.
 
-:- import_module mode_errors.
+:- import_module hlds_goal, mode_errors.
+:- import_module term.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/dense_switch.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/dense_switch.m,v
retrieving revision 1.25
diff -u -r1.25 dense_switch.m
--- dense_switch.m	1997/05/21 02:13:20	1.25
+++ dense_switch.m	1997/06/16 01:52:31
@@ -16,7 +16,8 @@
 
 :- interface.
 
-:- import_module llds, switch_gen, code_info, type_util.
+:- import_module llds, prog_data, switch_gen, code_info, type_util.
+:- import_module term.
 
 	% Should this switch be implemented as a dense jump table?
 	% If so, we return the starting and ending values for the table,
Index: compiler/det_analysis.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/det_analysis.m,v
retrieving revision 1.116
diff -u -r1.116 det_analysis.m
--- det_analysis.m	1997/06/12 00:10:43	1.116
+++ det_analysis.m	1997/06/16 01:53:02
@@ -104,7 +104,7 @@
 :- import_module hlds_goal, prog_data, det_report, det_util.
 :- import_module mode_util, globals, options, passes_aux.
 :- import_module hlds_out, mercury_to_mercury, instmap.
-:- import_module bool, list, map, set, std_util, require.
+:- import_module bool, list, map, set, std_util, require, term.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/det_util.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/det_util.m,v
retrieving revision 1.10
diff -u -r1.10 det_util.m
--- det_util.m	1997/05/20 01:51:41	1.10
+++ det_util.m	1997/06/16 01:53:24
@@ -18,7 +18,7 @@
 
 :- import_module hlds_module, hlds_pred, hlds_goal, hlds_data, globals.
 :- import_module instmap.
-:- import_module set, list.
+:- import_module bool, set, list.
 
 :- type maybe_changed	--->	changed ; unchanged.
 
Index: compiler/dnf.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/dnf.m,v
retrieving revision 1.19
diff -u -r1.19 dnf.m
--- dnf.m	1997/05/05 11:16:51	1.19
+++ dnf.m	1997/06/16 01:53:59
@@ -59,7 +59,7 @@
 
 :- import_module hlds_goal, hlds_data, prog_data, instmap.
 :- import_module excess, make_hlds, mode_util.
-:- import_module require, map, list, string, int, bool, std_util.
+:- import_module require, map, list, string, int, bool, std_util, term, varset.
 
 	% Traverse the module structure.
 
Index: compiler/equiv_type.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/equiv_type.m,v
retrieving revision 1.6
diff -u -r1.6 equiv_type.m
--- equiv_type.m	1996/12/22 03:01:15	1.6
+++ equiv_type.m	1997/06/16 03:51:05
@@ -37,7 +37,7 @@
 
 :- implementation.
 :- import_module bool, require, std_util, map, term, varset.
-:- import_module type_util, prog_util, prog_out.
+:- import_module hlds_data, type_util, prog_data, prog_util, prog_out.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
Index: compiler/excess.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/excess.m,v
retrieving revision 1.21
diff -u -r1.21 excess.m
--- excess.m	1997/05/05 11:16:52	1.21
+++ excess.m	1997/06/16 01:54:52
@@ -43,7 +43,7 @@
 :- implementation.
 
 :- import_module hlds_goal, goal_util.
-:- import_module varset, list, bool, map, set, std_util.
+:- import_module varset, list, bool, map, set, std_util, term.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/export.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/export.m,v
retrieving revision 1.11
diff -u -r1.11 export.m
--- export.m	1997/02/28 05:30:03	1.11
+++ export.m	1997/06/16 01:55:19
@@ -15,7 +15,7 @@
 
 :- interface.
 
-:- import_module hlds_module.
+:- import_module hlds_module, prog_data.
 :- import_module io, list, term.
 
 	% From the module_info, get a list of functions, each of which allows
Index: compiler/exprn_aux.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/exprn_aux.m,v
retrieving revision 1.20
diff -u -r1.20 exprn_aux.m
--- exprn_aux.m	1997/04/17 07:47:20	1.20
+++ exprn_aux.m	1997/06/16 01:55:38
@@ -9,7 +9,7 @@
 :- interface.
 
 :- import_module llds, options.
-:- import_module list, std_util, bool, assoc_list.
+:- import_module list, std_util, bool, assoc_list, term.
 
 :- type exprn_opts
 	--->	nlg_asm_sgt_ubf(
Index: compiler/globals.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/globals.m,v
retrieving revision 1.20
diff -u -r1.20 globals.m
--- globals.m	1997/05/20 01:51:46	1.20
+++ globals.m	1997/06/16 03:08:50
@@ -16,7 +16,7 @@
 %-----------------------------------------------------------------------------%
 
 :- interface.
-:- import_module bool, getopt.
+:- import_module bool, getopt, list.
 :- import_module options.
 
 :- type globals.
Index: compiler/hlds_goal.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/hlds_goal.m,v
retrieving revision 1.33
diff -u -r1.33 hlds_goal.m
--- hlds_goal.m	1997/05/21 02:13:26	1.33
+++ hlds_goal.m	1997/06/16 03:10:23
@@ -12,7 +12,7 @@
 
 :- interface.
 
-:- import_module hlds_data, prog_data, instmap.
+:- import_module hlds_data, hlds_pred, llds, prog_data, instmap.
 :- import_module list, assoc_list, set, map, std_util.
 
 	% Here is how goals are represented
Index: compiler/hlds_module.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/hlds_module.m,v
retrieving revision 1.21
diff -u -r1.21 hlds_module.m
--- hlds_module.m	1997/05/27 03:06:25	1.21
+++ hlds_module.m	1997/06/19 09:41:30
@@ -26,9 +26,9 @@
 
 :- implementation.
 
-:- import_module hlds_data, hlds_out, prog_data, prog_util.
-:- import_module require, int, string, list, map, set, std_util.
+:- import_module hlds_data, hlds_out, llds, prog_data, prog_util.
 :- import_module typecheck.
+:- import_module bool, require, int, string, list, map, set, std_util.
 
 %-----------------------------------------------------------------------------%
 
@@ -967,10 +967,20 @@
 				sym_name, arity, list(pred_id)) is semidet.
 :- mode predicate_table_search_pf_sym_arity(in, in, in, in, out) is semidet.
 
-	% Insert a new pred_info structure into the predicate_table
-	% and assign it a new pred_id. You should check beforehand
-	% that the pred doesn't already occur in the table.
+	% predicate_table_insert(PredTable0, PredInfo, NeedQual, PredId,
+	% 		PredTable).
+	% 
+	% Insert PredInfo into PredTable0 and assign it a new pred_id.
+	% You should check beforehand that the pred doesn't already 
+	% occur in the table. NeedQual should be yes if all uses of the 
+	% predicate must be module qualified, such as predicates from modules
+	% imported using `:- use_module' and predicates from `.opt' files.
+:- pred predicate_table_insert(predicate_table, pred_info, bool, pred_id,
+				predicate_table).
+:- mode predicate_table_insert(in, in, in, out, out) is det.
 
+	% Equivalent to predicate_table_insert(PredTable0, PredInfo, 
+	%	yes, PredId, PredTable). 
 :- pred predicate_table_insert(predicate_table, pred_info, pred_id,
 				predicate_table).
 :- mode predicate_table_insert(in, in, out, out) is det.
@@ -1023,8 +1033,11 @@
 :- type name_arity_index == map(name_arity, list(pred_id)).
 :- type name_arity ---> string / arity.
 
-:- type module_name_arity_index == map(module_name_arity, list(pred_id)).
-:- type module_name_arity ---> module_name_arity(module_name, string, arity).
+	% First search on module and name, then search on arity. The two 
+	% levels are needed because typecheck.m needs to be able to search
+	% on module and name only for higher-order terms.
+:- type module_name_arity_index == map(pair(module_name, string), 
+					map(arity, list(pred_id))).
 
 predicate_table_init(PredicateTable) :-
 	PredicateTable = predicate_table(Preds, NextPredId, PredIds,
@@ -1085,9 +1098,8 @@
 	predicate_table_search_name(PredicateTable, Name, PredIdList).
 predicate_table_search_sym(PredicateTable, qualified(Module, Name),
 		PredIdList) :-
-	predicate_table_search_name(PredicateTable, Name, PredIdList0),
-	predicate_table_get_preds(PredicateTable, PredTable),
-	find_preds_matching_module(PredIdList0, Module, PredTable, PredIdList),
+	predicate_table_search_module_name(PredicateTable, 
+		Module, Name, PredIdList),
 	PredIdList \= [].
 
 :- predicate_table_search_pred_sym(_, X, _) when X. % NU-Prolog indexing.
@@ -1097,9 +1109,8 @@
 	predicate_table_search_pred_name(PredicateTable, Name, PredIdList).
 predicate_table_search_pred_sym(PredicateTable, qualified(Module, Name),
 		PredIdList) :-
-	predicate_table_search_pred_name(PredicateTable, Name, PredIdList0),
-	predicate_table_get_preds(PredicateTable, PredTable),
-	find_preds_matching_module(PredIdList0, Module, PredTable, PredIdList),
+	predicate_table_search_pred_module_name(PredicateTable, 
+		Module, Name, PredIdList),
 	PredIdList \= [].
 
 :- predicate_table_search_func_sym(_, X, _) when X. % NU-Prolog indexing.
@@ -1109,30 +1120,13 @@
 	predicate_table_search_func_name(PredicateTable, Name, PredIdList).
 predicate_table_search_func_sym(PredicateTable, qualified(Module, Name),
 		PredIdList) :-
-	predicate_table_search_func_name(PredicateTable, Name, PredIdList0),
-	predicate_table_get_preds(PredicateTable, PredTable),
-	find_preds_matching_module(PredIdList0, Module, PredTable, PredIdList),
+	predicate_table_search_func_module_name(PredicateTable, Module,
+		Name, PredIdList),
 	PredIdList \= [].
 
 	% Given a list of predicates, and a module name, find all the
 	% predicates which came from that module.
 
-:- pred find_preds_matching_module(list(pred_id), module_name, pred_table,
-		list(pred_id)).
-:- mode find_preds_matching_module(in, in, in, out) is det.
-
-find_preds_matching_module([], _, _, []).
-find_preds_matching_module([PredId | PredIds0], Module, PredTable, PredIds) :-
-	(
-		map__search(PredTable, PredId, PredInfo),
-		pred_info_module(PredInfo, Module)
-	->
-		PredIds = [PredId | PredIds1]
-	;
-		PredIds = PredIds1
-	),
-	find_preds_matching_module(PredIds0, Module, PredTable, PredIds1).
-
 %-----------------------------------------------------------------------------%
 
 :- predicate_table_search_sym_arity(_, X, _, _) when X. % NU-Prolog indexing.
@@ -1200,6 +1194,54 @@
 
 %-----------------------------------------------------------------------------%
 
+:- pred predicate_table_search_module_name(predicate_table, module_name, 
+		string, list(pred_id)).
+:- mode predicate_table_search_module_name(in, in, in, out) is semidet.
+
+predicate_table_search_module_name(PredicateTable, Module, Name, PredIds) :-
+	(
+		predicate_table_search_pred_module_name(PredicateTable, 
+			Module, Name, PredPredIds0)
+	->
+		PredPredIds = PredPredIds0
+	;
+		PredPredIds = []
+	),
+	(
+		predicate_table_search_func_module_name(PredicateTable, 
+			Module, Name, FuncPredIds0)
+	->
+		FuncPredIds = FuncPredIds0
+	;
+		FuncPredIds = []
+	),
+	list__append(FuncPredIds, PredPredIds, PredIds),
+	PredIds \= [].
+
+:- pred predicate_table_search_pred_module_name(predicate_table, module_name,
+		string, list(pred_id)).
+:- mode predicate_table_search_pred_module_name(in, in, in, out) is semidet.
+
+predicate_table_search_pred_module_name(PredicateTable, 
+		Module, PredName, PredIds) :-
+	PredicateTable = predicate_table(_,_,_,_,_, Pred_MNA_Index, _,_,_),
+	map__search(Pred_MNA_Index, Module - PredName, Arities),
+	map__values(Arities, PredIdLists),
+	list__condense(PredIdLists, PredIds).
+
+:- pred predicate_table_search_func_module_name(predicate_table, module_name,
+		string, list(pred_id)).
+:- mode predicate_table_search_func_module_name(in, in, in, out) is semidet.
+
+predicate_table_search_func_module_name(PredicateTable, 
+		Module, FuncName, PredIds) :-
+	PredicateTable = predicate_table(_,_,_,_,_,_,_,_, Func_MNA_Index),
+	map__search(Func_MNA_Index, Module - FuncName, Arities),
+	map__values(Arities, PredIdLists),
+	list__condense(PredIdLists, PredIds).
+
+%-----------------------------------------------------------------------------%
+
 predicate_table_search_name_arity(PredicateTable, Name, Arity, PredIds) :-
 	(
 		predicate_table_search_pred_name_arity(PredicateTable,
@@ -1258,14 +1300,14 @@
 predicate_table_search_pred_m_n_a(PredicateTable, Module, PredName, Arity,
 		PredIds) :-
 	PredicateTable = predicate_table(_, _, _, _, _, P_MNA_Index, _, _, _),
-	MNA = module_name_arity(Module, PredName, Arity),
-	map__search(P_MNA_Index, MNA, PredIds).
+	map__search(P_MNA_Index, Module - PredName, ArityIndex),
+	map__search(ArityIndex, Arity, PredIds).
 
 predicate_table_search_func_m_n_a(PredicateTable, Module, FuncName, Arity,
 		PredIds) :-
 	PredicateTable = predicate_table(_, _, _, _, _, _, _, _, F_MNA_Index),
-	MNA = module_name_arity(Module, FuncName, Arity),
-	map__search(F_MNA_Index, MNA, PredIds).
+	map__search(F_MNA_Index, Module - FuncName, ArityIndex),
+	map__search(ArityIndex, Arity, PredIds).
 
 %-----------------------------------------------------------------------------%
 
@@ -1307,6 +1349,11 @@
 %-----------------------------------------------------------------------------%
 
 predicate_table_insert(PredicateTable0, PredInfo, PredId, PredicateTable) :-
+	predicate_table_insert(PredicateTable0, PredInfo, yes,
+		PredId, PredicateTable).
+
+predicate_table_insert(PredicateTable0, PredInfo, NeedQual,
+		PredId, PredicateTable) :-
 	PredicateTable0 = predicate_table(Preds0, NextPredId0, PredIds0,
 				Pred_N_Index0, Pred_NA_Index0, Pred_MNA_Index0,
 				Func_N_Index0, Func_NA_Index0, Func_MNA_Index0),
@@ -1323,41 +1370,10 @@
 	pred_info_get_is_pred_or_func(PredInfo, PredOrFunc),
 	( 
 		PredOrFunc = predicate,
-
-			% insert the pred_id into the pred name index
-		( map__search(Pred_N_Index0, Name, N_PredIdList0) ->
-			N_PredIdList = [PredId | N_PredIdList0],
-			map__det_update(Pred_N_Index0, Name, N_PredIdList,
-				Pred_N_Index)
-		;
-			N_PredIdList = [PredId],
-			map__det_insert(Pred_N_Index0, Name, N_PredIdList,
-				Pred_N_Index)
-		),
-
-			% insert it into the pred name/arity index
-		NA = Name / Arity,
-		( map__search(Pred_NA_Index0, NA, NA_PredIdList0) ->
-			NA_PredIdList = [PredId | NA_PredIdList0],
-			map__det_update(Pred_NA_Index0, NA, NA_PredIdList,	
-				Pred_NA_Index)
-		;
-			NA_PredIdList = [PredId],
-			map__det_insert(Pred_NA_Index0, NA, NA_PredIdList,	
-				Pred_NA_Index)
-		),
-
-			% insert it into the pred module:name/arity index
-		MNA = module_name_arity(Module, Name, Arity),
-		( map__search(Pred_MNA_Index0, MNA, MNA_PredIdList0) ->
-			MNA_PredIdList = [PredId | MNA_PredIdList0],
-			map__det_update(Pred_MNA_Index0, MNA, MNA_PredIdList,
-				Pred_MNA_Index)
-		;
-			MNA_PredIdList = [PredId],
-			map__det_insert(Pred_MNA_Index0, MNA, MNA_PredIdList,
-				Pred_MNA_Index)
-		),
+		predicate_table_do_insert(Module, Name, Arity, NeedQual,
+			PredId, Pred_N_Index0, Pred_N_Index, 
+			Pred_NA_Index0, Pred_NA_Index,
+			Pred_MNA_Index0, Pred_MNA_Index),
 
 		Func_N_Index = Func_N_Index0,
 		Func_NA_Index = Func_NA_Index0,
@@ -1365,41 +1381,12 @@
 	;
 		PredOrFunc = function,
 
-			% insert the pred_id into the func name index
-		( map__search(Func_N_Index0, Name, N_PredIdList0) ->
-			N_PredIdList = [PredId | N_PredIdList0],
-			map__det_update(Func_N_Index0, Name, N_PredIdList,
-				Func_N_Index)
-		;
-			N_PredIdList = [PredId],
-			map__det_insert(Func_N_Index0, Name, N_PredIdList,
-				Func_N_Index)
-		),
-
-			% insert it into the func name/arity index
 		FuncArity is Arity - 1,
-		NA = Name / FuncArity,
-		( map__search(Func_NA_Index0, NA, NA_PredIdList0) ->
-			NA_PredIdList = [PredId | NA_PredIdList0],
-			map__det_update(Func_NA_Index0, NA, NA_PredIdList,
-				Func_NA_Index)
-		;
-			NA_PredIdList = [PredId],
-			map__det_insert(Func_NA_Index0, NA, NA_PredIdList,
-				Func_NA_Index)
-		),
 
-			% insert it into the func module:name/arity index
-		MNA = module_name_arity(Module, Name, FuncArity),
-		( map__search(Func_MNA_Index0, MNA, MNA_PredIdList0) ->
-			MNA_PredIdList = [PredId | MNA_PredIdList0],
-			map__det_update(Func_MNA_Index0, MNA, MNA_PredIdList,
-				Func_MNA_Index)
-		;
-			MNA_PredIdList = [PredId],
-			map__det_insert(Func_MNA_Index0, MNA, MNA_PredIdList,
-				Func_MNA_Index)
-		),
+		predicate_table_do_insert(Module, Name, FuncArity, NeedQual,
+			PredId, Func_N_Index0, Func_N_Index, 
+			Func_NA_Index0, Func_NA_Index,
+			Func_MNA_Index0, Func_MNA_Index),
 
 		Pred_N_Index = Pred_N_Index0,
 		Pred_NA_Index = Pred_NA_Index0,
@@ -1416,6 +1403,63 @@
 				Pred_N_Index, Pred_NA_Index, Pred_MNA_Index,
 				Func_N_Index, Func_NA_Index, Func_MNA_Index).
 
+:- pred predicate_table_do_insert(module_name, string, arity, bool, pred_id,
+		name_index, name_index, name_arity_index, name_arity_index,
+		module_name_arity_index, module_name_arity_index).
+:- mode predicate_table_do_insert(in, in, in, in, in, 
+		in, out, in, out, in, out) is det.
+
+predicate_table_do_insert(Module, Name, Arity, NeedQual, PredId, 
+		N_Index0, N_Index, NA_Index0, NA_Index, 
+		MNA_Index0, MNA_Index) :-
+	( NeedQual = no ->
+			% insert the pred_id into the name index
+		( map__search(N_Index0, Name, N_PredIdList0) ->
+			N_PredIdList = [PredId | N_PredIdList0],
+			map__det_update(N_Index0, Name,
+				N_PredIdList, N_Index)
+		;
+			N_PredIdList = [PredId],
+			map__det_insert(N_Index0, Name, 
+				N_PredIdList, N_Index)
+		),
+
+			% insert it into the name/arity index
+		NA = Name / Arity,
+		( map__search(NA_Index0, NA, NA_PredIdList0) ->
+			NA_PredIdList = [PredId | NA_PredIdList0],
+			map__det_update(NA_Index0, NA,
+				NA_PredIdList, NA_Index)
+		;
+			NA_PredIdList = [PredId],
+			map__det_insert(NA_Index0, NA,
+				NA_PredIdList,	NA_Index)
+		)
+	;
+		N_Index = N_Index0,
+		NA_Index = NA_Index0
+	),
+
+		% insert it into the module:name/arity index
+	( map__search(MNA_Index0, Module - Name, MN_Arities0) ->
+		( map__search(MN_Arities0, Arity, MNA_PredIdList0) ->
+			map__det_update(MN_Arities0, Arity, 
+				[PredId | MNA_PredIdList0], MN_Arities)
+		;
+			map__det_insert(MN_Arities0, Arity, 
+				[PredId], MN_Arities)
+		),
+		map__det_update(MNA_Index0, Module - Name, MN_Arities,
+			MNA_Index)
+	;
+		map__init(MN_Arities0),
+		map__det_insert(MN_Arities0, Arity, 
+			[PredId], MN_Arities),
+		map__det_insert(MNA_Index0, Module - Name, MN_Arities,
+			MNA_Index)
+	).
+
+%-----------------------------------------------------------------------------%
 
 get_pred_id_and_proc_id(SymName, PredOrFunc, TVarSet, ArgTypes, ModuleInfo,
 			PredId, ProcId) :-
Index: compiler/hlds_out.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/hlds_out.m,v
retrieving revision 1.165
diff -u -r1.165 hlds_out.m
--- hlds_out.m	1997/06/27 04:01:25	1.165
+++ hlds_out.m	1997/06/27 07:36:35
@@ -1489,8 +1489,6 @@
 	io__write_string("abstract_imported").
 hlds_out__write_import_status(opt_imported) -->
 	io__write_string("opt_imported").
-hlds_out__write_import_status(opt_decl) -->
-	io__write_string("opt_decl").
 hlds_out__write_import_status(pseudo_imported) -->
 	io__write_string("pseudo_imported").
 
Index: compiler/hlds_pred.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/hlds_pred.m,v
retrieving revision 1.33
diff -u -r1.33 hlds_pred.m
--- hlds_pred.m	1997/06/27 04:01:26	1.33
+++ hlds_pred.m	1997/06/27 07:36:36
@@ -13,8 +13,8 @@
 
 :- interface.
 
-:- import_module hlds_data, hlds_goal, hlds_module, prog_data, instmap.
-:- import_module bool, list, map, std_util, varset.
+:- import_module hlds_data, hlds_goal, hlds_module, llds, prog_data, instmap.
+:- import_module bool, list, map, std_util, term, varset.
 
 :- implementation.
 
@@ -79,6 +79,13 @@
 :- type pred_proc_id	--->	proc(pred_id, proc_id).
 :- type pred_proc_list	==	list(pred_proc_id).
 
+	% The clauses_info structure contains the clauses for a predicate
+	% after conversion from the item_list by make_hlds.m.
+	% Typechecking is performed on the clauses info, then the clauses
+	% are copied to create the proc_info for each procedure.
+	% After mode analysis the clauses and the procedure goals are not
+	% guaranteed to be the same, and the clauses are only kept so that
+	% the optimized goal can be compared with the original in HLDS dumps.
 :- type clauses_info	--->	clauses_info(
 					varset,		% variable names
 					map(var, type), % variable types from
@@ -125,17 +132,12 @@
 	% The type `import_status' describes whether an entity (a predicate,
 	% type, inst, or mode) is local to the current module, exported from
 	% the current module, or imported from some other module.
-	% Only predicates can have status opt_decl, pseudo_exported
-	% or pseudo_imported.
+	% Only predicates can have status pseudo_exported or pseudo_imported.
 	% Only types can have status abstract_exported or abstract_imported.
 
 :- type import_status
 	--->	imported	% defined in the interface of some other module
 				% or `external' (in some other language)
-	;	opt_decl	% predicate declared in an optimization
-				% interface -  this is needed to identify 
-				% predicates that must be ignored when
-				% processing local predicates
 	;	opt_imported	% defined in the optimization 
 				% interface of another module
 	;	abstract_imported % describes a type with only an abstract
@@ -444,7 +446,7 @@
 
 pred_info_non_imported_procids(PredInfo, ProcIds) :-
 	pred_info_import_status(PredInfo, ImportStatus),
-	( ( ImportStatus = imported ; ImportStatus = opt_decl ) ->
+	( ImportStatus = imported ->
 		ProcIds = []
 	; ImportStatus = pseudo_imported ->
 		pred_info_procids(PredInfo, ProcIds0),
@@ -504,14 +506,7 @@
 				_).
 
 pred_info_is_imported(PredInfo) :-
-	pred_info_import_status(PredInfo, ImportStatus),
-	(
-		ImportStatus = imported
-	;
-		% opt_decl is equivalent to imported, except only 
-		% opt_imported preds can call an opt_decl pred.
-		ImportStatus = opt_decl
-	).
+	pred_info_import_status(PredInfo, imported).
 
 pred_info_is_pseudo_imported(PredInfo) :-
 	pred_info_import_status(PredInfo, ImportStatus),
Index: compiler/inlining.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/inlining.m,v
retrieving revision 1.65
diff -u -r1.65 inlining.m
--- inlining.m	1997/06/27 04:01:27	1.65
+++ inlining.m	1997/06/27 07:36:37
@@ -97,7 +97,7 @@
 
 :- import_module hlds_pred, hlds_goal, globals, options, llds.
 :- import_module dead_proc_elim, type_util, mode_util, goal_util.
-:- import_module passes_aux, code_aux, quantification, det_analysis.
+:- import_module passes_aux, code_aux, quantification, det_analysis, prog_data.
 
 :- import_module bool, int, list, assoc_list, map, set, std_util.
 :- import_module term, varset, require, hlds_data, dependency_graph.
Index: compiler/instmap.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/instmap.m,v
retrieving revision 1.10
diff -u -r1.10 instmap.m
--- instmap.m	1997/06/16 07:19:52	1.10
+++ instmap.m	1997/06/16 11:51:42
@@ -18,8 +18,8 @@
 :- module instmap.
 
 :- interface.
-:- import_module prog_data, mode_info.
-:- import_module set.
+:- import_module hlds_module, prog_data, mode_info.
+:- import_module set, term.
 
 :- type instmap.
 :- type instmap_delta.
Index: compiler/intermod.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/intermod.m,v
retrieving revision 1.26
diff -u -r1.26 intermod.m
--- intermod.m	1997/06/27 04:01:28	1.26
+++ intermod.m	1997/06/27 07:36:38
@@ -354,12 +354,21 @@
 	intermod_info_get_tvarset(TVarSet),
 	( { invalid_pred_id(PredId0) } ->
 		{ typecheck__resolve_pred_overloading(ModuleInfo, Args,
-			VarTypes, TVarSet, PredName0, PredName, PredId) }
+			VarTypes, TVarSet, PredName0, PredName1, PredId) }
 	;
 		{ PredId = PredId0 },
-		{ PredName = PredName0 }
+		{ PredName1 = PredName0 }
 	),	
 	(
+		{ PredName1 = qualified(_, _) },
+		{ PredName = PredName1 }
+	;
+		{ PredName1 = unqualified(Name) },
+		{ module_info_pred_info(ModuleInfo, PredId, PredInfo) },
+		{ pred_info_module(PredInfo, PredModule) },
+		{ PredName = qualified(PredModule, Name) }
+	),
+	(
 		% We don't need to export complicated unification
 		% pred declarations, since they will be recreated when 
 		% mode analysis is run on the importing module.
@@ -702,7 +711,7 @@
 	mercury_output_bracketed_constant(term__atom(ModName)),
 	io__write_string(".\n"),
 	( { Modules \= [] } ->
-		io__write_string(":- import_module "),
+		io__write_string(":- use_module "),
 		intermod__write_modules(Modules)
 	;
 		[]
@@ -1202,7 +1211,8 @@
 	{ list__sort_and_remove_dups(DirectImports0, DirectImports1) },
 	read_optimization_interfaces(DirectImports1, [],
 		OptItems, no, OptError),
-	{ get_dependencies(OptItems, NewDeps0) },
+	{ get_dependencies(OptItems, NewImportDeps0, NewUseDeps0) },
+	{ list__append(NewImportDeps0, NewUseDeps0, NewDeps0) },
 	{ set__list_to_set(NewDeps0, NewDepsSet0) },
 	{ set__delete_list(NewDepsSet0, [ModuleName | DirectImports1],
 						NewDepsSet) },
Index: compiler/llds.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/llds.m,v
retrieving revision 1.205
diff -u -r1.205 llds.m
--- llds.m	1997/05/27 03:06:28	1.205
+++ llds.m	1997/06/16 03:18:02
@@ -16,8 +16,8 @@
 
 :- interface.
 
-:- import_module tree, prog_data.
-:- import_module bool, list, set, term, std_util.
+:- import_module hlds_pred, tree, prog_data.
+:- import_module assoc_list, bool, list, set, term, std_util.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/llds_out.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/llds_out.m,v
retrieving revision 1.48
diff -u -r1.48 llds_out.m
--- llds_out.m	1997/05/28 07:39:07	1.48
+++ llds_out.m	1997/06/16 03:18:33
@@ -83,7 +83,7 @@
 :- import_module export.
 :- import_module exprn_aux, prog_data, prog_out, hlds_pred, mercury_to_mercury.
 :- import_module bool, int, list, char, string, set, std_util, term, varset.
-:- import_module require, globals, options.
+:- import_module assoc_list, require, globals, options.
 :- import_module library.	% for the version number.
 
 %-----------------------------------------------------------------------------%
Index: compiler/lookup_switch.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/lookup_switch.m,v
retrieving revision 1.22
diff -u -r1.22 lookup_switch.m
--- lookup_switch.m	1997/05/07 05:43:30	1.22
+++ lookup_switch.m	1997/06/16 03:19:01
@@ -42,7 +42,7 @@
 :- interface.
 
 :- import_module hlds_goal, hlds_data, llds, switch_gen, code_info.
-:- import_module list.
+:- import_module list, term.
 
 :- type case_consts == list(pair(int, list(rval))).
 
Index: compiler/make_hlds.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/make_hlds.m,v
retrieving revision 1.233
diff -u -r1.233 make_hlds.m
--- make_hlds.m	1997/06/27 04:01:31	1.233
+++ make_hlds.m	1997/06/27 07:36:40
@@ -23,18 +23,19 @@
 :- interface.
 
 :- import_module prog_data, hlds_module, hlds_pred, hlds_goal, hlds_data.
-:- import_module equiv_type.
+:- import_module equiv_type, module_qual.
 
 :- import_module io, std_util.
 
-% parse_tree_to_hlds(ParseTree, EqvMap, HLDS, UndefTypes, UndefModes):
-%	Given EqvMap, converts ParseTree to HLDS.
+% parse_tree_to_hlds(ParseTree, MQInfo, EqvMap, HLDS, UndefTypes, UndefModes):
+%	Given MQInfo (returned by module_qual.m) and EqvMap (returned by
+%	equiv_type.m), converts ParseTree to HLDS.
 %	Any errors found are recorded in the HLDS num_errors field.
 %	Returns UndefTypes = yes if undefined types found.
 %	Returns UndefModes = yes if undefined modes found.
-:- pred parse_tree_to_hlds(program, eqv_map, module_info, bool, bool,
+:- pred parse_tree_to_hlds(program, mq_info, eqv_map, module_info, bool, bool,
 			io__state, io__state).
-:- mode parse_tree_to_hlds(in, in, out, out, out, di, uo) is det.
+:- mode parse_tree_to_hlds(in, in, in, out, out, out, di, uo) is det.
 
 :- pred create_atomic_unification(var, unify_rhs, term__context,
 			unify_main_context, unify_sub_contexts, hlds_goal).
@@ -65,19 +66,18 @@
 :- import_module string, char, int, set, bintree, list, map, require.
 :- import_module bool, getopt, assoc_list, term, term_io, varset.
 
-parse_tree_to_hlds(module(Name, Items), EqvMap, Module, UndefTypes, UndefModes)
-		-->
+parse_tree_to_hlds(module(Name, Items), MQInfo0, EqvMap, Module, 
+		UndefTypes, UndefModes) -->
 	globals__io_get_globals(Globals),
 	{ module_info_init(Name, Globals, Module0) },
-	add_item_list_decls_pass_1(Items, local, Module0, Module1),
+	add_item_list_decls_pass_1(Items, local - no, Module0, Module1),
 	globals__io_lookup_bool_option(statistics, Statistics),
 	maybe_report_stats(Statistics),
-	add_item_list_decls_pass_2(Items, local, Module1, Module2),
+	add_item_list_decls_pass_2(Items, local - no, Module1, Module2),
 	maybe_report_stats(Statistics),
 		% balance the binary trees
 	{ module_info_optimize(Module2, Module3) },
 	maybe_report_stats(Statistics),
-	{ init_mq_info_module(Module3, MQInfo0) },
 	{ init_qual_info(MQInfo0, EqvMap, Info0) },
 	add_item_list_clauses(Items, local, Module3, Module4,
 				Info0, Info),
@@ -98,7 +98,7 @@
 	% Add the declarations one by one to the module,
 	% except for type definitions and pragmas.
 
-:- pred add_item_list_decls_pass_1(item_list, import_status,
+:- pred add_item_list_decls_pass_1(item_list, pair(import_status, bool),
 				module_info, module_info,
 				io__state, io__state).
 :- mode add_item_list_decls_pass_1(in, in, in, out, di, uo) is det.
@@ -127,7 +127,7 @@
 	% have processed all the mode declarations, since otherwise we
 	% can't be sure that there isn't a mode declaration for the function.
 
-:- pred add_item_list_decls_pass_2(item_list, import_status,
+:- pred add_item_list_decls_pass_2(item_list, pair(import_status, bool),
 		module_info, module_info, io__state, io__state).
 :- mode add_item_list_decls_pass_2(in, in, in, out, di, uo) is det.
 
@@ -156,8 +156,9 @@
 
 	% dispatch on the different types of items
 
-:- pred add_item_decl_pass_1(item, term__context, import_status, module_info,
-			import_status, module_info, io__state, io__state).
+:- pred add_item_decl_pass_1(item, term__context, pair(import_status, bool),
+		module_info, pair(import_status, bool), 
+		module_info, io__state, io__state).
 :- mode add_item_decl_pass_1(in, in, in, in, out, out, di, uo) is det.
 
 	% skip clauses
@@ -210,6 +211,9 @@
 	; { ModuleDefn = import(module(_)) } ->
 		{ Status = Status0 },
 		{ Module = Module0 }
+	; { ModuleDefn = use(module(_)) } ->
+		{ Status = Status0 },
+		{ Module = Module0 }
 	; { ModuleDefn = external(External) } ->
 		( { External = name_arity(Name, Arity) } ->
 			{ Status = Status0 },
@@ -240,8 +244,8 @@
 
 	% dispatch on the different types of items
 
-:- pred add_item_decl_pass_2(item, term__context, import_status, module_info,
-			import_status, module_info,
+:- pred add_item_decl_pass_2(item, term__context, pair(import_status, bool),
+			module_info, pair(import_status, bool), module_info,
 			io__state, io__state).
 :- mode add_item_decl_pass_2(in, in, in, in, out, out, di, uo) is det.
 
@@ -339,7 +343,8 @@
 			% This can only appear in .opt files.
 		{ Pragma = unused_args(PredOrFunc, SymName,
 				Arity, ProcId, UnusedArgs) },
-		( { Status \= opt_imported } ->
+		{ Status = ImportStatus - _ },
+		( { ImportStatus \= opt_imported } ->
 			prog_out__write_context(Context),
 			io__write_string("Error: unknown pragma unused_args.\n"),
 			{ module_info_incr_errors(Module0, Module) }
@@ -408,20 +413,21 @@
 %------------------------------------------------------------------------------
 
 	% If a module_defn updates the import_status, return the new
-	% status, otherwise fail.
+	% status and whether uses of the following items must be module 
+	% qualified, otherwise fail.
 :- pred module_defn_update_import_status(module_defn::in,
-				import_status::out) is semidet.
+		pair(import_status, bool)::out) is semidet.
 
-module_defn_update_import_status(interface, exported).
-module_defn_update_import_status(implementation, local).
-module_defn_update_import_status(imported, imported).
-module_defn_update_import_status(opt_imported, opt_imported).
+module_defn_update_import_status(interface, exported - no).
+module_defn_update_import_status(implementation, local - no).
+module_defn_update_import_status(imported(NeedQual), imported - NeedQual).
+module_defn_update_import_status(opt_imported, opt_imported - yes).
 
 %-----------------------------------------------------------------------------%
 
 	% dispatch on the different types of items
 
-:- pred add_item_clause(item, import_status, import_status, term__context,
+:- pred add_item_clause(item, import_status, import_status, term__context, 
 	module_info, module_info, qual_info, qual_info, io__state, io__state).
 :- mode add_item_clause(in, in, out, in, in, out, in, out, di, uo) is det.
 
@@ -448,10 +454,14 @@
 add_item_clause(func_mode(_, _, _, _, _, _), Status, Status, _,
 				Module, Module, Info, Info) --> [].
 add_item_clause(module_defn(_, Defn), Status0, Status, _,
-		Module, Module, Info, Info) --> 
-	{ module_defn_update_import_status(Defn, Status1) ->
+		Module, Module, Info0, Info) --> 
+	{ module_defn_update_import_status(Defn, Status1 - NeedQual) ->
+		qual_info_get_mq_info(Info0, MQInfo0),
+		mq_info_set_need_qual_flag(MQInfo0, NeedQual, MQInfo),
+		qual_info_set_mq_info(Info0, MQInfo, Info),
 		Status = Status1
 	;
+		Info = Info0,
 		Status = Status0
 	}.
 add_item_clause(pragma(Pragma), Status, Status, Context,
@@ -592,11 +602,12 @@
 %-----------------------------------------------------------------------------%
 
 :- pred module_add_inst_defn(module_info, varset, inst_defn, condition,
-		term__context, import_status, module_info, io__state, io__state).
+		term__context, pair(import_status, bool), 
+		module_info, io__state, io__state).
 :- mode module_add_inst_defn(in, in, in, in, in, in, out, di, uo) is det.
 
 module_add_inst_defn(Module0, VarSet, InstDefn, Cond,
-			Context, Status, Module) -->
+			Context, Status - _NeedQual, Module) -->
 	{ module_info_insts(Module0, InstTable0) },
 	{ inst_table_get_user_insts(InstTable0, Insts0) },
 	insts_add(Insts0, VarSet, InstDefn, Cond, Context, Status, Insts),
@@ -640,11 +651,12 @@
 %-----------------------------------------------------------------------------%
 
 :- pred module_add_mode_defn(module_info, varset, mode_defn, condition,
-	term__context, import_status, module_info, io__state, io__state).
+		term__context, pair(import_status, bool), 
+		module_info, io__state, io__state).
 :- mode module_add_mode_defn(in, in, in, in, in, in, out, di, uo) is det.
 
 module_add_mode_defn(Module0, VarSet, ModeDefn, Cond,
-		Context, Status, Module) -->
+		Context, Status - _NeedQual, Module) -->
 	{ module_info_modes(Module0, Modes0) },
 	modes_add(Modes0, VarSet, ModeDefn, Cond, Context, Status, Modes),
 	{ module_info_set_modes(Module0, Modes, Module) }.
@@ -691,11 +703,12 @@
 	% t which defines t as an abstract_type.
 
 :- pred module_add_type_defn(module_info, tvarset, type_defn, condition,
-	term__context, import_status, module_info, io__state, io__state).
+		term__context, pair(import_status, bool), 
+		module_info, io__state, io__state).
 :- mode module_add_type_defn(in, in, in, in, in, in, out, di, uo) is det.
 
-module_add_type_defn(Module0, TVarSet, TypeDefn, _Cond, Context, Status0,
-		Module) -->
+module_add_type_defn(Module0, TVarSet, TypeDefn, _Cond, Context,
+		Status0 - NeedQual, Module) -->
 	{ module_info_types(Module0, Types0) },
 	globals__io_get_globals(Globals),
 	{ convert_type_defn(TypeDefn, Globals, Name, Args, Body) },
@@ -765,7 +778,8 @@
 			{ Body = du_type(ConsList, _, _) }
 		->
 			{ module_info_ctors(Module0, Ctors0) },
-			ctors_add(ConsList, TypeId, Context, Ctors0, Ctors),
+			ctors_add(ConsList, TypeId, NeedQual, 
+				Context, Ctors0, Ctors),
 			{ module_info_set_ctors(Module0, Ctors, Module1) }
 		;
 			{ Module1 = Module0 }
@@ -887,12 +901,12 @@
 convert_type_defn(eqv_type(Name, Args, Body), _, Name, Args, eqv_type(Body)).
 convert_type_defn(abstract_type(Name, Args), _, Name, Args, abstract_type).
 
-:- pred ctors_add(list(constructor), type_id, term__context, cons_table,
+:- pred ctors_add(list(constructor), type_id, bool, term__context, cons_table,
 			cons_table, io__state, io__state).
-:- mode ctors_add(in, in, in, in, out, di, uo) is det.
+:- mode ctors_add(in, in, in, in, in, out, di, uo) is det.
 
-ctors_add([], _TypeId, _Context, Ctors, Ctors) --> [].
-ctors_add([Name - Args | Rest], TypeId, Context, Ctors0, Ctors) -->
+ctors_add([], _TypeId, _NeedQual, _Context, Ctors, Ctors) --> [].
+ctors_add([Name - Args | Rest], TypeId, NeedQual, Context, Ctors0, Ctors) -->
 	{ make_cons_id(Name, Args, TypeId, QualifiedConsId) },
 	{ assoc_list__values(Args, Types) },
 	{ ConsDefn = hlds_cons_defn(Types, TypeId, Context) },
@@ -923,7 +937,10 @@
 		{ QualifiedConsDefns = [ConsDefn | QualifiedConsDefns1] }	
 	),
 	{ map__set(Ctors0, QualifiedConsId, QualifiedConsDefns, Ctors1) },
-	{ QualifiedConsId = cons(qualified(_, ConsName), Arity) ->
+	{
+		QualifiedConsId = cons(qualified(_, ConsName), Arity),
+		NeedQual = no
+	->
 		% Add an unqualified version of the cons_id to the cons_table.
 		UnqualifiedConsId = cons(unqualified(ConsName), Arity),
 		(
@@ -939,29 +956,29 @@
 	;
 		Ctors2 = Ctors1
 	},
-	ctors_add(Rest, TypeId, Context, Ctors2, Ctors).
+	ctors_add(Rest, TypeId, NeedQual, Context, Ctors2, Ctors).
 
 %---------------------------------------------------------------------------%
 
 :- pred module_add_pred(module_info, varset, sym_name, list(type_and_mode),
-		maybe(determinism), condition, term__context, import_status,
-		module_info,
+		maybe(determinism), condition, term__context, 
+		pair(import_status, bool), module_info, 
 		io__state, io__state).
 :- mode module_add_pred(in, in, in, in, in, in, in, in, out, di, uo) is det.
 
 module_add_pred(Module0, VarSet, PredName, TypesAndModes, MaybeDet, Cond,
-		Context, Status, Module) -->
+		Context, Status - NeedQual, Module) -->
 	% Only preds with opt_imported clauses are tagged as opt_imported, so
 	% that the compiler doesn't look for clauses for other preds read in
 	% from optimization interfaces.
 	{ Status = opt_imported ->
-		DeclStatus = opt_decl 
+		DeclStatus = imported
 	;
 		DeclStatus = Status
 	},
 	{ split_types_and_modes(TypesAndModes, Types, MaybeModes) },
 	add_new_pred(Module0, VarSet, PredName, Types, Cond, Context,
-		DeclStatus, predicate, Module1),
+		DeclStatus, NeedQual, predicate, Module1),
 	(
 		{ MaybeModes = yes(Modes) }
 	->
@@ -973,12 +990,12 @@
 
 :- pred module_add_func(module_info, varset, sym_name, list(type_and_mode),
 		type_and_mode, maybe(determinism), condition, term__context,
-		import_status, module_info,
+		pair(import_status, bool), module_info,
 		io__state, io__state).
 :- mode module_add_func(in, in, in, in, in, in, in, in, in, out, di, uo) is det.
 
 module_add_func(Module0, VarSet, FuncName, TypesAndModes, RetTypeAndMode,
-		MaybeDet, Cond, Context, Status, Module) -->
+		MaybeDet, Cond, Context, Status - NeedQual, Module) -->
 	% Only funcs with opt_imported clauses are tagged as opt_imported, so
 	% that the compiler doesn't look for clauses for other preds.
 	{ Status = opt_imported ->
@@ -990,7 +1007,7 @@
 	{ split_type_and_mode(RetTypeAndMode, RetType, MaybeRetMode) },
 	{ list__append(Types, [RetType], Types1) },
 	add_new_pred(Module0, VarSet, FuncName, Types1, Cond, Context,
-		DeclStatus, function, Module1),
+		DeclStatus, NeedQual, function, Module1),
 	(
 		{ MaybeModes = yes(Modes) },
 		{ MaybeRetMode = yes(RetMode) }
@@ -1003,16 +1020,16 @@
 	).
 
 :- pred add_new_pred(module_info, tvarset, sym_name, list(type),
-		condition, term__context, import_status, pred_or_func,
+		condition, term__context, import_status, bool, pred_or_func,
 		module_info, io__state, io__state).
-:- mode add_new_pred(in, in, in, in, in, in, in, in, out, di, uo) is det.
+:- mode add_new_pred(in, in, in, in, in, in, in, in, in, out, di, uo) is det.
 
 % NB.  Predicates are also added in polymorphism.m, which converts
 % lambda expressions into separate predicates, so any changes may need
 % to be reflected there too.
 
-add_new_pred(Module0, TVarSet, PredName, Types, Cond, Context, Status,
-		PredOrFunc, Module) -->
+add_new_pred(Module0, TVarSet, PredName, Types, Cond, Context, 
+		Status, NeedQual, PredOrFunc, Module) -->
 	{ module_info_name(Module0, ModuleName) },
 	{ list__length(Types, Arity) },
 	(
@@ -1034,7 +1051,7 @@
 				PredOrFunc, MNameOfPred, PName, Arity,
 				[OrigPred|_]) }
 		->
-			( { Status \= opt_decl } ->
+			( { Status \= opt_imported } ->
 				{ module_info_incr_errors(Module1, Module) },
 				{ module_info_pred_info(Module, OrigPred,
 					OrigPredInfo) },
@@ -1050,7 +1067,7 @@
 			)
 		;
 			{ predicate_table_insert(PredicateTable0, PredInfo0, 
-					PredId, PredicateTable1) },
+				NeedQual, PredId, PredicateTable1) },
 			( 
 				{ code_util__predinfo_is_builtin(PredInfo0) }
 			->
@@ -1213,7 +1230,7 @@
 		ArgLives, yes(Det), Context, PredInfo, _),
 
 	module_info_get_predicate_table(Module0, PredicateTable0),
-	predicate_table_insert(PredicateTable0, PredInfo, PredId,
+	predicate_table_insert(PredicateTable0, PredInfo, no, PredId,
 		PredicateTable),
 	module_info_set_predicate_table(Module0, PredicateTable,
 		Module1),
@@ -1361,7 +1378,7 @@
 		\+ predicate_table_search_pf_sym_arity(PredicateTable0,
 			PredOrFunc, PredName, Arity, _)
 	->
-		predicate_table_insert(PredicateTable0, PredInfo, PredId,
+		predicate_table_insert(PredicateTable0, PredInfo, no, PredId,
 			PredicateTable)
 	;	
 		error("preds_add_implicit")
@@ -2764,7 +2781,7 @@
 	    }
 	->
 		{ qual_info_get_mq_info(Info0, MQInfo0) },
-		module_qual__qualify_mode_list(Modes1, Modes, Context,
+		module_qual__qualify_lambda_mode_list(Modes1, Modes, Context,
 						MQInfo0, MQInfo1),
 		{ qual_info_set_mq_info(Info0, MQInfo1, Info1) },
 		{ Det = Det1 },
@@ -2795,7 +2812,7 @@
 	    }
 	->
 		{ qual_info_get_mq_info(Info0, MQInfo0) },
-		module_qual__qualify_mode_list(Modes1, Modes, Context,
+		module_qual__qualify_lambda_mode_list(Modes1, Modes, Context,
 						MQInfo0, MQInfo1),
 		{ qual_info_set_mq_info(Info0, MQInfo1, Info1) },
 		{ Det = Det1 },
@@ -2930,7 +2947,8 @@
 	{ Info0 = qual_info(EqvMap, TVarSet0, TVarRenaming0, Index0,
 				VarTypes0, PredId, MQInfo0) },
 
-	module_qual__qualify_type(Type0, Type1, Context, MQInfo0, MQInfo),
+	module_qual__qualify_type_qualification(Type0, Type1, 
+		Context, MQInfo0, MQInfo),
 	{
 	% Find any new type variables introduced by this type, and
 	% add them to the var-name index and the variable renaming.
@@ -3083,7 +3101,8 @@
 :- pred init_qual_info(mq_info, eqv_map, qual_info).
 :- mode init_qual_info(in, in, out) is det.
 
-init_qual_info(MQInfo, EqvMap, QualInfo) :-
+init_qual_info(MQInfo0, EqvMap, QualInfo) :-
+	mq_info_set_need_qual_flag(MQInfo0, no, MQInfo),
 	varset__init(TVarSet),
 	map__init(Renaming),
 	map__init(Index),
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/mercury_compile.m,v
retrieving revision 1.43
diff -u -r1.43 mercury_compile.m
--- mercury_compile.m	1997/06/11 09:00:06	1.43
+++ mercury_compile.m	1997/06/19 09:47:56
@@ -257,27 +257,27 @@
 	globals__io_lookup_bool_option(statistics, Stats),
 	globals__io_lookup_bool_option(verbose, Verbose),
 
-	{ ModuleImports0 = module_imports(Module, LongDeps,
-					ShortDeps, Items0, _) },
+	{ ModuleImports0 = module_imports(Module, LongDeps, ShortDeps, _, _) },
 	write_dependency_file(Module, LongDeps, ShortDeps, FactDeps), !,
-	mercury_compile__module_qualify_items(Items0, Items1, Module, Verbose,
-					Stats, _, UndefTypes0, UndefModes0), !,
-		% Items from optimization interfaces are needed before
-		% equivalence types are expanded, but after module
-		% qualification.
-	{ ModuleImports1 = module_imports(Module, LongDeps, ShortDeps,
-							Items1, no) },
+
 		% Errors in .opt files result in software errors.
-	mercury_compile__maybe_grab_optfiles(ModuleImports1, Verbose,
-					 ModuleImports2, IntermodError), !,
-	{ ModuleImports2 = module_imports(_, _, _, Items2, _) },
+	mercury_compile__maybe_grab_optfiles(ModuleImports0, Verbose,
+				 ModuleImports1, IntermodError), !,
+
+	{ ModuleImports1 = module_imports(_, _, _, Items1, _) },
+	mercury_compile__module_qualify_items(Items1, Items2, Module, Verbose,
+				Stats, MQInfo, _, UndefTypes0, UndefModes0), !,
+
 	mercury_compile__expand_equiv_types(Items2, Verbose, Stats,
-					Items, CircularTypes, EqvMap), !,
+				Items, CircularTypes, EqvMap), !,
 	{ bool__or(UndefTypes0, CircularTypes, UndefTypes1) },
-	mercury_compile__make_hlds(Module, Items, EqvMap, Verbose, Stats,
-				HLDS0, UndefTypes2, UndefModes2, FoundError), !,
+
+	mercury_compile__make_hlds(Module, Items, MQInfo, EqvMap, Verbose, 
+			Stats, HLDS0, UndefTypes2, UndefModes2, FoundError), !,
+
 	{ bool__or(UndefTypes1, UndefTypes2, UndefTypes) },
 	{ bool__or(UndefModes0, UndefModes2, UndefModes) },
+
 	mercury_compile__maybe_dump_hlds(HLDS0, "1", "initial"), !,
 
 	% Only stop on syntax errors in .opt files.
@@ -288,16 +288,16 @@
 	).
 
 :- pred mercury_compile__module_qualify_items(item_list, item_list, string,
-		bool, bool, int, bool, bool, io__state, io__state).
+		bool, bool, mq_info, int, bool, bool, io__state, io__state).
 :- mode mercury_compile__module_qualify_items(in, out, in, in, in, out, out,
-			out, di, uo) is det. 
+			out, out, di, uo) is det. 
 
-mercury_compile__module_qualify_items(Items0, Items, ModuleName, Verbose, Stats,
-			NumErrors, UndefTypes, UndefModes) -->
+mercury_compile__module_qualify_items(Items0, Items, ModuleName, Verbose, 
+			Stats, MQInfo, NumErrors, UndefTypes, UndefModes) -->
 	maybe_write_string(Verbose, "% Module qualifying items...\n"),
 	maybe_flush_output(Verbose),
 	module_qual__module_qualify_items(Items0, Items, ModuleName, yes,
-				NumErrors, UndefTypes, UndefModes),
+				MQInfo, NumErrors, UndefTypes, UndefModes),
 	maybe_write_string(Verbose, "% done.\n"),
 	maybe_report_stats(Stats).
 
@@ -332,16 +332,16 @@
 	maybe_write_string(Verbose, " done.\n"),
 	maybe_report_stats(Stats).
 
-:- pred mercury_compile__make_hlds(module_name, item_list, eqv_map, bool, bool,
-	module_info, bool, bool, bool, io__state, io__state).
-:- mode mercury_compile__make_hlds(in, in, in, in, in,
+:- pred mercury_compile__make_hlds(module_name, item_list, mq_info, eqv_map, 
+	bool, bool, module_info, bool, bool, bool, io__state, io__state).
+:- mode mercury_compile__make_hlds(in, in, in, in, in, in,
 	out, out, out, out, di, uo) is det.
 
-mercury_compile__make_hlds(Module, Items, EqvMap, Verbose, Stats,
+mercury_compile__make_hlds(Module, Items, MQInfo, EqvMap, Verbose, Stats,
 		HLDS, UndefTypes, UndefModes, FoundSemanticError) -->
 	maybe_write_string(Verbose, "% Converting parse tree to hlds...\n"),
 	{ Prog = module(Module, Items) },
-	parse_tree_to_hlds(Prog, EqvMap, HLDS, UndefTypes, UndefModes),
+	parse_tree_to_hlds(Prog, MQInfo, EqvMap, HLDS, UndefTypes, UndefModes),
 	{ module_info_num_errors(HLDS, NumErrors) },
 	( { NumErrors > 0 } ->
 		{ FoundSemanticError = yes },
Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.107
diff -u -r1.107 mercury_to_mercury.m
--- mercury_to_mercury.m	1997/06/27 04:01:33	1.107
+++ mercury_to_mercury.m	1997/06/27 07:36:41
@@ -350,6 +350,10 @@
 		io__write_string(":- import_module "),
 		mercury_write_module_spec_list(ImportedModules),
 		io__write_string(".\n")
+	; { Module = use(module(UsedModules)) } ->
+		io__write_string(":- use_module "),
+		mercury_write_module_spec_list(UsedModules),
+		io__write_string(".\n")
 	; { Module = interface } ->
 		io__write_string(":- interface.\n")
 	; { Module = implementation } ->
Index: compiler/middle_rec.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/middle_rec.m,v
retrieving revision 1.62
diff -u -r1.62 middle_rec.m
--- middle_rec.m	1997/04/04 04:41:13	1.62
+++ middle_rec.m	1997/06/16 03:19:31
@@ -28,7 +28,7 @@
 :- import_module hlds_module, hlds_data.
 :- import_module code_gen, unify_gen, code_util, code_aux, opt_util.
 :- import_module bool, set, int, std_util, tree, list, assoc_list, require.
-:- import_module string.
+:- import_module string, term.
 
 %---------------------------------------------------------------------------%
 
Index: compiler/mode_debug.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/mode_debug.m,v
retrieving revision 1.6
diff -u -r1.6 mode_debug.m
--- mode_debug.m	1997/05/21 02:13:37	1.6
+++ mode_debug.m	1997/06/16 03:20:24
@@ -34,8 +34,9 @@
 
 :- implementation.
 :- import_module globals, std_util, list, assoc_list, io, bool, map.
+:- import_module term, varset.
 :- import_module modes, options, mercury_to_mercury, passes_aux.
-:- import_module hlds_goal, instmap.
+:- import_module hlds_goal, instmap, prog_data.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/mode_errors.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/mode_errors.m,v
retrieving revision 1.44
diff -u -r1.44 mode_errors.m
--- mode_errors.m	1997/04/23 01:02:37	1.44
+++ mode_errors.m	1997/06/16 03:20:44
@@ -17,7 +17,7 @@
 
 :- interface.
 
-:- import_module prog_data, mode_info.
+:- import_module hlds_data, prog_data, mode_info.
 :- import_module set, assoc_list.
 
 %-----------------------------------------------------------------------------%
Index: compiler/mode_info.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/mode_info.m,v
retrieving revision 1.36
diff -u -r1.36 mode_info.m
--- mode_info.m	1997/04/03 01:17:56	1.36
+++ mode_info.m	1997/06/16 03:21:31
@@ -18,7 +18,7 @@
 
 :- import_module hlds_module, hlds_pred, hlds_goal, hlds_data, instmap.
 :- import_module mode_errors, delay_info.
-:- import_module map, list, varset, set, bool.
+:- import_module map, list, varset, set, bool, term, assoc_list.
 
 :- interface.
 
Index: compiler/modecheck_call.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/modecheck_call.m,v
retrieving revision 1.10
diff -u -r1.10 modecheck_call.m
--- modecheck_call.m	1997/05/21 02:13:38	1.10
+++ modecheck_call.m	1997/06/16 05:16:23
@@ -22,6 +22,7 @@
 :- interface.
 
 :- import_module hlds_goal, mode_info.
+:- import_module term.
 
 :- pred modecheck_call_pred(pred_id, list(var), proc_id, list(var),
 				pair(list(hlds_goal)), mode_info, mode_info).
Index: compiler/modecheck_unify.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/modecheck_unify.m,v
retrieving revision 1.15
diff -u -r1.15 modecheck_unify.m
--- modecheck_unify.m	1997/05/30 17:16:47	1.15
+++ modecheck_unify.m	1997/06/16 05:19:24
@@ -21,6 +21,7 @@
 :- interface.
 
 :- import_module hlds_goal, mode_info, modes.
+:- import_module term.
 
 	% Modecheck a unification
 :- pred modecheck_unification( var, unify_rhs, unification, unify_context,
Index: compiler/module_qual.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/module_qual.m,v
retrieving revision 1.18
diff -u -r1.18 module_qual.m
--- module_qual.m	1997/06/27 04:01:35	1.18
+++ module_qual.m	1997/06/27 07:36:42
@@ -32,28 +32,32 @@
 	% ReportUndefErrors should be no when module qualifying the
 	% short interface.
 :- pred module_qual__module_qualify_items(item_list, item_list,
-		string, bool, int, bool, bool, io__state, io__state).
+		string, bool, mq_info, int, bool, bool, io__state, io__state).
 :- mode module_qual__module_qualify_items(in, out, in, in,
-		out, out, out, di, uo) is det.
+		out, out, out, out, di, uo) is det.
 
 	% This is called from make_hlds.m to qualify the mode of a lambda
 	% expression.
-:- pred module_qual__qualify_mode_list(list(mode), list(mode),
+:- pred module_qual__qualify_lambda_mode_list(list(mode), list(mode),
 		term__context, mq_info, mq_info,
 		io__state, io__state) is det.
-:- mode module_qual__qualify_mode_list(in, out, in, in, out, di, uo) is det.
+:- mode module_qual__qualify_lambda_mode_list(in, out, 
+		in, in, out, di, uo) is det.
 
-:- pred module_qual__qualify_type(type, type, term__context,
+	% This is called from make_hlds.m to qualify an 
+	% explicit type qualification.
+:- pred module_qual__qualify_type_qualification(type, type, term__context,
 		mq_info, mq_info, io__state, io__state).
-:- mode module_qual__qualify_type(in, out, in, in, out, di, uo) is det.
+:- mode module_qual__qualify_type_qualification(in, out, in, in,
+		out, di, uo) is det.
 
 :- type mq_info.
 
-:- pred init_mq_info_module(module_info::in, mq_info::out) is det.
-
 :- pred mq_info_get_num_errors(mq_info::in, int::out) is det.
 :- pred mq_info_get_type_error_flag(mq_info::in, bool::out) is det.
 :- pred mq_info_get_mode_error_flag(mq_info::in, bool::out) is det.
+:- pred mq_info_set_need_qual_flag(mq_info::in, bool::in, mq_info::out) is det.
+:- pred mq_info_get_need_qual_flag(mq_info::in, bool::out) is det.
 
 %-----------------------------------------------------------------------------%
 :- implementation.
@@ -63,7 +67,7 @@
 :- import_module int, list, map, require, set, std_util, string, term, varset.
 
 module_qual__module_qualify_items(Items0, Items, ModuleName, ReportErrors,
-			NumErrors, UndefTypes, UndefModes) -->
+			Info, NumErrors, UndefTypes, UndefModes) -->
 	{ init_mq_info(ReportErrors, Info0) },
 	{ collect_mq_info(Items0, Info0, Info1) },
 	do_module_qualify_items(Items0, Items, Info1, Info),
@@ -78,11 +82,11 @@
 	),
 	{ mq_info_get_num_errors(Info, NumErrors) }.
 
-module_qual__qualify_mode_list(Modes0, Modes, Context, Info0, Info) -->
+module_qual__qualify_lambda_mode_list(Modes0, Modes, Context, Info0, Info) -->
 	{ mq_info_set_error_context(Info0, lambda_expr - Context, Info1) },
 	qualify_mode_list(Modes0, Modes, Info1, Info).
 
-module_qual__qualify_type(Type0, Type, Context, Info0, Info) -->
+module_qual__qualify_type_qualification(Type0, Type, Context, Info0, Info) -->
 	{ mq_info_set_error_context(Info0, type_qual - Context, Info1) },
 	qualify_type(Type0, Type, Info1, Info).
 
@@ -100,7 +104,9 @@
 			bool,	% are there any undefined insts or modes.
 			bool, 	% do we want to report errors.
 			error_context,	% context of the current item.
-			unit	% junk slot
+			bool	% is the current item from a module imported
+				% using a use_module declaration or from an 
+				% optimization interface.
 	).
 		
 	% Pass over the item list collecting all defined type, mode and
@@ -144,7 +150,8 @@
 	),
 	list__length(Params, Arity),
 	mq_info_get_types(Info0, Types0),
-	id_set_insert(SymName - Arity, Types0, Types),
+	mq_info_get_need_qual_flag(Info0, NeedQualifier),
+	id_set_insert(NeedQualifier, SymName - Arity, Types0, Types),
 	mq_info_set_types(Info0, Types, Info).
 
 :- pred add_inst_defn(inst_defn::in, mq_info::in, mq_info::out) is det.
@@ -155,7 +162,8 @@
 	),
 	list__length(Params, Arity),
 	mq_info_get_insts(Info0, Insts0),
-	id_set_insert(SymName - Arity, Insts0, Insts),
+	mq_info_get_need_qual_flag(Info0, NeedQualifier),
+	id_set_insert(NeedQualifier, SymName - Arity, Insts0, Insts),
 	mq_info_set_insts(Info0, Insts, Info).
 
 :- pred add_mode_defn(mode_defn::in, mq_info::in, mq_info::out) is det.
@@ -163,7 +171,8 @@
 add_mode_defn(eqv_mode(SymName, Params, _), Info0, Info) :-
 	list__length(Params, Arity),
 	mq_info_get_modes(Info0, Modes0),
-	id_set_insert(SymName - Arity, Modes0, Modes),
+	mq_info_get_need_qual_flag(Info0, NeedQualifier),
+	id_set_insert(NeedQualifier, SymName - Arity, Modes0, Modes),
 	mq_info_set_modes(Info0, Modes, Info).
 
 	% Update import status.
@@ -175,25 +184,29 @@
 	mq_info_set_import_status(Info0, exported, Info).
 process_module_defn(implementation, Info0, Info) :-
 	mq_info_set_import_status(Info0, local, Info).
-process_module_defn(imported, Info0, Info) :-
-	mq_info_set_import_status(Info0, imported, Info).
+process_module_defn(imported(NeedQualifier), Info0, Info) :-
+	mq_info_set_import_status(Info0, imported, Info1),
+	mq_info_set_need_qual_flag(Info1, NeedQualifier, Info).
 process_module_defn(opt_imported, Info0, Info) :-
-	mq_info_set_import_status(Info0, opt_imported, Info).
+	mq_info_set_import_status(Info0, opt_imported, Info1),
+	mq_info_set_need_qual_flag(Info1, yes, Info).
 process_module_defn(external(_), Info, Info).
 process_module_defn(end_module(_), Info, Info).
 process_module_defn(export(_), Info, Info).
 process_module_defn(import(Imports), Info0, Info) :-
+	add_interface_imports(Imports, Info0, Info).
+process_module_defn(use(Imports), Info0, Info) :-
+	add_interface_imports(Imports, Info0, Info).
+
+:- pred add_interface_imports(sym_list::in,
+		mq_info::in, mq_info::out) is det.
+
+add_interface_imports(Imports, Info0, Info) :-
 	( Imports = module(ImportedModules) ->
-		( mq_info_get_import_status(Info0, exported) ->
-			mq_info_add_interface_modules(Info0,
-						ImportedModules, Info)
-		;
-			Info = Info0
-		)
+		mq_info_add_interface_modules(Info0, ImportedModules, Info)
 	;
 		Info = Info0
 	).
-process_module_defn(use(_), Info, Info).	% not implemented 
 
 %------------------------------------------------------------------------------
 
@@ -285,15 +298,13 @@
 :- pred update_import_status(module_defn::in, mq_info::in, mq_info::out,
 							bool::out) is det.
 
-update_import_status(opt_imported, Info0, Info, no) :-
-	mq_info_set_import_status(Info0, opt_imported, Info).
+update_import_status(opt_imported, Info, Info, no).
 update_import_status(module(_), Info, Info, yes).
 update_import_status(interface, Info0, Info, yes) :-
 	mq_info_set_import_status(Info0, exported, Info).
 update_import_status(implementation, Info0, Info, yes) :-
 	mq_info_set_import_status(Info0, local, Info).
-update_import_status(imported, Info0, Info, no) :-
-	mq_info_set_import_status(Info0, imported, Info).
+update_import_status(imported(_), Info, Info, no).
 update_import_status(external(_), Info, Info, yes).
 update_import_status(end_module(_), Info, Info, yes).
 update_import_status(export(_), Info, Info, yes).
@@ -606,11 +617,7 @@
 		{ Id0 = qualified(Module, Name) - Arity },
 		{ Id = Id0 },
 		( { id_set_search_m_n_a(Ids, Module, Name, Arity) } ->
-			( { mq_info_get_import_status(Info0, exported) } ->
-				{ mq_info_set_module_used(Info0, Module, Info) }
-			;
-				{ Info = Info0 }
-			)
+			{ mq_info_set_module_used(Info0, Module, Info) }
 		;
 			( { mq_info_get_report_error_flag(Info0, yes) } ->
 				{ mq_info_get_error_context(Info0,
@@ -647,12 +654,7 @@
 		; { Modules = [Module] } ->
 			% A unique match for this ID.
 			{ Id = qualified(Module, IdName) - Arity },
-			( { mq_info_get_import_status(Info0, exported) } ->
-				{ mq_info_set_module_used(Info0, Module,
-								Info) }
-			;
-				{ Info = Info0 }
-			)
+			{ mq_info_set_module_used(Info0, Module, Info) }
 		;
 			% There are multiple matches.
 			{ Id = Id0 },
@@ -734,7 +736,7 @@
 	io__write_string("  The possible matches are in modules\n"),
 	prog_out__write_context(Context),
 	io__write_string("  "),
-	write_module_list(Modules),
+	prog_out__write_module_list(Modules),
 	io__write_string(".\n"),
 	globals__io_lookup_bool_option(verbose_errors, Verbose),
 	( { Verbose = yes } ->
@@ -814,7 +816,7 @@
 		;
 			io__write_string("modules ")
 		),
-		write_module_list(UnusedImports),
+		prog_out__write_module_list(UnusedImports),
 		io__write_string("\n"),
 		prog_out__write_context(Context),
 		{ is_or_are(UnusedImports, IsOrAre) },
@@ -833,19 +835,6 @@
 is_or_are([_], "is").
 is_or_are([_, _ | _], "are").
 
-:- pred write_module_list(list(module_name)::in, io__state::di,
-					io__state::uo) is det.
-
-write_module_list([Import1, Import2, Import3 | Imports]) --> 
-	io__write_strings(["`", Import1, "', "]),
-	write_module_list([Import2, Import3 | Imports]).
-write_module_list([Import1, Import2]) -->
-	io__write_strings(["`", Import1, "' and `", Import2 ,"'"]).
-write_module_list([Import]) -->
-	io__write_strings(["`", Import, "'"]).
-write_module_list([]) -->
-	{ error("module_qual:write_module_list") }.
-
 	% Output an error message about an ill-formed type.
 :- pred report_invalid_type(term, error_context, io__state, io__state).
 :- mode report_invalid_type(in, in, di, uo) is det.
@@ -906,21 +895,7 @@
 	set__init(InterfaceModules0),
 	id_set_init(Empty),
 	Info0 = mq_info(Empty, Empty, Empty, InterfaceModules0,
-			local, 0, no, no, ReportErrors, ErrorContext, unit).
-
-init_mq_info_module(ModuleInfo, Info0) :-
-	module_info_typeids(ModuleInfo, TypeIds),
-	id_set_init(Empty),
-	list__foldl(id_set_insert, TypeIds, Empty, Types),
-	module_info_instids(ModuleInfo, InstIds),
-	list__foldl(id_set_insert, InstIds, Empty, Insts),
-	module_info_modeids(ModuleInfo, ModeIds),
-	list__foldl(id_set_insert, ModeIds, Empty, Modes),
-	term__context_init(Context),
-	ErrorContext = type(unqualified("") - 0) - Context,
-	set__init(InterfaceModules0),
-	Info0 = mq_info(Types, Insts, Modes, InterfaceModules0,
-		local, 0, no, no, yes, ErrorContext, unit).
+		local, 0, no, no, ReportErrors, ErrorContext, no).
 
 :- pred mq_info_get_types(mq_info::in, type_id_set::out) is det.
 :- pred mq_info_get_insts(mq_info::in, inst_id_set::out) is det.
@@ -933,7 +908,6 @@
 % :- pred mq_info_get_mode_error_flag(mq_info::in, bool::out) is det.
 :- pred mq_info_get_report_error_flag(mq_info::in, bool::out) is det.
 :- pred mq_info_get_error_context(mq_info::in, error_context::out) is det.
-:- pred mq_info_get_junk(mq_info::in, unit::out) is det.
 
 mq_info_get_types(mq_info(Types, _,_,_,_,_,_,_,_,_,_), Types).
 mq_info_get_insts(mq_info(_, Insts, _,_,_,_,_,_,_,_,_), Insts).
@@ -946,18 +920,19 @@
 						ModeError).
 mq_info_get_report_error_flag(mq_info(_,_,_,_,_,_,_,_, Report,_,_), Report).
 mq_info_get_error_context(mq_info(_,_,_,_,_,_,_,_,_, Context,_), Context).
-mq_info_get_junk(mq_info(_,_,_,_,_,_,_,_,_,_,Junk), Junk).
+mq_info_get_need_qual_flag(mq_info(_,_,_,_,_,_,_,_,_,_,UseModule), UseModule).
 
 :- pred mq_info_set_types(mq_info::in, type_id_set::in, mq_info::out) is det.
 :- pred mq_info_set_insts(mq_info::in, inst_id_set::in, mq_info::out) is det.
 :- pred mq_info_set_modes(mq_info::in, mode_id_set::in, mq_info::out) is det.
+:- pred mq_info_set_interface_modules(mq_info::in, set(module_name)::in,
+						mq_info::out) is det.
 :- pred mq_info_set_import_status(mq_info::in, import_status::in,
 						mq_info::out) is det.
 :- pred mq_info_set_type_error_flag(mq_info::in, mq_info::out) is det.
 :- pred mq_info_set_mode_error_flag(mq_info::in, mq_info::out) is det.
 :- pred mq_info_set_error_context(mq_info::in, error_context::in,
 						mq_info::out) is det.
-:- pred mq_info_set_junk(mq_info::in, unit::in, mq_info::out) is det.
 
 mq_info_set_types(mq_info(_, B,C,D,E,F,G,H,I,J,K), Types,
 		mq_info(Types, B,C,D,E,F,G,H,I,J,K)).
@@ -965,6 +940,8 @@
 		mq_info(A, Insts, C,D,E,F,G,H,I,J,K)).
 mq_info_set_modes(mq_info(A,B,_,D,E,F,G,H,I,J,K), Modes,
 		mq_info(A,B, Modes, D,E,F,G,H,I,J,K)).
+mq_info_set_interface_modules(mq_info(A,B,C,_,E,F,G,H,I,J,K), Modules,
+		mq_info(A,B,C, Modules, E,F,G,H,I,J,K)).
 mq_info_set_import_status(mq_info(A,B,C,D,_,F,G,H,I,J,K), Status,
 		mq_info(A,B,C,D, Status, F,G,H,I,J,K)).
 mq_info_set_type_error_flag(mq_info(A,B,C,D,E,F, _, H,I,J,K),
@@ -973,8 +950,8 @@
 		mq_info(A,B,C,D,E,F,G, yes, I,J,K)).
 mq_info_set_error_context(mq_info(A,B,C,D,E,F,G,H,I,_,K), Context,
 		mq_info(A,B,C,D,E,F,G,H,I, Context,K)).
-mq_info_set_junk(mq_info(A,B,C,D,E,F,G,H,I,J,_), Junk,
-		mq_info(A,B,C,D,E,F,G,H,I,J, Junk)).
+mq_info_set_need_qual_flag(mq_info(A,B,C,D,E,F,G,H,I,J,_), Flag,
+		mq_info(A,B,C,D,E,F,G,H,I,J, Flag)).
 
 :- pred mq_info_incr_errors(mq_info::in, mq_info::out) is det.
 
@@ -991,24 +968,44 @@
 mq_info_set_error_flag(Info0, inst_id, Info) :-
 	mq_info_set_mode_error_flag(Info0, Info).
 
+	% If the current item is in the interface, remove the its module 
+	% name from the list of modules not used in the interface.
 :- pred mq_info_set_module_used(mq_info::in, module_name::in,
 						mq_info::out) is det.
+
+mq_info_set_module_used(Info0, Module, Info) :-
+	( mq_info_get_import_status(Info0, exported) ->
+		mq_info_get_interface_modules(Info0, Modules0),
+		set__delete(Modules0, Module, Modules),
+		mq_info_set_interface_modules(Info0, Modules, Info)
+	;
+		Info = Info0
+	).
+
+	% Add to the list of modules imported in the interface.
 :- pred mq_info_add_interface_modules(mq_info::in, list(module_name)::in,
 						mq_info::out) is det.
 
-mq_info_set_module_used(mq_info(A,B,C,Modules0,E,F,G,H,I,J,K), Module,
-		mq_info(A,B,C, Modules, E,F,G,H,I,J,K)) :-
-	set__delete(Modules0, Module, Modules).
-mq_info_add_interface_modules(mq_info(A,B,C,Modules0, E,F,G,H,I,J,K),
-		NewModules, mq_info(A,B,C, Modules, E,F,G,H,I,J,K)) :-
-	set__insert_list(Modules0, NewModules, Modules).
+mq_info_add_interface_modules(Info0, NewModules, Info) :-
+	( mq_info_get_import_status(Info0, exported) ->
+		mq_info_get_interface_modules(Info0, Modules0),
+		set__insert_list(Modules0, NewModules, Modules),
+		mq_info_set_interface_modules(Info0, Modules, Info)
+	;
+		Info = Info0
+	).
 
 %----------------------------------------------------------------------------%
 % Define a type for representing sets of ids during module qualification
 % to allow efficient retrieval of all the modules which define an id
 % with a certain name and arity.
 
-:- type id_set == map(pair(string, arity), set(module_name)).
+% The first set of module_names can be used without module qualifiers,
+% items from the second set can only be used with module qualifiers.
+% Items from modules imported with a :- use_module declaration and from `.opt'
+% files should go into the second set.
+:- type id_set == map(pair(string, arity), pair(set(module_name))).
+
 :- type type_id_set == id_set.
 :- type mode_id_set == id_set.
 :- type inst_id_set == id_set.
@@ -1020,25 +1017,35 @@
 
 	% Insert an id into an id_set, aborting with an error if the
 	% id is not module qualified.
-:- pred id_set_insert(id::in, id_set::in, id_set::out) is det.
+:- pred id_set_insert(bool::in, id::in, id_set::in, id_set::out) is det.
 
-id_set_insert(unqualified(_) - _, _, _) :-
+id_set_insert(_, unqualified(_) - _, _, _) :-
 	error("module_qual:id_set_insert - unqualified id").
-id_set_insert(qualified(Module, Name) - Arity, IdSet0, IdSet) :-
-	( map__search(IdSet0, Name - Arity, Modules0) ->
-		Modules1 = Modules0 
+id_set_insert(NeedQualifier, qualified(Module, Name) - Arity, IdSet0, IdSet) :-
+	( map__search(IdSet0, Name - Arity, ImportModules0 - UseModules0) ->
+		ImportModules1 = ImportModules0,
+		UseModules1 = UseModules0
+	;
+		set__init(ImportModules1),
+		set__init(UseModules1)
+	),
+	(
+		NeedQualifier = yes,
+		set__insert(UseModules1, Module, UseModules),
+		ImportModules = ImportModules1
 	;
-		set__init(Modules1)
+		NeedQualifier = no,
+		set__insert(ImportModules1, Module, ImportModules),
+		UseModules = UseModules1
 	),
-	set__insert(Modules1, Module, Modules),
-	map__set(IdSet0, Name - Arity, Modules, IdSet).
+	map__set(IdSet0, Name - Arity, ImportModules - UseModules, IdSet).
 
 :- pred id_set_search_name_arity(id_set::in, string::in, int::in,
 				list(module_name)::out) is det.
 
 id_set_search_name_arity(IdSet0, Name, Arity, Modules) :-
-	( map__search(IdSet0, Name - Arity, ModuleSet) ->
-		set__to_sorted_list(ModuleSet, Modules)
+	( map__search(IdSet0, Name - Arity, ImportModules - _) ->
+		set__to_sorted_list(ImportModules, Modules)
 	;
 		Modules = []
 	).
@@ -1047,7 +1054,11 @@
 			 	string::in, int::in) is semidet.
 
 id_set_search_m_n_a(IdSet0, Module, Name, Arity) :-
-	map__search(IdSet0, Name - Arity, ModuleSet),
-	set__member(Module, ModuleSet).
+	map__search(IdSet0, Name - Arity, ImportModules - UseModules),
+	( 
+		set__member(Module, ImportModules)
+	;
+		set__member(Module, UseModules)
+	).
 
 %----------------------------------------------------------------------------%
Index: compiler/modules.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/modules.m,v
retrieving revision 1.34
diff -u -r1.34 modules.m
--- modules.m	1997/06/03 07:00:46	1.34
+++ modules.m	1997/06/14 06:43:21
@@ -107,12 +107,14 @@
 :- pred generate_dependencies(string, io__state, io__state).
 :- mode generate_dependencies(in, di, uo) is det.
 
-	% Given a module (well, a list of items),
-	% determine all the modules that it depends upon
-	% (both interface dependencies and also implementation dependencies).
-
-:- pred get_dependencies(item_list, list(string)).
-:- mode get_dependencies(in, out) is det.
+	% get_dependencies(Items, ImportDeps, UseDeps).
+	%	Get the list of modules that a list of items depends on.
+	%	ImportDeps is the list of modules imported using
+	% 	`:- import_module', UseDeps is the list of modules imported
+	%	using `:- use_module'.
+	%
+:- pred get_dependencies(item_list, list(string), list(string)).
+:- mode get_dependencies(in, out, out) is det.
 
 	% Do the importing of the interface files of imported modules.
 
@@ -155,30 +157,42 @@
 	{ get_interface(Items0, yes, InterfaceItems0) },
 	{ term__context_init(Context) },
 	{ varset__init(Varset) },
-	{ get_dependencies(InterfaceItems0, InterfaceDeps) },
+	{ get_dependencies(InterfaceItems0, 
+		InterfaceImportDeps, InterfaceUseDeps) },
 	{ list__append(InterfaceItems0,
-			[module_defn(Varset, imported) - Context],
-			InterfaceItems0a) },
-	{ Module0 = module_imports(ModuleName, [], [], InterfaceItems0a, no) },
+			[module_defn(Varset, imported(no)) - Context],
+			InterfaceItems1) },
+	{ Module1 = module_imports(ModuleName, [], [], InterfaceItems1, no) },
 		% Get the .int3s that the current .int depends on.
-	process_module_short_interfaces(["mercury_builtin" | InterfaceDeps],
-					".int3", Module0, Module),
-	{ Module = module_imports(_, _, _, InterfaceItems1, Error) },
+	process_module_short_interfaces(
+			["mercury_builtin" | InterfaceImportDeps],
+			".int3", Module1, Module2),
+	{ Module2 = module_imports(_, Direct2, Indirect2,
+			InterfaceItems2, Error2) },
+	{ list__append(InterfaceItems2,
+			[module_defn(Varset, imported(yes)) - Context],
+			InterfaceItems3) },
+	{ Module3 = module_imports(ModuleName, Direct2, Indirect2,
+			InterfaceItems3, Error2) },
+	process_module_short_interfaces(InterfaceUseDeps,
+			".int3", Module3, Module4),
+
+	{ Module4 = module_imports(_, _, _, InterfaceItems4, Error) },
 	( { Error = yes } ->
 		io__write_strings(["Error reading short interface files.\n",
 				ModuleName, ".int and ",
 				ModuleName, ".int2 not written.\n"])
 	;
 			% Qualify all items.
-		module_qual__module_qualify_items(InterfaceItems1,
-				InterfaceItems2, ModuleName, yes, _, _, _),
+		module_qual__module_qualify_items(InterfaceItems4,
+				InterfaceItems5, ModuleName, yes, _, _, _, _),
 		io__get_exit_status(Status),
 		( { Status \= 0 } ->
 			io__write_strings([ModuleName, ".int not written.\n"])
 		;
-			{ strip_imported_items(InterfaceItems2, [],
-							InterfaceItems3) },
-			check_for_clauses_in_interface(InterfaceItems3,
+			{ strip_imported_items(InterfaceItems5, [],
+							InterfaceItems6) },
+			check_for_clauses_in_interface(InterfaceItems6,
 							InterfaceItems),
 			check_for_no_exports(InterfaceItems, ModuleName),
 			write_interface_file(ModuleName, ".int",
@@ -198,7 +212,7 @@
 	check_for_clauses_in_interface(InterfaceItems0, InterfaceItems),
 	{ get_short_interface(InterfaceItems, ShortInterfaceItems0) },
 	module_qual__module_qualify_items(ShortInterfaceItems0,
-			ShortInterfaceItems, ModuleName, no, _, _, _),
+			ShortInterfaceItems, ModuleName, no, _, _, _, _),
 	write_interface_file(ModuleName, ".int3", ShortInterfaceItems),
 	touch_interface_datestamp(ModuleName, ".date3").
 
@@ -209,9 +223,7 @@
 strip_imported_items([], Items0, Items) :-
 	list__reverse(Items0, Items). 
 strip_imported_items([Item - Context | Rest], Items0, Items) :-
-	(
-		Item = module_defn(_, imported)
-	->
+	( Item = module_defn(_, imported(_)) ->
 		list__reverse(Items0, Items)
 	;
 		strip_imported_items(Rest, [Item - Context | Items0], Items)
@@ -364,20 +376,66 @@
 %-----------------------------------------------------------------------------%
 
 grab_imported_modules(ModuleName, Items0, Module, FactDeps, Error) -->
-	{ get_dependencies(Items0, ImportedModules) },
+	{ get_dependencies(Items0, ImportedModules0, UsedModules) },
+
+	{ set__list_to_set(ImportedModules0, ImportedSet) },
+	{ set__list_to_set(UsedModules, UsedSet) },
+
+	{ set__intersect(ImportedSet, UsedSet, BothSet) },
+
+		% Report errors for modules imported using both :- use_module
+		% and :- import_module. Remove the import_module declaration.
+	{ string__append(ModuleName, ".m", FileName) },
+	{ term__context_init(FileName, 0, Context) },
+	( { set__empty(BothSet) } ->
+		{ ImportedModules = ImportedModules0 }
+	;
+		prog_out__write_context(Context),
+		io__write_string("Error: "),
+		{ set__to_sorted_list(BothSet, BothList) },
+		( { BothList = [_] } ->
+			io__write_string(" module "),
+			prog_out__write_module_list(BothList),
+			io__write_string(" is ")
+		;
+			io__write_string(" modules "),
+			prog_out__write_module_list(BothList),
+			io__write_string(" are ")
+		),
+		io__write_string("imported using both\n"),
+		prog_out__write_context(Context),
+		io__write_string("  `:- import_module' and `:- use_module' declarations.\n"),
+
+		% Treat the modules with both types of import as if they 
+		% were imported using :- use_module.
+		{ list__delete_elems(ImportedModules0, BothList,
+			ImportedModules ) }
+	),
+
 	{ get_fact_table_dependencies(Items0, FactDeps) },
 
-		% we add a pseudo-declaration `:- imported' at the end
-		% of the item list, so that make_hlds knows which items
-		% are imported and which are defined in the main module
+		% we add a pseudo-declarations `:- imported(no)' at the end
+		% of the item list. Uses of the items with declarations 
+		% following this do not need module qualifiers.
 	{ varset__init(VarSet) },
-	{ term__context_init(ModuleName, 0, Context) },
 	{ list__append(Items0,
-		[module_defn(VarSet, imported) - Context], Items1) },
+		[module_defn(VarSet, imported(no)) - Context], Items1) },
 	{ dir__basename(ModuleName, BaseModuleName) },
-	{ Module0 = module_imports(BaseModuleName, [], [], Items1, no) },
+	{ Module1 = module_imports(BaseModuleName, [], [], Items1, no) },
+
 	process_module_interfaces(["mercury_builtin" | ImportedModules], 
-		[], Module0, Module),
+		[], Module1, Module2),
+	{ Module2 = module_imports(_, Direct2, Indirect2, Items2, Error2) },
+
+		% we add a pseudo-declarations `:- imported(yes)' at the end
+		% of the item list. Uses of the items with declarations 
+		% following this must be module qualified.
+	{ list__append(Items2,
+		[module_defn(VarSet, imported(yes)) - Context], Items3) },
+	{ Module3 = module_imports(BaseModuleName, Direct2, Indirect2, 
+		Items3, Error2) },
+	process_module_interfaces(UsedModules, [], Module3, Module),
+
 	{ Module = module_imports(_, _, _, _, Error) }.
 
 %-----------------------------------------------------------------------------%
@@ -1091,9 +1149,13 @@
 	;
 		{ Items = Items0 }
 	),
-	{ get_dependencies(Items, ImplementationDeps0) },
+	{ get_dependencies(Items, ImplImportDeps, ImplUseDeps) },
+	{ list__append(ImplImportDeps, ImplUseDeps, ImplementationDeps0) },
 	{ get_interface(Items, no, InterfaceItems) },
-	{ get_dependencies(InterfaceItems, InterfaceDeps0) },
+	{ get_dependencies(InterfaceItems, InterfaceImportDeps,
+		InterfaceUseDeps) },
+	{ list__append(InterfaceImportDeps, InterfaceUseDeps, 
+		InterfaceDeps0) },
 	{ InterfaceDeps = ["mercury_builtin" | InterfaceDeps0] },
 	{ ImplementationDeps = ["mercury_builtin" | ImplementationDeps0] },
 	{ get_fact_table_dependencies(Items, FactTableDeps) }.
@@ -1214,7 +1276,7 @@
 		globals__io_lookup_bool_option(statistics, Statistics),
 		maybe_report_stats(Statistics),
 
-		{ get_dependencies(Items1, IndirectImports1) },
+		{ get_dependencies(Items1, IndirectImports1, IndirectUses1) },
 		( { Error1 = fatal } ->
 			{ DirectImports1 = DirectImports0 }
 		;
@@ -1222,10 +1284,12 @@
 		),
 		{ list__append(IndirectImports0, IndirectImports1,
 			IndirectImports2) },
+		{ list__append(IndirectImports2, IndirectUses1,
+			IndirectImports3) },
 		{ list__append(Items0, Items1, Items2) },
 		{ Module1 = module_imports(ModuleName, DirectImports1, 
 					OldIndirectImports, Items2, Error2) },
-		process_module_interfaces(Imports, IndirectImports2,
+		process_module_interfaces(Imports, IndirectImports3,
 				Module1, Module)
 	).
 
@@ -1236,7 +1300,21 @@
 :- mode process_module_short_interfaces(in, in, out, di, uo) is det.
 
 process_module_short_interfaces(Imports, Module0, Module) -->
-	process_module_short_interfaces(Imports, ".int2", Module0, Module).
+	{ Module0 = module_imports(ModuleName, DirectImports0,
+		IndirectImports0, Items0, Error0) },
+
+		% Treat indirectly imported items as if they were imported 
+		% using `:- use_module', since all uses of them in the `.int'
+		% files must be module qualified.
+	{ varset__init(Varset) },
+	{ term__context_init(Context) },
+	{ list__append(Items0, [module_defn(Varset, imported(yes)) - Context], 
+		Items1) },
+
+	{ Module1 = module_imports(ModuleName, DirectImports0,
+		IndirectImports0, Items1, Error0) },
+
+	process_module_short_interfaces(Imports, ".int2", Module1, Module).
 
 
 :- pred process_module_short_interfaces(list(string), string, 
@@ -1277,7 +1355,7 @@
 		globals__io_lookup_bool_option(statistics, Statistics),
 		maybe_report_stats(Statistics),
 
-		{ get_dependencies(Items1, Imports1) },
+		{ get_dependencies(Items1, Imports1, _Uses1) },
 		{ list__append(Imports, Imports1, Imports2) },
 		{ list__append(Items0, Items1, Items2) },
 		{ IndirectImports1 = [Import | IndirectImports0] },
@@ -1288,22 +1366,31 @@
 
 %-----------------------------------------------------------------------------%
 
-get_dependencies(Items, Deps) :-
-	get_dependencies_2(Items, [], Deps).
+get_dependencies(Items, ImportDeps, UseDeps) :-
+	get_dependencies_2(Items, [], ImportDeps, [], UseDeps).
 
-:- pred get_dependencies_2(item_list, list(string), list(string)).
-:- mode get_dependencies_2(in, in, out) is det.
+:- pred get_dependencies_2(item_list, list(string), list(string), 
+		list(string), list(string)).
+:- mode get_dependencies_2(in, in, out, in, out) is det.
 
-get_dependencies_2([], Deps, Deps).
-get_dependencies_2([Item - _Context | Items], Deps0, Deps) :-
+get_dependencies_2([], ImportDeps, ImportDeps, UseDeps, UseDeps).
+get_dependencies_2([Item - _Context | Items], ImportDeps0, ImportDeps,
+		UseDeps0, UseDeps) :-
 	( 
 		Item = module_defn(_VarSet, import(module(Modules)))
 	->
-		list__append(Deps0, Modules, Deps1)
+		list__append(ImportDeps0, Modules, ImportDeps1),
+		UseDeps1 = UseDeps0
 	;
-		Deps1 = Deps0
+		Item = module_defn(_VarSet, use(module(Modules)))
+	->
+		list__append(UseDeps0, Modules, UseDeps1),
+		ImportDeps1 = ImportDeps0
+	;
+		ImportDeps1 = ImportDeps0,
+		UseDeps1 = UseDeps0
 	),
-	get_dependencies_2(Items, Deps1, Deps).
+	get_dependencies_2(Items, ImportDeps1, ImportDeps, UseDeps1, UseDeps).
 
 %-----------------------------------------------------------------------------%
 	% get the fact table dependencies for a module
@@ -1351,7 +1438,7 @@
 	( Item = module_defn(_, interface) ->
 		Items1 = Items0,
 		InInterface1 = yes
-	; Item = module_defn(_, imported) ->
+	; Item = module_defn(_, imported(_)) ->
 		% module_qual.m needs the :- imported declaration.
 		( IncludeImported = yes ->
 			InInterface1 = yes,
@@ -1409,6 +1496,10 @@
 			Items, Imports, NeedsImports) :-
 	ItemAndContext = Item0 - Context,
 	( Item0 = module_defn(_, import(_)) ->
+		Items1 = Items0,
+		Imports1 = [ItemAndContext | Imports0],
+		NeedsImports1 = NeedsImports0
+	; Item0 = module_defn(_, use(_)) ->
 		Items1 = Items0,
 		Imports1 = [ItemAndContext | Imports0],
 		NeedsImports1 = NeedsImports0
Index: compiler/options.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/options.m,v
retrieving revision 1.199
diff -u -r1.199 options.m
--- options.m	1997/06/25 05:18:59	1.199
+++ options.m	1997/06/25 23:49:03
@@ -18,7 +18,7 @@
 
 :- module options.
 :- interface.
-:- import_module io, getopt.
+:- import_module char, io, getopt.
 
 :- pred short_option(char::in, option::out) is semidet.
 :- pred long_option(string::in, option::out) is semidet.
Index: compiler/passes_aux.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/passes_aux.m,v
retrieving revision 1.20
diff -u -r1.20 passes_aux.m
--- passes_aux.m	1997/06/13 08:33:25	1.20
+++ passes_aux.m	1997/06/16 03:22:28
@@ -13,7 +13,7 @@
 
 :- interface.
 
-:- import_module hlds_module, hlds_pred.
+:- import_module hlds_module, hlds_pred, prog_data.
 :- import_module io.
 
 %-----------------------------------------------------------------------------%
Index: compiler/pragma_c_gen.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/pragma_c_gen.m,v
retrieving revision 1.8
diff -u -r1.8 pragma_c_gen.m
--- pragma_c_gen.m	1997/05/06 05:45:39	1.8
+++ pragma_c_gen.m	1997/06/16 03:22:52
@@ -23,7 +23,7 @@
 
 :- import_module hlds_goal, hlds_pred, prog_data.
 :- import_module llds, code_info.
-:- import_module list, std_util.
+:- import_module list, std_util, term.
 
 :- pred pragma_c_gen__generate_pragma_c_code(code_model::in, string::in,
 	may_call_mercury::in, pred_id::in, proc_id::in, list(var)::in,
Index: compiler/prog_data.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_data.m,v
retrieving revision 1.19
diff -u -r1.19 prog_data.m
--- prog_data.m	1997/06/27 04:01:36	1.19
+++ prog_data.m	1997/06/27 07:36:44
@@ -18,8 +18,8 @@
 
 :- interface.
 
-:- import_module hlds_pred.
-:- import_module list, varset, term, std_util.
+:- import_module hlds_data, hlds_pred.
+:- import_module bool, list, map, varset, term, std_util.
 
 %-----------------------------------------------------------------------------%
 
@@ -353,10 +353,12 @@
 :- type module_defn	--->	module(module_name)
 			;	interface
 			;	implementation
-			;	imported
+			;	imported(bool)
 				% this is used internally by the compiler,
 				% to identify declarations which originally
-				% came from some other module
+				% came from some other module. The argument
+				% is yes if uses of the following items must
+				% be module qualified
 			;	external(sym_name_specifier)
 				% this is used internally by the compiler,
 				% to identify items which originally
Index: compiler/prog_io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io.m,v
retrieving revision 1.155
diff -u -r1.155 prog_io.m
--- prog_io.m	1997/05/21 02:13:45	1.155
+++ prog_io.m	1997/06/16 03:24:46
@@ -101,7 +101,7 @@
 :- import_module prog_io_goal, prog_io_dcg, prog_io_pragma, prog_io_util.
 :- import_module hlds_data, hlds_pred, prog_util, globals, options.
 :- import_module bool, int, string, std_util, parser, term_io, dir, require.
-:- import_module term.
+:- import_module varset, term.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/prog_out.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_out.m,v
retrieving revision 1.30
diff -u -r1.30 prog_out.m
--- prog_out.m	1996/04/24 01:00:11	1.30
+++ prog_out.m	1997/06/12 07:19:47
@@ -32,11 +32,14 @@
 :- pred prog_out__write_module_spec(module_specifier, io__state, io__state).
 :- mode prog_out__write_module_spec(in, di, uo) is det.
 
+:- pred prog_out__write_module_list(list(module_name), io__state, io__state).
+:- mode prog_out__write_module_list(in, di, uo) is det.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
 :- implementation.
-:- import_module string, list, varset, std_util, term_io.
+:- import_module require, string, list, varset, std_util, term_io.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -103,6 +106,18 @@
 
 prog_out__write_module_spec(ModuleSpec) -->
 	io__write_string(ModuleSpec).
+
+%-----------------------------------------------------------------------------%
+
+prog_out__write_module_list([Import1, Import2, Import3 | Imports]) --> 
+	io__write_strings(["`", Import1, "', "]),
+	write_module_list([Import2, Import3 | Imports]).
+prog_out__write_module_list([Import1, Import2]) -->
+	io__write_strings(["`", Import1, "' and `", Import2 ,"'"]).
+prog_out__write_module_list([Import]) -->
+	io__write_strings(["`", Import, "'"]).
+prog_out__write_module_list([]) -->
+	{ error("prog_out__write_module_list") }.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
Index: compiler/quantification.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/quantification.m,v
retrieving revision 1.48
diff -u -r1.48 quantification.m
--- quantification.m	1997/05/05 11:17:27	1.48
+++ quantification.m	1997/06/16 03:25:28
@@ -34,8 +34,8 @@
 
 :- interface.
 
-:- import_module hlds_goal, hlds_pred.
-:- import_module list, set, term.
+:- import_module hlds_goal, hlds_pred, prog_data.
+:- import_module list, set, term, varset.
 
 :- pred implicitly_quantify_clause_body(list(var),
 		hlds_goal, varset, map(var, type),
Index: compiler/saved_vars.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/saved_vars.m,v
retrieving revision 1.8
diff -u -r1.8 saved_vars.m
--- saved_vars.m	1997/05/05 11:17:28	1.8
+++ saved_vars.m	1997/06/16 03:26:39
@@ -37,8 +37,8 @@
 :- implementation.
 
 :- import_module hlds_goal, hlds_out, goal_util, quantification, passes_aux.
-:- import_module mode_util.
-:- import_module bool, list, set, map, std_util, varset.
+:- import_module mode_util, prog_data.
+:- import_module bool, list, set, map, std_util, term, varset.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/special_pred.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/special_pred.m,v
retrieving revision 1.12
diff -u -r1.12 special_pred.m
--- special_pred.m	1997/05/20 01:52:06	1.12
+++ special_pred.m	1997/06/16 03:26:54
@@ -15,7 +15,7 @@
 
 :- module special_pred.
 :- interface.
-:- import_module list, prog_data, hlds_data, hlds_pred.
+:- import_module list, map, prog_data, hlds_data, hlds_pred.
 
 :- type special_pred_map	==	map(special_pred, pred_id).
 
Index: compiler/store_alloc.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/store_alloc.m,v
retrieving revision 1.51
diff -u -r1.51 store_alloc.m
--- store_alloc.m	1997/05/05 11:17:31	1.51
+++ store_alloc.m	1997/06/16 03:27:31
@@ -39,7 +39,7 @@
 :- import_module follow_vars, liveness, hlds_goal, llds.
 :- import_module options, globals, goal_util, mode_util, instmap.
 :- import_module list, map, set, std_util, assoc_list.
-:- import_module bool, int, require.
+:- import_module bool, int, require, term.
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/stratify.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/stratify.m,v
retrieving revision 1.7
diff -u -r1.7 stratify.m
--- stratify.m	1997/05/05 11:17:33	1.7
+++ stratify.m	1997/06/16 03:27:48
@@ -56,7 +56,7 @@
 :- import_module hlds_module, type_util, mode_util, prog_data, passes_aux.
 :- import_module prog_out, globals, options.
 
-:- import_module map, list, set, bool, std_util, relation, require.
+:- import_module assoc_list, map, list, set, bool, std_util, relation, require.
 
 
 
Index: compiler/string_switch.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/string_switch.m,v
retrieving revision 1.21
diff -u -r1.21 string_switch.m
--- string_switch.m	1997/04/07 05:39:47	1.21
+++ string_switch.m	1997/06/16 03:28:04
@@ -18,6 +18,7 @@
 :- interface.
 
 :- import_module hlds_goal, llds, switch_gen, code_info.
+:- import_module term.
 
 :- pred string_switch__generate(cases_list, var, code_model,
 	can_fail, store_map, label, code_tree, code_info, code_info).
Index: compiler/switch_gen.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/switch_gen.m,v
retrieving revision 1.57
diff -u -r1.57 switch_gen.m
--- switch_gen.m	1997/04/25 06:02:30	1.57
+++ switch_gen.m	1997/06/16 05:39:43
@@ -43,6 +43,7 @@
 :- interface.
 
 :- import_module hlds_goal, hlds_data, code_info.
+:- import_module term.
 
 :- pred switch_gen__generate_switch(code_model, var, can_fail, list(case),
 	store_map, hlds_goal_info, code_tree, code_info, code_info).
Index: compiler/tag_switch.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/tag_switch.m,v
retrieving revision 1.36
diff -u -r1.36 tag_switch.m
--- tag_switch.m	1997/05/21 02:13:50	1.36
+++ tag_switch.m	1997/06/16 03:29:14
@@ -26,8 +26,8 @@
 :- implementation.
 
 :- import_module hlds_module, hlds_pred, hlds_data, code_gen.
-:- import_module options, globals, type_util, std_util.
-:- import_module bool, map, tree, int, require.
+:- import_module options, globals, type_util, prog_data.
+:- import_module assoc_list, bool, map, tree, int, require, std_util, term.
 
 % where is the secondary tag (if any) for this primary tag value
 :- type stag_loc	--->	none ; local ; remote.
Index: compiler/typecheck.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/typecheck.m,v
retrieving revision 1.206
diff -u -r1.206 typecheck.m
--- typecheck.m	1997/06/01 19:45:39	1.206
+++ typecheck.m	1997/06/16 03:29:54
@@ -170,7 +170,7 @@
 :- import_module passes_aux, clause_to_proc.
 
 :- import_module int, list, map, set, string, require, std_util, tree234.
-:- import_module varset, term, term_io.
+:- import_module assoc_list, varset, term, term_io.
 
 %-----------------------------------------------------------------------------%
 
@@ -907,44 +907,26 @@
 		% non-polymorphic predicate)
 		( PredIdList = [PredId0] ->
 			
+			PredId = PredId0,
 			predicate_table_get_preds(PredicateTable, Preds),
-			map__lookup(Preds, PredId0, PredInfo),
-			pred_info_import_status(PredInfo, CalledStatus),
-			typecheck_info_get_pred_import_status(TypeCheckInfo1,
-						CallingStatus),
-			( 
-				% Only opt_imported preds can look at
-				% declarations from .opt files.
-				(
-				  CalledStatus \= opt_decl
-				; CallingStatus = opt_imported
-				) 
-			->
-				PredId = PredId0,
-				pred_info_arg_types(PredInfo, PredTypeVarSet,
-							PredArgTypes),
-
-					% rename apart the type variables in 
-					% called predicate's arg types and then
-					% unify the types of the call arguments
-					% with the called predicates' arg types
-					% (optimize for the common case of
-					% a non-polymorphic predicate)
-				( varset__is_empty(PredTypeVarSet) ->
-				    typecheck_var_has_type_list(Args,
-					PredArgTypes, 0, TypeCheckInfo1,
-					TypeCheckInfo)
-				;
-				    typecheck_var_has_polymorphic_type_list(
-					Args, PredTypeVarSet, PredArgTypes,
-					TypeCheckInfo1, TypeCheckInfo)
-				)
+			map__lookup(Preds, PredId, PredInfo),
+			pred_info_arg_types(PredInfo, PredTypeVarSet,
+						PredArgTypes),
+
+				% rename apart the type variables in 
+				% called predicate's arg types and then
+				% unify the types of the call arguments
+				% with the called predicates' arg types
+				% (optimize for the common case of
+				% a non-polymorphic predicate)
+			( varset__is_empty(PredTypeVarSet) ->
+			    typecheck_var_has_type_list(Args,
+				PredArgTypes, 0, TypeCheckInfo1,
+				TypeCheckInfo)
 			;
-				invalid_pred_id(PredId),
-				report_pred_call_error(TypeCheckInfo1, 
-					ModuleInfo, PredicateTable, 
-					PredCallId, TypeCheckInfo)
-				
+			    typecheck_var_has_polymorphic_type_list(
+				Args, PredTypeVarSet, PredArgTypes,
+				TypeCheckInfo1, TypeCheckInfo)
 			)
 		;
 			typecheck_info_get_pred_import_status(TypeCheckInfo1,
@@ -981,9 +963,8 @@
 	typecheck_info_get_io_state(TypeCheckInfo1, IOState0),
 	(
 		predicate_table_search_pred_sym(PredicateTable,
-			PredName, OtherIds0),
+			PredName, OtherIds),
 		predicate_table_get_preds(PredicateTable, Preds),
-		typecheck_filter_optdecls(Preds, OtherIds0, OtherIds),
 		OtherIds \= []
 	->
 		typecheck_find_arities(Preds, OtherIds, Arities),
@@ -991,9 +972,7 @@
 			Arities, IOState0, IOState)
 	;
 		predicate_table_search_func_sym(PredicateTable,
-			PredName, OtherIds0),
-		predicate_table_get_preds(PredicateTable, Preds),
-		typecheck_filter_optdecls(Preds, OtherIds0, OtherIds),
+			PredName, OtherIds),
 		OtherIds \= []
 	->
 		report_error_func_instead_of_pred(TypeCheckInfo1, PredCallId,
@@ -1014,20 +993,6 @@
 	pred_info_arity(PredInfo, Arity),
 	typecheck_find_arities(Preds, PredIds, Arities).
 
-	% Since .opt files are guaranteed to be type correct, and only
-	% preds defined in .opt files can "see" predicates declared in
-	% .opt files, filter out the opt_decl preds when working out
-	% a type error message.
-:- pred typecheck_filter_optdecls(pred_table, list(pred_id), list(pred_id)).
-:- mode typecheck_filter_optdecls(in, in, out) is det.
-
-typecheck_filter_optdecls(Preds, PredIds0, PredIds) :-
-	FilterOptDecls = lambda([PredId::in] is semidet, (
-			map__lookup(Preds, PredId, PredInfo),
-			\+ pred_info_import_status(PredInfo, opt_decl)
-		)),
-	list__filter(FilterOptDecls, PredIds0, PredIds).
-
 :- pred typecheck_call_overloaded_pred(list(pred_id), list(var),
 				import_status, typecheck_info, typecheck_info).
 :- mode typecheck_call_overloaded_pred(in, in, in,
@@ -1063,20 +1028,9 @@
 get_overloaded_pred_arg_types([PredId | PredIds], Preds, CallingPredStatus,
 		TypeAssignSet0, ArgsTypeAssignSet0, ArgsTypeAssignSet) :-
 	map__lookup(Preds, PredId, PredInfo),
-	pred_info_import_status(PredInfo, Status),
-	(
-		% Only opt_imported preds can look at
-		% declarations from .opt files.
-		( CallingPredStatus = opt_imported
-		; Status \= opt_decl
-		)
-	->
-		pred_info_arg_types(PredInfo, PredTypeVarSet, PredArgTypes),
-		rename_apart(TypeAssignSet0, PredTypeVarSet, PredArgTypes,
-				ArgsTypeAssignSet0, ArgsTypeAssignSet1)
-	;
-		ArgsTypeAssignSet1 = ArgsTypeAssignSet0
-	),
+	pred_info_arg_types(PredInfo, PredTypeVarSet, PredArgTypes),
+	rename_apart(TypeAssignSet0, PredTypeVarSet, PredArgTypes,
+		ArgsTypeAssignSet0, ArgsTypeAssignSet1),
 	get_overloaded_pred_arg_types(PredIds, Preds, CallingPredStatus,
 		TypeAssignSet0, ArgsTypeAssignSet1, ArgsTypeAssignSet).
 
@@ -1119,7 +1073,6 @@
 		% module qualified, so they should not be considered
 		% when resolving overloading.
 		module_info_pred_info(ModuleInfo, PredId, PredInfo),
-		\+ pred_info_import_status(PredInfo, opt_decl),
 
 		%
 		% lookup the argument types of the candidate predicate
@@ -2148,76 +2101,62 @@
 		module_info, list(cons_type_info), list(cons_type_info)).
 :- mode make_pred_cons_info(typecheck_info_ui, in, in, in, in, in, out) is det.
 
-make_pred_cons_info(TypeCheckInfo, PredId, PredTable, FuncArity,
+make_pred_cons_info(_TypeCheckInfo, PredId, PredTable, FuncArity,
 		_ModuleInfo, L0, L) :-
 	map__lookup(PredTable, PredId, PredInfo),
 	pred_info_arity(PredInfo, PredArity),
 	pred_info_get_is_pred_or_func(PredInfo, IsPredOrFunc),
-	pred_info_import_status(PredInfo, CalledStatus),
-	typecheck_info_get_pred_import_status(TypeCheckInfo, CallingStatus),
 	(
-		% Only opt_imported preds can look at the declarations of
-		% predicates from .opt files.
-		( CalledStatus \= opt_decl
-		; CallingStatus = opt_imported
-		)
+		IsPredOrFunc = predicate,
+		PredArity >= FuncArity
 	->
+		pred_info_arg_types(PredInfo, PredTypeVarSet,
+					CompleteArgTypes),
 		(
-			IsPredOrFunc = predicate,
-			PredArity >= FuncArity
+			list__split_list(FuncArity, CompleteArgTypes,
+				ArgTypes, PredTypeParams)
 		->
-			pred_info_arg_types(PredInfo, PredTypeVarSet,
-						CompleteArgTypes),
-			(
-				list__split_list(FuncArity, CompleteArgTypes,
-					ArgTypes, PredTypeParams)
-			->
-				term__context_init("<builtin>", 0, Context),
-				PredType = term__functor(term__atom("pred"),
-						PredTypeParams, Context),
-				ConsInfo = cons_type_info(PredTypeVarSet,
-						PredType, ArgTypes),
-				L = [ConsInfo | L0]
-			;
-				error("make_pred_cons_info: split_list failed")
-			)
+			term__context_init("<builtin>", 0, Context),
+			PredType = term__functor(term__atom("pred"),
+					PredTypeParams, Context),
+			ConsInfo = cons_type_info(PredTypeVarSet,
+					PredType, ArgTypes),
+			L = [ConsInfo | L0]
 		;
-			IsPredOrFunc = function,
-			PredAsFuncArity is PredArity - 1,
-			PredAsFuncArity >= FuncArity
+			error("make_pred_cons_info: split_list failed")
+		)
+	;
+		IsPredOrFunc = function,
+		PredAsFuncArity is PredArity - 1,
+		PredAsFuncArity >= FuncArity
+	->
+		pred_info_arg_types(PredInfo, PredTypeVarSet,
+					CompleteArgTypes),
+		(
+			list__split_list(FuncArity, CompleteArgTypes,
+				FuncArgTypes, FuncTypeParams),
+			list__length(FuncTypeParams, NumParams0),
+			NumParams1 is NumParams0 - 1,
+			list__split_list(NumParams1, FuncTypeParams,
+			    FuncArgTypeParams, [FuncReturnTypeParam])
 		->
-			pred_info_arg_types(PredInfo, PredTypeVarSet,
-						CompleteArgTypes),
-			(
-				list__split_list(FuncArity, CompleteArgTypes,
-					FuncArgTypes, FuncTypeParams),
-				list__length(FuncTypeParams, NumParams0),
-				NumParams1 is NumParams0 - 1,
-				list__split_list(NumParams1, FuncTypeParams,
-				    FuncArgTypeParams, [FuncReturnTypeParam])
-			->
-				( FuncArgTypeParams = [] ->
-					FuncType = FuncReturnTypeParam
-				;
-					term__context_init("<builtin>", 0,
-							Context),
-					FuncType = term__functor(
-							term__atom("="), [
-							term__functor(
-							term__atom("func"),
-							FuncArgTypeParams,
-							Context),
-							FuncReturnTypeParam
-						], Context)
-				),
-				ConsInfo = cons_type_info(PredTypeVarSet,
-						FuncType, FuncArgTypes),
-				L = [ConsInfo | L0]
+			( FuncArgTypeParams = [] ->
+				FuncType = FuncReturnTypeParam
 			;
-				error("make_pred_cons_info: split_list or remove_suffix failed")
-			)
+				term__context_init("<builtin>", 0, Context),
+				FuncType = term__functor(
+					term__atom("="), [
+					term__functor(term__atom("func"),
+						FuncArgTypeParams,
+						Context),
+					FuncReturnTypeParam
+					], Context)
+			),
+			ConsInfo = cons_type_info(PredTypeVarSet,
+					FuncType, FuncArgTypes),
+			L = [ConsInfo | L0]
 		;
-			L = L0
+			error("make_pred_cons_info: split_list or remove_suffix failed")
 		)
 	;
 		L = L0
@@ -2764,28 +2703,17 @@
 :- convert_cons_defn_list(_, L, _) when L.	% NU-Prolog indexing.
 
 convert_cons_defn_list(_TypeCheckInfo, [], []).
-convert_cons_defn_list(TypeCheckInfo, [X|Xs], Ys) :-
-	( convert_cons_defn(TypeCheckInfo, X, Y0) ->
-		Y = Y0,
-		convert_cons_defn_list(TypeCheckInfo, Xs, Ys1),
-		Ys = [Y | Ys1]
-	;
-		convert_cons_defn_list(TypeCheckInfo, Xs, Ys)
-	).
+convert_cons_defn_list(TypeCheckInfo, [X|Xs], [Y|Ys]) :-
+	convert_cons_defn(TypeCheckInfo, X, Y),
+	convert_cons_defn_list(TypeCheckInfo, Xs, Ys).
 
 :- pred convert_cons_defn(typecheck_info, hlds_cons_defn, cons_type_info).
-:- mode convert_cons_defn(typecheck_info_ui, in, out) is semidet.
+:- mode convert_cons_defn(typecheck_info_ui, in, out) is det.
 
 convert_cons_defn(TypeCheckInfo, HLDS_ConsDefn, ConsTypeInfo) :-
 	HLDS_ConsDefn = hlds_cons_defn(ArgTypes, TypeId, Context),
 	typecheck_info_get_types(TypeCheckInfo, Types),
 	map__lookup(Types, TypeId, TypeDefn),
-	typecheck_info_get_pred_import_status(TypeCheckInfo, PredStatus),
-	hlds_data__get_type_defn_status(TypeDefn, TypeStatus),
-	% Don't match constructors in local preds that shouldn't be visible.
-	( PredStatus = opt_imported
-	; TypeStatus \= opt_imported, TypeStatus \= abstract_imported
-	),
 	hlds_data__get_type_defn_tvarset(TypeDefn, ConsTypeVarSet),
 	hlds_data__get_type_defn_tparams(TypeDefn, ConsTypeParams),
 	construct_type(TypeId, ConsTypeParams, Context, ConsType),
Index: compiler/unused_args.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/unused_args.m,v
retrieving revision 1.30
diff -u -r1.30 unused_args.m
--- unused_args.m	1997/06/27 04:01:40	1.30
+++ unused_args.m	1997/06/27 07:36:47
@@ -53,7 +53,7 @@
 :- import_module passes_aux, inst_match.
 
 :- import_module bool, char, int, list, map, require.
-:- import_module set, std_util, string, varset. 
+:- import_module set, std_util, string, term, varset. 
 
 		% Information about the dependencies of a variable
 		% that is not known to be used.
Index: compiler/vn_debug.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/vn_debug.m,v
retrieving revision 1.25
diff -u -r1.25 vn_debug.m
--- vn_debug.m	1997/05/21 02:13:57	1.25
+++ vn_debug.m	1997/06/16 03:30:50
@@ -15,7 +15,7 @@
 :- interface.
 
 :- import_module atsort, vn_type, vn_table, livemap.
-:- import_module bool, list, io.
+:- import_module assoc_list, bool, list, io.
 
 :- pred vn_debug__tuple_msg(maybe(bool), list(instruction), vn_ctrl_tuple,
 	io__state, io__state).
Index: compiler/vn_table.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/vn_table.m,v
retrieving revision 1.14
diff -u -r1.14 vn_table.m
--- vn_table.m	1997/04/07 05:39:59	1.14
+++ vn_table.m	1997/06/16 03:31:03
@@ -15,7 +15,7 @@
 :- interface.
 
 :- import_module vn_type.
-:- import_module list, map.
+:- import_module assoc_list, list, map.
 
 :- type vn_tables.
 
Index: compiler/vn_type.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/vn_type.m,v
retrieving revision 1.30
diff -u -r1.30 vn_type.m
--- vn_type.m	1997/01/21 05:05:29	1.30
+++ vn_type.m	1997/06/19 09:57:41
@@ -14,7 +14,7 @@
 
 :- interface.
 :- import_module llds, livemap, options.
-:- import_module getopt, set, list, std_util.
+:- import_module bool, getopt, map, set, list, std_util.
 
 :- type vn == int.
 
Index: compiler/notes/todo.html
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/notes/todo.html,v
retrieving revision 1.3
diff -u -r1.3 todo.html
--- todo.html	1997/05/07 05:24:11	1.3
+++ todo.html	1997/06/27 08:48:25
@@ -82,10 +82,6 @@
   independently of any declarations or imports in the implementation
   section
 
-<li> implement a use_module directive
-  (like import_module, except that everything must be explicitly
-  module qualified).
-
 <li> imports should not be transitive
   (currently we get this right for predicates, constants, and functors,
   but wrong for types, insts, and modes).
@@ -195,9 +191,7 @@
 <h2> module system: </h2>
 
 <ul>
-<li> produce warnings for imports that are not needed
-  	[done by stayl, but not enabled by default, due to some
-	minor problems]
+<li> produce warnings for implementation imports that are not needed
 
 <li> produce warnings for imports that are in the wrong place
   (in the interface instead of the implementation, and vice versa)
@@ -231,15 +225,13 @@
 <li> more work on module system, separate compilation, and the multiple
      specialisation problem
 
-<li> deforestation
-
 <li> extended DCGs
 
 <li> transform non-tail-recursive predicates into tail-recursive form
      using accumulators
 	[being worked on by jammb]
 
-<li> partial deduction
+<li> deforestation and partial deduction
 	[being worked on by stayl]
 </ul>
 
@@ -283,7 +275,7 @@
 
 <li> give a better error message for the use of if-then without else.
 
-<li> give a better error message for the use of `<=' instead of `>='
+<li> give a better error message for the use of `<=' instead of `=<'
      (but how?)
 
 <li> give a better error message for type errors involving higher-order pred
Index: doc/reference_manual.texi
===================================================================
RCS file: /home/staff/zs/imp/mercury/doc/reference_manual.texi,v
retrieving revision 1.52
diff -u -r1.52 reference_manual.texi
--- reference_manual.texi	1997/06/27 04:02:12	1.52
+++ reference_manual.texi	1997/06/27 07:39:23
@@ -340,6 +340,7 @@
 :- interface
 :- implementation
 :- import_module
+:- use_module
 :- external
 :- end_module
 @end example
@@ -2263,20 +2264,21 @@
 @c ===> no
 
 If a module wishes to make use of entities exported by other modules,
-then it must explicitly import those modules
-using one or more @code{import_module} declarations.
-These declarations may occur
-either in the interface or the implementation section.
-If the imported entities are used in the interface section,
-then the corresponding @code{import_module} declaration must
-also be in the interface section.  If the imported entities are only
-used in the implementation section, the @code{import_module}
-declaration should be in the implementation section.
+then it must explicitly import those modules using one or more 
+ at code{import_module} or @code{use_module} declarations.
+These declarations may occur either in the interface or the implementation 
+section. If the imported entities are used in the interface section,
+then the corresponding @code{import_module} or @code{use_module}
+declaration must also be in the interface section. If the imported 
+entities are only used in the implementation section, the 
+ at code{import_module} or @code{use_module} declaration should be in 
+the implementation section.
 
 Declarations, predicate calls, types, modes and insts
 can be explicitly module qualified using the `:' operator,
 i.e. @code{module:name}. This is useful both for readability and
-for resolving name conflicts.
+for resolving name conflicts. Uses of entities imported using 
+ at code{use_module} declarations must be explicitly module qualified.
 
 Certain optimizations require information or source code for predicates
 defined in other modules to be as effective as possible. At the moment,
@@ -2766,11 +2768,7 @@
 with name @var{Name} and arity @var{Arity} should be inlined.
 
 The current Mercury implementation is smart enough to inline many
-simple predicates even without this hint.  However, in the current
-Mercury implementation, cross-module inlining is not yet supported, so
-if a predicate for which there is an @samp{inline} pragma is called
-from a module other than the one it is defined in, the compiler will
-ignore the hint.
+simple predicates even without this hint.
 
 @node Preventing Inlining
 @section Preventing Inlining
Index: library/multi_map.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/multi_map.m,v
retrieving revision 1.2
diff -u -r1.2 multi_map.m
--- multi_map.m	1997/05/21 02:16:16	1.2
+++ multi_map.m	1997/06/15 13:02:59
@@ -21,7 +21,7 @@
 
 :- module multi_map.
 :- interface.
-:- import_module map, list.
+:- import_module map, list, assoc_list, set.
 
 %-----------------------------------------------------------------------------%
 
Index: library/prolog.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/prolog.m,v
retrieving revision 1.3
diff -u -r1.3 prolog.m
--- prolog.m	1997/06/04 09:59:35	1.3
+++ prolog.m	1997/06/14 07:15:53
@@ -14,7 +14,7 @@
 %-----------------------------------------------------------------------------%
 :- module prolog.
 :- interface.
-:- import_module std_util.
+:- import_module list, std_util.
 
 % We define !/0 (and !/2 for dcgs) to be equivalent to `true'.  This is for
 % backwards compatibility with Prolog systems.  But of course it only works
Index: tests/hard_coded/bidirectional.m
===================================================================
RCS file: /home/staff/zs/imp/tests/hard_coded/bidirectional.m,v
retrieving revision 1.1
diff -u -r1.1 bidirectional.m
--- bidirectional.m	1996/11/07 14:18:22	1.1
+++ bidirectional.m	1997/06/17 06:43:36
@@ -9,7 +9,7 @@
 :- pred main(io__state::di, io__state::uo) is det.
 
 :- implementation.
-:- import_module string, std_util, list.
+:- import_module char, string, std_util, list.
 
 main -->
 	format_list_of_int(read, List),
Index: tests/hard_coded/cycles.m
===================================================================
RCS file: /home/staff/zs/imp/tests/hard_coded/cycles.m,v
retrieving revision 1.1
diff -u -r1.1 cycles.m
--- cycles.m	1997/04/09 13:46:17	1.1
+++ cycles.m	1997/06/17 06:47:10
@@ -14,7 +14,7 @@
 
 %---------------------------------------------------------------------------
 :- implementation.
-:- import_module list, bool, std_util.
+:- import_module list, bool, std_util, char.
 
 %--------------------------------------------------
 :- type node ---> a ; b ; c ; d ; e ; f ; g ; h ; i ; j ; k ; l.
Index: tests/invalid/errors.err_exp
===================================================================
RCS file: /home/staff/zs/imp/tests/invalid/errors.err_exp,v
retrieving revision 1.2
diff -u -r1.2 errors.err_exp
--- errors.err_exp	1997/04/27 05:28:48	1.2
+++ errors.err_exp	1997/06/13 10:03:04
@@ -1,10 +1,14 @@
 errors.m:001: Warning: module should start with a `:- module' declaration.
 errors.m:060: Error: free type parameter in RHS of type definition: f(_0).
 errors.m:061: Error: free type parameter in RHS of type definition: f(_0).
+errors.m:000: Error:  module `int' is imported using both
+errors.m:000:   `:- import_module' and `:- use_module' declarations.
 errors.m:051: In definition of type `errors:du_type_which_references_undefined_type'/0:
 errors.m:051:   error: undefined type `undefined_type'/0.
 errors.m:053: In definition of type `errors:eqv_type_which_references_undefined_type'/0:
 errors.m:053:   error: undefined type `undefined_type'/0.
+errors.m:100: In definition of type `errors:needs_qualification'/0:
+errors.m:100:   error: undefined type `state'/0.
 errors.m:055: Error: circular equivalence type `errors:circular_eqv_type'/0.
 errors.m:057: Error: circular equivalence type `errors:indirectly_circular_eqv_type_1'/0.
 errors.m:058: Error: circular equivalence type `errors:indirectly_circular_eqv_type_2'/0.
Index: tests/invalid/errors.m
===================================================================
RCS file: /home/staff/zs/imp/tests/invalid/errors.m,v
retrieving revision 1.1
diff -u -r1.1 errors.m
--- errors.m	1995/07/11 13:11:59	1.1
+++ errors.m	1997/06/13 10:03:01
@@ -91,3 +91,13 @@
 pred_with_singleton_vars(X).
 
 %-----------------------------------------------------------------------------%
+
+:- import_module int.
+:- use_module int.
+
+:- use_module io.
+
+:- type needs_qualification == state.
+:- type has_qualification == io__state.
+
+%-----------------------------------------------------------------------------%
Index: tests/invalid/errors2.err_exp
===================================================================
RCS file: /home/staff/zs/imp/tests/invalid/errors2.err_exp,v
retrieving revision 1.3
diff -u -r1.3 errors2.err_exp
--- errors2.err_exp	1997/05/04 17:43:02	1.3
+++ errors2.err_exp	1997/06/13 10:01:16
@@ -55,5 +55,10 @@
 errors2.m:072:   in argument 1 of call to pred `expect_int/1':
 errors2.m:072:   type error: variable `C' has type `string',
 errors2.m:072:   expected type was `int'.
+errors2.m:078: In clause for predicate `errors2:type_error_8/0':
+errors2.m:078:   in argument 1 of call to predicate `from_char_list/2':
+errors2.m:078:   error: undefined symbol `[]/0'.
+errors2.m:078: In clause for predicate `errors2:type_error_8/0':
+errors2.m:078:   error: undefined predicate `from_char_list/2'.
 errors2.m:009: Inferred :- pred bind_type_param(int).
 For more information, try recompiling with `-E'.
Index: tests/invalid/errors2.m
===================================================================
RCS file: /home/staff/zs/imp/tests/invalid/errors2.m,v
retrieving revision 1.3
diff -u -r1.3 errors2.m
--- errors2.m	1997/05/06 06:02:59	1.3
+++ errors2.m	1997/06/12 08:05:24
@@ -71,5 +71,11 @@
 	Z = bar_functor(A, B, C),
 	expect_int(C).
 
+:- use_module list, string.
+
+:- pred type_error_8.
+type_error_8 :-
+	from_char_list([], Str),
+	string__from_char_list(list:[], Str).
 
 %-----------------------------------------------------------------------------%
Index: tests/valid/Mmake
===================================================================
RCS file: /home/staff/zs/imp/tests/valid/Mmake,v
retrieving revision 1.42
diff -u -r1.42 Mmake
--- Mmake	1997/06/25 08:45:54	1.42
+++ Mmake	1997/06/27 08:04:04
@@ -33,6 +33,7 @@
 	implied_mode.m \
 	indexing.m \
 	intermod_lambda.m \
+	intermod_test.m \
 	lambda_inference.m\
 	lambda_instmap_bug.m \
 	lambda_quant.m \
@@ -98,6 +99,12 @@
 	$(MCOI) --grade $(GRADE) $(MCOIFLAGS) intermod_lambda2.m
 	$(MCG) --grade $(GRADE) $(MCGFLAGS) --intermodule-optimization \
 		intermod_lambda.m
+
+intermod_test.c:
+	$(MCI) --grade $(GRADE) $(MCIFLAGS) intermod_test2.m
+	$(MCOI) --grade $(GRADE) $(MCOIFLAGS) intermod_test2.m
+	$(MCG) --grade $(GRADE) $(MCGFLAGS) --intermodule-optimization \
+		intermod_test.m
 
 check:	objs
 



tests/valid/intermod_test.m
===================================================================
% Test overloading resolution for cross-module optimization.
:- module intermod_test.
:- interface.

:- import_module int.

:- pred p(int::out) is det.

:- type t
	--->	f(int)
	;	g.

:- implementation.

:- import_module intermod_test2.

p(X) :-
	Y = f(1),
	Y = f(_),
	Lambda = lambda([Z::int_mode] is det, Z = 2),
	local(Lambda, X).

:- mode int_mode :: out.

:- pred local(pred(int), int).
:- mode local(pred(int_mode) is det, out) is det.

local(Pred, Int) :- call(Pred, Int).


tests/valid/intermod_test2.m
===================================================================
:- module intermod_test2.

:- interface.

:- import_module int.
:- pred baz(int::in) is semidet.

:- implementation.

:- type t
	--->	f(int)
	;	g.

:- mode int_mode :: in.


baz(X) :- T = f(1), bar(T, X).

:- pred bar(t::in, int::int_mode) is semidet.

bar(T, 2) :- T = f(1).

:- pred local(pred(int), int).
:- mode local(pred(int_mode) is det, out) is det.

local(Pred, Int) :- call(Pred, Int0), Int is Int0 + 1.



More information about the developers mailing list