Spaces:
Runtime error
Runtime error
Create symbolic_reasoner.py
Browse files
components/symbolic_reasoner.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import datetime
|
| 3 |
+
from typing import Dict, List, Union
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class SymbolicReasoner:
|
| 7 |
+
"""
|
| 8 |
+
A symbolic logic engine for managing rules, facts, and inference.
|
| 9 |
+
Supports basic forward chaining, contradiction checks, and assertions.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.facts: Dict[str, bool] = {}
|
| 14 |
+
self.rules: List[Dict[str, Union[List[str], str]]] = []
|
| 15 |
+
self.inference_log: List[Dict] = []
|
| 16 |
+
logging.info("[SymbolicReasoner] Initialized.")
|
| 17 |
+
|
| 18 |
+
def add_fact(self, statement: str, value: bool = True):
|
| 19 |
+
if statement in self.facts and self.facts[statement] != value:
|
| 20 |
+
logging.warning(f"[SymbolicReasoner] Contradiction detected: {statement}")
|
| 21 |
+
raise ValueError(f"Contradiction: {statement} already known as {self.facts[statement]}")
|
| 22 |
+
self.facts[statement] = value
|
| 23 |
+
logging.info(f"[SymbolicReasoner] Fact added: {statement} = {value}")
|
| 24 |
+
self._evaluate_rules()
|
| 25 |
+
|
| 26 |
+
def add_rule(self, premises: List[str], conclusion: str):
|
| 27 |
+
rule = {"if": premises, "then": conclusion}
|
| 28 |
+
self.rules.append(rule)
|
| 29 |
+
logging.info(f"[SymbolicReasoner] Rule added: IF {premises} THEN {conclusion}")
|
| 30 |
+
self._evaluate_rules()
|
| 31 |
+
|
| 32 |
+
def _evaluate_rules(self):
|
| 33 |
+
inferred = True
|
| 34 |
+
while inferred:
|
| 35 |
+
inferred = False
|
| 36 |
+
for rule in self.rules:
|
| 37 |
+
if all(self.facts.get(prem, False) for prem in rule["if"]):
|
| 38 |
+
conclusion = rule["then"]
|
| 39 |
+
if conclusion not in self.facts:
|
| 40 |
+
self.facts[conclusion] = True
|
| 41 |
+
self.inference_log.append({
|
| 42 |
+
"inferred": conclusion,
|
| 43 |
+
"based_on": rule["if"],
|
| 44 |
+
"timestamp": datetime.datetime.utcnow().isoformat()
|
| 45 |
+
})
|
| 46 |
+
logging.info(f"[SymbolicReasoner] Inferred: {conclusion} from {rule['if']}")
|
| 47 |
+
inferred = True
|
| 48 |
+
|
| 49 |
+
def query(self, statement: str) -> bool:
|
| 50 |
+
result = self.facts.get(statement, False)
|
| 51 |
+
logging.info(f"[SymbolicReasoner] Query: {statement} = {result}")
|
| 52 |
+
return result
|
| 53 |
+
|
| 54 |
+
def explain(self, statement: str) -> List[Dict]:
|
| 55 |
+
return [entry for entry in self.inference_log if entry["inferred"] == statement]
|
| 56 |
+
|
| 57 |
+
def status(self):
|
| 58 |
+
return {
|
| 59 |
+
"facts": self.facts,
|
| 60 |
+
"rules": self.rules,
|
| 61 |
+
"inference_log": self.inference_log
|
| 62 |
+
}
|