Solver

General type for logic solvers, i.e. any entity capable of solving some logic query -- provided as a Struct -- according to some logic, implementing one or more inference rule, via some resolution strategy.

A Solver is deliberately strategy-agnostic: this module defines no resolution algorithm at all, only the contract that every implementation (:solve-classic's SLD-NF resolution, :solve-streams's side-effect-free strategy, :solve-concurrent, :solve-problog) must honour. Everything a resolution strategy needs to read while solving a goal -- loaded it.unibo.tuprolog.solve.library.Library/Runtime, FlagStore, the two Theory knowledge bases, I/O it.unibo.tuprolog.solve.channel.Channels -- is exposed through ExecutionContextAware, which this interface extends.

Solvers are not immutable entities. Their state may mutate as an effect of solving queries -- e.g. asserting a clause during resolution replaces the dynamic knowledge base with a new Theory instance. Between resolutions, a solver's assets can only be swapped by deriving a new Solver via copy, unless the concrete instance also implements MutableSolver.

Instances are usually obtained from a SolverFactory, several of which are reachable from the companion object, e.g.:

val solver = Solver.prolog.solverWithDefaultBuiltins()
val solutions = solver.solve(Struct.of("append", listA, listB, result))

See also

Inheritors

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val dynamicKb: Theory

Dynamic Knowledge-base, that is a KB that can change executing goals

Link copied to clipboard
abstract val flags: FlagStore

Enabled flags

Link copied to clipboard

The currently open input channels

Link copied to clipboard
abstract val libraries: Runtime

Loaded libraries

Link copied to clipboard
abstract val operators: OperatorSet

Loaded operators

Link copied to clipboard

The currently open output channels

Link copied to clipboard

Shortcut for the standard error channel defined in outputChannels. Returns null if the channel is closed

Link copied to clipboard

Shortcut for the standard input channel defined in inputChannels. Returns null if the channel is closed

Link copied to clipboard

Shortcut for the standard output channel defined in outputChannels. Returns null if the channel is closed

Link copied to clipboard
abstract val staticKb: Theory

Static Knowledge-base, that is a KB that can't change executing goals

Link copied to clipboard
abstract val unificator: Unificator
Link copied to clipboard

Shortcut for the warnings channel defined in outputChannels. Returns null if the channel is closed

Functions

Link copied to clipboard
open fun clone(): Solver

Shorthand for copy without overriding anything, i.e. an identical (but distinct) Solver instance.

Link copied to clipboard
abstract fun copy(unificator: Unificator = this.unificator, libraries: Runtime = this.libraries, flags: FlagStore = this.flags, staticKb: Theory = this.staticKb, dynamicKb: Theory = this.dynamicKb, stdIn: InputChannel<String> = this.standardInput, stdOut: OutputChannel<String> = this.standardOutput, stdErr: OutputChannel<String> = this.standardError, warnings: OutputChannel<Warning> = this.warnings): Solver

Creates a new Solver, sharing the same resolution strategy as this one, but with every explicitly-provided argument replacing the corresponding piece of state; arguments left unspecified default to this solver's current value. Useful to derive variants of a solver overriding just a few "mutable aspects" (e.g. redirecting stdOut while keeping everything else identical).

Link copied to clipboard
open fun solve(goal: Struct): Sequence<Solution>

Shorthand for solve with SolveOptions.DEFAULT (i.e. all solutions, lazily, without a timeout).

abstract fun solve(goal: Struct, options: SolveOptions): Sequence<Solution>

Solves goal, returning a (possibly infinite) Sequence of Solutions, computed according to options.

open fun solve(goal: Struct, timeout: TimeDuration): Sequence<Solution>

Shorthand for solve with options set to SolveOptions.allLazilyWithTimeout of timeout.

Link copied to clipboard
fun Solver.solve(maxDuration: TimeDuration = TimeDuration.MAX_VALUE, scopedContext: Scope.() -> Struct): Sequence<Solution>

Solves the goal built by scopedContext within a fresh, empty Scope (useful to build the goal Struct using scoped variables inline), capping resolution to maxDuration.

Link copied to clipboard
open fun solveList(goal: Struct): List<Solution>

Shorthand for solveList with SolveOptions.DEFAULT.

open fun solveList(goal: Struct, options: SolveOptions): List<Solution>

Eagerly solves goal and collects every produced Solution into a List. Unlike solve, this always computes solutions eagerly regardless of SolveOptions.isLazy -- be mindful of goals with infinite (or very large) solution sets, which will make this method never return (or exhaust memory).

open fun solveList(goal: Struct, timeout: TimeDuration): List<Solution>

Shorthand for solveList with options set to SolveOptions.allLazilyWithTimeout of timeout.

Link copied to clipboard
open fun solveOnce(goal: Struct): Solution

Shorthand for solveOnce with SolveOptions.someLazily of 1.

open fun solveOnce(goal: Struct, options: SolveOptions): Solution

Solves goal and eagerly returns its first Solution only, regardless of options' SolveOptions.limit (which is overridden to 1 via SolveOptions.setLimit).

open fun solveOnce(goal: Struct, timeout: TimeDuration): Solution

Shorthand for solveOnce with a timeout, and a limit of 1 solution.