Download presentation

Presentation is loading. Please wait.

Published byFelipe Betterley Modified about 1 year ago

1
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

2
Prolog CSE2303 Formal Methods I Lecture 20

3
Overview Prolog Database List Arithmetic Implementing NFA Towers of Hanoi SWI-Prolog

4
Prolog PROgramming in LOGic Used for solving problems between objects and the relationships between objects. A logic program is a finite set of program clauses. Programs clauses are: –Facts. –Rules.

5
Parent Relationship We represent the statement: “bob is a parent of pat” as parent(bob, pat). The compound statement “If X is a parent of Z, then X is a predecessor of Z” is represented as: predecessor(X, Z) :- parent(X,Z).

6
database parent(bob, ann). parent(bob, pat). parent(pat, jim). predecessor(X, Z) :- parent(X,Z). predecessor(X, Z) :- parent(X,Y), predecessor(Y, Z). Facts Rules Constants Variables

7
Using Prolog To read in the file database. Type: [database]. To ask if bob is the parent of pat. Type: parent(bob, pat). To ask who is the predecessors of jim. Type: predecessor(X, jim). To quit. Type: ctrl-D

8
Example Session ?- [database]. ?- parent(bob, pat). yes ?- parent(bob, jim). no ?- predecessor(X, jim). X = pat ; X = bob ; no Nothing matches no prompt Ask for another solution

9
Program Clauses Rule –The head and the body are nonempty. –The body is the conditional part. –The head is the conclusion. Fact –The body is empty, and is written as: A.A. A :- B 1, …, B n. Head Body End of clause marker “ if ”

10
Interpretations Logic X 1,..X m ((B 1 … B n ) A) Procedural –To execute A, first execute B 1, then execute B 2,... Process –A, B 1, …, B n are considered processes. –Shared variables are communication channels A :- B 1, …, B n.

11
Syntax Constants –Names: which always begin with a lowercase letter. likes mary parent predecessor –Numbers: 0 -23 3.4585 14092 Variables –Always begin with either an capital letter or an underline character. X Answer _ _Input Structures –Compound terms. owns(john, book) parent(X, parent(Y, jim))

12
Lists A list t 1,…, t n can be represented as: [t 1,…, t n ] For example: [a,b,c,d] Also represented as: [a | b,c,d] The empty list is [ ] Head of the list Tail of the list

13
Membership Problem Write a Prolog program which finds the members of a list.

14
Membership Relationships X is always a member of a list whose head is X. member(X, [X | T]). If X is a member of a list T, then X is a member of a list whose tail is T. member(X, [H | T]) :- member(X, T).

15
Member member(X, [X | T]). member(X, [H | T]) :- member(X, T). ?- member(john, [paul, john]). yes ?- member(X, [paul, john]). X = paul ; X = john ; no

16
Arithmetic Operators –X + Y, -X, X-Y, X*Y, X/Y, … Functions –abs(X), max(X, Y), sin(X), … Relations –X Y, X =< Y, X =:= Y, X =\= Y,.. –is “Evaluates arithmetic expression” ?- X is 2*3 + 4. X = 10 yes

17
Factorial Relationships The factorial of 0 is 1. factorial(0,F) :- F is 1. If N > 0, and N1 is N – 1, and the factorial of N1 is F1, and F is N*F1, then the factorial of N is F. factorial(N, F) :- N > 0, N1 is N-1, factorial(N1, F1), F is N*F1.

18
Factorial factorial(0, F) :- F is 1. factorial(N, F) :- N > 0, N1 is N-1, factorial(N1, F1), F is N*F1. ?- factorial(3, F). F = 6 yes

19
Size Relationships The size of an empty list is 0. size([ ], 0). If the size of the list T is N1 and N is N1+1, then the size of the list with tail T is N. size([H | T], N) :- size(T, N1), N is N1+1.

20
Size size([ ], 0). size([H | T], N) :- size(T, N1), N is N1+1. ?- size([a,b,c], N). N = 3 yes Note: There is a predefined funtion in SWI-Prolog called length

21
accept(W) :- start(S), path(S, W). path(S, [ ]) :- final(S). path(S, [H | T]) :- arc(S, H, N), path(N, T). start(1). final(3). arc(1, a, 1). arc(1, b, 2). arc(1, b, 3). arc(2, b, 3). arc(3, a, 3). a a b b b - + 1 2 3 ?- [nfa]. ?- accept([a, b, a]). yes NFA

22
Towers of Hanoi The object is to move the disks, one at a time, from the left peg to the right peg. You are allowed to use the middle peg. At no stage are you allowed to place a bigger disk on top of a smaller one.

23
Solution move(1, X, Y, Z) :- write(’ Move top disk from ’), write(X), write(’ to ’), write(Z), nl. move(N, X, Y, Z) :- N > 1, M is N-1, move(M, X, Z, Y), move(1, X, Y, Z), move(M, Y, X, Z). ?- move(3, left, middle, right).

24
More Information Courseware web site. Links to: –Prolog tutorial, SWI-Prolog, etc. Books: –“The Art of Prolog: Advanced Programming Techniques”, by L.Sterling and E. Shapiro. –“Prolog Programming for Artificial Intelligence”, by I. Bratko. –“Programming in Prolog”, by W. Clocksin and D. Mellish

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google