From 057fca4581e099b939d49470eca2875ae000bd48 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 23 Oct 2026 19:00:01 +0800 Subject: [PATCH 1/2] Document first-order syntax support --- notebooks/logic.ipynb | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/notebooks/logic.ipynb b/notebooks/logic.ipynb index f5013aa76..e06feb44e 100644 --- a/notebooks/logic.ipynb +++ b/notebooks/logic.ipynb @@ -3503,6 +3503,74 @@ "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": [ + "### Syntax in this implementation\n", + "\n", + "`expr()` builds `Expr` trees with Python syntax and special handling for `==>`, `<==`, and `<=>`.\n", + "It is not a parser for the full first-order grammar.\n", + "The same call form can denote a relation-like atom (`Teaches(x, y)`) or a function-like term\n", + "(`SubjectOf(y)`). In `Mentors(x, SubjectOf(y))`, the call is nested as a term. The tree does not\n", + "assign separate predicate and function types; context supplies the role.\n", + "In the FOL helpers, a no-argument lowercase symbol is treated as a variable; names such as\n", + "`Ada` are constants.\n", + "\n", + "`Expr.__eq__` compares expression trees: `x == y` is a Python boolean, not a logical equality\n", + "formula. `FolKB` does not provide built-in equality reasoning. Use `unify` for syntactic term\n", + "matching; a named predicate such as `SamePerson(x, y)` is an ordinary relation, not equality.\n", + "\n", + "`FolKB` accepts first-order definite clauses. Variables in these rules are implicitly universally\n", + "quantified by rule application; the chaining API does not implement general `forall`/`exists`\n", + "formulas. Query variables yield substitutions, as shown below." + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {"collapsed": true}, + "outputs": [ + { + "data": { + "text/plain": [ + "(False, Teaches(x, y), Mentors(x, SubjectOf(y)))" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x, y = expr('x, y')\n", + "x == y, expr('Teaches(x, y)'), expr('Mentors(x, SubjectOf(y))')" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {"collapsed": true}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{q: Logic}]" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "syntax_demo_kb = FolKB([\n", + " expr('Tutor(x, y) ==> Learns(x, y)'),\n", + " expr('Tutor(Ada, Logic)'),\n", + "])\n", + "list(fol_fc_ask(syntax_demo_kb, expr('Learns(Ada, q)')))" + ] + }, { "cell_type": "markdown", "metadata": {}, From 7d26f797757a0ee81ae95ef1f9a269ce0d321934 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Fri, 20 Nov 2026 01:23:05 +0100 Subject: [PATCH 2/2] Map the grammar of Figure 8.3 onto expr instead of repeating the connectives --- notebooks/logic.ipynb | 52 ++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/notebooks/logic.ipynb b/notebooks/logic.ipynb index e06feb44e..d604a0d6a 100644 --- a/notebooks/logic.ipynb +++ b/notebooks/logic.ipynb @@ -3507,68 +3507,64 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Syntax in this implementation\n", + "### First-order syntax in `expr`\n", "\n", - "`expr()` builds `Expr` trees with Python syntax and special handling for `==>`, `<==`, and `<=>`.\n", - "It is not a parser for the full first-order grammar.\n", - "The same call form can denote a relation-like atom (`Teaches(x, y)`) or a function-like term\n", - "(`SubjectOf(y)`). In `Mentors(x, SubjectOf(y))`, the call is nested as a term. The tree does not\n", - "assign separate predicate and function types; context supplies the role.\n", - "In the FOL helpers, a no-argument lowercase symbol is treated as a variable; names such as\n", - "`Ada` are constants.\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", - "`Expr.__eq__` compares expression trees: `x == y` is a Python boolean, not a logical equality\n", - "formula. `FolKB` does not provide built-in equality reasoning. Use `unify` for syntactic term\n", - "matching; a named predicate such as `SamePerson(x, y)` is an ordinary relation, not equality.\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", - "`FolKB` accepts first-order definite clauses. Variables in these rules are implicitly universally\n", - "quantified by rule application; the chaining API does not implement general `forall`/`exists`\n", - "formulas. Query variables yield substitutions, as shown below." + "`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": 100, - "metadata": {"collapsed": true}, + "execution_count": null, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "(False, Teaches(x, y), Mentors(x, SubjectOf(y)))" + "(False, (LeftLeg(John),))" ] }, - "execution_count": 100, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, y = expr('x, y')\n", - "x == y, expr('Teaches(x, y)'), expr('Mentors(x, SubjectOf(y))')" + "x == y, expr('Person(LeftLeg(John))').args" ] }, { "cell_type": "code", - "execution_count": 101, - "metadata": {"collapsed": true}, + "execution_count": null, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[{q: Logic}]" + "[{w: John}]" ] }, - "execution_count": 101, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "syntax_demo_kb = FolKB([\n", - " expr('Tutor(x, y) ==> Learns(x, y)'),\n", - " expr('Tutor(Ada, Logic)'),\n", - "])\n", - "list(fol_fc_ask(syntax_demo_kb, expr('Learns(Ada, q)')))" + "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)')))" ] }, {