parsing
tuprolog.theory.parsing
¶
Parsing theories, i.e. sequences of clauses, from Prolog source code.
Prolog source code is a sequence of clauses, each one terminated by a full stop (.), e.g.
parent(abe, homer). grandparent(X, Z) :- parent(X, Y), parent(Y, Z).. This package turns such
text into tuprolog.core.Clauses, or into a whole tuprolog.theory.Theory:
- parsers (
ClausesParser, and theparse_*functions) take strings; - readers (
ClausesReader, and theread_*functions) take files (paths) or streams, either Python ones (e.g.open("kb.pl"),io.StringIO,io.BytesIO) or JVM ones.
Parsing depends on the operators in use (see tuprolog.core.operators): operators let terms be
written in infix, prefix or postfix notation, e.g. X is 1 + 2 for is(X, +(1, 2)). By default,
the standard Prolog operators are known; custom ones can be added by passing an OperatorSet.
Invalid source code raises tuprolog.errors.ParsingErrors (which are ValueErrors), telling
where the error is.
Examples:
>>> import io
>>> parse_theory("parent(abe, homer). parent(homer, bart).")
IndexedTheory{ parent(abe, homer) :- true. parent(homer, bart) :- true }
>>> list(parse_clauses("a. b :- a."))
[a :- true, b :- a]
>>> read_theory(io.StringIO("likes(X, pizza)."))
IndexedTheory{ likes(X_..., pizza) :- true }
See Also
tuprolog.core.parsing, for parsing single terms; 2P-Kt's
parsing architecture.
ClausesParser
module-attribute
¶
ClausesParser = _parsing.ClausesParser
A parser of clauses and theories from strings.
Each parser has a default operator set, used unless another one is passed to its methods.
Create parsers with tuprolog.theory.parsing.clauses_parser.
Members
parse_theory(input, operators=None): thetuprolog.theory.Theoryof the clauses in a string.parse_clauses(input, operators=None): the list of the clauses in a string.parse_clauses_lazily(input, operators=None): the clauses in a string, as a lazy iterable parsing them on demand.default_operator_set: the operators the parser knows by default.
Examples:
>>> parser = ClausesParser.withDefaultOperators()
>>> parser.parse_clauses("a. b.")
[a :- true, b :- true]
Note
Wraps 2P-Kt's it.unibo.tuprolog.theory.parsing.ClausesParser interface.
ClausesReader
module-attribute
¶
ClausesReader = _parsing.ClausesReader
A reader of clauses and theories from files or streams.
Inputs may be paths (strings or path-like objects), Python streams (text ones are UTF-8
encoded), or JVM input streams. Create readers with tuprolog.theory.parsing.clauses_reader.
Members
read_theory(input, operators=None): thetuprolog.theory.Theoryof the clauses in the input.read_clauses(input, operators=None): the list of the clauses in the input.read_clauses_lazily(input, operators=None): the clauses in the input, as a lazy iterable reading them on demand.default_operator_set: the operators the reader knows by default.
Examples:
>>> import io
>>> ClausesReader.withDefaultOperators().read_clauses(io.StringIO("a. b."))
[a :- true, b :- true]
Note
Wraps 2P-Kt's it.unibo.tuprolog.theory.parsing.ClausesReader interface.
DEFAULT_CLAUSES_PARSER
module-attribute
¶
DEFAULT_CLAUSES_PARSER = (
ClausesParser.withDefaultOperators()
)
The ClausesParser knowing the standard Prolog operators, used by
tuprolog.theory.parsing.parse_theory and tuprolog.theory.parsing.parse_clauses.
DEFAULT_CLAUSES_READER
module-attribute
¶
DEFAULT_CLAUSES_READER = (
ClausesReader.withDefaultOperators()
)
The ClausesReader knowing the standard Prolog operators, used by
tuprolog.theory.parsing.read_theory and tuprolog.theory.parsing.read_clauses.
clauses_parser
¶
clauses_parser(
with_default_operators: bool = True,
operators: OperatorSet | None = None,
) -> ClausesParser
Creates a parser of clauses and theories from strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_default_operators
|
bool
|
Whether the parser knows the standard Prolog operators (True by default). |
True
|
operators
|
OperatorSet | None
|
Further operators the parser knows, if not None: added to the standard ones
if |
None
|
Returns:
| Type | Description |
|---|---|
ClausesParser
|
The parser. |
Examples:
>>> from tuprolog.core.operators import XFX, operator, operator_set
>>> parser = clauses_parser(operators=operator_set(operator("likes", XFX, 700)))
>>> parser.parse_theory("alice likes pizza.")
IndexedTheory{ likes(alice, pizza) :- true }
See Also
clauses_reader, for files and streams.
clauses_reader
¶
clauses_reader(
with_default_operators: bool = True,
operators: OperatorSet | None = None,
) -> ClausesReader
Creates a reader of clauses and theories from files or streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_default_operators
|
bool
|
Whether the reader knows the standard Prolog operators (True by default). |
True
|
operators
|
OperatorSet | None
|
Further operators the reader knows, if not None: added to the standard ones
if |
None
|
Returns:
| Type | Description |
|---|---|
ClausesReader
|
The reader. |
Examples:
>>> import io
>>> clauses_reader().read_theory(io.StringIO("a. b."))
IndexedTheory{ a :- true. b :- true }
See Also
clauses_parser, for strings.
parse_theory
¶
parse_theory(
string: str, operators: OperatorSet | None = None
) -> Theory
Parses Prolog source code into a theory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The source code: clauses, each one ended by a full stop. |
required |
operators
|
OperatorSet | None
|
The operators to use instead of the standard ones, if not None (see
|
None
|
Returns:
| Type | Description |
|---|---|
Theory
|
The immutable, indexed theory of the parsed clauses, in order. |
Raises:
| Type | Description |
|---|---|
ParsingError
|
If |
Examples:
>>> parse_theory("mortal(X) :- human(X). human(socrates).")
IndexedTheory{ mortal(X_...) :- human(X_...). human(socrates) :- true }
>>> from tuprolog.errors import ParsingError
>>> try:
... parse_theory("a :- .")
... except ParsingError as error:
... print(error.message)
Operator ':-' (xfx, priority 1200) is missing its right operand
See Also
read_theory, for files and streams.
parse_clauses
¶
parse_clauses(
string: str,
operators: OperatorSet | None = None,
lazy: bool = True,
) -> Iterable[Clause]
Parses Prolog source code into clauses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The source code: clauses, each one ended by a full stop. |
required |
operators
|
OperatorSet | None
|
The operators to use instead of the standard ones, if not None (see
|
None
|
lazy
|
bool
|
Whether clauses are parsed on demand, while iterating over the result (True by default), or all at once, into a list. |
True
|
Returns:
| Type | Description |
|---|---|
Iterable[Clause]
|
The parsed clauses, in order: a lazy iterable, or a list. |
Raises:
| Type | Description |
|---|---|
ParsingError
|
If |
Examples:
>>> parse_clauses("a. b :- a.", lazy=False)
[a :- true, b :- a]
read_theory
¶
read_theory(
input: InputStream | str,
operators: OperatorSet | None = None,
) -> Theory
Reads a theory from a file or stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
InputStream | str
|
A path (a string or a path-like object), a Python stream (e.g. |
required |
operators
|
OperatorSet | None
|
The operators to use instead of the standard ones, if not None (see
|
None
|
Returns:
| Type | Description |
|---|---|
Theory
|
The immutable, indexed theory of the clauses read, in order. |
Raises:
| Type | Description |
|---|---|
ParsingError
|
If the input is not valid Prolog. |
Examples:
>>> import io
>>> read_theory(io.StringIO("parent(abe, homer)."))
IndexedTheory{ parent(abe, homer) :- true }
See Also
parse_theory, for strings.
read_clauses
¶
read_clauses(
input: InputStream | str,
operators: OperatorSet | None = None,
lazy: bool = True,
) -> Iterable[Clause]
Reads clauses from a file or stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
InputStream | str
|
A path (a string or a path-like object), a Python stream, or a JVM input stream. |
required |
operators
|
OperatorSet | None
|
The operators to use instead of the standard ones, if not None (see
|
None
|
lazy
|
bool
|
Whether clauses are read on demand, while iterating over the result (True by default), or all at once, into a list. |
True
|
Returns:
| Type | Description |
|---|---|
Iterable[Clause]
|
The clauses read, in order: a lazy iterable, or a list. |
Raises:
| Type | Description |
|---|---|
ParsingError
|
If the input is not valid Prolog. |
Examples:
>>> import io
>>> read_clauses(io.BytesIO(b"a. b."), lazy=False)
[a :- true, b :- true]