logic.ipynb 25,4 ko
Newer Older
jeff3456's avatar
jeff3456 a validé
  {
   "cell_type": "markdown",
jeff3456's avatar
jeff3456 a validé
   "metadata": {
    "collapsed": true
jeff3456's avatar
jeff3456 a validé
   },
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "# Logic: `logic.py`; Chapters 6-8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "This notebook describes the [logic.py](https://github.com/aimacode/aima-python/blob/master/logic.py) module, which covers Chapters 6 (Logical Agents),  7 (First-Order Logic) and  8 (Inference in First-Order Logic) of *[Artificial Intelligence: A Modern Approach](http://aima.cs.berkeley.edu)*. See the [intro notebook](https://github.com/aimacode/aima-python/blob/master/intro.ipynb) for instructions.\n",
Peter Norvig's avatar
Peter Norvig a validé
    "We'll start by looking at `Expr`, the data type for logical sentences, and the convenience function `expr`. Then we'll cover `KB` and `ProbKB`, the classes for Knowledge Bases. Then, we will construct a knowledge base of a specific situation in the Wumpus World. We will next go through the `tt_entails` function and experiment with it a bit. The `pl_resolution` and `pl_fc_entails` functions will come next.  \n",
Peter Norvig's avatar
Peter Norvig a validé
    "But the first step is to load the code:"
jeff3456's avatar
jeff3456 a validé
   ]
  },
Peter Norvig's avatar
Peter Norvig a validé
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
    "from utils import *\n",
Peter Norvig's avatar
Peter Norvig a validé
    "from logic import *"
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
Peter Norvig's avatar
Peter Norvig a validé
    "## Logical Sentences"
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "The `Expr` class is designed to represent any kind of mathematical expression. The simplest type of `Expr` is a symbol, which can be defined with the function `Symbol`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Symbol('x')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can define multiple symbols at the same time with the function `symbols`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "(x, y, P, Q, f) = symbols('x, y, P, Q, f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can combine `Expr`s with the regular Python infix and prefix operators. Here's how we would form the logical sentence \"P and not Q\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(P & ~Q)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "P & ~Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "This works because the `Expr` class overloads the `&` operator with this definition:\n",
    "\n",
    "```python\n",
    "def __and__(self, other): return Expr('&',  self, other)```\n",
    "     \n",
    "and does similar overloads for the other operators. An `Expr` has two fields: `op` for the operator, which is always a string, and `args` for the arguments, which is a tuple of 0 or more expressions. By \"expression,\" I mean either an instance of `Expr`, or a number. Let's take a look at the fields for some `Expr` examples:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'&'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sentence = P & ~Q\n",
Peter Norvig's avatar
Peter Norvig a validé
    "\n",
    "sentence.op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(P, ~Q)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "sentence.args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
Peter Norvig's avatar
Peter Norvig a validé
       "'P'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "P.op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P.args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
Peter Norvig's avatar
Peter Norvig a validé
       "'P'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "Pxy = P(x, y)\n",
    "\n",
    "Pxy.op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
     "data": {
      "text/plain": [
       "(x, y)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
Peter Norvig's avatar
Peter Norvig a validé
    "Pxy.args"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is important to note that the `Expr` class does not define the *logic* of Propositional Logic sentences; it just gives you a way to *represent* expressions. Think of an `Expr` as an [abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree).  Each of the `args` in an `Expr` can be either a symbol, a number, or a nested `Expr`. We can nest these trees to any depth. Here is a deply nested `Expr`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
     "data": {
      "text/plain": [
       "(((3 * f(x, y)) + (P(y) / 2)) + 1)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
Peter Norvig's avatar
Peter Norvig a validé
    "3 * f(x, y) + P(y) / 2 + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Operators for Constructing Logical Sentences\n",
    "Here is a table of the operators that can be used to form sentences. Note that we have a problem: we want to use Python operators to make sentences, so that our programs (and our interactive sessions like the one here) will show simple code. But Python does not allow implication arrows as operators, so for now we have to use a more verbose notation that Python does allow: `|'==>'|` instead of just `==>`. Alternately, you can always use the more verbose `Expr` constructor forms:\n",
    "| Operation                | Book | Python Infix Input | Python Output | Python `Expr` Input\n",
Peter Norvig's avatar
Peter Norvig a validé
    "|--------------------------|----------------------|-------------------------|---|---|\n",
    "| Negation                 | ¬ P      | `~P`                       | `~P` | `Expr('~', P)`\n",
    "| And                      | P ∧ Q       | `P & Q`                     | `P & Q` | `Expr('&', P, Q)`\n",
    "| Or                       | P &or; Q | `P`<tt> &#124; </tt>`Q`| `P`<tt> &#124; </tt>`Q` | `Expr('`&#124;`', P, Q)`\n",
Peter Norvig's avatar
Peter Norvig a validé
    "| Inequality (Xor)         | P &ne; Q     | `P ^ Q`                | `P ^ Q`  | `Expr('^', P, Q)`\n",
    "| Implication                  | P &rarr; Q    | `P` <tt>&#124;</tt>`'==>'`<tt>&#124;</tt> `Q`   | `P ==> Q` | `Expr('==>', P, Q)`\n",
    "| Reverse Implication      | Q &larr; P     | `Q` <tt>&#124;</tt>`'<=='`<tt>&#124;</tt> `P`  |`Q <== P` | `Expr('<==', Q, P)`\n",
    "| Equivalence            | P &harr; Q   | `P` <tt>&#124;</tt>`'<=>'`<tt>&#124;</tt> `Q`   |`P <=> Q` | `Expr('<=>', P, Q)`\n",
    "Here's an example of defining a sentence with an implication arrow:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(~(P & Q) ==> (~P | ~Q))"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "~(P & Q)  |'==>'|  (~P | ~Q)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Peter Norvig's avatar
Peter Norvig a validé
    "## `expr`: a Shortcut for Constructing Sentences\n",
    "If the `|'==>'|` notation looks ugly to you, you can use the function `expr` instead:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(~(P & Q) ==> (~P | ~Q))"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr('~(P & Q)  ==>  (~P | ~Q)')"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`expr` takes a string as input, and parses it into an `Expr`. The string can contain arrow operators: `==>`, `<==`, or `<=>`, which are handled as if they were regular Python infix operators. And `expr` automatically defines any symbols, so you don't need to pre-define them:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sqrt(((b ** 2) - ((4 * a) * c)))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr('sqrt(b ** 2 - 4 * a * c)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For now that's all you need to know about `expr`. Later we will explain the messy details of how `expr` is implemented and how `|'==>'|` is handled."
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Propositional Knowledge Bases: `PropKB`\n",
Peter Norvig's avatar
Peter Norvig a validé
    "The class `PropKB` can be used to represent a knowledge base of propositional logic sentences.\n",
Peter Norvig's avatar
Peter Norvig a validé
    "We see that the class `KB` has four methods, apart from `__init__`. A point to note here: the `ask` method simply calls the `ask_generator` method. Thus, this one has already been implemented and what you'll have to actually implement when you create your own knowledge base class (if you want to, though I doubt you'll ever need to; just use the ones we've created for you), will be the `ask_generator` function and not the `ask` function itself.\n",
    "\n",
    "The class `PropKB` now.\n",
    "* `__init__(self, sentence=None)` : The constructor `__init__` creates a single field `clauses` which will be a list of all the sentences of the knowledge base. Note that each one of these sentences will be a 'clause' i.e. a sentence which is made up of only literals and `or`s.\n",
    "* `tell(self, sentence)` : When you want to add a sentence to the KB, you use the `tell` method. This method takes a sentence, converts it to its CNF, extracts all the clauses, and adds all these clauses to the `clauses` field. So, you need not worry about `tell`ing only clauses to the knowledge base. You can `tell` the knowledge base a sentence in any form that you wish; converting it to CNF and adding the resulting clauses will be handled by the `tell` method.\n",
    "* `ask_generator(self, query)` : The `ask_generator` function is used by the `ask` function. It calls the `tt_entails` function, which in turn returns `True` if the knowledge base entails query and `False` otherwise. The `ask_generator` itself returns an empty dict `{}` if the knowledge base entails query and `None` otherwise. This might seem a little bit weird to you. After all, it makes more sense just to return a `True` or a `False` instead of the `{}` or `None` But this is done to maintain consistency with the way things are in First-Order Logic, where, an `ask_generator` function, is supposed to return all the substitutions that make the query true. Hence the dict, to return all these substitutions. I will be mostly be using the `ask` function which returns a `{}` or a `False`, but if you don't like this, you can always use the `ask_if_true` function which returns a `True` or a `False`.\n",
    "* `retract(self, sentence)` : This function removes all the clauses of the sentence given, from the knowledge base. Like the `tell` function, you don't have to pass clauses to remove them from the knowledge base; any sentence will do fine. The function will take care of converting that sentence to clauses and then remove those."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: More on KBs, plus what was promised in Intro Section\n",
    "\n",
    "TODO: fill in here ..."
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Appendix: The Implementation of `|'==>'|`\n",
Peter Norvig's avatar
Peter Norvig a validé
    "\n",
    "Consider the `Expr` formed by this syntax:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(P ==> ~Q)"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P |'==>'| ~Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What is the funny `|'==>'|` syntax? The trick is that \"`|`\" is just the regular Python or-operator, and so is exactly equivalent to this: "
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(P ==> ~Q)"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(P | '==>') | ~Q"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In other words, there are two applications of or-operators. Here's the first one:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PartialExpr('==>', P)"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P | '==>'"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What is going on here is that the `__or__` method of `Expr` serves a dual purpose. If the right-hand-side is another `Expr` (or a number), then the result is an `Expr`, as in `(P | Q)`. But if the right-hand-side is a string, then the string is taken to be an operator, and we create a node in the abstract syntax tree corresponding to a partially-filled  `Expr`, one where we know the left-hand-side is `P` and the operator is `==>`, but we don't yet know the right-hand-side.\n",
Peter Norvig's avatar
Peter Norvig a validé
    "\n",
    "The `PartialExpr` class has an `__or__` method that says to create an `Expr` node with the right-hand-side filled in. Here we can see the combination of the `PartialExpr` with `Q` to create a complete `Expr`:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(P ==> ~Q)"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial = PartialExpr('==>', P) \n",
    "partial | ~Q"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This  [trick](http://code.activestate.com/recipes/384122-infix-operators/) is due to [Ferdinand Jamitzky](http://code.activestate.com/recipes/users/98863/), with a modification by [C. G. Vedant](https://github.com/Chipe1),\n",
    "who suggested using a string inside the or-bars.\n",
    "\n",
    "## Appendix: The Implementation of `expr`\n",
    "\n",
    "How does `expr` parse a string into an `Expr`? It turns out there are two tricks (besides the Jamitzky/Vedant trick):\n",
    "\n",
    "1. We do a string substitution, replacing \"`==>`\" with \"`|'==>'|`\" (and likewise for other operators).\n",
    "2. We `eval` the resulting string in an environment in which every identifier\n",
    "is bound to a symbol with that identifier as the `op`.\n",
    "\n",
    "In other words,"
Peter Norvig's avatar
Peter Norvig a validé
   "execution_count": 19,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(~(P & Q) ==> (~P | ~Q))"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr('~(P & Q)  ==>  (~P | ~Q)')"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "is equivalent to doing:"
Peter Norvig's avatar
Peter Norvig a validé
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
Peter Norvig's avatar
Peter Norvig a validé
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(~(P & Q) ==> (~P | ~Q))"
Peter Norvig's avatar
Peter Norvig a validé
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P, Q = symbols('P, Q')\n",
    "~(P & Q)  |'==>'|  (~P | ~Q)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One thing to beware of: this puts `==>` at the same precedence level as `\"|\"`, which is not quite right. For example, we get this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((P & Q) ==> P) | Q)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P & Q  |'==>'|  P | Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which is probably not what we meant; when in doubt, put in extra parens:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((P & Q) ==> (P | Q))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(P & Q)  |'==>'|  (P | Q)"
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
       "<div>\n",
       "<canvas id=\"canvas_bc_ask\" width=\"800\" height=\"600\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"canvas_bc_ask\")'></canvas>\n",
       "</div>\n",
       "\n",
       "<script> var canvas_bc_ask_canvas_object = new Canvas(\"canvas_bc_ask\");</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<script>\n",
       "canvas_bc_ask_canvas_object.clear();\n",
       "canvas_bc_ask_canvas_object.strokeWidth(3);\n",
       "canvas_bc_ask_canvas_object.stroke(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.font(\"12px Arial\");\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(340, 85, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(340, 85, 460, 85);\n",
       "canvas_bc_ask_canvas_object.line(340, 85, 340, 115);\n",
       "canvas_bc_ask_canvas_object.line(460, 85, 460, 115);\n",
       "canvas_bc_ask_canvas_object.line(340, 115, 460, 115);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Criminal(West)\", 348, 109);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(55, 255, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(55, 255, 175, 255);\n",
       "canvas_bc_ask_canvas_object.line(55, 255, 55, 285);\n",
       "canvas_bc_ask_canvas_object.line(175, 255, 175, 285);\n",
       "canvas_bc_ask_canvas_object.line(55, 285, 175, 285);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"American(West)\", 63, 279);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(245, 255, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(245, 255, 365, 255);\n",
       "canvas_bc_ask_canvas_object.line(245, 255, 245, 285);\n",
       "canvas_bc_ask_canvas_object.line(365, 255, 365, 285);\n",
       "canvas_bc_ask_canvas_object.line(245, 285, 365, 285);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Weapon(M1)\", 253, 279);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(435, 255, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(435, 255, 555, 255);\n",
       "canvas_bc_ask_canvas_object.line(435, 255, 435, 285);\n",
       "canvas_bc_ask_canvas_object.line(555, 255, 555, 285);\n",
       "canvas_bc_ask_canvas_object.line(435, 285, 555, 285);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Sells(West, M1, Nono)\", 443, 279);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(625, 255, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(625, 255, 745, 255);\n",
       "canvas_bc_ask_canvas_object.line(625, 255, 625, 285);\n",
       "canvas_bc_ask_canvas_object.line(745, 255, 745, 285);\n",
       "canvas_bc_ask_canvas_object.line(625, 285, 745, 285);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Hostile(Nono)\", 633, 279);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(55, 425, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(55, 425, 175, 425);\n",
       "canvas_bc_ask_canvas_object.line(55, 425, 55, 455);\n",
       "canvas_bc_ask_canvas_object.line(175, 425, 175, 455);\n",
       "canvas_bc_ask_canvas_object.line(55, 455, 175, 455);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Missile(M1)\", 63, 449);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(245, 425, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(245, 425, 365, 425);\n",
       "canvas_bc_ask_canvas_object.line(245, 425, 245, 455);\n",
       "canvas_bc_ask_canvas_object.line(365, 425, 365, 455);\n",
       "canvas_bc_ask_canvas_object.line(245, 455, 365, 455);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Missile(M1)\", 253, 449);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(435, 425, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(435, 425, 555, 425);\n",
       "canvas_bc_ask_canvas_object.line(435, 425, 435, 455);\n",
       "canvas_bc_ask_canvas_object.line(555, 425, 555, 455);\n",
       "canvas_bc_ask_canvas_object.line(435, 455, 555, 455);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Owns(Nono, M1)\", 443, 449);\n",
       "canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
       "canvas_bc_ask_canvas_object.rect(625, 425, 120, 30);\n",
       "canvas_bc_ask_canvas_object.line(625, 425, 745, 425);\n",
       "canvas_bc_ask_canvas_object.line(625, 425, 625, 455);\n",
       "canvas_bc_ask_canvas_object.line(745, 425, 745, 455);\n",
       "canvas_bc_ask_canvas_object.line(625, 455, 745, 455);\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Enemy(Nono, America)\", 633, 449);\n",
       "canvas_bc_ask_canvas_object.line(400, 115, 495, 255);\n",
       "canvas_bc_ask_canvas_object.line(305, 285, 115, 425);\n",
       "canvas_bc_ask_canvas_object.line(495, 285, 305, 425);\n",
       "canvas_bc_ask_canvas_object.line(685, 285, 685, 425);\n",
       "canvas_bc_ask_canvas_object.line(400, 115, 685, 255);\n",
       "canvas_bc_ask_canvas_object.line(400, 115, 305, 255);\n",
       "canvas_bc_ask_canvas_object.line(400, 115, 115, 255);\n",
       "canvas_bc_ask_canvas_object.line(495, 285, 495, 425);\n",
       "canvas_bc_ask_canvas_object.fill(255, 255, 255);\n",
       "canvas_bc_ask_canvas_object.rect(0, 540, 800, 60);\n",
       "canvas_bc_ask_canvas_object.strokeWidth(5);\n",
       "canvas_bc_ask_canvas_object.stroke(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.line(0, 540, 800, 540);\n",
       "canvas_bc_ask_canvas_object.font(\"22px Arial\");\n",
       "canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
       "canvas_bc_ask_canvas_object.fill_text(\"Click for text\", 20, 585);\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from canvas import Canvas_fol_bc_ask\n",
    "canvas_bc_ask = Canvas_fol_bc_ask('canvas_bc_ask', crime_kb, expr('Criminal(x)'))"
   ]
  },
Peter Norvig's avatar
Peter Norvig a validé
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
Peter Norvig's avatar
Peter Norvig a validé
   "source": [
    "# Authors\n",
    "\n",
    "This notebook by [Chirag Vertak](https://github.com/chiragvartak) and [Peter Norvig](https://github.com/norvig).\n",
    "\n"
   ]
  }
 ],
 "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.6.1"
 "nbformat_minor": 1