C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan
From Last Time: Prolog programs are made up of terms, which can be constants, variables, or structures. A constant is either an atom (foo, who, “Sue”) or a non-negative integer. A variable is a series of letter that begins with a capital letter (Alf, Result). A structure is a predicate with zero or more arguments (speaks(Who,russian)). The number of arguments is called the structure’s arity.
From Last Time: A fact is a term followed by a period (.) as in: speaks(allen, russian). A rule is a term followed by :- and a series of terms sepparated by commas and ended in a period: parent(A,B) :- father(A,B). parent(A,B) :- mother(A,B). Rules are interpreted as “only if” assertions and the commas are treated as logical “and” operators. A Prolog program is a series of facts and rules.
Lists: The basic Prolog data structure is the list, a series of terms separated by commas and enclosed in brackets: [this, is, a, list] Lists can contain “don’t care” terms designated by the underscore character (_). The first element of a list, the head, is distinguished from the remaining elements of the list like this: [X | Y]
List Functions: A Prolog function to do list concatenation is written recursively as follows: append([], X, X). append([H | T], Y, [H | Z]) :- append(T, Y, Z). The first line is the “base case” which says that an empty list concatenated with any other returns the other list. The recursive second case says “if Z is the result of concatenating lists T and Y then concatenating any new list [H | T] with Y yields [H | Z]”. The process goes something like this…
Partial Search Tree for append([english, russian], [spanish], L) Figure 9.5
List Functions: The following functions define prefix and suffix: prefix(X, Z) :- append(X, Y, Z). suffix(Y, Z) :- append(X, Y, Z). This function defines membership in a list: member(X, [X | _]). member(X, [_ | Y]) :- member(X, Y). Note the use of the don’t care terms; the first line says X is a member if it is the head of a list, the second line says that X is a member if it is a member of the tail of a list.
Solving Word Puzzles: Consider the following word puzzle: Baker, Cooper, Fletcher, Miller, and Smith live in a five story building. Baker doesn’t live on the 5 th floor and Cooper doesn’t live on the first floor. Fletcher doesn’t live on the top or bottom floor, and he is not on a floor adjacent to Smith or Cooper. Miller lives on some floor above Cooper. Who lives on what floors? In addition to the member function from before we will define: adjacent(X, Y) :- X =:= Y+1. adjacent(X, Y) :- X =:= Y-1.
Prolog Solution for the Building Problem Figure 9.12 To run the program type: building(X).
Cut, is, not and assert: Inserting a cut (!) in the right-hand terms (sub- goals) of a rule will cause the right-hand side to not be examined if it has evaluated to true once. (See the bsort example in the book). The is operator can be used to force instantiation of a variable: X is Y+3. The not operator negates goal failure, that is not(P) is true when P is false. The assert function is used to add new facts and rules during execution: assert(mother(jane, joe)).
The Factorial Function in Prolog Figure 9.7 Note that we introduced the intermediate variable M in order to force the evaluation of N-1 before the recursive call.
Trace of Factorial (4) Figure 9.8
Symbolic Differentiation Rules Figure 9.9
Prolog Symbolic Differentiator Figure 9.10
Next time… Logic Programming Wrap Up