Scope

interface Scope

A small, stateful factory of Terms that remembers the Variables it has already created, by Var.name.

Var.of always creates a brand-new variable, even when called twice with the same name (see Var for why). That is correct in general, but gets in the way whenever the same variable must occur more than once while building a term or a clause — e.g. member(H, [_|T]) :- member(H, T)., where H and T each occur twice. Scope solves this: asking it for varOf with a name it has already seen returns the very same Var instance, instead of minting a new, unrelated one. Every other factory method it exposes (structOf, ruleOf, logicListOf, numOf, and so on) mirrors the corresponding static factory found on the relevant Term subtype, so building terms inside a Scope reads exactly like building them outside of one — variable reuse just becomes automatic. anonymous is the one deliberate exception: each call still returns a fresh, unrelated variable, since anonymous variables are never meant to be shared.

A Scope is intentionally mutable and meant to be used once, for one self-contained unit of construction (typically: one clause). It is not meant to be reset or reused across unrelated terms. Term.freshCopy() and Term.freshCopy(Scope) are implemented on top of Scope for the very same reason: a fresh, empty scope guarantees that repeated occurrences of a variable are refreshed consistently.

Usage example, building the member/2 clause above and having H/T refer to the same variables in both head and body:

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

See also

Inheritors

Types

Link copied to clipboard
object Companion

Factory functions for creating Scopes.

Properties

Link copied to clipboard
open val _: Var

Shorthand for anonymous, letting Kotlin code refer to a fresh anonymous variable as _, mirroring Prolog's own syntax.

Link copied to clipboard
abstract val emptyBlock: EmptyBlock

Shorthand for the empty logic block, i.e. EmptyBlock.

Link copied to clipboard

Shorthand for the empty logic list, i.e. EmptyList.

Link copied to clipboard
abstract val fail: Truth

Shorthand for Truth.FAIL.

Link copied to clipboard
abstract val variables: Map<String, Var>

The Variables created so far through this Scope, indexed by their simple Var.name.

Functions

Link copied to clipboard
abstract fun anonymous(): Var

Creates a fresh, unrelated anonymous Var. See Var.anonymous.

Link copied to clipboard
abstract fun atomOf(value: Char): Atom
abstract fun atomOf(value: String): Atom

Creates an Atom with the given value. See Atom.of.

Link copied to clipboard
abstract fun blockOf(terms: Iterable<Term>): Block
abstract fun blockOf(terms: Sequence<Term>): Block
abstract fun blockOf(vararg terms: Term): Block

Creates a Block out of terms. See Block.of.

Link copied to clipboard
abstract fun clauseOf(head: Struct?, vararg body: Term): Clause

Creates a Clause with the given (possibly null) head and body. See Clause.of.

Link copied to clipboard
abstract fun consOf(head: Term, tail: Term): Cons

Creates a Cons with the given head and tail. See Cons.of.

Link copied to clipboard
abstract operator fun contains(variable: Var): Boolean

Checks whether variable was created through this Scope.

abstract operator fun contains(variable: String): Boolean

Checks whether a variable named variable was created through this Scope.

Link copied to clipboard
abstract fun directiveOf(body1: Term, vararg body: Term): Directive

Creates a Directive with the given body. See Directive.of.

Link copied to clipboard
abstract fun factOf(head: Struct): Fact

Creates a Fact with the given head. See Fact.of.

Link copied to clipboard
abstract operator fun get(variable: String): Var?

Retrieves the Var named variable previously created through this Scope, or null if none exists yet.

Link copied to clipboard
abstract fun indicatorOf(name: Term, arity: Term): Indicator
abstract fun indicatorOf(name: String, arity: Int): Indicator

Creates an Indicator with the given name and arity. See Indicator.of.

Link copied to clipboard
abstract fun intOf(value: Byte): Integer
abstract fun intOf(value: Int): Integer
abstract fun intOf(value: Long): Integer
abstract fun intOf(value: Short): Integer
abstract fun intOf(value: BigInteger): Integer

Creates an Integer out of value. See Integer.of.

abstract fun intOf(value: String): Integer

Parses value into an Integer. See Integer.of.

abstract fun intOf(value: String, radix: Int): Integer

Parses value, expressed in the given radix, into an Integer. See Integer.of.

Link copied to clipboard
abstract fun logicListFrom(terms: Iterable<Term>, last: Term? = null): List
abstract fun logicListFrom(terms: Sequence<Term>, last: Term? = null): List
abstract fun logicListFrom(vararg terms: Term, last: Term? = null): List

Creates a (possibly partial) logic LogicList out of terms, terminated by last. See List.from.

Link copied to clipboard
abstract fun logicListOf(terms: Iterable<Term>): List
abstract fun logicListOf(terms: Sequence<Term>): List
abstract fun logicListOf(vararg terms: Term): List

Creates a logic LogicList out of terms. See List.of.

Link copied to clipboard
abstract fun numOf(value: Byte): Integer
abstract fun numOf(value: Int): Integer
abstract fun numOf(value: Long): Integer
abstract fun numOf(value: Short): Integer
abstract fun numOf(value: BigInteger): Integer

Creates an Integer out of value. See Numeric.of.

abstract fun numOf(value: Double): Real
abstract fun numOf(value: Float): Real
abstract fun numOf(value: BigDecimal): Real

Creates a Real out of value. See Numeric.of.

abstract fun numOf(value: Number): Numeric

Creates a Numeric out of value. See Numeric.of.

abstract fun numOf(value: String): Numeric

Parses value into a Numeric, preferring Integer and falling back to Real. See Numeric.of.

Link copied to clipboard
abstract fun realOf(value: Double): Real
abstract fun realOf(value: Float): Real
abstract fun realOf(value: BigDecimal): Real

Creates a Real out of value. See Real.of.

abstract fun realOf(value: String): Real

Parses value into a Real. See Real.of.

Link copied to clipboard
abstract fun ruleOf(head: Struct, body1: Term, vararg body: Term): Rule

Creates a Rule with the given head and body. See Rule.of.

Link copied to clipboard
abstract fun structOf(functor: String, args: Iterable<Term>): Struct
abstract fun structOf(functor: String, args: List<Term>): Struct
abstract fun structOf(functor: String, args: Sequence<Term>): Struct
abstract fun structOf(functor: String, vararg args: Term): Struct

Creates a Struct with the given functor and args. See Struct.of.

Link copied to clipboard
abstract fun substitutionOf(assignments: Iterable<Pair<Var, Term>>): Substitution
abstract fun substitutionOf(assignments: Sequence<Pair<Var, Term>>): Substitution

Creates a Substitution out of the given Var-Term.

abstract fun substitutionOf(vararg assignments: Pair<String, Term>): Substitution

Creates a Substitution out of the given Var.name-Term.

Link copied to clipboard
abstract fun truthOf(value: Boolean): Truth

Creates a Truth out of value. See Truth.of.

Link copied to clipboard
abstract fun tupleOf(terms: Iterable<Term>): Tuple
abstract fun tupleOf(terms: Sequence<Term>): Tuple
abstract fun tupleOf(vararg terms: Term): Tuple

Creates a Tuple out of terms. See Tuple.of.

Link copied to clipboard
abstract fun unifierOf(assignments: Iterable<Pair<Var, Term>>): Substitution.Unifier
abstract fun unifierOf(assignments: Sequence<Pair<Var, Term>>): Substitution.Unifier

Creates a Substitution.Unifier out of the given Var-Term.

abstract fun unifierOf(vararg assignments: Pair<String, Term>): Substitution.Unifier

Creates a Substitution.Unifier out of the given Var.name-Term.

Link copied to clipboard
abstract fun varOf(name: Char): Var
abstract fun varOf(name: String): Var

Retrieves the Var named name, creating and caching a new one on first request.

Link copied to clipboard
abstract fun whatever(): Var

Alias for anonymous.

Link copied to clipboard
abstract fun where(lambda: Scope.() -> Unit): Scope

Runs lambda with this Scope as its receiver, for its side effects, then returns this same Scope. Useful to populate a Scope with variables/terms without capturing the result of the last expression.

Link copied to clipboard
abstract fun <R> with(lambda: Scope.() -> R): R

Runs lambda with this Scope as its receiver, returning whatever lambda produces. This is the idiomatic way of using a Scope: Scope.empty().with { ruleOf(...) }, or more concisely Scope.empty { ruleOf(...) } via the companion overloads.