diff --git a/logic_programming/3_PropositionalLogic.ipynb b/logic_programming/3_PropositionalLogic.ipynb index 0c42acec8cbc5ec89d6d61ab7f1b302ccd096575..75d3037101655ea6d52e04ef6b2f9c43331eb9d3 100644 --- a/logic_programming/3_PropositionalLogic.ipynb +++ b/logic_programming/3_PropositionalLogic.ipynb @@ -1665,9 +1665,27 @@ "jupyter:retry." ] }, + { + "cell_type": "markdown", + "id": "35fba4c7", + "metadata": {}, + "source": [ + "### Appendix: Generating equivalent formulas" + ] + }, + { + "cell_type": "markdown", + "id": "78beaf88", + "metadata": {}, + "source": [ + "The code below can be used to generate formulas in Prolog using a technique similar to iterative deepening.\n", + "We will study this technique much later in the course in the lectures on search.\n", + "All you need to know is that generate_formula generates formulas from smaller to deeper formulas." + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "id": "51d5630b", "metadata": { "vscode": { @@ -1675,6 +1693,210 @@ } }, "outputs": [], + "source": [ + "generate_formula(X) :- \n", + " peano(MaxDepth), gen(X,MaxDepth).\n", + "peano(0).\n", + "peano(s(X)) :- peano(X).\n", + "\n", + "proposition(p).\n", + "proposition(q).\n", + "proposition(r).\n", + "\n", + "gen(X,_) :- proposition(X).\n", + "gen(and(A,B),s(MaxDepth)) :- gen(A,MaxDepth), gen(B,MaxDepth).\n", + "gen(or(A,B),s(MaxDepth)) :- gen(A,MaxDepth), gen(B,MaxDepth).\n", + "gen(implies(A,B),s(MaxDepth)) :- gen(A,MaxDepth), gen(B,MaxDepth).\n", + "gen(equiv(A,B),s(MaxDepth)) :- gen(A,MaxDepth), gen(B,MaxDepth).\n", + "gen(not(A),s(MaxDepth)) :- gen(A,MaxDepth)." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "f843534d", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mX = p" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?-generate_formula(X)." + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "3e0e951d", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mX = q" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "jupyter:retry." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "8199e19a", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mX = r" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "jupyter:retry." + ] + }, + { + "cell_type": "markdown", + "id": "cd56c6d2", + "metadata": {}, + "source": [ + "We can now use this together with our other Prolog predicates to generate equivalent formulas:" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "fae03e15", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mFormula = and(q,p)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- generate_formula(Formula), equivalent(and(p,q),Formula), Formula \\= and(p,q)." + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "94b16168", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mFormula = and(p,and(p,q))" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "jupyter:retry." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "a49ecd78", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mFormula = p" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- generate_formula(Formula), equivalent(and(p,p),Formula)." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "49caff90", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mFormula = p" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- generate_formula(Formula), equivalent(and(p,or(p,q)),Formula)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79db3ffb", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], "source": [] } ],