LECTURE 3 PROLOG.

Slides:



Advertisements
Similar presentations
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
Advertisements

First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
Chapter 11 :: Logic Languages
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Getting Started with Visual Prolog
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Introduction Prolog program consists of clauses which are facts and rules. Predicate is the name given to the relation likes(bill, tennis) predicate name.
Chapter 7 Turbo Prolog Chapter 7 Turbo Prolog Artificial Intelligence ดร. วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะ วิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

LECTURE 3 PROLOG

Introduction Prolog program consists of clauses which are facts and rules. Predicate is the name given to the relation likes(bill, tennis) predicate name arguments

Anonymous Variable The anonymous variable ‘_’can be used in place of any other variable. It can’t be set to a value. Goal: parent(Parent, _). Finds all the parents Anonymous variables can be used in facts. owns(_, shoes). Everyone owns shoes eats( _ ). Everyone eats

Constants start with a small letter. Variables start with a capital letter. /* Example CH03 –3 */ predicates likes(symbol,symbol) clauses likes(ellen, reading). likes(john, computers). likes(john, badminton). likes(leonard, badminton). likes(eric, swimming). likes(eric, reading). likes(X, reading). likes(Person, reading).

/* Ch03ex04.pro */ predicates male(symbol) female(symbol) parent(symbol, symbol) clauses male(bill). male(joe). female(sue). female(tammy). parent(bill, joe). parent(sue, joe). parent(joe, tammy). Write a goal statement that finds the fathers Goal: parent(Person, _) and male(Person). Another way of defining father predicate father(X,Y):- parent(X, Y), male(X).

/* Ch03ex04.pro */ predicates male(symbol) female(symbol) parent(symbol, symbol) clauses male(bill). male(joe). female(sue). female(tammy). parent(bill, joe). parent(sue, joe). parent(joe, tammy). First solve the sub-goal parent(Person, _) Then bind the variable Person to a value returned by parent. The value returned by parent is now used to satisfy the goal male(Person) Compound goals are composed of conjunction (AND) and disjunction (OR).

/* Ch03Ex05 */ predicates car(symbol,real,integer,symbol,integer) truck(symbol,real,integer,symbol,integer) clauses car(chrysler, 130000, 3, red, 12000). car(ford, 90000, 4, gray, 25000). car(datsun, 8000, 1, red, 30000). truck(ford, 80000, 6, blue, 8000). truck(datsun, 50000, 5, orange, 20000). truck(toyota, 25000, 2, black, 25000). Goal: car(Make, Odometer, Years-on-road, Body, 25000) Goal: car(Make, Odometer, Years-on-road, Body, Cost) and cost < 25000.

PDC Prolog Programs Type declaration: to speed up the running time Clauses: facts and rules Predicates: declare predicates and domains of the arguments. (If standard domains are used for the predicate they need not be declared in the domain section) Domains: declare domains that aren’t PDC Prolog’s standard domain Goal: When the program is to run independent of the PDC Prolog development environment. If standard domains are used for the predicates they need not be declared in the domain section.

A PDC Prolog program has the following basic structure Domain /* domain decleration*/ Predicates /*... */ Goal /* subgoal1 Subgoal2 …… */ Clauses /*… rules and facts ….. */

Rules are of the form HEAD:- <subgoal1>, <subgoal2>, …, <subgoaln> For a rule to succeed, Prolog must satisfy all of its subgoals. Backtracking: If a subgoal fails prolog backs up and looks for alternatives for the earlier sub goals.

Unification When a clause is matched to the goal, it binds values to free variables so that the goal and the clause are identical. This matching operation is called unification.

/* Ch04Ex01.pro */ domains product, sum = integer predicates add_em_up(sum, sum, sum) multiply_em(product, product, product) clauses add_em_up(X, Y, Sum) :- Sum = X + Y. multiply_em(X, Y, Product) :- Product = X * Y. To double the product of 31 and 17 one may write multiply_em_up(31, 17, Sum) Add_em(Sum, Sum, Answer). Type error – Product and Sum are different domains

/* Ch04ex02.pro – Domain declerations to catch type errors */ domains brand, color = symbol age, price = integer mileage = real predicates car(brand, mileage, age, color, price) clauses car(chrysler, 130000, 3, red, 12000). car(ford, 90000, 4, gray, 25000). car(datsun, 8000, 1, red, 30000). If the order of the arguments are mixed up in the goal statement ‘type error’ is produced.

/*Unification Ch05Ex01.pro*/ domains title, author = symbol pages = integer predicates book(title, pages) written_by(author, title) long_novel(title) clauses written_by(fleming, "DR NO"). written_by(melville, "MOBY DICK"). book("MOBY DICK", 250). book("DR NO", 310). long_novel(Title) :- written_by(_, Title), book(Title, Length), Length > 300. Goal: written_by(X, Y) X=fleming, Y=DR NO X=melville, Y=MOBY DICK 2 Solutions Goal: Trace CALL: written_by( _, _) RETURN: *written_by("fleming","DR NO”) YES Goal: long_novel(Title)

/* Backtracking Cho5ex02.pro */ predicates likes(symbol,symbol) tastes(symbol,symbol) food(symbol) clauses likes(bill, X) :- food(X), tastes(X, good). tastes(pizza, good). tastes(brussels_sprouts, bad). food(brussels_sprouts). food(pizza). Goal: likes(bill, X) X=pizza 1 Solution CALL: likes("bill",_) CALL: food(_) RETURN: *food("brussels_sprouts") CALL: tastes("brussels_sprouts","good") REDO: tastes("brussels_sprouts","good") REDO: food(_) RETURN: food("pizza") CALL: tastes("pizza","good") RETURN: tastes("pizza","good") RETURN: likes("bill","pizza")

Summary When Prolog begins an attempt to satisfy a goal, it starts at the top of the program in search of a match. When a new call is made, a search for a match to that call also begins at the top of the program. When a call has found a successful match, the call is said to return, and the next subgoal in turn may be tried. Once a variable has been bound in a clause, the only way to free that binding is through backtracking. Backtracking always goes up to the last indeterministic call and tries to resatisfy that call.

/* Bactracking Ch05Ex05 */ predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal can_swim(What) , write("A ", What, " can swim."). clauses type(ungulate, animal). type(fish, animal). is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). lives(zebra, on_land). lives(frog, on_land). lives(frog, in_water). lives(shark, in_water). can_swim(Y) :- type(X, animal) , is_a(Y, X) , lives(Y, in_water). CALL: _PROLOG_Goal() CALL: can_swim(_) CALL: type(_,"animal") RETURN: *type("ungulate","animal") CALL: is_a(_,"ungulate") RETURN: is_a("zebra","ungulate") CALL: lives("zebra","in_water") REDO: lives("zebra","in_water") FAIL: lives("zebra","in_water") REDO: type(_,"animal") RETURN: type("fish","animal") CALL: is_a(_,"fish") REDO: is_a(_,"fish") RETURN: *is_a("herring","fish") CALL: lives("herring","in_water") REDO: lives("herring","in_water") FAIL: lives("herring","in_water") RETURN: is_a("shark","fish") CALL: lives("shark","in_water") REDO: lives("shark","in_water") RETURN: lives("shark","in_water") RETURN: can_swim("shark") RETURN: write("A ") write("shark") write(" can swim.")

Controlling the Search for Solutions Deterministic call produces only one solution Nondeterministic call may produce multiple solutions. Fail – to force backtracking Cut (!) – to prevent backtracking

/* use of fail - Ch05ex06.pro */ domains name = symbol predicates father(name, name) everybody clauses father(leonard, katherine). father(carl, jason). father(carl, marilyn). everybody :- father(X, Y), write(X, " is ", Y, "'s father\n"), fail. Goal: father(X, Y) X=leonard, Y=katherine X=carl, Y=jason X=carl, Y=marilyn 3 Solutions Goal: Without ‘fail’ Goal: everybody leonard is katherine’s father YES Goal: With ‘fail’ Goal: everybody leonard is katherine's father carl is jason's father carl is marilyn's father No Goal: An internal call to father will come up with only one solution. The predicate everybody uses fail to force backtracking, and therefore finds all possible solutions. It’s useless to place a subgoal after ‘fail’ in the body of a rule. Since the predicate fail always fails there is no way of reaching a subgoal located after fail.

Cut Green Cut: When it is known in advance that certain possibilities will never give rise to meaningfull solutions (waste of time and storage) Red Cut. The logic of the program demands the cut to prevent considerations of alternate subgoals. Prevent backtracking to a previous subgoal in a rule Example Rl:- a, b, !, c. Shows the satisfaction with finding the first solution that prolog finds to the subgoals a and b.

/* Cut Example - Ch05ex07.pro */ predicates buy_car(symbol, symbol) car(symbol, symbol, integer) colors(symbol, symbol) clauses buy_car(Model, Color) :- car(Model, Color, Price), colors(Color, sexy),!, Price < 25000. car(maserati, green, 25000). car(corvette, black, 24000). car(corvette, red, 26000). car(porsche, red, 24000). colors(red, sexy). colors(black, mean). colors(green, preppy). Goal: buy_car(X, Y) No Solution Goal: buy_car(X, red) No Solution CALL: buy_car(_,"red") CALL: car(_,"red",_) REDO: car(_,"red",_) RETURN: *car("corvette","red",26000) CALL: colors("red","sexy") RETURN: colors("red","sexy") 26000<25000 FAIL: buy_car(_,"red")

Use of cut to implement case structure r(X) :- X =1, ! , a, b, c. r(X) :- X =2, ! , d. r(X) :- X =3, ! , c. r(_) :- write(“This is a catch-all clause.”). r(1) :- ! , a, b, c. r(2) :- ! , d. r(3) :- ! , c. r(_) :- write(“This is a catch-all clause.”). Using the cut makes r a deterministic predicate

*/ The cut as a go to -- Ch05ex14 --*/ predicates action(integer) clauses action(1) :- !, write("You typed 1."). action(2) :- !, write("You typed two."). action(3) :- !, write("Three was what you typed."). action(_) :- !, write("I don't know that number!"). goal write("Type a number from 1 to 3: "), readreal(Choice), action(Choice).

The not predicate succeeds when the subgoal can’t be proven true. Free variables are not allowed in not. likes(bill, Anyone) :- likes(sue, Anyone), not(hates(bill, Anyone)). Will work likes(bill, Anyone) :- not(hates(bill, Anyone)), likes(sue, Anyone). Anyone is a free variable and is bound by likes(sue, Anyone) before PDC prolog finds out that hates(bill, Anyone) is not true. The second clause states that Bill likes anyone if no one tht Bill hates is known and Sue likes Anyone. Will not work

Simple and Compound Objects Simple Data Objects A simple data object is either a variable or a constant. If it is a constant, it is a character (a char), a number (an integer or a real), or an atom ( a string or a symbol). Variables as Data Objects Variables must begin with an upper case letter (A *Z) or an underscore (_). In prolog variables can bind with any legal Prolog argument or data object. Prolog variables are local not global. That is, if two clauses each contain a variable called X, these X’s are two distint Xs.

Simple and Compound Objects Constants as Data Objects Constants include characters, numbers, and atoms. A constan’s value is its name. That is, the constant 2 can olny stand for the number 2. Characters Characters are char type; they consist of printable and unprintable ASCII characters. Numbers Numbers are either integer (-32, 768 to 32, 767) or real (1e-308 to 1e308) types.

Simple and Compound Objects Atoms An atom is either a symbol or a string type. Symbol Atoms food rick_jones_2 new_york a String Atoms “Jesse James” “123 Pike street” “a” “New York”

cursor(Row, Column) is a built in predicate. Two purposes: /* Ch06ex02 */ domains row, column, step = integer movement = up(step); down(step); left(step); right(step) predicates move_cursor(row, column, movement) clauses move_cursor(R, C, up(Step)) :- cursor(R, C), R1=R-Step, cursor(R1, C). move_cursor(R, C, down(Step)) :- cursor(R, C), R1=R+Step, cursor(R1, C). move_cursor(R, C, left(Step)) :- cursor(R, C), C1=C-Step, cursor(R, C1). move_cursor(R, C, right(Step)) :- cursor(R, C), C1=C+Step, cursor(R, C1). cursor(Row, Column) is a built in predicate. Two purposes: denotes where the cursor is places the cursor

/*Ch06ex03*/ domains name = person(symbol, symbol) /* (First, Last) */ birthday = b_date(symbol, integer, integer) /* (Month, Day, Year) */ ph_num = symbol /* Phone_number */ predicates phone_list(name, symbol, birthday) get_months_birthdays convert_month(symbol, integer) check_birthday_month(integer, birthday) write_person(name)

/*Ch06ex03 - continue*/ clauses get_months_birthdays :- makewindow(1, 7, 7, " This Month's Birthday List ", 0, 0, 25, 80), write(" First name\t Last Name\n"), date(_, This_month, _), /* Get month from system clock (yy/mm/dd/ */ phone_list(Person, _, Date), check_birthday_month(This_month, Date), write_person(Person), fail. write("\n\n Press any key to continue: "), readchar(_). write_person(person(First_name, Last_name)) :- write(" ", First_name, "\t\t ", Last_name), nl. check_birthday_month(Mon, b_date(Month, _, _)) :- convert_month(Month, Month1), Mon = Month1.

/*Ch06ex03 - continue*/ phone_list(person(ed, willis), "767-8463", b_date(jan, 3, 1955)). phone_list(person(benjamin, thomas), "438-8400", b_date(feb, 5, 1985)). phone_list(person(ray, william), "555-5653", b_date(mar, 3, 1935)). phone_list(person(thomas, alfred), "767-2223", b_date(apr, 29, 1951)). phone_list(person(chris, grahm), "555-1212", b_date(may, 12, 1962)). phone_list(person(dustin, robert), "438-8400", b_date(jun, 17, 1980)). phone_list(person(anna, friend), "767-8463", b_date(jun, 20, 1986)). phone_list(person(brandy, rae), "555-5653", b_date(jul, 16, 1981)). convert_month(jan, 1). convert_month(feb, 2). convert_month(mar, 3). convert_month(apr, 4). convert_month(may, 5). convert_month(jun, 6). convert_month(jul, 7). convert_month(aug, 8). convert_month(sep, 9). convert_month(oct, 10). convert_month(nov, 11). convert_month(dec, 12).

Lists and Recursion based search in Prolog List specification: [1, 2, 3, 4] List representation: [H| T] /* Ch08ex06 */ domains namelist = name* name = symbol predicates is_a_member_of(name, namelist) clauses is_a_member_of(Name, [Name|_]). is_a_member_of(Name, [_|Tail]) :- is_a_member_of(Name,Tail).

Members of a List domains list = symbol* predicates colours(list) member(symbol, list) clauses colours([red, orange, green, blue, yellow]). member(X, List) :- member (X, [X| - ]), member(X, [T]). goal colours(X), member (green, X).

3x3 Knight’s tour problem domains liste=integer* predicates path(integer, integer, liste) member(integer, liste) move(integer, integer) clauses move(1,6). move(1,8). move(2,7). move(2,9). move(3,4). move(3,8). move(4,3). move(4,9). move(6,7). move(6,1). move(7,6). move(7,2). move(8,3). move(8,1). move(9,4). move(9,2). 5 1 2 3 4 6 7 8 9 path(Z, Z, L). path(X, Y, L):- move(X, Z), not(member(Z, L)), path(Z, Y, [Z|L]). member(X, [X|T]). member(X, [Y|T]):- member(X, T). Goal: path(1, 3, [1]).

Recursive Program that Adds the Elements of a List domains list = integer* predicates sum_of(list, integer) goal sum_of([1, 2, 3, 4], S). clauses sum_of([], 0). sum_of([H|T], S):- sum_of(T, Sum), S = Sum+H.