# On Scaling Neurosymbolic Programming through Guided Logical Inference

Thomas Jean-Michel Valentin, Luisa Sophie Werner, Pierre Genevès, Nabil Layaïda

► To cite this version:

Thomas Jean-Michel Valentin, Luisa Sophie Werner, Pierre Genevès, Nabil Layaïda. On Scaling Neurosymbolic Programming through Guided Logical Inference. 2025. hal-04914004

HAL Id: hal-04914004

https://hal.science/hal-04914004v1

Preprint submitted on 29 Jan 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# On Scaling Neurosymbolic Programming through Guided Logical Inference

Thomas Jean-Michel Valentin¹,², Luisa Sophie Werner², Pierre Genèves², Nabil Layaïda²

¹ENS Paris-Saclay, 4 av des Sciences, 91190 Gif-sur-Yvette, France, https://ens-paris-saclay.fr/

²TYREX Univ. Grenoble Alpes, CNRS, Inria, Grenoble INP, LIG, 655 av Europe, 38330 Montbonnot
Saint Martin, France, https://www.inria.fr/fr/tyrex

thomas.valentin@ens-paris-saclay.fr, {Luisa.Werner, Pierre.Geneves}@inria.fr

## Abstract

Probabilistic neurosymbolic learning seeks to integrate neural networks with symbolic programming. Many state-of-the-art systems rely on a reduction to the Probabilistic Weighted Model Counting Problem (PWMC), which requires computing a Boolean formula called the logical provenance. However, PWMC is #P-hard, and the number of clauses in the logical provenance formula can grow exponentially, creating a major bottleneck that significantly limits the applicability of PNL solutions in practice. We propose a new approach centered around an exact algorithm DPNL, that enables bypassing the computation of the logical provenance. The DPNL approach relies on the principles of an oracle and a recursive DPLL-like decomposition in order to guide and speed up logical inference. Furthermore, we show that this approach can be adapted for approximate reasoning with $\epsilon$ or $(\epsilon, \delta)$ guarantees, called ApproxDPNL. Experiments show significant performance gains. DPNL enables scaling exact inference further, resulting in more accurate models. Further, ApproxDPNL shows potential for advancing the scalability of neurosymbolic programming by incorporating approximations even further, while simultaneously ensuring guarantees for the reasoning process.

## 1 Introduction

Neurosymbolic artificial intelligence [van Harmelen and ten Teije, 2019; Vermeulen et al., 2023] seeks to integrate neural perception with symbolic methods to produce more accurate, efficient, and reliable models.

Probabilistic Neurosymbolic Learning is a research direction that combines neural networks with probabilistic reasoning [Manhaeve et al., 2018; De Raedt et al., 2020; Li et al., 2023]. Neural networks are employed to extract probability distributions from raw data. They are end-to-end integrated with the reasoning process of a logic program to derive solutions. In contrast to pure neural black-box models, the passage through a logic program provides a clear lineage of how a result is derived, enhancing interpretability.

However, neurosymbolic models face significant scalability challenges due to the complexity of the inference in the logic program. Prominent systems for exact inference such as DeepProbLog [Manhaeve et al., 2018] rely on a reduction to the Probabilistic Weighted Model Counting problem (PWMC). This requires computing a logical provenance formula, whose size can grow exponentially depending on the task. Furthermore, solving the PWMC is #P-hard. This significantly limits the applicability of PNL approaches in practice, as they struggle to handle large problem instances effectively.

Approximations have been developed to overcome these scalability issues [Manhaeve et al., 2021; Winters et al., 2022; van Krieken et al., 2023; Huang et al., 2021]. However, these approximations do not provide guarantees, which potentially undermines one of the primary benefit of PNL: increased confidence in the reliability of the model.

**Contribution.** We introduce a new approach for probabilistic neurosymbolic learning based on a new algorithm, DPNL, for exact probabilistic reasoning. One of its main advantages is that it bypasses the explicit computation of the logical provenance formula. Instead, DPNL relies on an oracle to guide and accelerate the logical inference process. This is achieved by recursively decomposing probabilistic computations in a DPLL-inspired manner. We explain how to construct valid oracles and formally prove the termination and correctness of DPNL. Additionally, it can be adapted for approximate computations with $\epsilon$ or $(\epsilon, \delta)$ guarantees. Experiments demonstrate that DPNL scales better in practice. This makes it possible to build accurate models for tasks that previous PNL methods with exact inference cannot handle. Furthermore, the experiments show that ApproxDPNL is advantageous in scaling DPNL to even larger tasks while providing reliability in the form of guarantees for the approximation.

## 2 Problem statement and definitions

**Notations.** We first introduce some notations. A function $f$ that maps elements from a set $A$ to a set $B$ is denoted as $f: A \to B$. The set of all such functions is written as $\mathcal{F}(A, B)$. For any function $f: A \to B$ and any $y \in B$, the preimage of $y$, i.e., the set of all $x \in A$ such that $f(x) = y$, is denoted by $f^{-1}(y)$. An estimator of a function $f$ is denoted by $\hat{f}$.

We consider the Probabilistic Learning (PL) problem and
focus on solving it using Probabilistic Neurosymbolic Learning (PNL) systems, which integrate both neural and symbolic components. The formal definitions follow.

**Probabilistic Learning Problem.** A PL problem is a quadruplet $P = ((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$ where:

* $(\Omega, \mathcal{A}, \mathbb{P})$ is a probability space with sample space $\Omega$, event space $\mathcal{A}$ and probability function $\mathbb{P}$.

* $\mathcal{I}$ is the input space.

* $\mathcal{O}$ is the finite output set.

* $F: \mathcal{I} \to \mathcal{F}(\Omega, \mathcal{O})$ is a function that maps each input $i \in \mathcal{I}$ to its output random variable $F(i)$.

The probability distribution of $P$ is the function $p$ that maps every input $i$ to the probability distribution of $F(i)$:

$$ \forall i \in \mathcal{I}, p(i) : o \in \mathcal{O} \mapsto \mathbb{P}(F(i) = o). $$

We define solving $P$ as estimating $p$.

**Example 1 (MNIST Classification).** Consider the MNIST classification task. The goal is to recognise handwritten digits in images, e.g. the digit in $\mathfrak{g}$ is 8. This task can be modelled as the MNIST PL problem $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}_{\text{MNIST}}, \mathcal{O}_{\text{MNIST}}, F_{\text{MNIST}})$, where:

* $\mathcal{I}_{\text{MNIST}} = \mathbb{R}^{28 \times 28}$ represents the space of all possible images,

* $\mathcal{O}_{\text{MNIST}} = [0, 9]$ is the set of possible output labels,

* $F_{\text{MNIST}}$ associates with each image $i$ the random variable $F_{\text{MNIST}}(i)$ for the digit in $i$.

Solving the MNIST PL problem amounts to estimating the function $p_{\text{MNIST}}$ that maps each image $i$ to the probability distribution of $F_{\text{MNIST}}(i)$, which can be done using a neural network $\hat{p}_{\text{MNIST}}$.

**Probabilistic Neurosymbolic Learning System.** Let $P = ((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$ be a PL problem and $p$ its corresponding probability distribution. A PNL system for $P$ is a quadruplet $((V_k)_{1 \le k \le m}, (X_k)_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ where:

* $m \ge 1$ is the order of the PNL system.

* $\forall k \in [1, m], P_k = ((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, V_k, X_k)$ is a PL problem with its probability distribution $p_k$.

* $\forall i \in \mathcal{I}$, the random variables $(X_k(i))_{1 \le k \le m}$ are independent. We say that the sub-problems $(\hat{P}_k)_{1 \le k \le m}$ are independent.

* $\forall k \in [1, m], \hat{p}_k$ is a neural network which solves $P_k$, i.e, it estimates $p_k$. The set of parameters of $\hat{p}_k$ is $\theta_k$.

* $S: V_1 \times \dots \times V_m \to \mathcal{O}$ is a symbolic function that maps values obtained from the sub-problems to the output. $S$ is known.

* $F = S \circ (X_1, \dots, X_m)$, that is, for all $i \in \mathcal{I}$ and $w \in \Omega$, $F(i)(w) = S(X_1(i)(w), \dots, X_m(i)(w))$.

The PNL system decomposes $P$ into $m$ smaller, independent sub-problems $(\hat{P}_k)_{1 \le k \le m}$. Each sub-problem $\hat{P}_k$ is solved by estimating $p_k$ with $\hat{p}_k$. The outputs of $\hat{p}_k$ are then combined using the symbolic function $S$ to estimate $p$ thanks to

the following relation, which holds because of the independence assumption of the random variables:

$$ p(i)(o) = \sum_{(x_1, \dots, x_m) \in S^{-1}(o)} \mathbb{P}(\bigcap_{k=1}^{m} X_k(i) = x_k) \quad (1) $$

$$ = \sum_{(x_1, \dots, x_m) \in S^{-1}(o)} \prod_{k=1}^{m} p_k(i)(x_k). \quad (2) $$

The corresponding estimator $\hat{p}$ is defined by:

$$ \hat{p}(i)(o) = \sum_{(x_1, \dots, x_m) \in S^{-1}(o)} \prod_{k=1}^{m} \hat{p}_k(i)(x_k). \quad (3) $$

Moreover, $\hat{p}(i)(o)$ is differentiable w.r.t. to $\theta = (\theta_k)_{1 \le k \le m}$, which allows to train the $(\hat{p}_k)_{1 \le k \le m}$ directly with gradient descent and a data set for the problem $P$.

**Major Challenge.** The primary computational challenge lies in efficiently evaluating the sum over $S^{-1}(o)$ in equation (3). Indeed, $|S^{-1}(o)|$ can grow exponentially w.r.t. $m$, and inverting a symbolic function is in essence a NP-hard task. Therefore, addressing the complexity of computing $S^{-1}(o)$ is a key focus in improving PNL system scalability.

**Example 2 (MNIST-SUM Task).** We consider the neurosymbolic MNIST-SUM task [Manhaeve et al., 2018], which will be used as a running example. The goal is to learn to predict the sum of handwritten digits, given only the label of the sum, but not the labels of the individual digits in the images, e.g. $\mathfrak{g} + \mathfrak{g} = 8$. This involves two subtasks: (1) perception of the digit in the image and (2) reasoning on the combination of the digits (with the sum as the symbolic function $S$). Building on the MNIST PL problem, see Example 1, we define the MNIST-SUM PL problem as $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$, where

* $\mathcal{I} = \mathcal{I}_{\text{MNIST}} \times \mathcal{I}_{\text{MNIST}}$ are pairs of images,

* $\mathcal{O} = [0, 18]$ are the output values of the sum,

* $F(i_1, i_2) = F_{\text{MNIST}}(i_1) + F_{\text{MNIST}}(i_2)$ is the random variable related to the sum of the digits in the two images $i_1, i_2$.

For this task, we also define the PNL system $((V_1, V_2), (X_1, X_2), (\hat{p}_1, \hat{p}_2), S)$, where

* $V_1 = V_2 = [0, 9]$,

* $X_1(i_1, i_2) = F_{\text{MNIST}}(i_1)$ and $X_2(i_1, i_2) = F_{\text{MNIST}}(i_2)$ for all pairs of images $(i_1, i_2) \in \mathcal{I}_{\text{MNIST}} \times \mathcal{I}_{\text{MNIST}}$,

* $\hat{p}_1(i_1, i_2) = \hat{p}_{\text{MNIST}}(i_1)$ and $\hat{p}_2(i_1, i_2) = \hat{p}_{\text{MNIST}}(i_2)$ for all $(i_1, i_2) \in \mathcal{I}^2$,

* $S(d_1, d_2) = d_1 + d_2$ for all digit values $d_1 \in V_1, d_2 \in V_2$.

Thus, this PNL system for MNIST-SUM consists of two independent MNIST PL problems for classifying the digits in the images.

This task can easily be extended to MNIST-N-SUM, where $N$ is the number of digits per summand. In this notation, the previous example is a MNIST-1-SUM. For $N=2$, two digits form the summands, e.g. $\mathfrak{g} + \mathfrak{g} = 63$. By extending from one-digit sums ($N=1$) to two-digits sums ($N=2$),
values in the range [0, 9] than to encode them with independent binary random variables (which would result in an exponential blowup, as done in [Manhaeve et al., 2018] and detailed in Appendix G). ProbDPLL does not require $G$ to be in CNF. As long as $G$ is represented in a form that allows the algorithm to partially evaluate it with respect to a variable $X$ and determine whether this partial evaluation makes $G$ true or false, the algorithm will function correctly. Furthermore, $G$ does not even need to be explicitly known. ProbDPLL can operate as long as it has access to the answers to the question: “Does the partial valuation evaluate $G$ to true or false?”

## 3.2 The DPNL Algorithm

We now introduce the main contribution: the DPNL algorithm, which generalizes ProbDPLL to support more general symbolic functions $S$ (not necessarily Boolean formulas like $G$) and independent random variables with finite domains (instead of only independent binary random variables).

Instead of working directly with a specific representation of $S$, as ProbDPLL does with $G$, DPNL follows a different approach. It calls an oracle function that extracts only the necessary information about $S$ to guide the inference process in a way similar to ProbDPLL. The concept of an oracle here is an abstraction that captures precisely the properties required for DPNL to be correct. The advantage of this method is that, in many cases, the oracle can be thoughtfully implemented, making DPNL more efficient than other PNL frameworks, such as DeepProbLog.

We now formally define the concepts of valuations and oracles used in DPNL.

**Valuation.** A valuation $v$ of the variables $(X_k)_{1 \le k \le m}$ is an array of size $m$ such that for all $k \in [1, m]$, $v[k] \in V_k \cup \{\text{unknown}\}$. The following notations are used:

* $\mathcal{V}$ denotes the set of all valuations.

* A total valuation is one where: $\forall k \in [1, m], v[k] \neq \text{unknown}$.

* A partial valuation is one that is not total.

* $v'$ is a sub-valuation of $v$ if:

$$ \forall k \in [1, m], v[k] \neq \text{unknown} \implies v'[k] = v[k] $$

* $\text{sub}(v)$ is the set of sub-valuations of $v$.

* $\text{tot}(v)$ is the set of total sub-valuations of $v$.

* $w(i, v)$ is the event corresponding to $v$ with input $i$. Formally, it is the set of $w \in \Omega$ such that $[X_1(i)(w), \dots, X_m(i)(w)] \in \text{tot}(v)$.

* $v_{v[k] \leftarrow x}$ is the valuation $v$ where the value of $v[k]$ is replaced by $x$.

**Oracle.** Given the symbolic function $S$ of a PNL system, an oracle $O_S$ for $S$ is a function that, given a valuation $v \in \mathcal{V}$ and an output $o \in \mathcal{O}$, answers the question “Is $S$ always equal to or always different from $o$ for all sub-valuations of $v$?”. Intuitively, from a given output $o$, the oracle is in charge of determining whether it is still relevant to explore sub-valuations or whether the exploration process can be stopped.

Formally, a function $O_S: \mathcal{V} \times \mathcal{O} \to \{0, 1, \text{unknown}\}$ is a valid oracle for $S$ if and only if, for all $v \in \mathcal{V}$ and $o \in \mathcal{O}$:

**Algorithm 2 DPNL(i, o, v, O_S)**

Input: $i \in \mathcal{I}, o \in \mathcal{O}, v \in \mathcal{V}, O_S$ an oracle for $S$

Output: $\mathbb{P}(F(i) = o|w(i, v))$

1. **if** $O_S(v, o) = 1$ **then**

2. **return** 1

3. **else if** $O_S(v, o) = 0$ **then**

4. **return** 0

5. **else**

6. Choose $k \in [1, m]$ such that $v[k] = \text{unknown}$.

7. **return** $\sum_{y \in V_k} \mathbb{P}(X_k(i) = y) \cdot \text{DPNL}(i, o, v_{v[k] \leftarrow y}, O_S)$

8. **end if**

* If the valuation $v$ is total,

$$ O_S(v, o) = \begin{cases} 1 & \text{if } S(v[1], \dots, v[m]) = o, \\ 0 & \text{otherwise} \end{cases} $$

* If $O_S(v, o) = 1$,

$$ \forall v' \in \text{tot}(v), S(v'[1], \dots, v'[m]) = o $$

* If $O_S(v, o) = 0$,

$$ \forall v' \in \text{tot}(v), S(v'[1], \dots, v'[m]) \neq o $$

We say that the oracle is complete if the last two points are equivalences.

**DPNL.** The pseudocode of DPNL is given in Algorithm 2. We can compute $p(i)(o)$ by initially calling DPNL$(i, o, [\text{unknown}, \dots, \text{unknown}], O_S)$. In practice, however, we want to compute $\hat{p}(i)(o)$ because we do not know $\mathbb{P}$. Therefore, we replace $\mathbb{P}(X_k(i) = y)$ at line 7 by its neural estimation $\hat{p}_k(i)(y)$.

**Termination and Correctness.** For every

* PL problem $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$,

* PNL system $((V_k)_{1 \le k \le m}, (X_k)_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ for the PL problem,

* valid oracle $O_S$ for $S$,

* $i \in \mathcal{I}, o \in \mathcal{O}$ and $v \in \mathcal{V}$,

DPNL$(i, o, v, O_S)$ always terminates and returns $\mathbb{P}(F(i) = o|w(i, v))$. A detailed proof of the termination and correctness of the algorithm is provided in Appendix B.

**Advantages of using Oracles**

For any symbolic function $S$, if we dispose of a procedure computing $S$, then there exists a valid oracle for $S$. Indeed, the oracle that returns unknown when the valuation is partial and tests if the result of $S$ is equal to the output when the valuation is total, is valid for $S$. However, with this naive oracle, the complexity of DPNL is the same as testing $S$ over all possible inputs to compute $S^{-1}(o)$. It is thus important to design oracles that return 0 or 1 for partial valuations in order to prune the search space of DPNL. Again, a naive oracle could test all total sub-valuations of $v$ to answer 0 or 1 as soon as possible, but this is not efficient. Thus, a good oracle should strike a balance between pruning capability and efficiency.

In practice, $S$ could naturally be represented by an algorithm. It is often possible to derive an efficient valid oracle
$O_S$ with pruning capability from this algorithm. For example, in the MNIST-N-SUM task, $S$ is the sum algorithm which operates digit by digit, computing the digits of the result from right to left (see Algorithm 5 in Appendix D). Since this algorithm constructs the result of $S$ digit by digit, we can take advantage of it to design an efficient oracle which is able to prune the search space (see Algorithm 6 in Appendix D).

This is a major advantage of the DPNL approach: it can readily work with naive oracles, while providing the flexibility to define custom oracles, allowing as much refinement and optimization as desired, depending on the effort invested in their design.

In the next section, we explain how valid oracles can be systematically derived from more general programs.

## 4 Obtaining Oracles from Logic Programs

We now explain how valid oracles can be systematically derived from a broad class of logic programs. To this end, we first recall the connection between logic and probabilistic learning (PL) problems. Next, we define a class of neural probabilistic logic programs, building on ProbLog [De Raedt et al., 2007] and DeepProbLog. Finally, we provide an algorithm to compute oracles from these programs, and prove that the obtained oracles are valid. The formal definitions follow.

**Logics.** A logic $\mathcal{L}$ consists of a set of functions $\mathcal{F}$ and predicates $\mathcal{P}$, logical operators such as $\wedge$ and $\exists$, their semantics, and an infinite set of variables $\mathcal{X}$. Combinations of functions and variables form terms, predicates applied to terms form atoms and logical combinations of atoms form logical formulas. The purpose of a logic is to model aspects of reality, which is formalized through an interpretation $I = (\Delta, [.]_\mathcal{F}, [.]_\mathcal{P})$, where:

* $\Delta$ is a set of real objects called the domain,

* for all $f \in \mathcal{F}, [f]_\mathcal{F} \in \mathcal{F}(\Delta^{arity}(f), \Delta)$ is the interpretation of $f$,

* for all $p \in \mathcal{P}, [p]_\mathcal{P} \in \mathcal{F}(\Delta^{arity}(p), \{0, 1\})$ is the interpretation of $p$.

This defines a semantics $[.]_I$ by recursively applying the semantics of logical operators and the interpretations of functions and predicates.

**Example 4 (Logic for MNIST).** In MNIST, we can consider $\mathcal{L}$ where $\mathcal{F} = \{i_1(0), i_2(0), \underline{0}(0), ..., \underline{9}(0)\}$ and $\mathcal{P} = \{is(2)\}$ where $s(k)$ denotes a symbol of arity $k$. We can define $I = (\text{Images} \cup [0, 9], [.]_\mathcal{F}, [.]_\mathcal{P})$ where $[i_1]_\mathcal{F}, [i_2]_\mathcal{F}$ are images, $[d]_\mathcal{F} = d$ and where, for example, $[is(i_1, \underline{7})]_I = [is]_\mathcal{P}([i_1]_\mathcal{F}, [\underline{7}]_\mathcal{F}) = 1 \text{ iff } [i_1]_\mathcal{F} \text{ is an image of } 7$.

In the MNIST classification example, the task consists in estimating the probability of $is(i_1, d)$ for every digit $d$. Therefore, we can model this probabilistic aspect in the logic by considering the predicate interpretation as probabilistic. We can then define logic PL problems by following this idea.

**Logic PL problem.** Consider a logic $\mathcal{L}$, a domain $\Delta$ and a closed (i.e without variables) logical formula $\phi$ over $\mathcal{L}$. We define:

* $\Gamma_{\mathcal{L},\Delta} = (\Omega_{\mathcal{L},\Delta}, A_{\mathcal{L},\Delta}, \mathbb{P}_{\mathcal{L},\Delta})$ is the probabilistic space of $\mathcal{L}$ in the real context $\Delta$. It models the probabilistic aspect of facts by taking $\Omega_{\mathcal{L},\Delta}$ as the set of all predicate interpretations $[.]_\mathcal{P}$ for $\Delta$ that are possible. $\mathbb{P}_{\mathcal{L},\Delta}$ is specific to the real context and models the likelihood of each $[.]_\mathcal{P}$.

* $\mathcal{I}_{\mathcal{L},\Delta}$ is a subset of all possible function interpretations $[.]_\mathcal{F}$ for $\Delta$. It models the possible observations about the reality and depends on the real context.

* $B_{\mathcal{L},\Delta}^{\phi}$ is the function that maps interpretations of $\mathcal{I}_{\mathcal{L},\Delta}$ to binary random variables in $\Gamma_{\mathcal{L},\Delta}$ corresponding to $\phi$:

$$B_{\mathcal{L},\Delta}^{\phi}([.]_\mathcal{F})([.]_\mathcal{P}) = [\phi]_{(\Delta, [.]_\mathcal{F}, [.]_\mathcal{P})}$$

* $P_{\mathcal{L},\Delta}^{\phi} = (\Gamma_{\mathcal{L},\Delta}, \mathcal{I}_{\mathcal{L},\Delta}, \{0, 1\}, B_{\mathcal{L},\Delta}^{\phi})$ is the logic PL problem of $\phi$.

By taking inspiration from ProbLog and DeepProbLog, we now define a class of PNL systems for logic PL problems. Consider a logical formula $\phi$. The idea is to decompose $P_{\mathcal{L},\Delta}^{\phi}$ into $m$ smaller logic PL problems $P_{\mathcal{L},\Delta}^{\phi_1}, ..., P_{\mathcal{L},\Delta}^{\phi_m}$ where $\phi_1, ..., \phi_m$ represent simpler concepts. A symbolic function $S$, based on axioms about the context, combines $\phi_1, ..., \phi_m$ to determine the truth value of $\phi$.

**Logic PNL system.** Consider a logic $\mathcal{L}$ and a domain $\Delta$. Suppose the existence of an axiom set $A = \{\alpha_1, ..., \alpha_n\}$ such that all $\alpha_i$ are true in $\Gamma_{\mathcal{L},\Delta}$:

$$\bigcup_{[.]_\mathcal{F} \in \mathcal{I}_{\mathcal{L},\Delta}} (B_{\mathcal{L},\Delta}^{\alpha_i}([.]_\mathcal{F}) = 0) = \emptyset$$

We write $\{\Phi_1, ..., \Phi_l\} \vdash \Phi$ to denote that there exists a proof of $\Phi$ in $\mathcal{L}$ assuming $\Phi_1, ..., \Phi_l$. Now suppose there exist formulas $\phi_1, ..., \phi_m$ such that:

* $(P_{\mathcal{L},\Delta}^{\phi_k})_{1 \le k \le m}$ are independent logic PL problems,

* for all $\phi'_1, ..., \phi'_m$ such that $\phi'_k = \phi_k$ or $\phi'_k = \neg \phi_k$:

- $A \cup \{\phi'_1, ..., \phi'_m\} \vdash \phi$ or $A \cup \{\phi'_1, ..., \phi'_m\} \vdash \neg \phi$

- $A \cup \{\phi'_1, ..., \phi'_m\} \nvdash \bot$.

We say that the $\phi_1, ..., \phi_m$ decide $\phi$ and are coherent with respect to $A$.

Under these assumptions, we have sufficient elements to prove that the following constitutes a PNL system for $P_{\mathcal{L},\Delta}^{\phi}$: $((\{0, 1\})_{1 \le k \le m}, (B_{\mathcal{L},\Delta}^{\phi_k})_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$, where:

* for all $k \in [1, m], \hat{p}_k$ is a neural network which takes as input ground terms of $\phi_k$ interpreted as elements of $\Delta$ and returns an estimation of the probability distribution of $B_{\mathcal{L},\Delta}^{\phi_k}$,

* for all $x_1, ..., x_m \in \{0, 1\}, S(x_1, ..., x_m)$ is equal to

$$\begin{cases} 1 & \text{else if } A \cup \{\phi_k | x_k = 1\} \cup \{\neg \phi_k | x_k = 0\} \vdash \phi, \\ 0 & \text{otherwise.} \end{cases}$$

The proof is given in Appendix E.1.
**Algorithm 3 Logic Oracle**

**Input:** $v \in \mathcal{V}$, $o \in \mathcal{O}$

**Output:** A valid oracle output for $S$

1: **if** $A \cup \{\phi_k|v[k] = 1\} \cup \{\neg\phi_k|v[k] = 0\} \vdash \neg\phi$ **then**

2: $res \leftarrow 0$

3: **else if** $A \cup \{\phi_k|v[k] = 1\} \cup \{\neg\phi_k|v[k] = 0\} \vdash \phi$ **then**

4: $res \leftarrow 1$.

5: **else**

6: $res \leftarrow \text{unknown}$.

7: **end if**

8: **if** $res \neq \text{unknown} \land o = 0$ **then**

9: $res \leftarrow 1 - res$.

10: **end if**

11: **return** $res$.

**Oracles for Logic PNL systems.** Consider a logic PNL system such that the theory $(\mathcal{L}, A)$ is decidable. Then, Algorithm 3 describes the pseudocode of a valid and complete oracle for the function $S$ based on a decision procedure of $(\mathcal{L}, A)$. The proof is given in Appendix E.2.

We can link this to standard frameworks such as DeepProbLog. Indeed, the logic $\mathcal{L}$ corresponds to the logic of Prolog. $\Delta$ is the set of possible inputs for neural networks, which are real-world data such as images. The formula $\phi$ represents the query. The rules annotated by neural networks in DeepProbLog correspond to the $\phi_1, \dots, \phi_m$, and the neural networks producing their probabilities are $\hat{p}_1, \dots, \hat{p}_k$. Finally, $S$ corresponds to the logical provenance formula, as $S^{-1}(1)$ represents the set of proofs of $\phi$. However, DeepProbLog differs slightly because it does not consider negation. A detailed application to DeepProbLog is provided in Appendix F. This application is particularly interesting when the DeepProbLog program does not contain functors. Indeed, in a Prolog program without negation and functors the query answering task is polynomial time in the size of the program as it can be transformed into an equivalent Horn-SAT instance. An oracle based on a solver can thus answer in polynomial time with respect to the size of the program. In comparison, under these conditions, the size of the logical provenance computed by DeepProbLog could be factorial in the square root of the size of the program (see Example 5).

## 5 ApproxDPNL

DPNL can be readily adapted to approximate reasoning with guarantees on the approximation error. We consider $(\epsilon, \delta)$-approximation defined as follows, where $\epsilon$ denotes the relative error bounded by a parameter $\epsilon$ with probability $1 - \delta$ [Chakraborty et al., 2016; Dubray et al., 2024].

($\epsilon, \delta$)-approximation. An estimator $\hat{y}$ is an $(\epsilon, \delta)$-approximation when $\mathbb{P}(|\frac{y-\hat{y}}{y}| > \epsilon) \le \delta$.

$\epsilon$-approximation. An estimator $\hat{y}$ is an $\epsilon$-approximation when $y/(1+\epsilon) \le \hat{y} \le y/(1+\epsilon)$.

DPNL with $(\epsilon, \delta)$-approximations, named ApproxDPNL, is proposed in Algorithm 4. As proposed in [Dubray et al., 2024], ApproxDPNL can be stopped at any time $T$ and provides lower and upper bound guarantees on the result of the

**Algorithm 4 ApproxDPNL(i, o, $O_S$, STOP, H)**

**Input:** $i \in \mathcal{I}$, $o \in \mathcal{O}$, $v \in \mathcal{V}$, $O_S$ an oracle for $S$

**STOP:** function taking a lower bound and an upper bound of the result and tells the algorithm to stop or not.

**H:** An heuristic that choose a valuation from a set.

**Output:** An estimation of $\mathbb{P}(F(i) = o|w(v))$

1: Set $low, up := 0, 1$.

2: Set $queue := \{[\text{unknown}, \dots, \text{unknown}]\}$.

3: **while** $|queue| > 0$ and $\neg STOP(low, up)$ **do**

4: Set $v = H(queue)$.

5: $queue \leftarrow queue \setminus \{v\}$

6: Set $K := \{k \in [1, m] | v[k] \neq \text{unknown}\}$.

7: Set $prob := \Pi_{k \in K} \mathbb{P}(X_k(i) = v[k])$.

8: **if** $O_S(v, o) = 1$ **then**

9: $low \leftarrow low + prob$

10: **else if** $O_S(v, o) = 0$ **then**

11: $up \leftarrow up - prob$

12: **else**

13: Choose $k \in [1, m]$ such that $v[k] = \text{unknown}$.

14: $queue \leftarrow queue \cup \{v_{v[k] \leftarrow y} | y \in V_k\}$

15: **end if**

16: **end while**

17: **return** $\sqrt{low \times up}$

exact weighted model counting. The choice of the function STOP determines the guarantee of the result, where low and up are the lower and upper bound of the approximation interval.

* If $STOP(low, up) := \begin{cases} 1 & \text{if } up \le low \cdot (1 + \epsilon)^2 \\ 0 & \text{else} \end{cases}$, the result of ApproxDPNL is an $\epsilon$-approximation of $\mathbb{P}(F(i) = o|w(v))$ (see [Dubray et al., 2024]).

* If $STOP(low, up) := \begin{cases} 1 & \text{if } up - low \le \epsilon \\ 0 & \text{else} \end{cases}$, the result of ApproxDPNL $r$ respects the following property $\mathbb{P}(F(i) = o|w(v) - \epsilon \le r \le \mathbb{P}(F(i) = o|w(v) + \epsilon)$.

* If STOP returns 1 after time $T$ has passed, the result of ApproxDPNL is a biased approximation of $\mathbb{P}(F(i) = o|w(v))$ that took time $T$ to compute.

The heuristic $H$ controls in which order ApproxDPNL explores subvaluations. Intuitively, it is important to prioritise the exploration of subvaluations that are the most probable so that the size of the approximation interval $|up - low|$ narrows faster. However, it is also possible for use a random choice as $H$, which results in uniform sampling.

## 6 Experiments

We experimentally evaluate DPNL in order to answer the following research questions:

**Q#1** How does DPNL compare to PNL systems with exact reasoning in terms of efficiency and accuracy?

**Q#2** How does DPNL compare to PNL systems with approximate reasoning in the state-of-the-art in terms of efficiency and accuracy?
**Q#3** How does ApproxDPNL compare to exact and approximate reasoning methods in the state-of-the-art in terms of efficiency and accuracy?

**Task.** We evaluate DPNL on the popular MNIST-N-SUM task [Manhaeve et al., 2018] as introduced in Section 2. By extending from an N digits sum to an N+1 digits sum, we increase the complexity of the reasoning task exponentially. Thus, the multi-digit MNIST-N-SUM task is well-suited to investigate the scaling properties of DPNL. We use the MNIST dataset from [Lecun et al., 1998] that contains 60K training images and 10K test images. In consequence, with respect to N, the training set contains 60K / 2N images and the test set contains 10K / 2N images so that every image is used once. Each training sample consists of the images for the two commands and the supervision label on their ground truth sum.

**Experimental Setup.** We compare DPNL with the PNL systems DeepProbLog [Manhaeve et al., 2018] and Scallop [Huang et al., 2021] that use exact inference (**Q#1**), and with A-Nesi [van Krieken et al., 2023], DPLA\* [Manhaeve et al., 2021] and Scallop (with the top-k semiring), which use approximate inference (**Q#2**). A-Nesi is evaluated across its different variants (predict only, explain, prune). We further test ApproxDPNL compare the results to the previously mentioned exact and approximate methods (**Q#3**). We choose a range of different values for reasoning timeouts $T = \{0.05, 0.075, 0.1, 0.5\}$ for ApproxDPNL. We evaluate the test accuracy and the runtime of the symbolic reasoning component. To ensure comparability, we maintain consistent experimental settings across all models, by reproducing baseline results on the same hardware and using the same parameters.

Furthermore, we also want to compare exact and approximate reasoning. To address this, we use a common set of hyperparameters. For all methods, we employ the MNIST classifier architecture proposed in [Manhaeve et al., 2018]. The Adam optimizer [Kingma and Ba, 2017], a batch size of 2, a single training epoch, and a learning rate of $1e-3$ are used. Each experiment is performed with multiple independent runs, and we report the average test accuracy and the average reasoning time per sample during training. Further, we enforce timeout thresholds, terminating any experiment with a run that exceeds the limit of six hours. Details on hyperparameter values, hardware and information on baseline configurations are provided in Appendix H.

**Results** The results of the experiments are given in Table 1. For **Q#1**, we first compare DPNL with other PNL systems using exact inference. DeepProbLog achieves similar accuracy to DPNL for $N=1$ and $N=2$. In terms of reasoning time per sample, DPNL outperforms DeepProbLog, with the advantage becoming more noticeable as task complexity increases. Scallop with exact reasoning only succeeds to complete the task $N=1$. For $N \ge 3$, DeepProbLog fails to finish, while DPNL achieves an accuracy of 0.82 for $N=4$ with an average reasoning time per sample of 1.24s. Among all the exact PNL systems considered, DPNL is the only one that scales to $N=4$.

When comparing exact and approximate reasoning systems for **Q#2**, exact reasoning positively impacts accuracy, and this effect becomes more pronounced as the task difficulty increases. For $N=1$, the mean test accuracy of all systems considered is centered around the value 0.95 (except DPLA\*). However, for $N=2$ there is already a noticeable accuracy gap between approximate methods and exact methods such as DeepProbLog and DPNL. This gap becomes even larger for $N=3$, where DPNL achieves a considerably higher accuracy of 0.87 compared to the approximate methods, which do not exceed 0.83. For $N=4$, even the approximate methods such as DPLA\* and Scallop (top-k) fail to complete their runs, while DPNL finishes successfully. The only baseline that finishes for $N=4$ is A-Nesi. All variants of A-Nesi have noticeably lower reasoning times per sample compared to DPNL. However, DPNL has a clear advantage in terms of accuracy. The most accurate A-Nesi model is the explain variant, which achieves an accuracy of 0.65, while DPNL reaches an accuracy of 0.82.

Regarding **Q#3**, ApproxDPNL controls the total runtime by setting a timeout $T$ during the reasoning process per sample. Over all tasks $N = \{1, 2, 3, 4\}$, ApproxDPNL achieves an accuracy competitive to DPNL with exact reasoning. For the approximate reasoning methods, although A-Nesi still has an advantage in terms of run time per sample, ApproxDPNL clearly outperforms A-Nesi in terms of test set accuracy.

# 7 Related Works

There is a large body of work on neuro-symbolic methods aimed at bridging the gap between learning and reasoning. Within this field, PNL systems can be categorized into two groups: those based on exact reasoning and those based on approximate reasoning.

**PNL systems with exact reasoning.** DeepProbLog [Manhaeve et al., 2018], one of the first PNL systems, is often used as a benchmark for accuracy, establishing itself as a foundational framework in PNL. DeepProbLog is based on a probabilistic extension of Prolog called ProbLog [De Raedt et al., 2007] where neural networks are used to estimate probabilities of extensional predicates. ProbLog reduces the probabilistic reasoning task to the PWMC problem. However, two key scalability issues arise. First, the logical provenance is initially computed in DNF, whose size can grow exponentially depending on the task. Even when the clauses of the logical provenance are stored in a prefix tree, its size can grow exponentially with the number of neural ground atoms. Second, solving the PWMC itself is \#P-hard causing blowups in the size of the compiled forms of the logical provenance. Specifically, the size of the Binary Decision Diagram can increase exponentially (as detailed in Appendix F).

NeurASP [Yang et al., 2020] uses a similar approach but relies on Answer Set Programming. It considers the neural network output as the probability distribution over atomic facts in answer set programs.

As noticed in [van Krieken et al., 2023], DeepProbLog, NeurASP and other methods in this line of works [Xu et al., 2018; Ahmed et al., 2022] provide exact inference with probabilistic circuit methods [Choi et al., 2020]. However, they
<table><caption>Table 1: Results on the MNIST-N-SUM task for $N = \{1, 2, 3, 4\}$. We report the average test accuracy (acc), its standard deviation (std) and the average reasoning time per sample (time) in seconds. T/O denotes a computational timeout. The systems with exact reasoning are DPNL, DeepProbLog and Scallop. The systems with approximate reasoning are DPLA*, Scallop with the top-k-proofs semiring and A-Nesi in the variants predict-only (predict), explain and prune. The highest test accuracy numbers and the shortest reasoning times are marked in bold.</caption><thead><tr><th rowspan="2"></th><th colspan="3">N = 1</th><th colspan="3">N = 2</th><th colspan="3">N = 3</th><th colspan="3">N = 4</th></tr><tr><th>Acc</th><th>Std</th><th>Time</th><th>Acc</th><th>Std</th><th>Time</th><th>Acc</th><th>Std</th><th>Time</th><th>Acc</th><th>Std</th><th>Time</th></tr></thead><tbody><tr><td>DPNL</td><td>0.9550</td><td>0.0090</td><td>0.0011</td><td>0.9116</td><td>0.0106</td><td><strong>0.0099</strong></td><td>0.8787</td><td>0.0100</td><td>0.1079</td><td>0.8262</td><td>0.0211</td><td>1.2485</td></tr><tr><td>DeepProbLog</td><td>0.9556</td><td>0.0060</td><td>0.0238</td><td>0.9098</td><td>0.0122</td><td>0.4784</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td></tr><tr><td>Scallop (exact)</td><td>0.9515</td><td>0.0037</td><td>0.0594</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td><td>T/O</td></tr><tr><td>ApproxDPNL (T=0.5)</td><td>0.9552</td><td>0.0100</td><td>0.0038</td><td><strong>0.9147</strong></td><td>0.0165</td><td>0.0163</td><td>0.8842</td><td>0.0079</td><td>0.1694</td><td>0.8334</td><td>0.0231</td><td>0.5381</td></tr><tr><td>ApproxDPNL (T=0.1)</td><td>0.9537</td><td>0.0107</td><td>0.0037</td><td>0.9121</td><td>0.0230</td><td>0.0166</td><td><strong>0.8892</strong></td><td>0.0108</td><td>0.0962</td><td>0.8293</td><td>0.0192</td><td>0.1052</td></tr><tr><td>ApproxDPNL (T=0.075)</td><td>0.9524</td><td>0.0097</td><td>0.0038</td><td>0.9055</td><td>0.0251</td><td>0.0160</td><td>0.8709</td><td>0.0146</td><td>0.0747</td><td><strong>0.8404</strong></td><td>0.0080</td><td>0.0797</td></tr><tr><td>ApproxDPNL (T=0.05)</td><td><strong>0.9570</strong></td><td>0.0086</td><td>0.0034</td><td>0.9060</td><td>0.0184</td><td>0.0162</td><td>0.8616</td><td>0.0126</td><td>0.0512</td><td>0.8354</td><td>0.0165</td><td>0.0535</td></tr><tr><td>DPL A*</td><td>0.8120</td><td>0.1635</td><td>0.0213</td><td>0.8945</td><td>0.0146</td><td>0.0255</td><td>0.8365</td><td>0.0303</td><td>0.1237</td><td>T/O</td><td>T/O</td><td>T/O</td></tr><tr><td>Scallop (top-k)</td><td>0.9512</td><td>0.0076</td><td><strong>0.0009</strong></td><td>0.8257</td><td>0.2060</td><td>0.0244</td><td>0.7523</td><td>0.2612</td><td>0.9521</td><td>T/O</td><td>T/O</td><td>T/O</td></tr><tr><td>A-Nesi (predict)</td><td>0.9435</td><td>0.0071</td><td>0.0328</td><td>0.8849</td><td>0.0142</td><td>0.0265</td><td>0.7796</td><td>0.0074</td><td><strong>0.0170</strong></td><td>0.5929</td><td>0.0394</td><td><strong>0.0221</strong></td></tr><tr><td>A-Nesi (explain)</td><td>0.9510</td><td>0.0066</td><td>0.0348</td><td>0.8952</td><td>0.0071</td><td>0.0213</td><td>0.7707</td><td>0.0179</td><td>0.0219</td><td>0.6584</td><td>0.0239</td><td>0.0268</td></tr><tr><td>A-Nesi (prune)</td><td>0.9489</td><td>0.0070</td><td>0.0251</td><td>0.7866</td><td>0.2225</td><td>0.0195</td><td>0.7706</td><td>0.0214</td><td>0.0206</td><td>0.5905</td><td>0.0339</td><td>0.0245</td></tr></tbody></table>

face significant scaling issues and struggle to handle large problem instances effectively. In comparison, DPNL provides an alternative approach, that does not depend on a reduction that requires binary random variables. Instead, it makes it possible to write oracles with respect to any symbolic function taking general values as input.

**PNL systems with approximate reasoning.** To address the scalability issue, several approaches have been developed to relax the exact reasoning process with approximations.

DeepStochLog [Winters et al., 2022] employs the semantics of stochastic definite clause grammars that achieves better scaling properties thanks to a reduced search space.

Scallop [Huang et al., 2021] computes only the *k* most probable clauses of the logical provenance and performs PWMC on the disjunction of these *k* clauses. This approach thus limits both the size of the formula and the complexity of PWMC to a polynomial function of *k*. DPLA* [Manhaeve et al., 2021] is based on DeepProbLog, but uses only a subset of all proofs. It employs A*-search and heuristics to efficiently search for the best proofs. However, while being polynomial in complexity, these approximations lack guarantees and may fail when the logical provenance's essential structure is not captured by a few clauses.

Other systems relying on similar polynomial approximations have been developed. In particular, A-Nesi [van Krieken et al., 2023] uses deep generative modeling to improve scalability. It employs a neural prediction model that performs approximate inference over PWMC problems and a neural explanation model that computes which world best explains a prediction. A prior belief function is used to sample data corresponding to the knowledge in order to train these models. A-Nesi can also be used in conjunction with a symbolic pruner that sets the probability of certain variables to zero and reduces the search space.

One of the important drawbacks of using approximations is that it undermines one of the primary strengths of PNL: confidence in the model's reliability. Without a guarantee that the approximation is close to the true result, the model's re-

liability may be compromised. Therefore, it makes sense to employ approximations with guarantees, especially during inference when the model is deployed.

In comparison, DPNL makes it possible to obtain more accurate models since it pushes the boundaries of exact inference further. In addition, the extension to ApproxDPNL enables even better scalability through approximations, while providing guarantees on the approximation error.

## 8 Conclusion

We have presented DPNL, a new approach for exact probabilistic reasoning in neurosymbolic learning. The core feature of DPNL lies in its use of oracles, which not only eliminate the need for computing logical provenance formulas, but also allow for significant acceleration of the inference process. Oracles can be automatically generated or customized for further efficiency gains, making DPNL a flexible solution. The correctness and termination of DPNL are formally proved. We furthermore introduced ApproxDPNL that provides approximate reasoning with guarantees on the approximation error in the context of DPNL. Experimental results firstly show that DPNL outperforms existing exact inference methods, handling more complex tasks with greater efficiency and producing more accurate models compared to approximate reasoning techniques. Secondly, ApproxDPNL has shown to further improve the scalability of neurosymbolic programming by incorporating approximations with guarantees on the tolerated error at inference. DPNL and ApproxDPNL thus lay a promising foundation for the development of highly accurate models across a wide range of challenging tasks that involve perception and reasoning.

## References

[Ahmed et al., 2022] Kareem Ahmed, Stefano Teso, Kai-Wei Chang, Guy Van den Broeck, and Antonio Vergari. Semantic probabilistic layers for neuro-symbolic learning, 2022.
[Bacchus et al., 2003] F. Bacchus, S. Dalmao, and T. Pitassi. Algorithms and complexity results for# sat and bayesian inference. In *44th IEEE SFCS*, pages 340–351, 2003.

[Biewald, 2020] Lukas Biewald. Experiment tracking with weights and biases, 2020. Software available from wandb.com.

[Chakraborty et al., 2016] Supratik Chakraborty, Kuldeep S. Meel, and Moshe Y. Vardi. Algorithmic improvements in approximate counting for probabilistic inference: from linear to logarithmic sat calls. In *Proceedings of the Twenty-Fifth International Joint Conference on Artificial Intelligence, IJCAI'16*, page 3569–3576. AAAI Press, 2016.

[Choi et al., 2020] YooJung Choi, Antonio Vergari, and Guy Van den Broeck. Probabilistic circuits: A unifying framework for tractable probabilistic models. *oct 2020*.

[Davis et al., 1962] M. Davis, G. Logemann, and D. Loveland. A machine program for theorem-proving. *Communications of the ACM*, pages 394–397, 1962.

[De Raedt and Kimmig, 2015] Luc De Raedt and Angelika Kimmig. Probabilistic (logic) programming concepts. *Machine Learning*, 100(1):5–47, 2015.

[De Raedt et al., 2007] Luc De Raedt, Angelika Kimmig, and Hannu Toivonen. Problog: A probabilistic prolog and its application in link discovery. In *IJCAI*, pages 2468–2473, 2007.

[De Raedt et al., 2020] Luc De Raedt, Sebastijan Dumančić, Robin Manhaeve, and Giuseppe Marra. From statistical relational to neural-symbolic artificial intelligence. In *IJCAI*, pages 4943–4950, 2020.

[Dubray et al., 2024] Alexandre Dubray, Pierre Schaus, and Siegfried Nijssen. Anytime weighted model counting with approximation guarantees for probabilistic inference. In *30th International Conference on Principles and Practice of Constraint Programming*, pages 10:1–10:16, 2024.

[Huang et al., 2021] Jiani Huang, Ziyang Li, Binghong Chen, Karan Samel, Mayur Naik, Le Song, and Xujie Si. Scallop: From probabilistic deductive databases to scalable differentiable reasoning. In *NeurIPS*, pages 25134–25145, 2021.

[Kingma and Ba, 2017] Diederik P. Kingma and Jimmy Ba. Adam: A method for stochastic optimization, 2017.

[Lecun et al., 1998] Y. Lecun, L. Bottou, Y. Bengio, and P. Haffner. Gradient-based learning applied to document recognition. *Proceedings of the IEEE*, 86(11):2278–2324, 1998.

[Li et al., 2023] Ziyang Li, Jiani Huang, and Mayur Naik. Scallop: A language for neurosymbolic programming. In *ACM on Programming Languages*, pages 1463–1487, 2023.

[Manhaeve et al., 2018] Robin Manhaeve, Sebastijan Dumančić, Angelika Kimmig, Thomas Demeester, and Luc De Raedt. Deepproblog: Neural probabilistic logic programming. In *NeurIPS*, pages 3753–3763, 2018.

[Manhaeve et al., 2021] Robin Manhaeve, Giuseppe Marra, and Luc De Raedt. Approximate Inference for Neural Probabilistic Logic Programming. In *Proceedings of the 18th International Conference on Principles of Knowledge Representation and Reasoning*, pages 475–486, 11 2021.

[Paszke et al., 2019] Adam Paszke, Sam Gross, Francisco Massa, Adam Lerer, James Bradbury, Gregory Chanan, Trevor Killeen, Zeming Lin, Natalia Gimelshein, Luca Antiga, Alban Desmaison, Andreas Köpf, Edward Yang, Zach DeVito, Martin Raison, Alykhan Tejani, Sasank Chilamkurthy, Benoit Steiner, Lu Fang, Junjie Bai, and Soumith Chintala. Pytorch: An imperative style, high-performance deep learning library, 2019.

[van Harmelen and ten Teije, 2019] Frank van Harmelen and Annette ten Teije. A boxology of design patterns for hybrid learning and reasoning systems, 2019.

[van Krieken et al., 2023] Emile van Krieken, Thiviyan Thanapalasingam, Jakub Tomczak, Frank van Harmelen, and Annette Ten Teije. A-nesi: A scalable approximate method for probabilistic neurosymbolic inference. In *NeurIPS*, pages 24586–24609, 2023.

[Vermeulen et al., 2023] Arne Vermeulen, Robin Manhaeve, and Giuseppe Marra. An experimental overview of neural-symbolic systems. In *ILP*, pages 124–138, 2023.

[Winters et al., 2022] Thomas Winters, Giuseppe Marra, Robin Manhaeve, and Luc De Raedt. Deepstochlog: Neural stochastic logic programming. In *AAAI*, pages 10090–10100, 2022.

[Xu et al., 2018] Jingyi Xu, Zilu Zhang, Tal Friedman, Yi-tao Liang, and Guy Van den Broeck. A semantic loss function for deep learning with symbolic knowledge. In Jennifer Dy and Andreas Krause, editors, *Proceedings of the 35th International Conference on Machine Learning*, volume 80 of *Proceedings of Machine Learning Research*, pages 5502–5511. PMLR, 10–15 Jul 2018.

[Yang et al., 2020] Zhun Yang, Adam Ishay, and Joohyung Lee. Neurasp: Embracing neural networks into answer set programming. In Christian Bessiere, editor, *Proceedings of the Twenty-Ninth International Joint Conference on Artificial Intelligence, IJCAI-20*, pages 1755–1762. International Joint Conferences on Artificial Intelligence Organization, 7 2020. Main track.
# A ProbDPLL

## A.1 Notation details

ProbDPLL is described in Algorithm 1. We first refine some notations. $\mathbf{X}$ is a set of (independent) binary (random) variables. A Boolean formula is a function from the set of valuations $\mathcal{F}(\mathbf{X}, \{0, 1\})$ into $\{0, 1\}$. For all $X \in \mathbf{X}$, we also denote by $X$ the Boolean formula such that for all valuation $v \in \mathcal{F}(\mathbf{X}, \{0, 1\})$, $X(v) = 1$ iff $v(X) = 1$. We use logical operators $\wedge, \vee$ and $\neg$ to build Boolean formulas. A Boolean formula $G$ is in CNF when

$$G = \bigwedge_{i=1}^{N} \bigvee_{j=1}^{M_i} L_{i,j}$$

where $N$ is the number of clauses in $G$, $M_i$ is the number of atoms in the $i$-th clause of $G$ and $L_{i,j}$ is a literal $X$ or $\neg X$ with $X \in \mathbf{X}$ for all $i, j$. For all $X \in \mathbf{X}$,

* $G|_{X=0}$ denotes $G$, where every clause containing $\neg X$ is set to true and where $X$ is eliminated from every clause in which it occurred.

* $G|_{X=1}$ denotes $G$, where every clause containing $X$ is set to true and where $\neg X$ is eliminated from every clause in which it occurred.

Consider the following relation $G \equiv (G|_{X=0} \wedge \neg X) \vee (G|_{X=1} \wedge X)$ (see [Davis et al., 1962]). Given a probability distribution $\sigma: \mathbf{X} \to [0, 1]$ of the variables in $\mathbf{X}$, we note $\text{PWMC}_G^\sigma$ the probabilistic weighted model count of $G$ w.r.t. $\sigma$ as defined in Section 2.

## A.2 Vizualisation

Figure 1 illustrates the trace of ProbDPLL($G, \sigma$), see Algorithm 1, for the example formula $G = (A \vee B \vee \neg C) \wedge (\neg A \vee B \vee C) \wedge (B \vee \neg A) \wedge (\neg B \vee C) \wedge (\neg B \vee \neg C)$ as a tree. Each node stands for a recursive call. Since $G$ is not satisfiable, $\text{PWMC}_G^\sigma$ returns 0.

<figure><img src="image_2.png" /><figcaption>Figure 1: Trace of ProbDPLL($G, \sigma$) where $G = (A \vee B \vee \neg C) \wedge (\neg A \vee B \vee C) \wedge (B \vee \neg A) \wedge (\neg B \vee C) \wedge (\neg B \vee \neg C)$.</figcaption></figure>

## A.3 Termination and Correctness

In this Section we proof the termination and correctness of ProbDPLL.

### Theorem 1. For every

* Boolean formula $G = \bigwedge_{i=1}^{N} \bigvee_{j=1}^{M_i} L_{i,j}$ in CNF over a finite set of (independent) binary (random) variables $\mathbf{X}$,

* probability distribution $\sigma: \mathbf{X} \to [0, 1]$,

$\text{ProbDPLL}(G, \sigma)$ terminates and returns $\text{PWMC}_G^\sigma$.

*Proof.* We proceed by induction for $n$ over the number of variables $|\mathbf{X}|$ in $G$. We define the induction hypothesis $H_n$:

For every Boolean formula $G = \bigwedge_{i=1}^{N} \bigvee_{j=1}^{M_i} L_{i,j}$ in CNF over a finite set of (independent) binary (random) variables $\mathbf{X}$ and a probability distribution $\sigma: \mathbf{X} \to [0, 1]$ such that $|\mathbf{X}| \le n$, $\text{ProbDPLL}(G, \sigma)$ terminates and returns $\text{PWMC}_G^\sigma$.

We can then prove $H_n$ for all $n \ge 0$ by induction:

**Base case $H_0$:** Let

* $G = \bigwedge_{i=1}^{N} \bigvee_{j=1}^{M_i} L_{i,j}$ a Boolean formula in CNF over $\mathbf{X}$ a finite set of (independent) binary (random) variables,

* $\sigma: \mathbf{X} \to [0, 1]$,

such that $|\mathbf{X}| \le 0$. Therefore, $G$ is constant because $\mathcal{F}(\emptyset, \{0, 1\})$ only contains one element $v_0$.

Moreover,

$$ \begin{aligned} \text{PWMC}_G^\sigma &:= \sum_{v \in G^{-1}(1)} \prod_{X \in v^{-1}(1)} \sigma(X) \prod_{X \in v^{-1}(0)} (1 - \sigma(X)) \\ &= \sum_{v \in G^{-1}(1)} 1 \\ &= G(v_0). \end{aligned} $$

There are two cases:

* $N = 0$: In this case ProbDPLL($G, \sigma$) terminates and returns 1 because $G$ has no clauses. Since $N = 0$, $G$ is constant and equal to the empty conjunction, so $G$ is equal to 1. It follows ProbDPLL($G, \sigma$) = $\text{PWMC}_G^\sigma = 1$.

* $N > 0$: In this case, ProbDPLL($G, \sigma$) terminates and returns 0 because $G$ contains the empty clause. Since $N > 0$, $G$ is constant and equal to a conjunction of empty clauses, so $G$ is constant and equal to 0. It follows ProbDPLL($G, \sigma$) = $\text{PWMC}_G^\sigma = 0$.

**Induction step:** $H_n \implies H_{n+1}$: Let $n \ge 0$ and suppose that $H_n$ holds. Consider

* a Boolean formula $G = \bigwedge_{i=1}^{N} \bigvee_{j=1}^{M_i} L_{i,j}$ in CNF over a finite set of (independent) binary (random) variables $\mathbf{X}$,

* a probability distribution $\sigma: \mathbf{X} \to [0, 1]$,
such that $|\mathbf{X}| \le n + 1$. If $|\mathbf{X}| < n + 1$, we can directly apply $H_n$ to conclude. So we consider that $|\mathbf{X}| = n + 1$. Let $X \in \mathbf{X}$ be the chosen variable in line 6 of Algorithm 1. We have:

* $G \equiv G_0 \lor G_1$ with $G_0 = (G_{|X=0} \land \neg X)$ and $G_1 = (G_{|X=1} \land X)$.

* Since $G_0$ contains $\neg X$, it follows for all $v \in G_0^{-1}(1)$ $v(X) = 0$.

* Since $G_1$ contains $X$, it follows for all $v \in G_1^{-1}(1)$, $v(X) = 1$.

Therefore, $G_0^{-1}(1)$ and $G_1^{-1}(1)$ are disjoint and form a partition of $G^{-1}(1)$. Since $\text{PWMC}_G^\sigma :=$

$$ \sum_{v \in G^{-1}(1)} \prod_{Y \in v^{-1}(1)} \sigma(Y) \cdot \prod_{Y \in v^{-1}(0)} (1 - \sigma(Y)) $$

by dividing the sum in two parts, we obtain $\text{PWMC}_G^\sigma = \text{PWMC}_{G_0}^\sigma + \text{PWMC}_{G_1}^\sigma$.

We now prove that

* $\text{PWMC}_{G_0}^\sigma = (1 - \sigma(X)) \cdot \text{PWMC}_{G_{|X=0}}^\sigma$ and

* $\text{PWMC}_{G_1}^\sigma = \sigma(X) \cdot \text{PWMC}_{G_{|X=1}}^\sigma$.

Since the two proofs are similar, we only show the proof for $G_1$. For every $v \in G_1^{-1}(1)$, the restriction of $v$ to $\mathbf{X} \setminus \{X\}$, noted as $v|_{\mathbf{X} \setminus \{X\}}$, is a model of $G_{|X=1}$, i.e. $v|_{\mathbf{X} \setminus X} \in G_{|X=1}^{-1}(1)$. And for every $v' \in G_{|X=1}^{-1}(1)$ the extension of $v'$ to $\mathbf{X}$ by setting $v'(X) = 1$ is a model of $G_1$. Indeed, $G_{|X=1}$ is a Boolean formula over $\mathbf{X} \setminus \{X\}$ and $G_1 = (G_{|X=1} \land X)$. From this follows:

$$ \begin{aligned} \text{PWMC}_{G_1}^{\sigma} &:= \sum_{v \in G_1^{-1}(1)} \prod_{Y \in v^{-1}(1)} \sigma(Y) \prod_{Y \in v^{-1}(0)} (1 - \sigma(Y)) \\ &= \sum_{v' \in G_{|X=1}^{-1}(1)} (\sigma(X) \cdot \prod_{Y \in v'^{-1}(1)} \sigma(Y) \cdot \prod_{Y \in v'^{-1}(0)} (1 - \sigma(Y))) \\ &= \sigma(X) \cdot \text{PWMC}_{G_{|X=1}}^{\sigma}. \end{aligned} $$

Thus $\text{PWMC}_{G_1}^\sigma = \sigma(X) \cdot \text{PWMC}_{G_{|X=1}}^\sigma$. Following the same considerations, we derive $\text{PWMC}_{G_0}^\sigma = (1 - \sigma(X)) \cdot \text{PWMC}_{G_{|X=0}}^\sigma$.

Since $G_{|X=1}$ and $G_{|X=0}$ are Boolean formula in CNF over $\mathbf{X} \setminus \{X\}$ and $|\mathbf{X}| = n + 1$, we can apply $H_n$:

* ProbDPLL($G_{|X=1}$, $\sigma$) terminates and returns $\text{PWMC}_{G_{|X=1}}^\sigma$.

* ProbDPLL($G_{|X=0}$, $\sigma$) terminates and returns $\text{PWMC}_{G_{|X=0}}^\sigma$.

In conclusion, we have:

$$ \begin{aligned} \text{PWMC}_{G}^{\sigma} &= \text{PWMC}_{G_0}^{\sigma} + \text{PWMC}_{G_1}^{\sigma} \\ &= (1 - \sigma(X)) \cdot \text{PWMC}_{G_{|X=0}}^{\sigma} + \\ & \qquad \sigma(X) \cdot \text{PWMC}_{G_{|X=1}}^{\sigma} \\ &= (1 - \sigma(X)) \cdot \text{ProbDPLL}(G_{|X=0}, \sigma) + \\ & \qquad \sigma(X) \cdot \text{ProbDPLL}(G_{|X=1}, \sigma) \end{aligned} $$

Thus ProbDPLL($G$, $\sigma$) terminates and returns $\text{PWMC}_G^\sigma$.

□

## B DPNL Termination and Correctness

In this Section we prove the termination and correctness of DPNL.

### Theorem 2. For every

* PL problem $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$,

* PNL system $((V_k)_{1 \le k \le m}, (X_k)_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ for the PL problem,

* valid oracle $O_S$ for $S$,

* $i \in \mathcal{I}, o \in \mathcal{O}$ and $v \in \mathcal{V}$,

$\text{DPNL}(i, o, v, O_S)$ terminates and returns $\mathbb{P}(F(i) = o|w(i, v))$.

*Proof.* We proceed by induction over the number of non-assigned variables $n$ of the valuation $v$, i.e, the size of $v^{-1}(\text{unknown})$. We define $H_n$ as:

For every

* PL problem $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$,

* PNL system $((V_k)_{1 \le k \le m}, (X_k)_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ for this problem,

* valid oracle $O_S: \mathcal{V} \times \mathcal{O} \to \{0, 1, \text{unknown}\}$ for $S$,

* $i \in \mathcal{I}, o \in \mathcal{O}$ and $v \in \mathcal{V}$,

such that $|v^{-1}(\text{unknown})| = n$, $\text{DPNL}(i, o, v, O_S)$ terminates and returns $\mathbb{P}(F(i) = o|w(i, v))$.

We can prove $H_n$ for all $n \ge 0$ by induction:

Base case $H_0$: Let

* $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$ be a PL problem,

* $((V_k)_{1 \le k \le m}, (X_k)_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ be a PNL system for this problem,

* $O_S: \mathcal{V} \times \mathcal{O} \to \{0, 1, \text{unknown}\}$ be a valid oracle for $S$,

* $i \in \mathcal{I}, o \in \mathcal{O}$ and $v \in \mathcal{V}$,

such that $|v^{-1}(\text{unknown})| = 0$.

Since $\forall k \in [1, m], v[k] \ne \text{unknown}$, $v$ is total and thus $\text{tot}(v) = \{v\}$. By definition of $w(i, o)$, $\forall w \in w(i, v)$,

$$ [X_1(i)(w), \dots, X_k(i)(w)] \in \text{tot}(v). $$

Therefore, for all $w \in w(i, v)$ and $k \in [1, m]$,

$$ X_k(i)(w) = v[k]. $$

It follows, for all $w \in w(i, v)$,

$$ \begin{aligned} F(i)(w) &= S(X_1(i)(w), \dots, X_1(i)(w)) \\ &= S(v[1], \dots, v[m]). \end{aligned} $$

There are two cases:
* Case $S(v[1], ..., v[m]) = o$:

In this case, for all $w \in w(i, v)$, $F(i)(w) = o$ thus $\mathbb{P}(F(i) = o|w(i, v)) = 1$.

Moreover, $O_S$ is a valid oracle so that $O_S(v, o) = 1$ because $v$ is total and $S(v[1], ..., v[m]) = o$. It follows that DPNL(i, o, v, $O_S$) terminates and returns 1.

* Case $S(v[1], ..., v[m]) \neq o$:

In this case, for all $w \in w(i, v)$, $F(i)(w) \neq o$ thus $\mathbb{P}(F(i) = o|w(i, v)) = 0$. Moreover, $O_S$ is a valid oracle so that $O_S(v, o) = 0$ because $v$ is total and $s(v[1], ..., v[m]) \neq o$. It follows that DPNL(i, o, v, $O_S$) terminates and returns 0.

Induction step $H_n \implies H_{n+1}$: Let $n \ge 0$ and assume that $H_n$ holds. Let

* $((\Omega, \mathcal{A}, \mathbb{P}), \mathcal{I}, \mathcal{O}, F)$ be a PL problem,

* $((V_k)_{1 \le k \le m}, (X_k)_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ be a PNL system for this PL problem,

* $O_S: \mathcal{V} \times \mathcal{O} \to \{0, 1, \text{unknown}\}$ be a valid oracle for $S$,

* $i \in \mathcal{I}, o \in \mathcal{O}$ and $v \in \mathcal{V}$,

such that $|v^{-1}(\text{unknown})| = n + 1$.

There are 3 cases about $O_S(v, o)$:

* Case $O_S(v, o) = 0$:

In this case, DPNL(i, o, v, $O_S$) terminates and returns 0. We prove that $\mathbb{P}(F(i) = o|w(i, v)) = 0$.

Let $w \in w(i, o)$.

By definition

$$[X_1(i)(w), ..., X_k(i)(w)] \in \text{tot}(v)$$

Moreover, since $O_S$ is a valid oracle and $O_S(v, o) = 0$ for all $v' \in \text{tot}(v)$,

$$S(v'[1], ..., v'[m]) \neq o.$$

Therefore,

$$F(i)(w) = S(X_1(i)(w), ..., X_m(i)(w)) \neq o.$$

It follows that $\mathbb{P}(F(i) = o|w(i, v)) = 0$.

* Case $O_S(v, o) = 1$:

In this case, DPNL(i, o, v, $O_S$) terminates and returns 1.

We prove that $\mathbb{P}(F(i) = o|w(i, v)) = 1$.

Let $w \in w(i, o)$.

By definition

$$[X_1(i)(w), ..., X_m(i)(w)] \in \text{tot}(v).$$

Moreover, since $O_S$ is a valid oracle and $O_S(v, o) = 0$ for all $v' \in \text{tot}(v)$

$$S(v'[1], ..., v'[m]) = o$$

Therefore,

$$F(i)(w) = S(X_1(i)(w), ..., X_k(i)(w)) = o.$$

It follows that $\mathbb{P}(F(i) = o|w(i, v)) = 1$.

* Case $O_S(v, o) = \text{unknown}$:

In this case, DPNL(i, o, v, $O_S$) chooses $k \in [1, m]$ such that $v[k] = \text{unknown}$.

Since $v$ is partial, such a $k$ exists. Indeed, if $v$ was a total valuation, $O_S(v, o)$ would return 0 or 1.

The events $(X_k(i) = y)_{y \in V_k}$ form a partition of $\Omega$. By the law of total probability, $\mathbb{P}(F(i) = o|w(i, v))$ is equal to

$$\Sigma_{y \in V_k} \mathbb{P}(X_k(i) = y|w(i, v)) \cdot \mathbb{P}(F(i) = o|w(i, v) \cap X_k(i) = y).$$

By definition, $w(i, v)$ only depends on the $X_l(i)$ such that $v[l] \neq \text{unknown}$, $v[k] = \text{unknown}$ and the $(X_l(i))_{1 \le l \le m}$ are independent by the definition of the PNL system. Therefore, $w(i, v)$ is independent of $X_k(i)$ for all $y \in V_k$:

$$\mathbb{P}(X_k(i) = y|w(i, v)) = \mathbb{P}(X_k(i) = y).$$

Moreover, by definition of $v_{v[k] \leftarrow y}$ and $w(i, v_{v[k] \leftarrow y})$

$$w(i, v_{v[k] \leftarrow y}) = w(i, v) \cap (X_k(i) = y)$$

because $v[k] = \text{unknown}$.

It holds

$$\mathbb{P}(F(i) = o|w(i, v) \cap X_k(i) = y) = \mathbb{P}(F(i) = o|w(i, v_{v[k] \leftarrow y})),$$

Finally, since $v[k] = \text{unknown}$ and $|v^{-1}(\text{unknown})| = n + 1$,

$$|v_{v[k] \leftarrow y}^{-1}(\text{unknown})| = n$$

Consequently, we can apply $H_n$. For all $y \in V_k$, DPNL(i, o, $v_{v[k] \leftarrow y}$, $O_S$) terminates and returns $\mathbb{P}(F(i) = o|w(i, v_{v[k] \leftarrow y}))$.

We conclude that DPNL terminates and returns $\mathbb{P}(F(i) = o|w(i, v))$.

□

## C Heuristics for choosing k in DPNL

In DPNL, choosing $k$ with its associated variable $X_k$ (see line 6 of Algorithm 2), as for ProbDPLL, determines the number of recursive calls. The objective is to choose $k$ such that the execution tree of DPNL is as small as possible. The heuristic is designed based on the specific oracle. However, when the oracle is complete, the following heuristic is intuitively interesting:

* When the oracle is complete and answers **unknown** this means that there exists a total sub-valuation $v_0$ of $v$ such that $S(v[1], ..., v[m]) \neq o$ and a total sub-valuation $v_1$ of $v$ such that $S(v[1], ..., v[m]) = o$. It is often straightforward to modify an oracle to return these two sub-valuations instead of **unknown** by taking inspiration from the proof of completeness of the oracle, which often explicitly constructs these sub-valuations.

* The heuristic should choose $k$ so that $v[k] = \text{unknown}$ and $v_0[k] \neq \text{unknown}$ or $v_1[k] \neq \text{unknown}$.
## D MNIST-N-SUM Oracle

In MNIST-N-SUM task as defined in example 2, $S$ is the sum algorithm which operates digit by digit, computing the digits of the result from right to left (see Algorithm 5). We can take advantage of this to design an efficient oracle which is able to prune the search space (see Algorithm 6). This oracle computes the result of the sum digit by digit from right to left using $v$, and tests whether it matches the expected result $o$. If it encounters that the result differs, it returns 0, and if it encounters that $v[k]$ is **unknown**, it returns **unknown**. Note that by choosing $k$ in sequential order from right to the left ($N, N + N, N - 1, N + (N - 1), ..., 1, N + 1$) in DPNL, Algorithm 6 acts like a complete oracle.

**Algorithm 5 Addition($d_1, ..., d_{2 \cdot N}$)**

**Input:** $2 \cdot N$ digits representing two numbers $n_1 = d_1...d_N$ and $n_2 = d_{N+1}...d_{N+N}$

**Output:** $r = r_0...r_N = n_1+n_2$

1: $r_0...r_N \leftarrow 0, ..., 0$

2: carry $\leftarrow 0$

3: **for** $i \in [N, N - 1, ..., 1]$ **do**

4: &nbsp;&nbsp;&nbsp;&nbsp; $d \leftarrow carry + d_i + d_{N+i}$

5: &nbsp;&nbsp;&nbsp;&nbsp; $r_i \leftarrow d \mod 10$

6: &nbsp;&nbsp;&nbsp;&nbsp; carry $\leftarrow$ inferior integer part of $d/10$

7: **end for**

8: $r_0 \leftarrow carry$

9: **return** $r = r_0...r_N$

**Algorithm 6 AdditionOracle($v, r$)**

**Input:** A valuation $v$ of size $2 \cdot N$ with values in $[0, 9] \cup \{\text{unknown}\}$, an output result $r \in [0, 2 \cdot 10^N - 1]$

**Output:** A valid oracle output for the symbolic function

1: $r_0...r_N \leftarrow r$ (Extract the base 10 representation of $r$)

2: carry $\leftarrow 0$

3: **for** $i \in [N, N - 1, ..., 1]$ **do**

4: &nbsp;&nbsp;&nbsp;&nbsp; **if** $v[i]$ or $v[N + i]$ are **unknown** return **unknown**

5: &nbsp;&nbsp;&nbsp;&nbsp; $d \leftarrow carry + v[i] + v[N + i]$

6: &nbsp;&nbsp;&nbsp;&nbsp; **if** $d \mod 10 \neq r_i$ **return** 0

7: &nbsp;&nbsp;&nbsp;&nbsp; carry $\leftarrow$ inferior integer part of $d/10$

8: **end for**

9: **if** carry $= r_0$ **return** 1 **else** **return** 0

We now prove that the AdditionOracle in Algorithm 6 is a valid oracle (as defined in Section 3.2) to compute the addition of N-digits as defined in Algorithm 5.

**Theorem 3.** For all $N \ge 0$, AdditionOracle is a valid oracle for Addition.

*Proof.* Let $N \ge 0$. We check each property of a valid oracle for Addition:

* AdditionOracle returns a result in $\{0, 1, \text{unknown}\}$.

* Let $v \in \mathcal{V}$ and $o \in [0, 2 \cdot 10^N]$. Suppose that the AdditionOracle returns 0. It follows that $\log_{10}(o) \le (N + 1)$ and we can extract the base 10 notation of $r_0...r_N$ which

is the output $o$ for $N + 1$ digits. Moreover, the for loop performs the addition algorithm on $v[1]...v[N]$ and $v[N + 1]...v[N + N]$. If the for loop ends, the valuation is total, but the digit of the result does not correspond to this of $o$. It follows that for all $v' \in \text{tot}(v)$, $v' = v$ and Addition($v'[1], ..., v'[N + N]$) $\neq o$. Otherwise, for each digit in the result calculated from right to left, it checks whether it is equal to the corresponding digit in $o$ and fails for $i_0$. This has two implications:

* When the algorithm has reached this point, for all $i \in [N, N - 1, ..., i_0]$, $v[i] \neq \text{unknown}$ and $v[N + i] \neq \text{unknown}$.

* The $i_0$-th digit of the result does not correspond to $r_{i_0}$. It results that the sum differs from $o$, regardless of the values $v[i]$ and $v[N + i]$ with $i \in [i_0 - 1, ..., 1]$.

We define the valuation $v_0$ such that for all $i \in [1, N]$,

$$v_0[i] = \begin{cases} v[i] & \text{if } i \ge i_0, \\ \text{unknown} & \text{else} \end{cases} \text{ and}$$

$$v_0[N + i] = \begin{cases} v[N + i] & \text{if } i \ge i_0, \\ \text{unknown} & \text{else.} \end{cases}$$

Regarding the second point it holds that Addition($v'[1], ..., v'[N + N]$) $\neq o$ for all $v' \in \text{tot}(v_0)$, and, regarding the first point, $\text{tot}(v) \subset \text{tot}(v_0)$. It can be concluded that Addition($v'[1], ..., v'[N + N]$) $\neq o$ for all $v' \in \text{tot}(v)$.

* Let $v \in \mathcal{V}$ and $o \in [0, 2 \cdot 10^N]$. Suppose that the oracle returns 1. Then $\log_{10}(o) \le (N + 1)$ and we can extract the base 10 notation of $r_0...r_N$ for $o$ on $N + 1$ digits. Moreover, for the for loop performs the addition algorithm on $v[1]...v[N]$ and $v[N + 1]...v[N + N]$ and terminates. It results that $v$ is total and all digits of the result correspond to those of $o$, hence for all $v' \in \text{tot}(v)$, $v' = v$ and Addition($v'[1], ..., v'[N + N]$) $= o$

□

## E Logic PNL systems and Oracles

We consider the definitions, notations and algorithms in Section 4.

### E.1 Logic PNL Systems Correctness

**Theorem 4.** Consider a logic $\mathcal{L}$, a domain $\Delta$ and a closed formula $\phi$. We assume the existence of an axiom set $A = \{\alpha_1, ..., \alpha_n\}$ and a set of closed formulas $\phi_1, ..., \phi_m$ that decide $\phi$ and are coherent with respect to $A$. Furthermore, $((\{0, 1\})_{1 \le k \le m}, (B_{\mathcal{L},\Delta}^{\phi_k})_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ is a PNL system for $P_{\mathcal{L},\Delta}^{\phi}$.

*Proof.* Consider a logic $\mathcal{L}$, a domain $\Delta$ and a closed formula $\phi$. Let us consider the following hypothesis (denoted as H).

* The existence of $A = \{\alpha_1, ..., \alpha_n\}$ such that all $\alpha_i$ are true in $\Gamma_{\mathcal{L},\Delta}$, i.e, for all $i \in [1, n]$:

$$(H_i) \cup_{[\cdot]_x \in \mathcal{I}_{\mathcal{L},\Delta}} (B_{\mathcal{L},\Delta}^{\alpha_i}([\cdot]_x) = \emptyset) = \emptyset$$

* The existence of a set of closed formulas $\phi_1, ..., \phi_m$ such that:
(H1) $(P_{\mathcal{L},\Delta}^{\phi_k})_{1\le k\le m}$ are independent logic PL problems

(H2) For all $\phi'_1, ..., \phi'_m$ such that $\phi'_k = \phi_k$ or $\phi'_k = \neg\phi_k$, $A \cup \{\phi'_1, ..., \phi'_m\} \vdash \phi$ or $A \cup \{\phi'_1, ..., \phi'_m\} \vdash \neg\phi$.

(H3) For all $\phi'_1, ..., \phi'_m$ such that $\phi'_k = \phi_k$ or $\phi'_k = \neg\phi_k$, $A \cup \{\phi'_1, ..., \phi'_m\} \nvDash \bot$.

By definition and by the assumptions:

* $\forall k \in [1, m], P_{\mathcal{L},\Delta}^{\phi_k}$ is a PL problem as previously defined

* $(P_{\mathcal{L},\Delta}^{\phi_k})_{1\le k\le m}$ are independent logic PL problems (by H1)

* $\forall k \in [1, m], \hat{p}_k$ is a neural network which solves $P_{\mathcal{L},\Delta}^{\phi_k}$, by definition.

* $S: \{0, 1\}^m \to \{0, 1\}$.

It still remains to prove that $B_{\mathcal{L},\Delta}^{\phi}$ is equal to $S \circ (B_{\mathcal{L},\Delta}^{\phi_1}, ..., B_{\mathcal{L},\Delta}^{\phi_m})$.

Let $[\cdot]_F \in \mathcal{I}_{\mathcal{L},\Delta}$ and $[\cdot]_P \in \Omega_{\mathcal{L},\Delta}$. We note:

* For all $k \in [1, m], x_k = B_{\mathcal{L},\Delta}^{\phi_k}([\cdot]_F)([\cdot]_P)$

* $T = A \cup \{\phi_k|x_k = 1\} \cup \{\neg\phi_k|x_k = 0\}$

In order to prove that $B_{\mathcal{L},\Delta}^{\phi}([\cdot]_F)([\cdot]_P) = S(x_1, ..., x_m)$ we consider two cases:

* If $S(x_1, ..., x_m) = 1$:

By definition of $S$, $T \vdash \phi$.

For all interpretations $I$, if $I$ satisfies $T$, that is:

- For all $i \in [1, n], [\alpha]_I = 1$

- For all $k \in [1, m], [\phi_k]_I = x_k$

it follows by soundness that $[\phi]_I = 1$.

Moreover,

- For all $i \in [1, n], [\alpha]_{(\Delta, [\cdot]_F, [\cdot]_P)} = 1$ (by $H_i$)

- For all $k \in [1, m], x_k = B_{\mathcal{L},\Delta}^{\phi_k}([\cdot]_F)([\cdot]_P) = [\phi_k]_{(\Delta, [\cdot]_F, [\cdot]_P)}$ by definition.

Therefore, $B_{\mathcal{L},\Delta}^{\phi}([\cdot]_F)([\cdot]_P) = [\phi]_{(\Delta, [\cdot]_F, [\cdot]_P)} = 1 = S(x_1, ..., x_m)$.

* If $S(x_1, ..., x_m) = 0$:

By definition of $S$, $T \nvDash \phi$. Since $T \nvDash \phi$, applying H2 results in $T \vdash \neg\phi$.

For all interpretation $I$, if $I$ satisfy $T$, that is:

- For all $i \in [1, n], [\alpha]_I = 1$

- For all $k \in [1, m], [\phi_k]_I = x_k$

it follows by soundness $[\neg\phi]_I = 1$. By applying the semantics of $\neg$ we can derive $[\phi]_I = 1 - [\neg\phi]_I = 0$. Moreover,

- For all $i \in [1, n], [\alpha]_{(\Delta, [\cdot]_F, [\cdot]_P)} = 1$ by ($H_i$)

- For all $k \in [1, m], x_k = B_{\mathcal{L},\Delta}^{\phi_k}([\cdot]_F)([\cdot]_P) = [\phi_k]_{(\Delta, [\cdot]_F, [\cdot]_P)}$ by definition.

It results that $B_{\mathcal{L},\Delta}^{\phi}([\cdot]_F)([\cdot]_P) = [\phi]_{(\Delta, [\cdot]_F, [\cdot]_P)} = 0 = S(x_1, ..., x_m)$.

## E.2 Logic Oracle Validity and Completeness

We now proof validity and completeness of the logic oracle in Algorithm 3.

**Theorem 5.** *The logic oracle is a valid oracle for S.*

*Proof.* We denote $O(v, o)$ the as the result of the logic oracle on $v \in \mathcal{V}$ and $o \in \{0, 1\}$ (in Algorithm 3). We check that Algorithm 3 satisfies every property of a valid oracle for S:

* Since res in the algorithm takes values in $\{1, 0, \text{unknown}\}$, the algorithm returns values in $\{0, 1, \text{unknown}\}$.

* Let $v \in \mathcal{V}$ and $o \in \{0, 1\}$ and assume that $O(v, o) = 0$. Given that $O$ satisfies $O(v, 0) = 1 - O(v, 1)$ and that res in the algorithm is inverted for $o = 0$, we can suppose without loss of generality that $o = 1$. We now want to prove that for all $v' \in \text{tot}(v)$, $S(v'[1], ..., v'[m]) = 0$. Let $v' \in \text{tot}(v)$.

We note $T = A \cup \{\phi_k|v[k] = 1\} \cup \{\neg\phi_k|v[k] = 0\}$ and $T' = A \cup \{\phi_k|v'[k] = 1\} \cup \{\neg\phi_k|v'[k] = 0\}$.

Since $O(v, o) = 0$ and we assumed that $o = 1$, this implies that res has been set to 0 which results in $T \vdash \neg\phi$.

Moreover, $v'$ is a sub-valuation of $v$ so that $T \subset T'$ hence $T' \vdash \neg\phi$ because the proof tree of $\neg\phi$ in $T$ is also a valid proof tree in $T'$.

In addition, by H3, $T' \nvDash \bot$ so that $T' \nvDash \phi$ since $T' \vdash \neg\phi$. By definition of $S$ it follows $S(v'[1], ..., v'[m]) = 0$.

* Let $v \in \mathcal{V}$ and $o \in \{0, 1\}$ and assume that $O(v, o) = 1$. Given that $O$ satisfies $O(v, 0) = 1 - O(v, 1)$ and that res is inverted for $o = 0$, we can suppose without loss of generality that $o = 1$. We thus want to prove that for all $v' \in \text{tot}(v)$, $S(v'[1], ..., v'[m]) = 1$. Let $v' \in \text{tot}(v)$.

We note $T = A \cup \{\phi_k|v[k] = 1\} \cup \{\neg\phi_k|v[k] = 0\}$ and $T' = A \cup \{\phi_k|v'[k] = 1\} \cup \{\neg\phi_k|v'[k] = 0\}$.

Since $O(v, o) = 1$ and we assumed that $o = 1$, this implies that res has been set to 1 which results in $T \vdash \phi$.

Moreover, $v'$ is a sub-valuation of $v$ so that $T \subset T'$ hence $T' \vdash \phi$ because the proof tree of $\phi$ in $T$ is also a valid proof tree in $T'$.

By definition of $S$ it follows $S(v'[1], ..., v'[m]) = 1$.

**Theorem 6.** *The logic oracle describes a complete oracle for S.*

*Proof.* We denote $O(v, o)$ as the result of Algorithm 3 on $v \in \mathcal{V}$ and $o \in \{0, 1\}$. Since we have already proven the validity, it remains to prove that for all $v \in \mathcal{V}$ and $o \in \{0, 1\}$, if $O(v, o) = \text{unknown}$ there exists $v_0, v_1 \in \text{tot}(v)$ such that $S(v_0[1], ..., v_0[m]) = 0$ and $S(v_1[1], ..., v_1[m]) = 1$.

Let $v \in \mathcal{V}$ and $o \in \{0, 1\}$ such that $O(v, o) = \text{unknown}$. We note $T = A \cup \{\phi_k|v[k] = 1\} \cup \{\neg\phi_k|v[k] = 0\}$.

Since $O(v, o) = \text{unknown}$, it means that $T \nvDash \phi$ and $T \nvDash \neg\phi$. We can thus apply the completeness theorem and
consider interpretations $I_0$ and $I_1$ that satisfy $T$ such that $[\phi]_{I_0} = 0$ and $[\phi]_{I_1} = 1$.

For $i \in \{0,1\}$, we note $v_i = [[\phi_1]_{I_i}, ..., [\phi_m]_{I_i}]$ and $T_i = A \cup \{\phi_k|v_i[k] = 1\} \cup \{\neg\phi_k|v_i[k] = 0\}$. Since $I_i$ satisfies $T$, we conclude that $v_i \in \text{tot}(v)$. We now show that $S(v_i[1], ..., v_i[m]) = i$:

* $i=0$:

By contradiction, we assume that $S(v_0[1], ..., v_0[m]) = 1$, which implies $T_0 \vdash \phi$. By definition of $I_0$, $v_0$ and $T_0$, $I_0$ satisfies $T_0$. Given completeness, it results that $[\phi]_{I_0} = 1$, which contradicts $[\phi]_{I_0} = 0$.

It follows that $S(v_0[1], ..., v_0[m]) = 0$.

* $i=1$:

By contradiction, we assume that $S(v_1[1], ..., v_1[m]) = 0$. It means that $T_1 \vdash \phi$ hence $T_1 \vdash \neg\phi$ because we assume that $\phi_1, ..., \phi_m$ decide $\phi$ w.r.t. $A$ when defining the PNL system.

By definition of $I_1$, $v_1$ and $T_1$, $I_1$ satisfies $T_1$. Given completeness, it results that $[\phi]_{I_1} = 1 - [\neg\phi]_{I_1} = 0$, which contradicts $[\phi]_{I_1} = 1$.

It follows that $S(v_1[1], ..., v_1[m]) = 1$.

<figure><img src="image_3.png" /></figure>

# F Application to DeepProbLog

This section discusses how a logic oracle can be used in the context of programs in DeepProbLog.

## F.1 Formal definitions

We start by giving formal the formal definitions of a Problog program [De Raedt et al., 2007] and a DeepProblog program [Manhaeve et al., 2018].

**ProbLog program.** A ProbLog program is defined as a set $T = \{p_1 : c_1, ..., p_n : c_n\}$ of rules $c_k$ without negation annotated by their probability $p_k$. It defines a probability distribution over logic programs $L \subseteq L_T = \{c_1, ..., c_n\}$ as follows:

$$P_T(L) := \prod_{c_k \in L} p_k \cdot \prod_{c_k \notin L} (1 - p_k)$$

For every query $q$ the probability of success of $q$ is defined as:

$$P_T(q) := \sum_{L \subseteq L_T, L \models q} P_T(L)$$

**DeepProbLog program.** For the sake of clarity, we use different notations from [Manhaeve et al., 2018] to define a DeepProbLog program. Formally, a DeepProbLog program over logic $\mathcal{L}$ (as previously defined) is a set $D = T \cup T_N$ where:

* $T = \{c_1, ..., c_n\}$ is a Prolog program over $\mathcal{L}$ without negation,

* $T_N = \{\text{NN}_1 : r_1, ..., \text{NN}_n : r_m\}$ is the set of neural predicates in the program

* $r_k$ is a Prolog rule without negation with $l_k$ ground terms $t_k^1, ..., t_k^{l_k}$

## Algorithm 7 DeepProbLog Oracle

**Input:** $v \in \mathcal{V}, o \in \mathcal{O}$

**Output:** A valid oracle output for the function $S$

1: **if** $R \cup \{r_k|v[k] = 1\} \vdash q$ **then**

2: &nbsp;&nbsp;&nbsp;&nbsp;res $\leftarrow 1$

3: **else if** $R \cup \{r_k|v[k] = 1 \lor v[k] = \text{unknown}\} \vdash q$ **then**

4: &nbsp;&nbsp;&nbsp;&nbsp;res $\leftarrow$ unknown.

5: **else**

6: &nbsp;&nbsp;&nbsp;&nbsp;res $\leftarrow 0$.

7: **end if**

8: **if** res $\neq$ unknown $\land o = 0$ **then**

9: &nbsp;&nbsp;&nbsp;&nbsp;res $\leftarrow 1 - \text{res}$.

10: **end if**

11: **return** res.

* $\text{NN}_k = (m_k, t_k^1, ..., t_k^{l_k})$ where $m_k$ is a neural network that maps the interpretations of the $t_k^1, ..., t_k^{l_k}$ as element of $\Delta$ to the probability of $r_k$.

For all $[\cdot]_\mathcal{F}$, we define the interpretation of $D$ as the Deep-ProbLog program:

$$[D]_{\mathcal{F}} = \{1 : c_k | 1 \le k \le n\} \cup \{m_k([t_k^1]_{\mathcal{F}}, ..., [t_k^{m_k}]_{\mathcal{F}}) : r_k | 1 \le k \le m\},$$

where $[t_k^l]_\mathcal{F}$ is the interpretation of $t_k^l$ in $\Delta$ by recursively applying $[\cdot]_\mathcal{F}$ to the functions of $t_k^l$.

**DeepProbLog PNL system.** Consider $P_{\mathcal{L},\Delta,q}$ and $D = T \cup T_N$ as defined above. If

* $q$ corresponds to a Prolog query,

* $(P_{\mathcal{L},\Delta}^{r_k})_{1 \le k \le m}$ are independent PL problems,

* $c_i \in T$ is true, i.e., $\bigcup_{[\cdot]_\mathcal{F} \in \mathcal{I}_{\mathcal{L},\Delta}} (B_{\mathcal{L},\Delta}^{c_i}([\cdot]_\mathcal{F}) = 0) = \emptyset$.

$D$ corresponds to a PNL system for $P_{\mathcal{L},\Delta}^q$. Indeed, $((\{0,1\})_{1 \le k \le m}, (B_{\mathcal{L},\Delta}^{r_k})_{1 \le k \le m}, (\hat{p}_k)_{1 \le k \le m}, S)$ is a PNL system where

* $\hat{p}_k([\cdot]_\mathcal{F})(1) = 1 - \hat{p}_k([\cdot]_\mathcal{F})(0) = m_k([t_k^1]_\mathcal{F}, ..., [t_k^l]_\mathcal{F})$,

* $S(x_1, ..., x_m) = \begin{cases} 1 & \text{if } T \cup \{r_k|x_k = 1\} \models q, \\ 0 & \text{else} \end{cases}$, .x

**Inference in DeepProbLog.** Given $[\cdot]_P \in \mathcal{I}_{\mathcal{L},\Delta}$, Deep-ProbLog computes $S$ in the form of a logical formula of the $x_1, ..., x_m$ in DNF. Then, it computes $\text{PWMC}_S^\sigma$ with the probability $\sigma(x_k) = m_k([t_k^1]_P, ..., [t_k^{l_k}]_P)$ of $r_k$. By definition of $P_{[D]_\mathcal{F}}$ and $S$, $P_{[D]_\mathcal{F}}(q)$ is equal to $\text{PWMC}_S^\sigma$.

## F.2 Oracle for DeepProbLog

Building on a Prolog solver, Algorithm 7 provides an oracle for the logical provenance. Its validity is based on the fact that the program has no negation. It follows that if we cannot prove $q$ by adding all rules $r_k$ such that $v[k] = \text{unknown}$, then we can directly return 0. If, in addition, the program has no functor, the Prolog solver is polynomial-time. It results that its oracle is also polynomial-time.
**Example 5 (Graph Reachability).** Graph reachability queries are the key ingredients in numerous tasks such as semi-supervised classification in citation networks [Winters et al., 2022]. A probabilistic directed graph consists of the following elements, denoted as follows

* $E = \{e_k | 1 \le k \le N\}$ describes the set of nodes

* $G(e_i, e_j)$ denotes an edge from node $e_i$ to node $e_j$, $(e_i, e_j \in E)$.

* $NN_G(e_i, e_j)$ is the probability that the edge $G(e_i, e_j)$ is present in the graph.

* Reachability is defined as $R(e_i)$. A node $e_i$ is reachable from node $e_1$ if $R(e_i)$ is true.

For this problem, we are interested in the probability that a node $e_N$ is reachable from a starting node $e_1$ and formulate this as query $q = R(e_N)$. The full logical provenance formula for a query $q$ is denoted as $LP_q$. Consider the following DeepProbLog program $D = T_N \cup T$ for graph reachability:

* $T_N = \{NN_{i,j} : G(e_i, e_j) | 1 \le i, j \le N\}$.

* $T = \{R(e_1), R(Y) : -R(X), G(X, Y)\}$.

This program contains neither negation nor functions. Therefore, the DeepProbLog oracle in algorithm 7 is polynomial-time. However,

* for each path $e_1 \to e_{i_1} \to \dots \to e_{i_k} \to e_N$ (with intermediate nodes $e_{i_1}, \dots, e_{i_k}$) the corresponding clause $G(e_1, e_{i_1}) \wedge \dots \wedge G(e_{i_k}, e_N)$ implies $LP_q$,

* and $LP_q$ implies the existence of such a path

Therefore, $LP_q$ is equivalent to the disjunction of clauses that correspond to the path from $e_1$ to $e_N$. Moreover,

* for each path we can potentially remove loops and obtain a path whose corresponding clause is included in the original clause (before removing loops),

* and if we remove a literal from a clause corresponding to a loop-free path it does not correspond to a path, hence it does not imply $LP_q$.

Therefore, $LP_q$ is exactly the disjunction of clauses that correspond to the loop-free path. Choosing a loop-free path from $e_1$ to $e_N$ in a complete graph is equivalent to:

* Choosing the number of intermediate node $i \in [0, |E| - 2]$

* Choosing $i$ nodes from the set of remaining $|E|-2$ nodes in the graph

* Choosing a permutation of these $i$ nodes that correspond to the order in which they are visited

Therefore, $LP_q$ contains exactly $\sum_{i=0}^{|E|-2} \binom{|E|-2}{i} \cdot i!$ clauses. In conclusion, since $|E| - 2 \sim \sqrt{|T|}$, $|LP_q| \sim (\sqrt{|T|})!

# G Modeling Probabilistic Choices

This section describes how probabilistic choices are treated in ProbLog [De Raedt et al., 2007] (and in DeepProbLog

[Manhaeve et al., 2018]), as detailed in [De Raedt and Kimmig, 2015]. Probabilistic choices are categorical random variables with mutually exclusive outcomes. The representation with probabilistic facts is not sufficient as these are considered to be independent. In contrast, the outcomes of probabilistic choices are dependent. ProbLog uses annotated disjunctions (ADs) to model probabilistic choices. Based on ADs, DeepProbLog uses neural annotated disjunctions (nADs), which can be represented by the activated classification function of a neural network.

Probabilistic choices for ADs are transformed to (1) a set of probabilistic facts and (2) deterministic clauses:

* $\tilde{p}_i :: sw_{id}(h_i, v_1, \dots, v_f)$ is the set of probabilistic facts.

* The deterministic clauses are

$h_i : -b_1, \dots, b_m, \text{not } (sw_{id}(h_1, v_1, \dots, v_f)), \dots, \text{not } (sw_{id}(h_{i-1}, v_1, \dots, v_f)), sw_{id}(h_i, v_1, \dots, v_f)$,

where $v_1, \dots, v_f$ are the free variables in the body of the AD and $sw_{id}$ is a switch variable with an identifier for the respective AD.

The probability $\tilde{p}_i$ is defined as

$$ \tilde{p}_i := \begin{cases} p_i \cdot \left(1 - \sum_{j=1}^{i-1} p_j\right)^{-1} & \text{if } p_i > 0 \\ 0 & \text{if } p_i = 0 \end{cases} $$

To recover the original probabilities $p$ from the transformed values $\tilde{p}$, one can initialize $p_1 := \tilde{p}_1$ and iteratively compute $p_i$ for $i = \{2, 3, \dots, n\}$ using the formula

$$ p_i := \tilde{p}_i \cdot \left(1 - \sum_{j=1}^{i-1} p_j\right). $$

**Example 6 (ADs for MNIST Classification).** Consider the example of MNIST classification, where the variables indicating that an image $I$ contains a certain digit in $[0, 9]$ are modeled as probabilistic facts in ProbLog, e.g.

$p_0 :: \text{digit}(I, 0), p_1 :: \text{digit}(I, 1), \dots, p_9 :: \text{digit}(I, 9)$.

This implies that the probability of all digits being simultaneously valid for an image equals $\prod_{i=0}^9 p_i$. In contrast, since the variables are mutually exclusive (and therefore dependent), the probability of this event should be zero. To this end, the digits can be modeled as probabilistic choices with ADs. For the MNIST Classification task, the AD can be represented as follows:

$$ \text{digit}(I, 0) : -sw(\text{digit}(I, 0)) $$

$$ \text{digit}(I, 1) : -\text{not}(sw(\text{digit}(I, 0))), sw(\text{digit}(I, 1)) $$

$$ \text{digit}(I, 2) : -\text{not}(sw(\text{digit}(I, 0))), \dots, sw(\text{digit}(I, 2)) $$

$$ \text{digit}(I, 9) : -\text{not}(sw(\text{digit}(I, 0))), \dots, sw(\text{digit}(I, 9)) $$

# H Experimental Details

## H.1 Hardware

All experiments are executed on an Apple Macbook M1 Max with 64GB RAM.
## H.2 Implementation Details

DPNL is implemented in Python and based on Pytorch [Paszke et al., 2019]. We use Weights and Biases [Biewald, 2020] as experiment tracking tool. The obtained results with DPNL will be made publicly available.<fnref n="1" />

## H.3 Hyperparameters.

The hyperparameters used in the experiments are summarized in Table 2. We keep the hyperparameters constant across the experiments with different numbers of digits. Further, we conduct 10 independent runs with different random seeds for DPNL.

<table><caption>Table 2: Hyperparameters used for DPNL and baselines.</caption><thead><tr><td>Parameter</td><td>value</td></tr></thead><tbody><tr><td># epochs</td><td>1</td></tr><tr><td>Batch size</td><td>2</td></tr><tr><td>Learning rate</td><td>1e-03</td></tr><tr><td>Classifier</td><td>MNIST Classifier</td></tr><tr><td>Loss</td><td>BCE Loss</td></tr></tbody></table>

**MNIST Classifier.** We follow [Manhaeve et al., 2018] and use the following neural network architecture as digit classifier. It consists of two convolutional layers with kernel size 5 and output dimension 6 and 16 (nn.Conv2d(1, 6, 5) and nn.Conv2d(6, 16, 5),) followed by Max pooling and Relu activation (nn.MaxPool2d(2, 2)). A classifier with three linear layers is stacked which have the dimensions 120 and 84 respectively. The output dimension corresponds to the number of digits and equals 10.

## H.4 Baselines

To obtain the results with the baselines, we used the source code from the following publicly available code repositories:

* DeepProbLog & DPLA*: https://github.com/ML-KULeuven/deepproblog

* A-Nesi: https://github.com/HEmile/a-nesi

* Scallop: https://github.com/scallop-lang/scallop

The code with which we ran the baselines and measured the runtimes is available.<fnref n="1" />

**A-Nesi.** We conduct five independent runs with different random seeds for each experiment. Table 3 shows the hyperparameters used for configurations used for the different variants of A-Nesi [van Krieken et al., 2023].

<table><caption>Table 3: Table of hyperparameters of A-Nesi for the variants explain, predict only, and prune.</caption><thead><tr><td>Key</td><td>Explain</td><td>Predict Only</td><td>Prune</td></tr></thead><tbody><tr><td>amtSamples</td><td>600</td><td>600</td><td>600</td></tr><tr><td>KBeliefs</td><td>2500</td><td>2500</td><td>2500</td></tr><tr><td>nrmLr</td><td>0.001</td><td>0.001</td><td>0.001</td></tr><tr><td>perceptionLr</td><td>0.001</td><td>0.001</td><td>0.001</td></tr><tr><td>dirichletLr</td><td>0.01</td><td>0.01</td><td>0.01</td></tr><tr><td>dirichletIters</td><td>50</td><td>50</td><td>50</td></tr><tr><td>dirichletInit</td><td>0.1</td><td>0.1</td><td>0.1</td></tr><tr><td>dirichletL2</td><td>900000</td><td>900000</td><td>900000</td></tr><tr><td>nrmLoss</td><td>mse</td><td>mse</td><td>mse</td></tr><tr><td>hiddenSize</td><td>800</td><td>800</td><td>800</td></tr><tr><td>layers</td><td>3</td><td>3</td><td>3</td></tr><tr><td>prune</td><td>False</td><td>False</td><td>True</td></tr><tr><td>predictOnly</td><td>False</td><td>True</td><td>False</td></tr><tr><td>usePrior</td><td>True</td><td>True</td><td>True</td></tr></tbody></table>

**Scallop.** Since Scallop has been only applied to the MNIST-1-SUM task [Huang et al., 2021], we extend the experiments with Scallop to the MNIST-N-SUM task for N={2,3,4} in order to use it as baseline. The logic programs are given in Listing 1. We use Scallop with exact as well as with approximate reasoning. For approximate reasoning of Scallop we choose the `diff-top-k-proof` semiring with $k=3$, as recommended in [Huang et al., 2021]. To investigate the performance of Scallop with exact reasoning, we choose $k$ in a way that the logic provenance formula is in any case smaller than $k$ and is consequently not pruned. In particular, we choose $k$ for every MNIST-N-SUM task depending on N:

$$k = 2^{2 \cdot 10 \cdot N}.$$

This way, $k$ represents an upper bound for the length of the logical provenance formula.

**DPLA*.** DPLA* employs heuristics to estimate the probability of partial proofs to search for the best proof. Here, we use the geometric mean heuristic [Manhaeve et al., 2021].

```
# N=1
self.scl_ctx.add_rule("summand_one(a) :-
  digit_1(a)")
self.scl_ctx.add_rule("summand_two(a) :-
  digit_2(a)")
# N=2
self.scl_ctx.add_rule("summand_one(10 * a + b
  ) :- digit_1(a), digit_2(b)")
self.scl_ctx.add_rule("summand_two(10 * a + b
  ) :- digit_3(a), digit_4(b)")
# N=3
self.scl_ctx.add_rule("summand_one(10 * 10 *
  a + 10 * b + c) :- digit_1(a), digit_2(b)
  , digit_3(c)")
self.scl_ctx.add_rule("summand_two(10 * 10 *
  a + 10 * b + c) :- digit_4(a), digit_5(b)
  , digit_6(c)")
# N=4
self.scl_ctx.add_rule("summand_one(10 * 10 *
  10 * a + 10 * 10 * b + 10 * c + d) :-
  digit_1(a), digit_2(b), digit_3(c),
  digit_4(d)")
```

<footnote n="1">¹https://anonymous.4open.science/r/dpnll_19FE/</footnote>
```python
self.scl_ctx.add_rule("summand_two(10 * 10 *
                       10 * a + 10 * 10 * b + 10 * c + d) :-
                       digit_5(a), digit_6(b), digit_7(c),
                       digit_8(d)")
```

<caption>Listing 1: Rules used in Scallop for the MNIST-N-SUM task</caption>