Newer
Older
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unify(expr('A(x)'), expr('A(B)'))"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{x: Bella, y: Dobby}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unify(expr('Cat(x) & Dog(Dobby)'), expr('Cat(Bella) & Dog(y)'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In cases where there is no possible substitution that unifies the two sentences the function return `None`."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(unify(expr('Cat(x)'), expr('Dog(Dobby)')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also need to take care we do not unintentionally use the same variable name. Unify treats them as a single variable which prevents it from taking multiple value."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(unify(expr('Cat(x) & Dog(Dobby)'), expr('Cat(Bella) & Dog(x)')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Forward Chaining Algorithm\n",
"We consider the simple forward-chaining algorithm presented in <em>Figure 9.3</em>. We look at each rule in the knowledge base and see if the premises can be satisfied. This is done by finding a substitution which unifies each of the premise with a clause in the `KB`. If we are able to unify the premises, the conclusion (with the corresponding substitution) is added to the `KB`. This inferencing process is repeated until either the query can be answered or till no new sentences can be added. We test if the newly added clause unifies with the query in which case the substitution yielded by `unify` is an answer to the query. If we run out of sentences to infer, this means the query was a failure.\n",
"The function `fol_fc_ask` is a generator which yields all substitutions which validate the query."
]
},
{
"cell_type": "code",
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">fol_fc_ask</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">alpha</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""A simple forward-chaining algorithm. [Figure 9.3]"""</span>\n",
" <span class=\"c1\"># TODO: Improve efficiency</span>\n",
" <span class=\"n\">kb_consts</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">({</span><span class=\"n\">c</span> <span class=\"k\">for</span> <span class=\"n\">clause</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span> <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span> <span class=\"n\">constant_symbols</span><span class=\"p\">(</span><span class=\"n\">clause</span><span class=\"p\">)})</span>\n",
" <span class=\"k\">def</span> <span class=\"nf\">enum_subst</span><span class=\"p\">(</span><span class=\"n\">p</span><span class=\"p\">):</span>\n",
" <span class=\"n\">query_vars</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">({</span><span class=\"n\">v</span> <span class=\"k\">for</span> <span class=\"n\">clause</span> <span class=\"ow\">in</span> <span class=\"n\">p</span> <span class=\"k\">for</span> <span class=\"n\">v</span> <span class=\"ow\">in</span> <span class=\"n\">variables</span><span class=\"p\">(</span><span class=\"n\">clause</span><span class=\"p\">)})</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">assignment_list</span> <span class=\"ow\">in</span> <span class=\"n\">itertools</span><span class=\"o\">.</span><span class=\"n\">product</span><span class=\"p\">(</span><span class=\"n\">kb_consts</span><span class=\"p\">,</span> <span class=\"n\">repeat</span><span class=\"o\">=</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">query_vars</span><span class=\"p\">)):</span>\n",
" <span class=\"n\">theta</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"n\">x</span><span class=\"p\">:</span> <span class=\"n\">y</span> <span class=\"k\">for</span> <span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span> <span class=\"ow\">in</span> <span class=\"nb\">zip</span><span class=\"p\">(</span><span class=\"n\">query_vars</span><span class=\"p\">,</span> <span class=\"n\">assignment_list</span><span class=\"p\">)}</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">theta</span>\n",
"\n",
" <span class=\"c1\"># check if we can answer without new inferences</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">q</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span><span class=\"p\">:</span>\n",
" <span class=\"n\">phi</span> <span class=\"o\">=</span> <span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"p\">,</span> <span class=\"n\">alpha</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">phi</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">phi</span>\n",
"\n",
" <span class=\"k\">while</span> <span class=\"bp\">True</span><span class=\"p\">:</span>\n",
" <span class=\"n\">new</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">rule</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span><span class=\"p\">:</span>\n",
" <span class=\"n\">p</span><span class=\"p\">,</span> <span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">parse_definite_clause</span><span class=\"p\">(</span><span class=\"n\">rule</span><span class=\"p\">)</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">theta</span> <span class=\"ow\">in</span> <span class=\"n\">enum_subst</span><span class=\"p\">(</span><span class=\"n\">p</span><span class=\"p\">):</span>\n",
" <span class=\"k\">if</span> <span class=\"nb\">set</span><span class=\"p\">(</span><span class=\"n\">subst</span><span class=\"p\">(</span><span class=\"n\">theta</span><span class=\"p\">,</span> <span class=\"n\">p</span><span class=\"p\">))</span><span class=\"o\">.</span><span class=\"n\">issubset</span><span class=\"p\">(</span><span class=\"nb\">set</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span><span class=\"p\">)):</span>\n",
" <span class=\"n\">q_</span> <span class=\"o\">=</span> <span class=\"n\">subst</span><span class=\"p\">(</span><span class=\"n\">theta</span><span class=\"p\">,</span> <span class=\"n\">q</span><span class=\"p\">)</span>\n",
" <span class=\"k\">if</span> <span class=\"nb\">all</span><span class=\"p\">([</span><span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">q_</span><span class=\"p\">,</span> <span class=\"p\">{})</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span> <span class=\"k\">for</span> <span class=\"n\">x</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span> <span class=\"o\">+</span> <span class=\"n\">new</span><span class=\"p\">]):</span>\n",
" <span class=\"n\">new</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">q_</span><span class=\"p\">)</span>\n",
" <span class=\"n\">phi</span> <span class=\"o\">=</span> <span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">q_</span><span class=\"p\">,</span> <span class=\"n\">alpha</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">phi</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">phi</span>\n",
" <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">new</span><span class=\"p\">:</span>\n",
" <span class=\"k\">break</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">clause</span> <span class=\"ow\">in</span> <span class=\"n\">new</span><span class=\"p\">:</span>\n",
" <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">tell</span><span class=\"p\">(</span><span class=\"n\">clause</span><span class=\"p\">)</span>\n",
" <span class=\"k\">return</span> <span class=\"bp\">None</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's find out all the hostile nations. Note that we only told the `KB` that Nono was an enemy of America, not that it was hostile."
]
},
{
"cell_type": "code",
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{x: Nono}]\n"
]
}
],
"source": [
"answer = fol_fc_ask(crime_kb, expr('Hostile(x)'))\n",
"print(list(answer))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The generator returned a single substitution which says that Nono is a hostile nation. See how after adding another enemy nation the generator returns two substitutions."
]
},
{
"cell_type": "code",
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{x: Nono}, {x: JaJa}]\n"
]
}
],
"source": [
"crime_kb.tell(expr('Enemy(JaJa, America)'))\n",
"answer = fol_fc_ask(crime_kb, expr('Hostile(x)'))\n",
"print(list(answer))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong><em>Note</em>:</strong> `fol_fc_ask` makes changes to the `KB` by adding sentences to it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Backward Chaining Algorithm\n",
"This algorithm works backward from the goal, chaining through rules to find known facts that support the proof. Suppose `goal` is the query we want to find the substitution for. We find rules of the form $\\text{lhs} \\implies \\text{goal}$ in the `KB` and try to prove `lhs`. There may be multiple clauses in the `KB` which give multiple `lhs`. It is sufficient to prove only one of these. But to prove a `lhs` all the conjuncts in the `lhs` of the clause must be proved. This makes it similar to <em>And/Or</em> search."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### OR\n",
"The <em>OR</em> part of the algorithm comes from our choice to select any clause of the form $\\text{lhs} \\implies \\text{goal}$. Looking at all rules's `lhs` whose `rhs` unify with the `goal`, we yield a substitution which proves all the conjuncts in the `lhs`. We use `parse_definite_clause` to attain `lhs` and `rhs` from a clause of the form $\\text{lhs} \\implies \\text{rhs}$. For atomic facts the `lhs` is an empty list."
]
},
{
"cell_type": "code",
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">fol_bc_or</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">goal</span><span class=\"p\">,</span> <span class=\"n\">theta</span><span class=\"p\">):</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">rule</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">fetch_rules_for_goal</span><span class=\"p\">(</span><span class=\"n\">goal</span><span class=\"p\">):</span>\n",
" <span class=\"n\">lhs</span><span class=\"p\">,</span> <span class=\"n\">rhs</span> <span class=\"o\">=</span> <span class=\"n\">parse_definite_clause</span><span class=\"p\">(</span><span class=\"n\">standardize_variables</span><span class=\"p\">(</span><span class=\"n\">rule</span><span class=\"p\">))</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">theta1</span> <span class=\"ow\">in</span> <span class=\"n\">fol_bc_and</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">lhs</span><span class=\"p\">,</span> <span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">rhs</span><span class=\"p\">,</span> <span class=\"n\">goal</span><span class=\"p\">,</span> <span class=\"n\">theta</span><span class=\"p\">)):</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">theta1</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### AND\n",
"The <em>AND</em> corresponds to proving all the conjuncts in the `lhs`. We need to find a substitution which proves each <em>and</em> every clause in the list of conjuncts."
]
},
{
"cell_type": "code",
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">fol_bc_and</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">goals</span><span class=\"p\">,</span> <span class=\"n\">theta</span><span class=\"p\">):</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">theta</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"k\">pass</span>\n",
" <span class=\"k\">elif</span> <span class=\"ow\">not</span> <span class=\"n\">goals</span><span class=\"p\">:</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">theta</span>\n",
" <span class=\"k\">else</span><span class=\"p\">:</span>\n",
" <span class=\"n\">first</span><span class=\"p\">,</span> <span class=\"n\">rest</span> <span class=\"o\">=</span> <span class=\"n\">goals</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">],</span> <span class=\"n\">goals</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">theta1</span> <span class=\"ow\">in</span> <span class=\"n\">fol_bc_or</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">subst</span><span class=\"p\">(</span><span class=\"n\">theta</span><span class=\"p\">,</span> <span class=\"n\">first</span><span class=\"p\">),</span> <span class=\"n\">theta</span><span class=\"p\">):</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">theta2</span> <span class=\"ow\">in</span> <span class=\"n\">fol_bc_and</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">rest</span><span class=\"p\">,</span> <span class=\"n\">theta1</span><span class=\"p\">):</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">theta2</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now the main function `fl_bc_ask` calls `fol_bc_or` with substitution initialized as empty. The `ask` method of `FolKB` uses `fol_bc_ask` and fetches the first substitution returned by the generator to answer query. Let's query the knowledge base we created from `clauses` to find hostile nations."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Rebuild KB because running fol_fc_ask would add new facts to the KB\n",
"crime_kb = FolKB(clauses)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{v_5: x, x: Nono}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"crime_kb.ask(expr('Hostile(x)'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may notice some new variables in the substitution. They are introduced to standardize the variable names to prevent naming problems as discussed in the [Unification section](#Unification)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Appendix: The Implementation of `|'==>'|`\n",
"Consider the `Expr` formed by this syntax:"
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"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: "
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In other words, there are two applications of or-operators. Here's the first one:"
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"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",
"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`:"
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"partial = PartialExpr('==>', P) \n",
"partial | ~Q"
]
},
{
"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,"
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"outputs": [
{
"data": {
"text/plain": [
"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",
"outputs": [
{
"data": {
"text/plain": [
"(((P & Q) ==> P) | Q)"
]
},
"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",
"outputs": [
{
"data": {
"text/plain": [
"((P & Q) ==> (P | Q))"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(P & Q) |'==>'| (P | Q)"
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples"
]
},
{
"cell_type": "code",
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
"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"
}
],
"from notebook import Canvas_fol_bc_ask\n",
"canvas_bc_ask = Canvas_fol_bc_ask('canvas_bc_ask', crime_kb, expr('Criminal(x)'))"
]
},
"metadata": {
"collapsed": true
},
"This notebook by [Chirag Vartak](https://github.com/chiragvartak) and [Peter Norvig](https://github.com/norvig).\n",
}
],
"metadata": {
"kernelspec": {
"language": "python",
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",