learner
learner

Reputation: 41

What does ! symbol mean in prolog?

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),
   ! . 

Upvotes: 1

Views: 5992

Answers (2)

Olivier L. Applin
Olivier L. Applin

Reputation: 285

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.

Upvotes: 2

Jorj
Jorj

Reputation: 1301

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

Upvotes: 2

Related Questions