Prolog programming
Topics:
First try to play with following examples. Read source code and try to understand it. Then read the materials below.
(These materials is my notes from book "Programming in Prolog")
Examples:
play with simple arithmatic operation in prolog command window: Arithmetic predicates
some arithmatic operation source code(some example takes simple read and write functions): Absolute value Add two numbers Double a number Artihmetic mean Circle: area & circumference(this example contains a minor error, can you find it and fix the problem?)
relation arithmatic operation code: Form a triangle?
arithmatic operation with recursion: Power Factorial(factorial second source code has some problem. modify it and make it works)
first touch with list: print all members in the list(need a minor modification)
more simple list examples: member in the list(actually, member is a default predicate. So, for this example, just read the source code. If you really want to make code working, just modified name "member" to be "mymember") The vowels in a word Length of list(remove member predicate from source code)
1. Syntax
- constants: atom, integer
- variables:
- Variables look like atoms, except they have names beginning with a capital letter or an underline sign "_".
- A variable should be thought of as standing for some object that we may not be able to name.
- Sometimes one needs to use a variable, but its name will never be used. For example, if we want to find out if anyone likes John, but we do not need to know just who, we can use the "anonymous variables". The anonymous variable is written as a single underline character. Our example is written as:
- ?-likes(_, john).
- Structure
- example: instead of type in fact as: owns(john, wuthering_heights), we can use:
- owns(john,book(wuthering_heights, bronte)).
- owns(john,book(wuthering_heights,author(emily,bronte))).
- We can ask questions like:
- -?owns(john, book(_,auther(_,bronte))). Anonymous variables do not "share" with each other.
2. Equality and Matching
- When an attempt is made to satisfy the goal: ?-X=Y. Prolog attemps to match X and Y.
- Given a goal of the form X=Y, where X and Y are any two terms which are permitted to contain uninstantiated variables, the rules for deciding whether X and Y are equal are as follows:
- If X is an uninstantiated variables, and if Y is instantiated to any term, then X and Y are equal. Also X will become instantiated to whatever Y is. For example, the following question succeeds, causing X to be instantiated to the structure rides(clergyman,bicycle):
- ?-rides(clergyman,bicycle)=X.
- Integers and atoms are always equal to themselves. For example, the following goals have the behaviour shown:
- policeman = policeman succeeds
- paper = pencil fails
- 1066 = 1066 succeeds
- 1206 = 1583 fails
- Two structures are equal if they have the same functor and number of components, and all the corresponding components are equal. For example, the following goal succeeds, and causes X to be instantiated to bicycle:
- ?-rides(clergyman,bicycle) = rides(clergyman, X).
- If we attempt to make two uninstantiated variables equal, this is just a special case of the first rule above. The goal succeeds, and the two variables share. If two variables share, then whenever one of them becomes instantiated to some term, the other one automatically is instantiated to the same term.
- try yourself: ?-a(b,C,d(e,F,g(h,i,J) = a(B,c,d(E,f,g(H,i,j))).
- ?-pilots(A,london) = pilots(london,paris).
- ?-point(X,Y,Z)=point(X1,Y1,Z1).
- ?-noun(alpha)=alpha
- ?-'vicar'=vicar
- ?-f(X,X)=f(a,b).
- ?-f(X,a(b,c))=f(Z,a(Z,c)).
3. Arithmetic
- relation operator:
- X = Y
- X \= Y
- X < Y
- X > Y
- X =< Y
- X >= Y
- Arithmetic operator:
- example: abs(X,Y):- Y is -X.
- The "is" operator is new. The "is" operator is an infix operator. Its right-hand argument is a term which is interpreted as an arithmetic expression. To satisfy an "is", Prolog first evaluates its right-hand argument according to the rules of arithmetic.
- Supported operator:
- X+Y
- X-Y
- X*Y
- X/Y
- X mod Y
4. Basic about lists(a simple list tutorial)
- The list is a very common data structure. It's an ordered sequence of elements that can have any length. "Ordered" means that the order of the elements in the sequence matters. The "elements" of a list may be any terms - constants, variables, structures, and other lists.
- Lists can be represented as a special kind of tree. A list is either an empty list, have no elements, or it is a structure that has two components: the head and tail. The end of a list is customarily represented as a tail that is set to the empty list. Thus, the list consisting of a one element "a" is " .
- list [a, V1, b, [X, Y]] can be represented as:
- The list with head X and tail Y is written [X|Y].
- example:
- p([1,2,3]).
- p([the, cat, sat, [on, the mat]]).
- Then, if ask questions: ?-p([X|Y]). Prolog will give you answer:
- X=1,Y=[2,3];
- X=the Y=[cat, sat, [on, the mat]]
- Read the those list examples again.
- When combined with recursion, be careful about "infinite loop".
- Example: person(X):-person(Y), mother(X,Y).
- Be careful about "circular" definition.
- Example: parent(X,Y):-child(Y,X). child(A,B):-parent(B,A).
- Sometimes, the sequence of rules matters.
- Example:
- islist([A|B]):-islist(B).
- islist([]).
- This example will fail if you ask ?-islist(X). The right sequence should be:
- islist([]).
- islist([A|B]):-islist(B).
Assignment3(bonus one!!)
Assignments need to check out by TA