Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions notebooks/logic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3503,6 +3503,70 @@
"The class `FolKB` can be used to represent a knowledge base of First-order logic sentences. You would initialize and use it the same way as you would for `PropKB` except that the clauses are first-order definite clauses. We will see how to write such clauses to create a database and query them in the following sections."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### First-order syntax in `expr`\n",
"\n",
"The connectives are written as in the table at the top of this notebook; the rest of the grammar of Figure 8.3 is written as follows.\n",
"\n",
"| Figure 8.3 | In `expr` | Example |\n",
"|------------|-----------|---------|\n",
"| *Constant* | a symbol starting with an uppercase letter | `John` |\n",
"| *Variable* | a symbol starting with a lowercase letter, with no arguments | `x` |\n",
"| *Predicate*(*Term*, ...) | a call | `Brother(Richard, John)` |\n",
"| *Function*(*Term*, ...) | a call nested inside another one | `LeftLeg(John)` in `Person(LeftLeg(John))` |\n",
"| *Term* = *Term* | not supported | |\n",
"| ∀ *Variable* *Sentence* | implicit: the variables of a clause in a `FolKB` are universally quantified | `King(x) & Greedy(x) ==> Evil(x)` |\n",
"| ∃ *Variable* *Sentence* | not supported: the object is named by a Skolem constant | `Owns(Nono, M1)` |\n",
"\n",
"`Expr` does not tell a predicate from a function, so the role of a call is given by where it appears. Python's `==` compares two expressions and returns `True` or `False`, so `x == y` is not an equality sentence; and since `FolKB` holds only definite clauses, there is no syntax for quantifiers. The variables of a query are answered by substitutions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(False, (LeftLeg(John),))"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x, y = expr('x, y')\n",
"x == y, expr('Person(LeftLeg(John))').args"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{w: John}]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kb = FolKB([expr('King(x) & Greedy(x) ==> Evil(x)'),\n",
" expr('King(John)'), expr('Greedy(John)')])\n",
"list(fol_fc_ask(kb, expr('Evil(w)')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down