deeplog.grounding.prolog.parser

Plain-Prolog source text: parsed into program clauses, and rendered from symbols.

The head / body / functor splitting is generic and knows only plain Prolog; an operator such as :: is read as the ordinary binary term it is.

Functions

parse_term(text) → deeplog.symbol.Symbol

Read the Prolog term text.

parse_atoms(atoms_str, split_str[, atom_parser]) → collections.abc.Iterable[deeplog.symbol.Symbol]

Parse atoms_str split on split_str with atom_parser per atom.

parse_rule(line[, atom_parser]) → deeplog.grounding.prolog.program.RuleType

Parse one clause line h1 ; ... <functor> b1 , ... ending in ..

str_to_rule(line) → deeplog.grounding.prolog.program.RuleType

Parse a single plain-Prolog clause line.

iter_clauses(code) → collections.abc.Iterable[str]

Split source text into .-terminated clause strings.

str_to_rules(code) → collections.abc.Iterable[deeplog.grounding.prolog.program.RuleType]

Parse a plain-Prolog program; clauses may share a line.

split_list(term) → tuple[list[deeplog.symbol.Symbol], deeplog.symbol.Symbol]

Return the elements of the list term and the tail they end in.

symbol_to_prolog_str(symbol) → str

Convert a Symbol to a valid Prolog term string using strict prefix notation.

Module Contents

parse_term(text: str) → Symbol[source]

Read the Prolog term text.

A term is read around the first of :-, ?-, ::, ; and , outside brackets; , and ; group to the right. Otherwise, text holding one of Prolog’s operators of priority 700 outside brackets is read around it: is, unification = \=, identity == \==, the standard order of terms @< @> @=< @>= and arithmetic comparison =:= =\= < > =< >=, so X + 1 =:= Y * 2 is =:=(+(X, 1), *(Y, 2)). Text holding two of them is not. Otherwise, text made of operands joined by the arithmetic operators + - * / // mod rem div ^ /\ \/ xor << >> is read with Prolog’s priorities and associativity: Z is (N1 + N2) mod 10 is is(Z, mod(+(N1, N2), 10)). Any other text is read by parse_symbol(), with each argument read as a Prolog term.

parse_atoms(
atoms_str: str,
split_str: str,
atom_parser: Callable[[str], Symbol] = parse_term,
) → Iterable[Symbol][source]

Parse atoms_str split on split_str with atom_parser per atom.

atom_parser defaults to parse_term(); an extension passes its own (e.g. one that peels a p :: label prefix) to reuse this splitting.

parse_rule(
line: str,
atom_parser: Callable[[str], Symbol] = parse_term,
) → RuleType[source]

Parse one clause line h1 ; ... <functor> b1 , ... ending in ..

The head/body/functor splitting is generic; atom_parser decides how each atom is read, so extensions reuse this for their own surface syntax. Every _ is a variable of its own, as in Prolog: it is named _1, _2, … with names the clause does not already use.

str_to_rule(line: str) → RuleType[source]

Parse a single plain-Prolog clause line.

iter_clauses(code: str) → Iterable[str][source]

Split source text into .-terminated clause strings.

A period ends a clause only at bracket depth zero and when followed by whitespace or the end of the text, so floats (0.8::a) and periods inside argument positions never split. Clauses may share a line or span several lines (newlines inside a clause are folded to spaces). # comment lines are skipped. A trailing fragment without its terminator is yielded as-is so the rule parser reports it instead of dropping it silently.

str_to_rules(
code: str,
) → Iterable[RuleType][source]

Parse a plain-Prolog program; clauses may share a line.

split_list(
term: Symbol,
) → tuple[list[Symbol], Symbol][source]

Return the elements of the list term and the tail they end in.

A list is a cons/nil chain, so [a, b] is ([a, b], nil), [a | T] is ([a], T), and a term that is not a list is ([], term).

symbol_to_prolog_str(symbol: Symbol) → str[source]

Convert a Symbol to a valid Prolog term string using strict prefix notation.

Translates cons/nil chains (deeplog’s internal list encoding) to Prolog list syntax [h1, h2, ... | tail] so SWI list builtins like nth0/3 and member/2 operate on them.