Question

I am studying DCG grammar and parse tree using the Ivan Bratko book: Programming fro Artificial Intelligence.

On the book I found the following example that show a DCG grammar that also generate a parse tree and a meaning/2 predicate that is used to mean the position after some moves.

This is the code:

move(move(Step)) --> step(Step).
move(move(Step, Move)) --> step(Step), move(Move).

step(step(up)) --> [up].
step(step(down)) --> [down].

meaning(move(Step, Move), Dist):- meaning(Step, D1),
                      meaning(Move, D2),
                      Dist is (D1 + D2).

meaning(step(Step), Dist):- meaning(Step, Dist).

meaning(step(up), 1).

meaning(step(down), -1).

On the book show the following query with the following output:

move(Tree, [up,up,down, up, up], []), meaning(Tree, Dist).
Tree = move(step(up), move(step(up), move(step(down), move(step(up), move(step(up)))))),
Dist = 3

The problem is that if I try to execute the previous query, I always obtain FALSE (I obtain false with whatever query...)

?- move(Tree, [up,up,down, up, up], []), meaning(Tree, Dist).
false.

Why? What is wrong? What ma I missing?

Was it helpful?

Solution

It seems that the second clause of meaning/2 is wrong. It should be:

meaning(move(Step), Dist):- meaning(Step, Dist).

To pinpoint this error you could have tried with a single move:

?- move(Tree, [up], []).
Tree = move(step(up))

So there has to be a base case in meaning/2 that deals with a single move, and inspecting your code it should be precisely the second clause.

In fact, every list of moves ends with one of this kind of terms, move(step(X)) with X in {up, down}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top