More about Terms

The Anonymous variable

The single underscore variable is called the anonymous variable. It is used when the value of the variable is not cared about.

Classifying terms

It is often useful to determine the nature of a term, especially of an instantiated variable.

Unification and instantiation

Unification is the operation that compares two expressions, and, if possible, instantiates (or binds) variables in the expressions so that the two expressions become identical. Before instantiation a variable is unbound, but once instantiated the variable is completely replaced by the term it has been instantiated to.

In unifications, the instantiation of variables is as general as possible, to create a common instance of the two expressions. For example, p(X,a,Z) and p(A,B,B). The most common instance is p(X,a,a), but p(a,a,a) is also a common instance. The rules for unification are :

Unification is used between query predicates and clause heads.

The = predicate determines if two expressions are unifiable, and \= the converse.

Note that the following is not allowed, and will not work. The reason is that it is second order logic.
?-lecturer(geoff,ai) = Job(geoff,ai).
yes
Job = lecturer
== and \==

Tutorial

  1. Write a set of clauses with head predicate symbol my_unify, which take two arguments. If the arguments are both atoms, both integers or both unbound variables, attempt to unify them using the = predicate, otherwise my_unify must fail. Answer
  2. Create an example where the lack of "occurs check" in unification causes a problem. Answer
  3. Integers may be represented by functions in Prolog. Zero is represented by 0, one by s(0), two by s(s(0)), etc. Write Prolog clauses to add, subtract, multiply and divide numbers in this representation. For example
    ?- add(s(0),s(s(0)),X).
    yes
    X = s(s(s(0)))
         
    Answer


Arithmetic

Evaluation is caused by the is predicate The arithmetic predicates < > >= =< can be used to compare evaluable arithmetic expressions.

Tutorial

Write clauses for sum and prod. sum takes three arguments, the first two being operands, the last being the sum of the first two. Answer


Lists

Lists as functions using dot notation. List notation, with head and tail String notation for lists. Examples of lists, and queries on lists. An Application: Counting user words
%-------------------------------------------------------------
%----Counts the number of words in some text, and how often
%----each occurs.
%----A list of the form [word(Word,Count), ...] is returned.
count_words(Words):-
%----Read the first word
    read_word(Word),
%----Build the list of words
    count_words(Word,[],Words).
%-------------------------------------------------------------
%----Adds words to a list until exit is reached
%----At exit return the current list
count_words(exit,Words,Words).

%----Otherwise add the word too the list.
count_words(Word,OldWords,Words):-
%----Add the word in the list
    add_word(Word,OldWords,NewWords),
%----Get another word
    read_word(NextWord),
%----Loop
    count_words(NextWord,NewWords,Words).
%-------------------------------------------------------------
%----Adds a word to a list.
%----If the first word in the list already, increment the
%----count
add_word(Word,OldWords,[word(Word,NewCount)|OtherWords]):-
    select(word(Word,OldCount),OldWords,OtherWords),
    NewCount is OldCount + 1.

%----If not the first word, then move on down the list
add_word(Word,OldWords,[word(Word,1)|OldWords]).
%-------------------------------------------------------------
%----Read a word from stdin
read_word(Word):-
%----Prompt user
    write('Enter word : '),
    read(Word).
%-------------------------------------------------------------
 

Tutorial

  1. Write clauses for mega_append, which has a list of lists as its first argument and their concatenation (all appended) as the second argument. Answer
  2. Write a program to solve the Towers of Hanoi problem. Your program must generate a list of functions of the form move(FromPeg,ToPeg), returned in the last argument of hanoi(FromPeg,ToPeg,SparePeg,NumberOfDisks,ListOfMoves). Answer


Manipulating Expressions

functor(Term,Functor,Arity) arg(Position,Term,Argument) Term =.. List (univ) name(Atom,List)

Tutorial

  1. Write a program that flattens an expression into a list of atoms, using the =.. predicate, for example:
    ?- flatten(p(X,f(a,b),g(g(g(a)))),Flat).
    
    yes
    Flat = [p, X, f, a, b, g, g, g, a]
  2. Write a program that generates a sequence of new atoms.