|
The Prolog Tutorial: More Built-in Predicates |
|
At this stage you should be able to load facts into the database. Understand what Prolog does behind the scenes. To write recursive procedures, esp. to carry out basic list processing
|
|
The aims of this tutorial
More Built-in Predicates These are just a few more predicates that you may find useful in constructing simple Prolog procedures. atomic - atomic( X) succeeds if X is currently instantiated to an atom, an integer or a floating point number. bagof - bagof( X, P, L) will produce a list L of all the objects such that a goal P is satisfied. fail - fail will always fail. functor - functor( Predicate, Functor, Arity) will when give a predicate ( with its arguments ) return the functor name, Functor, and the number of arguments, Arity. name - name( A, L ) is true if L is the list of ASCII codes of the characters in A. nonvar - nonvar( X ) is true is X is a term other than a variable. tab - tab( X ) will cause X spaces to be output. var - var( X ) is true is X is currently an uninstantiated variable.
Making the cut We have already seen how Prolog carries out what is know as backtracking ( see Tutorial ? ). Prolog has a built-in facility known as the ‘cut’ to help control backtracking. Automatic backtracking is useful, it is a case of the Prolog search mechanism doing work for the programmer. The downside is that uncontrolled backtracking can make programs inefficient. The cut is represented in Prolog as an exclamation mark, !. There are two types of cut in Prolog: the red cut and the green cut. A green cut is used where the programmer introduces a cut to avoid unnecessary processing. Thus making a procedure more efficient. A red cut is a cut introduced to make procedure act in a different way. These type of cuts generally change the declarative meaning of a program and should be avoided. How does a cut work? When a cut is encountered it will always succeed. Behind the scenes all the markers for the sub-goals are removed. We discussed these markers when talking about the Prolog search mechanism in an early tutorial. When backtracking occurs and the cut is reached no further backtracking can occur as the markers have been removed.
Building lists When carrying out list processing we may often want to return a new list. Consider the following procedure from the last tutorial- reverse( List, Reversed_list ) :- aux_reverse( List, [ ], Reversed_list ). aux_reverse( [ ], L, L ). aux_reverse( [ Head | Tail ], L2, L3 ) :- aux_reverse( Tail, [ Head | L2 ], L3 ). The procedure aux_reverse/3 is a good example of how we can take a list as input and return a new list after processing it. But the example we are about to consider is more typical. Lets write a procedure that will take a list of numbers and add one to every element in the list. We can call it add_one/2. add_one( [], [] ) . add_one( [ Head | Tail ], [ Head2 | Tail2 ) :- Head2 is Head + 1, add_one( Tail, Tail2 ). This procedure isn’t perfect it doesn’t check the input to make sure the elements in the list are all numbers but note what it does do. Almost everytime the procedure is called it takes the first element of the input and adds it to the output, the list we are creating. The case – add_one( [ ], [ ] ) – does two things it checks to see that the input list is empty and when it is it instantiates Tail2 to the empty list. So as the procedure exits the output list is built.
Failing to repeat yourself So far we have seen that generally we use recursion to repeat an action in Prolog. However, we can also use backtracking to repeat an action. Suppose we have following procedure. This will list all male/1 facts in a database. all_males :- male( X ), write( male( X ) ), nl, fail. all_males. The built-in predicate fail/0 will always fail and thus forcing backtracking. So Prolog will attempt to backtrack to male( X ) and attempt to resolve it. Eventually, we get to a point where no other values can be found and Prolog moves onto the last line above, which will always be true.
What you should have learned from this tutorial -
|
|
Author - Jer Hayes – 2000 |