diff --git a/notebooks/logic.ipynb b/notebooks/logic.ipynb index f5013aa76..d604a0d6a 100644 --- a/notebooks/logic.ipynb +++ b/notebooks/logic.ipynb @@ -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": {},