11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
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: ?-
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
Prolog.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Declarative Programming Lists in PROLOG Autumn 2014.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
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).
Logic Programming Lecture 3: Recursion, lists, and data structures.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Induction and recursion
Formal Models of Computation Part II The Logic Model
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CS 321 Programming Languages and Compilers Prolog part 2.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
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 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
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.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
1 Artificial Intelligence CS370D Prolog programming List operations.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
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:... :-....
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
Lists in Lisp and Scheme
PROLOG.
Tests, Backtracking, and Recursion
String and Lists Dr. José M. Reyes Álamo.
Programming Techniques
Presentation transcript:

11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 6 11/10/04

AIPP Lecture 6: Built-in Predicates2 Collecting Results Last time we discussed three methods of collecting results: 1.Compute result at base case first, then use this result as you backtrack through the program. 2.Accumulate a result as you recurse into the program and finalise it at the base case. 3.Recurse on an uninstantiated variable and accumulate results on backtracking. Today we will look how you can use an accumulator during recursion and backtracking to combine lists. | ?- L1=[a,b], L2=[c,d], Z=[L1|L2]. L1 = [a,b], L2 = [c,d], Z = [[a,b],c,d] ?

11/10/04AIPP Lecture 6: Built-in Predicates3 |?- pred([a,b],[c,d],Out). Out = [a,b,c,d]. To add L1 to L2 during recursion we can use the bar notation to decompose L1 and add the Head to L2. pred([H|T],L2,Out):- pred(T,[H|L2],Out). We need to have an extra variable ( Out ) which can be used to pass back the new list once L1 is empty. pred([],L2,L2).  base case: when L1 is empty make the new list equal to the Output list. The base case must go before the recursive case. Desired behaviour Constructing a list during Recursion Accumulator

11/10/04AIPP Lecture 6: Built-in Predicates4 | ?- pred([a,b],[c,d],Out). 1 1 Call: pred([a,b],[c,d],_515) ? 2 2 Call: pred([b],[a,c,d],_515) ? 3 3 Call: pred([],[b,a,c,d],_515) ? 3 3 Exit: pred([],[b,a,c,d],[b,a,c,d]) ? 2 2 Exit: pred([b],[a,c,d],[b,a,c,d]) ? 1 1 Exit: pred([a,b],[c,d],[b,a,c,d]) ? Out = [b,a,c,d] ? yes pred([],L2,L2). pred([H|T],L2,Out):- pred(T,[H|L2],Out). If you construct a list through recursion (on the way down) and then pass the answer back the elements will be in reverse order. Constructing a list during Recursion (2) Always the same variable

11/10/04AIPP Lecture 6: Built-in Predicates5 reverse/3 | ?- pred([a,b],[c,d],Out). 1 1 Call: pred([a,b],[c,d],_515) ? 2 2 Call: pred([b],[a,c,d],_515) ? 3 3 Call: pred([],[b,a,c,d],_515) ? 3 3 Exit: pred([],[b,a,c,d],[b,a,c,d]) ? 2 2 Exit: pred([b],[a,c,d],[b,a,c,d]) ? 1 1 Exit: pred([a,b],[c,d],[b,a,c,d]) ? Out = [b,a,c,d] ? yes reverse([],L2,L2). reverse([H|T],L2,Out):- reverse(T,[H|L2],Out). If you construct a list through recursion (on the way down) and then pass the answer back the elements will be in reverse order.

11/10/04AIPP Lecture 6: Built-in Predicates6 To maintain the order of list elements we need to construct the list on the way out of the program, i.e. through backtracking. Use the same bar deconstruction as before but add the head element of L1 to Out in the Head of the clause. pred([H|T],L2,[H|Out]):-  Head is not added pred(T,L2,Out). until backtracking. Now when we reach the base case we make L2 the foundation for the new Out list and add our L1 elements to it during backtracking. pred([],L2,L2).  base case: when L1 is empty make the new list equal to the Output list. Constructing a list in backtracking

11/10/04AIPP Lecture 6: Built-in Predicates7 append/3 | ?- pred2([a,b],[c,d],Out). 1 1 Call: pred2([a,b],[c,d],_515) ? 2 2 Call: pred2([b],[c,d],_1131) ? 3 3 Call: pred2([],[c,d],_1702) ? 3 3 Exit: pred2([],[c,d],[c,d]) ? 2 2 Exit: pred2([b],[c,d],[b,c,d]) ? 1 1 Exit: pred2([a,b],[c,d],[a,b,c,d]) ? Out = [a,b,c,d] ? yes append([],L2,L2). append([H|T],L2,[H|Rest]):- append(T,L2,Rest). * append/3 is another very common user-defined list processing predicate. Variable changes at every Call.

11/10/04AIPP Lecture 6: Built-in Predicates8 Both reverse/3 and append/3 can be used backwards to make two lists out of one. This can be a useful way to strip lists apart and check their contents. | ?- append(X,Y,[a,b,c,d]). X = [], Y = [a,b,c,d] ? ; X = [a], Y = [b,c,d] ? ; X = [a,b], Y = [c,d] ? ; X = [a,b,c], Y = [d] ? ; X = [a,b,c,d], Y = [] ? ; no |?-append(X,[c,d],[a,b,c,d]). X = [a,b] ? yes append([],L2,L2). append([H|T],L2,[H|Rest]):- append(T,L2,Rest). Computing in reverse

11/10/04AIPP Lecture 6: Built-in Predicates9 reverse([],L2,L2). reverse([H|T],L2,Out):- reverse(T,[H|L2],Out). Computing in reverse Both reverse/3 and append/3 can be used backwards to make two lists out of one. This can be a useful way to strip lists apart and check their contents. | ?- reverse(X,Y,[a,b,c,d]). X = [], Y = [a,b,c,d] ? ; X = [a], Y = [b,c,d] ? ; X = [b,a], Y = [c,d] ? ; X = [c,b,a], Y = [d] ? ; X = [d,c,b,a], Y = [] ? ; *loop* |?-reverse([d,c,b,a],Y,[a,b,c,d]). Y = [] ? yes

11/10/04AIPP Lecture 6: Built-in Predicates10 Built-in Predicates var/1 nonvar/1 atom/1 atomic/1 number/1 Identifying terms integer/1 float/1 compound/1 ground/1 =../2 functor/3 Decomposing structures arg/3 findall/3 setof/3 Collecting all solutions bagof/3

11/10/04AIPP Lecture 6: Built-in Predicates11 Identifying Terms These built-in predicates allow the type of terms to be tested. var(X) succeeds if X is currently an uninstantiated variable. nonvar(X) succeeds if X is not a variable, or already instantiated atom(X) is true if X currently stands for an atom number(X) is true if X currently stands for a number integer(X) is true if X currently stands for an integer float(X) is true if X currently stands for a real number. atomic(X) is true if X currently stands for a number or an atom. compound(X) is true if X currently stands for a structure. ground(X) succeeds if X does not contain any uninstantiated variables.

11/10/04AIPP Lecture 6: Built-in Predicates12 var(X) succeeds if X is currently an uninstantiated variable. | ?- var(X). | ?- X = 5, var(X). | ?- var([X]). true ? no no yes nonvar(X) Conversely, nonvar/1 succeeds if X is not a variable, or already instantiated. | ?- nonvar(X). | ?- X = 5,nonvar(X). | ?-nonvar([X]). no X = 5 ? true ? yes yes atom(X) is true if X currently stands for an atom: a non- variable term with 0 arguments, and not a number | ?- atom(paul). | ?- X = paul,atom(X). | ?- atom([]). yes X = paul ? yes | ?-atom([a,b]). no var/1, nonvar/1, atom/1

11/10/04AIPP Lecture 6: Built-in Predicates13 number(X) is true if X currently stands for any number | ?- number(X). | ?- X=5,number(X). | ?- number(5.46). no X = 5 ? yes yes To identify what type of number it is use: integer(X) is true if X currently stands for an integer (a whole positive or negative number or zero). float(X) is true if X currently stands for a real number. | ?- integer(5). | ?- integer(5.46). yes no | ?- float(5). | ?- float(5.46). no yes number/1, integer/1, float/1

11/10/04AIPP Lecture 6: Built-in Predicates14 atomic/1, compound/1, ground/1 If atom/1 is too specific then you can use atomic/1 which accepts numbers and atoms. | ?- atom(5). | ?- atomic(5). no yes If atomic/1 fails then the term is either an uninstantiated variable (which you can test with var/1 ) or a compound term: |?-compound([]). |?-compound([a]). |?-compound(b(a)). no yes yes ground(X) succeeds if X does not contain any uninstantiated variables. Also checks inside compound terms. |?-ground(X). |?-ground(a(b,X)). no |?-ground(a). |?-ground([a,b,c]). yesyes

11/10/04AIPP Lecture 6: Built-in Predicates15 Decomposing Structures When using compound structures you can’t use a variable to check or make a functor. |?- X=tree, Y = X(maple). Syntax error Y=X >(maple) functor(T,F,N) is true if F is the principal functor of T and N is the arity of F. arg(N,Term,A) is true if A is the Nth argument in Term. |?-functor(t(f(X),a,T),Func,N). |?-arg(2,t(t(X),[]),A). N = 3, Func = t ? A = [] ? yes | ?- functor(D,date,3), arg(1,D,11), arg(2,D,oct), arg(3,D,2004). D = date(11,oct,2004) ? yes

11/10/04AIPP Lecture 6: Built-in Predicates16 Decomposing Structures (2) We can also decompose a structure into a list of its components using =../2. Term =.. L is true if L is a list that contains the principal functor of Term, followed by its arguments. | ?- f(a,b) =.. L. |?- T =.. [is_blue,sam,today]. L = [f,a,b] ? T = is_blue(sam,today) ? yes By representing the components of a structure as a list they can be recursively processed without knowing the functor name. | ?- f(2,3)=..[F,N|Y], N1 is N*3, L=..[F,N1|Y]. L = f(6,3)? yes

11/10/04AIPP Lecture 6: Built-in Predicates17 Collecting all solutions You've seen how to generate all of the solutions to a given goal, at the SICStus prompt (;): | ?- member(X, [1,2,3,4]). X = 1 ? ; X = 2 ? ; X = 3 ? ; X = 4 ? ; no It would be nice if we could generate all of the solutions to some goal within a program. There are three similar built-in predicates for doing this: findall/3 setof/3 bagof/3

11/10/04AIPP Lecture 6: Built-in Predicates18 Meta-predicates findall/3, setof/3, and bagof/3 are all meta-predicates –they manipulate Prolog’s proof strategy. findall(X,P,L) setof(X,P,L) All produce a list L of all the objects X such bagof(X,P,L) that goal P is satisfied (e.g. age(X,Age) ). They all repeatedly call the goal P, instantiating the variable X within P and adding it to the list L. They succeed when there are no more solutions. Exactly simulate the repeated use of ‘;’ at the SICStus prompt to find all of the solutions.

11/10/04AIPP Lecture 6: Built-in Predicates19 findall/3 findall/3 is the most straightforward of the three, and the most commonly used: | ?- findall(X, member(X, [1,2,3,4]), Results). Results = [1,2,3,4] yes This reads: `find all of the Xs, such that X is a member of the list [1,2,3,4] and put the list of results in Results '. Solutions are listed in the result in the same order in which Prolog finds them. If there are duplicated solutions, all are included. If there are infinitely-many solutions, it will never terminate!

11/10/04AIPP Lecture 6: Built-in Predicates20 findall/3 (2) We can use findall/3 in more sophisticated ways. The second argument, which is the goal, might be a compound goal: | ?- findall(X, (member(X, [1,2,3,4]), X > 2), Results). Results = [3,4]? yes The first argument can be a term of any complexity: |?- findall(X/Y, (member(X,[1,2,3,4]), Y is X * X), Results). Results = [1/1, 2/4, 3/9, 4/16]? yes

11/10/04AIPP Lecture 6: Built-in Predicates21 setof/3 setof/3 works very much like findall/3, except that: –It produces the set of all results, with any duplicates removed, and the results sorted. –If any variables are used in the goal, which do not appear in the first argument, setof/3 will return a separate result for each possible instantiation of that variable: |?-setof(Child, age(Child,Age),Results). Age = 5, Results = [ann,tom] ? ; Age = 7, Results = [peter] ? ; Age = 8, Results = [pat] ? ; no age(peter, 7). age(ann, 5). age(pat, 8). age(tom, 5). age(ann, 5). Knowledge base

11/10/04AIPP Lecture 6: Built-in Predicates22 setof/3 (2) We can use a nested call to setof/3 to collect together the individual results: | ?- setof(Age/Children, setof(Child,age(Child,Age), Children), AllResults). AllResults = [5/[ann,tom],7/[peter],8/[pat]] ? yes If we don't care about a variable that doesn't appear in the first argument, we use the following form: | ?- setof(Child, Age^age(Child,Age), Results). Results = [ann,pat,peter,tom] ? ; no This reads: `Find the set of all children, such that the Child has an Age (whatever it might be), and put the results in Results.'

11/10/04AIPP Lecture 6: Built-in Predicates23 bagof/3 bagof/3 is very much like setof/3 except: –that the list of results might contain duplicates, –and isn’t sorted. | ?- bagof(Child, age(Child,Age),Results). Age = 5, Results = [tom,ann,ann] ? ; Age = 7, Results = [peter] ? ; Age = 8, Results = [pat] ? ; no bagof/3 is different to findall/3 as it will generate separate results for all the variables in the goal that do not appear in the first argument. | ?- findall(Child, age(Child,Age),Results). Results = [peter,pat,tom,ann,ann] ? ; no

11/10/04AIPP Lecture 6: Built-in Predicates24 Showed two techniques for combining lists: –Use an accumulator to build up result during recursion: reverse/3 –Build result in the head of the clause during backtracking: append/3 Built-in predicates –Identifying Terms var/1, nonvar/1, atom/1, atomic/1, number/1, integer/1, float/1, compound/1, ground/1 –Decomposing Structures =../2, functor/3, arg/3 –Collecting all solutions findall/3, setof/3, bagof/3 Summary