commonwealth of australia copyright regulations 1969 warning this material has been reproduced and...

24
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.

Upload: felipe-betterley

Post on 14-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

COMMONWEALTH OF AUSTRALIACopyright 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.

Page 2: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

PrologProlog

CSE2303 Formal Methods I

Lecture 20

Page 3: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

OverviewOverview

• Prolog

• Database

• List

• Arithmetic

• Implementing NFA

• Towers of Hanoi

• SWI-Prolog

Page 4: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

PrologProlog

• 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.

Page 5: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Parent RelationshipParent 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).

Page 6: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

databasedatabaseparent(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

Page 7: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Using Prolog 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

Page 8: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Example SessionExample Session?- [database].

?- parent(bob, pat).

yes

?- parent(bob, jim).

no

?- predecessor(X, jim).

X = pat ;

X = bob ;

no

no Nothing matches

no

prompt

Ask for another solution

Page 9: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Program ClausesProgram 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 :- B1, … , Bn.

Head BodyEnd of clause marker“ if ”

Page 10: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

InterpretationsInterpretations

• Logic

X1,..Xm ((B1 … Bn) A)

• Procedural

– To execute A, first execute B1, then execute B2,...

• Process

– A, B1, … , Bn are considered processes.

– Shared variables are communication channels

A :- B1, … , Bn.

Page 11: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

SyntaxSyntax• 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))

Page 12: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

ListsLists

• A list t1,…, tn can be represented as:

[t1,…, tn]

• 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

Page 13: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Membership ProblemMembership Problem

Write a Prolog program which finds the members of a list.

Page 14: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Membership RelationshipsMembership 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).

Page 15: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

MemberMember

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

Page 16: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

ArithmeticArithmetic

• 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, X =\= Y, ..– is “Evaluates arithmetic expression”

?- X is 2*3 + 4.X = 10yes

Page 17: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Factorial RelationshipsFactorial 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.

Page 18: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

FactorialFactorial

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

Page 19: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Size RelationshipsSize 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.

Page 20: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

SizeSizesize([ ], 0).size([H | T], N) :- size(T, N1), N is N1+1.

?- size([a,b,c], N).N = 3yes

Note: There is a predefined funtion in SWI-Prolog called length

Page 21: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

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

NFANFA

Page 22: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

Towers of HanoiTowers 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.

Page 23: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

SolutionSolution

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).

Page 24: COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University

More InformationMore 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