Protocols enable the separation between interface and implementation: several objects can implement the same protocol and an object can implement several protocols. Protocols may contain only predicate declarations. In some languages the term interface is used with similar meaning. Logtalk allows predicate declarations of any scope within protocols, contrary to some languages that only allow public declarations.
Defining a new protocol¶
We can define a new protocol in the same way we write Prolog code: by
using a text editor. Logtalk source files may contain one or more
objects, categories, or protocols. If you prefer to define each entity
in its own source file, it is recommended that the file be named after
the protocol. By default, all Logtalk source files use the extension
.lgt but this is optional and can be set in the adapter files.
Intermediate Prolog source files (generated by the Logtalk compiler)
have, by default, a
_lgt suffix and a
.pl extension. Again, this
can be set to match the needs of a particular Prolog compiler in the
corresponding adapter file. For example, we may define a protocol named
listp and save it in a
listp.lgt source file that will be
compiled to a
listp_lgt.pl Prolog file (depending on the backend
compiler, the names of the intermediate Prolog files may include a
Protocol names must be atoms. Objects, categories and protocols share the same namespace: we cannot have a protocol with the same name as an object or a category.
Protocol directives are textually encapsulated by using two Logtalk directives: protocol/1-2 and end_protocol/0. The most simple protocol will be one that is self-contained, not depending on any other Logtalk entity:
:- protocol(Protocol). ... :- end_protocol.
If a protocol extends one or more protocols, then the opening directive will be:
:- protocol(Protocol, extends([Protocol1, Protocol2, ...])). ... :- end_protocol.
In order to maximize protocol reuse, all predicates specified in a protocol should relate to the same functionality. Therefore, the only recommended use of protocol extension is when you need both a minimal protocol and an extended version of the same protocol with additional, convenient predicates.
Finding defined protocols¶
We can find, by backtracking, all defined protocols by using the current_protocol/1 built-in predicate with a unbound argument:
| ?- current_protocol(Protocol).
This predicate can also be used to test if a protocol is defined by calling it with a valid protocol identifier (an atom).
Creating a new protocol in runtime¶
We can create a new (dynamic) protocol at runtime by calling the Logtalk built-in predicate create_protocol/3:
| ?- create_protocol(Protocol, Relations, Directives).
The first argument should be either a variable or the name of the new protocol (a Prolog atom, which must not match an existing entity name). The remaining two arguments correspond to the relations described in the opening protocol directive and to the protocol directives.
For instance, the call:
| ?- create_protocol(ppp, [extends(qqq)], [public([foo/1, bar/1])]).
is equivalent to compiling and loading the protocol:
:- protocol(ppp, extends(qqq)). :- dynamic. :- public([foo/1, bar/1]). :- end_protocol.
If we need to create a lot of (dynamic) protocols at runtime, then is best to define a metaclass or a prototype with a predicate that will call this built-in predicate in order to provide more sophisticated behavior.
Abolishing an existing protocol¶
Dynamic protocols can be abolished using the abolish_protocol/1 built-in predicate:
| ?- abolish_protocol(Protocol).
The argument must be an identifier of a defined dynamic protocol, otherwise an error will be thrown.
Protocol directives are used to define protocol properties and documentation.
As usually happens with Prolog code, a protocol can be either static or dynamic. A protocol created during the execution of a program is always dynamic. A protocol defined in a file can be either dynamic or static. Dynamic protocols are declared by using the dynamic/0 directive in the protocol source code:
The directive must precede any predicate directives. Please be aware that using dynamic code results in a performance hit when compared to static code. We should only use dynamic protocols when these need to be abolished during program execution.
Logtalk provides two sets of built-in predicates that enable us to query the system about the possible relationships that a protocol have with other entities.
The extends_protocol/2-3 built-in predicates return all pairs of protocols so that the first one extends the second:
| ?- extends_protocol(Protocol1, Protocol2).
or, if we also want to know the extension scope:
| ?- extends_protocol(Protocol1, Protocol2, Scope).
To find which objects or categories implement which protocols we can call the implements_protocol/2-3 built-in predicates:
| ?- implements_protocol(ObjectOrCategory, Protocol).
or, if we also want to know the implementation scope:
| ?- implements_protocol(ObjectOrCategory, Protocol, Scope).
We can find the properties of defined protocols by calling the protocol_property/2 built-in predicate:
| ?- protocol_property(Protocol, Property).
A protocol may have the property
built_in. Dynamic protocols can be abolished in runtime by calling
built-in predicate. Depending on the backend Prolog compiler, a
protocol may have additional properties related to the source file where
it is defined.
The following protocol properties are supported:
The protocol is static
The protocol is dynamic (and thus can be abolished in runtime by calling the abolish_category/1 built-in predicate)
The protocol is a built-in protocol (and thus always available)
Source data available for the protocol
Absolute path of the source file defining the protocol (if applicable)
Basename and directory of the source file defining the protocol (if applicable);
Directoryalways ends with a
Source file begin and end lines of the protocol definition (if applicable)
List of public predicates declared by the protocol
List of protected predicates declared by the protocol
List of private predicates declared by the protocol
List of properties for a predicate declared by the protocol
List of properties for a predicate alias declared by the protocol (the properties include
Linebeing the begin line of the alias directive)
Some of the properties such as line numbers are only available when the protocol is defined in a source file compiled with the source_data flag turned on.
Any number of objects or categories can implement a protocol. The syntax is very simple:
:- object(Object, implements(Protocol)). ... :- end_object.
or, in the case of a category:
:- category(Object, implements(Protocol)). ... :- end_category.
To make all public predicates declared via an implemented protocol protected or to make all public and protected predicates private we prefix the protocol’s name with the corresponding keyword. For instance:
:- object(Object, implements(private::Protocol)). ... :- end_object.
:- object(Object, implements(protected::Protocol)). ... :- end_object.
Omitting the scope keyword is equivalent to writing:
:- object(Object, implements(public::Protocol)). ... :- end_object.
The same rules applies to protocols implemented by categories.
Logtalk defines a set of built-in protocols that are always available for any application.