Skip to content

prolog

tuprolog.solve.prolog

Prolog solvers: SLD resolution, as in ISO Prolog.

Prolog solvers prove a goal by SLD resolution: they try the clauses whose head unifies with the goal, in the order they appear in the knowledge base, proving the goals in each clause's body from left to right, and backtracking to the next alternative upon failure; each proof found is a solution. Negation is negation as failure (\+ G holds if G cannot be proven).

Prolog solvers come with the standard library of ISO Prolog predicates and functions (e.g. member/2, findall/3, assertz/1, is/2, see tuprolog.solve.stdlib), and load further libraries on request (e.g. tuprolog.solve.libs.io and tuprolog.solve.libs.oop).

Main functions and constants:

  • prolog_solver: creates a Prolog solver;
  • PROLOG_SOLVER_FACTORY: the factory of Prolog solvers (a tuprolog.solve.SolverFactory).

Examples:

>>> from tuprolog.theory.parsing import parse_theory
>>> kb = parse_theory(
...     "parent(abe, homer). parent(homer, bart). "
...     "ancestor(X, Y) :- parent(X, Y). "
...     "ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)."
... )
>>> solver = prolog_solver(static_kb=kb)
>>> [solution["Y"] for solution in solver.solve("ancestor(abe, Y)") if solution]
[homer, bart]
See Also
  • tuprolog.solve, for querying solvers and handling solutions.
  • tuprolog.solve.problog, for probabilistic logic programming.
  • 2P-Kt's solver API and state machine of its Prolog solver.

PROLOG_SOLVER_FACTORY module-attribute

PROLOG_SOLVER_FACTORY: SolverFactory = Solver.prolog()

The factory of Prolog solvers (2P-Kt's Solver.prolog), see tuprolog.solve.SolverFactory.

Its default_builtins is the library of ISO Prolog predicates and functions, prolog.lang. tuprolog.solve.prolog.prolog_solver is a simpler way to create solvers.

prolog_solver

prolog_solver(
    unificator: Unificator | None = None,
    libraries: Runtime | Library | None = None,
    flags: FlagStore = DEFAULT_FLAG_STORE,
    static_kb: Theory | None = None,
    dynamic_kb: Theory | None = None,
    std_in: InputChannel | IOBase | None = None,
    std_out: OutputChannel | IOBase | None = None,
    std_err: OutputChannel | IOBase | None = None,
    warning: OutputChannel | IOBase | None = None,
    mutable: bool = True,
) -> Solver

Creates a Prolog solver, with the standard library of ISO Prolog predicates and functions.

Parameters:

Name Type Description Default
unificator Unificator | None

The unification algorithm; by default, the standard one (see tuprolog.unify).

None
libraries Runtime | Library | None

Further libraries of predicates and functions (a Library, or a Runtime of several ones, see tuprolog.solve.library), loaded on top of the standard ones.

None
flags FlagStore

The flags (see tuprolog.solve.flags); by default, DEFAULT_FLAG_STORE.

DEFAULT_FLAG_STORE
static_kb Theory | None

The static knowledge base, i.e. the clauses queries cannot change; empty by default.

None
dynamic_kb Theory | None

The dynamic knowledge base, i.e. the clauses that predicates like assertz/1 and retract/1 change; empty by default.

None
std_in InputChannel | IOBase | None

The standard input: an InputChannel, a Python stream, or a callable returning a string per call (None at the end); by default, Python's sys.stdin.

None
std_out OutputChannel | IOBase | None

The standard output, where write/1 writes: an OutputChannel, a Python stream, or a callable taking each string; by default, Python's sys.stdout.

None
std_err OutputChannel | IOBase | None

The standard error, like std_out; by default, Python's sys.stderr.

None
warning OutputChannel | IOBase | None

Where warnings go (e.g. about calls to unknown predicates): an OutputChannel, a Python stream (receiving their messages), or a callable taking each tuprolog.solve.exception.Warning; by default, Python's sys.stderr.

None
mutable bool

Whether to create a tuprolog.solve.MutableSolver, whose state Python code can change between queries (the default), or a plain tuprolog.solve.Solver.

True

Returns:

Type Description
Solver

The Prolog solver.

Examples:

>>> import io
>>> from tuprolog.solve.flags import flag_store
>>> from tuprolog.theory.parsing import parse_theory
>>> output = io.StringIO()
>>> solver = prolog_solver(
...     static_kb=parse_theory("greet(Name) :- write(hello), write(' '), write(Name)."),
...     flags=flag_store(unknown="fail"),
...     std_out=output,
... )
>>> solver.solve_once("greet(world)").is_yes
True
>>> output.getvalue()
'hello world'
>>> solver.solve_once("undefined_predicate").is_no
True
Note

Wraps 2P-Kt's SolverFactory.mutableSolverWithDefaultBuiltins and solverWithDefaultBuiltins, of Solver.prolog.