Stack Overflow question followup

It was suggested to move this question here for further discussion. this


Copied by EricGT from StackOverflow

So my question is basically the oppsite of this.

I have some code, something like this:

even(X) :- 0 is mod(X, 2).
odd_number(X) :-
    not(even(X)).
odd_number(X) :-
    write(X).

Is there any reason why, upon failing the first predicate, not to try the second function?

Although my code is more complicated, I will not show it because it is a homework. I just want to know if there would be any reason why upon returning false from the first predicate, the program doesn’t redo the function, but with the second predicate.

Note: The pattern matching stuff is not the problem. I am 100% sure my function matches at least one pattern written after the predicate which fails.

Details for better understanding of my issue:

This is how a tracing works on this program:

Call:odd_number(6)
 Call:not(even(6))
 Fail:not('10892dd7-de05-49c0-8057-1d7684c4ae68' : even(6))
 Redo:odd_number(6)
 Call:write(6)
6
 Exit:write(6)
 Exit:odd_number(6)
true

This is how the tracing looks in my program:

Call:odd_number(6)
 Call:not(even(6))
 Fail:not('10892dd7-de05-49c0-8057-1d7684c4ae68' : even(6))
 *Calls some weird function completely unrelated to this predicate*

Would you be so kind and summarize your question?

Please explain what you are doing and why? Like, why are you using write? Why does odd have two clauses? Why?

But to answer your question, to me it looks very much like there is a REDO and the second clause of odd_number/1 is executed. The output is right there, it says “6” on a line of its own, right?

PS: “I am not allowed to show you the code” makes this very difficult.

1 Like

The code I wrote on StackOverflow is not my actual code. Let me rephrase everything.

predicate([H | T], L) :-
    condition,
    not(some_condition),
    another_condition.
predicate([H | T], L) :-
    write('a condition returned false').

My problem is that if not(some_condition) returns false, the program never goes to the second predicate (i.e doesn’t write ‘a condition returned false’).

Weirdly enough, if condition returns false, the program goes to the second predicate. This is really strange. Also, this proves it has nothing to do with pattern matching, but it has something to do with not(some_condition) in particular.

And you are running this on Swish only?

I am running it on SWI-Prolog Online.

I cannot reproduce your problem.

trace, odd_number(4).
 Call:odd_number(4)
 Call:not(even(4))
 Fail:not('faba3e32-7b83-4a57-b5a3-b5adb66c0389' : even(4))
 Redo:odd_number(4)
 Call:write(4)
4
 Exit:write(4)

This is from SWISH with the program exactly as you have shown it.

If you don’t show code that I can copy-paste and run I cannot help you :frowning:

1 Like

I’m sorry, I’m just not allowed to show any code since it is an assignment, so I can’t copy-paste the actual code. I just wanted an idea why behaviour like that would be exhibited by a Prolog predicate. The odd_number code works perfectly, I just wanted to offer an insight into what structure my code has. I’ll give you the “Solution” because you tried to understand - however, I still can’t reproduce any example of my code and am not allowed to copy-paste my code.

That’s strange, it isn’t a solution and we even haven’t got a defined problem.

1 Like