mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-20 16:31:04 +00:00
unifications. See the comments at the top of unify_proc.m for details of how it's done. hlds.m: Add new export statuses `pseudo_imported' and `pseudo_exported' to handle unification predicates, which can now have complicated modes. code_gen.pp, code_info.m, common.m, constraint.m, cse_detection.m, follow_code.m, follow_vars.m, hlds_out.m, inlining.m, live_vars, make_hlds.m, mercury_compile.pp, modes.m, store_alloc.m, switch_detection.m: Handle pseudo_imported predicates. modes.m, constraint.m: Change modecheck to return an updated module_info, since modechecking may insert new entries into the unify_requests queue in the module_info. Make sure that modechecking never inserts requests into the unify_requests queue for code that has mode errors (or needs rescheduling). call_gen.m, polymorphism.m: Move duplicated code into unify_proc.m. clause_to_proc.m: Add new predicate clauses_to_proc for use by unify_proc.m. unify_proc.m: Implement complicated unifications with complicated modes.
106 lines
4.0 KiB
Mathematica
106 lines
4.0 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1995 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.
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module clause_to_proc.
|
|
|
|
:- interface.
|
|
|
|
:- import_module hlds.
|
|
|
|
% In the hlds, we initially record the clauses for a predicate
|
|
% in the clauses_info data structure which is part of the
|
|
% pred_info data structure. But once the clauses have been
|
|
% type-checked, we want to have a separate copy of each clause
|
|
% for each different mode of the predicate, since we may
|
|
% end up reordering the clauses differently in different modes.
|
|
% Here we copy the clauses from the clause_info data structure
|
|
% into the proc_info data structure. Each clause is marked
|
|
% with a list of the modes for which it applies, so that
|
|
% there can be different code to implement different modes
|
|
% of a predicate (e.g. sort). For each mode of the predicate,
|
|
% we select the clauses for that mode, disjoin them together,
|
|
% and save this in the proc_info.
|
|
|
|
:- pred copy_clauses_to_procs(pred_info, pred_info).
|
|
:- mode copy_clauses_to_procs(in, out) is det.
|
|
|
|
:- pred copy_clauses_to_proc(proc_id, clauses_info, proc_info, proc_info).
|
|
:- mode copy_clauses_to_proc(in, in, in, out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module list, set, map, std_util.
|
|
|
|
copy_clauses_to_procs(PredInfo0, PredInfo) :-
|
|
pred_info_clauses_info(PredInfo0, ClausesInfo),
|
|
pred_info_procedures(PredInfo0, Procs0),
|
|
pred_info_non_imported_procids(PredInfo0, ProcIds),
|
|
copy_clauses_to_procs_2(ProcIds, ClausesInfo, Procs0, Procs),
|
|
pred_info_set_procedures(PredInfo0, Procs, PredInfo).
|
|
|
|
:- pred copy_clauses_to_procs_2(list(proc_id), clauses_info,
|
|
proc_table, proc_table).
|
|
:- mode copy_clauses_to_procs_2(in, in, in, out) is det.
|
|
|
|
copy_clauses_to_procs_2([], _, Procs, Procs).
|
|
copy_clauses_to_procs_2([ProcId | ProcIds], ClausesInfo, Procs0, Procs) :-
|
|
map__lookup(Procs0, ProcId, Proc0),
|
|
copy_clauses_to_proc(ProcId, ClausesInfo, Proc0, Proc),
|
|
map__set(Procs0, ProcId, Proc, Procs1),
|
|
copy_clauses_to_procs_2(ProcIds, ClausesInfo, Procs1, Procs).
|
|
|
|
copy_clauses_to_proc(ProcId, ClausesInfo, Proc0, Proc) :-
|
|
ClausesInfo = clauses_info(VarSet, VarTypes, HeadVars, Clauses),
|
|
select_matching_clauses(Clauses, ProcId, MatchingClauses),
|
|
get_clause_goals(MatchingClauses, GoalList),
|
|
( GoalList = [SingleGoal] ->
|
|
Goal = SingleGoal
|
|
;
|
|
% Construct a goal_info for the disjunction.
|
|
% We use the context of the first clause, unless
|
|
% there weren't any clauses at all, in which case
|
|
% we use the context of the mode declaration.
|
|
% The non-local vars are just the head variables.
|
|
goal_info_init(GoalInfo0),
|
|
( GoalList = [FirstGoal | _] ->
|
|
FirstGoal = _ - FirstGoalInfo,
|
|
goal_info_context(FirstGoalInfo, Context)
|
|
;
|
|
proc_info_context(Proc0, Context)
|
|
),
|
|
goal_info_set_context(GoalInfo0, Context, GoalInfo1),
|
|
set__list_to_set(HeadVars, NonLocalVars),
|
|
goal_info_set_nonlocals(GoalInfo1, NonLocalVars, GoalInfo),
|
|
Goal = disj(GoalList) - GoalInfo
|
|
),
|
|
proc_info_set_body(Proc0, VarSet, VarTypes, HeadVars, Goal, Proc).
|
|
|
|
:- pred select_matching_clauses(list(clause), proc_id, list(clause)).
|
|
:- mode select_matching_clauses(in, in, out) is det.
|
|
|
|
select_matching_clauses([], _, []).
|
|
select_matching_clauses([Clause | Clauses], ProcId, MatchingClauses) :-
|
|
Clause = clause(ProcIds, _, _),
|
|
( ProcIds = [] ->
|
|
MatchingClauses = [Clause | MatchingClauses1]
|
|
; list__member(ProcId, ProcIds) ->
|
|
MatchingClauses = [Clause | MatchingClauses1]
|
|
;
|
|
MatchingClauses = MatchingClauses1
|
|
),
|
|
select_matching_clauses(Clauses, ProcId, MatchingClauses1).
|
|
|
|
:- pred get_clause_goals(list(clause)::in, list(hlds__goal)::out) is det.
|
|
|
|
get_clause_goals([], []).
|
|
get_clause_goals([Clause | Clauses], Goals) :-
|
|
Clause = clause(_, Goal, _),
|
|
goal_to_disj_list(Goal, GoalList),
|
|
list__append(GoalList, Goals1, Goals),
|
|
get_clause_goals(Clauses, Goals1).
|