Presentation is loading. Please wait.

Presentation is loading. Please wait.

1Lecture 12 Introduction to Prolog Logic Programming Prolog.

Similar presentations


Presentation on theme: "1Lecture 12 Introduction to Prolog Logic Programming Prolog."— Presentation transcript:

1 1Lecture 12 Introduction to Prolog Logic Programming Prolog

2 2Lecture 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.

3 3Lecture 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

4 4Lecture 12 Introduction to Prolog Solving a problem using Prolog involves Declaring facts Defining 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 atom A 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.

5 5Lecture 12 Introduction to Prolog mary likes(mary, john). and the variable X are all terms. A term may be simple or compound. A simple term may be An atom A 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,

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

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

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

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

10 10Lecture 12 Introduction to Prolog Rules

11 11Lecture 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:

12 12Lecture 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 and X and Y have the same parents. we would say is_brother_of(X,Y):-male(X), parents(X, Father, Mother), parents(Y, Father, Mother).

13 13Lecture 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

14 14Lecture 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 = andrew Y = greg ; X = andrew Y = jennifer ; X = andrew Y = andrew ; X = john Y = john ; X = greg Y = greg ; X = greg Y = jennifer ; X = greg Y = 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).

15 15Lecture 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

16 16Lecture 12 Introduction to Prolog SWI-Prolog

17 17Lecture 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.

18 18Lecture 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

19 19Lecture 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:

20 20Lecture 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 bytes Yes ?- 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

21 21Lecture 12 Introduction to Prolog listing. listing(likes/2). Listing your predicates

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

23 23Lecture 12 Introduction to Prolog trace. notrace. Or simply n. spy(likes/2). nospy Tracing your program

24 24Lecture 12 Introduction to Prolog Call: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tom Y = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, john) ? creep X = mary Y = john ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, mouse) ? creep X = tom Y = mouse ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tom Y = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(jerry, cheeze) ? creep ?- trace. Yes [trace] ?- likes(X,Y).

25 25Lecture 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

26 26Lecture 12 Introduction to Prolog Structures

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

28 28Lecture 12 Introduction to Prolog Arithmetic

29 29Lecture 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 +additionX + Y - subtractionX - Y *multiplicationX * Y /divisionX / Y //division (integer)X // Y modmodulo (remainder)X mod Y **powerX ** Y isequalsZ is X + Y Arithmetic Operators

30 30Lecture 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.

31 31Lecture 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


Download ppt "1Lecture 12 Introduction to Prolog Logic Programming Prolog."

Similar presentations


Ads by Google