This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/library/maputils.yap

100 lines
2.4 KiB
Plaintext
Raw Normal View History

2013-09-28 11:08:34 +01:00
%%%%%%%%%%%%%%%%%%%%
% map utilities
%%%%%%%%%%%%%%%%%%%%
/**
* @file maputils.yap
*
* @addtogroup maplist
*
* Auxiliary routines
*
*@{
*/
2013-09-28 11:08:34 +01:00
:- module(maputils,
[compile_aux/2,
goal_expansion_allowed/0,
pred_name/4,
aux_preds/5,
append_args/3]).
:- use_module(library(lists), [append/3]).
:- dynamic number_of_expansions/1.
number_of_expansions(0).
%
% compile auxiliary routines for term expansion
%
2013-09-28 11:08:34 +01:00
compile_aux([Clause|Clauses], Module) :-
2014-08-06 16:25:08 +01:00
% compile the predicate declaration if needed
2013-09-28 11:08:34 +01:00
( Clause = (Head :- _)
; Clause = Head ),
!,
functor(Head, F, N),
( current_predicate(Module:F/N)
->
true
;
% format("*** Creating auxiliary predicate ~q~n", [F/N]),
% checklist(portray_clause, [Clause|Clauses]),
compile_term([Clause|Clauses], Module)
).
compile_term([], _).
compile_term([Clause|Clauses], Module) :-
assert_static(Module:Clause),
compile_term(Clauses, Module).
append_args(Term, Args, NewTerm) :-
Term =.. [Meta|OldArgs],
append(OldArgs, Args, GoalArgs),
NewTerm =.. [Meta|GoalArgs].
aux_preds(Meta, _, _, _, _) :-
var(Meta), !,
fail.
aux_preds(_:Meta, MetaVars, Pred, PredVars, Proto) :- !,
aux_preds(Meta, MetaVars, Pred, PredVars, Proto).
aux_preds(Meta, MetaVars, Pred, PredVars, Proto) :-
Meta =.. [F|Args],
aux_args(Args, MetaVars, PredArgs, PredVars, ProtoArgs),
Pred =.. [F|PredArgs],
Proto =.. [F|ProtoArgs].
aux_args([], [], [], [], []).
aux_args([Arg|Args], MVars, [Arg|PArgs], PVars, [Arg|ProtoArgs]) :-
ground(Arg), !,
aux_args(Args, MVars, PArgs, PVars, ProtoArgs).
aux_args([Arg|Args], [Arg|MVars], [PVar|PArgs], [PVar|PVars], ['_'|ProtoArgs]) :-
aux_args(Args, MVars, PArgs, PVars, ProtoArgs).
pred_name(Macro, Arity, _ , Name) :-
2014-08-06 16:25:08 +01:00
prolog_load_context(file, FullFileName),
file_base_name( FullFileName, File ),
prolog_load_context(term_position, Pos),
stream_position_data( line_count, Pos, Line ), !,
2013-09-28 11:08:34 +01:00
transformation_id(Id),
2014-08-06 16:25:08 +01:00
atomic_concat(['$$$ for ',Macro,'/',Arity,', line ',Line,' in ',File,' ',Id], Name).
pred_name(Macro, Arity, _ , Name) :-
transformation_id(Id),
atomic_concat(['$$$__expansion__ for ',Macro,'/',Arity,' ',Id], Name).
2013-09-28 11:08:34 +01:00
transformation_id(Id) :-
retract(number_of_expansions(Id)),
Id1 is Id+1,
assert(number_of_expansions(Id1)).
%% goal_expansion_allowed is semidet.
%
% `True` if we can use
% goal-expansion.
2013-09-28 11:08:34 +01:00
goal_expansion_allowed :-
once( prolog_load_context(_, _) ), % make sure we are compiling.
\+ current_prolog_flag(xref, true).
/**
@}
2014-08-06 16:25:08 +01:00
*/