Term

interface Term: Comparable<Term>, Any

Base type for all logic terms. Terms are immutable tree-like data structures.

Fields

Name Description
abstract variables: Sequence<Var>

The sequence of Variables directly or indirectly contained in the current term. Variables are lazily returned in a non-deterministic order. Notice that no occurrence-check is performed. Thus, if a Term contains the same Variable twice or more times, then the variables sequence may contain as many occurrences of that Variable

open isVariable: Boolean

Checks whether the current term is a variable. This method is guaranteed to return true if and only if the current term is an instance of Var.

open isGround: Boolean

Checks whether the current term is ground. A term is ground is ground if and only if it does not contain any variable. This method is guaranteed to return true if and only if the variables property of the current term refers to an empty sequence.

open isStruct: Boolean

Checks whether the current term is a structure, i.e., either a compound term or an atom. This method is guaranteed to return true if and only if the current term is an instance of Struct.

open isAtom: Boolean

Checks whether the current term is an atom. This method is guaranteed to return true if and only if the current term is an instance of Atom.

open isConstant: Boolean

Checks whether the current term is a constant, i.e., either an atom or a number. This method is guaranteed to return true if and only if the current term is an instance of Constant.

open isNumber: Boolean

Checks whether the current term is a number, i.e., either an integer or a real number. This method is guaranteed to return true if and only if the current term is an instance of Numeric.

open isInt: Boolean

Checks whether the current term is an integer. This method is guaranteed to return true if and only if the current term is an instance of Integer.

open isReal: Boolean

Checks whether the current term is a real number. This method is guaranteed to return true if and only if the current term is an instance of Real.

open isList: Boolean

Checks whether the current term is a (logic) list, i.e., either an empty list or a Cons. This method is guaranteed to return true if and only if the current term is an instance of List.

open isTuple: Boolean

Checks whether the current term is a logic tuple, i.e., a right-recursive conjunction of 2 or more terms. This method is guaranteed to return true if and only if the current term is an instance of Tuple.

open isSet: Boolean

Checks whether the current term is a logic set. This method is guaranteed to return true if and only if the current term is an instance of Set.

open isEmptySet: Boolean

Checks whether the current term is an empty logic set. This method is guaranteed to return true if and only if the current term is an instance of EmptySet.

open isClause: Boolean

Checks whether the current term is a clause, i.e., either a rule or a directive. This method is guaranteed to return true if and only if the current term is an instance of Clause.

open isRule: Boolean

Checks whether the current term is a rule, or a fact. This method is guaranteed to return true if and only if the current term is an instance of Rule.

open isFact: Boolean

Checks whether the current term is a fact. This method is guaranteed to return true if and only if the current term is an instance of Fact.

open isDirective: Boolean

Checks whether the current term is a directive. This method is guaranteed to return true if and only if the current term is an instance of Directive.

open isCons: Boolean

Checks whether the current term is a cons. This method is guaranteed to return true if and only if the current term is an instance of Cons.

open isEmptyList: Boolean

Checks whether the current term is an empty logic list. This method is guaranteed to return true if and only if the current term is an instance of EmptyList.

open isTrue: Boolean

Checks whether the current term is the true atom. This method is guaranteed to return true if and only if the current term is an instance of Truth and its Truth.value is "true".

open isFail: Boolean

Checks whether the current term is the either the fail atom or the false atom. This method is guaranteed to return true if and only if the current term is an instance of Truth and its Truth.value is "fail" or "false".

open isIndicator: Boolean

Checks whether the current term is an indicator. This method is guaranteed to return true if and only if the current term is an instance of Indicator.

Methods

as

open fun <T : Term> as(): T

Alias for castTo Helper method aimed at down-casting Terms using a fluent style.

ReturnValue

Name Description
T

the current term, cast'd into type T

castTo

open fun <T : Term> castTo(): T

Helper method aimed at down-casting Terms using a fluent style

ReturnValue

Name Description
T

the current term, cast'd into type T

compareTo

open fun compareTo(other: Term): Int

Compares this term to the provided one, returning a positive integer if this term precedes other, a negative integer if other precedes this term, or 0 otherwise

Parameters

Name Description
other: Term

is the term to be compared against this term

ReturnValue

Name Description
Int

an Int indicating whether this term precedes other or not

equals

abstract fun equals(other: Term, useVarCompleteName: Boolean): Boolean

Checks whether another term is equals to the current one or not, by explicitly letting the client decide whether to rely or not on Varriables complete names for checking equality among two Variables. If useVarCompleteName is true, Variables are compared through their Var.completeName property. Otherwise, they are compared through their Var.name property. Other sorts of terms are compared as Term.equals(Any?).

For example, if useVarCompleteName is true the following comparison should fail:


Var.of("X") == Var.of("X")

otherwise, it should succeed.

Parameters

Name Description
other: Term

is the Term the current Term should be compared with

useVarCompleteName: Boolean

indicates whether Var should be compared through their Var.completeName property or through their Var.name property

ReturnValue

Name Description
Boolean

true if the two terms are equal, or false, otherwise

equals

abstract fun equals(other: Any?): Boolean

Parameters

Name Description
other: Any?

ReturnValue

Name Description
Boolean

structurallyEquals

abstract infix fun structurallyEquals(other: Term): Boolean

Checks whether another term is structurally equals to the current one or not. Structural equivalence is a looser type of equivalence (w.r.t. term equivalence) where:

  • numbers are compared by value, e.g. 1 is structurally equal to 1.0
  • variables are always considered equal, e.g. f(X) is structurally equal to f(_)

Parameters

Name Description
other: Term

is the Term the current Term should be compared with

ReturnValue

Name Description
Boolean

true if the two terms are structurally equal, or false, otherwise

freshCopy

open fun freshCopy(): Term

Returns a fresh copy of this Term, that is, an instance of Term which is equal to the current one in any aspect, except variables directly or indirectly contained into this Term, which are refreshed. This means that it could return itself, if no variable is present (ground term), or a new Term with freshly generated variables.

Variables are refreshed consistently, meaning that, if more variables exists within this Term having the same name, all fresh copies of such variables will have the same complete name.

Example: "f(X, g(X))".freshCopy() returns something like "f(X_1, g(X_1))" instead of "f(X_1, g(X_2))"

Notice that, if the current term is ground, the same object may be returned as a result by this method.

ReturnValue

Name Description
Term

a fresh copy of the current term which is different because variables are consistently renamed

freshCopy

open fun freshCopy(scope: Scope): Term

Returns a fresh copy of this Term, similarly to freshCopy, possibly reusing variables from the provided scope, if any

Parameters

Name Description
scope: Scope

the Scope containing variables to be used in copying

ReturnValue

Name Description
Term

a fresh copy of the current term which is different because variables are consistently renamed

apply

open fun apply(substitution: Substitution): Term

Applies a Substitution to the current term, producing a new Term which differs from the current one because variables are replaced by their values, according to the binding carried by substitution.

Notice that, if the current term is ground, or the provided substitution is empty, the same object may be returned as a result by this method.

Parameters

Name Description
substitution: Substitution

is the Substitution to be applied to the current term

ReturnValue

Name Description
Term

a Term where variables in substitution are replaced by their values

apply

open fun apply(substitution: Substitution, vararg substitutions: Substitution): Term

Applies one or more Substitutions to the current term, producing a new Term which differs from the current one because variables are replaced by their values, according to the binding carried by the provided substitutions.

This method behaves like apply, assuming that the provided substitutions have been merged by means of Substitution.of.

Parameters

Name Description
substitution: Substitution

is the first Substitution to be applied to the current term

vararg substitutions: Substitution

is the vararg argument representing the 2nd, 3rd, etc., Substitutions to be applied

ReturnValue

Name Description
Term

a Term where variables in substitution are replaced by their values

get

open operator fun get(substitution: Substitution, vararg substitutions: Substitution): Term

This is an alias for apply aimed at supporting a square-brackets syntax for substitutions applications in Kotlin programs. It lets programmers write term[substitution] instead of term.apply(substitution). It applies one or more Substitutions to the current term, producing a new Term which differs from the current one because variables are replaced by their values, according to the binding carried by the provided substitutions.

This method behaves like apply, assuming that the provided substitutions have been merged by means of Substitution.of.

Parameters

Name Description
substitution: Substitution

is the first Substitution to be applied to the current term

vararg substitutions: Substitution

is the vararg argument representing the 2nd, 3rd, etc., Substitutions to be applied

ReturnValue

Name Description
Term

a Term where variables in substitution are replaced by their values

accept

open fun <T> accept(visitor: TermVisitor<T>): T

Lets the provided TermVisitor navigate the current term and build an object of type T. Such an object is then returned as a result by this method.

See also: https://en.wikipedia.org/wiki/Visitor_pattern for more information concerning the GoF's Visitor pattern.

Parameters

Name Description
visitor: TermVisitor<T>

is a TermVisitor, i.e., an object aimed at navigating the current term

ReturnValue

Name Description
T

an object of type T, produced by visitor through its visit

toString

abstract fun toString(): String

ReturnValue

Name Description
String

hashCode

abstract fun hashCode(): Int

ReturnValue

Name Description
Int

Extensions

unfoldGoals

fun Term.unfoldGoals(): Sequence<Term>

Receiver

Name Description
Term

ReturnValue

Name Description
Sequence<Term>

toGoals

fun Term.toGoals(): Cursor<Term>

Receiver

Name Description
Term

ReturnValue

Name Description
Cursor<Term>

isWellFormed

fun Term.isWellFormed(): Boolean

Check whether the receiver term is a well-formed predication

Receiver

Name Description
Term

ReturnValue

Name Description
Boolean

prepareForExecutionAsGoal

fun Term.prepareForExecutionAsGoal(): Struct

Prepares the receiver Goal for execution

For example, the goal A is transformed, after preparation for execution, as the Term: call(A)

Receiver

Name Description
Term

ReturnValue

Name Description
Struct

eq

infix fun Term.eq(that: Term): Equation<Term, Term>

Creates an equation with this and that terms

Receiver

Name Description
Term

Parameters

Name Description
that: Term

ReturnValue

Name Description
Equation<Term, Term>