1Lecture 12 Introduction to Prolog Logic Programming Prolog.

Post on 04-Jan-2016

257 views 5 download

Tags:

transcript

11 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Logic Programming

Prolog

22 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Prolog(Programming in Logic)

Has been around since 70’s

W.F. Clocksin, Programming in Prolog, Springer-Verlag New York Inc., 1998. Bratko, I (2001), PROLOG: programming for artificial intelligence, Third

edition, Addison-Wesley.

Prolog is an un-typed language

Prolog is a declarative programming language

Prolog is based on First Order (predicate) Logic

Prolog was selected as the language of the fifth generation computers by the Japanese

Prolog, is a suitable language for solving problems involving objects and relationships among these objects.

A Prolog program consists of facts and rules.

33 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

AI programming

Relational databases

Natural languages

Machine learning

Robot planning

Symbolic solution of equations

Chemical structure analysis

Expert Systems

Mechanical Theorem Provers

Applications of Prolog

44 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Solving a problem using Prolog involvesDeclaring factsDefining rules Asking questions

Facts

Mary likes john. likes(mary, john).

Predicate(in lowercase) Arguments (in lowercase) A name beginning with a lowercase letter is an atomA fact ends with a full stop.Arguments are seperated with commas.The order of arguments is arbitrary but the order chosen must be used

consistently.A fact may have an arbitrary number of arguments.

55 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

marylikes(mary, john).and the variable X are all terms.

A term may be simple or compound.

A simple term may beAn atomA number Or a variable.

A compound term consists of a functor followed by a number of arguments. The number of arguments is referred to as the arity of the functor.

All Prolog data objects are terms. For instance,

66 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Although we have to be consistent in the use of predicate and object names, they are arbitrary. We could have declared the above fact as: l(m,j). This would, however, make the program less readable.

Other examples of facts

interesting(declarative_programming).useful(declarative_programming).male(john).female(mary).father(john,mary).great(massey).has_a_job(mary).happy(john).plays(mary,john,tennis).valuable(money).gives(john,mary,money).

77 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

A prolog database is a collection of facts (and rules).

likes(tom,jerry).likes(mary,john).likes(tom,mouse).likes(tom,jerry).likes(jerry,cheeze).likes(mary,fruit).likes(john,book).likes(mary,book).Likes(tom,john).

Now that we have some facts, we may ask questions.

?- likes(jerry,cheeze).yes?- likes(jerry,mary).no

88 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?-knows(mary,john).no

We are getting a no answer because we do not have a fact in the database to indicate that mary and john know each other. This does not mean that the statement is false in reality. It just means that it is false based on the knowledge available.

We may ask questions involving variables. If we were interested in objects that like john, we would formulate the following query:

?-likes(X,john).X=mary;X=tom;no

likes(tom,jerry).likes(mary,john).likes(tom,mouse).likes(tom,jerry).likes(jerry,cheeze).likes(mary,fruit).likes(john,book).likes(mary,book).likes(tom,john).

99 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Who likes what??-likes(X,Y)X=tom, Y=jerry;X=mary, Y=john;X=tom,

Y=mouse;X=tom, Y=jerry;

Prolog tries to resatisfy the question everytime we enter a semicolon “;”.

Prolog uses a pointer called a place marker to keep track of the search

for more solutions.

The variable matches anything. Every time the variable X is bound to

a value like mary in the above example, we say that X is instantiated

to that value. In the example above, X was instantiated to mary once

and to tom the next time. Another Query

likes(tom,jerry).likes(mary,john).likes(tom,mouse).likes(tom,jerry).likes(jerry,cheeze).likes(mary,fruit).likes(john,book).likes(mary,book).likes(tom,john).

1010 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Rules

1111 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

In the above database we have a fact indicating that mary likes john.

This however does not make the fact that john also likes mary true. If

we wanted that to be true, we would have to add a relevant fact to the

database.

likes(john,mary).

likes(X,Y):-likes(Y,X).

Rule head if Rule body

Rules

We could make this a generalization by adding a rule to our database as follows:

1212 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Other examples of rules:

likes(john,X):-valuable(X).

To define a rule indicating that X is a bird if

it is an animal and it has feathers.

We would say,

is_a_bird(X):- is_an_animal(X),has(X,feathers).

To define the rule,X is brother of Y if

X is a male andX and Y have the same parents.

we would say

is_brother_of(X,Y):-male(X), parents(X, Father, Mother), parents(Y, Father, Mother).

1313 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

male(andrew).male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary).parents(greg,adam,eve).parents(jennifer, adam,eve).parents(andrew, adam,eve).is_brother_of(X,Y):-male(X),

parents(X, Father, Mother), parents(Y, Father, Mother).

is_sister_of(X,Y):-female(X), parents(X, Father, Mother), parents(Y, Father, Mother).

Another database

1414 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Yes?- is_sister_of(jennifer,greg).Yes?- is_brother_of(greg,X).X = greg ;X = jennifer ;X = andrew ;No

?- is_brother_of(X,Y).X = andrewY = greg ;X = andrewY = jennifer ;X = andrewY = andrew ;X = johnY = john ;X = gregY = greg ;X = gregY = jennifer ;X = gregY = andrew ;No

?- is_brother_of(greg,andrew).

male(andrew).male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary).parents(greg,adam,eve).parents(jennifer, adam,eve).parents(andrew, adam,eve).is_brother_of(X,Y):-male(X),

parents(X, Father, Mother), parents(Y, Father, Mother).

is_sister_of(X,Y):-female(X), parents(X, Father, Mother), parents(Y, Father, Mother).

1515 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

% d:/Prolog/sis.pl compiled 0.00 sec, 2,048 bytes

Welcome to SWI-Prolog (Version 4.0.9)

Copyright (c) 1990-2000 University of Amsterdam.

Copy policy: GPL-2 (see www.gnu.org)

For help, use ?- help(Topic). or ?- apropos(Word).

?- is_sister_of(X,Y).

X = jennifer

Y = greg ;

X = jennifer

Y = jennifer ;

X = jennifer

Y = andrew ;

No

?-

SWI-Prolog

1616 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

SWI-Prolog

1717 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) 1990-2000 University of Amsterdam. Copy policy: GPL-2 (see www.gnu.org)  For help, use ?- help(Topic). or ?- apropos(Word).

?-

SWI-Prolog

You may query Prolog now.

Use the built-in Predicate “halt” to exit Prolog.

1818 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- append([abc,def],[ghi,lmn],L).

L = [abc, def, ghi, lmn]

Yes

?- append([abc,def],L,[abc,def,ghi,lmn]).

L = [ghi, lmn]

Yes

?-

Query mode

1919 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- [user]. |:

Do not forget the period at the end of your input. At the “|:” prompt, type in what you want entered into your database.

male(john).female(mary).father(john,mary).great(massey).has_a_job(mary).

After the last fact is entered and at the “|:” prompt, enter a ctrl-D to exit the consult mode. An example is shown on the next slide.

Consult mode

You can enter consult mode by typing in “[user]” at the “?-”

Prompt:

2020 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- [user].|: likes(tom,jerry).|: likes(mary,john).|: likes(tom,mouse).|: likes(tom,jerry).|: likes(jerry,cheeze).|: likes(mary,fruit).|: likes(john,book).|: knows(mary,book).|: knows(tom,john).|: % user compiled 0.01 sec, 64 bytesYes?- listing. likes(tom, jerry).likes(mary, john).likes(tom, mouse).likes(tom, jerry).likes(jerry, cheeze).likes(mary, fruit).likes(john, book).knows(mary,book).knows(tom,john).Yes

2121 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

listing.

listing(likes/2).

Listing your predicates

2222 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- listing(likes/2).  /* we could have said “listing(likes).” in this case*/

likes(tom, jerry).

likes(mary, john).

likes(tom, mouse).

likes(tom, jerry).

likes(jerry, cheeze).

likes(john,book).

?- listing(knows). knows(mary,book).knows(tom,john).

2323 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

trace.

notrace.

Or simply n.

spy(likes/2).

nospy

Tracing your program

2424 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Call: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tomY = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, john) ? creepX = maryY = john ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, mouse) ? creepX = tomY = mouse ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creepX = tomY = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(jerry, cheeze) ? creep

?- trace.

Yes

[trace] ?- likes(X,Y).

2525 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

X = jerry

Y = cheeze ;

Redo: (7) likes(_G332, _G333) ? creep

Exit: (7) likes(mary, fruit) ? creep

X = mary

Y = fruit ;

Redo: (7) likes(_G332, _G333) ? creep

Exit: (7) likes(john, book) ? creep

X = john

Y = book ;

Fail: (7) likes(_G332, _G333) ? creep

No

2626 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Structures

2727 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- owns(john,book(prolog,author( _ ),year( X ),edition( _ ))),X >1990.

owns(john,book(prolog,author(clocksin_and_mellish),year(1994),edition(4))).owns(victoria,book(prolog,author(bratko),year(2001),edition(3))).

owns(john,book(prolog,clocksin_and_mellish)).owns(victoria,book(prolog,bratko)).

owns(john, book).owns(victoria, book).owns(george,book).

2828 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

Arithmetic

2929 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- A is 2+3, B is A-1, C is A*2, D is C/B, E is C // B.

A = 5

B = 4

C = 10

D = 2.5

E = 2

+ addition X + Y- subtraction X - Y* multiplication X * Y/ division X / Y// division (integer) X // Ymod modulo (remainder) X mod Y** power X ** Yis equals Z is X + Y

Arithmetic Operators

3030 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

population(us,275000).population(china,1262000).population(nz,4000).Population(india,1000000).land(us,3000).land(china,4000).land(nz,250).land(india,3288).concen(X,Y):-

population(X,P),land(X,L),Y is P / L.

3131 Lecture 12 Introduction to PrologLecture 12 Introduction to Prolog

?- concen(X,Y).

X = us

Y = 91.6667

Yes

?- concen(X,Y).

X = us

Y = 91.6667 ;

X = china

Y = 315.5 ;

X = nz

Y = 16 ;

X = india

Y = 304.136 ;

No