Declarative Programming Lists in PROLOG Autumn 2014.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
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.
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.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
Prolog Programming (Volume 3) Dr W.F. Clocksin. Mapping: The Full Map sqlist(, ) sqlist([], []). sqlist([X|T], [Y|L]) :- Y is X * X, sqlist(T, L). List.
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
© 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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
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.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
What is Divisibility? Divisibility means that after dividing, there will be No remainder.
Chapter 9: Functional Programming in a Typed Language.
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.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter 2 Syntax and meaning of prolog programs Part 1.
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.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Logic Programming Lecture 2: Unification and proof search.
CS 550 Programming Languages Jeremy Johnson
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
CS 270 Math Foundations of CS Jeremy Johnson
3.5 Programming paradigms
Prolog Programming (Volume 2)
Programming Paradigms and Languages
Programming Techniques
Announcements Quiz 5 HW6 due October 23
Chapter 2 Syntax and meaning of prolog programs
Course Overview PART I: overview material PART II: inside a compiler
Lisp.
List Operation in ProLog EUSL/TC/IS/2013/COM/08. In here list will print..
Lists in Lisp and Scheme
Presentation transcript:

Declarative Programming Lists in PROLOG Autumn 2014

Example - Fibonacci numbers Fibonacci numbers are defined as follows: 0, if i = 0 F(i) = 1,if i = 1 F(i–1) + F(i – 2), if i > 1

Example - Fibonacci numbers Fibonacci numbers are defined as follows: 0, if i = 0 F(i) = 1,if i = 1 F(i–1) + F(i – 2), if i > 1 fibonacci(0,0). fibonacci(1,1). fibonacci(X,R) :- X1 is X – 1, X2 is X – 2, fibonacci(X1,R1), fibonacci(X2,R2),R is R1+R2.

Example - Fibonacci numbers We can try to write a predicate that computes both values F(i) and F(i – 1) fibonacci(1,1,0). fibonacci(X,R1,R2) :- X1 is X – 1,fibonacci(X1,R2,R3),R1 is R2+R3.

Example - Fibonacci numbers If we want, we can now add a predicate just for computing of F(i) fibonacci(1,1,0). fibonacci(X,R1,R2) :- X1 is X – 1,fibonacci(X1,R2,R3),R1 is R2+R3. fibonacci(0,0). fibonacci(X,R) :- fibonacci(X,R,R1).

Example - Fibonacci numbers What, if we want something like this: 0, if i = 0 S(i) = 1,if i = 1 i*S(0)+ (i–1)*S(1)+...+2S(i–1), if i > 1

Example - Fibonacci numbers What, if we want something like this: 0, if i = 0 S(i) = 1,if i = 1 i*S(0)+ (i–1)*S(1)+...+2S(i–1), if i > 1 A predicate that computes all the values S(0)...S(i) at once could be useful...

Lists - the idea nilrepresents the empty list list(a,nil)represents list a list(a,list(b,nil)) represents list a,b list(a,list(b,list(c,nil))) represents list a,b,c...

Example - list of Fibonacci numbers fibonacci(0,list(0,nil)). fibonacci(1,list(1,list(0,nil))). fibonacci(X,list(R, list(R1,list(R2,Z)))) :- X1 is X – 1, fibonacci(X1,list(R1,list(R2,Z))), R is R1+R2.

Lists - predefined notation []represents the empty list.(a,[])represents list a.(a,.(b,[])) represents list a,b.(a,.(b,.(c,[]))) represents list a,b,c...

Lists - predefined notation There is also a special shortcut notation for lists in PROLOG.(a,.(b,.(c,[]))) can also be written as [a,b,c]

Lists - useful operations Often it is useful to divide non-empty list into the first element (head) and the remaining list (tail) head(.(H,_),H). tail(.(_,T),T).

Lists - useful operations There are special predicate “|” that does not require the use of “.” notation head([H|_],H). tail([_|T],T).

Lists - useful operations Any fixed number of initial elements can be accessed first_7([F1,F2,F3,F4,F5,F6,F7|_],F1,F2,F3,F4,F5,F6,F7).

Lists (from Clocksin) Lists are the same as in other languages (such as ML) in that a list of terms of any length is composed of list cells that are ‘consed’ together. The list of length 0 is called nil, written []. The list of length n is.(head,tail), where tail is a list of length n-1. So a list cell is a functor ‘.’ of arity 2. Its first component is the head, and the second component is the tail.

Lists - examples (from Clocksin) nil.(a, nil).(a,.(b, nil).(a,.(b,.(c,.(d,.(e. nil))))).(a,b) (note this is a pair, not a proper list).(a, X) (this might be a list, or might not!).(a,.(b, nil)),.(c, nil))

Lists can be written as trees anil a b a b c d e ab aX a b a

Lists - PROLOG syntax Nil is written []. The list consisting of n elements t 1, t 2, …,t n is written [t 1, t 2, …,t n ]..(X,Y) is written [X|Y] [X|[]] is written [X] The term.(a,.(b,.(c,Y))) is written [a,b,c|Y]. If Y is instantiated to [], then the term is a list, and can be written [a,b,c|[]] or simply [a,b,c].

Lists - Exercise 1 Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Lists - Exercise 1 Identify the heads and tails of these lists (if any): a[b, c] [a] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Lists - Exercise 1 Identify the heads and tails of these lists (if any): [a, b, c] a[] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Lists - Exercise 1 Identify the heads and tails of these lists (if any): [a, b, c] [a] [] (not a list, so doesn’t have head and tail. nil is a constant) [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Lists - Exercise 1 Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [the, cat] [sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Lists - Exercise 1 Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [[the, cat], sat] [the, cardinal] [pulled, [off]], [each, [plum, coloured], shoe]

Lists - Exercise 2 For each pair of terms, determine whether they unify, and if so, to which terms are the variables instantiated? [X, Y, Z][john, likes, fish] [cat][X|Y] [X,Y|Z][mary, likes, wine] [[the,Y]|Z][[X,answer], [is, here]] [X, Y, X][a, Z, Z] [[X], [Y], [X]][[a], [X], [X]]

Lists - Exercise 2 A variable may be instantiated to any term. X YZ mary likes wine[] [mary, likes, wine][X,Y|Z]

“member” predicate member(X, [X|T]). member(X, [H|T]) :- member(X, T). Examples: ?- member(john, [paul, john]). ?- member(X, [paul, john]). ?- member(joe, [marx, darwin, freud]). ?- member(foo, X).

“member” predicate member(X, [X|T]). member(X, [H|T]) :- member(X, T). member(X, [X|_]). member(X, [_|T]) :- member(X, T). Notice T isn’t ‘used’ Notice H isn’t ‘used’

Exercise 3 Here is a mystery predicate. What does it do? mystery(X, A, B) :- member(X, A), member(X, B). ?- mystery(a, [b, c, a], [p, a, l]). ?- mystery(b, [b, l, u, e], [y, e, l, l, o, w]). ?- mystery(X, [r, a, p, i, d], [a, c, t, i, o, n]). ?- mystery(X, [w, a, l, n, u, t], [c, h, e, r, r, y]).

Length of a list - Exercise 4 Naïve method: length([], 0). length([H|T], N) :- length(T, NT), N is NT + 1.

Length of a list - Tail recursion Procedures such as length/2: length([], 0). length([H|T], N) :- length(T, NT), N is NT + 1. often crash when processing large lists, even with a few thousand elements. This happens because in the recursive rule, the recursive call is not the last goal on the rhs and so may need to be backtracked into. For each call, Prolog must store the choice points in a memory stack of fixed size.

Length of a list - Tail recursion No matter how large this memory stack is, it will overflow if the list is long enough. This is less likely to happen if the recursive call is the last goal on the rhs - because within each call there is no choice point for it. Procedures in which recursive calls appear as the last goal in the rule(s) are known as tail recursive. There is a way of re-writing procedures such as length/2 to make them tail recursive.

Length of a list - Tail recursion The accumulator algorithm for the length of a list is: Initialise the current sub-total, LengthSoFar, to 0. To find the length of L while L is non-empty do for each element of L remove the element and add 1 to LengthSoFar when L is empty set Length = LengthSoFar.

Length of a list - Exercise 4 Tail-recursive method: length(L, N) :- acc(L, 0, N). /* acc(List, LengthSoFar, Length) */ acc([], A, A). acc([H|T], A, N) :- A1 is A + 1, acc(T, A1, N).

Length of a list - Exercise 4 ?- length([apple, pear], N). ?- length([alpha], 2). ?- length(L, 3). Modify length to give a procedure sum such that sum(L,N) succeeds if L is a list of integers and N is their sum.

“concat” predicate - Exercise 5 Write a predicate for concatenating 2 lists, i.e. ?-concat([a,b,c,d],[1,2,3],Result). Result = [a,b,c,d,1,2,3] ? Yes

“reverse” predicate - Exercise 6 Write a predicate for reversing a list, i.e. ?-reverse([a,b,c,d],Result). Result = [d,c,b,a] ? Yes

“sublist” predicate - Exercise 7 Write a predicate that checks whether a given list is a sublist (in the same order) of another, i.e. ?-sublist([a,b,c,d],[a,c]). Yes ?-sublist([a,b,c,d],[c,a]). No

“setify” predicate - Exercise 8 Write a predicate that removes duplicate elements from a given list, i.e. ?-setify([a,b,a,a,c,b,a,d],Result). Result = [a,b,c,d] ? Yes

“split” predicate - Exercise 9 Write a predicate that splits a given list of numbers into lists of odd and even numbers, i.e. ?-split([1,2,3,4,5,5,6,8,7,9,2,3],R1,R2). R1 = [1,3,5,5,7,9,3], R2 = [2,4,6,8,2] ? Yes