Prolog Search Strategy

Slides:



Advertisements
Similar presentations
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Advertisements

Some Prolog Prolog is a logic programming language
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
Last time Proof-system search ( ` ) Interpretation search ( ² ) Quantifiers Equality Decision procedures Induction Cross-cutting aspectsMain search strategy.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
1 About Prolog Lu Han Written at Modified at
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.
LING 388: Language and Computers Sandiway Fong Lecture 4.
Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Spring 2008 R O O T S Prolog - Part 1 Alexis Raptarchis.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
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.
Automated Reasoning Early AI explored how to automate 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.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
Introduction to Prolog
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
Prolog programming....Dr.Yasser Nada. Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
1 Section 9.2 Logic Programming A logic program is a set of clauses with the restriction that there is exactly one positive literal in each clause. Such.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 3 1 Logic Programming.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
CSE (c) S. Tanimoto, 2001 Logic Programming
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
State the domain, range and tell whether or not it is a function.
CSE 341, S. Tanimoto Logic Programming -
Unification, Recursion and Lists
First-Order Logic and Inductive Logic Programming
Horn Clauses and Unification
Logical Inference: Through Proof to Truth
Tests, Backtracking, and Recursion
Chapter 11 :: Logic Languages
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Logic: Top-down proof procedure and Datalog
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Horn Clauses and Unification
Chapter 12 :: Logic Languages
Horn Clauses and Unification
Chapter 12 :: Logic Languages
Horn Clauses and Unification
Introduction to Prolog
Programming Techniques
CSE (c) S. Tanimoto, 2004 Logic Programming
CSE (c) S. Tanimoto, 2002 Logic Programming
Horn Clauses and Unification
CS2136: Paradigms of Computation
Chapter 2: Prolog (Introduction and Basic Concepts)
Chapter 12 :: Logic Languages
Backtracking, Search, Heuristics
Function 1 st day Objectives: At the end of the class, the learners shall be able to; 1. demonstrate an understanding of key concepts of functions. 2.
Presentation transcript:

Prolog Search Strategy Chapter 3 Prolog Search Strategy Taif University Fall 2010 Dr. Yasser Ahmed Nada

Search is a major issue. Prolog provides a single method of search for free. This method is known as depth first search. The interactions between the programmer and the Prolog system can be thought of as a conversation

Queries and Disjunctions query is a goal which is submitted to Prolog in order to determine whether this goal is true or false. Prolog normally expects queries it prints the prompt: - ? and expects you to type in one or more goals Prolog System have finished a query —or any clause— by typing “.” followed by typing the key normally labeled “RETURN”.

Example : we would like to determine whether or not woman(jane) In this case we would type this in and see (what is actually typed is emboldened): ?- woman(jane). We now have to find out “if jane is a woman”. To do this we must search through the facts and rules known by Prolog to see if we can find out whether this is so. Notice: Prolog does not search through the facts before the rules

some facts assumed to be known woman(jean). man(fred). woman(jane). woman(joan). woman(pat).

Prolog searches through the set of clauses in the same way that we read (in the west). That is, from top to bottom. First, Prolog examine woman(jean). and finds that woman(jane) in example does not match , then examine women(fred) and find that women (jane) does not match then examine women(jane ) and find that women(jane) match it , so the search is stop and result is appear as “yes”. Query : ? – women(jane). Result : Yes.

Resolution table : Represent the process involved in matching the current goal with the head goal of a clause in the program database, finding whatever substitutions are implied by a successful match, and replacing the current goal with the relevant subgoals with any substitutions applied Substitution: is connected with the concept of associating a variable with some other Prolog object. This is important because we are often interested in the objects with which a variable has been associated in order to show that a query can be satisfied.

Failed match Resolution Table Women(jane) (program clause) Women (jane) (current goal) (indicate failure ) {} (no substitution) Failed match

A Simple Conjunction

Set Of Rule And Fact : woman(jean). man(fred). wealthy(fred). happy(Person):- woman(Person), wealthy(Person).

Now We shall ask whether “jean is happy”. Query : ?- happy(jean). Result : No

Prolog therefore has only one choice —to try using the single rule Prolog therefore has only one choice —to try using the single rule. It has to match: happy(jean) against happy(Person) In this case the match produces a substitution, Person=jean, and two subgoals replace the current goal. The substitution of Person by jean is known as a unifier and often written Person/jean. The process of replacing a single goal by one or more subgoals —with whatever substitutions are applicable— is part of the resolution process.

Now have something equivalent to: happy(jean):- woman(jean), wealthy(jean). So the two subgoals are: woman(jean) wealthy(jean)

Successful match Resolution table Happy(Person) women(Person) wealthy(Person) Happy(jean) women(jean) wealthy(jean) (new subgoals) Person=jean Successful match

We try to solve the subgoal woman(jean) and then the subgoal wealthy(jean) There is a possible match wealthy(fred) With wealthy(jean) but we cannot unify

Conjunctions and Disjunctions

Program Database fact and rule woman(jean). woman(jane). woman(joan). woman(pat). wise(jean). wealthy(jane). wealthy(jim). healthy(jim). healthy(jane). healthy(jean). happy(P):- healthy(P), woman(P). wealthy(P), wise(P), Rule 1 Rule 2 Rule 3

consider the solution of the goal happy(jean) Note that 1. Both the subgoal healthy(jean) and woman(jean) have to succeed for the whole goal to succeed. 2. We then return to the top level. Query : ?- happy (jean). Result: Yes Rule 1 is success and search stop .

Happy(P) standard AND/OR tree representation of the search space Healthy (p) women(P) women(P) wise(P) women(P) wealthy(P) Rule 1 Rule 3 Rule 2

Now consider goal of happy(joan) The resolution process generates the subgoals healthy(joan) and woman(joan) from the Rule 1. Query : ?- happy(joan). Result: Yes

Now consider goal happy(P) Notice that P is variable . First, healthy(P) succeeds binding P to jim (P/jim) but when the conjunctive goal woman(jim) is attempted it fails. Prolog now backtracks It reverses along the path through the tree until it can find a place where there was an alternative solution Prolog remembers to unbind any variables exactly at the places in the tree where they were bound

There are more result that satisfied query ?-happy(P). Result : Jane Yes There are more result that satisfied query To show every result you should write “;”

What You Should Be Able To Do? You should be able to load in a Prolog program. You should be able to issue a legal Prolog query. You should be able to generate successive solutions to a goal (provided that any exist). You should be able to apply a depth-first search strategy to simulate the Prolog execution of a goal in relation to a simple program. You should have an idea about the way in which Prolog uses matching. You should be aware of the effects of backtracking when a goal fails.

Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). ?- h(a). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree † f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). ?- h(a). † © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). ?- h(a). † © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). ?- h(a). ?- h(b). † © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). Y=b ?- h(a). ?- h(b). † © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). Y=b; no ?- ?- h(a). ?- h(b). † © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- jealous(X,Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). ?- jealous(X,Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia ?- loves(B,mia). ?- jealous(X,Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia ?- loves(B,mia). ?- jealous(X,Y). X=vincent Y=vincent B=vincent © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia ?- loves(B,mia). ?- jealous(X,Y). X=vincent Y=vincent; Y=marsellus B=vincent B=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). ?- jealous(X,Y). X=vincent Y=vincent; Y=marsellus; B=vincent B=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). …. X=vincent Y=marsellus; X=marsellus Y=vincent B=vincent B=vincent B=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). …. X=marsellus Y=vincent; Y=marsellus B=vincent B=vincent B=marsellus B=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). …. X=marsellus Y=vincent; Y=marsellus; no B=vincent B=vincent B=marsellus B=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Home Work Exercise 3.1 Exercise 3.2

End of chapter