Chapter 6: Neurosymbolic Architectures & Algorithms

The previous chapter explored the neurosymbolic paradigm as a design pattern: neural nets for perception, symbolic engines for reasoning, and a smooth interface that connects them. This chapter tours four concrete architectures that realize that pattern, each with a working example written in Racket. These architectures—Logic Tensor Networks (LTN), Logical Neural Networks (LNN), NeurASP, and Differentiable Inductive Logic Programming (∂ILP)—share the same goal but differ in the trade-offs they make between expressivity, training stability, and interpretability.

All code examples are in source-code/06-neurosymbolic-architectures/ and are written in plain Racket so that every step is visible. Shared dot, sigmoid, and t-norm helpers are imported from source-code/utils/math.rkt and utils/logic.rkt; each file now has an explicit provide and (module+ main ...)/(module+ test ...) so require is silent. None requires an external solver.

The figure below draws all four in one visual language. Blue boxes hold learned parameters, green boxes hold symbolic structure, gray boxes hold data or a score, and dashed arrows show where gradients flow. Keep it in view as you read: the four systems differ mainly in where the green boxes sit and whether the gradient passes through them.

Four neurosymbolic architectures in one visual language
Figure 4. Four neurosymbolic architectures in one visual language

Logic Tensor Networks (LTN)

Logic Tensor Networks (LTN) treat logical formulas as computations over tensors. Constants in a first-order language are mapped to embedding vectors, predicates are neural nets that map tuples of embeddings to truth degrees in [0,1], and logical connectives are smooth relaxations built from t-norms. The key insight is that, with these choices, logical inference becomes differentiable: one can adjust the parameters of the predicates—and even the constants themselves—via gradient descent to maximize the degree of satisfaction of a knowledge base.

The example 01-logic-tensor-network.rkt implements a minimal LTN. Constants are structs holding a name and an embedding:

1 (struct const (name embedding) #:transparent)
2 (define alice (const 'alice '(0.9 0.1)))
3 (define bob   (const 'bob   '(0.7 0.6)))
4 (define carol (const 'carol '(0.4 0.8)))

Predicates are structs with a mutable weight vector. Evaluating a predicate takes the dot product of its weights with the concatenated embeddings—dot and sigmoid are imported from utils/math.rkt—and checks that the concatenated length matches the weight length:

1 (require "../utils/math.rkt" "../utils/logic.rkt")
2 (struct pred (name [weights #:mutable]) #:transparent)
3 (define (eval-pred p args)
4   (define embs (apply append (map eval-const args)))
5   (unless (= (length embs) (length (pred-weights p)))
6     (raise-argument-error 'eval-pred "weights length vs embedding length" 0 p args))
7   (sigmoid (dot (pred-weights p) embs)))
8 ;; t-and, t-impl are imported from utils/logic.rkt

We encode the rule ∀x,y (friend(x,y) ∧ likes(x,sports) → likes(y,sports)) by first writing a per-grounding satisfaction function:

1 (define (rule-satisfaction x y)
2   (let* ((fxy (eval-pred friend (list x y)))
3          (lsx (eval-pred likes-sports (list x)))
4          (lsy (eval-pred likes-sports (list y)))
5          (premise (t-and fxy lsx)))
6     (t-impl premise lsy)))

and then generalizing it to the whole domain with a product over all pairs:

1 (define (kb-satisfaction)
2   (t-and-list
3    (for*/list ((x (list alice bob carol))
4                (y (list alice bob carol)))
5      (rule-satisfaction x y))))

Because every step is differentiable, we can perform gradient ascent on the parameters to maximize the KB satisfaction. The full gradient code from 01-logic-tensor-network.rkt — a central finite difference over each weight of a predicate, carefully restoring the original weights before returning:

 1 (define (numeric-grad p)
 2   (define orig (pred-weights p))
 3   (for/list ([i (in-range (length orig))])
 4     (define w+ (list-set orig i (+ (list-ref orig i) h)))
 5     (define w- (list-set orig i (- (list-ref orig i) h)))
 6     (set-pred-weights! p w+)
 7     (define sat+ (kb-satisfaction))
 8     (set-pred-weights! p w-)
 9     (define sat- (kb-satisfaction))
10     (set-pred-weights! p orig)
11     (/ (- sat+ sat-) (* 2 h))))

The output shows the KB satisfaction rise after a hundred ascent steps (from about 0.148 to 0.249), with the predicate weights moving in the direction preferred by the rule. LTNs have been used for knowledge‑base completion, semantic image interpretation, and any setting where a structured set of logical constraints must be reconciled with noisy learned models.

Logical Neural Networks (LNN)

Logical Neural Networks (LNN) take a complementary stance: the structure of a logical formula is mapped directly onto a neural network. Each neuron computes a subformula’s truth value, and the connections mirror the formula’s parse tree. Input neurons hold degrees of belief for atomic facts; internal neurons implement connectives such as AND and IMPL; and the network output is the degree of truth of the full formula.

The example 02-logical-neural-network.rkt defines neurons as a small struct type:

1 (struct neuron (kind [inputs #:mutable] [value #:mutable]) #:transparent)

where kind is either 'input, 'and, or 'impl. Evaluation is now split into a pure walk that does not mutate and a caching wrapper, so neuron-grad (which does finite differences on q) does not leave stale impl/and values:

1 (require "../utils/logic.rkt")
2 (define (eval-neuron-pure n)
3   (match (neuron-kind n)
4     ['input (neuron-value n)]
5     ['and (apply * (map eval-neuron-pure (neuron-inputs n)))]
6     ['impl (t-impl (eval-neuron-pure (first (neuron-inputs n)))
7                    (eval-neuron-pure (second (neuron-inputs n))))]))
8 (define (eval-neuron n) (define v (eval-neuron-pure n)) (set-neuron-value! n v) v)

Building the network for a simple theory P AND (P -> Q) looks like:

1 (define p (neuron 'input '() 0.9))
2 (define q (neuron 'input '() 0.2))
3 (define impl-node (neuron 'impl (list p q) #f))
4 (define net (neuron 'and (list p impl-node) #f))

To make learning concrete, the implementation also computes a finite‑difference gradient of the network output with respect to the input neuron q and performs a short gradient‑ascent loop. The full neuron-grad from 02-logical-neural-network.rkt perturbs the target’s value symmetrically and restores it afterwards — which is exactly why evaluation above is split into a pure walk; the gradient would otherwise read stale cached values:

1 (define (neuron-grad net target-neuron eps)
2   (define orig (neuron-value target-neuron))
3   (set-neuron-value! target-neuron (min 1.0 (+ orig eps)))
4   (define hi (eval-neuron-pure net))
5   (set-neuron-value! target-neuron (max 0.0 (- orig eps)))
6   (define lo (eval-neuron-pure net))
7   (set-neuron-value! target-neuron orig)
8   (/ (- hi lo) (* 2 eps)))

After a few dozen steps the value of q—initially low—rises toward 1.0, and the overall network output rises from 0.252 to 0.9. This is precisely the classical modus‑ponens behavior, but produced by gradient descent: the implication neuron penalizes states where P is high and Q is low, so the overall AND score improves only when Q goes up. LNNs thus allow both forward inference (deriving consequences) and backward inference (abducing likely premises) in the same network, and their learned parameters can be read off as the relative importance or confidence attached to each logical connection.

NeurASP: Integrating Answer Set Programming with Neural Networks

NeurASP bridges the declarative power of Answer Set Programming (ASP) with the pattern‑recognition power of neural nets. In a NeurASP system, a neural network acts as an oracle that produces probabilities over atomic facts (e.g., object classes in an image). These probabilities are then embedded in an ASP program, whose stable models correspond to solutions that satisfy symbolic constraints. Crucially, the training objective—maximize the probability of the correct answer set—gradients flow through the ASP solving step back into the neural network, so the network learns to produce perceptions that are consistent with the ASP rules.

The example 03-neurasp.rkt imports dot/sigmoid from utils/math.rkt and t-impl from utils/logic.rkt. The neural component is:

1 (require "../utils/math.rkt" "../utils/logic.rkt")
2 (define (nn-scores img ws) (map (lambda (w) (sigmoid (dot img w))) ws))

The ASP component is represented as a set of soft rules—each rule is a clause that takes a hash of atom‑to‑probability mappings and returns a satisfaction degree. For example:

 1 (define (rule-sat probs)
 2   (let* ((c-red  (hash-ref probs 'red))
 3          (c-blue (hash-ref probs 'blue))
 4          (s-circ (hash-ref probs 'circle))
 5          (s-sq   (hash-ref probs 'square))
 6          (c-flag (hash-ref probs 'flag)))
 7     (list
 8      (t-impl (* c-red s-circ) c-flag)
 9      (t-impl (* c-blue s-sq)   c-flag)
10      (t-impl (* c-red s-sq)   (- 1.0 c-flag)) ; red & square -> NOT flag
11      (t-impl (* c-blue s-circ) (- 1.0 c-flag)))))

The training loop is where the example earned a rewrite. The draft maximized rule satisfaction alone — the product of those four implications — but that objective rewards any world where the rules hold, including worlds with low flag (rules three and four are satisfied when flag is near 0). The reader watched P(flag) fall even as “satisfaction” rose: correct arithmetic, wrong lesson. Real NeurASP maximizes the probability of the observed answer given perception, so the objective must include a data term. Our image is a red circle observed with label flag = 1, and the objective becomes:

1 (define (objective ws)
2   (define probs (hash-from-weights ws))
3   (* (total-satisfaction probs)          ; prior: rules hold
4      (hash-ref probs 'flag)))            ; data: P(flag = observed label)

A finite‑difference gradient over the NN weights followed by gradient ascent now pushes the way the rules and the label agree it should:

1 NeurASP toy: objective = rule satisfaction x P(label):
2   Rule satisfaction initial: 0.551  final: 0.544
3   P(flag) initial: 0.454
4   P(flag) final  : 0.492
5   Objective initial: 0.250  final: 0.268

Rule satisfaction dips slightly but the data term dominates: ascent strengthens the circle-detecting neuron for this red‑circle image, and P(flag) rises. Flip the label to flag = 0 and the same objective pushes P(flag) down — the direction of learning comes from the data, not from the rules alone. That is exactly the NeurASP division of labor: the ASP rules shape the space of worlds the network may believe in, and the observation selects among them.

NeurASP shows how to reuse mature symbolic solvers (clingo) for structured prediction while still learning the perceptual front‑end. The key takeaway is that the symbolic layer does not need to be rewritten as a differentiable layer; rather, its output (a stable‑model probability) is differentiable, and that gradient path — combined with a data term anchoring outputs to labels — is enough to couple learning and reasoning.

Differentiable Inductive Logic Programming (∂ILP)

Inductive Logic Programming (ILP) learns logic programs from examples, but classical ILP relies on symbolic search over a discrete space of clauses and is not differentiable. Differentiable ILP (∂ILP) relaxes that discreteness: instead of committing to a single rule, it learns a soft selection over a set of candidate rules, with the selection expressed as a softmax over learnable weights. Inference then computes a weighted combination of candidate rule applications, a process that is end‑to‑end differentiable.

The example 04-dilp.rkt factors the repeated for/or chain into a chain helper, uses softmax explicitly, and derives the constants that intermediate variables range over from the database itself (db-constants), so adding facts extends the chain domain without editing the evaluator. The three candidates are:

1 (define (eval-rule r db x z)
2   (define (chain rel1 rel2)
3     (for/or ([y (db-constants db)]) (and (holds? db rel1 x y) (holds? db rel2 y z))))
4   (case r [(R1) (if (chain 'parent 'parent) 1.0 0.0)]
5           [(R2) (if (chain 'parent 'sibling) 1.0 0.0)]
6           [(R3) (if (chain 'sibling 'parent) 1.0 0.0)]))

Given rule weights, inference computes a softmax over the weights and produces a weighted sum of the candidate scores:

1 (define (infer-grandparent weights db x z)
2   (let* ((scores (rule-weights->scores weights db x z))
3          (exps   (map (lambda (w) (exp w)) weights))
4          (sumexps (apply + exps))
5          (softweights (map (lambda (e) (/ e sumexps)) exps)))
6     (apply + (map * softweights scores))))

Training minimizes a mean‑squared‑error loss between the inferred score and the target truth value for positive and negative examples. The gradient is again computed numerically, and gradient descent drives the rule weights until the softmax puts nearly all its mass on the correct rule R1. The final softmax, shown in the output, sharply favors R1 (~0.896) over the alternatives, and inference on held‑out examples behaves accordingly.

∂ILP illustrates how to combine the interpretability of ILP with the flexibility of gradient descent. Because the candidate rules are explicit and readable, the resulting model remains auditable: you can inspect the learned softmax and see which logical rule the system has come to rely on. At the same time, the relaxation allows the system to learn from noisy examples and to be trained jointly with neural components when the clauses themselves have learned parameters.

Running the Examples

All four are explicit-provide modules with (module+ main ...)/(module+ test ...); require is silent:

1 cd source-code/06-neurosymbolic-architectures
2 racket 01-logic-tensor-network.rkt
3 racket 02-logical-neural-network.rkt
4 racket 03-neurasp.rkt
5 racket 04-dilp.rkt
6 raco test .

Each output narrates the architectural idea in miniature: the LTN prints its KB‑satisfaction before and after ascent; the LNN prints its network output and how a belief neuron is updated; NeurASP prints the satisfaction of its soft rules and the probability change of a derived atom; and ∂ILP prints its learning loss and the final softmax over rule templates.

Wrap Up

These four architectures demonstrate different points on the neurosymbolic design space. LTNs and LNNs map logic directly onto tensor computations—LTNs at the level of embeddings and t‑norms, LNNs at the level of neurons that compute connectives—making them naturally end‑to‑end differentiable. NeurASP keeps the symbolic component (ASP) largely intact, using a probabilistic interface to couple it with a neural front‑end. ∂ILP keeps the symbolic form of clauses but softens the selection over them, preserving interpretability while permitting gradient training. All four show that the neurosymbolic paradigm, introduced in the previous chapter, can be instantiated in concrete, trained systems that combine perception, reasoning, and learning in a single architecture.

As we move to advanced topics in the next part of this book, these architectures form a practical toolbox: you can choose the one whose balance of structure, differentiability, and scalability best matches a given problem, and you can often compose them (for example, using an LNN to score candidate formulas that are then filtered by an ASP solver). The examples in this directory are intentionally minimal, but each captures the core mechanic so you can lift it into larger systems or compare it with published implementations.