[4ca57f]: doc / html / krb_syntax / bc_rule.html Maximize Restore History

Download this file

bc_rule.html    275 lines (262 with data), 18.7 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bc_rule Syntax</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../stylesheets/pyke.css" type="text/css" />
</head>
<body>
<table id="page-table">
<thead class="head">
<tr id="header1"><th id="header" colspan="3">
&nbsp;
</th></tr>
<tr id="header2">
<th id="crumb-left"></th>
<th id="crumb-line">
<div id="nav">
<ul>
<li><a href="../index.html">Home</a></li>
<li>&gt;</li>
<li><a href="index.html">KRB Syntax</a></li>
<li>&gt;</li>
<li>Bc_rule</li>
</ul>
</div>
</th>
<th id="crumb-right"></th>
</tr>
</thead>
<tbody id="body">
<tr id="body-tr">
<td id="left-nav">
<div id="left-nav-div">
<div class="title-nav"><a href="../index.html">Home</a></div><div class="nav-branch">
<div class="normal-nav"><a href="../overview/index.html">Overview</a></div>
<div class="normal-nav"><a href="../installing_pyke.html">Installing Pyke</a></div>
<div class="normal-nav"><a href="../using_pyke.html">Using Pyke</a></div>
<div class="title-nav"><a href="index.html">KRB Syntax</a></div><div class="nav-branch">
<div class="normal-nav"><a href="fc_rule.html">Fc_rule</a></div>
<div class="normal-nav"><a href="bc_rule.html">Bc_rule</a></div>
<div class="normal-nav"><a href="pattern.html">Pattern</a></div>
<div class="normal-nav"><a href="compound_premise.html">Compound Premise</a></div>
<div class="normal-nav"><a href="python_premise.html">Python Premise</a></div>
</div>
<div class="normal-nav"><a href="../examples.html">Examples</a></div>
<div class="normal-nav"><a href="../logic_tricks.html">Logic Tricks</a></div>
<div class="normal-nav"><a href="../PyCon2008-paper.html">PyCon 2008 Paper</a></div>
</div>
</div>
<div id="icons">
<div id="project-page">
<a href="http://sourceforge.net/projects/pyke/">Pyke Project Page</a>
</div>
Please Make a Donation:<br />
<a href="http://sourceforge.net/donate/index.php?group_id=207724">
<img src="http://images.sourceforge.net/images/project-support.jpg"
width="88" height="32" border="0"
alt="Support This Project" /> </a> <br /><br />
Hosted by: <br />
<!-- <a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=207724&amp;type=3" width="125" height="37" border="0" alt="SourceForge.net Logo" /></a> -->
<a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=207724&amp;type=4" width="125" height="37" border="0" alt="SourceForge.net Logo" /></a>
</div>
</td>
<td id="main-td">
<div id="main">
<a name="startcontent" id="startcontent"></a>
<div class="document" id="bc-rule-syntax">
<h1 class="title">Bc_rule Syntax</h1>
<div class="section">
<h2><a id="bc-rule" name="bc-rule">Bc_rule</a></h2>
<p><a class="reference" href="../overview/rules/backward_chaining.html">Backward-chaining</a> <a class="reference" href="../overview/rules/index.html">rules</a> have four parts:</p>
<ol class="arabic simple">
<li>A unique name.</li>
<li>A <tt class="docutils literal"><span class="pre">use</span></tt> clause.</li>
<li>An optional <tt class="docutils literal"><span class="pre">when</span></tt> clause.</li>
<li>An optional <tt class="docutils literal"><span class="pre">with</span></tt> clause.</li>
</ol>
<pre class="literal-block">
bc_rule ::= IDENTIFIER NL INDENT
use
[when]
[with]
DEINDENT
</pre>
<p>The <tt class="docutils literal"><span class="pre">IDENTIFIER</span></tt> is the unique name for this <a class="reference" href="../overview/rules/index.html">rule</a> and is used as the
corresponding python function name in the generated &lt;rb_name&gt;_bc.py file
and also for the python function name of the plan function (if any)
associated with the rule. This name will show up in stack traces
associated with exceptions raised during the inferencing or plan execution.</p>
</div>
<div class="section">
<h2><a id="use-clause" name="use-clause">Use Clause</a></h2>
<p>The <tt class="docutils literal"><span class="pre">use</span></tt> clause is the <strong>then</strong> part of the <a class="reference" href="../overview/rules/index.html">rule</a>. It identifies the
<em>goal</em> that this <a class="reference" href="../overview/rules/index.html">rule</a> is trying to prove.</p>
<pre class="literal-block">
use ::= 'use' IDENTIFIER '(' {pattern,} ')' NL
| 'use' IDENTIFIER '(' {pattern,} ')' 'taking' '(' &lt;python_arg_spec&gt; ')' NL
| 'use' IDENTIFIER '(' {pattern,} ')' NL
INDENT_TOK 'taking' '(' &lt;python_arg_spec&gt; ')' NL
DEINDENT_TOK
</pre>
<p>Notice that it uses a single <tt class="docutils literal"><span class="pre">IDENTIFIER</span></tt>. The <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> name is implied
as the <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">rule base category</a> name (the name of the root rule base) for the
<a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> containing this <a class="reference" href="../overview/rules/index.html">rule</a>.</p>
<div class="section">
<h3><a id="taking-clause" name="taking-clause">Taking Clause</a></h3>
<p>The <tt class="docutils literal"><span class="pre">use</span></tt> clause also defines parameters to the <a class="reference" href="../overview/plans.html">plan</a> function (if one is
used for this <a class="reference" href="../overview/rules/index.html">rule</a>) with the optional <tt class="docutils literal"><span class="pre">taking</span></tt> sub-clause.</p>
<p>The <em>python_arg_spec</em> is not parsed by pyke, but simply copied to the
output <a class="reference" href="../overview/plans.html">plan</a> function. Do <strong>not</strong> use <tt class="docutils literal"><span class="pre">$</span></tt> with these parameter names (or
default values).</p>
</div>
</div>
<div class="section">
<h2><a id="when-clause" name="when-clause">When Clause</a></h2>
<p>The <tt class="docutils literal"><span class="pre">when</span></tt> clause is the <strong>if</strong> part of the <a class="reference" href="../overview/rules/index.html">rule</a>. It defines the
premises that must be true for this rule to succeed.</p>
<p>If the <tt class="docutils literal"><span class="pre">when</span></tt> clause is omitted, the only
requirement for the <a class="reference" href="../overview/rules/index.html">rule</a> to succeed is that the <tt class="docutils literal"><span class="pre">use</span></tt> clause
<a class="reference" href="pattern.html">patterns</a> match the goal.</p>
<p>If the <tt class="docutils literal"><span class="pre">when</span></tt> clause is specified, the <a class="reference" href="../overview/rules/index.html">rule</a> succeeds for each
combination of true premises.</p>
<pre class="literal-block">
when ::= 'when' NL INDENT
{bc_premise NL}
DEINDENT
bc_premise ::= ['!'] [ name '.' ] name '(' {pattern,} ')' [ plan_spec ]
| compound_premise
| python_premise
name ::= IDENTIFIER
| '$'IDENTIFIER
</pre>
<p>Here are the links to the definitions for <a class="reference" href="pattern.html">pattern</a>, <a class="reference" href="compound_premise.html">compound_premise</a> and
<a class="reference" href="python_premise.html">python_premise</a>.</p>
<p>If the <em>bc_premise</em> includes the <tt class="docutils literal"><span class="pre">!</span></tt>, an AssertionError will be raised if the
premise fails on the first try. This can help in debugging.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">This does not apply when the premise fails on <a class="reference" href="../overview/rules/backward_chaining.html#backtracking">backtracking</a> (in which case
it has already succeeded at least once).</p>
</div>
<p>If a single <em>name</em> is used in the <em>bc_premise</em>,
the <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">rule base category</a> for the current <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> (the root rule base
name) is assumed.</p>
<p>If two <em>names</em> are used in the <em>bc_premise</em>, the first may name a <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">rule
base category</a> or a <a class="reference" href="../overview/knowledge_bases/fact_bases.html">fact base</a>.</p>
<p>If a <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">rule base category</a> name is used (or assumed),
the currently <a class="reference" href="../using_pyke.html#setting-up-each-case">active</a> <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> for that <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">category</a> is used to prove the
premise. Note that if the <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">rule base category</a> name is omitted, and
therefore assumed to be the current <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base's</a> <a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">rule base category</a>,
the current <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> does not have to be the <a class="reference" href="../using_pyke.html#setting-up-each-case">active</a> <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> for that
<a class="reference" href="../overview/knowledge_bases/rule_bases.html#rule-base-categories">category</a>. It could be the case that a derived <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> is the
<a class="reference" href="../using_pyke.html#setting-up-each-case">active</a> <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a>. In that case, the derived <a class="reference" href="../overview/knowledge_bases/rule_bases.html">rule base</a> is used to
prove the premise.</p>
<div class="section">
<h3><a id="plan-spec" name="plan-spec">Plan_spec</a></h3>
<p>A <em>plan_spec</em> is required for all premises that return subordinate <a class="reference" href="../overview/plans.html">plans</a>.
These show what this rule's <a class="reference" href="../overview/plans.html">plan</a> function should do with each of the
subordinate <a class="reference" href="../overview/plans.html">plan</a> functions.</p>
<p>Thus, a rule's <a class="reference" href="../overview/plans.html">plan</a> function is composed first of the collected
python_statements taken from its plan_specs (as described below), followed by
the python_statements within its <em>with</em> clause (if any). The inclusion of
any plan_spec with a python_statement will cause a <a class="reference" href="../overview/plans.html">plan</a> function to be
generated for this <a class="reference" href="../overview/rules/index.html">rule</a>, even if the <a class="reference" href="../overview/rules/index.html">rule</a> lacks a <tt class="docutils literal"><span class="pre">with</span></tt> clause.</p>
<pre class="literal-block">
plan_spec ::= [ 'step' NUMBER ] NL INDENT
{&lt;python_statement&gt; NL}
DEINDENT
| 'as' '$'IDENTIFIER NL
</pre>
<p>Within each python_statement, the subordinate <a class="reference" href="../overview/plans.html">plan</a> function is indicated by
<tt class="docutils literal"><span class="pre">$$</span></tt>.
The result of this function
may be assigned to a python variable, but not a <a class="reference" href="pattern.html#pattern-variable">pattern variable</a>
(<tt class="docutils literal"><span class="pre">$variable</span></tt>).
Parameters from the rule's <tt class="docutils literal"><span class="pre">taking</span></tt> clause may be passed on to the
subordinate <a class="reference" href="../overview/plans.html">plan</a> functions.</p>
<p>When multiple premises have python_statements in their <em>plan_specs</em>, the
python_statements in plan_specs <em>without</em> a <tt class="docutils literal"><span class="pre">step</span></tt> clause are executed first
in the order that they appear in this rule's <tt class="docutils literal"><span class="pre">when</span></tt> clause.</p>
<p>Then the python_statements in plan_specs
<em>with</em> a <tt class="docutils literal"><span class="pre">step</span></tt> clause are executed in ascending NUMBER sequence.
It is permissible for the NUMBER to be negative or a float.</p>
<p>If the <tt class="docutils literal"><span class="pre">as</span></tt> clause is used, the <a class="reference" href="../overview/plans.html">plan</a> function is bound to the
<a class="reference" href="pattern.html#pattern-variable">pattern variable</a> as a python function, but not automatically executed.
This allows you to call the function (or not) when and as many times as you
please.
The parameters
required are defined in the <tt class="docutils literal"><span class="pre">taking</span></tt> clause of the <a class="reference" href="../overview/rules/index.html">rule</a> used to prove the
premise.</p>
<p>Note that within a <tt class="docutils literal"><span class="pre">forall</span></tt>, <tt class="docutils literal"><span class="pre">require</span></tt> or <tt class="docutils literal"><span class="pre">notany</span></tt> clause, the only
<tt class="docutils literal"><span class="pre">plan_spec</span></tt> that may be used is the <tt class="docutils literal"><span class="pre">as</span></tt> clause.</p>
</div>
</div>
<div class="section">
<h2><a id="with-clause" name="with-clause">With Clause</a></h2>
<p>The <tt class="docutils literal"><span class="pre">with</span></tt> clause contains python
statements to include in the <a class="reference" href="../overview/plans.html">plan</a> produced by this <a class="reference" href="../overview/rules/index.html">rule</a>. These python
statements may
include <a class="reference" href="pattern.html#pattern-variable">pattern variables</a> whose values will be substituted into the
statements when finalizing the final top-level <a class="reference" href="../overview/plans.html">plan</a>.</p>
<pre class="literal-block">
with ::= 'with' NL INDENT
{&lt;python_statement&gt; NL}
DEINDENT
</pre>
<p>These <em>python_statements</em> are included in the rule's <a class="reference" href="../overview/plans.html">plan</a> function after
all of the automatic calls to the subordinate <a class="reference" href="../overview/plans.html">plan</a> functions from the
<em>plan_specs</em> in the <tt class="docutils literal"><span class="pre">when</span></tt> clause.</p>
<p>If the <tt class="docutils literal"><span class="pre">with</span></tt> clause is omitted, but the <tt class="docutils literal"><span class="pre">when</span></tt> clause has <em>plan_specs</em>
(excluding <em>as</em> specs), a <a class="reference" href="../overview/plans.html">plan</a> function is still generated for this
<a class="reference" href="../overview/rules/index.html">rule</a> so that the subordinate <a class="reference" href="../overview/plans.html">plan</a> functions are still called.</p>
<p>The <em>python_statements</em> are not parsed. They are simply scanned for <tt class="docutils literal"><span class="pre">$</span></tt>
<a class="reference" href="pattern.html#pattern-variable">pattern variables</a> that don't occur in string literals or comments.
The resulting <a class="reference" href="../overview/plans.html">plan</a> will substitute the values bound to these <a class="reference" href="pattern.html#pattern-variable">variables</a>
into the code.</p>
<p>All <a class="reference" href="pattern.html#pattern-variable">pattern variables</a> used in <em>python_statements</em> (both in the
<tt class="docutils literal"><span class="pre">plan_specs</span></tt> and the <tt class="docutils literal"><span class="pre">when</span></tt> clause) must be bound to a value. This
value is a constant value that never changes for this <a class="reference" href="../overview/plans.html">plan</a>.</p>
<p>But note that this <a class="reference" href="pattern.html#pattern-variable">pattern variable</a> substitution isn't done until
<em>after</em> the entire top-level goal has been proven. So it is permissible to use
unbound <a class="reference" href="pattern.html#pattern-variable">pattern variables</a>, so long as they will become bound to values by
subsequent <a class="reference" href="../overview/rules/index.html">rules</a> required to prove higher-level goals before the top-level
goal has been proven.</p>
</div>
</div>
<!-- <div id="return-to-top">
<a href="#">Return to Top</a>
</div>
-->
</div>
</td>
<td id="right-nav">
<div id="right-nav-div">
<h3>More:</h3>
<div class="right-item"><a href="fc_rule.html">Fc_rule Syntax</a><p>The syntax of a forward-chaining rule.</p>
</div>
<div class="right-item"><a href="bc_rule.html">Bc_rule Syntax</a><p>The syntax of a backward-chaining rule.</p>
</div>
<div class="right-item"><a href="pattern.html">Pattern Syntax</a><p>The syntax of a pattern used to match data values.</p>
</div>
<div class="right-item"><a href="compound_premise.html">Compound Premise Syntax</a><p>The syntax of compound premises.</p>
</div>
<div class="right-item"><a href="python_premise.html">Python Premise Syntax</a><p>The syntax of a python premise.</p>
</div>
</div>
</td>
</tr>
</tbody>
<tfoot id="foot">
<tr id="foot2">
<td id="copyright" colspan="3">
Copyright &copy; 2007 Bruce Frederiksen
</td>
</tr>
</tfoot>
</table>
<div id="last-modified">
Page last modified Sun, Jun 08 2008.
</div>
</body>
</html>