COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and...

Post on 14-Dec-2015

214 views 0 download

transcript

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.

PrologProlog

CSE2303 Formal Methods I

Lecture 20

OverviewOverview

• Prolog

• Database

• List

• Arithmetic

• Implementing NFA

• Towers of Hanoi

• SWI-Prolog

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.

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

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

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

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

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 ”

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.

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

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

Membership ProblemMembership Problem

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

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

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

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

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.

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

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.

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

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

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.

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

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