{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Pyswip-notebook usage examples" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'X': 'Python', 'Y': 130}]\n" ] } ], "source": [ "from pyswip import Prolog, registerForeign, Atom\$n", "\n", "\n", "def atom_checksum(*a):\n", " if isinstance(a[0], Atom):\n", " r = sum(ord(c)&0xFF for c in str(a[0]))\n", " a[1].value = r&0xFF\$n", " return True\$n", " else:\n", " return False\$n", "\n", "p = Prolog()\n", "registerForeign(atom_checksum, arity=2)\n", "print(list(p.query(\"X='Python', atom_checksum(X, Y)\", catcherrors=False)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Spawn the interpreter instance and add some initial facts. SWI-Prolog will be started automatically in the background.\n", "\n", "The facts can be loaded directly from a string as well.\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from pyswip_notebook import IsolatedProlog\$n", "\n", "prolog = IsolatedProlog()\n", "\n", "\n", "facts = \"\"\"\n", "parent(cronus,hestia).\n", "parent(cronus,pluto).\n", "parent(cronus,poseidon).\n", "parent(cronus,zeus).\n", "parent(cronus,hera).\n", "parent(cronus,demeter).\n", "parent(rhea, hestia).\n", "parent(rhea, pluto).\n", "parent(rhea, poseidon).\n", "parent(rhea, zeus).\n", "parent(rhea, hera).\n", "parent(rhea, demeter).\n", "\n", "parent(zeus, athena).\n", "\n", "parent(zeus, ares).\n", "parent(zeus, hebe).\n", "parent(zeus, hephaestus).\n", "parent(hera, ares).\n", "parent(hera, hebe).\n", "parent(hera, hephaestus).\n", "\n", "parent(zeus, persephone).\n", "parent(demeter, persephone).\n", "\n", "\n", "male(cronus).\n", "male(pluto).\n", "male(poseidon).\n", "male(zeus).\n", "male(ares).\n", "male(hephaestus).\n", "female(rhea).\n", "female(hestia).\n", "female(hera).\n", "female(demeter).\n", "female(athena).\n", "female(hebe).\n", "female(persephone).\n", "\"\"\"\n", "\n", "prolog.consult(facts, file=False) \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print all facts in the knowledge base." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "male(cronus).\n", "male(pluto).\n", "male(poseidon).\n", "male(zeus).\n", "male(ares).\n", "male(hephaestus).\n", "\n", "female(rhea).\n", "female(hestia).\n", "female(hera).\n", "female(demeter).\n", "female(athena).\n", "female(hebe).\n", "female(persephone).\n", "\n", "parent(cronus, hestia).\n", "parent(cronus, pluto).\n", "parent(cronus, poseidon).\n", "parent(cronus, zeus).\n", "parent(cronus, hera).\n", "parent(cronus, demeter).\n", "parent(rhea, hestia).\n", "parent(rhea, pluto).\n", "parent(rhea, poseidon).\n", "parent(rhea, zeus).\n", "parent(rhea, hera).\n", "parent(rhea, demeter).\n", "parent(zeus, athena).\n", "parent(zeus, ares).\n", "parent(zeus, hebe).\n", "parent(zeus, hephaestus).\n", "parent(hera, ares).\n", "parent(hera, hebe).\n", "parent(hera, hephaestus).\n", "parent(zeus, persephone).\n", "parent(demeter, persephone).\n", "{}\n" ] } ], "source": [ "# \n", "for l in prolog.query(\"listing\"):\n", " print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we add a few more complext rules." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "prolog.assertz(\"isFather(X, Y) :- male(X), parent(X, Y)\")\n", "\n", "prolog.assertz(\"isMother(X, Y) :- female(X), parent(X, Y)\")\n", "\n", "prolog.assertz(\"isDaughter(X, Y) :- female(X), parent(Y, X)\")\n", "\n", "prolog.assertz(\"isSon(X, Y) :- male(X), parent(Y, X)\")\n", "\n", "prolog.assertz(\"isAncestor(X, Y) :- parent(X, Y)\")\n", "\n", "prolog.assertz(\"isAncestor(X, Y) :- parent(X, T), parent(T, Y)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can query the knowledge base." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\$n" ] } ], "source": [ "# \n", "res = list(prolog.query(\"isAncestor(rhea, persephone)\"))\n", "print(\"false\" if len(res) == 0 else \"true\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Y': 'athena'}\n", "{'Y': 'ares'}\n", "{'Y': 'hebe'}\n", "{'Y': 'hephaestus'}\n", "{'Y': 'persephone'}\n" ] } ], "source": [ "# a more complext query with a variable\$n", "for res in prolog.query(\"isFather(zeus, Y)\"):\n", " print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple instances of `IsolatedProlog` do not interfere with each other." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n", "\n", ":- dynamic is_empty/1.\n", "\n", "is_empty(another_prolog).\n" ] } ], "source": [ "# \n", "\n", "another_prolog = IsolatedProlog()\n", "\n", "another_prolog.assertz(\"is_empty(another_prolog)\")\n", "\n", "for l in another_prolog.query(\"listing\"):\n", " print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Foreign functions from `pyswip` work as well" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, john\$n", "Hello, gina\$n", "[{'X': 'john'}, {'X': 'gina'}]\n" ] } ], "source": [ "from pyswip import registerForeign\$n", "\n", "def hello(t):\n", " print(\"Hello,\", t)\n", "hello.arity = 1\$n", "\n", "registerForeign(hello)\n", "\n", "prolog = IsolatedProlog()\n", "prolog.assertz(\"father(michael,john)\")\n", "prolog.assertz(\"father(michael,gina)\")\n", "print(list(prolog.query(\"father(michael,X), hello(X)\")))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple prologterms example\$n", "\n", "Walkthrough of basic commands" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from prologterms import TermGenerator, PrologRenderer, Var\$n", "\n", "# term generators allow for convient construction of\$n", "# compound terms\$n", "P = TermGenerator()\n", "\n", "# prolog variables; must start with uppercase\$n", "X = Var('X')\n", "\n", "# create a compound term object, representing\$n", "# a non-ground prolog goal\$n", "term = P.member(X, [1, 2, 3])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "member(X, [1, 2, 3])\n" ] } ], "source": [ "from prologterms import PrologRenderer\$n", "r = PrologRenderer()\n", "print(r.render(term))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### escaping characters\$n", "\n", "this module will take care of escaping characters to conform to\$n", "prolog syntax\$n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "member(X, [1, b, 'foo\\nbar', '\\'hi\\''])\n" ] } ], "source": [ "term = P.member(X, [1, 'b', \"foo\\nbar\", \"'hi'\"])\n", "print(r.render(term))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rules\$n", "\n", "The \"<=\" operator in python is overloaded to mean the same as prolog\$n", "\":-\". This means a rule object can be created as follows:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ancestor(X, Y) :-\n", " parent(X, Z), ancestor(Z, Y)\n" ] } ], "source": [ "X = Var('X')\n", "Y = Var('Y')\n", "Z = Var('Z')\n", "\n", "rule = P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y))\n", "print(r.render(rule))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding comments\$n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "% the ancestor relation propagates over parent/2\$n", "ancestor(X, Y) :-\n", " parent(X, Z), ancestor(Z, Y)\n" ] } ], "source": [ "rule = (P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y))) % 'the ancestor relation propagates over parent/2'\n", "print(r.render(rule))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Programs\$n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ancestor(X, Y) :-\n", " parent(X, Z), ancestor(Z, Y).\n", "ancestor(X, Y) :-\n", " parent(X, Y).\n", "parent(a, b).\n", "parent(b, c).\n", "parent(c, d).\n", "\n" ] } ], "source": [ "from prologterms import TermGenerator, PrologRenderer, Program, Var\$n", "\n", "P = TermGenerator()\n", "X = Var('X')\n", "Y = Var('Y')\n", "Z = Var('Z')\n", "R = PrologRenderer()\n", "\n", "prog = Program(\n", " P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y)),\n", " P.ancestor(X,Y) <= P.parent(X,Y),\n", " P.parent('a','b'),\n", " P.parent('b','c'),\n", " P.parent('c','d')\n", ")\n", "\n", "print(R.render(prog))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interfacing with prolog systems\$n", "\n", "we show two ways of doing this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interfacing with SWI-Prolog via commandline\$n", "\n", "This requires `swipl` on command line to run" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "swipl -l anc.pl -g (forall(ancestor(X, Y), writeln(anc(X, Y))), halt)'\n" ] } ], "source": [ "# write program\$n", "f = open('anc.pl', 'w')\n", "f.write(R.render(prog))\n", "f.close()\n", "\n", "# query\$n", "q = P.ancestor(X,Y)\n", "\n", "# make a wrapper goal\$n", "goal = (P.forall(q, P.writeln(P.anc(X,Y))),'halt')\n", "\n", "# call\$n", "cmd = \"swipl -l anc.pl -g '{}'\".format(R.render(goal))\n", "print(cmd)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "anc(a,d)\n", "anc(a,c)\n", "anc(b,d)\n", "anc(a,b)\n", "anc(b,c)\n", "anc(c,d)\n", "\n" ] } ], "source": [ "import subprocess\$n", "from subprocess import run\$n", "result = run(cmd, shell=True, stdout=subprocess.PIPE)\n", "print(\"{}\".format(result.stdout.decode('ascii')))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interfacing via pengines\$n", "\n", "[pengines](http://pengines.swi-prolog.org/docs/index.html) (Prolog Engines) is a means of communicating with a prolog server from any language. We will use the python pengines API here (TODO: release to PyPi).\n", "\n", "This example requires a pengines server to be running on port 4242. This is fairly easy to do using the pengines framework, and even easier if you have docker, in which case you can run a container like this:\n", "\n", "```\n", "docker run -p 4242:9083 -e PORT=9083 --name cmungall/sparqlprog\$n", "```\n", "\n", "Once this is done, the following program should run." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Builder._getActualURL log.\n", "New value is : http://localhost:4242/pengine/create\$n", "a <- d\$n", "a <- c\$n", "b <- d\$n", "a <- b\$n", "b <- c\$n", "c <- d\$n" ] } ], "source": [ "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "\n", "factory = PengineBuilder(urlserver=\"http://localhost:4242\",\n", " srctext=R.render(prog),\n", " ask=R.render(q))\n", "pengine = Pengine(builder=factory, debug=False)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "for p in pengine.currentQuery.availProofs:\n", " print('{} <- {}'.format(p[X.name], p[Y.name]))" ], "ancestor(X, Y) }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } , { "cell_type": "code", "execution_count": null, "id": "4e901d96-860c-41a4-93fa-1b312200126e", "metadata": {}, "outputs": [], "source": [ "!pip install pengines prologterms\$n", "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "\n", "q = ((member(X,[1,2,3])))'\n", "factory = PengineBuilder(urlserver=\"http://logicmoo.org:3020\", destroy=False, ask=q)\n", "pengine = Pengine(builder=factory)\n", "# Start query.\n", "print()\n", "print(pengine.currentQuery.availProofs, \"Has More? \", pengine.currentQuery.hasMore)\n", "print()\n", "# Get next query.\n", "print(pengine.state.current_state)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "print(pengine.currentQuery.availProofs)\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "3cab67b7-cd1d-4614-8488-32ab378344a6", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "Bad event in answer ()", "output_type": "error", "traceback": [ "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", " File \u001b[1;32m/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py:3251\u001b[0m in \u001b[1;35mrun_code\u001b[0m\$n exec(code_obj, self.user_global_ns, self.user_ns)\n", " Input \u001b[1;32mIn [13]\u001b[0m in \u001b[1;35m\u001b[0m\$n pengine = Pengine(builder=factory)\n", " File \u001b[1;32m/usr/local/lib/python3.10/dist-packages/pengines/Pengine.py:40\u001b[0m in \u001b[1;35m__init__\u001b[0m\$n self.pengineID = self.create()\n", " File \u001b[1;32m/usr/local/lib/python3.10/dist-packages/pengines/Pengine.py:126\u001b[0m in \u001b[1;35mcreate\u001b[0m\$n self.handleAnswer(response[\"answer\"])\n", "\u001b[0;36m File \u001b[0;32m/usr/local/lib/python3.10/dist-packages/pengines/Pengine.py:309\u001b[0;36m in \u001b[0;35mhandleAnswer\u001b[0;36m\u001b[0m\$n\u001b[0;31m raise SyntaxError(\"Bad event in answer\")\u001b[0m\$n", "\u001b[0;36m File \u001b[0;32m\u001b[0;36m\u001b[0m\$n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Bad event in answer\$n" ] } ], "source": [ "#!pip install pengines\$n", "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "\n", "#q = 'X=((wots(S,((current_output(CO),set_stream(CO,alias(user_error)),set_stream(CO,alias(current_output)),ls))),fail))'\n", "q = ((e2ec(\"A man waits\")))'\n", "\n", "factory = PengineBuilder(urlserver=\"http://logicmoo.org:3020\", destroy=False, ask=q)\n", "pengine = Pengine(builder=factory)\n", "# Start query.\n", "print()\n", "print(pengine.currentQuery.availProofs, \"Has More? \", pengine.currentQuery.hasMore)\n", "print()\n", "# Get next query.\n", "print(pengine.state.current_state)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "print(pengine.currentQuery.availProofs)\n", "\n", "q = 'e2eco(X)'\n", "\n", "factory2 = PengineBuilder(urlserver=\"http://logicmoo.org:3020\", destroy=False, ask=q)\n", "pengine = Pengine(builder=factory2)\n", "# Start query.\n", "print()\n", "print(pengine.currentQuery.availProofs, \"Has More? \", pengine.currentQuery.hasMore)\n", "print()\n", "# Get next query.\n", "print(pengine.state.current_state)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "print(pengine.currentQuery.availProofs)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9488e5ca-983a-497f-9545-b593095ee119", "metadata": {}, "outputs": [], "source": [ "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "from prologterms import TermGenerator, PrologRenderer, Program, Var\$n", "\n", "P = TermGenerator()\n", "X = Var('X')\n", "Y = Var('Y')\n", "Z = Var('Z')\n", "R = PrologRenderer()\n", "\n", "p = Program(\n", " P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y)),\n", " P.ancestor(X,Y) <= P.parent(X,Y),\n", " P.parent('a','b'),\n", " P.parent('b','c'),\n", " P.parent('c','d')\n", ")\n", "\n", "q = P.ancestor(X,Y)\n", "\n", "factory = PengineBuilder(urlserver=\"http://logicmoo.org:3020\",\n", " srctext=R.render(p),\n", " ask=R.render(q))\n", "pengine = Pengine(builder=factory)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "for p in pengine.currentQuery.availProofs:\n", " print('{} <- {}'.format(p[X.name], p[Y.name]))" ] }, { "cell_type": "code", "execution_count": null, "id": "0828c87c-532c-4895-852d-53d56a07a510", "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, HTML\$n", "display(HTML('

Hello, world!

'))" ] }, { "cell_type": "code", "execution_count": null, "id": "ae1e2a3f-04b2-4061-ae9c-611d6801418f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6412931c-e124-47c0-a4cc-1751f10145d3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1e86afcb-8812-454d-b740-ea20a5cbf086", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4e901d96-860c-41a4-93fa-1b312200126e", "metadata": {}, "outputs": [], "source": [ "!pip install pengines prologterms\$n", "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "\n", "q = ((member(X,[1,2,3])))'\n", "factory = PengineBuilder(urlserver=\"http://logicmoo.org:3020\", destroy=False, ask=q)\n", "pengine = Pengine(builder=factory)\n", "# Start query.\n", "print()\n", "print(pengine.currentQuery.availProofs, \"Has More? \", pengine.currentQuery.hasMore)\n", "print()\n", "# Get next query.\n", "print(pengine.state.current_state)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "print(pengine.currentQuery.availProofs)\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "3cab67b7-cd1d-4614-8488-32ab378344a6", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "Bad event in answer ()", "output_type": "error", "traceback": [ "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", " File \u001b[1;32m/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py:3251\u001b[0m in \u001b[1;35mrun_code\u001b[0m\$n exec(code_obj, self.user_global_ns, self.user_ns)\n", " Input \u001b[1;32mIn [13]\u001b[0m in \u001b[1;35m\u001b[0m\$n pengine = Pengine(builder=factory)\n", " File \u001b[1;32m/usr/local/lib/python3.10/dist-packages/pengines/Pengine.py:40\u001b[0m in \u001b[1;35m__init__\u001b[0m\$n self.pengineID = self.create()\n", " File \u001b[1;32m/usr/local/lib/python3.10/dist-packages/pengines/Pengine.py:126\u001b[0m in \u001b[1;35mcreate\u001b[0m\$n self.handleAnswer(response[\"answer\"])\n", "\u001b[0;36m File \u001b[0;32m/usr/local/lib/python3.10/dist-packages/pengines/Pengine.py:309\u001b[0;36m in \u001b[0;35mhandleAnswer\u001b[0;36m\u001b[0m\$n\u001b[0;31m raise SyntaxError(\"Bad event in answer\")\u001b[0m\$n", "\u001b[0;36m File \u001b[0;32m\u001b[0;36m\u001b[0m\$n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Bad event in answer\$n" ] } ], "source": [ "#!pip install pengines\$n", "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "\n", "#q = 'X=((wots(S,((current_output(CO),set_stream(CO,alias(user_error)),set_stream(CO,alias(current_output)),ls))),fail))'\n", "q = ((e2ec(\"A man waits\")))'\n", "\n", "factory = PengineBuilder(urlserver=\"http://logicmoo.org:3020\", destroy=False, ask=q)\n", "pengine = Pengine(builder=factory)\n", "# Start query.\n", "print()\n", "print(pengine.currentQuery.availProofs, \"Has More? \", pengine.currentQuery.hasMore)\n", "print()\n", "# Get next query.\n", "print(pengine.state.current_state)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "print(pengine.currentQuery.availProofs)\n", "\n", "q = 'e2eco(X)'\n", "\n", "factory2 = PengineBuilder(urlserver=\"http://logicmoo.org:3020\", destroy=False, ask=q)\n", "pengine = Pengine(builder=factory2)\n", "# Start query.\n", "print()\n", "print(pengine.currentQuery.availProofs, \"Has More? \", pengine.currentQuery.hasMore)\n", "print()\n", "# Get next query.\n", "print(pengine.state.current_state)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "print(pengine.currentQuery.availProofs)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9488e5ca-983a-497f-9545-b593095ee119", "metadata": {}, "outputs": [], "source": [ "from pengines.Builder import PengineBuilder\$n", "from pengines.Pengine import Pengine\$n", "from prologterms import TermGenerator, PrologRenderer, Program, Var\$n", "\n", "P = TermGenerator()\n", "X = Var('X')\n", "Y = Var('Y')\n", "Z = Var('Z')\n", "R = PrologRenderer()\n", "\n", "p = Program(\n", " P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y)),\n", " P.ancestor(X,Y) <= P.parent(X,Y),\n", " P.parent('a','b'),\n", " P.parent('b','c'),\n", " P.parent('c','d')\n", ")\n", "\n", "q = P.ancestor(X,Y)\n", "\n", "factory = PengineBuilder(urlserver=\"http://logicmoo.org:3020\",\n", " srctext=R.render(p),\n", " ask=R.render(q))\n", "pengine = Pengine(builder=factory)\n", "while pengine.currentQuery.hasMore:\n", " pengine.doNext(pengine.currentQuery)\n", "for p in pengine.currentQuery.availProofs:\n", " print('{} <- {}'.format(p[X.name], p[Y.name]))" ] }, { "cell_type": "code", "execution_count": null, "id": "0828c87c-532c-4895-852d-53d56a07a510", "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, HTML\$n", "display(HTML('

Hello, world!

'))" ] }, { "cell_type": "code", "execution_count": null, "id": "ae1e2a3f-04b2-4061-ae9c-611d6801418f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6412931c-e124-47c0-a4cc-1751f10145d3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1e86afcb-8812-454d-b740-ea20a5cbf086", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }