mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-20 08:19:28 +00:00
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.
183 lines
5.3 KiB
Mathematica
183 lines
5.3 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1996-1997, 2003-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.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: mode_debug.m.
|
|
% Main author: fjh.
|
|
%
|
|
% This module contains code for tracing the actions of the mode checker.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module check_hlds__mode_debug.
|
|
|
|
:- interface.
|
|
|
|
:- import_module check_hlds__mode_info.
|
|
:- import_module io.
|
|
|
|
% Print a debugging message which includes the port, message string,
|
|
% and the current instmap (but only if `--debug-modes' was enabled).
|
|
%
|
|
:- pred mode_checkpoint(port::in, string::in, mode_info::in, mode_info::out,
|
|
io::di, io::uo) is det.
|
|
|
|
:- type port
|
|
---> enter
|
|
; exit
|
|
; wakeup.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module check_hlds__modes.
|
|
:- import_module hlds__hlds_goal.
|
|
:- import_module hlds__hlds_module.
|
|
:- import_module hlds__instmap.
|
|
:- import_module hlds__passes_aux.
|
|
:- import_module libs__globals.
|
|
:- import_module libs__options.
|
|
:- import_module parse_tree__mercury_to_mercury.
|
|
:- import_module parse_tree__prog_data.
|
|
:- import_module parse_tree__prog_out.
|
|
|
|
:- import_module assoc_list.
|
|
:- import_module bool.
|
|
:- import_module list.
|
|
:- import_module map.
|
|
:- import_module std_util.
|
|
:- import_module term.
|
|
:- import_module varset.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% This code is used to trace the actions of the mode checker.
|
|
|
|
mode_checkpoint(Port, Msg, !ModeInfo, !IO) :-
|
|
mode_info_get_debug_modes(!.ModeInfo, DebugModes),
|
|
(
|
|
DebugModes = yes(debug_flags(Verbose, Minimal, Statistics)),
|
|
mode_checkpoint_write(Verbose, Minimal, Statistics, Port, Msg,
|
|
!ModeInfo, !IO)
|
|
;
|
|
DebugModes = no
|
|
).
|
|
|
|
:- pred mode_checkpoint_write(bool::in, bool::in, bool::in, port::in,
|
|
string::in, mode_info::in, mode_info::out, io::di, io::uo) is det.
|
|
|
|
mode_checkpoint_write(Verbose, Minimal, Statistics, Port, Msg, !ModeInfo,
|
|
!IO) :-
|
|
mode_info_get_errors(!.ModeInfo, Errors),
|
|
( Port = enter ->
|
|
io__write_string("Enter ", !IO),
|
|
Detail = yes
|
|
; Port = wakeup ->
|
|
io__write_string("Wake ", !IO),
|
|
Detail = no
|
|
; Errors = [] ->
|
|
io__write_string("Exit ", !IO),
|
|
Detail = yes
|
|
;
|
|
io__write_string("Delay ", !IO),
|
|
Detail = no
|
|
),
|
|
io__write_string(Msg, !IO),
|
|
(
|
|
Detail = yes,
|
|
io__write_string(":\n", !IO),
|
|
maybe_report_stats(Statistics, !IO),
|
|
maybe_flush_output(Statistics, !IO),
|
|
mode_info_get_instmap(!.ModeInfo, InstMap),
|
|
( instmap__is_reachable(InstMap) ->
|
|
instmap__to_assoc_list(InstMap, NewInsts),
|
|
mode_info_get_last_checkpoint_insts(!.ModeInfo,
|
|
OldInstMap),
|
|
mode_info_get_varset(!.ModeInfo, VarSet),
|
|
mode_info_get_instvarset(!.ModeInfo, InstVarSet),
|
|
write_var_insts(NewInsts, OldInstMap, VarSet,
|
|
InstVarSet, Verbose, Minimal, !IO)
|
|
;
|
|
io__write_string("\tUnreachable\n", !IO)
|
|
),
|
|
mode_info_set_last_checkpoint_insts(InstMap, !ModeInfo)
|
|
;
|
|
Detail = no
|
|
),
|
|
io__write_string("\n", !IO),
|
|
io__flush_output(!IO).
|
|
|
|
:- pred write_var_insts(assoc_list(prog_var, inst)::in, instmap::in,
|
|
prog_varset::in, inst_varset::in, bool::in, bool::in,
|
|
io::di, io::uo) is det.
|
|
|
|
write_var_insts([], _, _, _, _, _, !IO).
|
|
write_var_insts([Var - Inst | VarInsts], OldInstMap, VarSet, InstVarSet,
|
|
Verbose, Minimal, !IO) :-
|
|
instmap__lookup_var(OldInstMap, Var, OldInst),
|
|
(
|
|
(
|
|
identical_insts(Inst, OldInst)
|
|
;
|
|
Inst = OldInst
|
|
)
|
|
->
|
|
(
|
|
Verbose = yes,
|
|
io__write_string("\t", !IO),
|
|
mercury_output_var(Var, VarSet, no, !IO),
|
|
io__write_string(" ::", !IO),
|
|
io__write_string(" unchanged\n", !IO)
|
|
;
|
|
Verbose = no
|
|
)
|
|
;
|
|
io__write_string("\t", !IO),
|
|
mercury_output_var(Var, VarSet, no, !IO),
|
|
io__write_string(" ::", !IO),
|
|
(
|
|
Minimal = yes,
|
|
io__write_string(" changed\n", !IO)
|
|
;
|
|
Minimal = no,
|
|
io__write_string("\n", !IO),
|
|
mercury_output_structured_inst(Inst, 2, InstVarSet,
|
|
!IO)
|
|
)
|
|
),
|
|
write_var_insts(VarInsts, OldInstMap, VarSet, InstVarSet, Verbose,
|
|
Minimal, !IO).
|
|
|
|
% In the usual case of a C backend, this predicate allows us to
|
|
% conclude that two insts are identical without traversing them.
|
|
% Since the terms can be very large, this is a big gain; it can
|
|
% turn the complexity of printing a checking from quadratic in the
|
|
% number of variables live at the checkpoint (when the variables
|
|
% are e.g. all part of a single long list) to linear. The minor
|
|
% increase in the constant factor in cases where identical_insts fails
|
|
% is much easier to live with.
|
|
|
|
:- pred identical_insts((inst)::in, (inst)::in) is semidet.
|
|
|
|
identical_insts(_, _) :-
|
|
semidet_fail.
|
|
|
|
:- pragma foreign_proc("C",
|
|
identical_insts(InstA::in, InstB::in),
|
|
[will_not_call_mercury, promise_pure],
|
|
"
|
|
if (InstA == InstB) {
|
|
SUCCESS_INDICATOR = MR_TRUE;
|
|
} else {
|
|
SUCCESS_INDICATOR = MR_FALSE;
|
|
}
|
|
").
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|