30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.

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.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
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.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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).
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
© 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 Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
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.
CS 321 Programming Languages and Compilers Prolog part 2.
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.
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.
F28PL1 Programming Languages Lecture 16: Prolog 1.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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.
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.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
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.
© 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.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
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.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Logic Programming Lecture 2: Unification and proof search.
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
A Conjunction is a logical operator that connects two logical terms.
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.
Algorithm design and Analysis
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Programming Techniques
PROLOG.
Presentation transcript:

30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 4 04/10/04

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists2 The central ideas of Prolog SUCCESS/FAILURE –any computation can “succeed'' or “fail'', and this is used as a ‘test‘ mechanism. MATCHING –any two data items can be compared for similarity, and values can be bound to variables in order to allow a match to succeed. 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 –when the system fails during its search, it returns to previous choices to see if making a different choice would allow success.

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists3 Likes program 1) drinks(alan,beer). 2) likes(alan,coffee). 3) likes(heather,coffee). 4) likes(Person,Drink):- drinks(Person,Drink). 5) likes(Person,Somebody):- likes(Person,Drink), likes(Somebody,Drink).

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists4 Representing Proof using Trees To help us understand Prolog’s proof strategy we can represent its behaviour using AND/OR trees. 1.Query is the top-most point (node) of the tree. 2.Tree grows downwards (looks more like roots!). 3.Each branch denotes a subgoal. 1.The branch is labelled with the number of the matching clause and 2. any variables instantiated when matching the clause head. 4.Each branch ends with either: 1.A successful match, 2.A failed match, or 3.Another subgoal. |?- likes(alan,X). 2 X/coffee X = coffee 1 st solution = “Alan likes coffee.”

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists5 Representing Proof using Trees (2) |?- likes(alan,X). X/coffee X = coffee Using the tree we can see what happens when we ask for another match ( ; ) 2 4 drinks(alan,X). 1 X/beer X = beer 2 nd solution = “Alan likes beer because Alan drinks beer.” 1 st match is failed and forgotten Backtracking

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists6 Recursion using Trees |?- likes(alan,X). X/coffee X = coffee When a predicate calls itself within its body we say the clause is recursing X/beer X = beer 5 likes(alan,Drink) Conjoined subgoals likes(Somebody,Drink) drinks(alan,X). X/coffee 2 X = coffee

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists7 |?- likes(alan,X). X/coffee X = coffee X/beer X = beer 3 rd solution = “Alan likes Alan because Alan likes coffee.” 5 likes(alan,coffee) likes(Somebody,coffee) drinks(alan,X). X/coffee 2 X = coffee Somebody /alan 2 Somebody = alan Recursion using Trees (2) When a predicate calls itself within its body we say the clause is recursing

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists8 |?- likes(alan,X). X/coffee X = coffee X/beer X = beer 4 th solution = “Alan likes Heather because Heather likes coffee.” 5 likes(alan,coffee) likes(Somebody,coffee) drinks(alan,X). X/coffee 2 X = coffee Somebody /alan 2 Somebody = alan Somebody / heather 3 Somebody = heather When a predicate calls itself within its body we say the clause is recursing Recursion using Trees (3)

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists9 Infitite Recursive Loop If a recursive clause is called with an incorrect goal it will loop as it can neither prove it nor disprove it. likes(Someb,coffee) 2 Somebody = alan 3 Somebody = heather 5 likes(Someb,coffee) Someb = alan 2 likes(coffee,coffee) likes(coffee,X) likes(coffee,X2) likes(coffee,X3) likes(X,X2) likes(X2,X3) likes/2 is a left recursive clause.

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists10 Why use recursion? It allows us to define very clear and elegant code. –Why repeat code when you can reuse existing code. Relationships may be recursive e.g. “X is my ancestor if X is my Ancestor’s ancestor.” Data is represented recursively and best processed iteratively. –Grammatical structures can contain themselves –E.g. NP  (Det) N (PP), PP  P (NP) –Ordered data: each element requires the same processing Allows Prolog to perform complex search of a problem space without any dedicated algorithms.

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists11 Prolog Data Objects (Terms) Simple objects Structured Objects Constants IntegersAtoms Symbols Strings Signs VariablesStructuresLists a bob l8r_2day ‘a’ ‘Bob’ ‘L8r 2day’ ==> … X A_var _Var date(4,10,04) person(bob,48) [] [a,b,g] [[a],[b]] [bit(a,d),a,’Bob’]

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists12 Structures To create a single data element from a collection of related terms we use a structure. A structure is constructed from a functor (a constant symbol) and one of more components. somerelationship(a,b,c,[1,2,3]) The components can be of any type: atoms, integers, variables, or structures. As functors are treated as data objects just like constants they can be unified with variables |?- X = date(04,10,04). X = date(04,10,04)? yes functor

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists13 Structure unification 2 structures will unify if –the functors are the same, –they have the same number of components, –and all the components unify. | ?- person(Nm,london,Age) = person(bob,london,48). Nm = bob, Age = 48? yes | ?- person(Someone,_,45) = person(harry,dundee,45). Someone = harry ? yes (A plain underscore ‘_’ is not bound to any value. By using it you are telling Prolog to ignore this argument and not report it.)

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists14 Structure unification (2) A structure may also have another structure as a component. |?-addr(flat(4),street(‘Home Str.’),postcode(eh8_9lw)) = addr(flat(Z),Yy,postcode(Xxx)). Z = 4, Yy = street(‘Home Str.’), Xxx = eh8_9lw ? yes Unification of nested structures works recursively: –first it unifies the entire structure, –then it tries to unify the nested structures. Remember to close brackets! Reported variables are ordered according to number of characters in the variable name.

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists15 Structures = facts? The syntax of structures and facts is identical but: –Structures are not facts as they are not stored in the database as being true (followed by a period ‘.’); –Structures are generally just used to group data; –Functors do not have to match predicate names. However predicates can be stored as structures command(X):- X. | ?- X = write(‘Passing a command’), command(X). Passing a command X = write('Passing a command') ? yes By instantiating a variable with a structure which is also a predicate you can pass commands.

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists16 Lists A collection of ordered data. Has zero or more elements enclosed by square brackets (‘[ ]’) and separated by commas (‘,’). [a]  a list with one element []  an empty list [34,tom,[2,3]]  a list with 3 elements where the 3 rd element is a list of 2 elements. Like any object, a list can be unified with a variable |?- [Any, list, ‘of elements’] = X. X = [Any, list, ‘of elements’]? yes

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists17 List Unification Two lists unify if they are the same length and all their elements unify. |?-[a,B,c,D]=[A,b,C,d]. |?-[(a+X),(Y+b)]=[(W+c),(d+b)]. A = a, W = a, B = b, X = c, C = c, Y = d? D = d ? yes yes |?- [[X,a]]=[b,Y]. |?-[[a],[B,c],[]]=[X,[b,c],Y]. no B = b, X = [a], Y = [] ? yes Length 1 Length 2

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists18 Definition of a List Lists are recursively defined structures. “An empty list, [], is a list. A structure of the form [X, …] is a list if X is a term and […] is a list, possibly empty.” This recursiveness is made explicit by the bar notation –[Head|Tail] (‘|’ = bottom left PC keyboard character) Head must unify with a single term. Tail unifies with a list of any length, including an empty list, []. –the bar notation turns everything after the Head into a list and unifies it with Tail.

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists19 Head and Tail |?-[a,b,c,d]=[Head|Tail]. |?-[a,b,c,d]=[X|[Y|Z]]. Head = a, X = a, Tail = [b,c,d]? Y = b, yes Z = [c,d]; yes |?-[a] = [H|T]. |?-[a,b,c]=[W|[X|[Y|Z]]]. H = a, W = a, T = []; X = b, yes Y = c, Z = []? yes |?-[] = [H|T]. |?-[a|[b|[c|[]]]]= List. no List = [a,b,c]? yes

30/09/04AIPP Lecture 3: Recursion, Structures, and Lists20 Summary Prolog’s proof strategy can be represented using AND/OR trees. Tree representations allow us trace Prolog’s search for multiple matches to a query. They also highlight the strengths and weaknesses of recursion (e.g. economical code vs. infinite looping). Recursive data structures can be represented as structures or lists. Structures can be unified with variables then used as commands. Lists can store ordered data and allow its sequential processing through recursion.