Theory

A Prolog knowledge base: an ordered collection of Clauses (facts, rules and directives) that can be queried by unification against a goal, and grown or shrunk with the usual ISO-inspired vocabulary (assertA/assertZ/retract/abolish).

Theory is deliberately a thin, Prolog-flavoured façade: how clauses are actually stored, searched and mutated is delegated to a ClauseCollection (see the it.unibo.tuprolog.collections package) chosen at construction time. Two independent axes of variation exist, both hidden behind this same interface:

  • mutable vs. immutable: a plain Theory is a persistent data structure — every assertA/assertZ/ retract returns a new Theory, leaving this untouched; a MutableTheory (obtained via toMutableTheory) edits itself in place instead, which is cheaper when a program asserts/retracts clauses very frequently (e.g. a Prolog dynamic predicate).

  • indexed vs. listed: an indexed theory (the default, see indexedOf) keeps clauses in a discrimination-tree-like structure (see it.unibo.tuprolog.collections.rete.custom.ReteTree) indexed by directive-vs-rule, functor, arity and first-argument shape, trading memory and update cost for fast lookup; a listed theory (see listedOf) is a thin wrapper around an ordered clause list, cheap to build and to keep in insertion order, but linear to query (each get scans and unifies against every clause). A knowledge base that is queried far more often than it is changed (a typical static, "library" theory) benefits from indexing; one that is asserted/retracted on nearly every resolution step, or kept small, may not be worth indexing at all — which is exactly why solvers (see :solve) build their static knowledge base with of (indexed) and their dynamic one with listedOf.

Example — building a small theory and querying it:

val theory =
Theory.indexedOf(
Unificator.default,
Fact.of(Struct.of("parent", Atom.of("alice"), Atom.of("bob"))),
Fact.of(Struct.of("parent", Atom.of("bob"), Atom.of("carol"))),
)
val goal = Struct.of("parent", Var.of("Who"), Atom.of("bob"))
theory[goal].toList() // rules/facts whose head *might* unify with `parent(Who, bob)`

See also

Inheritors

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val clauses: Iterable<Clause>

All Clauses in this theory

Link copied to clipboard

Only clauses that are Directives

Link copied to clipboard
abstract val isEmpty: Boolean

Whether this Theory is empty or not

Link copied to clipboard
open val isMutable: Boolean

Whether this Theory supports in-place mutation; true for MutableTheory, false otherwise.

Link copied to clipboard
abstract val isNonEmpty: Boolean

Whether this Theory is full or not

Link copied to clipboard
open val rules: Iterable<Rule>

Only clauses that are Rules

Link copied to clipboard
abstract val size: Long

The amount of clauses in this Theory

Link copied to clipboard
abstract val tags: Map<String, Any>
Link copied to clipboard
abstract val unificator: Unificator

Functions

Link copied to clipboard
abstract fun abolish(indicator: Indicator): Theory

Removes from this theory all the clauses whose head has the given indicator as functor/arity, mirroring ISO Prolog's abolish/1.

Link copied to clipboard
abstract fun assertA(clause: Clause): Theory

Returns a Theory with the given clause inserted before all other clauses of this one.

open fun assertA(struct: Struct): Theory

Returns a Theory with the Fact built from struct inserted before all other clauses of this one

abstract fun assertA(clauses: Iterable<Clause>): Theory
abstract fun assertA(clauses: Sequence<Clause>): Theory

Returns a Theory with the given clauses inserted, in order, before all other clauses of this one.

Link copied to clipboard
abstract fun assertZ(clause: Clause): Theory

Returns a Theory with the given clause inserted after all other clauses of this one.

open fun assertZ(struct: Struct): Theory

Returns a Theory with the Fact built from struct inserted after all other clauses of this one

abstract fun assertZ(clauses: Iterable<Clause>): Theory
abstract fun assertZ(clauses: Sequence<Clause>): Theory

Returns a Theory with the given clauses inserted, in order, after all other clauses of this one.

Link copied to clipboard
abstract fun clone(): Theory

Returns a copy of this Theory; for an immutable theory this may return this unchanged.

Link copied to clipboard
abstract operator fun contains(clause: Clause): Boolean

Checks if given clause is contained in this theory

abstract operator fun contains(indicator: Indicator): Boolean

Checks if clauses exist in this theory having the specified indicator as head.

abstract operator fun contains(head: Struct): Boolean

Checks if given clause is present in this theory

Link copied to clipboard
open fun containsTag(name: String): Boolean
Link copied to clipboard
abstract fun equals(other: Theory, useVarCompleteName: Boolean): Boolean

Checks whether this theory and other contain the same clauses, in the same order. If useVarCompleteName is true, variables are compared also by their (possibly generated) complete name, rather than only by their position within each clause.

Link copied to clipboard
abstract operator fun get(clause: Clause): Sequence<Clause>

Retrieves the Sequence of clauses in this theory that could unify against the given clause

abstract operator fun get(indicator: Indicator): Sequence<Rule>

Retrieves all the Rules in this theory having the specified indicator as head.

abstract operator fun get(head: Struct): Sequence<Rule>

Retrieves the Sequence of Rules in this theory whose head could unify against the given head

Link copied to clipboard
open fun <T : Any> getTag(name: String): T?
Link copied to clipboard
abstract operator fun iterator(): Iterator<Clause>
Link copied to clipboard
open operator fun plus(clause: Clause): Theory

Adds the given clause to this Theory; equivalent to assertZ.

abstract operator fun plus(theory: Theory): Theory

Adds all the clauses of the given theory after all the clauses of this one, returning the resulting Theory (as a new instance, unless is emptyTheory.isEmpty).

Link copied to clipboard
abstract fun replaceTags(tags: Map<String, Any>): Theory
Link copied to clipboard
abstract fun retract(clause: Clause): RetractResult<Theory>

Tries to delete the first clause in this theory unifying against the given clause, returning a RetractResult.Success wrapping the resulting Theory and the removed clause, or a RetractResult.Failure wrapping this same theory if no clause matched.

Tries to delete a clause whose head unifies against the given head; equivalent to retract(Rule.of(head, _))

abstract fun retract(clauses: Iterable<Clause>): RetractResult<Theory>
abstract fun retract(clauses: Sequence<Clause>): RetractResult<Theory>

Tries to delete, from this theory, one clause unifying against each of the given clauses patterns.

Link copied to clipboard
abstract fun retractAll(clause: Clause): RetractResult<Theory>

Tries to delete all the clauses in this theory unifying against the given clause

Tries to delete all the clauses whose head unifies against the given head; equivalent to retractAll(Rule.of(head, _))

Link copied to clipboard
abstract fun setUnificator(unificator: Unificator): Theory

Returns a Theory with the same clauses as this one, but using the given unificator to match them.

Link copied to clipboard
abstract fun toImmutableTheory(): Theory

Returns an immutable Theory with the same clauses (and Unificator) as this one; this, if it already is one.

Link copied to clipboard

Returns a MutableTheory with the same clauses (and Unificator) as this one; this, if it already is one.

Link copied to clipboard
abstract fun toString(asPrologText: Boolean): String

An enhanced toString that prints the theory in a Prolog program format, if asPrologText is true