Files
mercury/compiler/constraint.m
Simon Taylor 2725b1a331 Aditi update syntax, type and mode checking.
Estimated hours taken: 220

Aditi update syntax, type and mode checking.

Change the hlds_goal for constructions in preparation for
structure reuse to avoid making multiple conflicting changes.

compiler/hlds_goal.m:
	Merge `higher_order_call' and `class_method_call' into a single
	`generic_call' goal type. This also has alternatives for the
	various Aditi builtins for which type declarations can't
	be written.

	Remove the argument types field from higher-order/class method calls.
	It wasn't used often, and wasn't updated by optimizations
	such as inlining. The types can be obtained from the vartypes
	field of the proc_info.

	Add a `lambda_eval_method' field to lambda_goals.

	Add a field to constructions to identify which RL code fragment should
	be used for an top-down Aditi closure.

	Add fields to constructions to hold structure reuse information.
	This is currently ignored -- the changes to implement structure
	reuse will be committed to the alias branch.
	This is included here to avoid lots of CVS conflicts caused by
	changing the definition of `hlds_goal' twice.

	Add a field to `some' goals to specify whether the quantification
	can be removed. This is used to make it easier to ensure that
	indexes are used for updates.

	Add a field to lambda_goals to describe whether the modes were
	guessed by the compiler and may need fixing up after typechecking
	works out the argument types.

	Add predicate `hlds_goal__generic_call_id' to work out a call_id
	for a generic call for use in error messages.

compiler/purity.m:
compiler/post_typecheck.m:
	Fill in the modes of Aditi builtin calls and closure constructions.
	This needs to know which are the `aditi__state' arguments, so
	it must be done after typechecking.

compiler/prog_data.m:
	Added `:- type sym_name_and_arity ---> sym_name/arity'.

	Add a type `lambda_eval_method', which describes how a closure
	is to be executed. The alternatives are normal Mercury execution,
	bottom-up execution by Aditi and top-down execution by Aditi.

compiler/prog_out.m:
	Add predicate `prog_out__write_sym_name_and_arity', which
	replaces duplicated inline code in a few places.

compiler/hlds_data.m:
	Add a `lambda_eval_method' field to `pred_const' cons_ids and
	`pred_closure_tag' cons_tags.

compiler/hlds_pred.m:
	Remove type `pred_call_id', replace it with type `simple_call_id',
	which combines a `pred_or_func' and a `sym_name_and_arity'.

	Add a type `call_id' which describes all the different types of call,
	including normal calls, higher-order and class-method calls
	and Aditi builtins.

	Add `aditi_top_down' to the type `marker'.

	Remove `aditi_interface' from type `marker'. Interfacing to
	Aditi predicates is now handled by `generic_call' hlds_goals.

	Add a type `rl_exprn_id' which identifies a predicate to
	be executed top-down by Aditi.
	Add a `maybe(rl_exprn_id)'  field to type `proc_info'.

	Add predicate `adjust_func_arity' to convert between the arity
	of a function to its arity as a predicate.

	Add predicates `get_state_args' and `get_state_args_det' to
	extract the DCG state arguments from an argument list.

	Add predicate `pred_info_get_call_id' to get a `simple_call_id'
	for a predicate for use in error messages.

compiler/hlds_out.m:
	Write the new representation for call_ids.

	Add a predicate `hlds_out__write_call_arg_id' which
	replaces similar code in mode_errors.m and typecheck.m.

compiler/prog_io_goal.m:
	Add support for `aditi_bottom_up' and `aditi_top_down' annotations
	on pred expressions.

compiler/prog_io_util.m:
compiler/prog_io_pragma.m:
	Add predicates
	- `prog_io_util:parse_name_and_arity' to parse `SymName/Arity'
		(moved from prog_io_pragma.m).
	- `prog_io_util:parse_pred_or_func_name_and_arity to parse
		`pred SymName/Arity' or `func SymName/Arity'.
	- `prog_io_util:parse_pred_or_func_and_args' to parse terms resembling
		a clause head (moved from prog_io_pragma.m).

compiler/type_util.m:
	Add support for `aditi_bottom_up' and `aditi_top_down' annotations
	on higher-order types.

	Add predicates `construct_higher_order_type',
	`construct_higher_order_pred_type' and
	`construct_higher_order_func_type' to avoid some code duplication.

compiler/mode_util.m:
	Add predicate `unused_mode/1', which returns `builtin:unused'.
	Add functions `aditi_di_mode/0', `aditi_ui_mode/0' and
	`aditi_uo_mode/0' which return `in', `in', and `out', but will
	be changed to return `di', `ui' and `uo' when alias tracking
	is implemented.

compiler/goal_util.m:
	Add predicate `goal_util__generic_call_vars' which returns
	any arguments to a generic_call which are not in the argument list,
	for example the closure passed to a higher-order call or
	the typeclass_info for a class method call.

compiler/llds.m:
compiler/exprn_aux.m:
compiler/dupelim.m:
compiler/llds_out.m:
compiler/opt_debug.m:
	Add builtin labels for the Aditi update operations.

compiler/hlds_module.m:
	Add predicate predicate_table_search_pf_sym, used for finding
	possible matches for a call with the wrong number of arguments.

compiler/intermod.m:
	Don't write predicates which build `aditi_top_down' goals,
	because there is currently no way to tell importing modules
	which RL code fragment to use.

compiler/simplify.m:
	Obey the `cannot_remove' field of explicit quantification goals.

compiler/make_hlds.m:
	Parse Aditi updates.

	Don't typecheck clauses for which syntax errors in Aditi updates
	are found - this avoids spurious "undefined predicate `aditi_insert/3'"
	errors.

	Factor out some common code to handle terms of the form `Head :- Body'.
	Factor out common code in the handling of pred and func expressions.

compiler/typecheck.m:
	Typecheck Aditi builtins.

	Allow the argument types of matching predicates to be adjusted
	when typechecking the higher-order arguments of Aditi builtins.

	Change `typecheck__resolve_pred_overloading' to take a list of
	argument types rather than a `map(var, type)' and a list of
	arguments to allow a transformation to be performed on the
	argument types before passing them.

compiler/error_util.m:
	Move the part of `report_error_num_args' which writes
	"wrong number of arguments (<x>; expected <y>)" from
	typecheck.m for use by make_hlds.m when reporting errors
	for Aditi builtins.

compiler/modes.m:
compiler/unique_modes.m:
compiler/modecheck_call.m:
	Modecheck Aditi builtins.

compiler/lambda.m:
	Handle the markers for predicates introduced for
	`aditi_top_down' and `aditi_bottom_up' lambda expressions.

compiler/polymorphism.m:
	Add extra type_infos to `aditi_insert' calls
	describing the tuple to insert.

compiler/call_gen.m:
	Generate code for Aditi builtins.

compiler/unify_gen.m:
compiler/bytecode_gen.m:
	Abort on `aditi_top_down' and `aditi_bottom_up' lambda
	expressions - code generation for them is not yet implemented.

compiler/magic.m:
	Use the `aditi_call' generic_call rather than create
	a new procedure for each Aditi predicate called from C.

compiler/rl_out.pp:
compiler/rl_gen.m:
compiler/rl.m:
	Move some utility code used by magic.m and call_gen.m into rl.m.

	Remove an XXX comment about reference counting being not yet
	implemented - Evan has fixed that.

library/ops.m:
compiler/mercury_to_mercury.m:
doc/transition_guide.texi:
	Add unary prefix operators `aditi_bottom_up' and `aditi_top_down',
	used as qualifiers on lambda expressions.
	Add infix operator `==>' to separate the tuples in an
	`aditi_modify' call.

compiler/follow_vars.m:
	Thread a `map(prog_var, type)' through, needed because
	type information is no longer held in higher-order call goals.

compiler/table_gen.m:
	Use the `make_*_construction' predicates in hlds_goal.m
	to construct constants.

compiler/*.m:
	Trivial changes to add extra fields to hlds_goal structures.

doc/reference_manual.texi:
	Document Aditi updates.

	Use @samp{pragma base_relation} instead of
	@samp{:- pragma base_relation} throughout the Aditi documentation
	to be consistent with other parts of the reference manual.

tests/valid/Mmakefile:
tests/valid/aditi_update.m:
tests/valid/aditi.m:
	Test case.

tests/valid/Mmakefile:
	Remove some hard-coded --intermodule-optimization rules which are
	no longer needed because `mmake depend' is now run in this directory.

tests/invalid/*.err_exp:
	Fix expected output for changes in reporting of call_ids
	in typecheck.m.

tests/invalid/Mmakefile
tests/invalid/aditi_update_errors.{m,err_exp}:
tests/invalid/aditi_update_mode_errors.{m,err_exp}:
	Test error messages for Aditi updates.

tests/valid/aditi.m:
tests/invalid/aditi.m:
	Cut down version of extras/aditi/aditi.m to provide basic declarations
	for Aditi compilation such as `aditi__state' and the modes
	`aditi_di', `aditi_uo' and `aditi_ui'. Installing extras/aditi/aditi.m
	somewhere would remove the need for these.
1999-07-13 08:55:28 +00:00

358 lines
13 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1995-1999 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
% File: constraint.m
% Main author: bromage.
%
% This module will eventually perform constraint propagation on
% an entire module. At the moment, though, it just does propagation
% within a single goal.
%
% The constraint propagation transformation attempts to improve
% the efficiency of a generate-and-test style program by statically
% scheduling constraints as early as possible, where a "constraint"
% is any goal which has no output and can fail.
%
% XXX Code is broken. Do not attempt to compile using the
% --constraint-propagation option!
%-----------------------------------------------------------------------------%
:- module constraint.
:- interface.
:- import_module hlds_module.
:- import_module io.
:- pred constraint_propagation(module_info, module_info, io__state, io__state).
:- mode constraint_propagation(in, out, di, uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module hlds_pred, hlds_goal, hlds_data.
:- import_module mode_util, passes_aux, code_aux, prog_data, instmap.
:- import_module delay_info, mode_info, inst_match, modes, mode_debug.
:- import_module transform, options, globals, varset, term.
:- import_module mercury_to_mercury, hlds_out, dependency_graph.
:- import_module bool, list, map, set, std_util, assoc_list, string.
:- import_module varset, term, require.
:- type constraint == hlds_goal.
%-----------------------------------------------------------------------------%
constraint_propagation(ModuleInfo0, ModuleInfo) -->
{ module_info_ensure_dependency_info(ModuleInfo0, ModuleInfo1) },
{ module_info_dependency_info(ModuleInfo1, DepInfo) },
{ hlds_dependency_info_get_dependency_ordering(DepInfo, DepOrd) },
constraint_propagation2(DepOrd, ModuleInfo1, ModuleInfo).
:- pred constraint_propagation2(dependency_ordering, module_info, module_info,
io__state, io__state).
:- mode constraint_propagation2(in, in, out, di, uo) is det.
constraint_propagation2([], ModuleInfo, ModuleInfo) --> [].
constraint_propagation2([C | Cs], ModuleInfo0, ModuleInfo) -->
constraint_propagation3(C, ModuleInfo0, ModuleInfo1),
constraint_propagation2(Cs, ModuleInfo1, ModuleInfo).
:- pred constraint_propagation3(list(pred_proc_id), module_info, module_info,
io__state, io__state).
:- mode constraint_propagation3(in, in, out, di, uo) is det.
constraint_propagation3([], ModuleInfo, ModuleInfo) --> [].
constraint_propagation3([proc(Pred, Proc) | Rest], ModuleInfo0, ModuleInfo) -->
constraint__propagate_in_proc(Pred, Proc, ModuleInfo0, ModuleInfo1),
modecheck_proc(Proc, Pred, ModuleInfo1, ModuleInfo2, Errs, _Changed),
( { Errs \= 0 } ->
{ error("constraint_propagation3") }
;
[]
),
constraint_propagation3(Rest, ModuleInfo2, ModuleInfo).
%-----------------------------------------------------------------------------%
:- pred constraint__propagate_in_proc(pred_id, proc_id, module_info,
module_info, io__state, io__state).
:- mode constraint__propagate_in_proc(in, in, in, out, di, uo) is det.
constraint__propagate_in_proc(PredId, ProcId, ModuleInfo0, ModuleInfo,
IoState0, IoState) :-
module_info_preds(ModuleInfo0, PredTable0),
map__lookup(PredTable0, PredId, PredInfo0),
pred_info_procedures(PredInfo0, ProcTable0),
map__lookup(ProcTable0, ProcId, ProcInfo0),
proc_info_goal(ProcInfo0, Goal0),
proc_info_varset(ProcInfo0, VarSet0),
varset__vars(VarSet0, VarList),
set__list_to_set(VarList, VarSet1),
proc_info_get_initial_instmap(ProcInfo0, ModuleInfo0, InstMap0),
proc_info_context(ProcInfo0, Context),
mode_info_init(IoState0, ModuleInfo0, PredId, ProcId,
Context, VarSet1, InstMap0, check_modes, ModeInfo0),
constraint__propagate_goal(Goal0, Goal, ModeInfo0, ModeInfo),
mode_info_get_io_state(ModeInfo, IoState),
mode_info_get_varset(ModeInfo, VarSet),
mode_info_get_var_types(ModeInfo, VarTypes),
mode_info_get_module_info(ModeInfo, ModuleInfo1),
proc_info_set_varset(ProcInfo0, VarSet, ProcInfo1),
proc_info_set_vartypes(ProcInfo1, VarTypes, ProcInfo2),
proc_info_set_goal(ProcInfo2, Goal, ProcInfo),
map__set(ProcTable0, ProcId, ProcInfo, ProcTable),
pred_info_set_procedures(PredInfo0, ProcTable, PredInfo),
map__set(PredTable0, PredId, PredInfo, PredTable),
module_info_set_preds(ModuleInfo1, PredTable, ModuleInfo).
%-----------------------------------------------------------------------------%
:- pred constraint__propagate_goal(hlds_goal, hlds_goal,
mode_info, mode_info).
:- mode constraint__propagate_goal(in, out,
mode_info_di, mode_info_uo) is det.
constraint__propagate_goal(Goal0 - GoalInfo, Goal - GoalInfo) -->
mode_info_dcg_get_instmap(InstMap0),
{ goal_info_get_instmap_delta(GoalInfo, DeltaInstMap) },
{ instmap__apply_instmap_delta(InstMap0, DeltaInstMap, InstMap) },
mode_info_set_instmap(InstMap),
constraint__propagate_goal_2(Goal0, Goal),
mode_info_set_instmap(InstMap).
%-----------------------------------------------------------------------------%
:- pred constraint__propagate_goal_2(hlds_goal_expr, hlds_goal_expr,
mode_info, mode_info).
:- mode constraint__propagate_goal_2(in, out,
mode_info_di, mode_info_uo) is det.
constraint__propagate_goal_2(conj(Goals0), conj(Goals)) -->
mode_checkpoint(enter, "conj"),
constraint__propagate_conj(Goals0, Goals),
mode_checkpoint(exit, "conj").
constraint__propagate_goal_2(par_conj(_, _), par_conj(_, _)) -->
{ error("constraint__propagate_goal_2: par_conj not supported") }.
constraint__propagate_goal_2(disj(Goals0, SM), disj(Goals, SM)) -->
mode_checkpoint(enter, "disj"),
constraint__propagate_disj(Goals0, Goals),
mode_checkpoint(exit, "disj").
constraint__propagate_goal_2(switch(Var, Det, Cases0, SM),
switch(Var, Det, Cases, SM)) -->
mode_checkpoint(enter, "switch"),
constraint__propagate_cases(Cases0, Cases),
mode_checkpoint(exit, "switch").
constraint__propagate_goal_2(if_then_else(Vars, Cond0, Then0, Else0, SM),
if_then_else(Vars, Cond, Then, Else, SM)) -->
mode_checkpoint(enter, "if_then_else"),
mode_info_dcg_get_instmap(InstMap0),
constraint__propagate_goal(Cond0, Cond),
% mode_info_dcg_get_instmap(InstMap1),
constraint__propagate_goal(Then0, Then),
mode_info_set_instmap(InstMap0),
constraint__propagate_goal(Else0, Else),
mode_checkpoint(exit, "if_then_else").
constraint__propagate_goal_2(not(Goal0), not(Goal)) -->
mode_checkpoint(enter, "not"),
constraint__propagate_goal(Goal0, Goal),
mode_checkpoint(exit, "not").
constraint__propagate_goal_2(some(Vars, Goal0), some(Vars, Goal)) -->
mode_checkpoint(enter, "some"),
constraint__propagate_goal(Goal0, Goal),
mode_checkpoint(exit, "some").
constraint__propagate_goal_2(
generic_call(A, B, C, D),
generic_call(A, B, C, D)) -->
mode_checkpoint(enter, "generic call"),
mode_checkpoint(exit, "generic call").
constraint__propagate_goal_2(
call(PredId, ProcId, ArgVars, Builtin, Sym, Context),
call(PredId, ProcId, ArgVars, Builtin, Sym, Context)) -->
mode_checkpoint(enter, "call"),
mode_checkpoint(exit, "call").
constraint__propagate_goal_2(unify(A,B,C,D,E), unify(A,B,C,D,E)) -->
mode_checkpoint(enter, "unify"),
mode_checkpoint(exit, "unify").
constraint__propagate_goal_2(
pragma_c_code(A, B, C, D, E, F, G),
pragma_c_code(A, B, C, D, E, F, G)) -->
mode_checkpoint(enter, "pragma_c_code"),
mode_checkpoint(exit, "pragma_c_code").
%-----------------------------------------------------------------------------%
:- pred constraint__propagate_disj(list(hlds_goal), list(hlds_goal),
mode_info, mode_info).
:- mode constraint__propagate_disj(in, out,
mode_info_di, mode_info_uo) is det.
constraint__propagate_disj([], []) --> [].
constraint__propagate_disj([Goal0|Goals0], [Goal|Goals]) -->
mode_info_dcg_get_instmap(InstMap0),
constraint__propagate_goal(Goal0, Goal),
mode_info_set_instmap(InstMap0),
constraint__propagate_disj(Goals0, Goals).
%-----------------------------------------------------------------------------%
:- pred constraint__propagate_cases(list(case), list(case),
mode_info, mode_info).
:- mode constraint__propagate_cases(in, out,
mode_info_di, mode_info_uo) is det.
constraint__propagate_cases([], []) --> [].
constraint__propagate_cases([case(Cons, Goal0)|Goals0],
[case(Cons, Goal)|Goals]) -->
mode_info_dcg_get_instmap(InstMap0),
constraint__propagate_goal(Goal0, Goal),
mode_info_set_instmap(InstMap0),
constraint__propagate_cases(Goals0, Goals).
%-----------------------------------------------------------------------------%
% constraint__propagate_conj detects the constraints in
% a conjunction and moves them to as early as possible
% in the list.
:- pred constraint__propagate_conj(list(hlds_goal), list(hlds_goal),
mode_info, mode_info).
:- mode constraint__propagate_conj(in, out,
mode_info_di, mode_info_uo) is det.
constraint__propagate_conj(Goals0, Goals) -->
=(ModeInfo0),
{ mode_info_get_delay_info(ModeInfo0, DelayInfo0) },
{ delay_info__enter_conj(DelayInfo0, DelayInfo1) },
mode_info_set_delay_info(DelayInfo1),
% mode_info_add_goals_live_vars(Goals0),
mode_info_dcg_get_instmap(InstMap0),
constraint__find_constraints(Goals0, Goals1, Constraints1),
mode_info_set_instmap(InstMap0),
% constraint__distribute_constraints(Constraints1, Goals1, Goals),
{ list__append(Constraints1, Goals1, Goals2) },
transform__reschedule_conj(Goals2, Goals),
=(ModeInfo1),
{ mode_info_get_delay_info(ModeInfo1, DelayInfo2) },
{ delay_info__leave_conj(DelayInfo2, DelayedGoals, DelayInfo3) },
mode_info_set_delay_info(DelayInfo3),
( { DelayedGoals = [] } ->
[]
;
{ error("constraint__propagate_conj") }
).
:- pred constraint__find_constraints(list(hlds_goal), list(hlds_goal),
list(constraint), mode_info, mode_info).
:- mode constraint__find_constraints(in, out, out,
mode_info_di, mode_info_uo) is det.
constraint__find_constraints([], [], []) --> [].
constraint__find_constraints([Goal0 | Goals0], Goals, Constraints) -->
mode_info_dcg_get_instmap(InstMap0),
constraint__propagate_goal(Goal0, Goal1),
% mode_info_dcg_get_instmap(InstMap1),
{ Goal1 = Goal1Goal - Goal1Info },
( { Goal1Goal = conj(Goal1List) } ->
{ list__append(Goal1List, Goals0, Goals1) },
mode_info_set_instmap(InstMap0),
constraint__find_constraints(Goals1, Goals, Constraints)
;
constraint__find_constraints(Goals0, Goals1, Constraints0),
=(ModeInfo),
( { constraint__is_constraint(Goal1Info, ModeInfo) } ->
{ Constraints = [Goal1 | Constraints0] },
{ Goals = Goals1 }
;
{ Constraints = Constraints0 },
{ Goals = [Goal1 | Goals1] }
)
).
%:- pred constraint__distribute_constraints(list(constraint), list(hlds_goal),
% list(hlds_goal), mode_info, mode_info).
%:- mode constraint__distribute_constraints(in, in, out,
% mode_info_di, mode_info_uo) is det.
%-----------------------------------------------------------------------------%
:- pred constraint__is_constraint(hlds_goal_info, mode_info).
:- mode constraint__is_constraint(in, mode_info_ui) is semidet.
constraint__is_constraint(GoalInfo, ModeInfo) :-
goal_info_get_determinism(GoalInfo, Det),
constraint__determinism(Det),
constraint__no_output_vars(GoalInfo, ModeInfo).
:- pred constraint__no_output_vars(hlds_goal_info, mode_info).
:- mode constraint__no_output_vars(in, mode_info_ui) is semidet.
constraint__no_output_vars(GoalInfo, ModeInfo) :-
goal_info_get_instmap_delta(GoalInfo, InstMapDelta),
goal_info_get_nonlocals(GoalInfo, Vars),
mode_info_get_module_info(ModeInfo, ModuleInfo),
mode_info_get_instmap(ModeInfo, InstMap),
instmap__no_output_vars(InstMap, InstMapDelta, Vars, ModuleInfo).
% constraint__determinism(Det) is true iff Det is
% a possible determinism of a constraint. The
% determinisms which use a model_semi code model
% are obviously constraints. Should erroneous
% also be treated as a constraint?
:- pred constraint__determinism(determinism).
:- mode constraint__determinism(in) is semidet.
constraint__determinism(semidet).
constraint__determinism(failure).
% constraint__determinism(erroneous). % maybe
%-----------------------------------------------------------------------------%
:- pred mode_info_write_string(string, mode_info, mode_info).
:- mode mode_info_write_string(in, mode_info_di, mode_info_uo) is det.
mode_info_write_string(Msg, ModeInfo0, ModeInfo) :-
mode_info_get_io_state(ModeInfo0, IOState0),
io__write_string(Msg, IOState0, IOState),
mode_info_set_io_state(ModeInfo0, IOState, ModeInfo).
:- pred mode_info_write_goal(hlds_goal, int, mode_info, mode_info).
:- mode mode_info_write_goal(in, in, mode_info_di, mode_info_uo) is det.
mode_info_write_goal(Goal, Indent, ModeInfo0, ModeInfo) :-
mode_info_get_io_state(ModeInfo0, IOState0),
% globals__io_lookup_bool_option(debug_modes, DoCheckPoint,
% IOState0, IOState1),
IOState0 = IOState1,
( semidet_succeed ->
mode_info_get_module_info(ModeInfo0, ModuleInfo),
mode_info_get_varset(ModeInfo0, VarSet),
hlds_out__write_goal(Goal, ModuleInfo, VarSet, no, Indent,
"\n", IOState1, IOState)
;
IOState = IOState1
),
mode_info_set_io_state(ModeInfo0, IOState, ModeInfo).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%