DeepLog language
SPECIFICATION
DeepLog language
The DeepLog language is a compact syntax that maps directly to formula nodes. It is used to build differentiable circuits from logical formulas and supports aggregations, transforms, unary/binary operators, and predicate leaves.
Overview
The DeepLog language is a compact formula syntax built around aggregations, operators, transformations, and structured predicate leaves. It is designed to be readable while still mapping directly to symbolic formula graphs.
The reference grammar lives in docs/textual_formula_grammar.md.
Lexical elements
Identifier: ASCII names that start with a letter or underscore and then use letters, digits, or underscores. These name operators, aggregations, structures, and variables.
Symbol literal: anything accepted by
deeplog.symbol.parse_symbol()(e.g.p(X,1),('=',X,true)).Structure: an identifier such as
booleanorprobability.Variable: an identifier that will be converted to a
Symbol; conventionally variables are capitalized.Whitespace: spaces, tabs, and newlines are insignificant.
Comments: lines starting with
#are ignored.
Grammar summary
Formula ::= Aggregation
| BinaryExpr
| UnaryExpr
| Transformation
| AggregationOp
| Leaf
Aggregation ::= Identifier "(" BinderList [";" ParamList] ")" ":" Formula
BinderList ::= Variable { "," Variable }
ParamList ::= Formula { "," Formula }
BinaryExpr ::= UnaryExpr BinaryOp UnaryExpr { BinaryOp UnaryExpr }
BinaryOp ::= Identifier
UnaryExpr ::= PrefixOp UnaryExpr
| "(" Formula ")"
| Transformation
| AggregationOp
PrefixOp ::= Identifier
Transformation ::= "(" Formula ")" "_" Structure
AggregationOp ::= Identifier "[" Formula { "," Formula } "]"
Leaf ::= SymbolLiteral "_" Structure
Key semantics
Aggregation scope:
Op(X, Y; params): bodybindsXandYin the body and parameter formulas.Aggregations of operands:
Op[phi, psi]applies the aggregatorOpof its operands’ algebra to their values, asOp(X): bodyapplies it to the body’s values at each value ofX. It binds nothing and takes one operand at least.Operator precedence: all binary operators share one precedence level and associate to the left; use parentheses to enforce evaluation order.
Unary operators: any identifier can act as a unary prefix operator. A functor written against its parentheses makes an atom, as in Prolog:
q(x_bar)_booleanis the atomq(x_bar), whileq (x_bar)_booleanapplies the operatorqto a cast.Transformations:
(phi)_structureturns a grouped formula into aTransformationnode.Leaves: every leaf pairs a symbol literal with its structure via the
_structuresuffix.Structure names: the built-in structures are
_boolean,_probability,_logprobability,_real,_mpe, and the fuzzy_godel,_productand_lukasiewicz.
Underscores are reserved for _structure suffixes. Predicate names that
include underscores should be quoted inside the symbol literal (e.g.
'=foo_bar').
Structures
Structures tag each atom with the “domain” it lives in, and they determine how operators are interpreted across a formula. The most common structures are:
Boolean: truth-valued formulas (
_boolean), typically combined with logical operators.Probability: weighted formulas (
_probability) that combine numeric scores.Log-probability: log-space weighted formulas (
_logprobability).Real: real numbers (
_real), such as a network’s logits or a count.Fuzzy: truth values in
[0, 1]under a t-norm: Gödel’s minimum (_godel), the product (_product) or Łukasiewicz’s (_lukasiewicz). Each readsandas its t-norm,oras the dual conorm,notas1 - xandimpliesas the t-norm’s residuum.
Structures are attached to leaves (predicate(args)_structure) and can be
changed mid-formula using transformations such as (expr)_probability. This
is how you express patterns like “evaluate a Boolean condition, then turn it
into a probability and multiply by literal weights.” The operator names in the
surface language stay the same; the structure tells the system how to interpret
them.
A structure also declares its aggregators: the operations an aggregation over
one of its formulas can name. real and probability aggregate with
sum, logprobability with sum as log-sum-exp, and boolean with
exists and forall. A fuzzy structure has the same two quantifiers:
forall is its t-norm over the variable’s domain, and exists its conorm.
Counting a Boolean formula’s models is therefore a
sum over the formula cast into real, where true is 1.
Structure classes
All structures are instances of AlgebraicStructure.
The built-in structures use specialised subclasses, each naming the axiom it
adds and the operator that carries it:
Semiring— adds namedproductandsumroles (with constantszeroandone). A circuit transformation maps operators by role, so a source’s product becomes the target’s product whatever the two call it.Algebra— extendsSemiringwith anegationrole, which is also mapped automatically.Semifield— aSemiringwhose product is invertible. Declaring that axiom is what defines division: the class registers adivisionoperator (divide, computed bydivision_fn), so a formula divides where the structure is a semifield and nowhere else. It is independent ofAlgebra— an invertible product says nothing about a complement — so a structure can be either, both, or neither.
BOOLEAN is an Algebra. PROBABILITY and LOGPROBABILITY are both
Algebra and Semifield, which is what makes a conditional probability
E[q and e] divide E[e] an ordinary formula rather than a special construct;
LOGPROBABILITY divides by subtracting, because that is what division is in
log space. GODEL is an Algebra too: the minimum distributes over the
maximum. PRODUCT and LUKASIEWICZ are plain structures, because their
and does not distribute over their or, so a formula over them is
evaluated as it is written. Custom structures use whichever classes provide
the roles they have. An operator’s function applies element by element: it
receives its operands as tensors of one shape and computes each entry of its
result from the operands’ entries at that position, so an evaluator can apply
it to many nodes of a circuit in one call.
from deeplog import Algebra, AlgebraicStructure, Semifield, Semiring
# Minimal: free-form operator and aggregator names
fuzzy = AlgebraicStructure(
name="fuzzy",
operator_fns={"and": ..., "or": ..., "not": ...},
aggregation_fns={"exists": ..., "forall": ...},
)
# Semiring: enables auto-mapping of product/sum
tropical = Semiring(
name="tropical",
product="plus", product_fn=lambda a, b: a + b,
sum="min", sum_fn=lambda a, b: torch.minimum(a, b),
)
# Algebra: enables auto-mapping of product/sum/negation
my_algebra = Algebra(
name="myalgebra",
product="times", sum="plus", negation="negate",
)
# Semifield: an invertible product, so `divide` is an operator as well
ratio = Semifield(
name="ratio",
product="times", sum="plus",
)
Examples
# Boolean structure: combine truth-valued literals
=(Burglary,true)_boolean or =(Earthquake,true)_boolean
# Probability structure: weight a Boolean formula and combine scores
(=(Burglary,true)_boolean or =(Earthquake,true)_boolean)_probability
times p(Burglary)_probability
# Fuzzy structure: the same connectives, under the Łukasiewicz t-norm
forall(X): smokes(X)_lukasiewicz implies cancer(X)_lukasiewicz
Predicates
Predicates are the smallest executable units in the language. A predicate ties
symbolic atoms (like =(X,true) or digit(I,N)) to tensor computations and
defines whether an atom holds (Boolean predicates) or how strongly it holds
(probability/neural predicates).
In the language syntax, predicates appear only as leaves with a structure suffix. The functor name and arguments describe the atom, while the structure decides the domain of the result. Built-in predicate modules cover common cases like equality, probabilities, arithmetic constraints, neural classifiers, and indexing. See DeepLog predicate modules for the full list and usage patterns.
Implementation hooks
These details explain how the language connects to the underlying codebase. They are not required to read or write formulas, but help when extending or debugging the parser.
Mapping to AST nodes
Syntax |
AST node |
|
|
|
|
|
|
|
|
|
|
|
|
Examples
Model counting
sum(Burglary, Earthquake):
(=(Burglary,true)_boolean or =(Earthquake,true)_boolean)_real
Weighted model counting
sum(Burglary, Earthquake):
(=(Burglary,true)_boolean or =(Earthquake,true)_boolean)_probability
times
(p(Burglary,0.2)_probability times p(Earthquake,0.6)_probability)
Parsing API
Use deeplog.formula.text_parser_lark.parse_formula() to parse a formula into
factory calls, or deeplog.formula.text_parser_lark.parse_formula_to_module()
to parse and immediately build a DeepLogModule.
See also
DeepLog predicate modules for the built-in predicate modules.
DeepProbLog language for the Prolog-style language used by the DeepProbLog engines.