Files
mercury/compiler/mark_static_terms.m
Zoltan Somogyi 8b8b3b7d3f Replace the some() HLDS goal with a more general scope() goal, which can be
Estimated hours taken: 12
Branches: main

Replace the some() HLDS goal with a more general scope() goal, which can be
used not just for existential quantification but also for other purposes.

The main such purposes are new goal types that allow the programmer
to annotate arbitrary goals, and not just whole procedure bodies, with the
equivalents of promise_pure/promise_semipure and promise_only_solution:

	promise_pure ( <impure/semipure goal> )
	promise_semipure ( <impure goal> )

	promise_equivalent_solutions [OutVar1, OutVar2] (
		<cc_multi/cc_nondet goal that computed OutVar1 & OutVar2>
	)

Both are intended to be helpful in writing constraint solvers, as well as in
other situations.

doc/reference_manual.texi:
	Document the new constructs.

library/ops.m:
	Add the keywords of the new constructs to the list of operators.
	Since they work similarly to the "some" operator, they have the same
	precedence.

compiler/hlds_goal.m:
	Replace the some(Vars, SubGoal) HLDS construct, with its optional
	keep_this_commit attribute, with the new scope(Reason, SubGoal)
	construct. The Reason argument may say that this scope is an
	existential quantification, but it can also say that it represents
	a purity promise, the introduction of a single-solution context
	with promise_equivalent_solutions, or a decision by a compiler pass.

	It can also say that the scope represents a set of goals that all arise
	from the unraveling of a unification between a variable and a ground
	term. This was intended to speed up mode checking by significantly
	reducing the number of delays and wakeups, but the cost of the scopes
	themselves turned out to be bigger than the gain in modechecking speed.

	Update the goal_path_step type to refer to scope goals instead of just
	existential quantification.

compiler/prog_data.m:
	Add new function symbols to the type we use to represent goals in items
	to stand for the new Mercury constructs.

compiler/prog_io_goal.m:
	Add code to read in the new language constructs.

compiler/prog_io_util.m:
	Add a utility predicate for use by the new code in prog_io_goal.m.

compiler/make_hlds.m:
	Convert the item representation of the new constructs to the HLDS
	representation.

	Document how the from_ground_term scope reason would work, but do not
	enable the code.

compiler/purity.m:
	When checking the purity of goals, respect the new promise_pure and
	promise_semipure scopes. Generate warnings if such scopes are
	redundant.

compiler/det_analysis.m:
	Make the insides of promise_equivalent_solutions goals single solution
	contexts.

compiler/det_report.m:
	Provide mechanisms for reporting inappropriate usage of
	promise_equivalent_solutions goals.

compiler/instmap.m:
	Add a utility predicate for use by one of the modules above.

compiler/deep_profiling.m:
	Use one of the new scope reasons to prevent simplify from optimizing
	away commits of goals that have been made impure, instead of the old
	keep_this_commit goal feature.

compiler/modes.m:
	Handle from_ground_term scopes when present; for now, they won't be
	present, since make_hlds isn't creating them.

compiler/options.m:
	Add two new compiler options, for use by implementors only, to allow
	finer control over the amount of output one gets with --debug-modes.
	(I used them when debugging the performance of the from_ground_term
	scope reason.) The options are --debug-modes-minimal and
	--debug-modes-verbose.

compiler/handle_options.m:
	Make the options that are meaningful only in the presence of
	--debug-modes imply --debug-modes, since this allows more convenient
	(shorter) invocations.

compiler/mode_debug.m:
	Respect the new options when deciding how much data to print
	when debugging of the mode checking process is enabled.

compiler/switch_detect.m:
	Rename a predicate to make it differ from another predicate by more
	than just its arity.

compiler/passes_aux.m:
	Bring this module up to date with our current style guidelines,
	by using state variable syntax where appropriate.

compiler/*.m:
	Minor changes to conform to the change in the HLDS and/or parse tree
	goal type.

mdbcomp/program_representation.m:
	Rename the some goal to the scope goal, and the same for path steps,
	to keep them in sync with the HLDS.

browser/declarative_tree.m:
	Conform to the change in goal representations.

tests/hard_coded/promise_equivalent_solutions_test.{m,exp}:
	A new test case to test the handling of the
	promise_equivalent_solutions construct.

tests/hard_coded/Mmakefile:
	Enable the new test.

tests/hard_coded/purity/promise_pure_test.{m,exp}:
	A new test case to test the handling of the promise_pure and
	promise_semipure constructs.

tests/hard_coded/purity/Mmakefile:
	Enable the new test.

tests/invalid/promise_equivalent_solutions.{m,err_exp}:
	A new test case to test the error messages for improper use of the
	promise_pure and promise_semipure constructs.

tests/invalid/Mmakefile:
	Enable the new test.
2005-03-24 05:34:41 +00:00

218 lines
7.3 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 2000-2005 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.
%-----------------------------------------------------------------------------%
% This module traverses the HLDS, updating the `how_to_construct'
% field of construction unifications. For each construction which
% can be done statically, i.e. whose arguments are all static,
% it replaces this field with `construct_statically'.
% This field is then used by the MLDS back-end to determine when it can
% generate static initialized constants rather than using
% new_object() MLDS statements.
% Main author: fjh.
:- module ml_backend__mark_static_terms.
:- interface.
:- import_module hlds__hlds_module.
:- import_module hlds__hlds_pred.
:- pred mark_static_terms(module_info::in, proc_info::in, proc_info::out)
is det.
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module hlds__hlds_data.
:- import_module hlds__hlds_goal.
:- import_module parse_tree__prog_data.
:- import_module bool.
:- import_module list.
:- import_module map.
%
% As we traverse the goal, we keep track of which variables are static at
% this point, and for each such variable, we keep information on how to
% construct it.
%
:- type static_info == map(prog_var, static_cons).
:- import_module hlds__hlds_goal.
:- import_module int.
:- import_module list.
:- import_module require.
:- import_module std_util.
mark_static_terms(_ModuleInfo, !Proc) :-
% The ModuleInfo argument is there just for passes_aux
proc_info_goal(!.Proc, Goal0),
map__init(StaticInfo0),
goal_mark_static_terms(Goal0, Goal, StaticInfo0, _StaticInfo),
proc_info_set_goal(Goal, !Proc).
:- pred goal_mark_static_terms(hlds_goal::in, hlds_goal::out,
static_info::in, static_info::out) is det.
goal_mark_static_terms(GoalExpr0 - GoalInfo, GoalExpr - GoalInfo, !SI) :-
goal_expr_mark_static_terms(GoalExpr0, GoalExpr, !SI).
:- pred goal_expr_mark_static_terms(hlds_goal_expr::in, hlds_goal_expr::out,
static_info::in, static_info::out) is det.
goal_expr_mark_static_terms(conj(Goals0), conj(Goals), !SI) :-
conj_mark_static_terms(Goals0, Goals, !SI).
goal_expr_mark_static_terms(par_conj(Goals0), par_conj(Goals), !SI) :-
% it's OK to treat parallel conjunctions as if they were
% sequential here, since if we mark any variables as
% static, the computation of those variables will be
% done at compile time.
conj_mark_static_terms(Goals0, Goals, !SI).
goal_expr_mark_static_terms(disj(Goals0), disj(Goals), !SI) :-
% we revert to the original static_info at the end of branched goals
disj_mark_static_terms(Goals0, Goals, !.SI).
goal_expr_mark_static_terms(switch(A, B, Cases0), switch(A, B, Cases), !SI) :-
% we revert to the original static_info at the end of branched goals
cases_mark_static_terms(Cases0, Cases, !.SI).
goal_expr_mark_static_terms(not(Goal0), not(Goal), !SI) :-
% we revert to the original static_info at the end of the negation
goal_mark_static_terms(Goal0, Goal, !.SI, _SI).
goal_expr_mark_static_terms(scope(A, Goal0), scope(A, Goal), !SI) :-
goal_mark_static_terms(Goal0, Goal, !SI).
goal_expr_mark_static_terms(if_then_else(A, Cond0, Then0, Else0),
if_then_else(A, Cond, Then, Else), SI0, SI0) :-
% we run the Cond and the Then in sequence,
% and we run the Else in parallel with that,
% and then we throw away the static_infos we computed
% and revert to the original static_info at the end,
% since this was a branched goal.
goal_mark_static_terms(Cond0, Cond, SI0, SI_Cond),
goal_mark_static_terms(Then0, Then, SI_Cond, _SI_Then),
goal_mark_static_terms(Else0, Else, SI0, _SI_Else).
goal_expr_mark_static_terms(Goal @ call(_, _, _, _, _, _), Goal, !SI).
goal_expr_mark_static_terms(Goal @ generic_call(_, _, _, _), Goal, !SI).
goal_expr_mark_static_terms(unify(LHS, RHS, Mode, Unification0, Context),
unify(LHS, RHS, Mode, Unification, Context), !SI) :-
unification_mark_static_terms(Unification0, Unification, !SI).
goal_expr_mark_static_terms(Goal @ foreign_proc(_, _, _, _, _, _), Goal, !SI).
goal_expr_mark_static_terms(shorthand(_), _, !SI) :-
% these should have been expanded out by now
error("fill_expr_slots: unexpected shorthand").
:- pred conj_mark_static_terms(hlds_goals::in, hlds_goals::out,
static_info::in, static_info::out) is det.
conj_mark_static_terms(Goals0, Goals, !SI) :-
list__map_foldl(goal_mark_static_terms, Goals0, Goals, !SI).
:- pred disj_mark_static_terms(hlds_goals::in, hlds_goals::out,
static_info::in) is det.
disj_mark_static_terms([], [], _).
disj_mark_static_terms([Goal0 | Goals0], [Goal | Goals], SI0) :-
% we throw away the static_info obtained after each branch
goal_mark_static_terms(Goal0, Goal, SI0, _SI),
disj_mark_static_terms(Goals0, Goals, SI0).
:- pred cases_mark_static_terms(list(case)::in, list(case)::out,
static_info::in) is det.
cases_mark_static_terms([], [], _SI0).
cases_mark_static_terms([Case0 | Cases0], [Case | Cases], SI0) :-
Case0 = case(ConsId, Goal0),
Case = case(ConsId, Goal),
% we throw away the static_info obtained after each branch
goal_mark_static_terms(Goal0, Goal, SI0, _SI),
cases_mark_static_terms(Cases0, Cases, SI0).
:- pred unification_mark_static_terms(unification::in, unification::out,
static_info::in, static_info::out) is det.
unification_mark_static_terms(Unification0, Unification,
StaticVars0, StaticVars) :-
(
Unification0 = construct(Var, ConsId, ArgVars, D,
HowToConstruct0, F, G),
(
% if all the arguments are static,
% then the newly constructed variable
% is static too
CheckStaticArg = (pred(V::in, C::out) is semidet :-
map__search(StaticVars0, V, C)),
list__map(CheckStaticArg, ArgVars, StaticArgs)
->
HowToConstruct = construct_statically(StaticArgs),
map__det_insert(StaticVars0, Var,
static_cons(ConsId, ArgVars, StaticArgs),
StaticVars)
;
HowToConstruct = HowToConstruct0,
StaticVars = StaticVars0
),
( HowToConstruct = HowToConstruct0 ->
% this is a minor optimization to improve the
% efficiency of the compiler: don't bother
% allocating memory if we don't need to
Unification = Unification0
;
Unification = construct(Var, ConsId, ArgVars, D,
HowToConstruct, F, G)
)
;
Unification0 = deconstruct(_Var, _ConsId, _ArgVars, _UniModes,
_CanFail, _CanCGC),
Unification = Unification0,
StaticVars = StaticVars0
% (
% % if the variable being deconstructed is static,
% % and the deconstruction cannot fail,
% % then the newly extracted argument variables
% % are static too
% % (XXX is the "cannot fail" bit really necessary?)
% map__search(StaticVars0, Var, Data),
% CanFail = cannot_fail
% ->
% XXX insert ArgVars into StaticVars0
% ;
% StaticVars = StaticVars0
% )
;
Unification0 = assign(TargetVar, SourceVar),
Unification = Unification0,
(
% if the variable being assign from is static,
% then the variable being assigned to is static too
map__search(StaticVars0, SourceVar, Data)
->
map__det_insert(StaticVars0, TargetVar, Data,
StaticVars)
;
StaticVars = StaticVars0
)
;
Unification0 = simple_test(_, _),
Unification = Unification0,
StaticVars = StaticVars0
;
Unification0 = complicated_unify(_, _, _),
Unification = Unification0,
StaticVars = StaticVars0
).