Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[05a128]: doc / source / overview / knowledge_bases / special.txt Maximize Restore History

Download this file

special.txt    116 lines (92 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
.. $Id$
..
.. Copyright Š 2007 Bruce Frederiksen
..
.. Permission is hereby granted, free of charge, to any person obtaining a copy
.. of this software and associated documentation files (the "Software"), to deal
.. in the Software without restriction, including without limitation the rights
.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
.. copies of the Software, and to permit persons to whom the Software is
.. furnished to do so, subject to the following conditions:
..
.. The above copyright notice and this permission notice shall be included in
.. all copies or substantial portions of the Software.
..
.. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
.. THE SOFTWARE.
restindex
crumb: Special
page-description:
Explanation of the special knowledge base.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
/restindex
=============================================
Special
=============================================
Other kinds of knowledge bases may be created that act in different ways
when they are asked to lookup facts or prove goals. Pyke includes one of
these named *special*. Each entity in this knowledge
base is a python function that does something "special" when run.
Currently, there is only one special function::
special.claim_goal()
This acts like the prolog ``cut`` operator.
In general there are multiple rules that might be used to try to prove any
goal. They are each tried in the order that they appear in the .krb file.
If one rule fails, the next rule is tried. The goal itself doesn't fail
until all of the rules for it have failed.
Example
===============
Suppose I want to translate a number, N, into the phrase "N dogs". I could
use the following rules::
one_dog:
use n_dogs(1, '1 dog')
n_dogs:
use n_dogs($n, $phrase)
when
$phrase = "%d dogs" % $n
The problem here is that both rules might be used with ``n`` is 1, but the
second rule isn't appropriate in this case. ``Special.claim_goal()`` may
be used to fix this, as follows::
one_dog:
use n_dogs(1, '1 dog')
when
special.claim_goal()
n_dogs:
use n_dogs($n, $phrase)
when
$phrase = "%d dogs" % $n
The ``special.claim_goal()`` prevents the second rule from being used when
``n`` is 1.
Explanation
=================================
When a rule executes ``special.claim_goal()`` in its ``when``
clause, none of the rest of the rules will be tried for that goal.
Thus, when ``special.claim_goal()`` is backtracked over, the goal fails
immediately without trying any more rules for it.
This ends up acting like an "else". You place it in the ``when`` clause
after the premises that show that this rule **must** be
the correct one to use. Then the subsequent rules will only be tried if
these premises fail, such that ``special.claim_goal()`` is never executed.
This means that you don't need to
add extra premises in each subsequent rule to make sure that these premises
have **not** occurred.
Without the ``special.claim_goal()``, I would have to write::
one_dog:
use n_dogs(1, '1 dog')
n_dogs:
use n_dogs($n, $phrase)
when
check $n != 1
$phrase = "%d dogs" % $n
This is a simple example where it is easy to add the check in the second
rule. But in general, it is difficult to prove that something is **not**
true, making ``special.claim_goal()`` indispensable.