© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

Formal Models of Computation Part II The Logic Model
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
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.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 12: Working with Files This lecture is concerned with various aspects of file handling and.
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.
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Theory –Discuss database manipulation in.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 8: More DCGs Theory –Examine two important capabilities offered by DCG notation: Extra arguments.
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 Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 7: Definite Clause Grammars Theory –Introduce context free grammars and some related concepts.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
LISP and PROLOG AI Programming Language Submitted To: Dr. Hesham El-Zoka Submitted By: Eng. Ismail Fathalla El-Gayar.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
LING 388: Language and Computers Sandiway Fong Lecture 4.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Agile Software Development Lab Dr. Günter Kniesel, Daniel Speicher, Tobias Rho Spring 2008 Prolog - Part 2 Patrick Rypalla Alexis Raptarchis
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
11.2 Sets and Compound Inequalities 11.3 Absolute Value Equations 11.4 Absolute Value Inequalities.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
CS 103 Discrete Structures Lecture 19 Relations. Chapter 9.
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
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?
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
1 Artificial Intelligence CS370D Prolog programming List operations.
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.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Instructor :Bayan Ghozlan الاستاذه : بيان غزلان.  البرولوغ Prolog هي لغة برمجة منطقية. وتعني برمجه المنطق programing language  تم اختراع اللغة بواسطة.
Section 16.5, 16.6 plus other references
Chapter 11 :: Logic Languages
Lists Prolog Pepper Credit to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, (ISBN )
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Programming Techniques
Lecture 7: Definite Clause Grammars
Lisp.
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog programming –Define the member/2 predicate, a fundamental Prolog tool for manipulating lists –Illustrate the idea of recursing down lists Exercises –Exercises of LPN chapter 4 –Practical work

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lists A list is a finite sequence of elements Examples of lists in Prolog: [mia, vincent, jules, yolanda] [mia, robber(honeybunny), X, 2, mia] [ ] [mia, [vincent, jules], [butch, friend(butch)]] [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists List elements are enclosed in square brackets The length of a list is the number of elements it has All sorts of Prolog terms can be elements of a list There is a special list: the empty list [ ]

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail A non-empty list can be thought of as consisting of two parts –The head –The tail The head is the first item in the list The tail is everything else –The tail is the list that remains when we take the first element away –The tail of a list is always a list

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1 [mia, vincent, jules, yolanda] Head: Tail:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1 [mia, vincent, jules, yolanda] Head: mia Tail:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1 [mia, vincent, jules, yolanda] Head: mia Tail: [vincent, jules, yolanda]

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3 [dead(z)] Head: Tail:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3 [dead(z)] Head: dead(z) Tail:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3 [dead(z)] Head: dead(z) Tail: [ ]

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and tail of empty list The empty list has neither a head nor a tail For Prolog, [ ] is a special simple list without any internal structure The empty list plays an important role in recursive predicates for list processing in Prolog

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | Prolog has a special built-in operator | which can be used to decompose a list into its head and tail The | operator is a key tool for writing Prolog list manipulation predicates

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X|Y] = [mia, vincent, jules, yolanda]. X = mia Y = [vincent,jules,yolanda] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X|Y] = [ ]. no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]]. X = [ ] Y = dead(z) Z = _4543 Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Anonymous variable Suppose we are interested in the second and fourth element of a list ?- [X1,X2,X3,X4|Tail] = [mia, vincent, marsellus, jody, yolanda]. X1 = mia X2 = vincent X3 = marsellus X4 = jody Tail = [yolanda] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Anonymous variables There is a simpler way of obtaining only the information we want: ?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda]. X2 = vincent X4 = jody yes ?- The underscore is the anonymous variable

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The anonymous variable Is used when you need to use a variable, but you are not interested in what Prolog instantiates it to Each occurrence of the anonymous variable is independent, i.e. can be bound to something different

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercises Exercise 4.1 of LPN Exercise 4.2 of LPN

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Member One of the most basic things we would like to know is whether something is an element of a list or not So let`s write a predicate that when given a term X and a list L, tells us whether or not X belongs to L This predicate is usually called member/2

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]). no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]). X = yolanda; X = trudy; X = vincent; X = jules; no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Rewriting member/2 member(X,[X|_]). member(X,[_|T]):- member(X,T).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Recursing down lists The member/2 predicate works by recursively working its way down a list –doing something to the head, and then –recursively doing the same thing to the tail This technique is very common in Prolog and therefore very important that you master it So let`s look at another example!

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: a2b/2 The predicate a2b/2 takes two lists as arguments and succeeds –if the first argument is a list of as, and –the second argument is a list of bs of exactly the same length ?- a2b([a,a,a,a],[b,b,b,b]). yes ?- a2b([a,a,a,a],[b,b,b]). no ?- a2b([a,c,a,a],[b,b,b,t]). no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining a2b/2: step 1 Often the best away to solve such problems is to think about the simplest possible case Here it means: the empty list a2b([],[]).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining a2b/2: step 2 Now think recursively! When should a2b/2 decide that two non-empty lists are a list of as and a list of bs of exactly the same length? a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Testing a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a],[b,b,b]). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Testing a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a,a],[b,b,b]). no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Testing a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,t,a,a],[b,b,b,c]). no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Further investigating a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a,a,a], X). X = [b,b,b,b,b] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Further investigating a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b(X,[b,b,b,b,b,b,b]). X = [a,a,a,a,a,a,a] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Summary of this lecture In this lecture we introduced list and recursive predicates that work on lists The kind of programming that these predicates illustrated is fundamental to Prolog You will see that most Predicates you will write in your Prolog career will be variants of these predicates

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Next lecture Introduce arithmetic in Prolog –Introduce Prolog`s built-in abilities for performing arithmetic –Apply them to simple list processing problems –Introduce the idea of accumulators