Skip to content

Instantly share code, notes, and snippets.

@Hsankesara
Last active December 13, 2018 16:55
Show Gist options
  • Save Hsankesara/15af4f997a6b1fcca94b7963447361a7 to your computer and use it in GitHub Desktop.
Save Hsankesara/15af4f997a6b1fcca94b7963447361a7 to your computer and use it in GitHub Desktop.
First Order Logic in Kinship Domain
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pyDatalog import pyDatalog"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"pyDatalog.create_terms('factorial, N')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"factorial[N] = N*factorial[N-1] "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"factorial[1] = 1"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"N\n",
"-\n",
"6\n"
]
}
],
"source": [
"print(factorial[3]==N)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"pyDatalog.create_terms('X,Y')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X\n",
"-\n",
"1\n"
]
}
],
"source": [
"# give me all the X so that X is 1\n",
"print(X==1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X | Y \n",
"-----|------\n",
"True | False\n"
]
}
],
"source": [
"# give me all the X and Y so that X is True and Y is False\n",
"print((X==True) & (Y==False))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Y | X\n",
"--|--\n",
"1 | 0\n"
]
}
],
"source": [
"print((Y==1) & (X._in((0, 1,2,3))) & (Y==X+1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Family Data"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"pyDatalog.create_terms('X1, X, Y, Z,W, parent, gender, sibling, cousin, aunt, uncle, ancestor')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"+ parent('Alice', 'Cadece')\n",
"+ parent('Bob', 'Cadece')\n",
"+ parent('Alice', 'Doug')\n",
"+ parent('Bob', 'Doug')\n",
"+ gender('Bob', 'Male')\n",
"+ gender('Alice', 'Female')\n",
"+ gender('Cadece', 'Female')\n",
"+ gender('Doug', 'Male')\n",
"+ parent('Doug', 'Ertho')\n",
"+ parent('Freda', 'Ertho')\n",
"+ parent('Cadece', 'gute')\n",
"+ parent('hamil', 'gute')\n",
"+ gender('Ertho', 'Male')\n",
"+ gender('Freda', 'Female')\n",
"+ gender('gute', 'Male')\n",
"+ gender('hamil', 'Male')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"uncle(X,Y) <= parent(Z,Y)&sibling(Z,X)&gender(X,'M"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sibling(X,Y) <= (parent(Z, X) & parent(Z, Y) & ~(X==Y))\n",
"cousin(X, Y) <= parent(Z, X) & parent(W,Y) & sibling(Z, W)\n",
"aunt(X,Y) <= parent(Z, Y) & sibling(Z, X) & gender(X, 'Female')\n",
"uncle(X,Y) <= parent(Z, Y) & sibling(Z, X) & gender(X, 'Male')\n",
"ancestor(X, Y) <= (parent(W,Y) & (X==W))\n",
"ancestor(X, Y) <= (parent(W,Y) & ancestor(X, W))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Cadece',)]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sibling(X, 'Doug')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('gute',)]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cousin('Ertho', Y)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cousin('Doug', X)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Ertho',)]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aunt('Cadece', X)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Cadece', 'Ertho')]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aunt(X, Y)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Doug', 'gute')]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"uncle(X, Y)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Bob',), ('hamil',), ('Cadece',), ('Alice',)]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ancestor(X, 'gute')"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment