Skip to content

utils

tuprolog.utils

General-purpose JVM interfaces shared by 2P-Kt's types.

  • Castable: types whose objects can be cast to sub-types by explicit methods (e.g. term.cast_to_atom(), term.as_atom()).
  • Taggable: immutable objects carrying named metadata, their tags (e.g. terms and substitutions).

Examples:

>>> from tuprolog.core import atom
>>> from tuprolog.utils import Castable, Taggable
>>> isinstance(atom("a"), Castable), isinstance(atom("a"), Taggable)
(True, True)

Castable module-attribute

Castable = _utils.Castable

Objects of a type hierarchy that can be cast to its sub-types by explicit methods, e.g. terms.

For terms, term.cast_to_struct() returns the term as a tuprolog.core.Struct, raising a Java ClassCastException when it is not one, while term.as_struct() returns None in that case (and so on for the other term types).

Note

2P-Kt's it.unibo.tuprolog.utils.Castable.

Taggable module-attribute

Taggable = _utils.Taggable

Immutable objects carrying tags, i.e. named metadata, e.g. terms and substitutions.

Members: tags (a map from tag names to values), get_tag(name) (a tag's value, or None), contains_tag(name), replace_tags(tags) (a copy whose tags are tags).

Examples:

>>> from tuprolog.core import atom
>>> tagged = atom("a").replace_tags({"source": "user"})
>>> tagged.get_tag("source"), tagged.contains_tag("other"), atom("a").contains_tag("source")
('user', False, False)
Note

2P-Kt's it.unibo.tuprolog.utils.Taggable.