Files
mercury/compiler/goal_path.m
Mark Brown 7460aadbf8 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.
2008-01-22 15:08:36 +00:00

260 lines
9.6 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-2008 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: goal_path.m.
% Main author: zs.
%
% This module looks after goal paths, which associate each goal with its
% position in a procedure definition,
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module check_hlds.goal_path.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_pred.
:- import_module hlds.hlds_module.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module bool.
%-----------------------------------------------------------------------------%
% IMPORTANT: the type constraint_id in hlds_data.m makes use of
% goal_paths to identify constraints between the typechecking pass
% and the polymorphism pass. For this reason, goal paths should not
% be recalculated anywhere between these two passes. See the XXX
% comment near the declaration of constraint_id.
%
:- pred fill_goal_path_slots(module_info::in, proc_info::in, proc_info::out)
is det.
% Fill in the goal_paths for goals in the clauses_info of the predicate.
% Clauses are given goal paths `disj(1)', ..., `disj(N)'. If the bool
% argument is true then the goal paths are stored in a form where any
% prefix consisting of `disj(_)', `neg', `exist(_)' and `ite_else'
% components is removed. This is used to optimise the constraint-based
% mode analysis where the instantiatedness of a variable at such a goal
% path is always equivalent to its instantiatedness at the parent goal
% path.
%
:- pred fill_goal_path_slots_in_clauses(module_info::in, bool::in,
pred_info::in, pred_info::out) is det.
:- pred fill_goal_path_slots_in_goal(hlds_goal::in, vartypes::in,
module_info::in, hlds_goal::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module check_hlds.type_util.
:- import_module hlds.hlds_clauses.
:- import_module hlds.hlds_goal.
:- import_module libs.
:- import_module libs.compiler_util.
:- import_module mdbcomp.
:- import_module mdbcomp.program_representation.
:- import_module parse_tree.prog_data.
:- import_module cord.
:- import_module int.
:- import_module list.
:- import_module map.
:- import_module maybe.
:- import_module pair.
%-----------------------------------------------------------------------------%
:- type slot_info
---> slot_info(
vartypes :: vartypes,
module_info :: module_info,
omit_mode_equiv_prefix :: bool
).
fill_goal_path_slots(ModuleInfo, !Proc) :-
proc_info_get_goal(!.Proc, Goal0),
proc_info_get_vartypes(!.Proc, VarTypes),
fill_goal_path_slots_in_goal(Goal0, VarTypes, ModuleInfo, Goal),
proc_info_set_goal(Goal, !Proc).
fill_goal_path_slots_in_clauses(ModuleInfo, OmitModeEquivPrefix, !PredInfo) :-
pred_info_get_clauses_info(!.PredInfo, ClausesInfo0),
clauses_info_clauses_only(ClausesInfo0, Clauses0),
clauses_info_get_vartypes(ClausesInfo0, VarTypes),
SlotInfo = slot_info(VarTypes, ModuleInfo, OmitModeEquivPrefix),
list.map_foldl(fill_slots_in_clause(SlotInfo), Clauses0, Clauses, 1, _),
clauses_info_set_clauses(Clauses, ClausesInfo0, ClausesInfo),
pred_info_set_clauses_info(ClausesInfo, !PredInfo).
:- pred fill_slots_in_clause(slot_info::in, clause::in, clause::out,
int::in, int::out) is det.
fill_slots_in_clause(SlotInfo, Clause0, Clause, ClauseNum, ClauseNum + 1) :-
Clause0 = clause(ProcIds, Goal0, Lang, Context),
fill_goal_slots(cord.singleton(step_disj(ClauseNum)), SlotInfo,
Goal0, Goal),
Clause = clause(ProcIds, Goal, Lang, Context).
fill_goal_path_slots_in_goal(Goal0, VarTypes, ModuleInfo, Goal) :-
SlotInfo = slot_info(VarTypes, ModuleInfo, no),
fill_goal_slots(empty, SlotInfo, Goal0, Goal).
:- pred fill_goal_slots(goal_path::in, slot_info::in,
hlds_goal::in, hlds_goal::out) is det.
fill_goal_slots(Path0, SlotInfo,
hlds_goal(Expr0, Info0), hlds_goal(Expr, Info)) :-
OmitModeEquivPrefix = SlotInfo ^ omit_mode_equiv_prefix,
(
OmitModeEquivPrefix = yes,
PathSteps0 = cord.list(Path0),
list.takewhile(mode_equiv_step, PathSteps0, _, PathSteps),
Path = cord.from_list(PathSteps)
;
OmitModeEquivPrefix = no,
Path = Path0
),
goal_info_set_goal_path(Path, Info0, Info),
fill_expr_slots(Info, Path0, SlotInfo, Expr0, Expr).
:- pred mode_equiv_step(goal_path_step::in) is semidet.
mode_equiv_step(Step) :-
( Step = step_disj(_)
; Step = step_neg
; Step = step_scope(_)
; Step = step_ite_else
).
:- pred fill_expr_slots(hlds_goal_info::in, goal_path::in, slot_info::in,
hlds_goal_expr::in, hlds_goal_expr::out) is det.
fill_expr_slots(GoalInfo, Path0, SlotInfo, Goal0, Goal) :-
(
Goal0 = conj(ConjType, Goals0),
fill_conj_slots(Path0, 0, SlotInfo, Goals0, Goals),
Goal = conj(ConjType, Goals)
;
Goal0 = disj(Goals0),
fill_disj_slots(Path0, 0, SlotInfo, Goals0, Goals),
Goal = disj(Goals)
;
Goal0 = switch(Var, CanFail, Cases0),
VarTypes = SlotInfo ^ vartypes,
ModuleInfo = SlotInfo ^ module_info,
map.lookup(VarTypes, Var, Type),
( switch_type_num_functors(ModuleInfo, Type, NumFunctors) ->
MaybeNumFunctors = yes(NumFunctors)
;
MaybeNumFunctors = no
),
fill_switch_slots(Path0, 0, MaybeNumFunctors, SlotInfo, Cases0, Cases),
Goal = switch(Var, CanFail, Cases)
;
Goal0 = negation(SubGoal0),
fill_goal_slots(cord.snoc(Path0, step_neg), SlotInfo,
SubGoal0, SubGoal),
Goal = negation(SubGoal)
;
Goal0 = scope(Reason, SubGoal0),
SubGoal0 = hlds_goal(_, InnerInfo),
OuterDetism = goal_info_get_determinism(GoalInfo),
InnerDetism = goal_info_get_determinism(InnerInfo),
( InnerDetism = OuterDetism ->
MaybeCut = scope_is_no_cut
;
MaybeCut = scope_is_cut
),
fill_goal_slots(cord.snoc(Path0, step_scope(MaybeCut)), SlotInfo,
SubGoal0, SubGoal),
Goal = scope(Reason, SubGoal)
;
Goal0 = if_then_else(A, Cond0, Then0, Else0),
fill_goal_slots(cord.snoc(Path0, step_ite_cond), SlotInfo,
Cond0, Cond),
fill_goal_slots(cord.snoc(Path0, step_ite_then), SlotInfo,
Then0, Then),
fill_goal_slots(cord.snoc(Path0, step_ite_else), SlotInfo,
Else0, Else),
Goal = if_then_else(A, Cond, Then, Else)
;
Goal0 = unify(LHS, RHS0, Mode, Kind, Context),
(
RHS0 = rhs_lambda_goal(A, B, C, D, E, F, G, H, LambdaGoal0),
fill_goal_slots(Path0, SlotInfo, LambdaGoal0, LambdaGoal),
RHS = rhs_lambda_goal(A, B, C, D, E, F, G, H, LambdaGoal)
;
( RHS0 = rhs_var(_)
; RHS0 = rhs_functor(_, _, _)
),
RHS = RHS0
),
Goal = unify(LHS, RHS, Mode, Kind, Context)
;
Goal0 = plain_call(_, _, _, _, _, _),
Goal = Goal0
;
Goal0 = generic_call(_, _, _, _),
Goal = Goal0
;
Goal0 = call_foreign_proc(_, _, _, _, _, _, _),
Goal = Goal0
;
Goal0 = shorthand(_),
% These should have been expanded out by now.
unexpected(this_file, "fill_expr_slots: unexpected shorthand")
).
:- pred fill_conj_slots(goal_path::in, int::in, slot_info::in,
list(hlds_goal)::in, list(hlds_goal)::out) is det.
fill_conj_slots(_, _, _, [], []).
fill_conj_slots(Path0, N0, SlotInfo, [Goal0 | Goals0], [Goal | Goals]) :-
N1 = N0 + 1,
fill_goal_slots(cord.snoc(Path0, step_conj(N1)), SlotInfo, Goal0, Goal),
fill_conj_slots(Path0, N1, SlotInfo, Goals0, Goals).
:- pred fill_disj_slots(goal_path::in, int::in, slot_info::in,
list(hlds_goal)::in, list(hlds_goal)::out) is det.
fill_disj_slots(_, _, _, [], []).
fill_disj_slots(Path0, N0, SlotInfo, [Goal0 | Goals0], [Goal | Goals]) :-
N1 = N0 + 1,
fill_goal_slots(cord.snoc(Path0, step_disj(N1)), SlotInfo, Goal0, Goal),
fill_disj_slots(Path0, N1, SlotInfo, Goals0, Goals).
:- pred fill_switch_slots(goal_path::in, int::in, maybe(int)::in,
slot_info::in, list(case)::in, list(case)::out) is det.
fill_switch_slots(_, _, _, _, [], []).
fill_switch_slots(Path0, N0, MaybeNumFunctors, SlotInfo,
[Case0 | Cases0], [Case | Cases]) :-
Case0 = case(MainConsId, OtherConsIds, Goal0),
N1 = N0 + 1,
fill_goal_slots(cord.snoc(Path0, step_switch(N1, MaybeNumFunctors)),
SlotInfo, Goal0, Goal),
Case = case(MainConsId, OtherConsIds, Goal),
fill_switch_slots(Path0, N1, MaybeNumFunctors, SlotInfo, Cases0, Cases).
%-----------------------------------------------------------------------------%
:- func this_file = string.
this_file = "goal_path.m".
%-----------------------------------------------------------------------------%
:- end_module goal_path.
%-----------------------------------------------------------------------------%