# Prolog 'fail' predicate.

8 replies to this topic

### #1everlonggg

New Member

• Members
• 5 posts

Posted 07 December 2008 - 01:45 AM

The fail predicate seems a bit unpredictable under certain cases to me. Take the following example.

Code:
a(1).
a(2).
b(3).
b(4).
go:- a(X), write(X).
go:- a(X), write(X), b(Y), write(Y).

Query this with ?- go, fail.

The result is 12134234. How does this make sense? Can somebody explain the logic behind that? Thanks.

### #2Mihail121

Senior Member

• Members
• 1059 posts

Posted 07 December 2008 - 09:14 AM

OMG, it's not the result, it's just SWI printing stuff as it moves along the search tree :). Try it without 'fail' and check out the 6 models you get.

### #3everlonggg

New Member

• Members
• 5 posts

Posted 07 December 2008 - 04:27 PM

Mihail121 said:

OMG, it's not the result, it's just SWI printing stuff as it moves along the search tree :). Try it without 'fail' and check out the 6 models you get.

I realize it's just printing stuff as it moves along. However, the way it reaches things is beyond my understanding. Namely, when it reaches the single "4" instead of "2, 4". Any explanation of this?

### #4.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 07 December 2008 - 05:09 PM

It's very logical though. There are two go's, one with a single a(X), and one with an a(X) followed by a b(Y).

So the first two digits, 1 and 2, are simply all combinations that can be made with a. The following digits, 134234, are from the second go, which are all combinations with a and b combined. First it combines a(1) with b(Y), resulting in 134, then it does the same for a(2), resulting in 234.
-
Currently working on: the 3D engine for Tomb Raider.

### #5everlonggg

New Member

• Members
• 5 posts

Posted 07 December 2008 - 05:26 PM

.oisyn said:

The following digits, 134234, are from the second go, which are all combinations with a and b combined. First it combines a(1) with b(Y), resulting in 134, then it does the same for a(2), resulting in 234.

Would this not work in the manner of "13", followed by "24"? This is the approach I imagine it should have taken. I apparently misunderstand something in the way prolog processes these

### #6.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 07 December 2008 - 05:54 PM

No, because there are four possible combinations: a(1)b(3), a(1)b(4), a(2)b(3) and a(2)b(4). So for every a(X), it needs to check every b(Y)
-
Currently working on: the 3D engine for Tomb Raider.

### #7everlonggg

New Member

• Members
• 5 posts

Posted 07 December 2008 - 05:56 PM

Oh! So does the fail predicate not make you start the whole rule over again? It solely checks for the last call and backtracks that?

### #8.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 07 December 2008 - 06:06 PM

Note that I'm not a prolog guru whatsoever, but yes I think that's true . Which makes sense btw, because if it backtracked the whole searchtree up until the root, it has to start all over again.
-
Currently working on: the 3D engine for Tomb Raider.

### #9everlonggg

New Member

• Members
• 5 posts

Posted 07 December 2008 - 06:07 PM

Wow, that clears a LOT up. Thanks a bunch for your help!

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users