Var

interface Var : Term

A logic variable, i.e. a placeholder for a Term that is yet to be determined.

A Var is created out of a simple, human-readable name (e.g. X), but its real identity is its completeName, which pairs name with a hidden, per-name sequential id. This is why, perhaps surprisingly, Var.of("X") == Var.of("X") is always false: each call mints a genuinely new variable, and relying on the name alone for equality would make every clause containing a variable called X accidentally alias every other clause using that same name. See the "Variables and Scoping" explanation page in the project documentation for the full rationale.

Because of this, code that needs to refer to the same variable more than once while building a term (e.g. member(H, [_|T]) :- member(H, T)., where H and T each occur twice) should not call Var.of repeatedly with the same name: doing so creates unrelated variables. Instead, either keep a single Var reference around and reuse it, or use a Scope, which caches variables by name and hands back the same instance on repeated requests:

Scope.of("H", "T") {
ruleOf(structOf("member", varOf("H"), consOf(anonymous(), varOf("T"))), structOf("member", varOf("H"), varOf("T")))
}

The anonymous variable (conventionally named _) is the deliberate exception to variable reuse: every call to anonymous (or Var.anonymous) produces a fresh, unrelated variable, matching Prolog's convention that _ never binds to anything meaningful shared across occurrences.

See also

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val completeName: String

The full identity of this variable, obtained by combining name and id (e.g. "X_1"). Two Var instances are equals to each other (via Term.equals(Any?)) if and only if their completeNames match; see Term.equals for how to compare variables by name alone instead.

Link copied to clipboard
abstract val id: String

The identifier distinguishing this variable from any other Var sharing the same name. Combined with name, it forms completeName. Identifiers are assigned automatically and cannot be chosen by client code, precisely so that no two independently-created variables can accidentally collide.

Link copied to clipboard

Whether this variable is the anonymous variable, i.e. whether its name is "_". Anonymous variables are never meant to be shared: each one created via Var.anonymous is distinct, regardless of this property being true for all of them.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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".

Link copied to clipboard
open val isGround: Boolean

Checks whether the current object is ground. An object 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 object refers to an empty sequence.

Link copied to clipboard

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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard

Whether name matches NAME_PATTERN, i.e. whether it could legally appear as a variable name in Prolog syntax. This is purely informational: constructing a Var via Var.of never validates or throws because of an ill-formed name.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard

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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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".

Link copied to clipboard
open val 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.

Link copied to clipboard
open val 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.

Link copied to clipboard
open override val 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.

Link copied to clipboard
abstract val name: String

The simple, human-readable name of this variable, as it would appear in Prolog source (e.g. "X"). This is not used to determine variable identity/equality; see completeName for that.

Link copied to clipboard
abstract val tags: Map<String, Any>
Link copied to clipboard
open override val variables: Sequence<Var>

The sequence of Variables directly or indirectly contained in the current object. 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

Functions

Link copied to clipboard
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.

Link copied to clipboard
abstract fun apply(substitution: Substitution): Term

Applies a Substitution to the current object, producing a new instance of T which differs from the current object because variables are replaced by their values, according to the binding carried by substitution.

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

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

Link copied to clipboard
open override fun <T : Term> as(): T?

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

Link copied to clipboard
open fun asAtom(): Atom?

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

Link copied to clipboard
open fun asBlock(): Block?

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

Link copied to clipboard
open fun asClause(): Clause?

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

Link copied to clipboard
open fun asCons(): Cons?

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

Link copied to clipboard
open fun asConstant(): Constant?

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

Link copied to clipboard
open fun asDirective(): Directive?

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

Link copied to clipboard

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

Link copied to clipboard
open fun asEmptyList(): EmptyList?

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

Link copied to clipboard
open fun asFact(): Fact?

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

Link copied to clipboard
open fun asIndicator(): Indicator?

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

Link copied to clipboard
open fun asInteger(): Integer?

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

Link copied to clipboard
open fun asList(): List?

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

Link copied to clipboard
open fun asNumeric(): Numeric?

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

Link copied to clipboard
open fun asReal(): Real?

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

Link copied to clipboard
open fun asRecursive(): Recursive?

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

Link copied to clipboard
open fun asRule(): Rule?

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

Link copied to clipboard
open fun asStruct(): Struct?

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

Link copied to clipboard
open fun asTerm(): Term

Casts the current Term to Term

Link copied to clipboard
open fun asTruth(): Truth?

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

Link copied to clipboard
open fun asTuple(): Tuple?

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

Link copied to clipboard
open override fun asVar(): Var

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

Link copied to clipboard
open override fun <T : Term> castTo(): T

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

Link copied to clipboard
open fun castToAtom(): Atom

Casts the current Term to Atom, if possible

Link copied to clipboard
open fun castToBlock(): Block

Casts the current Term to Block, if possible

Link copied to clipboard
open fun castToClause(): Clause

Casts the current Term to Clause, if possible

Link copied to clipboard
open fun castToCons(): Cons

Casts the current Term to Cons, if possible

Link copied to clipboard

Casts the current Term to Constant, if possible

Link copied to clipboard

Casts the current Term to Directive, if possible

Link copied to clipboard

Casts the current Term to EmptyBlock, if possible

Link copied to clipboard

Casts the current Term to EmptyList, if possible

Link copied to clipboard
open fun castToFact(): Fact

Casts the current Term to Fact, if possible

Link copied to clipboard

Casts the current Term to Indicator, if possible

Link copied to clipboard

Casts the current Term to Integer, if possible

Link copied to clipboard
open fun castToList(): List

Casts the current Term to List, if possible

Link copied to clipboard

Casts the current Term to Numeric, if possible

Link copied to clipboard
open fun castToReal(): Real

Casts the current Term to Real, if possible

Link copied to clipboard

Casts the current Term to Recursive, if possible

Link copied to clipboard
open fun castToRule(): Rule

Casts the current Term to Rule, if possible

Link copied to clipboard
open fun castToStruct(): Struct

Casts the current Term to Struct, if possible

Link copied to clipboard
open fun castToTerm(): Term

Casts the current Term to Term

Link copied to clipboard
open fun castToTruth(): Truth

Casts the current Term to Truth, if possible

Link copied to clipboard
open fun castToTuple(): Tuple

Casts the current Term to Tuple, if possible

Link copied to clipboard
open fun castToVar(): Var

Casts the current Term to Var, if possible

Link copied to clipboard
open operator override 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

Link copied to clipboard
open fun containsTag(name: String): Boolean
Link copied to clipboard
abstract operator override fun equals(other: Any?): Boolean

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?).

Link copied to clipboard
abstract override fun freshCopy(): Var

Returns a fresh variable, sharing this one's name but a newly minted, distinct completeName. Used, in particular, when a Term containing this variable is refreshed via Term.freshCopy.

abstract override fun freshCopy(scope: Scope): Var

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

Link copied to clipboard
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 object[substitution] instead of object.apply(substitution). It applies one or more Substitutions to the current object, 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.

Link copied to clipboard
open fun <T : Any> getTag(name: String): T?
Link copied to clipboard
abstract override fun hashCode(): Int
Link copied to clipboard
abstract fun replaceTags(tags: Map<String, Any>): Term
Link copied to clipboard
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:

Link copied to clipboard
abstract override fun toString(): String