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 isVar: 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 isTruth: Boolean

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

open isRecursive: Boolean

Checks whether the current term is a recursive structure, i.e., a list, a tuple, or a block. This method is guaranteed to return true if and only if the current term is an instance of Recursive.

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 isInteger: 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 isBlock: Boolean

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

open isEmptyBlock: Boolean

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

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?

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

ReturnValue

Name Description
T?

the current term, cast'd into type T, or null, in case the current term is not an instance of 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

abstract 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

abstract 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

abstract 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

abstract 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

castToAtom

open fun castToAtom(): Atom

Casts the current Term to Atom, if possible

ReturnValue

Name Description
Atom

the current Term, casted to Atom

castToClause

open fun castToClause(): Clause

Casts the current Term to Clause, if possible

ReturnValue

Name Description
Clause

the current Term, casted to Clause

castToCons

open fun castToCons(): Cons

Casts the current Term to Cons, if possible

ReturnValue

Name Description
Cons

the current Term, casted to Cons

castToConstant

open fun castToConstant(): Constant

Casts the current Term to Constant, if possible

ReturnValue

Name Description
Constant

the current Term, casted to Constant

castToDirective

open fun castToDirective(): Directive

Casts the current Term to Directive, if possible

ReturnValue

Name Description
Directive

the current Term, casted to Directive

castToEmptyList

open fun castToEmptyList(): EmptyList

Casts the current Term to EmptyList, if possible

ReturnValue

Name Description
EmptyList

the current Term, casted to EmptyList

castToEmptyBlock

open fun castToEmptyBlock(): EmptyBlock

Casts the current Term to EmptyBlock, if possible

ReturnValue

Name Description
EmptyBlock

the current Term, casted to EmptyBlock

castToFact

open fun castToFact(): Fact

Casts the current Term to Fact, if possible

ReturnValue

Name Description
Fact

the current Term, casted to Fact

castToIndicator

open fun castToIndicator(): Indicator

Casts the current Term to Indicator, if possible

ReturnValue

Name Description
Indicator

the current Term, casted to Indicator

castToInteger

open fun castToInteger(): Integer

Casts the current Term to Integer, if possible

ReturnValue

Name Description
Integer

the current Term, casted to Integer

castToList

open fun castToList(): List

Casts the current Term to List, if possible

ReturnValue

Name Description
List

the current Term, casted to List

castToNumeric

open fun castToNumeric(): Numeric

Casts the current Term to Numeric, if possible

ReturnValue

Name Description
Numeric

the current Term, casted to Numeric

castToReal

open fun castToReal(): Real

Casts the current Term to Real, if possible

ReturnValue

Name Description
Real

the current Term, casted to Real

castToRule

open fun castToRule(): Rule

Casts the current Term to Rule, if possible

ReturnValue

Name Description
Rule

the current Term, casted to Rule

castToBlock

open fun castToBlock(): Block

Casts the current Term to Block, if possible

ReturnValue

Name Description
Block

the current Term, casted to Block

castToStruct

open fun castToStruct(): Struct

Casts the current Term to Struct, if possible

ReturnValue

Name Description
Struct

the current Term, casted to Struct

castToRecursive

open fun castToRecursive(): Recursive

Casts the current Term to Recursive, if possible

ReturnValue

Name Description
Recursive

the current Term, casted to Recursive

castToTerm

open fun castToTerm(): Term

Casts the current Term to Term

ReturnValue

Name Description
Term

the current Term

castToTruth

open fun castToTruth(): Truth

Casts the current Term to Truth, if possible

ReturnValue

Name Description
Truth

the current Term, casted to Truth

castToTuple

open fun castToTuple(): Tuple

Casts the current Term to Tuple, if possible

ReturnValue

Name Description
Tuple

the current Term, casted to Tuple

castToVar

open fun castToVar(): Var

Casts the current Term to Var, if possible

ReturnValue

Name Description
Var

the current Term, casted to Var

asAtom

open fun asAtom(): Atom?

Casts the current Term to Atom, if possible, or returns null otherwise

ReturnValue

Name Description
Atom?

the current Term, casted to Atom, or null, if the current term is not an instance of Atom

asClause

open fun asClause(): Clause?

Casts the current Term to Clause, if possible, or returns null otherwise

ReturnValue

Name Description
Clause?

the current Term, casted to Clause, or null, if the current term is not an instance of Clause

asCons

open fun asCons(): Cons?

Casts the current Term to Cons, if possible, or returns null otherwise

ReturnValue

Name Description
Cons?

the current Term, casted to Cons, or null, if the current term is not an instance of Cons

asConstant

open fun asConstant(): Constant?

Casts the current Term to Constant, if possible, or returns null otherwise

ReturnValue

Name Description
Constant?

the current Term, casted to Constant, or null, if the current term is not an instance of Constant

asDirective

open fun asDirective(): Directive?

Casts the current Term to Directive, if possible, or returns null otherwise

ReturnValue

Name Description
Directive?

the current Term, casted to Directive, or null, if the current term is not an instance of Directive

asEmptyList

open fun asEmptyList(): EmptyList?

Casts the current Term to EmptyList, if possible, or returns null otherwise

ReturnValue

Name Description
EmptyList?

the current Term, casted to EmptyList, or null, if the current term is not an instance of EmptyList

asEmptyBlock

open fun asEmptyBlock(): EmptyBlock?

Casts the current Term to EmptyBlock, if possible, or returns null otherwise

ReturnValue

Name Description
EmptyBlock?

the current Term, casted to EmptyBlock, or null, if the current term is not an instance of EmptyBlock

asFact

open fun asFact(): Fact?

Casts the current Term to Fact, if possible, or returns null otherwise

ReturnValue

Name Description
Fact?

the current Term, casted to Fact, or null, if the current term is not an instance of Fact

asIndicator

open fun asIndicator(): Indicator?

Casts the current Term to Indicator, if possible, or returns null otherwise

ReturnValue

Name Description
Indicator?

the current Term, casted to Indicator, or null, if the current term is not an instance of Indicator

asInteger

open fun asInteger(): Integer?

Casts the current Term to Integer, if possible, or returns null otherwise

ReturnValue

Name Description
Integer?

the current Term, casted to Integer, or null, if the current term is not an instance of Integer

asList

open fun asList(): List?

Casts the current Term to List, if possible, or returns null otherwise

ReturnValue

Name Description
List?

the current Term, casted to List, or null, if the current term is not an instance of List

asNumeric

open fun asNumeric(): Numeric?

Casts the current Term to Numeric, if possible, or returns null otherwise

ReturnValue

Name Description
Numeric?

the current Term, casted to Numeric, or null, if the current term is not an instance of Numeric

asReal

open fun asReal(): Real?

Casts the current Term to Real, if possible, or returns null otherwise

ReturnValue

Name Description
Real?

the current Term, casted to Real, or null, if the current term is not an instance of Real

asRule

open fun asRule(): Rule?

Casts the current Term to Rule, if possible, or returns null otherwise

ReturnValue

Name Description
Rule?

the current Term, casted to Rule, or null, if the current term is not an instance of Rule

asBlock

open fun asBlock(): Block?

Casts the current Term to Block, if possible, or returns null otherwise

ReturnValue

Name Description
Block?

the current Term, casted to Block, or null, if the current term is not an instance of Block

asStruct

open fun asStruct(): Struct?

Casts the current Term to Struct, if possible, or returns null otherwise

ReturnValue

Name Description
Struct?

the current Term, casted to Struct, or null, if the current term is not an instance of Struct

asRecursive

open fun asRecursive(): Recursive?

Casts the current Term to Recursive, if possible, or returns null otherwise

ReturnValue

Name Description
Recursive?

the current Term, casted to Recursive, or null, if the current term is not an instance of Recursive

asTerm

open fun asTerm(): Term

Casts the current Term to Term

ReturnValue

Name Description
Term

the current Term

asTruth

open fun asTruth(): Truth?

Casts the current Term to Truth, if possible, or returns null otherwise

ReturnValue

Name Description
Truth?

the current Term, casted to Truth, or null, if the current term is not an instance of Truth

asTuple

open fun asTuple(): Tuple?

Casts the current Term to Tuple, if possible, or returns null otherwise

ReturnValue

Name Description
Tuple?

the current Term, casted to Tuple, or null, if the current term is not an instance of Tuple

asVar

open fun asVar(): Var?

Casts the current Term to Var, if possible, or returns null otherwise

ReturnValue

Name Description
Var?

the current Term, casted to Var, or null, if the current term is not an instance of Var

hashCode

abstract fun hashCode(): Int

ReturnValue

Name Description
Int

toString

abstract fun toString(): String

ReturnValue

Name Description
String

Extensions

toClause

fun Term.toClause(source: Any?, line: Int, column: Int): Clause

Receiver

Name Description
Term

Parameters

Name Description
source: Any?
line: Int
column: Int

ReturnValue

Name Description
Clause

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

evalAsExpression

fun Term.evalAsExpression(request: Request<*>, index: Int?): Term

Receiver

Name Description
Term

Parameters

Name Description
request: Request<*>
index: Int?

ReturnValue

Name Description
Term

evalAsArithmeticExpression

fun Term.evalAsArithmeticExpression(request: Request<*>, index: Int?): Numeric

Receiver

Name Description
Term

Parameters

Name Description
request: Request<*>
index: Int?

ReturnValue

Name Description
Numeric

eq

infix fun Term.eq(that: Term): Equation

Creates an equation with this and that terms

Receiver

Name Description
Term

Parameters

Name Description
that: Term

ReturnValue

Name Description
Equation