Package-level declarations

Types

Link copied to clipboard
typealias BinaryOperator<T> = (T, T) -> T

A function taking two values of type T and combining them into a single value of the same type, e.g. an addition, a union, or a merge operation.

Link copied to clipboard
interface Cache<K, V>

Mutable, fixed-capacity cache whose eviction strategy depends on the specific implementation.

Link copied to clipboard
interface Cached<T>

A lazily-computed, invalidatable single value, i.e. a memoized version of a () -> T generator.

Link copied to clipboard
interface Castable<T : Castable<T>>

A type for all objects belonging to a hierarchy rooted in T which can be down-casted to any subtype of T via explicit methods, rather than via Kotlin's as/as? operators.

Link copied to clipboard
interface Cursor<T>

An immutable, persistent, singly-linked view over a (possibly lazily produced) sequence of elements of type T, akin to a Lisp-style (head . tail) cons cell.

Link copied to clipboard
interface Indexed<K, T>

A value of type T paired with an index of type K, i.e. a labelled/positional value.

Link copied to clipboard

An Indexed value whose index is an Int, comparable to other IntIndexed values by index. This is the type produced by Sequence.indexed, the "counted" counterpart of kotlin.collections.withIndex used throughout the codebase whenever a position needs to travel along with a value through further transformations (e.g. .map { it.map(...) }), which a plain IndexedValue/destructured pair would not allow as conveniently.

Link copied to clipboard

An Indexed value whose index is a Long, comparable to other LongIndexed values by index. This is the type produced by Sequence.longIndexed, the 64-bit counterpart of IntIndexed, useful whenever the position of a value may exceed Int.MAX_VALUE (e.g. it.unibo.tuprolog.collections.rete.custom.clause.IndexedClause in the :theory module indexes clauses by insertion order using a Long, since a knowledge base may grow without a practical Int-sized bound).

Link copied to clipboard

Classifies platform Numbers (e.g. Int, Double, Float, or any other JVM/JS numeric boxed type encountered while converting external, non-Prolog values into Prolog terms) as integral or decimal, and converts them into the arbitrary-precision BigInteger/BigDecimal types used internally to represent Prolog numbers, based on their String representation rather than on their runtime class.

Link copied to clipboard
sealed class Optional<T>

A container that either holds exactly one value of type T (Some), or holds none (None), used across the codebase as an explicit, null-safe substitute for a nullable T? wherever null itself could be a meaningful value to store (so that "no value" and "the value happens to be null" cannot be confused), e.g. as the return type of Cache.get/Cache.set (the evicted pair, if any) or Cached.ifValid.

Link copied to clipboard
interface Taggable<Self : Taggable<Self>>

A type for immutable objects that carry an arbitrary, named bag of metadata (their tags), attachable without polluting the type's own API with a case-by-case property for every possible piece of metadata.

Link copied to clipboard

A BinaryOperator over Taggable.tags maps, used to combine the tags of two Taggable instances into a single tags map, e.g. when two tagged objects are merged into one and their tags need to be reconciled (see, for instance, Substitution.plus(Substitution, TagsOperator) in the :core module).

Functions

Link copied to clipboard
expect fun <T> MutableList<T>.addFirst(item: T)

Inserts item at the head of this list, shifting all other elements one position towards the tail. Named after (and, on platforms with one, backed by) the "add first" operation of a proper deque.

expect fun <T> MutableList<T>.addFirst(items: Iterable<T>)
expect fun <T> MutableList<T>.addFirst(items: Sequence<T>)

Inserts all the elements of items, in iteration order, at the head of this list.

actual fun <T> MutableList<T>.addFirst(item: T)

Inserts item at the head of this list, shifting all other elements one position towards the tail. Named after (and, on platforms with one, backed by) the "add first" operation of a proper deque.

actual fun <T> MutableList<T>.addFirst(items: Iterable<T>)
actual fun <T> MutableList<T>.addFirst(items: Sequence<T>)

Inserts all the elements of items, in iteration order, at the head of this list.

actual fun <T> MutableList<T>.addFirst(item: T)

Inserts item at the head of this list, shifting all other elements one position towards the tail. Named after (and, on platforms with one, backed by) the "add first" operation of a proper deque.

actual fun <T> MutableList<T>.addFirst(items: Iterable<T>)
actual fun <T> MutableList<T>.addFirst(items: Sequence<T>)

Inserts all the elements of items, in iteration order, at the head of this list.

Link copied to clipboard
fun <T : U, U : Taggable<U>, X : Any> T.addTag(name: String, value: X): T

Returns a new instance, otherwise equivalent to this one, with an additional tag named name holding value merged into its existing Taggable.tags (overwriting any existing tag with the same name).

Link copied to clipboard
fun <T : U, U : Taggable<U>, X : Any> T.addTags(tags: Map<String, X>): T

Returns a new instance, otherwise equivalent to this one, with tags merged into its existing Taggable.tags (overwriting any existing tags with the same names as one of tags' keys).

fun <T : U, U : Taggable<U>, X : Any> T.addTags(tag: Pair<String, X>, vararg otherTags: Pair<String, X>): T

Returns a new instance, otherwise equivalent to this one, with tag (and any otherTags) merged into its existing Taggable.tags (overwriting any existing tags with the same names).

Link copied to clipboard

Asserts that none of the elements of this array is null, returning it as an Array of the non-nullable element type T (which, for a reference-typed array, is the very same array instance, unsafely cast).

Same as Sequence.assertItemsAreNotNull, starting from an Iterable rather than a Sequence.

Asserts that none of the elements of this list is null, returning it as a List of the non-nullable element type T.

Lazily asserts that none of the elements of this sequence is null, yielding a Sequence of the non-nullable element type T; each element is checked only as it is traversed.

Link copied to clipboard

Eagerly materializes this sequence into a List and returns it as a (now safely re-iterable) Sequence. Use this to consume a one-shot/expensive-to-recompute sequence more than once without recomputing it every time, at the cost of holding all of its elements in memory at once; see also cached for a lazier alternative that memoizes elements only as they are first traversed.

Link copied to clipboard

Returns a Sequence view that lazily memoizes the elements of this sequence as they are first traversed (via Cursor.asSequence), so that traversing the result more than once does not recompute already-seen elements. Unlike buffered, this does not eagerly consume the whole sequence upfront.

Link copied to clipboard
fun <T : U, U : Taggable<U>> T.clearTags(): T

Returns a new instance, otherwise equivalent to this one, but with no tags attached at all.

Link copied to clipboard
fun <T> Array<T>.cursor(): Cursor<out T>

Returns a lazy Cursor traversing the elements of this Array, in index order.

fun <T> Collection<T>.cursor(): Cursor<out T>

Returns a lazy Cursor traversing the elements of this Collection, in iteration order.

fun <T> Iterable<T>.cursor(): Cursor<out T>

Returns a lazy Cursor traversing the elements of this Iterable, in iteration order.

fun <T> Sequence<T>.cursor(): Cursor<out T>

Returns a lazy Cursor traversing the elements of this Sequence, in iteration order.

Link copied to clipboard
expect fun <T> dequeOf(items: Iterable<T>): MutableList<T>
expect fun <T> dequeOf(items: Sequence<T>): MutableList<T>

Creates a new, mutable dequeOf deque containing the elements of items, in iteration order.

expect fun <T> dequeOf(vararg items: T): MutableList<T>

Creates a new, mutable double-ended queue (deque) containing the given items, in order.

actual fun <T> dequeOf(items: Iterable<T>): MutableList<T>
actual fun <T> dequeOf(items: Sequence<T>): MutableList<T>

Creates a new, mutable dequeOf deque containing the elements of items, in iteration order.

actual fun <T> dequeOf(vararg items: T): MutableList<T>

Creates a new, mutable double-ended queue (deque) containing the given items, in order.

actual fun <T> dequeOf(items: Iterable<T>): MutableList<T>
actual fun <T> dequeOf(items: Sequence<T>): MutableList<T>

Creates a new, mutable dequeOf deque containing the elements of items, in iteration order.

actual fun <T> dequeOf(vararg items: T): MutableList<T>

Creates a new, mutable double-ended queue (deque) containing the given items, in order.

Link copied to clipboard

Returns a new sequence containing all the elements of this sequence except the last one. Returns an empty sequence if this sequence has zero or one elements.

Link copied to clipboard
expect fun <T> Any?.forceCast(): T

Force-casts this (possibly null) receiver to T, with no runtime type check performed by this function itself (on the JVM, this is a plain as T unchecked cast; on JS, it goes through a dynamic value, bypassing type checking altogether). Use this only where the caller has independent, external knowledge that the cast is safe (e.g. the Cursor plus operator in CursorExtensions.kt uses it to narrow an Iterable-backed Cursor<out T> back down to a Cursor<T>), since, unlike a checked as T, an invalid cast may not fail immediately at the call site, but only later, wherever the miscast value is first used as a T.

actual fun <T> Any?.forceCast(): T

Force-casts this (possibly null) receiver to T, with no runtime type check performed by this function itself (on the JVM, this is a plain as T unchecked cast; on JS, it goes through a dynamic value, bypassing type checking altogether). Use this only where the caller has independent, external knowledge that the cast is safe (e.g. the Cursor plus operator in CursorExtensions.kt uses it to narrow an Iterable-backed Cursor<out T> back down to a Cursor<T>), since, unlike a checked as T, an invalid cast may not fail immediately at the call site, but only later, wherever the miscast value is first used as a T.

actual fun <T> Any?.forceCast(): T

Force-casts this (possibly null) receiver to T, with no runtime type check performed by this function itself (on the JVM, this is a plain as T unchecked cast; on JS, it goes through a dynamic value, bypassing type checking altogether). Use this only where the caller has independent, external knowledge that the cast is safe (e.g. the Cursor plus operator in CursorExtensions.kt uses it to narrow an Iterable-backed Cursor<out T> back down to a Cursor<T>), since, unlike a checked as T, an invalid cast may not fail immediately at the call site, but only later, wherever the miscast value is first used as a T.

Link copied to clipboard

Pairs each element of this sequence with its 0-based position, as an IntIndexed value, lazily.

Link copied to clipboard
fun <T> Sequence<T>.insertAt(index: Int, item: T, vararg items: T): Sequence<T>

Returns a new sequence equal to this one, except that item (followed by any items) is inserted right before the element that was originally at position index (0-based); if index is (at or) beyond the end of this sequence, item and items are appended at the end.

Link copied to clipboard
fun <T> interleave(iterables: Iterable<Iterable<T>>): Sequence<T>

Lazily interleaves the elements of the given iterables, round-robin style: the first element of every iterable, then the second element of every (still non-exhausted) iterable, and so on, until all of them are exhausted. Unlike merge, this does not require the inputs to be sorted, and does not reorder elements based on their value, only on their position within their originating iterable.

fun <T> interleave(iterables: Sequence<Iterable<T>>): Sequence<T>

Same as interleave, taking the outer collection of iterables as a Sequence.

fun <T> interleave(vararg iterables: Iterable<T>): Sequence<T>

Same as interleave, taking the iterables to interleave as varargs.

Link copied to clipboard

Same as interleave, where each of the interleaved elements is a Sequence rather than an Iterable.

Same as interleave, where both the outer collection and each of the interleaved elements are Sequences.

fun <T> interleaveSequences(vararg iterables: Sequence<T>): Sequence<T>

Same as interleave, taking the Sequences to interleave as varargs.

Link copied to clipboard
fun <T> itemWiseEquals(iterable1: Iterable<T>, iterable2: Iterable<T>): Boolean

Same as itemWiseEquals, using Any.equals (==) as the element comparator.

fun <T> itemWiseEquals(sequence1: Sequence<T>, sequence2: Sequence<T>): Boolean

Same as itemWiseEquals, for two Sequences rather than Iterables, using Any.equals (==) as the element comparator.

fun <T> itemWiseEquals(iterable1: Iterable<T>, iterable2: Iterable<T>, comparator: (T, T) -> Boolean): Boolean

Compares iterable1 and iterable2 element-by-element, in iteration order, via comparator, returning true if and only if both have the same number of elements and every corresponding pair of elements satisfies comparator. Useful to compare two iterables for equality without materializing them into Lists first, or using a notion of "equality" other than Any.equals (e.g. structural equality of terms that ignores variable naming).

fun <T> itemWiseEquals(sequence1: Sequence<T>, sequence2: Sequence<T>, comparator: (T, T) -> Boolean): Boolean

Same as itemWiseEquals, for two Sequences rather than Iterables.

Link copied to clipboard
fun <T> itemWiseHashCode(iterable: Iterable<T>): Int

Same as itemWiseHashCode, taking the items as an Iterable.

fun <T> itemWiseHashCode(sequence: Sequence<T>): Int

Same as itemWiseHashCode, taking the items as a Sequence.

fun <T> itemWiseHashCode(vararg items: T): Int

Computes a hash code out of the given items, consistent with itemWiseEquals: two iterables that are itemWiseEquals (under ==) always produce the same itemWiseHashCode, much like List.hashCode does for lists, but usable on any Iterable/Sequence. Handy when implementing Any.hashCode for a custom collection-like type that should be compared item-wise (e.g. structurally-shared term lists) rather than by reference.

Link copied to clipboard
fun <T> iterable(iterable: Iterable<T>): MutableIterable<T>

Wraps iterable as a MutableIterable, usable from JVM code expecting one; each call to MutableIterable.iterator wraps a fresh iterable.iterator() via iterator.

fun <T> iterable(sequence: Sequence<T>): MutableIterable<T>

Wraps sequence as a MutableIterable, usable from JVM code expecting one; each call to MutableIterable.iterator wraps a fresh sequence.iterator().

Link copied to clipboard
fun <T> iterator(iterator: Iterator<T>): MutableIterator<T>

Wraps iterator as a MutableIterator, usable from JVM code expecting one.

Link copied to clipboard

Pairs each element of this sequence with its 0-based position, as a LongIndexed value, lazily. Use this rather than indexed when the sequence may contain more than Int.MAX_VALUE elements.

Link copied to clipboard
fun <T> merge(comparator: Comparator<T>, iterables: Iterable<Iterable<T>>): Sequence<T>

Performs a k-way merge of the given iterables, each of which is assumed to already be sorted according to comparator, producing a single Sequence that yields all their elements in the order induced by comparator (like the "merge" step of a merge-sort, generalized to more than two inputs).

fun <T> merge(comparator: Comparator<T>, iterables: Sequence<Iterable<T>>): Sequence<T>
fun <T> merge(iterables: Sequence<Iterable<T>>, comparator: (T, T) -> Int): Sequence<T>

Same as merge, taking the iterables to merge as a Sequence of Iterables.

fun <T> merge(comparator: Comparator<T>, vararg iterables: Iterable<T>): Sequence<T>
fun <T> merge(vararg iterables: Iterable<T>, comparator: (T, T) -> Int): Sequence<T>

Same as merge, taking the iterables to merge as varargs instead of an Iterable.

fun <T> merge(iterables: Iterable<Iterable<T>>, comparator: (T, T) -> Int): Sequence<T>

Same as merge, with comparator expressed as a plain comparison lambda instead of a Comparator.

Link copied to clipboard
fun <T> mergeSequences(comparator: Comparator<T>, iterables: Iterable<Sequence<T>>): Sequence<T>
fun <T> mergeSequences(iterables: Iterable<Sequence<T>>, comparator: (T, T) -> Int): Sequence<T>

Same as merge, but each of the iterables to merge is itself a Sequence rather than an Iterable.

fun <T> mergeSequences(comparator: Comparator<T>, iterables: Sequence<Sequence<T>>): Sequence<T>
fun <T> mergeSequences(iterables: Sequence<Sequence<T>>, comparator: (T, T) -> Int): Sequence<T>

Same as merge, but both the outer collection and each of the iterables to merge are Sequences.

fun <T> mergeSequences(comparator: Comparator<T>, vararg iterables: Sequence<T>): Sequence<T>
fun <T> mergeSequences(vararg iterables: Sequence<T>, comparator: (T, T) -> Int): Sequence<T>

Same as merge, taking the Sequences to merge as varargs.

Link copied to clipboard
fun <T> permutations(vararg items: T): Sequence<List<T>>

Lazily generates every permutation of the given items, as Lists, in an unspecified order. The number of permutations generated is items.size factorial, so this quickly becomes expensive for anything but small inputs.

Link copied to clipboard

Same as List.permutations, starting from an Iterable rather than a List.

Lazily generates every permutation of the elements of this list, as Lists, in an unspecified order, via the standard recursive "pick each element as head, permute the rest" algorithm. The number of permutations generated is size factorial (e.g. 720 for a 6-element list), so this quickly becomes expensive for anything but small inputs.

Same as List.permutations, starting from a Sequence rather than a List.

Link copied to clipboard
operator fun <T : U, U : Taggable<U>, X : Any> T.plus(tag: Pair<String, X>): T

Operator alias for addTags, allowing tags to be merged in via the + operator, e.g. term + ("k" to v).

operator fun <T : U, U : Taggable<U>, X : Any> T.plus(tags: Map<String, X>): T

Operator alias for addTags, allowing a whole tags map to be merged in via the + operator.

operator fun <T> Cursor<out T>.plus(other: Cursor<out T>): Cursor<out T>

Concatenates this cursor with other, yielding a Cursor that traverses first the elements of this cursor, then, once it Cursor.isOver, the elements of other.

Link copied to clipboard
fun <T, U> Sequence<T>.product(other: Sequence<U>): Sequence<Pair<T, U>>

Same as product, pairing up elements into Pairs instead of combining them via a custom function.

fun <T, U, R> Sequence<T>.product(other: Sequence<U>, combinator: (T, U) -> R): Sequence<R>

Returns the lazy Cartesian product of this sequence and other, combining each pair of elements via combinator. Equivalent to a nested for (x in this) for (y in other) yield(combinator(x, y)), except lazy: the result has this.count() * other.count() elements, but other must be safely re-iterable (e.g. backed by a List, not a one-shot generator), since it is traversed once per element of this sequence.

Link copied to clipboard
fun <T : U, U : Taggable<U>, X : Any> T.setTag(key: String, value: X): T

Returns a new instance, otherwise equivalent to this one, whose only tag is key holding value (any other previously attached tag with a different name is preserved, but one named key is overwritten).

Link copied to clipboard
fun <T : U, U : Taggable<U>, X : Any> T.setTags(tags: Map<String, X>): T

Returns a new instance, otherwise equivalent to this one, whose Taggable.tags are entirely replaced by tags (i.e. this is not a merge: any previously attached tag not present in tags is dropped). Thin, type-preserving wrapper around Taggable.replaceTags.

fun <T : U, U : Taggable<U>, X : Any> T.setTags(tag: Pair<String, X>, vararg otherTags: Pair<String, X>): T

Same as setTags, taking the replacement tags as a tag (and any otherTags) rather than as a Map.

Link copied to clipboard
fun <T> Sequence<T>.skipIndex(index: Int): Sequence<T>

Returns a new sequence containing all the elements of this sequence except the one at the given index (0-based). If index is beyond the end of this sequence, the returned sequence is equal to this one.

Link copied to clipboard

Same as squared, pairing up elements into Pairs instead of combining them via a custom function.

fun <T, R> Sequence<T>.squared(combinator: (T, T) -> R): Sequence<R>

Returns the lazy Cartesian product of this sequence with itself (see product), combining each pair of elements (including a value with itself) via combinator.

Link copied to clipboard
fun <T> subsequences(vararg items: T): Sequence<Sequence<T>>

Same as Sequence.subsequences, taking the items to generate prefixes of as varargs.

Link copied to clipboard

Same as Sequence.subsequences, but starting from an Iterable.

Lazily generates every prefix (i.e. leading subsequence) of this sequence, from the singleton sequence containing only the first element up to the full sequence, one element longer than the previous at each step; stops as soon as a prefix turns out shorter than requested (i.e. once this sequence is exhausted). Note that, despite the name, this yields growing prefixes, not every possible (non-contiguous) subsequence.

Link copied to clipboard
expect fun <T> synchronizedOn(obj: Any, action: () -> T): T

Runs action while holding the monitor/lock associated with obj, returning its result; a platform-agnostic, expression-based counterpart of the JVM's synchronized(obj) { ... } block, usable from common code. On platforms without real concurrency (e.g. JS), this may simply invoke action directly, since no synchronization is needed there.

actual fun <T> synchronizedOn(obj: Any, action: () -> T): T

Runs action while holding the monitor/lock associated with obj, returning its result; a platform-agnostic, expression-based counterpart of the JVM's synchronized(obj) { ... } block, usable from common code. On platforms without real concurrency (e.g. JS), this may simply invoke action directly, since no synchronization is needed there.

actual fun <T> synchronizedOn(obj: Any, action: () -> T): T

Runs action while holding the monitor/lock associated with obj, returning its result; a platform-agnostic, expression-based counterpart of the JVM's synchronized(obj) { ... } block, usable from common code. On platforms without real concurrency (e.g. JS), this may simply invoke action directly, since no synchronization is needed there.

Link copied to clipboard
fun <T : Any, R> T.synchronizedOnSelf(action: () -> R): R

Runs action while holding the monitor/lock associated with this receiver object, returning its result. Shorthand for synchronizedOn(this, action), used e.g. by the internal LRUCache and SimpleLRUCache implementations of Cache to guard all their read/write operations:

Link copied to clipboard
expect fun <T> MutableList<T>.takeFirst(): T?

Removes and returns the first element of this list, or null if the list is empty.

actual fun <T> MutableList<T>.takeFirst(): T?

Removes and returns the first element of this list, or null if the list is empty.

actual fun <T> MutableList<T>.takeFirst(): T?

Removes and returns the first element of this list, or null if the list is empty.

Link copied to clipboard
fun <T> Iterator<T>.toCursor(): Cursor<out T>

Wraps this Iterator into a lazy Cursor, pulling elements from it, one by one, as the resulting cursor is traversed. Equivalent to Cursor.of.