3.1 3. Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Recursion and the order of terms in a PROLOG clause.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
1. An Overview of Prolog.
Logic Programming Automated Reasoning in practice.
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).
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
4. PROLOG Data Objects And PROLOG Arithmetic
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Declarative Programming Autumn 2014 Basic syntax and sample programs.
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/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
Discussion #20 1/11 Discussion #20 Resolution in Datalog.
CHA2555 Week2: Knowledge Representation, Logic and Prolog Lee McCluskey First term:
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
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.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
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.
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.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
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.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical 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.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 25 A First Course in Database Systems.
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.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CHA2555 Week2 Practical: Lee McCluskey First term:
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
ISBN Chapter 16 Logic Programming Languages.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Invitation to Computer Science, Java Version, Second Edition 1 Logic Programming Logic programming  Various facts are asserted to be true  On the basis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Artificial Intelligence CS370D
1-1 An Introduction to Logical Programming Sept
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.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 2: Unification and proof search.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog (part3)
Chapter 12 :: Logic Languages
CS2136: Paradigms of Computation
Presentation transcript:

Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.

3.2 Simple Rules So far PROLOG programs have consisted of facts. –Things which are true.  So far as the program is concerned that is. PROLOG programs can also contain rules. –Things which are true provided that something else is true. Rules and facts are called clauses or predicates. preceded(X,Y) :- succeeded(Y,X). Head or Condition Head or Condition Body or Conclusion Body or Conclusion if

3.3 Processing Rules The program already contains the succeeded relation (see practical 1, question 5). succeeded(tiberius, octavius). succeeded(gaius, tiberius). succeeded(claudius, gaius). Match 1 is with the only clause for preceded. –No choice point generated as there are no choices available. The consequence of match 1 is that PROLOG must now attempt to prove succeeded(tiberius,octavius). Choice point generated as there are three clauses for the succeeded relation. Match 2 succeeds. There are no more answers in the database.

3.4 Processing Rules II Another query : |? - preceded(gaius, tiberius). no |? - A query containing a logical variable : |? - preceded(gaius, K). K = claudius yes |? - Obviously, preceded is just succeeded “backwards”. –Not very useful. Queries with more than one sub-goal are much more interesting.

3.5 Multiple Sub-Goals A parricide is someone who kills one (or more) of their parents. parricide(X) :- murdered(X,Y), parent(Y,X). Note the local logical variable, Y. The database already contains the murdered relation (see practical 1, question 6). murdered(octavius, postumus). murdered(livia, octavius). murdered(livia, drusus). murdered(tiberius, germanicus). murdered(gaius, germanicus). murdered(gaius, tiberius).

3.6 Multiple Sub-Goals II |? - parricide(gaius). yes |? - Based on parent we can define grandparent. grandparent(X,Y) :- parent(X,Z), parent(Z,Y). Note the local logical variable, Z. |? - grandparent(G,C). C = agrippina G = octavius ; C = postumus G = octavius yes |? - GNU PROLOG does not print whatever values it used for Z to find values for G and C.

3.7 A Bit Of PROLOG Syntax A PROLOG program is a set of facts and rules. –A set of clauses. –The order does not matter. –Can have any number of clauses for a given relation. –It is good practice to keep together all the facts and rules for a given relation.  Some implementations insist on this. –The facts and rules for a given relation are sometimes called the procedure for that relation. In general, a clause has a head and a body. –A fact is a clause with an empty body. –A query is a clause with an empty head. PROLOG comments start with % and continue to the end of the line.

3.8 Recursive Rules To find great-grandparents : |? - parent(X,Y),grandparent(Y,Z). Remember, the, means logical and (c.f. && in C++ or Java). – ; actually means logical or (c.f. || in C++ or Java). What about great-great-grandparents and so on? Better to have a general relation for finding ancestors. ancestor(X,Y) :- parent(X,Y). % Ancestor 1 ancestor(X,Y) :- parent(X,Z), % Ancestor 2 ancestor(Z,Y). Ancestor 1 is simple : parents are also ancestors. Ancestor 2 says that if X is a parent of some person Z and that Z is an ancestor of Y then X is also an ancestor of Y.

3.9 Recursive Rules II ancestor is the classic example of a simple transitive relation. |? - ancestor(livia,tiberius). true ? yes |? - ancestor(octavius,postumus). true ? yes |? - ancestor(octavius,Who). Who = julia ? ; Who = agrippina ? ; Who = postumus ? ; Who = gaius ? ; no |? -

3.10 Summary PROLOG programs consist of a set of clauses. –A clause is a fact or a rule. A set of clauses for the same relation are called the procedure for that relation. A clause has a head and a body. –Empty body : fact. –Empty head : query. –Otherwise : rule. A body is made up of zero or more sub-goals. The body of a rule can refer to the rule recursively.