DeepProbLog language
SPECIFICATION
Logic accepted by the DeepProbLog solver
The DeepProbLog Solver, driving a plain-Prolog grounder
from deeplog.grounding.prolog, interprets a compact DeepProbLog-like language.
This page spells out its syntax, lexical rules, and the operational semantics that ultimately produce
boolean proof formulas, which compile to differentiable modules.
Syntactic categories
Programs are sequences of clauses emitted by deeplog.grounding.prolog.str_to_rules()
or built manually through the helper constructors in deeplog.grounding.prolog.
Clauses are always terminated with a . and belong to one of four forms:
Rules –
H :- B.whereHis a disjunction of one or more atoms andBis a (possibly empty) conjunction of literals.Facts – either
a.(deterministic) orlabel :: a.(probabilistic) which are just rules with an empty body.Queries –
?- B.which request the engine to solveBand produce answer substitutions.Constraints –
:- B.which reject the program wheneverBis provable.
Lexical conventions mirror Prolog:
Variables begin with an uppercase letter or
_._denotes an anonymous variable.Atoms / functors begin with a lowercase letter or are quoted; numerals are treated as atoms.
Terms are atoms, variables, or functor applications
f(t1,...,tn)whose arguments are themselves terms.Labels reuse the term grammar so probabilistic facts can refer to neural predicates or numeric tensors.
trueandfalseare reserved: the helper constructors map empty bodies totrueand empty heads tofalse.
Grammar
The parser in deeplog.grounding.prolog.str_to_rule() accepts the following grammar
({x} means zero or more occurrences and [x] means optional):
<program> ::= { <clause> }
<clause> ::= <rule> | <fact> | <query> | <constraint>
<rule> ::= <disjunctive-head> ":-" <body> "."
<fact> ::= <labeled-atom> "."
<query> ::= "?-" <body> "."
<constraint> ::= ":-" <body> "."
<disjunctive-head>::= <labeled-atom> { ";" <labeled-atom> }
<body> ::= "true" | <goal>
<goal> ::= <literal> { "," <literal> }
<literal> ::= <atom>
| "not" <atom>
| "(" <goal> ")"
| "(" <goal> ";" <goal> ")"
<labeled-atom> ::= [ <term> "::" ] <atom>
<atom> ::= <predicate> [ "(" <term> { "," <term> } ")" ]
<term> ::= <variable> | <constant> | <atom> | <list>
<list> ::= "[" [ <list-elements> ] "]"
<list-elements> ::= <term> { "," <term> } [ "|" <term> ]
<predicate> ::= <constant>
; inside a head produces a disjunctive rule and is also available in bodies through explicit parenthesised
subgoals. Nested parentheses are parsed with deeplog.util.bracket_aware_split(), so constructs such as
a :- (b ; c), d. are valid. List terms are syntactic sugar: the grammar above rewrites [t1,...,tn|tail] into
cons(t1, cons(t2, ... cons(tn, tail)...)) with [] treated as the atom nil.
Parser limitations
The lightweight parser reads compound terms and lists as parse_symbol() does, and Prolog’s
operators itself; it is not the full ISO Prolog grammar. Known limitations:
Only
#-prefixed full-line comments are ignored. Inline%or/* */comments are treated as atoms.Operator declarations are not recognised. Besides the fixed connectives
:-,?-,::,',',';'andnot/1, Prolog’s operators of priority 700 (is, unification= \=, identity== \==, the standard order of terms@< @> @=< @>=and arithmetic comparison=:= =\= < > =< >=), and the arithmetic operators+ - * / // mod rem div ^ /\ \/ xor << >>, which are read with Prolog’s priorities (Z is (N1 + N2) mod 10,X + 1 =:= Y * 2), every other functor must be written in prefix form.Atoms are not unquoted:
'has space'becomes the literal functor"'has space'"(quotes included). Escape sequences are not interpreted.Numbers are parsed greedily as atoms; there is no automatic float/integer detection beyond what built-ins do when evaluating arithmetic.
List support
Square-bracket list syntax is supported directly by parse_symbol(). The parser rewrites
[t1,t2,...,tn] and [H|T] into nested cons/2 functors that terminate in the atom nil/0:
>>> from deeplog import parse_symbol
>>> parse_symbol('[a,b,c]')
('cons', ('a',), ('cons', ('b',), ('cons', ('c',), ('nil',))))
>>> parse_symbol('[H|T]')
('cons', ('H',), ('T',))
This applies recursively, so nested lists (e.g. [a,[b,c],d|T]) work as expected. These cons tuples behave
exactly like canonical Prolog lists during unification and rule evaluation, so you can pattern match on [Head|Tail]
in rule heads or bodies without extra boilerplate.
Operational semantics
Inference follows memoised SLD-resolution as implemented in SimpleGrounder.
For a goal G the grounder:
Chooses the predicate at the root of
G.Dispatches special connectives
','/2,';'/2,not/1andtrue/0directly.Matches built-in predicates (see below) before consulting user rules.
Selects every rule whose head predicate unifies with the goal, substitutes variables, and recursively proves the body.
Answer substitutions retain bindings only for variables that appeared in the original goal. The memoisation layer collapses duplicate substitutions by disjoining their formulas.
Boolean proof formulas
A grounder returns each answer’s proof as a boolean formula
(FormulaNode):
trueandfalseare boolean constant atoms.A conjunction or disjunction of proofs is an
and/orBinaryOp, and a negation anotUnaryOp.An open-predicate fact is a leaf
Atom; every other (closed) fact collapses totrue. The grounder attaches no meaning to those leaves.
The result is always a boolean proof formula. Probabilistic labels are reattached
afterwards by the DeepProbLog Solver
(matching each ground leaf to its label :: atom declaration), and
compile_to_module() compiles the result into a
differentiable module (see Compiling an engine result below and
Circuits and transformations).
Built-in predicates
The default grounder ships with a small built-in predicate library. See
DeepProbLog built-in predicates for the full list and semantics. Additional
predicates can be registered by calling add_builtin().
Example
digit(0). digit(1). digit(2). digit(3). digit(4).
digit(5). digit(6). digit(7). digit(8). digit(9).
nn_is_sum(A, B) :: sum(A, B, S).
valid_sum(A, B, S) :-
digit(A),
digit(B),
between(0, 18, S),
sum(A, B, S).
?- valid_sum(D1, D2, R).
The deterministic digit facts collapse to true leaves, while the labeled sum/3 fact introduces a
learnable probability (nn_is_sum/2) that is multiplied into every proof of valid_sum/3. The final query
returns all pairs of digits together with the proof formulas that DeepLog compiles into a differentiable module.
Compiling an engine result
Added in version 2.2.0.
After the solver produces an
EngineResult, the
compilation module turns it into a single differentiable
DeepLogModule.
compile_to_module() does this in one call:
Gives each random atom a variable of its own: a probabilistic fact a two-valued variable, an annotated disjunction one variable over its values. Each proof becomes the
expectationof its formula, with its atoms turned into tests of their variables’ values, and its distribution the product of those atoms in probability, each extended with its variable.Declares the variables’ domains and their labelling function, built from the engine’s atom labels, to the compiler (
declaring()). A label is a constant, or the value of the atom it names: a network’s output when the compiler has a builder for that atom, and an input of the module otherwise.Compiles every expectation together, which knowledge-compiles the proofs into the probability semiring once for all of them (see Circuits and transformations). With evidence, each answer divides by the one expectation of the evidence.
from deeplog import Compiler
from deeplog.grounding.prolog import SimpleGrounder
from deeplog.systems.deepproblog import Solver, compile_to_module
result = Solver(SimpleGrounder()).get_query_result(program)
module = compile_to_module(result, Compiler())
# module accepts input tensors and returns query probabilities
output = module(input_tensor)
The engine result’s formulas and labels can also be read directly:
for answer, formula in result.formulas.items():
print(f"{answer}: {formula}") # the proof, as formula text
print(result.labels)
See also
SimpleGrounderfor the pure Python interpreter.JanusGrounderfor the Janus/SWI-Prolog backend that accepts the same language.Circuits and transformations for the circuit transformation API used internally.