mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
* docs(cookbook): add Reasoning module notebook Add cookbook/introduction/23_Reasoning.ipynb covering the reasoning module with verified, executable examples: - Reasoner facade: add_fact / add_rule / forward_chain - one-shot infer_facts(facts, rules) - backward_chain goal proving with premises - re-run-safe rule deduplication (#732) - DatalogReasoner: semi-naive fixpoint evaluation + variable queries - ExplanationGenerator: Explanation / ReasoningPath records The reasoning module currently has no cookbook coverage even though it ships reasoning_usage.md in the package. All API calls and outputs were verified against semantica/reasoning/reasoner.py, datalog_reasoner.py, and explanation_generator.py. Signed-off-by: LeonSGP43 <LeonSGP43@users.noreply.github.com> * docs(cookbook): correct infer_facts semantics description (appends to instance state, no reset) Signed-off-by: LeonSGP43 <leonsgp43@users.noreply.github.com> * docs(cookbook): execute reasoning notebook in Jupyter (real kernel run, stream outputs, execution counts) Signed-off-by: LeonSGP43 <cine.dreamer.one@gmail.com> --------- Signed-off-by: LeonSGP43 <LeonSGP43@users.noreply.github.com> Signed-off-by: LeonSGP43 <leonsgp43@users.noreply.github.com> Signed-off-by: LeonSGP43 <cine.dreamer.one@gmail.com> Co-authored-by: LeonSGP43 <LeonSGP43@users.noreply.github.com>
12 KiB
12 KiB
In [1]:
!pip install -q semanticaIn [2]:
from semantica.reasoning import Reasoner
reasoner = Reasoner()
reasoner.add_fact("Person(John)")
reasoner.add_fact("Person(Jane)")
reasoner.add_rule("IF Person(?x) THEN Human(?x)")
results = reasoner.forward_chain()
print(f"Inferred {len(results)} new facts")
for res in results:
print(f" {res.conclusion} (rule: {res.rule_used.name}, confidence: {res.confidence})")🧠 Semantica - 📊 Current Progress
| Status | Action | Module | Submodule | Progress | ETA | Rate | Time | Extracted |
|---|---|---|---|---|---|---|---|---|
| ✅ | Semantica is reasoning | 🤔 reasoning | Reasoner | 100.0% | - | - | 0.00s | - |
| ✅ | Semantica is reasoning | 🤔 reasoning | DatalogReasoner | 100.0% | - | - | 0.00s | - |
| ✅ | Semantica is reasoning | 🤔 reasoning | ExplanationGenerator | 100.0% | - | - | 0.00s | - |
🔄 Semantica is reasoning: Performing forward chaining 🤔 reasoning Reasoner |░░░░░░░░░░░░░░░| 0.0% ETA: - Rate: - Time: 0.00s Extracted: -
Inferred 2 new facts Human(Jane) (rule: Rule 1, confidence: 1.0) Human(John) (rule: Rule 1, confidence: 1.0)
In [3]:
from semantica.reasoning import Reasoner
derived = Reasoner().infer_facts(
facts=["WorksFor(John, Acme)", "WorksFor(Jane, Acme)"],
rules=["IF WorksFor(?x, ?y) THEN Employee(?x, ?y)"],
)
derivedOut [3]:
['Employee(Jane, Acme)', 'Employee(John, Acme)']
In [4]:
from semantica.reasoning import Reasoner
reasoner = Reasoner()
reasoner.add_fact("Person(John)")
reasoner.add_rule("IF Person(?x) THEN Human(?x)")
proof = reasoner.backward_chain("Human(John)")
print(proof.conclusion if proof else "not provable")
print("premises:", proof.premises if proof else None)Human(John) premises: ['Person(John)']
In [5]:
from semantica.reasoning import Reasoner
reasoner = Reasoner()
reasoner.add_fact("Person(John)")
# Simulate a Jupyter cell re-run: add the same rule twice
r1 = reasoner.add_rule("IF Person(?x) THEN Human(?x)")
r2 = reasoner.add_rule("IF Person(?x) THEN Human(?x)")
len(reasoner.rules)Out [5]:
Skipping duplicate rule (same conditions/conclusion as 'rule_1'): IF Person(?x) THEN Human(?x)
1
In [6]:
from semantica.reasoning import DatalogReasoner
datalog = DatalogReasoner()
datalog.add_fact("parent(tom, mary)")
datalog.add_fact("parent(mary, ann)")
datalog.add_rule("grandparent(X, Z) :- parent(X, Y), parent(Y, Z)")
datalog.derive_all()
datalog.query("grandparent(X, Z)")Out [6]:
[{'X': 'tom', 'Z': 'ann'}]In [7]:
from semantica.reasoning import Reasoner, ExplanationGenerator
reasoner = Reasoner()
reasoner.add_fact("Person(John)")
reasoner.add_rule("IF Person(?x) THEN Human(?x)")
results = reasoner.forward_chain()
gen = ExplanationGenerator()
explanation = gen.generate_explanation(results[0])
path = gen.show_reasoning_path(results[0])
type(explanation).__name__, type(path).__name__Out [7]:
('Explanation', 'ReasoningPath')