Learning Programs as Neural Networks for Generalization and Reuse

Shoichiro Hara, Yuji WatanabearXiv 2026

TL;DR

Standard neural networks often memorize statistical quirks that crumble on larger, unseen inputs. Neural NP4G learns small, sparse neural circuits from examples, mathematically proves that they capture exact logical rules across graphs of any finite size, and freezes them into a growing library of reusable functions—much like a human programmer building software out of verified helper routines.

What Problem Does This Paper Address?

When humans learn a rule—like what makes someone an "uncle"—we infer an abstract, reusable concept. It works whether a family tree has ten people or ten thousand. Neural networks, by contrast, usually approximate rules statistically. Even if a model scores 100% on held-out test sets, how do we know it actually learned the true underlying rule rather than a brittle shortcut? And how can it build complex knowledge on top of simpler concepts without catastrophic forgetting?

Previous work on Network Programming for Generalization (NP4G) tackled program reuse via random search over discrete networks. Hara and Watanabe ask: can we use gradient-based optimization to jointly learn a neural circuit’s wiring and weights, extract its exact logical meaning, formally verify that it works on every valid input in an unbounded domain, and lock it in as a building block for harder tasks?

Methods

Neural NP4G pairs differentiable circuit learning with formal verification:

  • Gated Neural Circuits: Programs are directed networks of numerical nodes with binary selection gates on edges and hidden units. Using straight-through gradient estimators (binary forward pass, smooth backward pass) and pruning, the system simultaneously learns numerical weights and strips away unnecessary wiring.
  • Relational Message Passing: To reason over graphs and family trees, a shared local circuit ($K=2$ ticks) processes aggregated relational primitives—checking existence, universal conditions, capped counts, and relation compositions—across $T$ message-passing steps.
  • Reusable Functions (ADFs): Once accepted, a circuit is frozen and saved as an Automatically Defined Function (ADF). Downstream tasks execute these ADFs first and feed their outputs in as clean Boolean features.
Figure 1: Complete relational computation. Acquired ADF outputs are concatenated with the original inputs once; binary ADF self-pairs are set to false. The target arity selects the unary or binary row. Primitive operators act on the current states at every message tick, and concatenated features enter the shared circuit Fθ. Thresholding h occurs at initialization and immediately after each circuit call; binary states persist between ticks. Training uses the relaxation hα defined in the text.
  • The Detector & Exact Extraction: Instead of trusting test-set accuracy, the authors introduce a "detector" (inspired by formal synthesis and cognitive error-monitoring). Using interval arithmetic, an extractor maps numerical weights into strict logical formulas. It then checks bidirectional query containment (or saturated count tables) to prove exact equivalence to the target specification across all finite structures. In a secondary setup, if a circuit fails, the detector feeds counterexamples back into the training loss.

Datasets

  • Relational Reasoning Suite: Adapts 10 task categories from the Neural Logic Machines (NLM) benchmark—5 family tree tasks (e.g., Has a sister, Uncle, Maternal great-uncle) and 5 directed, loop-free graph tasks with vertex colors (e.g., Adjacent to red, Outdegree two, Reachability within 6 edges)—plus 4 auxiliary tasks (Sister, Parent, Brother, 2-edge reachability) to supply reusable dependencies. Training uses synthetic structures of 10, 20, 30, and 40 objects; predictive extrapolation tests up to 500 objects. (Unlike NLM's undirected spatial graphs, edges here are sampled independently on ordered pairs.)
  • Detector Feedback Micro-benchmark: Synthetic directed loop-free graphs (trained on 3-node graphs) testing edge reversal and edge symmetrization across four detector types.

Key Insights and Ideas

  • Zero test error can be an illusion: Several circuits achieved zero validation and test errors, yet the formal extractor rejected them. Why? Two outdegree circuits quietly relied on a "non-red vertex existing somewhere in the graph"—a fluke that always held in random test graphs, but instantly failed when audited on an all-red graph! Formal extraction catches silent shortcuts that finite test sets miss.
  • Creative, verifiable composition: To learn 6-edge reachability (Reach6), the target specification composed Reach4 followed by Reach2. Neural NP4G wired them in the exact opposite order (Reach2 then Reach4), and the equivalence checker mathematically proved that this alternative algorithm computes the exact same relation on every finite graph.
  • Counterexample feedback supercharges learning, while detector strength guards the gate: Feeding counterexamples back into gradient training substantially boosts verified program acquisition. Meanwhile, weaker detectors (like checking finite 4-node graphs) can sometimes guide optimization faster, though at the risk of accepting subtly flawed rules.

Key Results

  • Full Library Coverage: Selecting the first verified circuit out of 10 random seeds per task yielded a complete, verified library covering all 14/14 tasks.
  • Acquisition Rates: With upstream ADFs fixed, verified acquisition succeeded in 74/100 primary attempts and 22/40 auxiliary attempts. Every single one of the 37 accepted reuse-dependent circuits genuinely relied on its mandatory ADFs (disabling any ADF increased errors on independent check data).
Table 1: Acquisition outcomes, ten seeds per task. Acquired, counterexample, and inconclusive partition each row. Zero-error counts selected checkpoints with no validation or test errors. Blocks are primary family, primary graph, and auxiliary tasks. Per-task 95% intervals and complete diagnostics are in Appendix G.3.
  • Counterexample Replay: In the 160-run detector study, returning exact counterexamples to training raised verified acquisition from 5/10 to 8/10 on edge reversal, and from 2/10 to 5/10 on edge symmetrization.

Limitations and Future Work

  • Oracle Assumptions: In the main study, the detector already knows the target specification—meaning the learner is synthesizing a reusable neural implementation from examples rather than discovering an unknown scientific law from scratch.
  • Hand-crafted Curriculum & Shallow Local Depth: Task order, arities, and mandatory dependencies are prescribed in advance, and local circuits are fixed at $K=2$ ticks (effectively one sigmoid hidden layer with an affine skip), leaving deeper local computations untested.
  • Exact-Real vs. Floating-Point Semantics: Proofs hold under exact real arithmetic on saved binary parameters; extending guarantees to standard PyTorch/CUDA execution requires bounding floating-point rounding near decision thresholds. Additionally, extraction faces strict memory (6 GB) and time (1,800s) caps, leaving some valid circuits unproved. Future work includes ablations on gating/pruning, independent end-to-end library builds from scratch, and direct comparisons with discrete neuro-symbolic systems under matched criteria.

Summary updated

BibTeX
@misc{hara2026learning,
  author = {Shoichiro Hara and Yuji Watanabe},
  title = {{Learning Programs as Neural Networks for Generalization and Reuse}},
  year = {2026},
  eprint = {2611.39825},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.39825},
}
Figure