mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-20 00:15:27 +00:00
Estimated hours taken: 8
Branches: main
Improve the error messages generated for determinism errors involving committed
choice contexts. Previously, we printed a message to the effect that e.g.
a cc pred is called in context that requires all solutions, but we didn't say
*why* the context requires all solutions. We now keep track of all the goals
to the right that could fail, since it is these goals that may reject the first
solution of a committed choice goal.
The motivation for this diff was the fact that I found that locating the
failing goal can be very difficult if the conjunction to the right is
a couple of hundred lines long. This would have been a nontrivial problem,
since (a) unifications involving values of user-defined types are committed
choice goals, and (b) we can expect uses of user-defined types to increase.
compiler/det_analysis.m:
Keep track of goals to the right of the current goal that could fail,
and include them in the error representation if required.
compiler/det_report.m:
Include the list of failing goals to the right in the representations
of determinism errors involving committed committed choice goals.
Convert the last part of this module that wasn't using error_util
to use error_util. Make most parts of this module just construct
error message specifications; print those specifications (using
error_util) in only a few places.
compiler/hlds_out.m:
Add a function for use by the new code in det_report.m.
compiler/error_util.m:
Add a function for use by the new code in det_report.m.
compiler/error_util.m:
compiler/compiler_util.m:
Error_util is still changing reasonably often, and yet it is
included in lots of modules, most of which need only a few simple
non-parse-tree-related predicates from it (e.g. unexpected).
Move those predicates to a new module, compiler_util.m. This also
eliminates some undesirable dependencies from libs to parse_tree.
compiler/libs.m:
Include compiler_util.m.
compiler/notes/compiler_design.html:
Document compiler_util.m, and fix the documentation of some other
modules.
compiler/*.m:
Import compiler_util instead of or in addition to error_util.
To make this easier, consistently use . instead of __ for module
qualifying module names.
tests/invalid/det_errors_cc.{m,err_exp}:
Add this new test case to test the error messages for cc contexts.
tests/invalid/det_errors_deet.{m,err_exp}:
Add this new test case to test the error messages for unifications
inside function symbols.
tests/invalid/Mmakefile:
Add the new test cases.
tests/invalid/det_errors.err_exp:
tests/invalid/magicbox.err_exp:
Change the expected output to conform to the change in det_report.m,
which is now more consistent.
184 lines
6.7 KiB
Mathematica
184 lines
6.7 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1995-1996, 2004-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: graph_colour.m
|
|
% main author: conway.
|
|
%
|
|
% This file contains functionality to find a 'good' colouring of a graph.
|
|
% The predicate group_elements(set(set(T)), set(set(T))),
|
|
% takes a set of sets each containing elements that touch, and returns
|
|
% a set of sets each containing elements that can be assigned the same
|
|
% colour, ensuring that touching elements have different colours.
|
|
% ("Good" means using as few colours as possible.)
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module libs__graph_colour.
|
|
|
|
:- interface.
|
|
|
|
:- import_module set.
|
|
|
|
:- pred group_elements(set(set(T))::in, set(set(T))::out) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module libs.compiler_util.
|
|
|
|
:- import_module list.
|
|
:- import_module require.
|
|
|
|
group_elements(Constraints, Colours) :-
|
|
set__power_union(Constraints, AllVars),
|
|
set__init(EmptySet),
|
|
set__delete(Constraints, EmptySet, Constraints1),
|
|
set__to_sorted_list(Constraints1, ConstraintList),
|
|
find_all_colours(ConstraintList, AllVars, ColourList),
|
|
set__list_to_set(ColourList, Colours).
|
|
|
|
% % performance reducing sanity check....
|
|
% (
|
|
% set__power_union(Colours, AllColours),
|
|
% (set__member(Var, AllVars) => set__member(Var, AllColours))
|
|
% ->
|
|
% error("group_elements: sanity check failed")
|
|
% ;
|
|
% true
|
|
% ).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Iterate the assignment of a new colour until all constraints
|
|
% are satisfied.
|
|
%
|
|
:- pred find_all_colours(list(set(T))::in, set(T)::in,
|
|
list(set(T))::out) is det.
|
|
|
|
find_all_colours(ConstraintList, Vars, ColourList) :-
|
|
(
|
|
ConstraintList = [],
|
|
ColourList = []
|
|
;
|
|
ConstraintList = [_ | _],
|
|
next_colour(Vars, ConstraintList, RemainingConstraints, Colour),
|
|
set__difference(Vars, Colour, RestVars),
|
|
find_all_colours(RemainingConstraints, RestVars, ColourList0),
|
|
ColourList = [Colour | ColourList0]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred next_colour(set(T)::in, list(set(T))::in,
|
|
list(set(T))::out, set(T)::out) is det.
|
|
|
|
next_colour(Vars0, ConstraintList, Remainder, SameColour) :-
|
|
% Check if there are any constraints left to be satisfied.
|
|
(
|
|
ConstraintList = [_ | _],
|
|
% Select a variable to assign a colour, ...
|
|
choose_var(Vars0, Var, Vars1),
|
|
|
|
% ... and divide the constraints into those that
|
|
% may be the same colour as that var and those
|
|
% that may not.
|
|
divide_constraints(Var, ConstraintList, WereContaining, NotContaining,
|
|
Vars1, RestVars),
|
|
(
|
|
% See if there are sets that can share a colour with the
|
|
% selected var.
|
|
NotContaining = [_ | _],
|
|
( set__empty(RestVars) ->
|
|
% There were no variables left that could share a colour,
|
|
% so create a singleton set containing this variable.
|
|
set__singleton_set(SameColour, Var),
|
|
ResidueSets = NotContaining
|
|
;
|
|
% If there is at least one variable that can share a colour
|
|
% with the selected variable, then recursively use the
|
|
% remaining constraints to assign a colour to one of the
|
|
% remaining vars, and assemble the constraint residues.
|
|
next_colour(RestVars, NotContaining, ResidueSets, SameColour0),
|
|
|
|
% Add this variable to the variables of the current colour.
|
|
set__insert(SameColour0, Var, SameColour)
|
|
)
|
|
;
|
|
NotContaining = [],
|
|
% There were no more constraints which could be satisfied
|
|
% by assigning any variable a colour the same as the current
|
|
% variable, so create a signleton set with the current var,
|
|
% and assign the residue to the empty set.
|
|
set__singleton_set(SameColour, Var),
|
|
ResidueSets = []
|
|
),
|
|
% The remaining constraints are the residue sets that could not be
|
|
% satisfied by assigning any variable to the current colour, and the
|
|
% constraints that were already satisfied by the assignment of the
|
|
% current variable to this colour.
|
|
list__append(ResidueSets, WereContaining, Remainder)
|
|
;
|
|
% If there were no constraints, then no colours were needed.
|
|
ConstraintList = [],
|
|
Remainder = [],
|
|
set__init(SameColour)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Divide_constraints takes a var and a list of sets of var, and divides
|
|
% the list into two lists: a list of sets containing the given variable
|
|
% and a list of sets not containing that variable. The sets in the list
|
|
% containing the variable have that variable removed. Additionally, a set
|
|
% of variables is threaded through the computation, and any variables that
|
|
% were in sets that also contained the given variables are removed from
|
|
% the threaded set.
|
|
%
|
|
:- pred divide_constraints(T::in, list(set(T))::in,
|
|
list(set(T))::out, list(set(T))::out, set(T)::in, set(T)::out) is det.
|
|
|
|
divide_constraints(_Var, [], [], [], !Vars).
|
|
divide_constraints(Var, [S | Ss], C, NC, !Vars) :-
|
|
divide_constraints(Var, Ss, C0, NC0, !Vars),
|
|
( set__member(Var, S) ->
|
|
set__delete(S, Var, T),
|
|
( set__empty(T) ->
|
|
C = C0
|
|
;
|
|
C = [T | C0]
|
|
),
|
|
NC = NC0,
|
|
set__difference(!.Vars, T, !:Vars)
|
|
;
|
|
C = C0,
|
|
NC = [S | NC0]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Choose_var/3, given a set of variables, chooses one, returns it
|
|
% and the set with that variable removed.
|
|
%
|
|
:- pred choose_var(set(T)::in, T::out, set(T)::out) is det.
|
|
|
|
choose_var(Vars0, Var, Vars) :-
|
|
( set__remove_least(Vars0, VarPrime, VarsPrime) ->
|
|
Var = VarPrime,
|
|
Vars = VarsPrime
|
|
;
|
|
unexpected(this_file, "choose_var: no vars!")
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- func this_file = string.
|
|
|
|
this_file = "graph_colour.m".
|
|
|
|
%-----------------------------------------------------------------------------%
|