Files
mercury/compiler/field_access.m
Zoltan Somogyi 253378fe21 Improve error messages for malformed lambda expressions.
Do this by insisting that terms whose top level(s) look like lambda
expressions should be parsed as lambda expressions, and return error
messages if that parsing attempt fails.

I did this is in several commits to modules in the parse_tree.m package
that translated terms to the parse tree, i.e. to goal_exprs. This diff
does the same thing for lambda expressions.

NEWS:
    Document this change, as well as other, similar changes in the recent past.

compiler/superhomogeneous.m:
    When translating a var-functor unification, which has the form
    X = f(Y1, ..., Yn), to the HLDS, we parse the right hand side term,
    looking for Mercury constructs such as lambda expressions and
    field accesses. We used to do this by checking if the RHS was
    a well-formed version of the construct we were looking for,
    and falling back to parsing it as an ordinary term. That meant that
    terms that the programmer *intended* to be lambda expressions,
    but which contained a syntax error, were not diagnosed during the
    construction of the HLDS, but during a later pass. Virtually always,
    that later pass was typechecking. Since in such cases, the typechecker
    is given terms whose function symbols were *intended* to be the "keywords"
    of Mercury constructs (to the extent that Mercury *has* keywords),
    not the names of data constructors or predicates, those error messages
    were almost always confusing, and at best an *indirect* indication
    of the actual problem with the code.

    This diff changes things so that when the top function symbol on
    the RHS of a unification is a Mercury "keyword" used only in lambda
    expressions, we commit to parsing it as such. If the parsing fails,
    we now generate an error message that describes the problem *directly*.

    To make the task of generating good error messages easier, and to make it
    possible to generate *consistent* error messages for the same error
    in different contexts, unify the four separate pieces of code that
    *used* to parse different kinds of lambda expressions. These four
    pieces of code used to parse

    - predicates defined using DCG notation;
    - predicates defined using non-DCG notation;
    - functions that used the default function mode and determinism; and
    - functions that did not use the default function mode and determinism.

    The unified code allows a function lambda expression to omit the
    default argument modes *independently* of whether it omits the default
    determinism, and vice versa. The old code did not do that: if you didn't
    explicitly specify one, you couldn't explicitly specify the other either,
    probably because it would have required two more pieces of code.

    The language reference manual says, in section 8.1:

        As with ‘:- func’ declarations, if the modes and determinism
        of the function are omitted in a higher-order function term, then
        the modes default to ‘in’ for the arguments, ‘out’ for
        the function result, and the determinism defaults to ‘det’.

    This text is vague on whether the modes and determinism are *allowed*
    to be omitted independently. The old behavior was a strict reading
    of this text. The new behavior follows the most liberal possible reading,
    and thus allows all the behaviors that the old one did, and more.

    Integrate the test for big integers that are too big to be represented
    with the conversion of all ordinary functors into cons_ids, both to
    simplify the code, and to speed it up. In the process, improve the
    error message for too-big integers.

compiler/add_clause.m:
    Don't try to generate warnings for singleton variables in a clause
    if the clause had syntax errors. Part of the parser's recovery from
    those syntax errors (e.g. when they occur in lambda expressions' clause
    heads) may have included not translating parts of the original term
    into the parsed clause body, so any such warnings could be misleading.

    Such warnings could also be correct, but I expect most programmers
    would prefer to miss some correct singleton warnings while the
    syntax errors are present in their code, and get them only once
    they have fixed those syntax errors, than to have to wade through
    misleading errors messages for the initial syntax error to get to
    the real ones.

compiler/field_access.m:
    Clarify the text of an error message.

    Delete a predicate that superhomogeneous.m no longer needs.

compiler/mercury_compile_front_end.m:
    Don't proceed past typechecking if the parser found syntax errors,
    since any errors we find later, e.g. as part of mode checking,
    are quite likely to be avalanche errors caused by those syntax errors.

    The reason why this wasn't a problem in the past is that when
    the program contained malformed uses of builtin constructs such as
    lambda expressions and field accesses, the parser used to simply
    transform the terms containing the malformed constructs into the arguments
    of call goals and unifications, and it was the typechecker that discovered
    that these function symbols did not match any declared type.
    The "type errors" generated for such problems told the compiler
    not to run later compiler passes to prevent avalanche errors.

    We now report syntax errors earlier, during the construction of the HLDS,
    and it is entirely possible that the parser's recovery from those errors
    leaves *no* type errors to be reported. This is why we need to make
    the presence of syntax errors in a predicate block the invocation
    of later passes.

compiler/typecheck.m:
    Return whether the predicates we typechecked had syntax errors,
    to make the change in mercury_compile_front_end.m possible.

compiler/state_var.m:
    If a lambda expression has two or more arguments of the form !X,
    we now print an error message for each, with each message naming the bad
    state variable and giving the context of the argument. We used to
    print only a single message saying that *some* argument misused
    state variables this way. Similarly, we now print the correct context
    for !X appearing as a function result, whether in a lambda expression
    or at the top level of a clause.

    The actual printing of those error messages takes place elsewhere,
    but change the utility predicates exported by state_var.m to make
    the change possible.

    Change the error message we generate for !X appearing as a function
    result, to avoid suggesting a possible repair that is virtually never
    the right repair.

compiler/mode_util.m:
    Delete a predicate that was used only by superhomogeneous.m; an updated
    version of that predicate is now in superhomogeneous.m itself.

compiler/module_qual.m:
compiler/module_qual.qualify_items.m:
    Instead of exporting predicates to module qualify the whole list of
    argument modes of a lambda expression, export predicates that module
    qualify only one such mode, since that is what superhomogeneous.m
    now wants.

compiler/hlds_clauses.m:
    Add access predicates for fields of the clauses_info type that previously
    did not have them, including the one that records the presence of syntax
    errors.

    Put related fields of the clauses_info together.

    Group both the declarations and the definitions of the access predicates
    together, and put them in the same order as the fields themselves.

compiler/add_class.m:
compiler/add_pred.m:
compiler/add_pragma_type_spec.m:
compiler/higher_order.m:
compiler/hlds_pred.m:
compiler/unify_proc.m:
    Conform to the changes above.

tests/invalid/lambda_syntax_error.err_exp:
    Print one error message for each of the four malformed lambda expressions
    in this test case, instead of the 13 avalanche messages we used to get
    from the typechecker, which tried to interpret the malformed lambda
    expression heads as calls, unifications etc.

    The motivation for this diff was a set of similar set of avalanche error
    messages for a real-life syntax error in a lambda expression in a change
    I worked on a while ago.

tests/benchmarks/deriv.{m,exp}:
    This test case used to use "^" as a data constructor (to represent
    raising X to the Nth power). Since the parser now insists on treating it
    as a field access operator, replace "^" with "power". (This test has
    the excuse of predating the addition of field access syntax to Mercury
    by several years.)

tests/dppd/grammar_impl.m:
    This test case used to define a type which contained "is" as a data
    constructor. After this diff, this is no longer allowed (as part of
    "is detism", it looks to the parser like the top constructor in a
    lambda expression head, and therefore as the top constructor of
    the lambda expression as a whole if it has no body), so add the type
    name as a prefix to this data constructor. To future-proof the test case,
    do the same with the several other data constructors that also duplicate
    the names of Mercury keywords.

tests/hard_coded/pprint_test2.{m,exp}:
tests/hard_coded/write.{m,exp}:
tests/hard_coded/write_binary.{m,exp}:
    These test cases used to define a type which contained ":-" as a data
    constructor. After this diff, this is no longer allowed, so replace them
    with "?-". (The tests check how io.m formats terms whose data constructor
    is an operator, so the replacement needs to be an operator, and Mercury
    syntax does not use "?-"; library/ops.m lists it as an operator only
    because it is an operator in Prolog.)

    Update write_binary.m to conform to our current style guide, and to avoid
    using the recently-deprecated io.make_temp.

tests/invalid/invalid_int.err_exp:
    Expect the updated text of the error message for a too-big
    integer constant.

tests/invalid/record_syntax_error.err_exp:
    Expect the updated text of the error message for a malformed field name.

tests/invalid/state_vars_test3.err_exp:
    Expect the updated text of the error message for a !X appearing
    as the result of a lambda function.

tests/invalid/state_vars_test4.err_exp:
    Expect the error message for a !X lambda argument, but (since we now
    stop after typechecking in the presence of such syntax errors),
    don't expect the avalanche error messages we used to print from the
    mode checker.
2016-05-20 05:47:36 +10:00

325 lines
14 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1993-2006, 2008-2012 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: field_access.
%
% This submodule of make_hlds handles the declarations of fields
%
%-----------------------------------------------------------------------------%
:- module hlds.make_hlds.field_access.
:- interface.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_module.
:- import_module hlds.make_hlds.qual_info.
:- import_module hlds.make_hlds.state_var.
:- import_module mdbcomp.
:- import_module mdbcomp.sym_name.
:- import_module parse_tree.error_util.
:- import_module parse_tree.maybe_error.
:- import_module parse_tree.prog_data.
:- import_module assoc_list.
:- import_module list.
:- import_module pair.
%-----------------------------------------------------------------------------%
:- type field_list == assoc_list(sym_name, list(prog_term)).
% Expand a field update goal into a list of goals which each get or set
% one level of the structure.
%
% A field update goal:
% Term = Term0 ^ module_info ^ ctors := Ctors
% is expanded into
% V_1 = Term0 ^ module_info,
% V_3 = V_2 ^ ctors := Ctors,
% Term = Term0 ^ module_info := V_3.
%
:- pred expand_set_field_function_call(prog_context::in,
unify_main_context::in, unify_sub_contexts::in, field_list::in,
prog_var::in, prog_var::in, prog_var::in, cons_id::out,
pair(cons_id, unify_sub_contexts)::out, hlds_goal::out,
svar_state::in, svar_state::out, svar_store::in, svar_store::out,
prog_varset::in, prog_varset::out,
module_info::in, module_info::out, qual_info::in, qual_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
% Expand a field extraction goal into a list of goals which each get one
% level of the structure.
%
% A field extraction goal:
% := (ModuleName, ^ module_info ^ sub_info ^ module_name,
% DCG_in, DCG_out).
% is expanded into
% DCG_out = DCG_in,
% V_1 = DCG_out ^ module_info
% V_2 = V_1 ^ sub_info,
% ModuleName = V_2 ^ module_name.
%
:- pred expand_dcg_field_extraction_goal(prog_context::in,
unify_main_context::in, unify_sub_contexts::in, field_list::in,
prog_var::in, prog_var::in, prog_var::in, cons_id::out,
pair(cons_id, unify_sub_contexts)::out, hlds_goal::out,
svar_state::in, svar_state::out, svar_store::in, svar_store::out,
prog_varset::in, prog_varset::out,
module_info::in, module_info::out, qual_info::in, qual_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
% Expand a field extraction function call into a list of goals which
% each get one level of the structure.
%
% A field extraction goal:
% ModuleName = Info ^ module_info ^ sub_info ^ module_name
% is expanded into
% V_1 = Info ^ module_info,
% V_2 = V_1 ^ sub_info,
% ModuleName = V_2 ^ module_name.
%
:- pred expand_get_field_function_call(prog_context::in,
unify_main_context::in, unify_sub_contexts::in, field_list::in,
prog_var::in, prog_var::in, purity::in, cons_id::out,
pair(cons_id, unify_sub_contexts)::out, hlds_goal::out,
svar_state::in, svar_state::out, svar_store::in, svar_store::out,
prog_varset::in, prog_varset::out,
module_info::in, module_info::out, qual_info::in, qual_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
:- pred parse_field_list(prog_term::in, prog_varset::in,
list(format_component)::in, maybe1(field_list)::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module hlds.hlds_data.
:- import_module hlds.hlds_pred.
:- import_module hlds.make_hlds.superhomogeneous.
:- import_module parse_tree.parse_sym_name.
:- import_module parse_tree.parse_tree_out_term.
:- import_module int.
:- import_module require.
:- import_module term.
:- import_module varset.
%-----------------------------------------------------------------------------%
expand_set_field_function_call(Context, MainContext, SubContext0, FieldNames,
FieldValueVar, TermInputVar, TermOutputVar, Functor, FieldSubContext,
Goal, !SVarState, !SVarStore, !VarSet,
!ModuleInfo, !QualInfo, !Specs) :-
expand_set_field_function_call_2(Context, MainContext, SubContext0,
FieldNames, FieldValueVar, TermInputVar, TermOutputVar, Functor,
FieldSubContext, Goals,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs),
goal_info_init(Context, GoalInfo),
conj_list_to_goal(Goals, GoalInfo, Goal).
:- pred expand_set_field_function_call_2(prog_context::in,
unify_main_context::in, unify_sub_contexts::in, field_list::in,
prog_var::in, prog_var::in, prog_var::in, cons_id::out,
pair(cons_id, unify_sub_contexts)::out, list(hlds_goal)::out,
svar_state::in, svar_state::out, svar_store::in, svar_store::out,
prog_varset::in, prog_varset::out,
module_info::in, module_info::out, qual_info::in, qual_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
expand_set_field_function_call_2(_, _, _, [], _, _, _, _, _, _,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs) :-
unexpected($module, $pred, "empty list of field names").
expand_set_field_function_call_2(Context, MainContext, SubContext0,
[FieldName - FieldArgs | FieldNames], FieldValueVar,
TermInputVar, TermOutputVar, Functor, FieldSubContext, Goals,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs) :-
make_fresh_arg_vars_subst_svars(FieldArgs, FieldArgVars, !VarSet,
!SVarState, !Specs),
(
FieldNames = [_ | _],
varset.new_var(SubTermInputVar, !VarSet),
varset.new_var(SubTermOutputVar, !VarSet),
SetArgs = FieldArgVars ++ [TermInputVar, SubTermOutputVar],
construct_field_access_function_call(set, Context,
MainContext, SubContext0, FieldName, TermOutputVar,
SetArgs, purity_pure, Functor, UpdateGoal, !QualInfo),
% Extract the field containing the field to update.
construct_field_access_function_call(get, Context,
MainContext, SubContext0, FieldName, SubTermInputVar,
FieldArgVars ++ [TermInputVar], purity_pure, _, GetSubFieldGoal,
!QualInfo),
% Recursively update the field.
SubTermInputArgNumber = 2 + list.length(FieldArgs),
TermInputContext = unify_sub_context(Functor, SubTermInputArgNumber),
SubContext = [TermInputContext | SubContext0],
expand_set_field_function_call_2(Context, MainContext,
SubContext, FieldNames, FieldValueVar, SubTermInputVar,
SubTermOutputVar, _, FieldSubContext, Goals0,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs),
Goals1 = [GetSubFieldGoal | Goals0] ++ [UpdateGoal]
;
FieldNames = [],
SetArgs = FieldArgVars ++ [TermInputVar, FieldValueVar],
construct_field_access_function_call(set, Context,
MainContext, SubContext0, FieldName, TermOutputVar,
SetArgs, purity_pure, Functor, Goal, !QualInfo),
FieldSubContext = Functor - SubContext0,
Goals1 = [Goal]
),
ArgContext = ac_functor(Functor, MainContext, SubContext0),
goal_info_init(Context, GoalInfo),
conj_list_to_goal(Goals1, GoalInfo, Conj0),
insert_arg_unifications(FieldArgVars, FieldArgs, Context, ArgContext,
Conj0, Conj, !SVarState, !SVarStore, !VarSet,
!ModuleInfo, !QualInfo, !Specs),
svar_goal_to_conj_list(Conj, Goals, !SVarStore).
expand_dcg_field_extraction_goal(Context, MainContext, SubContext, FieldNames,
FieldValueVar, TermInputVar, TermOutputVar, Functor, FieldSubContext,
Goal, !SVarState, !SVarStore, !VarSet,
!ModuleInfo, !QualInfo, !Specs) :-
% Unify the DCG input and output variables.
make_atomic_unification(TermOutputVar, rhs_var(TermInputVar), Context,
MainContext, SubContext, UnifyDCG, !QualInfo),
% Process the access function as a get function on the output DCG variable.
expand_get_field_function_call_2(Context, MainContext, SubContext,
FieldNames, FieldValueVar, TermOutputVar, purity_pure,
Functor, FieldSubContext, Goals1,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs),
Goals = [UnifyDCG | Goals1],
goal_info_init(Context, GoalInfo),
conj_list_to_goal(Goals, GoalInfo, Goal).
expand_get_field_function_call(Context, MainContext, SubContext0, FieldNames,
FieldValueVar, TermInputVar, Purity, Functor, FieldSubContext,
Goal, !SVarState, !SVarStore, !VarSet,
!ModuleInfo, !QualInfo, !Specs) :-
expand_get_field_function_call_2(Context, MainContext, SubContext0,
FieldNames, FieldValueVar, TermInputVar, Purity, Functor,
FieldSubContext, Goals, !SVarState, !SVarStore, !VarSet,
!ModuleInfo, !QualInfo, !Specs),
goal_info_init(Context, GoalInfo),
conj_list_to_goal(Goals, GoalInfo, Goal).
:- pred expand_get_field_function_call_2(prog_context::in,
unify_main_context::in, unify_sub_contexts::in, field_list::in,
prog_var::in, prog_var::in, purity::in, cons_id::out,
pair(cons_id, unify_sub_contexts)::out, list(hlds_goal)::out,
svar_state::in, svar_state::out, svar_store::in, svar_store::out,
prog_varset::in, prog_varset::out,
module_info::in, module_info::out, qual_info::in, qual_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
expand_get_field_function_call_2(_, _, _, [], _, _, _, _, _, _,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs) :-
unexpected($module, $pred, "empty list of field names").
expand_get_field_function_call_2(Context, MainContext, SubContext0,
[FieldName - FieldArgs | FieldNames], FieldValueVar, TermInputVar,
Purity, Functor, FieldSubContext, Goals,
!SVarState, !SVarStore, !VarSet, !ModuleInfo, !QualInfo, !Specs) :-
make_fresh_arg_vars_subst_svars(FieldArgs, FieldArgVars, !VarSet,
!SVarState, !Specs),
GetArgVars = FieldArgVars ++ [TermInputVar],
(
FieldNames = [_ | _],
varset.new_var(SubTermInputVar, !VarSet),
construct_field_access_function_call(get, Context, MainContext,
SubContext0, FieldName, SubTermInputVar, GetArgVars, Purity,
Functor, Goal, !QualInfo),
% Recursively extract until we run out of field names.
TermInputArgNumber = 1 + list.length(FieldArgVars),
TermInputContext = unify_sub_context(Functor, TermInputArgNumber),
SubContext = [TermInputContext | SubContext0],
expand_get_field_function_call_2(Context, MainContext,
SubContext, FieldNames, FieldValueVar, SubTermInputVar, Purity,
_, FieldSubContext, Goals1, !SVarState, !SVarStore,
!VarSet, !ModuleInfo, !QualInfo, !Specs),
Goals2 = [Goal | Goals1]
;
FieldNames = [],
FieldSubContext = Functor - SubContext0,
construct_field_access_function_call(get, Context,
MainContext, SubContext0, FieldName, FieldValueVar,
GetArgVars, Purity, Functor, Goal, !QualInfo),
Goals2 = [Goal]
),
ArgContext = ac_functor(Functor, MainContext, SubContext0),
goal_info_init(Context, GoalInfo),
conj_list_to_goal(Goals2, GoalInfo, Conj0),
insert_arg_unifications(FieldArgVars, FieldArgs, Context, ArgContext,
Conj0, Conj, !SVarState, !SVarStore, !VarSet,
!ModuleInfo, !QualInfo, !Specs),
svar_goal_to_conj_list(Conj, Goals, !SVarStore).
:- pred construct_field_access_function_call(field_access_type::in,
prog_context::in, unify_main_context::in, unify_sub_contexts::in,
sym_name::in, prog_var::in, list(prog_var)::in, purity::in,
cons_id::out, hlds_goal::out, qual_info::in, qual_info::out) is det.
construct_field_access_function_call(AccessType, Context,
MainContext, SubContext, FieldName, RetArg, Args, Purity, Functor,
Goal, !QualInfo) :-
field_access_function_name(AccessType, FieldName, FuncName),
list.length(Args, Arity),
Functor = cons(FuncName, Arity, cons_id_dummy_type_ctor),
make_atomic_unification(RetArg,
rhs_functor(Functor, is_not_exist_constr, Args),
Context, MainContext, SubContext, Purity, Goal, !QualInfo).
parse_field_list(Term, VarSet, ContextPieces, MaybeFieldNames) :-
( if
Term = term.functor(term.atom("^"),
[FieldNameTerm, OtherFieldNamesTerm], _)
then
( if try_parse_sym_name_and_args(FieldNameTerm, FieldName, Args) then
parse_field_list(OtherFieldNamesTerm, VarSet, ContextPieces,
MaybeFieldNamesTail),
(
MaybeFieldNamesTail = error1(_),
MaybeFieldNames = MaybeFieldNamesTail
;
MaybeFieldNamesTail = ok1(FieldNamesTail),
MaybeFieldNames = ok1([FieldName - Args | FieldNamesTail])
)
else
Spec = make_field_list_error(VarSet,
get_term_context(FieldNameTerm), Term, ContextPieces),
MaybeFieldNames = error1([Spec])
)
else
( if try_parse_sym_name_and_args(Term, FieldName, Args) then
MaybeFieldNames = ok1([FieldName - Args])
else
Spec = make_field_list_error(VarSet, get_term_context(Term), Term,
ContextPieces),
MaybeFieldNames = error1([Spec])
)
).
:- func make_field_list_error(prog_varset, term.context, prog_term,
list(format_component)) = error_spec.
make_field_list_error(VarSet, Context, Term, ContextPieces) = Spec :-
TermStr = mercury_term_to_string(VarSet, print_name_only, Term),
Pieces = ContextPieces ++ [lower_case_next_if_not_first,
words("Error: expected field name, found"),
quote(TermStr), suffix("."), nl],
Spec = error_spec(severity_error, phase_term_to_parse_tree,
[simple_msg(Context, [always(Pieces)])]).
%-----------------------------------------------------------------------------%
:- end_module hlds.make_hlds.field_access.
%-----------------------------------------------------------------------------%