mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-21 00:39:37 +00:00
Implement higher-order any' insts. Pred or func expressions with an any'
Estimated hours taken: 100 Branches: main Implement higher-order `any' insts. Pred or func expressions with an `any' inst may bind non-local solver variables, but themselves must not be called in a negated context. (The existing ground pred and func expressions may not bind non-local solver variables, but may be called in a negated context.) Higher-order `any' insts are specified by using `any_pred' and `any_func' in place of `pred' and `func', respectively. We implement these insts by adding a new field to the any/1 constructor of mer_inst, which is identical to the ground_inst_info field of the ground/2 constructor. Both are given the new type `ho_inst_info'. We then relax the locking of non-local variables in these pred and func expressions, and extend call/N and apply/N to also accept the new insts (provided the variables are not locked). We also store the groundness (ho_ground or ho_any) of each lambda expression in a unification, in a new field in the rhs_lambda_goal constructor. NEWS: Mention the new feature. compiler/prog_data.m: Rename the ground_inst_info type ho_inst_info, and update its documentation. Add the ho_inst_info field to the any constructor in mer_inst. compiler/hlds_goal.m: Add the rhs_groundness field to rhs_lambda_goal in unify_rhs. compiler/inst_match.m: Propagate inst matching into the pred_inst_infos of any insts, if they exist. compiler/inst_util.m: Propagate abstract unification and inst merging into the pred_inst_infos of any insts, if they exist. May use of this information when building ground, any, shared and mostly_unique versions of insts. compiler/modecheck_call.m: Allow an `any' inst as the pred (func) argument to call/N (apply/N), but check that the variable is not locked. If the variable is locked, report a mode error which suggests using the ground inst. (We could also suggest that the goal be made impure, but it is best to point users towards the pure approach.) compiler/modecheck_unify.m: Relax the locking of non-locals when processing non-ground lambda goals. Update documentation. compiler/mode_util.m: Propagate type information into the pred_inst_infos of any insts. compiler/mode_errors.m: Change the purity error "lambda should be impure" to "lambda should be any", since this is better advice. Also provide an example of correct syntax if the verbose errors option is given. compiler/prog_io_goal.m: Parse the new kinds of expressions, returning the groundness along with the existing information about lambda expressions. compiler/superhomogeneous.m: Use the above groundness when building the lambda unification. compiler/prog_io_util.m: Parse the new kind of insts, filling in the new ho_inst_info field where appropriate. compiler/polymorphism.m: Handle the new fields. Assume that the shorthand form of lambda expressions always defines a ground inst -- if users want non-ground higher-order expressions they will need to use an explicit any_pred or any_func expression. compiler/equiv_type_hlds.m: Replace equivalent types in the pred_inst_infos of `any' insts. compiler/module_qual.m: Module qualify the pred_inst_infos of `any' insts. compiler/recompilation.usage.m: compiler/unused_imports.m: Look for items or imports used by insts in the pred_inst_infos of `any' insts. compiler/hlds_out.m: compiler/mercury_to_mercury.m: Output the new lambda expressions and insts in the correct format. compiler/type_util.m: Treat all pred and func types as solver types. (Effectively they are, since all such types can now have non-ground values, with call/N and apply/N acting as constraints.) compiler/lambda.m: Pass the groundness value when building procedures for lambda expressions. This is not currently required for anything. doc/reference_manual.texi: Document the new feature, and update existing documentation on solver types and negated contexts. tests/valid/Mmakefile: tests/valid/ho_any_inst.m: New test case for some valid code using higher-order any insts. tests/invalid/Mmakefile: tests/invalid/ho_any_inst.err_exp: tests/invalid/ho_any_inst.m: New test case for some illegal code. tests/invalid/anys_in_negated_contexts.err_exp: Update expected error message for this test case. We now report that the expression should be `any', rather than impure. compiler/*.m: Handle the new fields.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
%-----------------------------------------------------------------------------%
|
||||
% vim: ft=mercury ts=4 sw=4 et
|
||||
%-----------------------------------------------------------------------------%
|
||||
% Copyright (C) 1994-2007 The University of Melbourne.
|
||||
% Copyright (C) 1994-2008 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.
|
||||
%-----------------------------------------------------------------------------%
|
||||
@@ -138,11 +138,9 @@
|
||||
% contained an inst any non-local, but was not inside a
|
||||
% promise_purity scope.
|
||||
|
||||
; purity_error_lambda_should_be_impure(list(prog_var)).
|
||||
% A lambda term containing inst any non-locals should have been
|
||||
% declared impure, but hasn't been (executing such a lambda may
|
||||
% further constrain the inst any variables, thereby violating
|
||||
% referential transparency).
|
||||
; purity_error_lambda_should_be_any(list(prog_var)).
|
||||
% A ground lambda term contains inst any non-locals, and is not
|
||||
% marked impure.
|
||||
|
||||
:- type negated_context_desc
|
||||
---> if_then_else
|
||||
@@ -336,8 +334,8 @@ mode_error_to_spec(ModeInfo, ModeError) = Spec :-
|
||||
Spec = purity_error_should_be_in_promise_purity_scope_to_spec(NegCtxt,
|
||||
ModeInfo, Var)
|
||||
;
|
||||
ModeError = purity_error_lambda_should_be_impure(Vars),
|
||||
Spec = purity_error_lambda_should_be_impure_to_spec(ModeInfo, Vars)
|
||||
ModeError = purity_error_lambda_should_be_any(Vars),
|
||||
Spec = purity_error_lambda_should_be_any_to_spec(ModeInfo, Vars)
|
||||
).
|
||||
|
||||
mode_warning_info_to_spec(!.ModeInfo, Warning) = Spec :-
|
||||
@@ -1127,22 +1125,30 @@ purity_error_should_be_in_promise_purity_scope_to_spec(NegCtxtDesc,
|
||||
|
||||
%-----------------------------------------------------------------------------%
|
||||
|
||||
:- func purity_error_lambda_should_be_impure_to_spec(mode_info, list(prog_var))
|
||||
:- func purity_error_lambda_should_be_any_to_spec(mode_info, list(prog_var))
|
||||
= error_spec.
|
||||
|
||||
purity_error_lambda_should_be_impure_to_spec(ModeInfo, Vars) = Spec :-
|
||||
purity_error_lambda_should_be_any_to_spec(ModeInfo, Vars) = Spec :-
|
||||
Preamble = mode_info_context_preamble(ModeInfo),
|
||||
mode_info_get_context(ModeInfo, Context),
|
||||
mode_info_get_varset(ModeInfo, VarSet),
|
||||
Pieces = [
|
||||
words("purity error: lambda should be impure because it"),
|
||||
words("contains the following non-local variables"),
|
||||
words("whose insts contain `any':"),
|
||||
words("purity error: lambda is `ground' but contains the"),
|
||||
words("following non-local variables whose insts contain `any':"),
|
||||
words(mercury_vars_to_string(VarSet, no, Vars)),
|
||||
suffix("."), nl
|
||||
],
|
||||
Always = always(Preamble ++ Pieces),
|
||||
VerboseOnly = verbose_only([
|
||||
words("Predicate expressions with inst `any' can be written"),
|
||||
quote("any_pred(Args) is det :- ..."),
|
||||
suffix("."),
|
||||
words("Function expressions with inst `any' can be written"),
|
||||
quote("any_func(Args) = Result is det :- ..."),
|
||||
suffix(".")
|
||||
]),
|
||||
Spec = error_spec(severity_error, phase_mode_check(report_in_any_mode),
|
||||
[simple_msg(Context, [always(Preamble ++ Pieces)])]).
|
||||
[simple_msg(Context, [Always, VerboseOnly])]).
|
||||
|
||||
%-----------------------------------------------------------------------------%
|
||||
|
||||
|
||||
Reference in New Issue
Block a user