Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Chapter 11 :: Logic Languages
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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.
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).
1 AI: Prolog Review of Prolog Rules and Facts Prolog Syntax Matching.
Chapter 12 - Logic Programming
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CHA2555 Week2: Knowledge Representation, Logic and Prolog Lee McCluskey First term:
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.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
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.
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.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
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.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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/
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:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
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.
Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.
CHA2555 Week2 Practical: Lee McCluskey First term:
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.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
Artificial Intelligence CS370D
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
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.
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
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

Prolog Fundamentals

2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-.... –Query: ?-.... –Variables: must begin with an upper case letter or underscore. Nora, _nora. –Constants (atoms): begin with lowercase letter, or enclosed in single quotes. uncle_ali, nora, year2007,. numbers 3, 6, 2957, 8.34,... –complex terms An atom (the functor) is followed by a comma separated sequence of Prolog terms enclosed in parenthesis (the arguments). likes(lama,apples), likes(amy,X).

3 Outline Predicate Definitions Matching Backtracking Recursion

4 Predicate Definitions Both facts and rules are predicate definitions. Predicate: is the name given to the word occurring before the bracket in a fact or rule: parent(ali,samir). By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true. Predicate name

5 Prolog Matching (unification) Two atoms match if they are the same atom. –Ex.: nora = nora, but nora ≠ ’Nora’. A variable matches any other Prolog term. The variable gets instantiated with the other term. –Ex.: X = teacher(jacky) –Ex.: X = Y Two complex terms match if they have the same functor and the same number of arguments and if all pairs of parallel arguments match. –Ex.: like(sara,basma) = like(sara,X) –Ex.: like(sara,basma) ≠ like(X,X)

6 Prolog Execution When Prolog tries to answer a query, it does so by trying to match the goal to the head of the rule. This might result in some variables getting bound. Example: KB: cleaner(nancey). Query: cleaner (mary). cleaner (shona). nurse(sister_jacky). ?- cleaner(X). X = nancey ; X = mary ; X = shona ; No Prolog checks for facts that match the query. (There are three.) Prolog starts from the top of the knowledge base and, therefore, finds cleaner(nancey) first. Typing ; forces Prolog to check whether there are other possibilities.

7 Example likes(huda, X) :- fresh(X), sweet(X). fresh(Y) :- juicy(Y). juicy(orange). sweet(orange). ?- likes(huda, orange). MATCHES likes(huda, orange) to head of rule 1, with X=orange. Sets fresh(orange), sweet(orange) as new goals. Tries to prove fresh(orange). MATCHES head of rule 2. Y=orange. Tries to prove juicy(orange). MATCHES a fact. So proved. Tries to prove sweet(orange). MATCHES a fact, So proved.

8 Proof tree. Likes (huda, orange) fresh(orange) sweet(orange) juicy(orange) Using rule 1 Using rule 2 True fact likes(huda, X) :- fresh(X), sweet(X). fresh(Y) :- juicy(Y). juicy(orange). sweet(orange). ?- likes(huda, orange).

9 Backtracking Prolog goes through facts/rules top to bottom looking for facts or rule heads which match the goal. If a rule fails as can’t prove body, Prolog will try next rule/fact matching current goal. If can’t find ANY way to prove current goal, Prolog will retry the previous goal, to see if it can be solved another way. Example: facts only teacher(saleh). teacher(nora). teacher('Aunt Laura'). father( saleh, jaber). teacher(X). X = saleh ; X = nora ; X = 'Aunt Laura' ; No Note: Here we use “;” to ask it to look for other solutions, which forces backtracking.

10 Example using facts & rules (1) likes(huda, X) :- fresh(X), sweet(X). fresh(apple). fresh(orange). sweet(orange). ?- likes(huda, What). –Matches head of rule with X=What (binding two variables to same value). –Tries to satisfy fresh(What). –fresh(apple) succeeds. –Tries to satisfy sweet(apple). –This fails. –So backtracks and retries fresh(What). –Succeeds with What=orange –etc.

11 Example using facts & rules (2) female(lara). female(hana). female(sara). female(dia). haschildren(sara). haschildren(dia). mother(X) :-female(X), haschildren(X). mother(aisha). ?- mother(X). Matches with head of first rule. Tries to satisfy female(X). Matches female(lara). Tries to satisfy haschildren(lara). FAILS, so GOES BACK to try female(X) again. Maches femal(sara). Tries haschildrens(sara). Succeeds, so mother(X) succeeds/ X = sara ; Going back and trying later femal facts: X = dia; And trying later “mother” fact: X = aisha.

12 Recursion in any language is a function that can call itself until the goal has been succeed. In Prolog, when a predicate contain a goal that refers to itself. Example. –parent(noha,jaber). –parent(jaber,salim). –parent(salim,reda). –ancestor(X,Y) :- parent(X,Y). /* If X is a parent of Y, then X is an ancestor of Y */ –ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z). /* if Y is an ancestor of Z and X is a parent of Y, then X is an ancestor of Z */ recursion

13 Recursion (Cont.) KB:Query: ?- ancestor(jaber,reda). parent(noha,jaber). parent(jaber,salim). parent(salim,reda). ancestor(X,Y) :- parent(X,Y). ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z). 1.Prolog will first try the goal parent(jaber,reda). and it will fail. 2. then try the second clause of ancestor. The new query is parent(jaber,Y). 3.Prolog will find Y=salim and try ancestor(salim,reda) wich will conduct to check parent(salim,reda). This is succesfull. As a result the goal ancestor(salim,reda) succeeds. Then ancestor(jaber,reda) can succeed.

14 Summary Matching: –Prolog tries to prove goals by matching them with rules/facts. –Tries to find variable bindings making expressions identical. SEARCHING –the whole activity of the Prolog system is to search through various options to find a combination that succeeds. Main search tools are backtracking and recursion Backtracking –Prolog goes through facts/rules from top to bottom to try to find matching rules or facts. –when the system fails during its search, it returns to previous choices to see if making a different choice would allow success. Recursion –when a predicate contain a goal that refers to itself.