{ "cells": [ { "cell_type": "markdown", "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "source": [ "# Demo: Proof Reconstructor\n", "\n", "This notebook demonstrates the current explanation-reconstruction slice in `rdflib-reasoning-engine`.\n", "It rebuilds a `DirectProof` from engine-native `DerivationRecord` values using `DerivationProofReconstructor`.\n", "\n", "Current scope:\n", "- triple goals only\n", "- recursive reconstruction from derivation logs\n", "- `RuleApplication` nodes for explained conclusions\n", "- `ProofLeaf` fallback for unexplained premises or goals\n", "\n", "This notebook is also the place to compare proof renderings and inspect the raw proof payload." ] }, { "cell_type": "code", "execution_count": 1, "id": "acae54e37e7d407bbb7b55eff062a284", "metadata": {}, "outputs": [], "source": [ "from pprint import pprint\n", "\n", "from rdflib import BNode, Graph, URIRef\n", "from rdflib.namespace import RDF, RDFS\n", "from rdflib_reasoning.engine import (\n", " DerivationProofReconstructor,\n", " DerivationRecord,\n", " RuleId,\n", " TripleFact,\n", ")" ] }, { "cell_type": "code", "execution_count": 2, "id": "9a63283cbaf04dbcab1f6479b197f3a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DirectProof(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), goal=TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Animal'))), proof=RuleApplication(node_kind='rule_application', conclusions=[TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Animal')))], premises=[RuleApplication(node_kind='rule_application', conclusions=[TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Mammal')))], premises=[ProofLeaf(node_kind='leaf', claim=TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Human'))), grounding=[]), ProofLeaf(node_kind='leaf', claim=TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:Human'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.term.URIRef('urn:test:Mammal'))), grounding=[])], rule_id=RuleId(ruleset='rdfs', rule_id='rdfs9'), description=None, derivation=DerivationRecord(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), conclusions=[TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Mammal')))], premises=[TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Human'))), TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:Human'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.term.URIRef('urn:test:Mammal')))], rule_id=RuleId(ruleset='rdfs', rule_id='rdfs9'), bindings=[], depth=1, silent=False), rationale=None, stated_theorem_name=None), ProofLeaf(node_kind='leaf', claim=TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:Mammal'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.term.URIRef('urn:test:Animal'))), grounding=[])], rule_id=RuleId(ruleset='rdfs', rule_id='rdfs9'), description=None, derivation=DerivationRecord(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), conclusions=[TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Animal')))], premises=[TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:alice'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Mammal'))), TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:Mammal'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.term.URIRef('urn:test:Animal')))], rule_id=RuleId(ruleset='rdfs', rule_id='rdfs9'), bindings=[], depth=2, silent=False), rationale=None, stated_theorem_name=None), verdict='proved', notes=None)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context = BNode()\n", "display_graph = Graph()\n", "display_graph.bind(\"ex\", \"urn:test:\")\n", "\n", "alice = URIRef(\"urn:test:alice\")\n", "human = URIRef(\"urn:test:Human\")\n", "mammal = URIRef(\"urn:test:Mammal\")\n", "animal = URIRef(\"urn:test:Animal\")\n", "\n", "goal = TripleFact(context=context, triple=(alice, RDF.type, animal))\n", "intermediate = TripleFact(context=context, triple=(alice, RDF.type, mammal))\n", "premise_a = TripleFact(context=context, triple=(alice, RDF.type, human))\n", "premise_b = TripleFact(context=context, triple=(human, RDFS.subClassOf, mammal))\n", "premise_c = TripleFact(context=context, triple=(mammal, RDFS.subClassOf, animal))\n", "\n", "records = [\n", " DerivationRecord(\n", " context=context,\n", " conclusions=[intermediate],\n", " premises=[premise_a, premise_b],\n", " rule_id=RuleId(ruleset=\"rdfs\", rule_id=\"rdfs9\"),\n", " depth=1,\n", " ),\n", " DerivationRecord(\n", " context=context,\n", " conclusions=[goal],\n", " premises=[intermediate, premise_c],\n", " rule_id=RuleId(ruleset=\"rdfs\", rule_id=\"rdfs9\"),\n", " depth=2,\n", " ),\n", "]\n", "\n", "proof = DerivationProofReconstructor().reconstruct(goal, records)\n", "proof" ] }, { "cell_type": "markdown", "id": "0ef0ab7a0e134f8292ed0f7d830b8bd7", "metadata": {}, "source": [ "## 1. Render the Proof as a Diagram\n", "\n", "Mermaid is useful when you want to see the proof structure and rule nesting at a glance." ] }, { "cell_type": "code", "execution_count": 3, "id": "318f17253ce44b24b6e78978daec36ea", "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "```mermaid\n", "flowchart TD\n", "n2[[\"rdfs:rdfs9\n", "IF (?x, rdfs:subClassOf, ?y) AND (?a, rdf:type, ?x) AND different_terms(?x, ?y)\n", "THEN (?a, rdf:type, ?y)\"]]\n", "n3(\"(ex:alice rdf:type ex:Animal)\")\n", "n3 -->|entailed_by| n2\n", "n4[[\"rdfs:rdfs9\n", "IF (?x, rdfs:subClassOf, ?y) AND (?a, rdf:type, ?x) AND different_terms(?x, ?y)\n", "THEN (?a, rdf:type, ?y)\"]]\n", "n5(\"(ex:alice rdf:type ex:Mammal)\")\n", "n5 -->|entailed_by| n4\n", "n6[\"(ex:alice rdf:type ex:Human)\"]\n", "n4 -->|had_premise| n6\n", "n7[\"(ex:Human rdfs:subClassOf ex:Mammal)\"]\n", "n4 -->|had_premise| n7\n", "n2 -->|had_premise| n5\n", "n8[\"(ex:Mammal rdfs:subClassOf ex:Animal)\"]\n", "n2 -->|had_premise| n8\n", "n1>\"(ex:alice rdf:type ex:Animal)\"]\n", "n1 -->|justified_by| n3\n", "```" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from rdflib_reasoning.engine.proof_notebook import display_proof_mermaid\n", "\n", "display_proof_mermaid(proof, namespace_manager=display_graph.namespace_manager)" ] }, { "cell_type": "markdown", "id": "5d244cb7787d48d8aa83859d37173f53", "metadata": {}, "source": [ "## 2. Render the Proof as Markdown\n", "\n", "The markdown view is a better fit when you want a compact text explanation instead of a graph visualization." ] }, { "cell_type": "code", "execution_count": 4, "id": "1a65b490988b498a90e1d40dc4be972d", "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "## Direct Proof\n", "\n", "- Context: `_:Nb7d1cc6de98a41d691597cb2b41cc883`\n", "- Goal: `(ex:alice rdf:type ex:Animal)`\n", "- Verdict: `proved`\n", "\n", "### Steps\n", "\n", "- Rule Application: `rdfs:rdfs9`\n", " - Conclusions:\n", " - `(ex:alice rdf:type ex:Animal)`\n", " - Premises:\n", " - Rule Application: `rdfs:rdfs9`\n", " - Conclusions:\n", " - `(ex:alice rdf:type ex:Mammal)`\n", " - Premises:\n", " - Leaf: `(ex:alice rdf:type ex:Human)`\n", " - Leaf: `(ex:Human rdfs:subClassOf ex:Mammal)`\n", " - Leaf: `(ex:Mammal rdfs:subClassOf ex:Animal)`" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from rdflib_reasoning.engine.proof_notebook import display_proof_markdown\n", "\n", "display_proof_markdown(proof, namespace_manager=display_graph.namespace_manager)" ] }, { "cell_type": "markdown", "id": "e72f7a86d2084484893ab9ea4779226d", "metadata": {}, "source": [ "## 3. Inspect the Proof Object\n", "\n", "The reconstructed proof remains a typed data model, so you can inspect its structure directly or serialize it for other tooling." ] }, { "cell_type": "code", "execution_count": 5, "id": "8dd0d8092fe74a7c96281538738b07e2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Verdict: proved\n", "Top-level node kind: rule_application\n", "Nested premise node kinds: ['rule_application', 'leaf']\n", "{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'goal': {'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Animal'))},\n", " 'notes': None,\n", " 'proof': {'conclusions': [{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Animal'))}],\n", " 'derivation': {'bindings': [],\n", " 'conclusions': [{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Animal'))}],\n", " 'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'depth': 2,\n", " 'premises': [{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Mammal'))},\n", " {'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:Mammal'),\n", " rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'),\n", " rdflib.term.URIRef('urn:test:Animal'))}],\n", " 'rule_id': {'rule_id': 'rdfs9', 'ruleset': 'rdfs'},\n", " 'silent': False},\n", " 'description': None,\n", " 'node_kind': 'rule_application',\n", " 'premises': [{'conclusions': [{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Mammal'))}],\n", " 'derivation': {'bindings': [],\n", " 'conclusions': [{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Mammal'))}],\n", " 'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'depth': 1,\n", " 'premises': [{'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Human'))},\n", " {'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:Human'),\n", " rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'),\n", " rdflib.term.URIRef('urn:test:Mammal'))}],\n", " 'rule_id': {'rule_id': 'rdfs9',\n", " 'ruleset': 'rdfs'},\n", " 'silent': False},\n", " 'description': None,\n", " 'node_kind': 'rule_application',\n", " 'premises': [{'claim': {'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:alice'),\n", " rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),\n", " rdflib.term.URIRef('urn:test:Human'))},\n", " 'grounding': [],\n", " 'node_kind': 'leaf'},\n", " {'claim': {'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:Human'),\n", " rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'),\n", " rdflib.term.URIRef('urn:test:Mammal'))},\n", " 'grounding': [],\n", " 'node_kind': 'leaf'}],\n", " 'rationale': None,\n", " 'rule_id': {'rule_id': 'rdfs9', 'ruleset': 'rdfs'},\n", " 'stated_theorem_name': None},\n", " {'claim': {'context': rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'),\n", " 'kind': 'triple',\n", " 'triple': (rdflib.term.URIRef('urn:test:Mammal'),\n", " rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'),\n", " rdflib.term.URIRef('urn:test:Animal'))},\n", " 'grounding': [],\n", " 'node_kind': 'leaf'}],\n", " 'rationale': None,\n", " 'rule_id': {'rule_id': 'rdfs9', 'ruleset': 'rdfs'},\n", " 'stated_theorem_name': None},\n", " 'verdict': 'proved'}\n" ] } ], "source": [ "print(\"Verdict:\", proof.verdict)\n", "print(\"Top-level node kind:\", proof.proof.node_kind)\n", "print(\n", " \"Nested premise node kinds:\",\n", " [premise.node_kind for premise in proof.proof.premises],\n", ")\n", "\n", "pprint(proof.model_dump(mode=\"python\"))" ] }, { "cell_type": "markdown", "id": "ba0a8b4f849845d5a9a98a67f934e3df", "metadata": {}, "source": [ "## 4. Missing Derivations Fall Back to Leaves" ] }, { "cell_type": "code", "execution_count": 6, "id": "72eea5119410473aa328ad9291626812", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Verdict: incomplete\n", "Proof node kind: leaf\n" ] }, { "data": { "text/plain": [ "DirectProof(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), goal=TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:bob'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Human'))), proof=ProofLeaf(node_kind='leaf', claim=TripleFact(context=rdflib.term.BNode('Nb7d1cc6de98a41d691597cb2b41cc883'), kind='triple', triple=(rdflib.term.URIRef('urn:test:bob'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:test:Human'))), grounding=[]), verdict='incomplete', notes=None)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# If no derivation record exists for the goal, reconstruction falls back to a leaf.\n", "unexplained_goal = TripleFact(\n", " context=context,\n", " triple=(URIRef(\"urn:test:bob\"), RDF.type, URIRef(\"urn:test:Human\")),\n", ")\n", "\n", "unexplained = DerivationProofReconstructor().reconstruct(unexplained_goal, records)\n", "print(\"Verdict:\", unexplained.verdict)\n", "print(\"Proof node kind:\", unexplained.proof.node_kind)\n", "unexplained" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 5 }