最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

PROLOG predicate returns false and negation does as well (using not) - Stack Overflow

matteradmin8PV0评论

I have the following rules set up.

pokemonOfType(charmander, fire).
pokemonOfType(staryu, water).

typeWins(water, fire). % this should not affect the outcome,
                       % but who knows

pokemonWins(Winner, Loser) :-
    pokemonOfType(Winner, TypeWinner), 
    pokemonOfType(Loser, TypeLoser),
    typeWins(TypeWinner, TypeLoser).

If I run ?-pokemonWins(charmander, staryu). I get the following output (false). If I run ?-not(pokemonWins(charmander, staryu)). I also get false!

What is going on?

I have the following rules set up.

pokemonOfType(charmander, fire).
pokemonOfType(staryu, water).

typeWins(water, fire). % this should not affect the outcome,
                       % but who knows

pokemonWins(Winner, Loser) :-
    pokemonOfType(Winner, TypeWinner), 
    pokemonOfType(Loser, TypeLoser),
    typeWins(TypeWinner, TypeLoser).

If I run ?-pokemonWins(charmander, staryu). I get the following output (false). If I run ?-not(pokemonWins(charmander, staryu)). I also get false!

What is going on?

Share Improve this question edited Nov 18, 2024 at 20:36 TessellatingHeckler 29.1k4 gold badges54 silver badges93 bronze badges asked Nov 18, 2024 at 17:28 ChuweeChuwee 11 bronze badge 2
  • not(pokemonWins(charmander, staryu)). succeeds, i.e. shows true, in swi-prolog 9.2.8. – brebs Commented Nov 18, 2024 at 18:09
  • Try using the built-in negation. Instead of not(pokemonWins...) write: \+ pokemonWins(...) – TA_intern Commented Nov 19, 2024 at 7:22
Add a comment  | 

1 Answer 1

Reset to default 0
?- pokemonWins(charmander, staryu)
false

?- not(pokemonWins(charmander, staryu))
true

should work. So, it's likely something else.

But, in Prolog, not is implemented as "negation as failure". not(X) succeeds if X fails, and fails if X succeeds. not doesn't mean "logical not" in the classical sense.

And it's considered deprecated due to such confusion and should not be used in a new code according to the SWI prolog. https://www.swi-prolog./pldoc/man?predicate=not/1

Post a comment

comment list (0)

  1. No comments so far