14.4 Meta-goals (Ad)


On this page, we look at two meta-goals. A meta-goal is a goal that has side-effects apart from merely succeeding or failing.

The two goals we treat are:

  1. / (cut)
  2. write

/ (cut)


The cut/0 goal is a quite important goal in Prolog. It is used to stop backtracking. If the Prolog system meets a cut operator as one of the subgoals of a clause, the effect is the following:

  1. Any attempt to backtrack to the left of the cut operator in the same call to the clause will fail.
  2. Clauses in the same predicate (i.e., with the same functor and arity), which are below the clause with an executed cut operator, cannot be reached. They are not called even if the overall goal of the clause succeeds. Normally, backtracking would have gone on to calling later clauses, but the cut operator entails that they are not called.


For example, consider the following, rather artificial, program:

aa(X) :- ba(X), /, ca(X).
aa(X) :- da(X).




It is available in the AAU directory as "Cut1.plgCG".

It says that aa(X) succeeds in two cases:

  1. Either, if ba(X) succeeds, and then ca(X) succeeds (with the cut operator in between, which always succeeds).
  2. Or, if da(X) succeeds.

ba(X) and ca(X) succeed for X=1, and X=3, while da(X) succeeds for X=2.

Now let us see what the effect of the cut is. If we ask:

?- aa(X).

we get the answer:

{X = 1}

Normally, without the cut, we would have expected the following answers:

{X = 1}
{X = 3}
{X = 2}

This is because backtracking would have found all solutions.

Now, however, the cut does two things:

  1. It prevents ba(X) to be resatisfied with

    {X = 3}

    (rule 1 above)

  2. It prevents the clause

    aa(X) :- da(X)

    from being called (rule 2 above).


The write/1 goal takes one argument, which can be any kind of term (i.e., any kind of Prolog+CG data). It always succeeds, and it writes the argument to the screen.

For example, consider the following program:

yeehaaa(X) :- eq(X, "Texas"),
              write("Yeehaa!  Howdy y'all.").

If we then ask:

?- yeehaaa("Texas").

the answer will be:

"Yeehaa!  Howdy y'all."

As was mentioned, you can write any kind of Prolog+CG data, including CGs.

You can also use the write/1 goal to debug your programs, by inserting it in important places to see whether Prolog executes the subgoals before it.

Prev: 14.3 List goals (Ad)
Up: 14 Prolog+CG (Ad)
Next: 14.5 Next (Ad)