..
   This file is part of Logtalk <https://logtalk.org/>  
   Copyright 1998-2019 Paulo Moura <pmoura@logtalk.org>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.


.. _categories_categories:

Categories
==========

Categories are *fine-grained units of code reuse* and can be regarded as a
dual concept of protocols. Categories provide a way to encapsulate a set
of related predicate declarations and definitions that do not represent
a complete object and that only make sense when composed with other
predicates. Categories may also be used to break a complex object in
functional units. A category can be imported by several objects (without
code duplication), including objects participating in prototype or
class-based hierarchies. This concept of categories shares some ideas
with Smalltalk-80 functional categories [Goldberg83]_, Flavors mix-ins
[Moon86]_ (without necessarily implying multi-inheritance), and
Objective-C categories [Cox86]_. Categories may also *complement*
existing objects, thus providing a :term:`hot patching` mechanism
inspired by the Objective-C categories functionality.

.. _categories_defining:

Defining a new category
-----------------------

We can define a new category 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 category. 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 category named
``documenting`` and save it in a ``documenting.lgt`` source file that
will be compiled to a ``documenting_lgt.pl`` Prolog file (depending on
the :term:`backend compiler <backend Prolog compiler>`, the names of the
intermediate Prolog files may include a directory hash).

Category names can be atoms or compound terms (when defining parametric
categories). Objects, categories, and protocols share the same name
space: we cannot have a category with the same name as an object or a
protocol.

Category code (directives and predicates) is textually encapsulated by
using two Logtalk directives: :ref:`directives_category_1_3` and
:ref:`directives_end_category_0`. The
most simple category will be one that is self-contained, not depending
on any other Logtalk entity:

::

   :- category(Category).
       ...
   :- end_category.

If a category implements one or more protocols then the opening
directive will be:

::

   :- category(Category,
       implements([Protocol1, Protocol2, ...])).
       ...
   :- end_category.

A category may be defined as a composition of other categories by
writing:

::

   :- category(Category,
       extends([Category1, Category2, ...])).
       ...
   :- end_category.

This feature should only be used when extending a category without
breaking its functional cohesion (for example, when a modified version
of a category is needed for importing on several unrelated objects). The
preferred way of composing several categories is by importing them into
an object. When a category overrides a predicate defined in an extended
category, the overridden definition can still be called by using the
:ref:`control_call_super_1` control construct.

Categories cannot inherit from objects. In addition, categories cannot
define clauses for dynamic predicates. This restriction applies because
a category can be imported by several objects and because we cannot use
the database handling built-in methods with categories (messages can
only be sent to objects). However, categories may contain declarations
for dynamic predicates and they can contain predicates which handle
dynamic predicates. For example:

::

   :- category(attributes).

       :- public(attribute/2).
       :- public(set_attribute/2).
       :- public(del_attribute/2).

       :- private(attribute_/2).
       :- dynamic(attribute_/2).

       attribute(Attribute, Value) :-
           % called in the context of "self"
           ::attribute_(Attribute, Value).

       set_attribute(Attribute, Value) :-
           % retract old clauses in "self"
           ::retractall(attribute_(Attribute, _)),
           % assert new clause in "self"
           ::assertz(attribute_(Attribute, Value)).

       del_attribute(Attribute, Value) :-
           % retract clause in "self"
           ::retract(attribute_(Attribute, Value)).

   :- end_category.

Each object importing this category will have its own ``attribute_/2``
private, dynamic predicate. The predicates ``attribute/2``,
``set_attribute/2``, and ``del_attribute/2`` always access and modify
the dynamic predicate contained in the object receiving the
corresponding messages (i.e. *self*). But it's also possible to define
predicates that handle dynamic predicates in the context of *this*
instead of *self*. For example:

::

   :- category(attributes).

       :- public(attribute/2).
       :- public(set_attribute/2).
       :- public(del_attribute/2).

       :- private(attribute_/2).
       :- dynamic(attribute_/2).

       attribute(Attribute, Value) :-
           % call in the context of "this"
           attribute_(Attribute, Value).

       set_attribute(Attribute, Value) :-
           % retract old clauses in "this"
           retractall(attribute_(Attribute, _)),
           % asserts clause in "this"
           assertz(attribute_(Attribute, Value)).

       del_attribute(Attribute, Value) :-
           % retract clause in "this"
           retract(attribute_(Attribute, Value)).

   :- end_category.

When defining a category that declares and handles dynamic predicates,
working in the context of *this* ties those dynamic predicates to the
object importing the category while working in the context of *self*
allows each object inheriting from the object that imports the category
to have its own set of clauses for those dynamic predicates.

.. _categories_patching:

Hot patching
------------

A category may also explicitly complement one or more existing objects,
thus providing :term:`hot patching` functionality inspired by Objective-C
categories:

::

   :- category(Category,
       complements([Object1, Object2, ....])).
       ...
   :- end_category.

This allows us to add missing directives (e.g. to define
:term:`aliases <predicate alias>` for complemented object predicates),
replace broken predicate definitions, add new predicates, and add protocols
and categories to existing objects without requiring access or modifications
to their source code. Common scenarios are adding logging or debugging
predicates to a set of objects. Complemented objects need to be compiled
with the :ref:`complements <flag_complements>` compiler flag set ``allow``
(to allow both patching and adding functionality) or ``restrict`` (to allow
only adding new functionality). A complementing category takes preference
over a previously loaded complementing category for the same object thus
allowing patching a previous patch if necessary.

Note that :term:`super calls <super call>` from predicates defined in complementing
categories lookup inherited definitions as if the calls were made from
the complemented object instead of the category ancestors. This allows
more comprehensive object patching. But it also means that, if you want
to patch an object so that it imports a category that extends another
category and uses super calls to access the extended category
predicates, you will need to define a (possibly empty) complementing
category that extends the category that you want to add.

An unfortunate consequence of allowing an object to be patched at
runtime using a complementing category is that it disables the use of
:term:`static binding` optimizations for messages sent to the complemented
object as it can always be later patched, thus rendering the static
binding optimizations invalid.

Another important caveat is that, while a complementing category can
replace a predicate definition, local callers of the replaced predicate
will still call the unpatched version of the predicate. This is a
consequence of the lack of a portable solution at the
:term:`backend Prolog compiler` level for destructively replacing static
predicates.

.. _categories_finding:

Finding defined categories
--------------------------

We can find, by backtracking, all defined categories by using the
:ref:`predicates_current_category_1` built-in predicate with a
unbound argument:

.. code-block:: text

   | ?- current_category(Category).

This predicate can also be used to test if a category is defined by
calling it with a valid category identifier (an atom or a compound
term).

.. _categories_creating:

Creating a new category in runtime
----------------------------------

A category can be dynamically created at runtime by using the
:ref:`predicates_create_category_4` built-in predicate:

.. code-block:: text

   | ?- create_category(Category, Relations, Directives, Clauses).

The first argument should be either a variable or the name of the new
category (a Prolog atom, which must not match with an existing entity
name). The remaining three arguments correspond to the relations
described in the opening category directive and to the category code
contents (directives and clauses).

For example, the call:

.. code-block:: text

   | ?- create_category(
            ccc,
            [implements(ppp)],
            [private(bar/1)],
            [(foo(X):-bar(X)), bar(1), bar(2)]
        ).

is equivalent to compiling and loading the category:

::

   :- category(ccc,
       implements(ppp)).

       :- dynamic.

       :- private(bar/1).

       foo(X) :-
           bar(X).

       bar(1).
       bar(2).

   :- end_category.

If we need to create a lot of (dynamic) categories 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.

.. _categories_abolishing:

Abolishing an existing category
-------------------------------

Dynamic categories can be abolished using the
:ref:`predicates_abolish_category_1` built-in predicate:

.. code-block:: text

   | ?- abolish_category(Category).

The argument must be an identifier of a defined dynamic category,
otherwise an error will be thrown.

.. _categories_directives:

Category directives
-------------------

Category directives are used to define category properties, to document
a category dependencies on other Logtalk entities, and to load the
contents of files into a category.

.. _categories_dynamic:

Dynamic categories
~~~~~~~~~~~~~~~~~~

As usually happens with Prolog code, a category can be either static or
dynamic. A category created during the execution of a program is always
dynamic. A category defined in a file can be either dynamic or static.
Dynamic categories are declared by using the :ref:`directives_dynamic_0`
directive in the category source code:

::

   :- dynamic.

The directive must precede any predicate directives or clauses. Please
be aware that using dynamic code results in a performance hit when
compared to static code. We should only use dynamic categories when
these need to be abolished during program execution.

.. _categories_documentation:

Category documentation
~~~~~~~~~~~~~~~~~~~~~~

A category can be documented with arbitrary user-defined information by
using the :ref:`directives_info_1` directive:

::

   :- info(List).

See the :ref:`documenting_documenting` section for details.

.. _categories_include:

Loading files into a category
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The :ref:`directives_include_1` directive
can be used to load the contents of a file into a category. See the
:ref:`objects_objects` section for an example of using this
directive.

.. _categories_relationships:

Category relationships
----------------------

Logtalk provides two sets of built-in predicates that enable us to query
the system about the possible relationships that a category can have
with other entities.

The built-in predicates :ref:`predicates_implements_protocol_2_3`
and :ref:`predicates_conforms_to_protocol_2_3`
allows us to find which categories implements which protocols:

.. code-block:: text

   | ?- implements_protocol(Category, Protocol, Scope).

or, if we also want to consider inherited protocols:

.. code-block:: text

   | ?- conforms_to_protocol(Category, Protocol, Scope).

Note that, if we use a unbound first argument, we will need to use the
:ref:`predicates_current_category_1` built-in predicate to ensure that
the returned entity is a category and not an object.

To find which objects import which categories we can use the
:ref:`predicates_imports_category_2_3` built-in predicates:

.. code-block:: text

   | ?- imports_category(Object, Category).

or, if we also want to know the importation scope:

.. code-block:: text

   | ?- imports_category(Object, Category, Scope).

Note that a category may be imported by several objects.

To find which categories extend other categories we can use the
:ref:`predicates_extends_category_2_3` built-in predicates:

.. code-block:: text

   | ?- extends_category(Category1, Category2).

or, if we also want to know the extension scope:

.. code-block:: text

   | ?- extends_category(Category1, Category2, Scope).

Note that a category may be extended by several categories.

To find which categories explicitly complement existing objects we can
use the :ref:`predicates_complements_object_2` built-in predicate:

.. code-block:: text

   | ?- complements_object(Category, Object).

Note that a category may explicitly complement several objects.

.. _categories_properties:

Category properties
-------------------

We can find the properties of defined categories by calling the built-in
predicate :ref:`predicates_category_property_2`:

.. code-block:: text

   | ?- category_property(Category, Property).

The following category properties are supported:

``static``
   The category is static
``dynamic``
   The category is dynamic (and thus can be abolished in runtime by
   calling the :ref:`predicates_abolish_category_1` built-in predicate)
``built_in``
   The category is a built-in category (and thus always available)
``file(Path)``
   Absolute path of the source file defining the category (if
   applicable)
``file(Basename, Directory)``
   Basename and directory of the source file defining the category (if
   applicable)
``lines(BeginLine, EndLine)``
   Source file begin and end lines of the category definition (if
   applicable)
``events``
   Messages sent from the category generate events
``source_data``
   Source data available for the category
``public(Predicates)``
   List of public predicates declared by the category
``protected(Predicates)``
   List of protected predicates declared by the category
``private(Predicates)``
   List of private predicates declared by the category
``declares(Predicate, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for a predicate declared by the category
``defines(Predicate, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for a predicate defined by the category
``includes(Predicate, Entity, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for an object multifile predicate that are defined
   in the specified entity (the properties include
   ``number_of_clauses(Number)``, ``number_of_rules(Number)``, and
   ``line_count(Line)`` with ``Line`` being the begin line of the
   multifile predicate clause)
``provides(Predicate, Entity, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for other entity multifile predicate that are
   defined in the category (the properties include
   ``number_of_clauses(Number)``, ``number_of_rules(Number)``, and
   ``line_count(Line)`` with ``Line`` being the begin line of the
   multifile predicate clause)
``alias(Predicate, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for a :term:`predicate alias` declared by the category
   (the properties include ``for(Original)``, ``from(Entity)``,
   ``non_terminal(NonTerminal)``, and ``line_count(Line)`` with ``Line``
   being the begin line of the alias directive)
``calls(Call, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for predicate calls made by the category (``Call``
   is either a predicate indicator or a control construct such as
   ``::/1-2`` or ``^^/1`` with a predicate indicator as argument; note
   that ``Call`` may not be ground in case of a call to a control
   construct where its argument is only know at runtime; the properties
   include ``caller(Caller)``, ``alias(Alias)``, and
   ``line_count(Line)`` with both ``Caller`` and ``Alias`` being
   predicate indicators and ``Line`` being the begin line of the
   predicate clause or directive making the call)
``updates(Predicate, Properties)``
   List of :ref:`properties <grammar_entity_properties>` for dynamic predicate updates (and also access
   using the ``clause/2`` predicate) made by the object (``Predicate``
   is either a predicate indicator or a control construct such as
   ``::/1-2`` or ``:/2`` with a predicate indicator as argument; note
   that ``Predicate`` may not be ground in case of a control construct
   argument only know at runtime; the properties include
   ``updater(Updater)``, ``alias(Alias)``, and ``line_count(Line)`` with
   ``Updater`` being a (possibly multifile) predicate indicator,
   ``Alias`` being a predicate indicator, and ``Line`` being the begin
   line of the predicate clause or directive updating the predicate)
``number_of_clauses(Number)``
   Total number of predicate clauses defined in the category (includes
   both user-defined clauses and auxiliary clauses generated by the
   compiler or by the :ref:`expansion hooks <expansion_expansion>` but
   does not include clauses for multifile predicates defined for other
   entities or clauses for the category own multifile predicates
   contributed by other entities)
``number_of_rules(Number)``
   Total number of predicate rules defined in the category (includes
   both user-defined rules and auxiliary rules generated by the compiler
   or by the :ref:`expansion hooks <expansion_expansion>` but does not
   include rules for multifile predicates defined for other entities or
   rules for the category own multifile predicates contributed by other
   entities)
``number_of_user_clauses(Number)``
   Total number of user-defined predicate clauses defined in the
   category (does not include clauses for multifile predicates defined
   for other entities or clauses for the category own multifile predicates
   contributed by other entities)
``number_of_user_rules(Number)``
   Total number of user-defined predicate rules defined in the category
   (does not include rules for multifile predicates defined for other
   entities or rules for the category own multifile predicates contributed
   by other entities)

Some properties such as line numbers are only available when the
category is defined in a source file compiled with the
:ref:`source_data <flag_source_data>` flag turned on.

The properties that return the number of clauses (rules) report the
clauses (rules) *textually defined in the object* for both multifile and
non-multifile predicates. Thus, these numbers exclude clauses (rules)
for multifile predicates *contributed* by other entities.

.. _categories_importing:

Importing categories
--------------------

Any number of objects can import a category. In addition, an object may
import any number of categories. The syntax is very simple:

::

   :- object(Object,
       imports([Category1, Category2, ...])).
       ...
   :- end_object.

To make all public predicates imported via a category protected or to
make all public and protected predicates private we prefix the
category's name with the corresponding keyword:

::

   :- object(Object,
       imports(private::Category)).
       ...
   :- end_object.

or:

::

   :- object(Object,
       imports(protected::Category)).
       ...
   :- end_object.

Omitting the scope keyword is equivalent to writing:

::

   :- object(Object,
       imports(public::Category)).
       ...
   :- end_object.

.. _categories_predicates:

Calling category predicates
---------------------------

Category predicates can be called from within an object by sending a
message to *self* or using a *super* call. Consider the following
category:

::

   :- category(output).

       :- public(out/1).

       out(X) :-
           write(X), nl.

   :- end_category.

The predicate ``out/1`` can be called from within an object importing
the category by simply sending a message to *self*. For example:

::

   :- object(worker,
       imports(output)).

       ...
       do(Task) :-
           execute(Task, Result),
           ::out(Result).
       ...

   :- end_object.

This is the recommended way of calling a category predicate that can be
specialized/overridden in a descendant object as the predicate definition
lookup will start from *self*.

A direct call the predicate definition found in an imported category can
be made using the :ref:`control_call_super_1`
control construct. For example:

::

   :- object(worker,
       imports(output)).

       ...
       do(Task) :-
           execute(Task, Result),
           ^^out(Result).
       ...

   :- end_object.

This alternative should only be used when the user knows a priori that
the category predicates will not be specialized or redefined by
descendant objects of the object importing the category. Its advantage
is that, when the :ref:`optimize <flag_optimize>` flag is turned
on, the Logtalk compiler will try to optimize the calls by using
:term:`static binding`. When :term:`dynamic binding` is used due to e.g.
the lack of sufficient information at compilation time, the performance is
similar to calling the category predicate using a message to :term:`self`
(in both cases a predicate lookup caching mechanism is used).

.. _categories_parametric:

Parametric categories
---------------------

Category predicates can be parameterized in the same way as object
predicates by using a compound term as the category identifier. The
category parameters can be accessed by calling the :ref:`methods_parameter_2`
or :ref:`methods_this_1` built-in local methods in
the category predicate clauses or by using :term:`parameter variables <parameter variable>`.
Category parameter values can be defined by the importing objects. For example:

::

   :- object(speech(Season, Event),
       imports([dress(Season), speech(Event)])).
       ...
   :- end_object.

Note that access to category parameters is only possible from within the
category. In particular, calls to the :ref:`methods_this_1` built-in local method
from category predicates always access the importing object identifier
(and thus object parameters, not category parameters).
