Struct

interface Struct: Term, Any

Base type for compound Terms, a.k.a. structures. A Struct is characterised by a functor and a given (non-negative) amount of args, namely arity. Each argument can be a Term of any sort.

Fields

Name Description
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 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 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 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 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 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 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 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 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 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.

open 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

abstract functor: String

The functor of this Struct.

abstract isFunctorWellFormed: Boolean

Returns true if and only if functor matches Struct.WELL_FORMED_FUNCTOR_PATTERN.

open arity: Int

The total amount of arguments of this Struct. This is equal to the length of args.

open indicator: Indicator

The indicator corresponding to this Struct, i.e. functor/arity.

abstract args: List<Term>

List of arguments of this Struct.

open argsSequence: Sequence<Term>

Sequence of arguments of this Struct.

Methods

freshCopy

abstract fun freshCopy(): Struct

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
Struct

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

freshCopy

abstract fun freshCopy(scope: Scope): Struct

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
Struct

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

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

append

open fun append(argument: Term): Struct

An alias for addLast.

Parameters

Name Description
argument: Term

ReturnValue

Name Description
Struct

addLast

abstract fun addLast(argument: Term): Struct

Creates a novel Struct which is a copy of the current one, expect that is has one more argument. The novel argument is appended at the end of the new Struct's arguments list.

Parameters

Name Description
argument: Term

is a Term of any sort

ReturnValue

Name Description
Struct

a new Struct, whose functor is equals to the current one, whose arity is greater than the current one, and whose last argument is argument

addFirst

abstract fun addFirst(argument: Term): Struct

Creates a novel Struct which is a copy of the current one, expect that is has one more argument. The novel argument is appended at the beginning of the new Struct's arguments list.

Parameters

Name Description
argument: Term

is a Term of any sort

ReturnValue

Name Description
Struct

a new Struct, whose functor is equals to the current one, whose arity is greater than the current one, and whose first argument is argument

insertAt

abstract fun insertAt(index: Int, argument: Term): Struct

Creates a novel Struct which is a copy of the current one, expect that is has one more argument. The novel argument is inserted into the new Struct's arguments list, at index index, wheres subsequent arguments indexes are shifted by 1.

Parameters

Name Description
index: Int

is the index the new argument should be inserted into

argument: Term

is a Term of any sort

ReturnValue

Name Description
Struct

a new Struct, whose functor is equals to the current one, whose arity is greater than the current one, and whose index-th argument is argument

setFunctor

abstract fun setFunctor(functor: String): Struct

Creates a novel Struct which is a copy of the current one, expect that is has a different functor.

Parameters

Name Description
functor: String

is a String representing the new functor

ReturnValue

Name Description
Struct

a new Struct, whose functor is functor, and whose arity and arguments list are equal to the current one

getArgAt

open fun getArgAt(index: Int): Term

Gets the index-th argument if this Struct.

Parameters

Name Description
index: Int

is the index the argument which should be retrieved

ReturnValue

Name Description
Term

the Term having position index in args

setArgs

abstract fun setArgs(vararg args: Term): Struct

Parameters

Name Description
vararg args: Term

ReturnValue

Name Description
Struct

setArgs

abstract fun setArgs(args: Iterable<Term>): Struct

Parameters

Name Description
args: Iterable<Term>

ReturnValue

Name Description
Struct

setArgs

abstract fun setArgs(args: Sequence<Term>): Struct

Parameters

Name Description
args: Sequence<Term>

ReturnValue

Name Description
Struct

get

open operator fun get(index: Int): Term

Alias for getArgAt. In Kotlin, this method enables the syntax struct[index].

Parameters

Name Description
index: Int

ReturnValue

Name Description
Term

Extensions

extractSignature

fun Struct.extractSignature(): Signature

Extracts this Struct indicator and converts it to Signature

Receiver

Name Description
Struct

ReturnValue

Name Description
Signature

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

CompanionObject

Struct

interface Struct: Term, Any

Base type for compound Terms, a.k.a. structures. A Struct is characterised by a functor and a given (non-negative) amount of args, namely arity. Each argument can be a Term of any sort.

Fields

Name Description
val WELL_FORMED_FUNCTOR_PATTERN: /** * The pattern of a well-formed functor for a [Struct]. * A functor is well-formed if and only if: * - it starts with a lower-case letter * - it only contains letters, digits, or underscores */

The pattern of a well-formed functor for a Struct. A functor is well-formed if and only if:

  • it starts with a lower-case letter
  • it only contains letters, digits, or underscores
val NON_PRINTABLE_CHARACTER_PATTERN: /** * The pattern of an ill-formed functor for a [Struct] containing * - non-printable characters (e.g. `\n`, `\r`, `\t`) * - other characters which must be escaped (e.g. `"`, `'`, `\`) */

The pattern of an ill-formed functor for a Struct containing

  • non-printable characters (e.g. \n, \r, \t)
  • other characters which must be escaped (e.g. ", ', \)

Methods

isWellFormedFunctor

fun isWellFormedFunctor(string: String): Boolean

Checks if a string matches WELL_FORMED_FUNCTOR_PATTERN.

Parameters

Name Description
string: String

the String to be checked

ReturnValue

Name Description
Boolean

true if string matches WELL_FORMED_FUNCTOR_PATTERN, false otherwise

enquoteFunctor

fun enquoteFunctor(string: String): String

Wraps the provided string within single quotes, unconditionally.

Parameters

Name Description
string: String

the String to be enquoted

ReturnValue

Name Description
String

a new String, beginning and ending with '

enquoteFunctorIfNecessary

fun enquoteFunctorIfNecessary(string: String): String

Wraps the provided string within single quotes, but only if it is not well-formed. Well-formed check is performed via the isWellFormedFunctor method.

Parameters

Name Description
string: String

the String to be enquoted

ReturnValue

Name Description
String

either a new String, beginning and ending with ', or string

functorNeedsEscape

fun functorNeedsEscape(string: String): Boolean

Checks if a string matches NON_PRINTABLE_CHARACTER_PATTERN.

Parameters

Name Description
string: String

the String to be checked

ReturnValue

Name Description
Boolean

true if string matches NON_PRINTABLE_CHARACTER_PATTERN, false otherwise

escapeFunctor

fun escapeFunctor(string: String, escapeSingleQuotes: Boolean, escapeDoubleQuotes: Boolean): String

Unconditionally, escapes all occurrences of the characters \n, \r, \t, and \ in string. Depending on the value of parameters escapeDoubleQuotes and escapeSingleQuotes, occurrences of characters " and ' may be escaped as well.

Parameters

Name Description
string: String

the String whose characters must should be escaped

escapeSingleQuotes: Boolean

decides whether single-quote characters (i.e., ') should be escaped (defaults to !escapeDoubleQuotes)

escapeDoubleQuotes: Boolean

decides whether double-quote characters (i.e., ") should be escaped (defaults to true)

ReturnValue

Name Description
String

either a new String, beginning and ending with ', or string

escapeFunctorIfNecessary

fun escapeFunctorIfNecessary(string: String, escapeSingleQuotes: Boolean, escapeDoubleQuotes: Boolean): String

Escapes all occurrences of the characters \n, \r, \t, and \ in string, but only if a preliminary check reveals that it contains some character which require escaping. Depending on the value of parameters escapeDoubleQuotes and escapeSingleQuotes, occurrences of characters " and ' may be escaped as well.

Parameters

Name Description
string: String

the String whose characters must should be escaped

escapeSingleQuotes: Boolean

decides whether single-quote characters (i.e., ') should be escaped (defaults to !escapeDoubleQuotes)

escapeDoubleQuotes: Boolean

decides whether double-quote characters (i.e., ") should be escaped (defaults to true)

ReturnValue

Name Description
String

either a new String, beginning and ending with ', or string

template

fun template(functor: String, arity: Int): Struct

Creates a new Struct with functor as functor and a given amount of anonymous Variables, namely arity. Instances of Struct are always created of the most adequate sub-type of Struct.

Parameters

Name Description
functor: String
arity: Int

ReturnValue

Name Description
Struct

a new instance of Struct

of

fun of(functor: String, args: List<Term>): Struct

Creates a new Struct from the given KtList of Terms. Instances of Struct are always created of the most adequate sub-type of Struct. This implies that, whenever possible, the creation of the new Struct may be delegated to:

  • Cons.of
  • Rule.of
  • Directive.of
  • Tuple.of
  • Indicator.of
  • Block.of

depending on the value of functor and on the amount and sorts of items in args.

Parameters

Name Description
functor: String

is the String to be used as functor of the new Struct

args: List<Term>

is the KtList of Terms to be used as argument list of the new Struct

ReturnValue

Name Description
Struct

a new instance of Struct (or of some particular sub-type of Struct)

of

fun of(functor: String, vararg args: Term): Struct

Creates a new Struct from the given Terms. Instances of Struct are always created of the most adequate sub-type of Struct. This implies that, whenever possible, the creation of the new Struct may be delegated to:

  • Cons.of
  • Rule.of
  • Directive.of
  • Tuple.of
  • Indicator.of
  • Block.of

depending on the value of functor and on the amount and sorts provided Terms.

Parameters

Name Description
functor: String

is the String to be used as functor of the new Struct

vararg args: Term

is the vararg array of Terms to be used as argument list of the new Struct

ReturnValue

Name Description
Struct

a new instance of Struct (or of some particular sub-type of Struct)

of

fun of(functor: String, args: Sequence<Term>): Struct

Creates a new Struct from the given Sequence of Terms. Instances of Struct are always created of the most adequate sub-type of Struct. This implies that, whenever possible, the creation of the new Struct may be delegated to:

  • Cons.of
  • Rule.of
  • Directive.of
  • Tuple.of
  • Indicator.of
  • Block.of

depending on the value of functor and on the amount and sorts of items in args.

Parameters

Name Description
functor: String

is the String to be used as functor of the new Struct

args: Sequence<Term>

is the Sequence of Terms to be used as argument list of the new Struct

ReturnValue

Name Description
Struct

a new instance of Struct (or of some particular sub-type of Struct)

of

fun of(functor: String, args: Iterable<Term>): Struct

Creates a new Struct from the given Iterable of Terms. Instances of Struct are always created of the most adequate sub-type of Struct. This implies that, whenever possible, the creation of the new Struct may be delegated to:

  • Cons.of
  • Rule.of
  • Directive.of
  • Tuple.of
  • Indicator.of
  • Block.of

depending on the value of functor and on the amount and sorts of items in args.

Parameters

Name Description
functor: String

is the String to be used as functor of the new Struct

args: Iterable<Term>

is the Iterable of Terms to be used as argument list of the new Struct

ReturnValue

Name Description
Struct

a new instance of Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, terms: List<Term>): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either Tuple in case the argument operator is adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

terms: List<Term>

the KtList of Terms to be folded

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, terms: List<Term>, terminal: Term?): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, if terminal is non-null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n, T) ...))

where T is the value of terminal, and t_n is the last item in terms. Conversely, if terminal is null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either List, or Tuple in case the arguments operator and terminal are adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

terms: List<Term>

the KtList of Terms to be folded

terminal: Term?

the termination Term used as the deepest one in the returned Struct, or null if t_n should be used instead

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, terms: Sequence<Term>, terminal: Term?): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, if terminal is non-null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n, T) ...))

where T is the value of terminal, and t_n is the last item in terms. Conversely, if terminal is null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either List, or Tuple in case the arguments operator and terminal are adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

terms: Sequence<Term>

the Sequence of Terms to be folded

terminal: Term?

the termination Term used as the deepest one in the returned Struct, or null if t_n should be used instead

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, terms: Sequence<Term>): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either Tuple in case the argument operator is adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

terms: Sequence<Term>

the Sequence of Terms to be folded

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, terms: Iterable<Term>, terminal: Term?): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, if terminal is non-null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n, T) ...))

where T is the value of terminal, and t_n is the last item in terms. Conversely, if terminal is null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either List, or Tuple in case the arguments operator and terminal are adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

terms: Iterable<Term>

the Iterable of Terms to be folded

terminal: Term?

the termination Term used as the deepest one in the returned Struct, or null if t_n should be used instead

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, terms: Iterable<Term>): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either Tuple in case the argument operator is adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

terms: Iterable<Term>

the Iterable of Terms to be folded

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, vararg terms: Term, terminal: Term?): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, if terminal is non-null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n, T) ...))

where T is the value of terminal, and t_n is the last item in terms. Conversely, if terminal is null, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either List, or Tuple in case the arguments operator and terminal are adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

vararg terms: Term

the vararg array of Terms to be folded

terminal: Term?

the termination Term used as the deepest one in the returned Struct, or null if t_n should be used instead

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

fold

fun fold(operator: String, vararg terms: Term): Struct

Folds the Terms in terms from left to right, creating binary structures having operator as functor. Let f be the value of operator, and let t_i be the i-th term in terms. Then, this method constructs the Structure


f(t_1, f(t_2, ... f(t_n-1, t_n) ...))

Of course, this method will return an instance of either Tuple in case the argument operator is adequate.

Parameters

Name Description
operator: String

the functor of the Structures used to fold the terms

vararg terms: Term

the vararg array of Terms to be folded

ReturnValue

Name Description
Struct

a new Struct (or of some particular sub-type of Struct)

Extensions

parse

fun Struct.Companion.parse(input: String, operators: OperatorSet): Struct

Receiver

Name Description
Struct.Companion

Parameters

Name Description
input: String
operators: OperatorSet

ReturnValue

Name Description
Struct

parse

fun Struct.Companion.parse(input: String): Struct

Receiver

Name Description
Struct.Companion

Parameters

Name Description
input: String

ReturnValue

Name Description
Struct