2

I really need to know what does this symbol mean when comes after and symbol... For example look at this code

glitter(yes) :-
   agent_location(X, Y),
   gold(X, Y),
   ! . 
false
  • 10,264
  • 13
  • 101
  • 209
learner
  • 51
  • 2
  • 10
  • 1
    You should read a prolog tutorial or get a good text book. There's much to be explained there. Prolog does *backtracking* to find solutions. That means it explores as many options as it can based upon your given facts and rules to achieve success. A cut (`!`) tells Prolog not to backtrack at that point. – lurker Jul 07 '18 at 17:15
  • 1
    See e.g. [this question](https://stackoverflow.com/questions/43861932/what-is-the-difference-in-execution-if-the-cut-is-present) for an explanation of the cut operator. When you look for [Prolog questions about cut](https://stackoverflow.com/search?q=%5Bprolog%5D+cut), you will quickly see that it is not easy to handle. – lambda.xy.x Jul 09 '18 at 14:59

2 Answers2

2

! symbol represents the cut. You can read more about cut here. Also, an example in prolog can be found here.

Jorj
  • 1,291
  • 1
  • 11
  • 32
  • 6
    Answers based solely upon links are not considered good form. You should explain the cut and then refer to the links for more detail. – lurker Jul 07 '18 at 17:16
2

To understand "the cut" ( ! ) you need to understand the backtracking process involved in Prolog evaluation of it's code. As you might know, with this piece of code, Prolog only know that the rule glitter() with input yes is verified if agent_location(X, Y) is itself verified when X and Y also verifies with gold(X, Y). In other word, agent_location and gold must be verified with the same arguments.

This means that Prolog will try to find specific value for X and Y so that everything can be verified. It will go down an evaluation path (evaluation tree) and try a value for X and see if it is possible to continue the evaluation with that same value for X. If it fails (let's say Prolog tried X = 0 in agent_location but X = 0 does not verify gold) it will go back and try with another rule for agent_location. That's when "the cut" comes is. If something after a ! fails, Prolog will never go check if it can solve with another rule with everything that is BEFORE a cut.

In this example, eventually, if everything fails for that particular glitter rule, Prolog will want to try another rule for verification. I suppose your original code had another rule specified for glitter and, if I 'm not mistaken, the cut at the end of the rule you show us means that if a rule that follows this one fail, Prolog will not go back and check if it can try new values to solve for glitter(yes).

It is usually used to gain efficiency in evaluation and prevent infinite loop.

Olivier L. Applin
  • 285
  • 1
  • 4
  • 15