Rule fails right after succeding

12 views
Skip to first unread message

Mr.SpOOn

unread,
Oct 29, 2009, 8:01:14 AM10/29/09
to py...@googlegroups.com
Hi,
my project is growing really fast in these days :D

Till today I just used "print" to debug the application. Now I'm
starting to use the trace method.

So, I have these rules:

interval_from_type
use interval($type, $name, $degree, $size)
when
$tuple_type = tuple($type)
type($tuple_type, $modifier, $degree)
note_basics.interval_modifier($modifier, $modifier_value)
note_basics.natural_interval($degree, $degree_value)
$size = $degree_value + $modifier_value
$name = print_interval_name($size, $degree)

dominant_7
use type(('7',), '', 7)
maj_7
use type(('m', 'a', 'j', '7'), 'maj', 7)

and in the file note_basics:
#interval_modifier('', 0) <---
interval_modifier('#', 1)
interval_modifier('b', -1)
interval_modifier('maj', 1)

The maj_7 rules works fine. When I try to prove interval with just
'7', so that it should use the dominant_7 rule, this happens:

intervals.interval_from_type('7', '$ans_0', '$ans_1', '$ans_2')
intervals.dominant_7(('7',), '$modifier', '$ans_1')
intervals.dominant_7 succeeded with (('7',), '', 7)
intervals.dominant_7 failed

If I uncomment the interval_modifier fact in note_basics it works fine.
Now, what I don't understand is why is the dominant_7 rule to fail. It
says it succeded, so the problem should just exist with
interval_modifier. I'm sure I'm wrong, but I don't why.

Then, I don't really like having this assertion: interval_modifier('',
0), but I couldn't find a better way. Any suggestion?

Thanks again,
Carlo

Bruce Frederiksen

unread,
Oct 30, 2009, 10:25:01 AM10/30/09
to py...@googlegroups.com
It sounds like there are two things that need to be cleared up, and they are related.

When pyke tries to prove a list of goals in the "when" clause of a backward-chaining rule, it uses a procedure called backtracking.  What it was doing with interval_modifier('', 0) commented out was first finding a solution to "type('7', $modifier, $degree)".  This succeeds with the dominant_7 rule by setting $modifier to '' and $degree to 0.

Next pyke has to prove "note_basics.interval_modifier('', $modifier_value)".  The first argument is '' because $modifier has been set to '' by the use of the dominant_7 rule to prove the line above.  Because you commented out interval_modifier('', 0), pyke is unable to prove this (because '' doesn't match any of the other first arguments for interval_modifier).  So the proof for this line fails, and pyke backtracks back to the line above (the "type('7', $modifier, $degree)" line) to see if there is another solution to it.  The dominant_7 rule does not produce any other solutions, so that rule now fails (being tried a second time).  All of the other "type" rules fail to match the '7', so pyke can't find any other solutions to "type('7', $modifier, $degree)" and backtracks again to the line above that (the "$tuple_type = tuple($type)" line).  This kind of python premise always fails on backtracking (this is explained at the top of this page).  When pyke tries to backup another line, it discovers that it is at the start of the "interval_from_type" rule, and so that rule fails to find an answer to the "interval('7', $name, $degree, $size)" goal.

So the two problems are:
  1. understanding that pyke will go back to a rule on backtracking, and a rule that may have succeeded the first time, may fail the second time (or third, or fourth, etc).  You might also read the explanation on backward-chaining again.  This is very different than the execution model of standard programming languages (like Python), and may be worth reading again now that you've got more experience using pyke.  This explains seeing dominant_7 succeed, followed by dominant_7 failed in the trace.
  2. forgetting that you need to tell pyke what the interval_modifier is for all $modifiers, including the '' $modifier used in the dominant_7 rule.
I hope that the links that I have provided above will help.  I am sure that as you continue to wrestle with this, you will come to better understand how the inferencing works.  It may also help to look at the family_relation example again and try tracing those rules too.

-Bruce
Reply all
Reply to author
Forward
0 new messages