Reasoning¶
Differentiable forward chaining over a propositional Horn knowledge base: declare facts
and rules in a PropKB, then run a ForwardChainer to the deductive closure. The
premise conjunction is a product t-norm (a Pi neuron — see Logic), so
chaining is differentiable in the facts.
polyweave.reasoning ¶
Differentiable symbolic reasoning — forward chaining over a propositional KB.
Define facts and Horn-clause rules in a :class:PropKB, then run a
:class:ForwardChainer to the deductive closure. The premise conjunction is a product
t-norm (a Pi neuron, see :mod:polyweave.logic), so chaining is differentiable in the
facts: you can backpropagate through it or embed it as a reasoning layer.
For propositional Horn clauses, forward chaining to the fixpoint is sound and complete for entailment, so a goal-directed backward chainer is not needed to answer "does the KB entail the goal?". A genuinely differentiable backward/Neural-Theorem-Prover-style prover (soft unification + proof trees) is a deliberate future addition, not required here.
ForwardChainer ¶
Bases: Module
Iterate :class:ForwardChainingStep to the fixpoint (deductive closure).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kb
|
PropKB
|
the knowledge base. |
required |
max_steps
|
int
|
maximum chaining iterations (bounds reasoning depth). |
10
|
t_norm
|
str
|
|
'product'
|
tol
|
float
|
stop early once a step changes the facts by less than this. |
1e-06
|
Source code in polyweave/reasoning/forward.py
forward ¶
forward(facts: Tensor, return_history: bool = False) -> 'torch.Tensor | Tuple[torch.Tensor, List[torch.Tensor]]'
Chain facts to the fixpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
facts
|
Tensor
|
|
required |
return_history
|
bool
|
also return the per-step list of fact tensors. |
False
|
Returns:
| Type | Description |
|---|---|
'torch.Tensor | Tuple[torch.Tensor, List[torch.Tensor]]'
|
The closure |
Source code in polyweave/reasoning/forward.py
entails ¶
Does the KB entail goal from facts? Returns (entailed, truth).
Source code in polyweave/reasoning/forward.py
ForwardChainingStep ¶
Bases: Module
One differentiable forward-chaining step over a :class:PropKB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kb
|
PropKB
|
the knowledge base (facts + rules) to compile into frozen masks. |
required |
t_norm
|
str
|
|
'product'
|
Source code in polyweave/reasoning/forward.py
forward ¶
Apply one chaining step. facts: (batch, N) -> (batch, N).
Source code in polyweave/reasoning/forward.py
PropKB ¶
A propositional knowledge base: named facts + Horn-clause rules.
Example
kb = PropKB() kb.add_rule(["raining"], "wet_grass") # raining -> wet_grass kb.add_rule(["wet_grass"], "slippery") # wet_grass -> slippery kb.add_rule(["wet_grass", "sunny"], "rainbow") # a conjunction f0 = kb.initial_facts(["raining"]) # (1, N) truth vector
Source code in polyweave/reasoning/kb.py
add_fact ¶
Register name (idempotent) and return its integer index.
idx ¶
initial_facts ¶
A (1, N) truth vector with 1.0 at each name in true_facts.
add_rule ¶
Add a Horn clause and(premises) -> conclusion.
All names are auto-registered as facts. name is an optional human label
(defaults to a rendered "a, b -> c").
Source code in polyweave/reasoning/kb.py
describe ¶
Print a short summary of facts and rules.
print_facts ¶
Print each fact's truth value, ticking those at/above threshold.