0

If we use fail in a predicate after the cut (!), it somehow still finds another solutoins using backtrack, but I think it shouldn't. Consider the next example in Visual Prolog

implement main
    open core, stdio

class facts
    m : (integer M).

clauses
    m(1).
    m(2).
    m(3).
    m(4).

class predicates
    p : () failure.
clauses
    p() :-
        m(X),
        !,
        write(X),
        nl,
        fail.

clauses
    run() :-
        p().
    run().

end implement main

goal
    console::runUtf8(main::run).

Its output is

1
2
3
4

However, I expected only 1, since, as written in the official doc,

Cut "!" removes all backtrack points created since the entrance to the current predicate, this means all backtrack points to subsequent clauses, plus backtrack points in predicate calls made in the current clause before the "!".

Moreover, if we run similar code in SWI-Prolog, its output will be 1 for p :- m(X), !, write(X), fail. and 1234 for p :- m(X), write(X), fail., as expected. What am I missing?

false
  • 10,264
  • 13
  • 101
  • 209
Viacheslav Zhukov
  • 1,130
  • 9
  • 15
  • Your assumption is correct. But what happens if you remove the second `run()` ? – David Tonhofer May 31 '20 at 18:06
  • Second `run` stands just to ensure that `run` has `procedure` type, i.e. it gives only one solution (it's created by default). And since `p()` always fails, we have to put second `run` there. – Viacheslav Zhukov Jun 01 '20 at 05:03
  • Also, if we change to `p() :- m(X), !, write(X), nl.` and `run() :- p(), fail.` - everything works as expected, it prints only `1`. So the problem is when `!` and `fail` are met in the same clause – Viacheslav Zhukov Jun 01 '20 at 05:05

0 Answers0