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),

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Modern Programming Languages, 2nd ed.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Declarative Programming Lists in PROLOG Autumn 2014.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Chapter Three: Lists, Operators, Arithmetic CS 461.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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).
1 Programming Languages and Paradigms Lisp Programming.
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.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Formal Models of Computation Part II The Logic Model
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)
LING 388: Language and Computers Sandiway Fong Lecture 4.
Lists in Python.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
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)
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.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
1 Section 1.6 Sets. 2 Set Fundamental discrete structure on which all other discrete structures are built Can be loosely defined as a collection of elements.
Chapter 2 Section 2.1 Sets and Set Operations. A set is a particular type of mathematical idea that is used to categorize or group different collections.
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.
Artificial Intelligence
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
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.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Section 16.5, 16.6 plus other references
CS314 – Section 5 Recitation 9
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Lists in Lisp and Scheme
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Presentation transcript:

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), hens(3, french), doves(2, turtle), partridge(1, in(tree(1,pear)))] –[X, Y, Z, Z, Y] –[1, brian, doves(2, turtle), Z]

Representations of Lists n Written in [square] brackets –Note: (parentheses), [brackets], {braces} –mean different things in computer languages n Elements, separated, by, commas n Each element can be anything –atom, term, variable, or list n Simplest list is the empty list: []

Head and Tail n First element of a list is called its head n Everything else in list is part of the tail –tail AKA “the rest of the list” –tail is itself a list n [1, b, 3, 5] –head is 1 – note: an atom (in this example) –tail is [b, 3, 5] – note: a list (always)

List Decapitation n Can separate the head from the tail n [Head | Tail] –use upright bar instead of comma ?- [1, b, 3, 5] = [Head | Tail]. Head = 1 Tail = [b, 3, 5] Yes

List Deconstruction n Can actually pull any number of elements off the front of the list ?- [1, 2, 3, 4, 5, 6] = [A, B, C | Tail]. A = 1 B = 2 C = 3 Tail = [4, 5, 6] No comma in front of the bar

Deconstruct Only At Front n Can’t get at the back elements (directly) ?- [1, 2, 3, 4, 5] = [Head | Tail, Last]. No n Only one item appears after the bar –it will become a list, if it isn’t already

Empty Tails n It’s OK if the tail is the empty list ?- [Head | Tail] = [alone]. Head = alone, Tail = [] ?- [A, B | Rest] = [1, 2]. A = 1, B = 2, Rest = [] ?- [A, B, C | More] = [1, 2]. No

Constructing Lists n Same way as you deconstruct them –only in reverse ?- _H = 1, _T = [2, 3, 4], L = [_H | _T]. L = [1, 2, 3, 4] Yes n Can use other list forms as well –[A, B, C, D] or [A, B, C | Tail], for example

Exercise n Show Prolog’s response… ?- [4, 3, 2, 1] = [H | T]. ?- [S, E, N, D | More] = [2, 4, 6, 8, 10]. ?- [T, h, I, s] = [t, H, a, T]. ?- [1, 2, 3 | More] = [X, Y, Z, Z, Y].

Lists in Terms n Lists can be arguments in terms –a([1, 2, 3]) –b(x, [w, e, r]) n And also any combinations thereof –[a([1, 2, 3]), [b(x, [w, e, r]), c], d, e]

Lists in Lists n A list can be an element of a list n [[a,b,c], [1,2,3], [do,re,mi]] has 3 elements –first element is the list [a, b, c] –second element is the list [1, 2, 3] –third/last element is the list [do, re, mi] n Can have lists in lists in lists as deep as you want – tho it can get hard to read –[a, [b], [[c,d], [e,f,g], [h,[[[[[i, j]]]]]]]

Built-In Predicates for Lists n Find the length of a list –length/2 n Ask if some element in a list –member/2 n Add one list to the end of another –append/3conc/3 in the text ?- apropos(list).% for more

The Length of a List n Want to know how long a list is ?- length([1, 2, 3, 4], N). N = 4 Yes n Only counts top-level elements ?- length([a, [b, c, d], e], N). N = 3 Yes

The length/2 Relationship n Can also generate list of desired length ?- length(L, 3). L = [_G263, _G269, _G272] Yes n length/2 is a relationship –between a list and a natural number –holds if the list has that many elements –not just for “counting elements”

Generating Every List n Can ask for any list of any length ?- length(L, N). L = [], N = 0 ; L = [_G1], N = 1 ; L = [_G1, _G2], N = 2 ; L = [_G1, _G2, _G3], N = 3 ; … n It’ll generate answers as long as you like

Printing Long Lists n Prolog may not print all the elements of a long list –[1, 2, 3, 4, 5, 6, 7, 8, 9|…] –Saves space – easier to read n If you want to see the whole thing: –type w instead of space or semi-colon –(semi-)permanent change; type p to go back –also applies when tracing

List Membership n We have some thing X and some list L n Want to know if X is in L ?- member(X, L). n Note: looks only at the top-level elements –member(b, [a, b, c]) succeeds –member(b, [a, [b, c], d]) fails –member(b, list(a, b, c, d)) fails

Taking Elements One at a Time n member/2 will, as usual, fill in variables as required to get true answers ?- member(X, [1, 2, 3]). X = 1 ; X = 2 ; X = 3 ; No member/2 is a relationship holds between X and L if X is in L

Filling In a List n Variables are instantiated in the list itself ?- member(3, [1, 2, X, 4]). X = 3 Yes ?- length(L, 2), member(1, L), member(2, L). L = [1, 2] ; L = [2, 1] Yes

member/2 Implemented in Prolog n X is in the list if: –it’s the head of the list, or –it’s in the tail of the list ?- listing(member). member(A, [A|B]). member(A, [B|C]) :- member(A, C). Recursive definition Common in predicates for lists

member/2 Clauses n Base case: –member(H, [H | _]). –first element of a list is a member of the list –[H | _] is H followed by anything (pattern) n Recursive case –member(E, [_ | T]) :- member(E, T). –member of tail of list is member of list

Exercise n Show (all) Prolog’s responses… ?- length([1, 2, [3, 4], [5, 6]], N). ?- length([1, 2, 3 | T], 4), member(5, T). ?- length(L, 3), member(a, L), member(b, L). ?- member(a, [[X, Y]]), member(b, [[X, Y]]).

List Concatenation n Want to put two lists together n Want to split a list in two ?- append([1, 2, 3], [4, 5, 6], L). L = [1, 2, 3, 4, 5, 6] Yes ?- append(L1, L2, [1, 2, 3, 4, 5, 6]). L1 = [], L2 = [1, 2, 3, 4, 5, 6] ; L1 = [1], L2 = [2, 3, 4, 5, 6] ;

The append/3 Relationship n append/3 can be used with any combination of lists and variables n It’s a relationship between the three lists n Picture append(L1, L2, L3): L1L2 L3 Doesn’t matter which one(s) you know ahead of time

Pictures of Lists n Making pictures of lists can help write code n Does list L have three zs together in it? ?- append(_, [z, z, z | _], L). 1 st sublist L _z, z, z_ 2 nd sublist

_ 1 st sublist L _ 2 nd sublist Pictures of Lists n Two sub-lists together make a longer list n Elements come at the front of some list ?- append(_, [z, z, z | _], L). z, z, z

Exercise n Draw a picture & write a question to ask whether a list L has two elements the same together –[1, 2, 2, 3, 4] does –[1, 2, 3, 2, 3] does not

Solution n Picture and code ?- append(_, [X, X | _], L). 1 st sublist L _X, X_ 2 nd sublist

Question to Predicate n Rewrite the query above as a predicate doubled_element/2 –first argument is the list –second argument is the doubled element doubled_element(L, X) :- append(_, [X, X | _], L). n Works with values or variables everywhere

Exercise n Draw a picture of a list that starts and ends with the same element n Write a question that asks whether L has this property (use append/3) n Translate the query into a predicate start_end/2 –1 st arg. is list, 2 nd is start/end element

Other Predicates with Lists n Facts about lists months([jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]). –this is a list of the months (in order!) n Rules with lists is_month(M) :- months(MonthList), member(M, MonthList).

Note on Efficiency n months/1 is a fact with one clause –only one solution n member/2 –two clauses – one fact, one rule (use listing/1) –multiple solutions n Ask months/1 first –apply facts early –apply questions with less answers early

Note on Style n Having a months/1 predicate is (slightly!) slower than hand coding list into is_month n But it’s more general –can replace with full names… –…or with French, Spanish, … –use for other predicates that need months n Equivalent to declaring a constant –in style if not efficiency

Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X) for each element X of L n Identify the predicate & its argument(s) –we’ll talk about the ? soon n State the relationship/property that holds

Logic of List Testing n Every element is male n Going to use recursion (no loops!) n Base case –??? n Recursive case –??? What’s the simplest list of all males? No, there’s one simpler than that! What to do if it’s NOT the simplest case?

All Males % all_male(?L) %-- male(X) for all elements X of L all_male([]). all_male([H | T]) :- male(H),all_male(T).

General List Use n Common to want to do stuff with every element of a list –test it, make a copy of it, … n Multi-clause predicate (usually 2) –first clause is base case (usually 0 or 1 element) –last clause recursive case (extracts head, recurs on tail) –extra cases as required

On ?, + and – n Documented argument with ? in front of it –means that arg. can be variable or value n Alternatives are + –should have a value –unbound variables not allowed/bad idea n And – –should not have a value –unbound variables only “bound” = has a value other than just another variable Used in system documentation see help(member), help(is)

Exercise n Write a predicate that takes a fully filled in list of numbers and creates (or tests) a second list where each element is 1 more than in the first list –one_more([1,2,3], L) makes L=[2,3,4] –one_more([1,2,4], [2,3,5]) succeeds –one_more([1,2,5], [2,3,7]) fails n Document it

Case Analysis n Simplest list is the empty list n If the first list is empty, what should the second list be? n If the first list is not empty, how is the head of the second list related to the head of the first? n How are the tails related?

Documentation n How is the second list related to the first? n What notations on the arguments? –?? –+? ––? n What shall we call the arguments?

Possible Solution % one_more(+Less, ?More) %-- N is M+1 for each N in More % and the corresponding M in Less one_more([], []). one_more([M | Ms], [N | Ns]) :- N is M + 1, one_more(Ms, Ns).

Logical Addition %one_more_1(?Less, ?More) % -- N is M+1 for each N in More % and the corresponding M in Less % -- Note: N or M must be instantiated one_more_1([], []). one_more_1([M | Ms], [N | Ns]) :- plus(M, 1, N), one_more(Ms, Ns). % M + 1 is equal to N

Miscellaneous Math Stuff n Logical addition (will fill in any one variable) –plus(X, Y, Z) X + Y is equal to Z –succ(M, N) M + 1 is equal to N n Comparisons (variables must be bound) –M < N, N < P M < N & N < P –M = = P M  N & N  P –N \= PN  P

By Cases in Prolog n Suppose we want to write larger/3 –3 rd argument is larger of 1 st two n Two cases: –first argument is larger than second –first argument is less than or equal to second % larger(+First, +Second, ?Larger) larger(First, Second, First) :- First > Second. larger(First, Second, Second) :- First =< Second.

Exercise n Write a predicate all_leq/2 –each element of the first list is less than or equal to the corresponding element of the second –(also, lists are the same length) ?- all_leq([1, 2, 3, 4], [1, 2, 3, 5]). Yes ?- all_leq([1, 2, 3, 4, 5], [7, 6, 5, 4, 3]). No

Getting Elements by Position n last/2 queries last element of a list n nth1/3 queries nth element of a list –starts counting at 1 n nth0/3 queries nth element of a list –starts counting at 0 ?- last(_Last, [a, c, e]), nth0(1, [a, c, e], _Second), nth1(2, L, _Second), nth1(1, L, _Last). L = [e, c | _G1]

Insertion and Deletion n Insertion to & deletion from lists –just opposites –three way relationship –shorter list, extra element, longer list –use same predicate for both –select/3 in SWI-Prolog

Using select/3 n To delete an element ?- select([g, o, n, e], n, L). L = [g, o, e] n To insert an element ?- select(L, n, [g, o, e]). L = [n, g, o, e] ; L = [g, n, o, e] ; L = [g, o, n, e] ;

Using select/3 n Deleting any element ?- select([a, b, c], X, Rest). X = a, Rest = [b, c] ; X = b, Rest = [a, c] ; X = c, Rest = [a, b] ; No

Using select/3 n To insert into a given position (1-based) ?- nth1(3, L, n), select(L, n, [g, o, e]). L = [g, o, n, e] Yes n Exercise: change that into a predicate % insertKth(Old, Kth, Value, New) %New is Old with Value inserted into Kth place %(first element of list = position 1)

Next Time n Using Structures and Lists