[ed186b]: doc / source / logic_tricks.txt Maximize Restore History

Download this file

logic_tricks.txt    140 lines (106 with data), 4.8 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
.. $Id$
..
.. Copyright Š 2008 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: Logic Tricks
page-description:
How to accomplish different logic operations with rules.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
/restindex
===================================
Logic Tricks
===================================
This describes how to accomplish different logic operations with rules.
X OR Y
===========
For example: check for text_x($arg) OR test_y($arg).
Create a separate subgoal for the OR case, let's call it ``or_goal``. Then use
a separate rule for each OR clause::
or_goal_test_x:
use or_goal($arg)
when
test_x($arg)
or_goal_test_y:
use or_goal($arg)
when
test_y($arg)
Then you'd use ``or_goal($arg)`` where you wanted the *OR* clause.
This can go on for any number of OR-ed clauses by just adding more rules for
``or_goal``.
IF X THEN Y
=============
For example: if test_x($arg) then test_y($arg).
What this means is that test_y($arg) must be true if test_x($arg) is true.
But if test_x($arg) is not true, then test_y($arg) doesn't matter and ``if
test_x($arg) then test_y($arg)`` is true.
Create a new subgoal, let's call it ``if_x_then_y``. Use two rules::
if_x_then_y_if:
use if_x_then_y($arg)
when
test_x($arg) # if this fails, the next rule will be used
special.claim_goal() # don't use any other rules for if_x_then_y
test_y($arg) # if this fails, the next rule will not be used, so if_x_then_y will fail.
if_x_then_y_else:
use if_x_then_y($_) # this rule is only used if test_x($arg) fails
NOT X
==========================
For example: not test_x($arg)
You have to be careful about *not* logic because it can mean different things.
In this example, if we try ``not test_x($arg)`` and ``$arg`` is unbound what
should happen? Should it generate all of the different values for ``$arg``
for which ``test_x($arg)`` fails? This generally isn't very practical!
The other interpretation is that there is **no** possible binding for ``$arg``
that makes ``test_x($arg)`` true. In this example, ``not test_x($arg)`` would
then be false, because there is some ``$arg`` value that makes ``test_x($arg)``
true.
This second interpretation can be implemented by creating a new subgoal, let's
say ``not_test_x`` and using a pair of rules::
not_test_x_fail:
use not_test_x($arg)
when
test_x($arg) # if there is any way for test_x($arg) to be true
special.claim_goal() # don't use any other rules for not_test_x
check False # and cause this rule to fail, which causes the not_test_x goal to fail
not_test_x_success:
use not_test_x($_) # this rule only runs if test_x($arg) fails, above
EXISTS X SUCH THAT Y
===============================================================
For example: there exists a ``$x`` from ``generate_x($x)``, where
``test_y($x)`` is true.
This doesn't require a new subgoal. You can just use the following two
clauses in place of the ``exists`` test::
generate_x($x)
test_y($x) # if this fails, it will backtrack and try the next value from generate_x($x)
FOR ALL X, Y
===============================================================
For example: for all $x from generate_x($x), test_y($x) is true
Use the forall clause::
forall
generate_x($x)
require
test_y($x)
Note that all pattern variable bindings done in the forall clause are undone
before the next clause is run.
This always fails on backtracking.