Library
tuprolog.solve.library
¶
Libraries: bundles of predicates, functions, clauses and operators that solvers load.
A library extends solvers with:
- primitives: predicates implemented in Python (see
tuprolog.solve.primitive); - functions: functions usable in arithmetic, e.g. by
is/2(seetuprolog.solve.function); - clauses: predicates written in Prolog;
- operators: e.g.
::for ProbLog (seetuprolog.core.operators).
Primitives and functions are keyed by signature, i.e. name and arity (see
tuprolog.solve.signature). Libraries have an alias, i.e. a name made of dot-separated words
(e.g. prolog.lang, the standard library), identifying them in solvers.
A solver loads a runtime, i.e. a set of libraries with distinct aliases: e.g. the standard
library, plus the ones given to tuprolog.solve.prolog.prolog_solver(libraries=...).
Main types and functions:
Library, created bylibrary(and re-aliased byaliased);Runtime, created bylibraries;Pluggable, what libraries and runtimes have in common.
Examples:
>>> from tuprolog.solve.function import function
>>> from tuprolog.solve.primitive import predicate
>>> from tuprolog.solve.prolog import prolog_solver
>>> from tuprolog.theory.parsing import parse_clauses
>>> strings = library(
... "my.strings",
... primitives={"upper/2": predicate(lambda text, _: [(text, text.upper())])},
... functions={"half/1": function(lambda x: x / 2)},
... clauses=parse_clauses("shout(X, Y) :- upper(X, Y)."),
... )
>>> solver = prolog_solver(libraries=strings)
>>> list(solver.query("shout(hello, Y), H is half(3)"))
[{'Y': 'HELLO', 'H': 1.5}]
See Also
tuprolog.solve.libs, the libraries shipped with 2P-Kt (e.g. input/output).tuprolog.solve.library.exception, the errors of loading libraries.- 2P-Kt's libraries.
Library
module-attribute
¶
Library = _library.Library
A library: an aliased bundle of primitives, functions, clauses and operators (a Pluggable).
Members: alias, and those of Pluggable; Python additions: to_runtime(), the runtime made of
this library only. Create libraries with library.
Examples:
>>> from tuprolog.solve.library import library
>>> lib = library("my.lib")
>>> lib.alias, list(lib.to_runtime().aliases)
('my.lib', ['my.lib'])
Pluggable
module-attribute
¶
Pluggable = _library.Pluggable
Something a solver can load: the base of Library and Runtime.
Members: primitives and functions (JVM maps from signatures), clauses (a list),
operators (an OperatorSet), and the lookups has_primitive(signature),
has_function(signature), has_rule(signature), rules_signatures, has_protected(signature)
(whether programs cannot change a predicate, e.g. via assertz/1).
Runtime
module-attribute
¶
Runtime = _library.Runtime
A runtime: the set of libraries loaded by a solver, a read-only JVM map from aliases.
Members: aliases, libraries, runtime[alias], and those of Pluggable, merging the contents
of all the libraries. Python additions:
runtime + other merges a library or another runtime, raising
tuprolog.solve.library.exception.AlreadyLoadedLibraryException if an alias is already loaded;
runtime - other removes a library, given as such, by alias, or as an iterable of aliases
(2P-Kt's own minus is broken: it raises exactly when the library is loaded).
Create runtimes with libraries.
Examples:
>>> from tuprolog.solve.library import libraries, library
>>> runtime = libraries(library("a.lib")) + library("b.lib")
>>> list(runtime.aliases)
['a.lib', 'b.lib']
>>> list((runtime - "a.lib").aliases)
['b.lib']
library
¶
library(
alias: str | None = None,
primitives: Mapping[Signature | str, Primitive] = {},
clauses: Iterable[Clause] = [],
operators: OperatorSet
| Iterable[Operator]
| None = None,
functions: Mapping[Signature | str, LogicFunction] = {},
) -> Library | Pluggable
Creates a library of primitives, functions, clauses and operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str | None
|
The alias of the library, i.e. dot-separated words (e.g. |
None
|
primitives
|
Mapping[Signature | str, Primitive]
|
The primitives (see |
{}
|
clauses
|
Iterable[Clause]
|
The clauses ( |
[]
|
operators
|
OperatorSet | Iterable[Operator] | None
|
The operators, as an |
None
|
functions
|
Mapping[Signature | str, LogicFunction]
|
The functions (see |
{}
|
Returns:
| Type | Description |
|---|---|
Library | Pluggable
|
The library. |
Examples:
>>> from tuprolog.core import atom, fact
>>> lib = library("my.facts", clauses=[fact(atom("sunny"))])
>>> lib.alias, lib.clauses
('my.facts', [sunny :- true])
aliased
¶
Returns a copy of a library with another alias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str
|
The new alias. |
required |
library
|
Library
|
The library, left unchanged. |
required |
Returns:
| Type | Description |
|---|---|
Library
|
The library with the new alias. |
Examples:
>>> aliased("new.name", library("old.name")).alias
'new.name'
libraries
¶
Creates a runtime, i.e. a set of libraries with distinct aliases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*libs
|
Library | Runtime | Iterable[Library]
|
Libraries, runtimes (contributing their libraries), or iterables of them. |
()
|
**kwargs
|
Library
|
More libraries, loaded under the keyword as alias. |
{}
|
Returns:
| Type | Description |
|---|---|
Runtime
|
The runtime; empty without arguments. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an argument is none of the above. |
AlreadyLoadedLibraryException
|
If two libraries have the same alias. |
Examples:
>>> runtime = libraries(library("a.lib"), [library("b.lib")], c=library("whatever"))
>>> list(runtime.aliases)
['a.lib', 'b.lib', 'c']