Bahar Pamuk. Outline Introduction Syntaxa Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
SLD-resolution Introduction Most general unifiers SLD-resolution
Prolog.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
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.
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.
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).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
CPSC 322 Introduction to Artificial Intelligence September 20, 2004.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
CHA2555 Week2: Knowledge Representation, Logic and Prolog Lee McCluskey First term:
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
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.
CS 321 Programming Languages and Compilers Prolog part 2.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: 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.
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)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
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.
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.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
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.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Lecture 041 Predicate Calculus Learning outcomes Students are able to: 1. Evaluate predicate 2. Translate predicate into human language and vice versa.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Section 16.5, 16.6 plus other references
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Programming Techniques
Chapter 2 Syntax and meaning of prolog programs
PROLOG.
Presentation transcript:

Bahar Pamuk

Outline Introduction Syntaxa Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References

Introduction Declarative programming language Instead of specifying how to achieev a goal, we specify what the situation (facts, rules) and the goal(query) are and let Prolog to derive the solution.

Facts  mother(“A”, “B”).  sister(“B”, “C”). Rules  is_bigger(X, Y) :- bigger(X, Y).  is_bigger(X, Y) :- bigger(X,Z), is_bigger(Z, Y) Introduction

Syntax Terms  Atoms a, ab123, z_999, a_b_c, 'an atom sample'  Numbers  Variables A, Z_1_2, _123, _  Compound Terms mother(ali, X), f(g(X, _))

Clauses, Programs & Queries Clauses  Fact: A certain instance of a relation is true.  Rule: The goal expressed by its head is true, if we show that all of the expressions in the rule's body are true. Program: Sequence of clauses Queries: Ask prolog interpreter whether all its predicates are provably true.  is_bigger(elephant, donkey). is elephant bigger than donkey.  small(X), green(X), slimy(X). is there any X that small(X), green(X) and slimy(X) are all true.

Built-in Predicates = (equality) X=Y consult('myprogram.pl'). ?-write('hello world'), n1. hello world Yes ?-X = elephant, write(X), n1. elephant X=elephant Yes ?-atom(elephant) Yes ?- compound(f(elephant)). Yes

Matching Queries ?- is_bigger(X, dog) = is_bigger(elephant, dog). X=elephant Yes ?- p(X, 2, 2) = p(1, Y, X). No ?- p(_, 2, 2) = p(1, Y, _) Y=2 Yes ?- f(a, g(X, Y)) = f(X, Z), Z= g(W, h(X)). X=a Y=h(a) Z=g(W, h(a)) W=a Yes ?- X=my_functor(Y). X=my_functor(_G177) Y=_G177 Yes

Goal Execution All man are mortal. Socrates is a man. Hence Socrates is mortal. mortal(X):-man(X) man(socrates) ?- mortal(socrates). Yes mortal(socrates) is the initial goal. mortal(X) is the first possible fact to match. X=socrates Variable inst. Is extended to the body of the rule. man(socrates) man(socrates) is our new goal It is matched to the fact man(socrates). Current goal succeeds, so initial goal succeeds.

List Manipulation [elephant, horse, dog, cat] [elephant, [], X, parent(X, tom), [a, b, c], f(22)] [a,b,c] =.(a,.(b,.(c, []))) Head: first element Tail: Rest of the list ?- [1,2,3,4] = [Head | Tail] Head = 1 Tail=[2,3,4] Yes

List Manipulation ?-concat_lists([1,2], [3,4,5], X). X=[1,2,3,4,5] concat_lists([],List, List). concat_lists([Elem | List1], List2, [Elem | List3]) :- concat_lists(List1, List2, List3). ?- concat_lists(X, Y, [1,2,3]). X=[] Y=[1,2,3]; X=[1] Y=[2,3]; X=[1,2] Y=[3]; X=[1,2,3] Y=[]

Built-in Predicates length([1,2,3], X). X=3 length(List, 3). List=[_G123, _G234, _G456] member(Elem, List) append(List1, List2, List3) last(Elem, List) reverse(List1, List2) % reverse order of List1 select(List1, Elem, List2) % select elements of List1 after Elem

Arithmetic Expressions 2+3 = 5 % they do not match X is 2+3, X=5. X=5 5 is 2+3. Yes 2+3 is 5. % is evaluates rhs and matches it to left. No max, min, abs, //, **... for rhs of is operator

Relations Compare two evaluated arithmetic expressions. Whenever an arithmetic expression is used, the arguments are evaluated (no need to use is operator.), =, =:=, =\= =:= arithmetically equal, = pattern matching 2 ** 3 =:= Yes 2 ** 3 = No

Operators Every operator is associated with an integer denoting its precedence. The lower the precedence the stronger the operator is binding. The precedence of a term is defined as 0 unless its principal functor is an operator > * * 2 -> 400 elephant -> 0 (3+5) -> 0

Backtracking, Cuts & Negation During proof search, Prolog keeps track of choice points; situations where there is more than 1 match. When the chosen path is a failure/the user asks for alternative solutions, the systems jumps back to the last choice point and try the next alternative. permutation ([], []). permutation ( List, [Elem | Perm]) :- select(List, Elem, Rest), permutation (Rest, Perm).

Backtracking ?- permutation( [1,2,3], X). X = [1,2,3]; X = [1,3,2]; X = [2,1,3]; X = [2,3,1]; X = [3,1,2]; X = [3,2,1]; No

Backtracking remove_dup ([], []). remove_dup ([Head | Tail], Result) :- member(Head, Tail), remove_dup(Tail, Result). remove_dup ([Head | Tail], [Head| Result]) :- remove_dup(Tail, Result). ?- remove_dup ([a, b, b, c, a], X). X = [b,c,a]; X = [b, b,c,a]; X = [a,b,c,a]; X = [a,b,b,c,a]; No.

Derivation Tree remove_dup ([a, b, b, c, a], X) member(a,[ b,b,c,a]) remove_dup([b,b,c,a], Result) member(b, [b,c,a]) remove_dup([b,c,a], Result) member(b, [c,a]) remove_dup([b|c,a], [b|Result]):- remove_dup([c,a], Result) member(c, [a]) remove_dup([c|a], [c|Result]):- remove_dup([a], Result) member(a, []) remove_dup([a|], [a|Result]):- remove_dup([], Result) Result=[] Result=[a] Result=[c,a] Result=[b,c,a]

Derivation Tree remove_dup ([a, b, b, c, a], X) member(a,[ b,b,c,a]) remove_dup([b,b,c,a], Result) member(b, [b,c,a]) remove_dup([b,c,a], Result) member(b, [c,a]) remove_dup([b|c,a], [b|Result]):- remove_dup([c,a], Result) member(c, [a]) remove_dup([c|a], [c|Result]):- remove_dup([a], Result) member(a, []) remove_dup([a|], [a|Result]):- remove_dup([], Result) remove_dup([b|b,c,a], [b|Result]):- remove_dup([b,c,a], Result) Result=[b,c,a] Result=[b,b,c,a]

Cuts It is possible to cut out backtracking points and preventing unwanted alternative solutions. ! is a predicate and can be placed anywhere inside rule body. Execution of ! will always succeed remove_dup ([Head|Tail], Result) :- member(Head, Tail), !,remove_dup(Tail, Result). Without cut, alternative solution search will continue. But when cut is passed this isn't possible anymore. remove_dup([a,b,b,c,a], X). X = [b,c,a]; No.

Negation In order to give a positive answer to a query Prolog has to construct a proof to show that the set of facts and rules implies that query. Yes means the query is provably true. No doesn’t mean that the query is necessarily false, just not provably true. Closed world assumption: Negating everything that is not explicitly in the program.

References Endriss, U, Lecture Notes, An Introduction to Prolog Programming, King's College, London