© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
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.
© 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.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
© 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.
© 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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
© 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 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
© 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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© 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.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
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.
Chapter 9: Functional Programming in a Typed Language.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
© 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.
Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
CHP-3 STACKS.
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
1 Artificial Intelligence CS370D Prolog programming List operations.
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.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Stacks Chapter 4.
Chapter 11 :: Logic Languages
Lists Prolog Pepper Credit to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, (ISBN )
Prolog Programming (Volume 2)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
PROGRAMMING IN HASKELL
Declarative Programming Techniques Accumulators (CTM 3. 4
Programming Techniques
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
CSCE 314: Programming Languages Dr. Dylan Shell
Pairing Functions and Gödel Numbers
Lecture 7: Definite Clause Grammars
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply these to simple list processing problems, using accumulators –Look at tail-recursive predicates and explain why they are more efficient than predicates that are not tail-recursive Exercises –Correction exercises LPN chapter 4 –Exercises LPN chapter 5

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arithmetic in Prolog Prolog provides a number of basic arithmetic tools Integer and real numbers = 5 3 x 4 = 12 5 – 3 = 2 3 – 5 = -2 4  2 = 2 1 is the remainder when 7 is divided by 2 ?- 5 is 2+3. ?- 12 is 3  4. ?- 2 is 5-3. ?- -2 is 3-5. ?- 2 is 4/2. ?- 1 is mod(7,2). ArithmeticProlog

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example queries ?- 10 is 5+5. yes ?- 4 is 2+3. no ?- X is 3  4. X=12 yes ?- R is mod(7,2). R=1 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is (X+3)  2.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is (X+3)  2. ?- addThreeAndDouble(1,X). X=8 yes ?- addThreeAndDouble(2,X). X=10 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look It is important to know that +, -, / and  do not carry out any arithmetic Expressions such as 3+2, 4-7, 5/5 are ordinary Prolog terms –Functor: +, -, /,  –Arity: 2 –Arguments: integers

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look ?- X =

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look ?- X = X = 3+2 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look ?- X = X = 3+2 yes ? = X.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look ?- X = X = 3+2 yes ? = X. X = 3+2 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate To force Prolog to actually evaluate arithmetic expressions, we have to use is just as we did in the other examples This is an instruction for Prolog to carry out calculations Because this is not an ordinary Prolog predicate, there are some restrictions

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is X = 5 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is X = 5 yes ? is X.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is X = 5 yes ? is X. ERROR: is/2: Arguments are not sufficiently instantiated ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is X = 5 yes ? is X. ERROR: is/2: Arguments are not sufficiently instantiated ?- Result is

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is X = 5 yes ? is X. ERROR: is/2: Arguments are not sufficiently instantiated ?- Result is Result = 10 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Restrictions on use of is/2 We are free to use variables on the right hand side of the is predicate But when Prolog actually carries out the evaluation, the variables must be instantiated with a variable-free Prolog term This Prolog term must be an arithmetic expression

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Notation Two final remarks on arithmetic expressions –3+2, 4/2, 4-5 are just ordinary Prolog terms in a user-friendly notation: 3+2 is really +(3,2) and so on. –Also the is predicate is a two-place Prolog predicate

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Notation Two final remarks on arithmetic expressions –3+2, 4/2, 4-5 are just ordinary Prolog terms in a user-friendly notation: 3+2 is really +(3,2) and so on. –Also the is predicate is a two-place Prolog predicate ?- is(X,+(3,2)). X = 5 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arithmetic and Lists How long is a list? –The empty list has length: zero; –A non-empty list has length: one plus length of its tail.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog len([],0).% not a Prolog native predicate! len([_|L],N):- len(L,X), N is X + 1. ?- len([a,b,c,d,e,[a,x],t],X). X=7 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Accumulators This is quite a good program –Easy to understand –Relatively efficient But there is another method of finding the length of a list –Introduce the idea of accumulators –Accumulators are variables that hold intermediate results

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining acclen/3 The predicate acclen/3 has three arguments –The list whose length we want to find –The length of the list, an integer –An accumulator, keeping track of the intermediate values for the length

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining acclen/3 The accumulator of acclen/3 –Initial value of the accumulator is 0 –Add 1 to accumulator each time we can recursively take the head of a list –When we reach the empty list, the accumulator contains the length of the list

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog acclen([],Acc,Length):- Length = Acc. acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog acclen([],Acc,Length):- Length = Acc. acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). ?- add 1 to the accumulator each time we take off a head from the list

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog acclen([],Acc,Length):- Length = Acc. acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). ?- When we reach the empty list, the accumulator contains the length of the list

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog acclen([],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog acclen([],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). ?-acclen([a,b,c],0,Len). Len=3 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). / \ acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). / \ no ?- acclen([b,c],1,Len). / \ acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). / \ no ?- acclen([b,c],1,Len). / \ no ?- acclen([c],2,Len). / \ acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). / \ no ?- acclen([b,c],1,Len). / \ no ?- acclen([c],2,Len). / \ no ?- acclen([],3,Len). / \ acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). / \ no ?- acclen([b,c],1,Len). / \ no ?- acclen([c],2,Len). / \ no ?- acclen([],3,Len). / \ Len=3 no acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Adding a wrapper predicate acclen([ ],Acc,Acc). acclen([ _|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). length(List,Length):- % native Prolog predicate acclen(List,0,Length). ?-length([a,b,c], X). X=3 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Tail recursion Why is acclen/3 better than len/2 ? –acclen/3 is tail-recursive, and len/2 is not Difference: –In tail recursive predicates the results is fully calculated once we reach the base clause –In recursive predicates that are not tail recursive, there are still goals on the stack when we reach the base clause

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparison acclen([],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length). len([],0). len([_|L],NewLength):- len(L,Length), NewLength is Length + 1. Not tail-recursive Tail-recursive

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for len/2 ?- len([a,b,c], Len). len([],0). len([_|L],NewLength):- len(L,Length), NewLength is Length + 1.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for len/2 ?- len([a,b,c], Len). / \ no ?- len([b,c],Len1), Len is Len len([],0). len([_|L],NewLength):- len(L,Length), NewLength is Length + 1.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for len/2 ?- len([a,b,c], Len). / \ no ?- len([b,c],Len1), Len is Len / \ no ?- len([c], Len2), Len1 is Len2+1, Len is Len1+1. len([],0). len([_|L],NewLength):- len(L,Length), NewLength is Length + 1.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for len/2 ?- len([a,b,c], Len). / \ no ?- len([b,c],Len1), Len is Len / \ no ?- len([c], Len2), Len1 is Len2+1, Len is Len1+1. / \ no ?- len([], Len3), Len2 is Len3+1, Len1 is Len2+1, Len is Len len([],0). len([_|L],NewLength):- len(L,Length), NewLength is Length + 1.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for len/2 ?- len([a,b,c], Len). / \ no ?- len([b,c],Len1), Len is Len / \ no ?- len([c], Len2), Len1 is Len2+1, Len is Len1+1. / \ no ?- len([], Len3), Len2 is Len3+1, Len1 is Len2+1, Len is Len / \ Len3=0, Len2=1, no Len1=2, Len=3 len([],0). len([_|L],NewLength):- len(L,Length), NewLength is Length + 1.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree for acclen/3 ?- acclen([a,b,c],0,Len). / \ no ?- acclen([b,c],1,Len). / \ no ?- acclen([c],2,Len). / \ no ?- acclen([],3,Len). / \ Len=3 no acclen([ ],Acc,Acc). acclen([_|L],OldAcc,Length):- NewAcc is OldAcc + 1, acclen(L,NewAcc,Length).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing Integers Some Prolog arithmetic predicates actually do carry out arithmetic by themselves These are the operators that compare integers

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing Numbers x < y x  y x = y x  y x  y x > y X < Y X =< Y X =:= Y X =\= Y X >= Y X > Y ArithmeticProlog

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparison Operators Have the obvious meaning Force both left and right hand arguments to be evaluated ?- 2 < 4+1. yes ?- 4+3 > 5+5. no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparison Operators Have the obvious meaning Force both left and right hand arguments to be evaluated ?- 4 = 4. yes ?- 2+2 = 4. no ?- 2+2 =:= 4. yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing Numbers We are going to define a predicate that takes two arguments, and is true when: –The first argument is a list of integers –The second argument is the highest integer in the list Basic idea –We will use an accumulator –The accumulator keeps track of the highest value encountered so far –If we find a higher value, the accumulator will be updated

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Definition of accMax/3 accMax([H|T],A,Max):- H > A, accMax(T,H,Max). accMax([H|T],A,Max):- H =< A, accMax(T,A,Max). accMax([],A,A). ?- accMax([1,0,5,4],0,Max). Max=5 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Adding a wrapper max/2 accMax([H|T],A,Max):- H > A, accMax(T,H,Max). accMax([H|T],A,Max):- H =< A, accMax(T,A,Max). accMax([],A,A). max([H|T],Max):- accMax(T,H,Max). ?- max([1,0,5,4], Max). Max=5 yes ?- max([-3, -1, -5, -4], Max). Max= -1 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Summary of this lecture In this lecture we showed how Prolog does arithmetic We demonstrated the difference between tail-recursive predicates and predicates that are not tail-recursive We introduced the programming technique of using accumulators We also introduced the idea of using wrapper predicates

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution Exercise 4.1 How does Prolog respond to the following queries? 1.[a,b,c,d] = [a,[b,c,d]]. ✗ 2.[a,b,c,d] = [a|[b,c,d]]. ✔ 3.[a,b,c,d] = [a,b,[c,d]]. ✗ 4.[a,b,c,d] = [a,b|[c,d]]. ✔ 5.[a,b,c,d] = [a,b,c,[d]]. ✗ 6.[a,b,c,d] = [a,b,c|[d]]. ✔ 7.[a,b,c,d] = [a,b,c,d,[]]. ✗ 8.[a,b,c,d] = [a,b,c,d|[]]. ✔ 9.[] = _. ✔ 10.[] = [_]. ✗ 11.[] = [_|[]]. ✗

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution Exercise 4.3 twice([], []). twice([H|T], Out) :- Out = [H, H | TailOut], twice(T, TailOut). ?- twice([a,4,buggle],X). X = [a,a,4,4,buggle,buggle]). ?- twice([1,2,1,1],X). X = [1,1,2,2,1,1,1,1]. Write a predicate twice(In,Out) whose left argument is a list, and whose right argument is a list consisting of every element in the left list written twice.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution Exercise 4.4 member(x,[a,b,c]). † X = a member(X,[X|_]). member(X,[_|T]):- member(X,T). member(x,[b,c]). member(x,[c]). member(x,[]). member(a,[c,b,a,y]). member(a,[b,a,y]). member(a,[a,y]). member(X,[a,b,c]). member(a,[a|[b,c]])

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise LPN Chapter 5 5.1, 5.2, 5.3

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Next lecture Yes, more lists! –Defining the append/3, a predicate that concatenates two lists –Discuss the idea of reversing a list, first naively using append/3, then with a more efficient way using accumulators