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
Properties
Shorthand for the empty logic block, i.e. EmptyBlock.
Shorthand for the empty logic list, i.e. EmptyList.
Shorthand for Truth.FAIL.
Functions
Creates a fresh, unrelated anonymous Var. See Var.anonymous.
Creates a Directive with the given body. See Directive.of.
Creates an Indicator with the given name and arity. See Indicator.of.
Creates an Integer out of value. See Integer.of.
Parses value into an Integer. See Integer.of.
Parses value, expressed in the given radix, into an Integer. See Integer.of.
Creates an Integer out of value. See Numeric.of.
Creates a Real out of value. See Numeric.of.
Creates a Numeric out of value. See Numeric.of.
Parses value into a Numeric, preferring Integer and falling back to Real. See Numeric.of.
Creates a Substitution out of the given Var-Term.
Creates a Substitution out of the given Var.name-Term.
Creates a Substitution.Unifier out of the given Var-Term.
Creates a Substitution.Unifier out of the given Var.name-Term.