Foundations of Programming Paradigms 2000

 8  Programming With Prolog


What's in This Set of Notes ?


8.1 Meaning of Prolog Programs

Declarative Meaning

Procedural Meaning


8.2 Programming with Relations

Example


Example

uses(dwight, compiler, sun).
uses(dwight, compiler, pc).
uses(dwight, compiler, mac).
uses(dwight, editor, sun).
uses(anna, editor, mac).
uses(jane, database, pc).

needs(compiler,128).
needs(editor, 512).
needs(database, 8192).

What program needs more than 256k memory?
 
?needs(Program, Memory), Memory > 256.


What program does each person use?
 

?uses(Person, Program, Machine).


Which programs are used by two different people on the same machine?
 

?uses(Person1, Program, Machine), uses(Person2, Program, Machine), Person1 \= Person2.


What people use what programs on what machines with what memory? // A Relational Join
 

?uses(Person, Program, Machine), needs(Program, Memory).


What programs do both Anna and Jane use? // A Relational Intersection
 

?uses(anna, Program, Machine), uses(jane, Program, Machine).

8.3 Basic Syntax 

Syntax

Operators


8.4 Unification


Matching Rules

  1. Any uninstantiated variables matches any object, including another variable. Therefore, the object will be bound to a variable
  2. Integer or atoms only match with themselves
  3. Structures will match with other structures with the same functor and number of arguments with all arguments matching

Examples

 
Term 1  Term 2  Does Term 1 unify/match with Term 2?
policeman policeman yes
paper pencil no
1066 1066 yes
1206 1583 no
rides(clergyman bicycle) rides(clergyman, X) yes with X bound to bicycle
a(b,c,d(e,F,g(h,i,J)) a(B,c,d(E,f,g(H,i,j)) yes with B bound to b, E bound to e, F bound to f, H, bound to h, and J bound to j

 
8.5 Backtracking


8.6 Lists


Programming with Lists


Member relation member(X, Y). Is X a member of the list Y
 

member(X, [X|_]).
member(X,[_|Y]) :- member(X,Y).



8.7 Summary