PROLOG PROGRAMMING - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

PROLOG PROGRAMMING

Description:

loves (peter,X), not dog (X) ... parent (mary, X), male (X). The first subgoal reduces to : mother(mary, X) ... the same variable names that you used. 15 ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 42
Provided by: lila7
Category:

less

Transcript and Presenter's Notes

Title: PROLOG PROGRAMMING


1
PROLOG PROGRAMMING
  • Lila Rao-Graham

2
Starting Prolog
  • On Windows
  • Click the Prolog icon in the start menu.
  • On Linux
  • Type pl at the prompt
  • The prompt 1?- will appear.
  • Make sure all files end in .pl
  • Loading
  • consult(filename).
  • OR
  • ltfilenamegt.
  • N.B. The terminating period.

3
Basics of Prolog Literals
  • A positive literal has the form
  • pred (term-1, , term-n).
  • Where each term is either
  • a constant e.g. mary, br54
  • a variable e.g.X, Person
  • the anonymous variable, _,
  • a predicate/relation
  • A literal is either a positive literal or a
    literal preceded by not.
  • Literals can be put together to make facts,
    queries and rules.

4
Examples of Literals
  • mother (mary, ed).
  • father (john, peter).
  • female (wife(peter)).
  • male (grandfather(wife(john))).
  • loves (peter, X).
  • not male (wife(peter)).
  • not male (wife(X)).

5
Basics of Prolog Rules or Clauses
  • Prolog programs consist of facts and rules.
  • Facts are positive literals.
  • Rules, or clauses, are of the form
  • pred (term-1, , term-n) -
  • literal-1,
  • literal-m.

6
Examples of Rules
  • female (X) -
  • mother (X,_).
  • brother (X,Y) -
  • father(Z,X),
  • father(Z,Y),
  • not(XY).
  • brother (X,Y) -
  • mother(Z,X),
  • mother(Z,Y),
  • not (XY).

7
Queries
  • A query is a set of literals, separated by
    commas.
  • loves (peter, claire).
  • loves (peter,X), not dog (X).
  • A query is used to determine whether something
    follows from the program.

8
Computation in Prolog
  • A Prolog query succeeds if it can be derived from
    the set of facts and rules that constitute the
    Prolog program.
  • If the query succeeds then the interpreter
    returns yes. If the query contains variables,
    then the interpreter returns a unification, i.e.
    a binding of variables to constants.
  • If the query does not succeed, the interpreter
    returns no.

9
A Simple Example
  • The program
  • mother (mary, ed).
  • father (john, ed).
  • parent (Parent, Child) -
  • mother (Parent, Child).
  • parent (Parent, Child) -
  • father (Parent, Child).
  • son (Child, Parent) -
  • parent (Parent, Child),
  • male (Child).
  • male (X) -
  • father (X, _).
  • male (ed).

10
Some Simple Queries
  • ?- mother (mary, ed).
  • Yes
  • ?- mother(mary, Child).
  • Child ed
  • Yes
  • ?- mother (X,Y).
  • X mary
  • Y ed
  • Yes
  • ?- mother (X, harry).
  • No

11
More Complex Queries
  • ?- son (X, mary).
  • This reduces to the subgoals
  • parent (mary, X), male (X).
  • The first subgoal reduces to mother(mary, X)
  • which succeeds with X ed.
  • This unification is passed on to the subgoal
    male(X) to give male(ed) which succeeds.
    Therefore, the whole query returns with the
    result
  • X ed.

12
Debugging Prolog Programs
  • ?- spy(P).
  • Spies predicate P.
  • You get information at
  • Entry Point
  • Exit Point
  • Re-entry Point
  • ?- nospy(P).
  • Turns spying off.
  • ?- trace.
  • Gives you a wall-paper trace i.e. a trace of
    every subgoal.
  • ?- notrace.
  • Turns this off.

13
  • At each entry or re-entry point, SWI-prolog gives
    you a number of options. Some examples
  • h help (shows list of options)
  • a abort
  • c creep (go to next point)
  • g goals
  • s skip (do not show any of the subgoals)
  • L listing of the predicate in question

14
  • Comments in Prolog are preceded by .
  • ?- listing(P).
  • Prints the clauses for P.
  • You need to be aware that every time Prolog
    retrieves a clause, variables in the clause are
    changed.
  • Thus, the listing does not produce the same
    variable names that you used.

15
List Notation
  • Prolog uses the following notation for lists
  • a,b,c,d
  • Internally this is stored as
  • (. a (. b (. c ( . d ))))
  • where is the empty list.
  • In programs, you often use the special notation
  • HT
  • where H unifies with the head of the list i.e.
    the first element, and T with the tail of the
    list, i.e. the rest of the list.
  • Example
  • member(Element, Element _).
  • member(Element, _ T) -
  • member(Element, T).

16
Arithmetic in Prolog
  • Arithmetic predicates in Prolog are examples of
    evaluable predicates.
  • All have the form
  • X is Y op Z
  • where op is one of , -, , /, mod.
  • Note that Y and Z must be instantiated. If either
    is unstantiated, SWI-prolog generates a warning
    and starts tracing.
  • Example
  • g(A,B) - A is B 3.
  • ?- g(A, 3).
  • A 9
  • Yes
  • ?- g(12,B).
  • WARNING ...

17
Adding Facts/Clauses
  • Facts or clauses can be added during computation
    by means of assert(P).
  • This can be used, for example, to record the
    results of previous computations.
  • asserta guarantees that the new clause is the
    first one.
  • assertz guarantees that it is the last one.
  • assert makes no guarantees.
  • All of these always succeed.

18
Example
  • member(X, X_).
  • member(X, ST) -
  • member(X, T),
  • asserta(member(X, ST)).
  • After the query, member(3, 1,2,3,4)
  • member(3, 1,2,3,4).
  • member(3, 2,3,4).
  • member(A, AB).
  • member(A, BC) -
  • member(A, C),
  • asserta(member(A, BC)).

19
  • You can also insert clauses
  • Example
  • member(X, X_).
  • member(X, ST) -
  • member(X, T),
  • asserta(member(X, ST) - !).
  • After the query, member(3, 1,2,3,4)
  • member(3, 1,2,3,4) - ! .
  • member(3, 2,3,4) - ! .
  • member(A, AB).
  • member(A, BC) -
  • member(A, C),
  • asserta((member(A, BC) - !)).

20
  • You can get rid of clauses by means of retract
    and retractall. Both always succeed, even if
    there are no clauses to retract.
  • Example
  • ?- retractall (member(_,_) - !).
  • Yes
  • ?- listing(member).
  • member(A, AB).
  • member(A, BC) -
  • member(A, C),
  • asserta((member(A, BC) - !)).
  • Yes
  • ?-

21
Style
  • Prolog clauses should be relatively short.
  • Clauses for the same predicate should be kept
    together.
  • If you need to use a variable whose identity you
    are not interested in, then use the anonymous
    variable.

22
Procedural vs Declarative
  • In prolog it is possible to understand what a
    program is doing without knowing how it is being
    done.
  • Declarative meaning of Prolog Programs (What)
  • A clause lists all the subgoals that must be
    satisfied for the head of the clause to be true.
    A goal is true if all its subgoals are true for
    the same instantiation of the variables.
  • Procedural meaning of Prolog programs (How)
  • And/Or trees.

23
Control And/Or Trees
G
C1
C2
T3
T2
T1
S3
S2
S1
V1
V2
24
Control Or how pure is Prolog?
  • A control problem arises
  • when there is more than one way to prove a query
  • Which to try first?
  • Ideally, try the easiest first.
  • when the query is reduced to more than one
    subgoal
  • Which to try first?
  • Ideally try the hardest first.
  • Prolog uses a hardwired control regime, not
    heuristics.
  • Prolog retrieves clauses from its knowledge base
    in textual order, i.e in the order in which they
    appear in the consulted file.

25
Control Which clause to use first?
  • Use textual order to decide which clause to use
    first.
  • father (john, ed).
  • ancestor (X,Y) -
  • ancestor (X,Z),
  • ancestor (Z,Y).
  • ancestor(X,Y) -
  • father (X,Y).
  • loops on ancestor (A,D).
  • Turning the clauses for ancestor around will lead
    to the answer
  • A john
  • D ed

26
Control Which subgoal to try first?
  • Try subgoals left-to-right.
  • father (pete, john).
  • father (john, ed).
  • ancestor (X, Y) -
  • father(X,Y).
  • ancestor (X,Y) -
  • ancestor (X,Z),
  • father (Z,Y).
  • Loops on ancestor (harry,X).

27
  • Change the second clause for ancestor to
  • ancestor(X,Y) -
  • father(X,Z),
  • ancestor(Z,Y).
  • No

28
Backtracking
  • When there are alternative ways to prove a goal,
    Prolog tries the first, and sets up the others as
    backtrack points.
  • When failure occurs, then Prolog backtracks and
    tries one of the alternative ways.
  • Prolog always backtracks to the most recently
    created backtrack point.

29
Examples of Backtracking
  • mother (mary, ed).
  • father (john,ed).
  • father (pete, john).
  • grandparent (X, Y) -
  • parent (X, Z),
  • parent (Z,Y).
  • parent (X, Y) -
  • mother(X, Y).
  • parent (X, Y) -
  • father (X, Y).

30
Example 1
  • Query parent(john,ed).
  • parent(john, ed)
  • mother(john, ed) father(john, ed)
  • no yes

31
Example 2
  • Query grandparent (G, ed).
  • gt parent (G, Z)
  • gt mother (G,Z).
  • lt success G mary, Z ed.
  • lt success G mary, Z ed.
  • gt parent(ed, ed).
  • gt mother (ed, ed).
  • lt failure
  • lt failure
  • gtredo parent (ed, ed)
  • gt father (ed, ed)
  • lt failure
  • lt failure

32
Example 2 (continued)
  • gt redo parent (G, Z)
  • gt father(G, Z)
  • lt G john, Z ed.
  • lt G john, Z ed.
  • gt parent (ed, ed)
  • gt mother (ed, ed)
  • lt failure
  • gt redo parent (ed, ed)
  • gt father (ed, ed)
  • lt failure
  • lt failure

33
Example 2(continued)
  • gt redo father (G, Z)
  • lt G pete, Z john
  • lt G pete, Z john
  • lt G pete, Z john
  • gt parent (john, ed)
  • gt mother (john, ed)
  • lt failure
  • gtredo parent(john, ed)
  • gt father (john, ed).
  • lt success
  • lt success
  • lt G pete

34
Example 3
  • Add the rules
  • predecessor(X,Z) - parent(X,Z).
  • predecessor(X,Z) -
  • parent(X,Y),
  • predecessor(Y,Z).
  • And the fact
  • mother(susan,mary).
  • Query
  • predecessor(susan,ed).

35
Example 3(continued)
  • predecessor(susan,ed)
  • parent(susan,ed) parent(susan,Y)
  • predecessor(Y,ed)
  • mother(susan,Y) parent(mary,ed)

  • mother(mary,ed)
  • So parent(susan,Y) is satisfied by mary, the
    question now becomes predecessor(mary,ed))

36
Control And/Or Trees
G
C1
C2
T3
T2
T1
S3
S2
S1
V1
V2
37
Cuts Controlling Backtracking
  • When Prolog goes over a cut, it cannot backtrack
    over any goal left of the cut.
  • In our previous chart,
  • If
  • S2 is a cut,
  • S1 succeeds because of C1
  • then
  • C2 and V2 disappear as backtrack points.

38
Example of the use of the cut
  • pension (X) -
  • male(X), !,
  • age (X,Y),
  • Y gt 65.
  • pension (X) -
  • age (X,Y),
  • Y gt 60.
  • male(fred).
  • age (fred, 67).
  • male(harry).
  • age (harry, 63).

39
Example of the use of a cut
  • Query pension (harry).
  • gt male (harry)
  • lt success
  • gt !
  • lt success
  • cut the second clause for pension
  • gt age (harry, Y)
  • lt success Y 63
  • gt 63 gt 65
  • lt failure
  • Without the cut, we would now try the second
    clause for pension.

40
  • Clauses
  • A - B,C,D.
  • C - R,S,!,T,U.
  • C - V.
  • Query ?- A

41
A
C
D
B
R
S
!
T
U
Write a Comment
User Comments (0)
About PowerShow.com