2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: atts.yap *
|
|
|
|
* Last rev: 8/2/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: attribute support for Prolog *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2010-03-04 23:41:45 +00:00
|
|
|
:- module(attributes, [op(1150, fx, attribute)]).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
%% @{
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
/**
|
2015-12-15 09:28:43 +00:00
|
|
|
@addtogroup attributes
|
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
SICStus style attribute declarations are activated through loading the
|
|
|
|
library <tt>atts</tt>. The command
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
~~~~~
|
2015-11-18 15:06:25 +00:00
|
|
|
| ?- use_module(library(atts)).
|
2014-09-11 20:06:57 +01:00
|
|
|
~~~~~
|
2015-11-18 15:06:25 +00:00
|
|
|
enables this form of attributed variables.
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
The directive
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
- attribute/1
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
and the following user defined predicates can be used:
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
- Module:get_atts/2
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
- Module:put_atts/2
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
- Module:put_atts/3
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
- Module:woken_att_do/4
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
*/
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
2010-05-27 12:02:52 +01:00
|
|
|
:- use_module(library(lists), [member/2]).
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
:- multifile
|
|
|
|
user:goal_expansion/3.
|
2016-01-05 03:32:51 +00:00
|
|
|
:- multifile
|
|
|
|
user:term_expansion/2.
|
|
|
|
:- multifile
|
|
|
|
attributed_module/3.
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
:- dynamic existing_attribute/4.
|
|
|
|
:- dynamic modules_with_attributes/1.
|
2007-03-30 23:42:15 +01:00
|
|
|
:- dynamic attributed_module/3.
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2010-05-27 12:02:52 +01:00
|
|
|
modules_with_attributes([]).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
%
|
|
|
|
% defining a new attribute is just a question of establishing a
|
|
|
|
% Functor, Mod -> INT mappings
|
|
|
|
%
|
|
|
|
new_attribute(V) :- var(V), !,
|
|
|
|
throw(error(instantiation_error,attribute(V))).
|
|
|
|
new_attribute((At1,At2)) :-
|
|
|
|
new_attribute(At1),
|
|
|
|
new_attribute(At2).
|
|
|
|
new_attribute(Na/Ar) :-
|
|
|
|
source_module(Mod),
|
|
|
|
functor(S,Na,Ar),
|
2005-09-09 18:24:39 +01:00
|
|
|
existing_attribute(S,Mod,_,_) , !.
|
2001-04-09 20:54:03 +01:00
|
|
|
new_attribute(Na/Ar) :-
|
|
|
|
source_module(Mod),
|
|
|
|
functor(S,Na,Ar),
|
2005-09-09 18:24:39 +01:00
|
|
|
store_new_module(Mod,Ar,Position),
|
|
|
|
assertz(existing_attribute(S,Mod,Ar,Position)).
|
|
|
|
|
|
|
|
store_new_module(Mod,Ar,ArgPosition) :-
|
|
|
|
(
|
|
|
|
retract(attributed_module(Mod,Position,_))
|
|
|
|
->
|
|
|
|
true
|
|
|
|
;
|
2005-10-18 18:04:43 +01:00
|
|
|
retract(modules_with_attributes(Mods)),
|
2011-02-02 20:45:29 +00:00
|
|
|
assert(modules_with_attributes([Mod|Mods])), Position = 2
|
2005-09-09 18:24:39 +01:00
|
|
|
),
|
|
|
|
ArgPosition is Position+1,
|
|
|
|
( Ar == 0 -> NOfAtts is Position+1 ; NOfAtts is Position+Ar),
|
|
|
|
functor(AccessTerm,Mod,NOfAtts),
|
2015-12-15 09:28:43 +00:00
|
|
|
assertz(attributed_module(Mod,NOfAtts,AccessTerm)).
|
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
:- user_defined_directive(attribute(G), attributes:new_attribute(G)).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
/** @pred Module:get_atts( _-Var_, _?ListOfAttributes_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Unify the list _?ListOfAttributes_ with the attributes for the unbound
|
|
|
|
variable _Var_. Each member of the list must be a bound term of the
|
|
|
|
form `+( _Attribute_)`, `-( _Attribute_)` (the <tt>kbd</tt>
|
|
|
|
prefix may be dropped). The meaning of <tt>+</tt> and <tt>-</tt> is:
|
2015-12-15 09:28:43 +00:00
|
|
|
+ +( _Attribute_)
|
|
|
|
Unifies _Attribute_ with a corresponding attribute associated with
|
|
|
|
_Var_, fails otherwise.
|
|
|
|
|
|
|
|
+ -( _Attribute_)
|
|
|
|
Succeeds if a corresponding attribute is not associated with
|
|
|
|
_Var_. The arguments of _Attribute_ are ignored.
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
*/
|
2005-09-09 18:24:39 +01:00
|
|
|
user:goal_expansion(get_atts(Var,AccessSpec), Mod, Goal) :-
|
|
|
|
expand_get_attributes(AccessSpec,Mod,Var,Goal).
|
2015-11-18 15:06:25 +00:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
/** @pred Module:put_atts( _-Var_, _?ListOfAttributes_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Associate with or remove attributes from a variable _Var_. The
|
|
|
|
attributes are given in _?ListOfAttributes_, and the action depends
|
|
|
|
on how they are prefixed:
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
+ +( _Attribute_ )
|
|
|
|
Associate _Var_ with _Attribute_. A previous value for the
|
|
|
|
attribute is simply replace (like with `set_mutable/2`).
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
+ -( _Attribute_ )
|
|
|
|
Remove the attribute with the same name. If no such attribute existed,
|
|
|
|
simply succeed.
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
*/
|
2005-09-09 18:24:39 +01:00
|
|
|
user:goal_expansion(put_atts(Var,AccessSpec), Mod, Goal) :-
|
|
|
|
expand_put_attributes(AccessSpec, Mod, Var, Goal).
|
|
|
|
|
|
|
|
|
|
|
|
expand_get_attributes(V,_,_,_) :- var(V), !, fail.
|
|
|
|
expand_get_attributes([],_,_,true) :- !.
|
|
|
|
expand_get_attributes([-G1],Mod,V,attributes:free_att(V,Mod,Pos)) :-
|
|
|
|
existing_attribute(G1,Mod,_,Pos), !.
|
|
|
|
expand_get_attributes([+G1],Mod,V,attributes:get_att(V,Mod,Pos,A)) :-
|
|
|
|
existing_attribute(G1,Mod,1,Pos), !,
|
|
|
|
arg(1,G1,A).
|
|
|
|
expand_get_attributes([G1],Mod,V,attributes:get_att(V,Mod,Pos,A)) :-
|
|
|
|
existing_attribute(G1,Mod,1,Pos), !,
|
|
|
|
arg(1,G1,A).
|
|
|
|
expand_get_attributes(Atts,Mod,Var,attributes:get_module_atts(Var,AccessTerm)) :- Atts = [_|_], !,
|
|
|
|
attributed_module(Mod,NOfAtts,AccessTerm),
|
2006-01-04 03:45:49 +00:00
|
|
|
void_term(Void),
|
|
|
|
cvt_atts(Atts,Mod,Void,LAtts),
|
2005-09-09 18:24:39 +01:00
|
|
|
sort(LAtts,SortedLAtts),
|
2006-01-04 03:45:49 +00:00
|
|
|
free_term(Free),
|
|
|
|
build_att_term(1,NOfAtts,SortedLAtts,Free,AccessTerm).
|
2015-12-15 09:28:43 +00:00
|
|
|
expand_get_attributes(Att,Mod,Var,Goal) :-
|
2005-09-09 18:24:39 +01:00
|
|
|
expand_get_attributes([Att],Mod,Var,Goal).
|
|
|
|
|
|
|
|
build_att_term(NOfAtts,NOfAtts,[],_,_) :- !.
|
|
|
|
build_att_term(I0,NOfAtts,[I-Info|SortedLAtts],Void,AccessTerm) :-
|
|
|
|
I is I0+1, !,
|
|
|
|
copy_att_args(Info,I0,NI,AccessTerm),
|
|
|
|
build_att_term(NI,NOfAtts,SortedLAtts,Void,AccessTerm).
|
|
|
|
build_att_term(I0,NOfAtts,SortedLAtts,Void,AccessTerm) :-
|
|
|
|
I is I0+1,
|
|
|
|
arg(I,AccessTerm,Void),
|
|
|
|
build_att_term(I,NOfAtts,SortedLAtts,Void,AccessTerm).
|
|
|
|
|
2006-01-04 03:45:49 +00:00
|
|
|
cvt_atts(V,_,_,_) :- var(V), !, fail.
|
|
|
|
cvt_atts([],_,_,[]).
|
|
|
|
cvt_atts([V|_],_,_,_) :- var(V), !, fail.
|
|
|
|
cvt_atts([+Att|Atts],Mod,Void,[Pos-LAtts|Read]) :- !,
|
2005-09-09 18:24:39 +01:00
|
|
|
existing_attribute(Att,Mod,_,Pos),
|
|
|
|
(atom(Att) -> LAtts = [_] ; Att=..[_|LAtts]),
|
2006-01-04 03:45:49 +00:00
|
|
|
cvt_atts(Atts,Mod,Void,Read).
|
|
|
|
cvt_atts([-Att|Atts],Mod,Void,[Pos-LVoids|Read]) :- !,
|
2005-09-09 18:24:39 +01:00
|
|
|
existing_attribute(Att,Mod,_,Pos),
|
|
|
|
(
|
|
|
|
atom(Att)
|
|
|
|
->
|
|
|
|
LVoids = [Void]
|
|
|
|
;
|
|
|
|
Att =..[_|LAtts],
|
|
|
|
void_vars(LAtts,Void,LVoids)
|
2015-12-15 09:28:43 +00:00
|
|
|
),
|
2006-01-04 03:45:49 +00:00
|
|
|
cvt_atts(Atts,Mod,Void,Read).
|
|
|
|
cvt_atts([Att|Atts],Mod,Void,[Pos-LAtts|Read]) :- !,
|
2005-09-09 18:24:39 +01:00
|
|
|
existing_attribute(Att,Mod,_,Pos),
|
|
|
|
(atom(Att) -> LAtts = [_] ; Att=..[_|LAtts]),
|
2006-01-04 03:45:49 +00:00
|
|
|
cvt_atts(Atts,Mod,Void,Read).
|
2005-09-09 18:24:39 +01:00
|
|
|
|
|
|
|
copy_att_args([],I,I,_).
|
|
|
|
copy_att_args([V|Info],I,NI,AccessTerm) :-
|
|
|
|
I1 is I+1,
|
|
|
|
arg(I1,AccessTerm,V),
|
|
|
|
copy_att_args(Info,I1,NI,AccessTerm).
|
|
|
|
|
|
|
|
void_vars([],_,[]).
|
|
|
|
void_vars([_|LAtts],Void,[Void|LVoids]) :-
|
|
|
|
void_vars(LAtts,Void,LVoids).
|
|
|
|
|
|
|
|
expand_put_attributes(V,_,_,_) :- var(V), !, fail.
|
|
|
|
expand_put_attributes([-G1],Mod,V,attributes:rm_att(V,Mod,NOfAtts,Pos)) :-
|
|
|
|
existing_attribute(G1,Mod,_,Pos), !,
|
|
|
|
attributed_module(Mod,NOfAtts,_).
|
|
|
|
expand_put_attributes([+G1],Mod,V,attributes:put_att(V,Mod,NOfAtts,Pos,A)) :-
|
|
|
|
existing_attribute(G1,Mod,1,Pos), !,
|
|
|
|
attributed_module(Mod,NOfAtts,_),
|
|
|
|
arg(1,G1,A).
|
|
|
|
expand_put_attributes([G1],Mod,V,attributes:put_att(V,Mod,NOfAtts,Pos,A)) :-
|
|
|
|
existing_attribute(G1,Mod,1,Pos), !,
|
|
|
|
attributed_module(Mod,NOfAtts,_),
|
|
|
|
arg(1,G1,A).
|
|
|
|
expand_put_attributes(Atts,Mod,Var,attributes:put_module_atts(Var,AccessTerm)) :- Atts = [_|_], !,
|
|
|
|
attributed_module(Mod,NOfAtts,AccessTerm),
|
|
|
|
void_term(Void),
|
2006-01-07 02:12:32 +00:00
|
|
|
cvt_atts(Atts,Mod,Void,LAtts),
|
|
|
|
sort(LAtts,SortedLAtts),
|
|
|
|
free_term(Free),
|
|
|
|
build_att_term(1,NOfAtts,SortedLAtts,Free,AccessTerm).
|
2015-12-15 09:28:43 +00:00
|
|
|
expand_put_attributes(Att,Mod,Var,Goal) :-
|
2005-09-09 18:24:39 +01:00
|
|
|
expand_put_attributes([Att],Mod,Var,Goal).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2010-03-12 14:26:35 +00:00
|
|
|
woken_att_do(AttVar, Binding, NGoals, DoNotBind) :-
|
2006-01-07 02:12:32 +00:00
|
|
|
modules_with_attributes(AttVar,Mods0),
|
|
|
|
modules_with_attributes(Mods),
|
|
|
|
find_used(Mods,Mods0,[],ModsI),
|
|
|
|
do_verify_attributes(ModsI, AttVar, Binding, Goals),
|
2010-03-12 14:26:35 +00:00
|
|
|
process_goals(Goals, NGoals, DoNotBind).
|
2009-09-09 23:59:49 +01:00
|
|
|
|
|
|
|
% dirty trick to be able to unbind a variable that has been constrained.
|
|
|
|
process_goals([], [], _).
|
|
|
|
process_goals((M:do_not_bind_variable(Gs)).Goals, (M:Gs).NGoals, true) :- !,
|
|
|
|
process_goals(Goals, NGoals, _).
|
|
|
|
process_goals(G.Goals, G.NGoals, Do) :-
|
|
|
|
process_goals(Goals, NGoals, Do).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-07 02:12:32 +00:00
|
|
|
find_used([],_,L,L).
|
|
|
|
find_used([M|Mods],Mods0,L0,Lf) :-
|
2010-05-27 12:02:52 +01:00
|
|
|
member(M,Mods0), !,
|
2006-01-07 02:12:32 +00:00
|
|
|
find_used(Mods,Mods0,[M|L0],Lf).
|
2006-03-21 19:20:35 +00:00
|
|
|
find_used([_|Mods],Mods0,L0,Lf) :-
|
2006-01-07 02:12:32 +00:00
|
|
|
find_used(Mods,Mods0,L0,Lf).
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
/** @pred Module:verify_attributes( _-Var_, _+Value_, _-Goals_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
The predicate is called when trying to unify the attributed variable
|
|
|
|
_Var_ with the Prolog term _Value_. Note that _Value_ may be
|
|
|
|
itself an attributed variable, or may contain attributed variables. The
|
|
|
|
goal <tt>verify_attributes/3</tt> is actually called before _Var_ is
|
|
|
|
unified with _Value_.
|
|
|
|
|
|
|
|
It is up to the user to define which actions may be performed by
|
|
|
|
<tt>verify_attributes/3</tt> but the procedure is expected to return in
|
|
|
|
_Goals_ a list of goals to be called <em>after</em> _Var_ is
|
|
|
|
unified with _Value_. If <tt>verify_attributes/3</tt> fails, the
|
|
|
|
unification will fail.
|
|
|
|
|
|
|
|
Notice that the <tt>verify_attributes/3</tt> may be called even if _Var_<
|
|
|
|
has no attributes in module <tt>Module</tt>. In this case the routine should
|
|
|
|
simply succeed with _Goals_ unified with the empty list.
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
do_verify_attributes([], _, _, []).
|
|
|
|
do_verify_attributes([Mod|Mods], AttVar, Binding, [Mod:Goal|Goals]) :-
|
2005-09-09 18:24:39 +01:00
|
|
|
current_predicate(verify_attributes,Mod:verify_attributes(_,_,_)), !,
|
|
|
|
Mod:verify_attributes(AttVar, Binding, Goal),
|
|
|
|
do_verify_attributes(Mods, AttVar, Binding, Goals).
|
2001-04-09 20:54:03 +01:00
|
|
|
do_verify_attributes([_|Mods], AttVar, Binding, Goals) :-
|
|
|
|
do_verify_attributes(Mods, AttVar, Binding, Goals).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/**
|
2015-01-04 23:58:23 +00:00
|
|
|
@}
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|