UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

1. An Overview of Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Standard Logical Equivalences
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).
Lecture 10 Logic programming, PROLOG (PROgramming in LOGic)
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
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
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
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.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
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.
ARTIFICIAL INTELLIGENCE DR. ABRAHAM AI a field of computer science that is concerned with mechanizing things people do that require intelligent.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Programming Languages Tucker and Noonan – 2e Chapter 15 – Part 1 Logic Programming “Q: How many legs does a dog have if you call its tail a leg? A: Four.
The AI War LISP and Prolog Basic Concepts of 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.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
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.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
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
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Back to “Serious” Topics…
Programming Paradigms and Languages
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
PROLOG.
Presentation transcript:

UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor: Alicia Tang Y. C.

2 PROLOG STANDS FOR PROGRAMMING IN LOGIC Features: SYMBOLIC LANGUAGE CLEAN SYNTAX BACKTRACKING PATTERN MATCHING BUILT-IN SEARCH ENGINE RECURSION UNIFICATION Look at “List” See last example Refer diagram

3 Prolog as a Language for AI Problem Solving Introduction to PROLOG – PROLOG is a language, an environment, and as a powerful problem solving tool. – As a representational formalism, PROLOG offers an interpreter for predicate calculus specifications.

4 PROLOG It is based on Predicate Logic (Horn clause, a special type of clausal form). It makes use of resolution to proof goals. Search is done depth first with backtracking. The primitive form is a predicate: – father(kasim, hani).Kasim is the father of Hani – age(kasim, 60).Kasim is 60 years old – retired(kasim).Kasim has retired – intake(bit, june, 2004). BIT intake is in June’04 – pi(3.145).Pi = 3.145

5 FACTS (KNOWLEDGE ) parent(john,bob). parent(mary,alice). parent(mary,bob). parent(john,alice). parent(lee, june). sex(john,male). sex(mary,female). sex(bob, male). sex(alice,female). sex(june,female). sex(lee,male). First Prolog Program

6 mother(Mother,Child):- parent(Mother,Child). sex(Mother, female). mother_1(Mother,Child):- sex(Mother, female), parent(Mother, Child). sibling(Child1, Child2):- parent(M,Child1), sex(M, female), parent(F, Child1), sex(F, male), parent(M, Child2), parent(F, Child2). female is a fact or “constant” In English, the statements read as: “Siblings are those who share the same parents”. You will get funny answer. Try it out in Lab.

7 Simpler is better. Define extra predicates. mother(M, Child):- parent(M,Child), sex(M, female). father(F, Child):- parent(F,Child), sex(M, male). sibling(Child1, Child2):- mother(M, Child1), father(F,Child1), mother(M, Child2), father(F, Child2), Child1 \= Child2. Checks that generated answers are not the same, then return them

8 Prolog Queries Queries use the format as a predicate. Predicates must be terminated by a full stop. ?- parent(Who, unix). Who = thompson; Who = ritchie; no Prolog variables are designated with a capitalised first letter in a word. ?- father(tom, pat). yes (Prolog answers ‘yes’ indicating query success)

9 Prolog Queries ?- father(tom,Who). Who = pat; Who = jane yes (Prolog answers and the user types a semicolon; Prolog gives another solution)

10 Prolog Queries Note: Whenever Prolog gives an answer and the user responds with a semicolon (‘;’), Prolog backtracks and looks for alternative answers. ?- father(X,pat), age(X,Age). X = tom, Age = 42 yes Note: A multi-condition query can be specified using the commas (‘,’) to separate the conditions. This represents a conjunction (‘and’) of literals.

11 Prolog “ Rules ” Prolog programs consists of statements called clauses which are similar to sentences in Natural Language. Prolog clauses consists of a head and a body and generally has the form: head:-body. The head of a clause consists of a single positive literal and the body consists of one or more literals. A fact is a clause that has an empty body thus having the form: P.

12 Prolog Rules A rule is a clause that has a head and a non-empty body. The simplest form of a rule is the following: P :- Q. Examples of rules are: parent(X,Y) :- child(Y,X). % X is a parent of Y if % Y is a child of X. nice(DAY):- sunny(DAY), not windy(DAY), neighbour(X,Y) :- address(X, Street, Num), address(Y, Street, Num1), not equal(Num, Num1).

13 Prolog Symbols and Structures p(X,Y): predicate symbol p describes relationship between arguments X and Y.,: Logical “and”. ;: Logical “or”. not: Logical “not”..: Terminator (same as ; in Pascal). :-: Implication symbol (P  Q) is written as (Q if P). !: “Cut” symbol, used to control backtracking to make program more deterministic. [] : List symbol. Example: [1,2,3], [anne, arthur, ada]. [Head|Tail] : A format used for list manipulation where Head is head of list and tail is tail of the list. E.g. [1,2,3]: Head is 1 and Tail is [2,3].

14 Lists A list is a Prolog structure used to represent an ordered sequence of items. For example: [prolog, miranda, lisp, clips] [1,2,3,4,5,6,7,8,9,10] [ [a,b], [a], [c], [] ] A list with no elements is an empty list and is denoted as []. The empty list is a Prolog atom. A non-empty list consists of a head and a tail. The head is the first element of the list and the tail is the list of all the other elements.

15 List Examples listhead tail [1,2,3]1[2,3] [ [a,b], [a], [c]][a,b][[a],[c]] [mon,tue,wed]mon[tue,wed] [a]a[] An empty list has no head and no tail.

16 sum_fr_zero_to_n(0,0). sum_fr_zero_to_n(N,S):- sum_fr_zero_to_n(N1,S1), N is N1+1, S is S1+N. factorial(0,1). factorial(N,R):- factorial(N1,R1), N is N1+1, R is N*R1.

17 rev_print([]). rev_print([H|R]):- rev_print(R), write(H), nl. reverse([],[]). reverse([H|T], L):- reverse(T,R), append(R,[H],L). append([], L, L). append([H|T], L, [H|T1]):- append(T,L,T1).

18 del(X,[],[]). del(X,[X|Y],Y):-!. del(X,[Y|Z],[Y|W]):- del(X,Z,W). member(X,[X|Tail]). member(X, [_Head|Tail]):- member(X,Tail). count([],0). count([Head|Tail],Count):- count(Tail,Tailcount), Count is Tailcount + 1.

19 Backtracking … very powerful Suppose that we have developed a knowledge base of: boss(dick,harry). boss(tom,dick). boss(ann,mary). boss(mary,harry). and, query is made as: ?- boss(X,Y), boss(Y,Z).

20 Let see how backtracking in Prolog works and get a feel of how “intelligent” Prolog as a logic language.  The first predicate expression in the query will match the first fact in the database, with X=dick and Y= harry.  Moving to the second predicate expression in the query, the interpreter searches for a boss fact with harry as its first argument. There is no such fact in the database, so the second expression in the query fails.  So the interpreter backtracks, returning to the first expression to make another choice (this happens every time a Prolog interpreter fails). It now has to go backward to a point where there exists a choice not explored yet. So now it uses the second fact and sets X=tom and Y=dick.

21  The old match for the second expression was from the first fact, so mow it examines the second, third and the fourth facts in order. Unfortunately, none have dick as their first argument, so the expression fails.  Now the interpreter returns to the first predicate expression yet again. The first and the second facts have been tried, so it uses the third fact and sets X=ann and Y=mary.  It succeeds again and Z=harry again.

22  The interpreter goes to the second predicate expression, and searches from the start of the database for a boss fact where dick is the first argument. And, yes there is such a fact (i.e. the first fact in the knowledge base).  So Z=harry, and since we are at the end of the query, the query succeeds. Therefore tom is the super-boss. The interpreter displays X=tom, Y=dick, Z=harry.  If the user now types a semicolon, the interpreter forces a failure and start backtracking. The interpreter goes back to what it just finished, the second expression of the query, and try to find a different match.