update docummentation.
This commit is contained in:
parent
222ead095c
commit
c5002e4c98
191
docs/swi.tex
191
docs/swi.tex
@ -1,8 +1,8 @@
|
|||||||
@chapter SWI-Prolog Emulation
|
@chapter SWI-Prolog Emulation
|
||||||
|
|
||||||
This library provides a number of SWI-Prolog builtins that are not by
|
This library provides a number of SWI-Prolog builtins that are not by
|
||||||
default in YAP. This library is loaded with the
|
default in YAP. This support is loaded with the
|
||||||
@code{use_module(library(swi))} command.
|
@code{expects_dialect(swi)} command.
|
||||||
|
|
||||||
@table @code
|
@table @code
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ triple. See the example above.
|
|||||||
@c @var{Pred} applies.
|
@c @var{Pred} applies.
|
||||||
@end table
|
@end table
|
||||||
|
|
||||||
@node Forall,hProlog and SWI-Prolog Attributed Variables,Invoking Predicates on all Members of a List, SWI-Prolog
|
@node Forall, ,Invoking Predicates on all Members of a List, SWI-Prolog
|
||||||
@section Forall
|
@section Forall
|
||||||
@c \label{sec:forall2}
|
@c \label{sec:forall2}
|
||||||
|
|
||||||
@ -189,176 +189,8 @@ The next example verifies that all arithmetic statements in the list
|
|||||||
|
|
||||||
@end table
|
@end table
|
||||||
|
|
||||||
@node hProlog and SWI-Prolog Attributed Variables, SWI-Prolog Global Variables, Forall,SWI-Prolog
|
@node SWI-Prolog Global Variables, Extensions, SWI-Prolog, Top
|
||||||
@section hProlog and SWI-Prolog Attributed Variables
|
@chapter SWI Global variables
|
||||||
@cindex hProlog Attributed Variables
|
|
||||||
|
|
||||||
Attributed variables
|
|
||||||
@c @ref{Attributed variables}
|
|
||||||
provide a technique for extending the
|
|
||||||
Prolog unification algorithm by hooking the binding of attributed
|
|
||||||
variables. There is little consensus in the Prolog community on the
|
|
||||||
exact definition and interface to attributed variables. Yap Prolog
|
|
||||||
traditionally implements a SICStus-like interface, but to enable
|
|
||||||
SWI-compatibility we have implemented the SWI-Prolog interface,
|
|
||||||
identical to the one realised by Bart Demoen for hProlog.
|
|
||||||
|
|
||||||
Binding an attributed variable schedules a goal to be executed at the
|
|
||||||
first possible opportunity. In the current implementation the hooks are
|
|
||||||
executed immediately after a successful unification of the clause-head
|
|
||||||
or successful completion of a foreign language (builtin) predicate. Each
|
|
||||||
attribute is associated to a module and the hook (attr_unify_hook/2) is
|
|
||||||
executed in this module. The example below realises a very simple and
|
|
||||||
incomplete finite domain reasoner.
|
|
||||||
|
|
||||||
@example
|
|
||||||
:- module(domain,
|
|
||||||
[ domain/2 % Var, ?Domain
|
|
||||||
]).
|
|
||||||
:- use_module(library(oset)).
|
|
||||||
|
|
||||||
domain(X, Dom) :-
|
|
||||||
var(Dom), !,
|
|
||||||
get_attr(X, domain, Dom).
|
|
||||||
domain(X, List) :-
|
|
||||||
sort(List, Domain),
|
|
||||||
put_attr(Y, domain, Domain),
|
|
||||||
X = Y.
|
|
||||||
|
|
||||||
% An attributed variable with attribute value Domain has been
|
|
||||||
% assigned the value Y
|
|
||||||
|
|
||||||
attr_unify_hook(Domain, Y) :-
|
|
||||||
( get_attr(Y, domain, Dom2)
|
|
||||||
-> oset_int(Domain, Dom2, NewDomain),
|
|
||||||
( NewDomain == []
|
|
||||||
-> fail
|
|
||||||
; NewDomain = [Value]
|
|
||||||
-> Y = Value
|
|
||||||
; put_attr(Y, domain, NewDomain)
|
|
||||||
)
|
|
||||||
; var(Y)
|
|
||||||
-> put_attr( Y, domain, Domain )
|
|
||||||
; memberchk(Y, Domain)
|
|
||||||
).
|
|
||||||
@end example
|
|
||||||
|
|
||||||
|
|
||||||
Before explaining the code we give some example queries:
|
|
||||||
|
|
||||||
@table @code
|
|
||||||
@item ?- domain(X, [a,b]), X = c
|
|
||||||
no
|
|
||||||
@item ?- domain(X, [a,b]), domain(X, [a,c]).
|
|
||||||
X = a
|
|
||||||
@item ?- domain(X, [a,b,c]), domain(X, [a,c]).
|
|
||||||
X = _D0
|
|
||||||
@end table
|
|
||||||
|
|
||||||
The predicate @code{domain/2} fetches (first clause) or assigns
|
|
||||||
(second clause) the variable a @emph{domain}, a set of values it can
|
|
||||||
be unified with. In the second clause first associates the domain
|
|
||||||
with a fresh variable and then unifies X to this variable to deal
|
|
||||||
with the possibility that X already has a domain. The
|
|
||||||
predicate @code{attr_unify_hook/2} is a hook called after a variable with
|
|
||||||
a domain is assigned a value. In the simple case where the variable
|
|
||||||
is bound to a concrete value we simply check whether this value is in
|
|
||||||
the domain. Otherwise we take the intersection of the domains and either
|
|
||||||
fail if the intersection is empty (first example), simply assign the
|
|
||||||
value if there is only one value in the intersection (second example) or
|
|
||||||
assign the intersection as the new domain of the variable (third
|
|
||||||
example).
|
|
||||||
|
|
||||||
|
|
||||||
@table @code
|
|
||||||
|
|
||||||
@item put_attr(+@var{Var},+@var{Module},+@var{Value})
|
|
||||||
@findex put_attr/3
|
|
||||||
@snindex put_attr/3
|
|
||||||
@cnindex put_attr/3
|
|
||||||
If @var{Var} is a variable or attributed variable, set the value for the
|
|
||||||
attribute named @var{Module} to @var{Value}. If an attribute with this
|
|
||||||
name is already associated with @var{Var}, the old value is replaced.
|
|
||||||
Backtracking will restore the old value (i.e. an attribute is a mutable
|
|
||||||
term. See also @code{setarg/3}). This predicate raises a type error if
|
|
||||||
@var{Var} is not a variable or @var{Module} is not an atom.
|
|
||||||
|
|
||||||
@item get_attr(+@var{Var},+@var{Module},+@var{Value})
|
|
||||||
@findex get_attr/3
|
|
||||||
@snindex get_attr/3
|
|
||||||
@cnindex get_attr/3
|
|
||||||
Request the current @var{value} for the attribute named @var{Module}. If
|
|
||||||
@var{Var} is not an attributed variable or the named attribute is not
|
|
||||||
associated to @var{Var} this predicate fails silently. If @var{Module}
|
|
||||||
is not an atom, a type error is raised.
|
|
||||||
|
|
||||||
@item del_attr(+@var{Var},+@var{Module})
|
|
||||||
@findex del_attr/2
|
|
||||||
@snindex del_attr/2
|
|
||||||
@cnindex del_attr/2
|
|
||||||
Delete the named attribute. If @var{Var} loses its last attribute it
|
|
||||||
is transformed back into a traditional Prolog variable. If @var{Module}
|
|
||||||
is not an atom, a type error is raised. In all other cases this
|
|
||||||
predicate succeeds regardless of whether or not the named attribute is
|
|
||||||
present.
|
|
||||||
|
|
||||||
@item attr_unify_hook(+@var{AttValue},+@var{VarValue})
|
|
||||||
@findex attr_unify_hook/2
|
|
||||||
@snindex attr_unify_hook/2
|
|
||||||
@cnindex attr_unify_hook/2
|
|
||||||
Hook that must be defined in the module an attributed variable refers
|
|
||||||
to. It is called @emph{after} the attributed variable has been
|
|
||||||
unified with a non-var term, possibly another attributed variable.
|
|
||||||
@var{AttValue} is the attribute that was associated to the variable
|
|
||||||
in this module and @var{VarValue} is the new value of the variable.
|
|
||||||
Normally this predicate fails to veto binding the variable to
|
|
||||||
@var{VarValue}, forcing backtracking to undo the binding. If
|
|
||||||
@var{VarValue} is another attributed variable the hook often combines
|
|
||||||
the two attribute and associates the combined attribute with
|
|
||||||
@var{VarValue} using @code{put_attr/3}.
|
|
||||||
|
|
||||||
@c \predicate{attr_portray_hook}{2}{+AttValue, +Var}
|
|
||||||
@c Called by write_term/2 and friends for each attribute if the option
|
|
||||||
@c \term{attributes}{portray} is in effect. If the hook succeeds the
|
|
||||||
@c attribute is considered printed. Otherwise \exam{Module = ...} is
|
|
||||||
@c printed to indicate the existence of a variable.
|
|
||||||
@end table
|
|
||||||
|
|
||||||
@subsection Special Purpose SWI Predicates for Attributes
|
|
||||||
|
|
||||||
Normal user code should deal with @code{put_attr/3}, @code{get_attr/3}
|
|
||||||
and @code{del_attr/2}. The routines in this section fetch or set the
|
|
||||||
entire attribute list of a variables. Use of these predicates is
|
|
||||||
anticipated to be restricted to printing and other special purpose
|
|
||||||
operations.
|
|
||||||
|
|
||||||
@table @code
|
|
||||||
@item get_attrs(+@var{Var},-@var{Attributes})
|
|
||||||
@findex get_attrs/2
|
|
||||||
@snindex get_attrs/2
|
|
||||||
@cnindex get_attrs/2
|
|
||||||
Get all attributes of @var{Var}. @var{Attributes} is a term of the form
|
|
||||||
@code{att(Module, Value, MoreAttributes)}, where @var{MoreAttributes} is
|
|
||||||
@code{[]} for the last attribute.
|
|
||||||
|
|
||||||
@item put_attrs(+@var{Var},+@var{Attributes})
|
|
||||||
@findex put_attrs/2
|
|
||||||
@snindex put_attrs/2
|
|
||||||
@cnindex put_attrs/2
|
|
||||||
Set all attributes of @var{Var}. See get_attrs/2 for a description of
|
|
||||||
@var{Attributes}.
|
|
||||||
|
|
||||||
@item copy_term_nat(?@var{TI},-@var{TF})
|
|
||||||
@findex copy_term_nat/2
|
|
||||||
@snindex copy_term_nat/2
|
|
||||||
@cnindex copy_term_nat/2
|
|
||||||
As @code{copy_term/2}. Attributes however, are @emph{not} copied but replaced
|
|
||||||
by fresh variables.
|
|
||||||
@end table
|
|
||||||
|
|
||||||
|
|
||||||
@node SWI-Prolog Global Variables, ,hProlog and SWI-Prolog Attributed Variables,SWI-Prolog
|
|
||||||
@section SWI Global variables
|
|
||||||
@c \label{sec:gvar}
|
@c \label{sec:gvar}
|
||||||
|
|
||||||
SWI-Prolog global variables are associations between names (atoms) and
|
SWI-Prolog global variables are associations between names (atoms) and
|
||||||
@ -478,11 +310,11 @@ enumeration is undefined.
|
|||||||
Delete the named global variable.
|
Delete the named global variable.
|
||||||
@end table
|
@end table
|
||||||
|
|
||||||
@subsection Compatibility of SWI-Prolog Global Variables
|
@section Compatibility of Global Variables
|
||||||
|
|
||||||
Global variables have been introduced by various Prolog
|
Global variables have been introduced by various Prolog
|
||||||
implementations recently. The implementation of them in SWI-Prolog is
|
implementations recently. YAP follows their implementation in SWI-Prolog, itself
|
||||||
based on hProlog by Bart Demoen. In discussion with Bart it was
|
based on hProlog by Bart Demoen. Jan and Bart
|
||||||
decided that the semantics if hProlog @code{nb_setval/2}, which is
|
decided that the semantics if hProlog @code{nb_setval/2}, which is
|
||||||
equivalent to @code{nb_linkval/2} is not acceptable for normal Prolog
|
equivalent to @code{nb_linkval/2} is not acceptable for normal Prolog
|
||||||
users as the behaviour is influenced by how builtin predicates
|
users as the behaviour is influenced by how builtin predicates
|
||||||
@ -493,10 +325,3 @@ Arrays can be implemented easily in SWI-Prolog using @code{functor/3} and
|
|||||||
@code{setarg/3} due to the unrestricted arity of compound terms.
|
@code{setarg/3} due to the unrestricted arity of compound terms.
|
||||||
|
|
||||||
|
|
||||||
@node Extensions,Debugging,SWI-Prolog,Top
|
|
||||||
@chapter Extensions to Prolog
|
|
||||||
|
|
||||||
YAP includes several extensions that are not enabled by
|
|
||||||
default, but that can be used to extend the functionality of the
|
|
||||||
system. These options can be set at compilation time by enabling the
|
|
||||||
related compilation flag, as explained in the @code{Makefile}
|
|
||||||
|
318
docs/yap.tex
318
docs/yap.tex
@ -91,6 +91,7 @@ us to include his text in this document.
|
|||||||
* Built-ins:: Built In Predicates
|
* Built-ins:: Built In Predicates
|
||||||
* Library:: Library Predicates
|
* Library:: Library Predicates
|
||||||
* SWI-Prolog:: SWI-Prolog emulation
|
* SWI-Prolog:: SWI-Prolog emulation
|
||||||
|
* Global Variables :: Global Variables for Prolog
|
||||||
* Extensions:: Extensions to Standard YAP
|
* Extensions:: Extensions to Standard YAP
|
||||||
* Rational Trees:: Working with Rational Trees
|
* Rational Trees:: Working with Rational Trees
|
||||||
* Co-routining:: Changing the Execution of Goals
|
* Co-routining:: Changing the Execution of Goals
|
||||||
@ -242,7 +243,6 @@ Subnodes of Attributes
|
|||||||
|
|
||||||
Subnodes of SWI-Prolog
|
Subnodes of SWI-Prolog
|
||||||
* Invoking Predicates on all Members of a List :: maplist and friends
|
* Invoking Predicates on all Members of a List :: maplist and friends
|
||||||
* hProlog and SWI-Prolog Attributed Variables :: Emulating SWI-like attributed variables
|
|
||||||
* SWI-Prolog Global Variables :: Emulating SWI-like attributed variables
|
* SWI-Prolog Global Variables :: Emulating SWI-like attributed variables
|
||||||
|
|
||||||
@c Subnodes of CLP(Q,R)
|
@c Subnodes of CLP(Q,R)
|
||||||
@ -3317,22 +3317,6 @@ in @var{TI} are also duplicated.
|
|||||||
|
|
||||||
Also refer to @code{copy_term/2}.
|
Also refer to @code{copy_term/2}.
|
||||||
|
|
||||||
@item copy_term(?@var{TI},-@var{TF},-@var{Goals})
|
|
||||||
@findex copy_term/3
|
|
||||||
@syindex copy_term/3
|
|
||||||
@cnindex copy_term/3
|
|
||||||
Term @var{TF} is a variant of the original term @var{TI}, such that for
|
|
||||||
each variable @var{V} in the term @var{TI} there is a new variable @var{V'}
|
|
||||||
in term @var{TF} without any attributes attached. Attributed
|
|
||||||
variables are thus converted to standard variables. @var{Goals} is
|
|
||||||
unified with a list that represents the attributes. The goal
|
|
||||||
@code{maplist(call,@var{Goals})} can be called to recreate the
|
|
||||||
attributes.
|
|
||||||
|
|
||||||
Before the actual copying, @code{copy_term/3} calls
|
|
||||||
@code{attribute_goals/1} in the module where the attribute is
|
|
||||||
defined.
|
|
||||||
|
|
||||||
@end table
|
@end table
|
||||||
|
|
||||||
@node Predicates on Atoms, Predicates on Characters, Testing Terms, Top
|
@node Predicates on Atoms, Predicates on Characters, Testing Terms, Top
|
||||||
@ -6567,7 +6551,7 @@ Execute a new shell.
|
|||||||
@snindex alarm/3
|
@snindex alarm/3
|
||||||
@cnindex alarm/3
|
@cnindex alarm/3
|
||||||
Arranges for YAP to be interrupted in @var{Seconds} seconds, or in
|
Arranges for YAP to be interrupted in @var{Seconds} seconds, or in
|
||||||
@var{[Seconds|MicroSeconds]}. When interrupted, YAP will execute
|
[@var{Seconds}|@var{MicroSeconds}]. When interrupted, YAP will execute
|
||||||
@var{Callable} and then return to the previous execution. If
|
@var{Callable} and then return to the previous execution. If
|
||||||
@var{Seconds} is @code{0}, no new alarm is scheduled. In any event,
|
@var{Seconds} is @code{0}, no new alarm is scheduled. In any event,
|
||||||
any previously set alarm is canceled.
|
any previously set alarm is canceled.
|
||||||
@ -12222,19 +12206,25 @@ are released.
|
|||||||
@end table
|
@end table
|
||||||
|
|
||||||
|
|
||||||
@node SWI-Prolog, Extensions, Library, Top
|
@node SWI-Prolog, SWI-Prolog Global Variables, Library, Top
|
||||||
@cindex SWI-Prolog
|
@cindex SWI-Prolog
|
||||||
|
|
||||||
@menu SWI-Prolog Emulation
|
@menu SWI-Prolog Emulation
|
||||||
Subnodes of SWI-Prolog
|
Subnodes of SWI-Prolog
|
||||||
* Invoking Predicates on all Members of a List :: maplist and friends
|
* Invoking Predicates on all Members of a List :: maplist and friends
|
||||||
* Forall :: forall built-in
|
* Forall :: forall built-in
|
||||||
* hProlog and SWI-Prolog Attributed Variables :: Emulating SWI-like attributed variables
|
|
||||||
* SWI-Prolog Global Variables :: Emulating SWI-like attributed variables
|
|
||||||
@end menu
|
@end menu
|
||||||
|
|
||||||
@include swi.tex
|
@include swi.tex
|
||||||
|
|
||||||
|
@node Extensions,Debugging,SWI-Prolog Global Variables,Top
|
||||||
|
@chapter Extensions to Prolog
|
||||||
|
|
||||||
|
YAP includes several extensions that are not enabled by
|
||||||
|
default, but that can be used to extend the functionality of the
|
||||||
|
system. These options can be set at compilation time by enabling the
|
||||||
|
related compilation flag, as explained in the @code{Makefile}
|
||||||
|
|
||||||
@menu
|
@menu
|
||||||
Extensions to Traditional Prolog
|
Extensions to Traditional Prolog
|
||||||
|
|
||||||
@ -12401,16 +12391,11 @@ no
|
|||||||
@cindex attributed variables
|
@cindex attributed variables
|
||||||
|
|
||||||
@menu
|
@menu
|
||||||
|
* New Style Attribute Declarations:: New Style code
|
||||||
* Attribute Declarations:: Declaring New Attributes
|
* Old Style Attribute Declarations:: Old Style code (deprecated)
|
||||||
* Attribute Manipulation:: Setting and Reading Attributes
|
|
||||||
* Attributed Unification:: Tuning the Unification Algorithm
|
|
||||||
* Displaying Attributes:: Displaying Attributes in User-Readable Form
|
|
||||||
* Projecting Attributes:: Obtaining the Attributes of Interest
|
|
||||||
* Attribute Examples:: Two Simple Examples of how to use Attributes.
|
|
||||||
@end menu
|
@end menu
|
||||||
|
|
||||||
YAP now supports the attributed variables packaged developed at OFAI by
|
YAP supports attributed variables, originally developed at OFAI by
|
||||||
Christian Holzbaur. Attributes are a means of declaring that an
|
Christian Holzbaur. Attributes are a means of declaring that an
|
||||||
arbitrary term is a property for a variable. These properties can be
|
arbitrary term is a property for a variable. These properties can be
|
||||||
updated during forward execution. Moreover, the unification algorithm is
|
updated during forward execution. Moreover, the unification algorithm is
|
||||||
@ -12419,16 +12404,257 @@ trying to unify these variables.
|
|||||||
|
|
||||||
Attributed variables provide an elegant abstraction over which one can
|
Attributed variables provide an elegant abstraction over which one can
|
||||||
extend Prolog systems. Their main application so far has been in
|
extend Prolog systems. Their main application so far has been in
|
||||||
implementing constraint handlers, such as Holzbaur's CLPQR and Fruewirth
|
implementing constraint handlers, such as Holzbaur's CLPQR, Fruewirth
|
||||||
and Holzbaur's CHR, but other applications have been proposed in the
|
and Holzbaur's CHR, and CLP(BN).
|
||||||
literature.
|
|
||||||
|
Different Prolog systems implement attributed variables in different
|
||||||
|
ways. Traditionally, YAP has used the interface designed by SICStus
|
||||||
|
Prolog. This interface is still
|
||||||
|
available in the @t{atts} library, but from YAP-6.0.3 we recommend using
|
||||||
|
the hProlog, SWI style interface. The main reason to do so is that
|
||||||
|
most packages included in YAP that use attributed variables, such as CHR, CLP(FD), and CLP(QR),
|
||||||
|
rely on the SWI-Prolog interface.
|
||||||
|
|
||||||
|
|
||||||
The command
|
@node New Style Attribute Declarations, Old Style Attribute Declarations, , Attributed Variables
|
||||||
|
@section hProlog and SWI-Prolog style Attribute Declarations
|
||||||
|
|
||||||
|
The following documentation is taken from the SWI-Prolog manual.
|
||||||
|
|
||||||
|
Binding an attributed variable schedules a goal to be executed at the
|
||||||
|
first possible opportunity. In the current implementation the hooks are
|
||||||
|
executed immediately after a successful unification of the clause-head
|
||||||
|
or successful completion of a foreign language (built-in) predicate. Each
|
||||||
|
attribute is associated to a module and the hook @code{attr_unify_hook/2} is
|
||||||
|
executed in this module. The example below realises a very simple and
|
||||||
|
incomplete finite domain reasoner.
|
||||||
|
|
||||||
|
@example
|
||||||
|
:- module(domain,
|
||||||
|
[ domain/2 % Var, ?Domain
|
||||||
|
]).
|
||||||
|
:- use_module(library(ordsets)).
|
||||||
|
|
||||||
|
domain(X, Dom) :-
|
||||||
|
var(Dom), !,
|
||||||
|
get_attr(X, domain, Dom).
|
||||||
|
domain(X, List) :-
|
||||||
|
list_to_ord_set(List, Domain),
|
||||||
|
put_attr(Y, domain, Domain),
|
||||||
|
X = Y.
|
||||||
|
|
||||||
|
% An attributed variable with attribute value Domain has been
|
||||||
|
% assigned the value Y
|
||||||
|
|
||||||
|
attr_unify_hook(Domain, Y) :-
|
||||||
|
( get_attr(Y, domain, Dom2)
|
||||||
|
-> ord_intersection(Domain, Dom2, NewDomain),
|
||||||
|
( NewDomain == []
|
||||||
|
-> fail
|
||||||
|
; NewDomain = [Value]
|
||||||
|
-> Y = Value
|
||||||
|
; put_attr(Y, domain, NewDomain)
|
||||||
|
)
|
||||||
|
; var(Y)
|
||||||
|
-> put_attr( Y, domain, Domain )
|
||||||
|
; ord_memberchk(Y, Domain)
|
||||||
|
).
|
||||||
|
|
||||||
|
% Translate attributes from this module to residual goals
|
||||||
|
|
||||||
|
attribute_goals(X) -->
|
||||||
|
@{ get_attr(X, domain, List) @},
|
||||||
|
[domain(X, List)].
|
||||||
|
@end example
|
||||||
|
|
||||||
|
|
||||||
|
Before explaining the code we give some example queries:
|
||||||
|
|
||||||
|
@multitable @columnfractions .70 .30
|
||||||
|
@item @code{?- domain(X, [a,b]), X = c}
|
||||||
|
@tab @code{fail}
|
||||||
|
@item @code{domain(X, [a,b]), domain(X, [a,c]).}
|
||||||
|
@tab @code{X=a}
|
||||||
|
@item @code{domain(X, [a,b,c]), domain(X, [a,c]).}
|
||||||
|
@tab @code{domain(X, [a,c]).}
|
||||||
|
@end multitable
|
||||||
|
|
||||||
|
The predicate @code{domain/2} fetches (first clause) or assigns
|
||||||
|
(second clause) the variable a @emph{domain}, a set of values it can
|
||||||
|
be unified with. In the second clause first associates the domain
|
||||||
|
with a fresh variable and then unifies X to this variable to deal
|
||||||
|
with the possibility that X already has a domain. The
|
||||||
|
predicate @code{attr_unify_hook/2} is a hook called after a variable with
|
||||||
|
a domain is assigned a value. In the simple case where the variable
|
||||||
|
is bound to a concrete value we simply check whether this value is in
|
||||||
|
the domain. Otherwise we take the intersection of the domains and either
|
||||||
|
fail if the intersection is empty (first example), simply assign the
|
||||||
|
value if there is only one value in the intersection (second example) or
|
||||||
|
assign the intersection as the new domain of the variable (third
|
||||||
|
example). The nonterminal @code{attribute_goals/3} is used to translate
|
||||||
|
remaining attributes to user-readable goals that, when executed, reinstate
|
||||||
|
these attributes.
|
||||||
|
|
||||||
|
@table @code
|
||||||
|
|
||||||
|
@item attvar(?@var{Term})
|
||||||
|
@findex attvar/1
|
||||||
|
@snindex attvar/1
|
||||||
|
@cnindex attvar/1
|
||||||
|
|
||||||
|
Succeeds if @code{Term} is an attributed variable. Note that @code{var/1} also
|
||||||
|
succeeds on attributed variables. Attributed variables are created with
|
||||||
|
@code{put_attr/3}.
|
||||||
|
|
||||||
|
@item put_attr(+@var{Var},+@var{Module},+@var{Value})
|
||||||
|
@findex put_attr/3
|
||||||
|
@snindex put_attr/3
|
||||||
|
@cnindex put_attr/3
|
||||||
|
|
||||||
|
If @var{Var} is a variable or attributed variable, set the value for the
|
||||||
|
attribute named @var{Module} to @var{Value}. If an attribute with this
|
||||||
|
name is already associated with @var{Var}, the old value is replaced.
|
||||||
|
Backtracking will restore the old value (i.e., an attribute is a mutable
|
||||||
|
term. See also @code{setarg/3}). This predicate raises a representation error if
|
||||||
|
@var{Var} is not a variable and a type error if @var{Module} is not an atom.
|
||||||
|
|
||||||
|
@item get_attr(+@var{Var},+@var{Module},-@var{Value})
|
||||||
|
@findex get_attr/3
|
||||||
|
@snindex get_attr/3
|
||||||
|
@cnindex get_attr/3
|
||||||
|
|
||||||
|
Request the current @var{value} for the attribute named @var{Module}. If
|
||||||
|
@var{Var} is not an attributed variable or the named attribute is not
|
||||||
|
associated to @var{Var} this predicate fails silently. If @var{Module}
|
||||||
|
is not an atom, a type error is raised.
|
||||||
|
|
||||||
|
@item del_attr(+@var{Var},+@var{Module})
|
||||||
|
@findex del_attr/2
|
||||||
|
@snindex del_attr/2
|
||||||
|
@cnindex del_attr/2
|
||||||
|
|
||||||
|
Delete the named attribute. If @var{Var} loses its last attribute it
|
||||||
|
is transformed back into a traditional Prolog variable. If @var{Module}
|
||||||
|
is not an atom, a type error is raised. In all other cases this
|
||||||
|
predicate succeeds regardless whether or not the named attribute is
|
||||||
|
present.
|
||||||
|
|
||||||
|
@item attr_unify_hook(+@var{AttValue},+@var{VarValue})
|
||||||
|
@findex attr_unify_hook/2
|
||||||
|
@snindex attr_unify_hook/2
|
||||||
|
@cnindex attr_unify_hook/2
|
||||||
|
|
||||||
|
Hook that must be defined in the module an attributed variable refers
|
||||||
|
to. Is is called @emph{after} the attributed variable has been
|
||||||
|
unified with a non-var term, possibly another attributed variable.
|
||||||
|
@var{AttValue} is the attribute that was associated to the variable
|
||||||
|
in this module and @var{VarValue} is the new value of the variable.
|
||||||
|
Normally this predicate fails to veto binding the variable to
|
||||||
|
@var{VarValue}, forcing backtracking to undo the binding. If
|
||||||
|
@var{VarValue} is another attributed variable the hook often combines
|
||||||
|
the two attribute and associates the combined attribute with
|
||||||
|
@var{VarValue} using @code{put_attr/3}.
|
||||||
|
|
||||||
|
@item attr_portray_hook(+@var{AttValue},+@var{Var})
|
||||||
|
@findex attr_portray_hook/2
|
||||||
|
@snindex attr_portray_hook/2
|
||||||
|
@cnindex attr_portray_hook/2
|
||||||
|
|
||||||
|
Called by @code{write_term/2} and friends for each attribute if the option
|
||||||
|
@code{attributes(portray)} is in effect. If the hook succeeds the
|
||||||
|
attribute is considered printed. Otherwise @code{Module = ...} is
|
||||||
|
printed to indicate the existence of a variable.
|
||||||
|
|
||||||
|
@item attribute_goals(+@var{Var},-@var{Gs},+@var{GsRest})
|
||||||
|
@findex attribute_goals/2
|
||||||
|
@snindex attribute_goals/2
|
||||||
|
@cnindex attribute_goals/2
|
||||||
|
|
||||||
|
This nonterminal, if it is defined in a module, is used by @var{copy_term/3}
|
||||||
|
to project attributes of that module to residual goals. It is also
|
||||||
|
used by the toplevel to obtain residual goals after executing a query.
|
||||||
|
@end table
|
||||||
|
|
||||||
|
Normal user code should deal with @code{put_attr/3}, @code{get_attr/3} and @code{del_attr/2}.
|
||||||
|
The routines in this section fetch or set the entire attribute list of a
|
||||||
|
variables. Use of these predicates is anticipated to be restricted to
|
||||||
|
printing and other special purpose operations.
|
||||||
|
|
||||||
|
@table @code
|
||||||
|
|
||||||
|
@item get_attrs(+@var{Var},-@var{Attributes})
|
||||||
|
@findex get_attrs/2
|
||||||
|
@snindex get_attrs/2
|
||||||
|
@cnindex get_attrs/2
|
||||||
|
|
||||||
|
Get all attributes of @var{Var}. @var{Attributes} is a term of the form
|
||||||
|
@code{att(@var{Module}, @var{Value}, @var{MoreAttributes})}, where @var{MoreAttributes} is
|
||||||
|
@code{[]} for the last attribute.
|
||||||
|
|
||||||
|
@item put_attrs(+@var{Var},+@var{Attributes})
|
||||||
|
@findex put_attrs/2
|
||||||
|
@snindex put_attrs/2
|
||||||
|
@cnindex put_attrs/2
|
||||||
|
Set all attributes of @var{Var}. See @code{get_attrs/2} for a description of
|
||||||
|
@var{Attributes}.
|
||||||
|
|
||||||
|
@item del_attrs(+@var{Var})
|
||||||
|
@findex del_attrs/1
|
||||||
|
@snindex del_attrs/1
|
||||||
|
@cnindex del_attrs/1
|
||||||
|
If @var{Var} is an attributed variable, delete @emph{all} its
|
||||||
|
attributes. In all other cases, this predicate succeeds without
|
||||||
|
side-effects.
|
||||||
|
|
||||||
|
@item term_attvars(+@var{Term},-@var{AttVars})
|
||||||
|
@findex term_attvars/2
|
||||||
|
@snindex term_attvars/2
|
||||||
|
@cnindex term_attvars/2
|
||||||
|
@var{AttVars} is a list of all attributed variables in @var{Term} and
|
||||||
|
its attributes. I.e., @code{term_attvars/2} works recursively through
|
||||||
|
attributes. This predicate is Cycle-safe.
|
||||||
|
|
||||||
|
@item copy_term(?@var{TI},-@var{TF},-@var{Goals})
|
||||||
|
@findex copy_term/3
|
||||||
|
@syindex copy_term/3
|
||||||
|
@cnindex copy_term/3
|
||||||
|
Term @var{TF} is a variant of the original term @var{TI}, such that for
|
||||||
|
each variable @var{V} in the term @var{TI} there is a new variable @var{V'}
|
||||||
|
in term @var{TF} without any attributes attached. Attributed
|
||||||
|
variables are thus converted to standard variables. @var{Goals} is
|
||||||
|
unified with a list that represents the attributes. The goal
|
||||||
|
@code{maplist(call,@var{Goals})} can be called to recreate the
|
||||||
|
attributes.
|
||||||
|
|
||||||
|
Before the actual copying, @code{copy_term/3} calls
|
||||||
|
@code{attribute_goals/1} in the module where the attribute is
|
||||||
|
defined.
|
||||||
|
|
||||||
|
@item copy_term_nat(?@var{TI},-@var{TF})
|
||||||
|
@findex copy_term_nat/2
|
||||||
|
@syindex copy_term_nat/2
|
||||||
|
@cnindex copy_term_nat/2
|
||||||
|
As @code{copy_term/2}. Attributes however, are @emph{not} copied but replaced
|
||||||
|
by fresh variables.
|
||||||
|
@end table
|
||||||
|
|
||||||
|
@node Old Style Attribute Declarations, , New Style Attribute Declarations, Attributed Variables
|
||||||
|
@section SICStus Prolog style Attribute Declarations
|
||||||
|
|
||||||
|
@menu
|
||||||
|
* Attribute Declarations:: Declaring New Attributes
|
||||||
|
* Attribute Manipulation:: Setting and Reading Attributes
|
||||||
|
* Attributed Unification:: Tuning the Unification Algorithm
|
||||||
|
* Displaying Attributes:: Displaying Attributes in User-Readable Form
|
||||||
|
* Projecting Attributes:: Obtaining the Attributes of Interest
|
||||||
|
* Attribute Examples:: Two Simple Examples of how to use Attributes.
|
||||||
|
@end menu
|
||||||
|
|
||||||
|
Old style attribute declarations are activated through loading the library @t{atts} . The command
|
||||||
@example
|
@example
|
||||||
| ?- use_module(library(atts)).
|
| ?- use_module(library(atts)).
|
||||||
@end example
|
@end example
|
||||||
enables the use of attributed variables. The package provides the
|
enables this form of use of attributed variables. The package provides the
|
||||||
following functionality:
|
following functionality:
|
||||||
@itemize @bullet
|
@itemize @bullet
|
||||||
@item Each attribute must be declared first. Attributes are described by a functor
|
@item Each attribute must be declared first. Attributes are described by a functor
|
||||||
@ -12453,8 +12679,8 @@ the top-level, where it is used to output the set of
|
|||||||
floundered constraints at the end of a query.
|
floundered constraints at the end of a query.
|
||||||
@end itemize
|
@end itemize
|
||||||
|
|
||||||
@node Attribute Declarations, Attribute Manipulation, , Attributed Variables
|
@node Attribute Declarations, Attribute Manipulation, , Old Style Attribute Declarations
|
||||||
@section Attribute Declarations
|
@subsection Attribute Declarations
|
||||||
|
|
||||||
Attributes are compound terms associated with a variable. Each attribute
|
Attributes are compound terms associated with a variable. Each attribute
|
||||||
has a @emph{name} which is @emph{private} to the module in which the
|
has a @emph{name} which is @emph{private} to the module in which the
|
||||||
@ -12480,8 +12706,8 @@ preprocessed depending on the module. The @code{user:goal_expansion/3}
|
|||||||
mechanism is used for this purpose.
|
mechanism is used for this purpose.
|
||||||
|
|
||||||
|
|
||||||
@node Attribute Manipulation, Attributed Unification, Attribute Declarations, Attributed Variables
|
@node Attribute Manipulation, Attributed Unification, Attribute Declarations, Old Style Attribute Declarations
|
||||||
@section Attribute Manipulation
|
@subsection Attribute Manipulation
|
||||||
|
|
||||||
|
|
||||||
The attribute manipulation predicates always work as follows:
|
The attribute manipulation predicates always work as follows:
|
||||||
@ -12531,8 +12757,8 @@ Remove the attribute with the same name. If no such attribute existed,
|
|||||||
simply succeed.
|
simply succeed.
|
||||||
@end table
|
@end table
|
||||||
|
|
||||||
@node Attributed Unification, Displaying Attributes, Attribute Manipulation, Attributed Variables
|
@node Attributed Unification, Displaying Attributes, Attribute Manipulation, Old Style Attribute Declarations
|
||||||
@section Attributed Unification
|
@subsection Attributed Unification
|
||||||
|
|
||||||
The user-predicate predicate @code{verify_attributes/3} is called when
|
The user-predicate predicate @code{verify_attributes/3} is called when
|
||||||
attempting to unify an attributed variable which might have attributes
|
attempting to unify an attributed variable which might have attributes
|
||||||
@ -12569,8 +12795,8 @@ Succeed if @var{Var} is an attributed variable.
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@node Displaying Attributes, Projecting Attributes,Attributed Unification, Attributed Variables
|
@node Displaying Attributes, Projecting Attributes,Attributed Unification, Old Style Attribute Declarations
|
||||||
@section Displaying Attributes
|
@subsection Displaying Attributes
|
||||||
|
|
||||||
Attributes are usually presented as goals. The following routines are
|
Attributes are usually presented as goals. The following routines are
|
||||||
used by built-in predicates such as @code{call_residue/2} and by the
|
used by built-in predicates such as @code{call_residue/2} and by the
|
||||||
@ -12594,8 +12820,8 @@ User-defined procedure, called to project the attributes in the query,
|
|||||||
|
|
||||||
@end table
|
@end table
|
||||||
|
|
||||||
@node Projecting Attributes, Attribute Examples, Displaying Attributes, Attributed Variables
|
@node Projecting Attributes, Attribute Examples, Displaying Attributes, Old Style Attribute Declarations
|
||||||
@section Projecting Attributes
|
@subsection Projecting Attributes
|
||||||
|
|
||||||
Constraint solvers must be able to project a set of constraints to a set
|
Constraint solvers must be able to project a set of constraints to a set
|
||||||
of variables. This is useful when displaying the solution to a goal, but
|
of variables. This is useful when displaying the solution to a goal, but
|
||||||
@ -12626,8 +12852,8 @@ original constraints into a set of new constraints on the projection,
|
|||||||
and these constraints are the ones that will have an
|
and these constraints are the ones that will have an
|
||||||
@code{attribute_goal/2} handler.
|
@code{attribute_goal/2} handler.
|
||||||
|
|
||||||
@node Attribute Examples, ,Projecting Attributes, Attributed Variables
|
@node Attribute Examples, ,Projecting Attributes, Old Style Attribute Declarations
|
||||||
@section Attribute Examples
|
@subsection Attribute Examples
|
||||||
|
|
||||||
The following two examples example is taken from the SICStus Prolog manual. It
|
The following two examples example is taken from the SICStus Prolog manual. It
|
||||||
sketches the implementation of a simple finite domain ``solver''. Note
|
sketches the implementation of a simple finite domain ``solver''. Note
|
||||||
|
Reference in New Issue
Block a user