aprender 0.31.2

Next-generation ML framework in pure Rust — `cargo install aprender` for the `apr` CLI
Documentation
<!-- PCU: ml-fundamentals-neuro-symbolic | contract: contracts/apr-page-ml-fundamentals-neuro-symbolic-v1.yaml -->
<!-- Example: cargo run -p aprender-core --example none -->
<!-- Status: enforced -->

# Neuro-Symbolic Reasoning Theory

Neuro-symbolic AI combines neural networks (learning from data) with symbolic AI (logical reasoning) to create systems that can both learn and reason.

## The Symbol Grounding Problem

Traditional AI approaches face a fundamental challenge:

| Approach | Strengths | Weaknesses |
|----------|-----------|------------|
| **Neural Networks** | Learn from data, handle noise, generalize | Black box, need lots of data, can't explain reasoning |
| **Symbolic AI** | Explainable, compositional, data-efficient | Brittle, hard to learn symbols, can't handle noise |

Neuro-symbolic AI bridges this gap by combining both approaches.

## Core Concepts

### 1. Differentiable Logic

Traditional logic operations (AND, OR, NOT) are discrete and non-differentiable. Differentiable logic replaces these with smooth approximations:

```
# Traditional logic (non-differentiable)
AND(x, y) = 1 if x=1 AND y=1, else 0

# Product t-norm (differentiable)
AND(x, y) = x * y

# Godel t-norm
AND(x, y) = min(x, y)

# Lukasiewicz t-norm
AND(x, y) = max(0, x + y - 1)
```

This allows gradient-based optimization through logical operations.

### 2. Logic Tensor Networks

Logic Tensor Networks (LTNs) represent:
- **Constants**: As vectors in embedding space
- **Predicates**: As neural networks
- **Logical formulas**: As differentiable computations

```
# Predicate: "is_mammal(x)"
is_mammal = NeuralNetwork(input_dim=embedding_dim, output_dim=1)

# Logical formula: "mammal(x) AND has_fur(y) -> warm_blooded(x)"
loss = 1 - implies(
    and_(is_mammal(x), has_fur(x)),
    is_warm_blooded(x)
)
```

### 3. Neural Theorem Proving

Use neural networks to guide proof search:

1. Encode facts and rules as embeddings
2. Train a neural network to predict useful proof steps
3. Use the network to prioritize search during inference

### 4. Knowledge Graph Embeddings

Represent entities and relations in continuous vector spaces:

```
# TransE model
score(head, relation, tail) = ||head + relation - tail||

# RotatE model
score(head, relation, tail) = ||head ⊙ relation - tail||
```

## TensorLogic Architecture

Aprender's TensorLogic implements neuro-symbolic reasoning using tensor operations:

```
┌─────────────────────────────────────────────────────────────┐
│                    TensorLogic Engine                        │
├─────────────────────────────────────────────────────────────┤
│  Knowledge Base          │  Inference Engine                │
│  ┌──────────────────┐   │  ┌──────────────────────────┐   │
│  │ Facts (tensors)  │   │  │ Forward Chaining         │   │
│  │ Rules (programs) │   │  │ Backward Chaining        │   │
│  │ Weights (probs)  │   │  │ Probabilistic Inference  │   │
│  └──────────────────┘   │  └──────────────────────────┘   │
├─────────────────────────────────────────────────────────────┤
│  Tensor Operations (SIMD-accelerated via Trueno)            │
│  ┌──────────────────────────────────────────────────────┐  │
│  │ logical_join  │ logical_project │ logical_select    │  │
│  │ logical_aggregate │ matrix multiplication           │  │
│  └──────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘
```

## Mathematical Foundation

### Relational Composition

Given relations R(X,Y) and S(Y,Z) represented as matrices:

```
(R ∘ S)[i,k] = ∨_j (R[i,j] ∧ S[j,k])
```

For Boolean tensors, this is matrix multiplication over the Boolean semiring.
For weighted tensors, use standard matrix multiplication.

### Existential Quantification

Project out a variable using logical OR:

```
(∃Y: R(X,Y))[i] = ∨_j R[i,j]
```

Implemented as max along the projected dimension.

### Universal Quantification

```
(∀Y: R(X,Y))[i] = ∧_j R[i,j]
```

Implemented as min along the quantified dimension.

## Training Neuro-Symbolic Models

### Loss Functions

1. **Satisfaction Loss**: Penalize unsatisfied logical constraints
   ```
   L_sat = Σ_φ (1 - satisfaction(φ))
   ```

2. **Semantic Loss**: Match predictions to logical semantics
   ```
   L_sem = KL(P_neural || P_logical)
   ```

3. **Hybrid Loss**: Combine data loss with logical constraints
   ```
   L = L_data + λ * L_logical
   ```

### Regularization

Logical constraints act as regularization:
- Enforce consistency between predictions
- Reduce need for labeled data
- Improve generalization

## Applications

1. **Knowledge Graph Completion**
   - Infer missing facts in knowledge graphs
   - Example: If (Alice, parent, Bob) and (Bob, parent, Charlie), infer (Alice, grandparent, Charlie)

2. **Question Answering**
   - Multi-hop reasoning over structured data
   - Combine entity linking with logical inference

3. **Program Synthesis**
   - Learn programs from input-output examples
   - Use logical constraints to prune search space

4. **Explainable AI**
   - Generate logical explanations for neural predictions
   - Trace inference steps through proof trees

## Comparison with Pure Neural Approaches

| Aspect | Neural Only | Neuro-Symbolic |
|--------|-------------|----------------|
| **Data efficiency** | Needs large datasets | Can leverage prior knowledge |
| **Explainability** | Black box | Logical traces available |
| **Compositionality** | Limited | Strong (from logic) |
| **Noise handling** | Robust | Depends on formulation |
| **Computational cost** | Efficient (batch) | Can be expensive |

## Further Reading

- Marcus, G. (2020). "The Next Decade in AI: Four Steps Towards Robust Artificial Intelligence"
- De Raedt, L. et al. (2020). "From Statistical Relational to Neural Symbolic Artificial Intelligence"
- Lamb, L. et al. (2020). "Graph Neural Networks Meet Neural-Symbolic Computing: A Survey and Perspective"