© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
© 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.
More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
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.
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.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
Prolog.
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.
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).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
4. PROLOG Data Objects And PROLOG Arithmetic
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© 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.
© 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 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.
© 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.
Formal Models of Computation Part II The Logic Model
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
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.
Chapter 9: Functional Programming in a Typed Language.
Declarative Programming Arithmetic in PROLOG Autumn 2014.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
© 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.
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Artificial Intelligence
Cs774 (Prasad)L4ListProcessing1 List processing in Prolog
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
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.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Section 16.5, 16.6 plus other references
Types CSCE 314 Spring 2016.
Tests, Backtracking, and Recursion
Artificial Intelligence CS370D
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Lecture 7: Definite Clause Grammars
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure –Introduce strings in Prolog –Introduce operators Exercises –Exercises of LPN: 9.1, 9.2, 9.3, 9.4, 9.5 –Practical session

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing terms: ==/2 Prolog contains an important predicate for comparing terms This is the identity predicate ==/2 The identity predicate ==/2 does not instantiate variables, that is, it behaves differently from =/2

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing terms: ==/2 Prolog contains an important predicate for comparing terms This is the identity predicate ==/2 The identity predicate ==/2 does not instantiate variables, that is, it behaves differently from =/2 ?- a==a. yes ?- a==b. no ?- a=='a'. yes ?- a==X. X = _443 no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing variables Two different uninstantiated variables are not identical terms Variables instantiated with a term T are identical to T

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing variables Two different uninstantiated variables are not identical terms Variables instantiated with a term T are identical to T ?- X==X. X = _443 yes ?- Y==X. Y = _442 X = _443 no ?- a=U, a==U. U = _443 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing terms: \==/2 The predicate \==/2 is defined so that it succeeds in precisely those cases where ==/2 fails In other words, it succeeds whenever two terms are not identical, and fails otherwise

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing terms: \==/2 The predicate \==/2 is defined so that it succeeds in precisely those cases where ==/2 fails In other words, it succeeds whenever two terms are not identical, and fails otherwise ?- a \== a. no ?- a \== b. yes ?- a \== 'a'. no ?- a \== X. X = _443 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Terms with a special notation Sometimes terms look different, but Prolog regards them as identical For example: a and 'a', but there are many other cases Why does Prolog do this? –Because it makes programming more pleasant –More natural way of coding Prolog programs

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arithmetic terms Recall lecture 5 where we introduced arithmetic +, -,, etc are functors and expressions such as 2+3 are actually ordinary complex terms The term 2+3 is identical to the term +(2,3)

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arithmetic terms Recall lecture 5 where we introduced arithmetic +, -,, etc are functors and expressions such as 2+3 are actually ordinary complex terms The term 2+3 is identical to the term +(2,3) ?- 2+3 == +(2,3). yes ?- -(2,3) == 2-3. yes ?- (4<2) == <(4,2). yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Summary of comparison predicates Negation of arithmetic equality predicate=\= Arithmetic equality predicate=:= Negation of identity predicate\== Identity predicate== Negation of unification predicate\= Unification predicate=

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lists as terms Another example of Prolog working with one internal representation, while showing another to the user Using the | constructor, there are many ways of writing the same list ?- [a,b,c,d] == [a|[b,c,d]]. yes ?- [a,b,c,d] == [a,b,c|[d]]. yes ?- [a,b,c,d] == [a,b,c,d|[]]. yes ?- [a,b,c,d] == [a,b|[c,d]]. yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog lists internally Internally, lists are built out of two special terms: –[] (which represents the empty list) –’.’ (a functor of arity 2 used to build non-empty lists) These two terms are also called list constructors A recursive definition shows how they construct lists

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Definition of prolog list The empty list is the term []. It has length 0. A non-empty list is any term of the form.(term,list), where term is any Prolog term, and list is any Prolog list. If list has length n, then.(term,list) has length n+1.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A few examples… ?-.(a,[]) == [a]. yes ?-.(f(d,e),[]) == [f(d,e)]. yes ?-.(a,.(b,[])) == [a,b]. yes ?-.(a,.(b,.(f(d,e),[]))) == [a,b,f(d,e)]. yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Internal list representation Works similar to the | notation: It represents a list in two parts –Its first element, the head –the rest of the list, the tail The trick is to read these terms as trees –Internal nodes are labeled with. –All nodes have two daughter nodes Subtree under left daughter is the head Subtree under right daughter is the tail

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example of a list as tree Example: [a,[b,c],d] a.. b... c [] d

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Examining terms We will now look at built-in predicates that let us examine Prolog terms more closely –Predicates that determine the type of terms –Predicates that tell us something about the internal structure of terms

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Type of terms Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Checking the type of a term atom/1 integer/1 float/1 number/1 atomic/1 var/1 nonvar/1 Is the argument an atom? … an interger? … a floating point number? … an integer or float? … a constant? … an uninstantiated variable? … an instantiated variable or another term that is not an uninstantiated variable

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Type checking: atom/1 ?- atom(a). yes ?- atom(7). no ?- atom(X). no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Type checking: atom/1 ?- X=a, atom(X). X = a yes ?- atom(X), X=a. no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Type checking: atomic/1 ?- atomic(mia). yes ?- atomic(5). yes ?- atomic(loves(vincent,mia)). no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Type checking: var/1 ?- var(mia). no ?- var(X). yes ?- X=5, var(X). no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Type checking: nonvar/1 ?- nonvar(X). no ?- nonvar(mia). yes ?- nonvar(23). yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The structure of terms Given a complex term of unknown structure, what kind of information might we want to extract from it? Obviously: –The functor –The arity –The argument Prolog provides built-in predicates to produce this information

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The functor/3 predicate The functor/3 predicate gives the functor and arity of a complex predicate

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The functor/3 predicate The functor/3 predicate gives the functor and arity of a complex predicate  ?- functor(friends(lou,andy),F,A). F = friends A = 2 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The functor/3 predicate The functor/3 predicate gives the functor and arity of a complex predicate  ?- functor(friends(lou,andy),F,A). F = friends A = 2 yes  ?- functor([lou,andy,vicky],F,A). F =. A = 2 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz functor/3 and constants What happens when we use functor/3 with constants?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz functor/3 and constants What happens when we use functor/3 with constants?  ?- functor(mia,F,A). F = mia A = 0 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz functor/3 and constants What happens when we use functor/3 with constants?  ?- functor(mia,F,A). F = mia A = 0 yes  ?- functor(14,F,A). F = 14 A = 0 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz functor/3 for constructing terms You can also use functor/3 to construct terms: –?- functor(Term,friends,2). Term = friends(_,_) yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Checking for complex terms complexTerm(X):- nonvar(X), functor(X,_,A), A > 0.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arguments: arg/3 Prolog also provides us with the predicate arg/3 This predicate tells us about the arguments of complex terms It takes three arguments: –A number N –A complex term T –The Nth argument of T

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arguments: arg/3 Prolog also provides us with the predicate arg/3 This predicate tells us about the arguments of complex terms It takes three arguments: –A number N –A complex term T –The Nth argument of T ?- arg(2,likes(lou,andy),A). A = andy yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Strings Strings are represented in Prolog by a list of character codes Prolog offers double quotes for an easy notation for strings ?- S = “Vicky“. S = [86,105,99,107,121] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Working with strings There are several standard predicates for working with strings A particular useful one is atom_codes/2 ?- atom_codes(vicky,S). S = [118,105,99,107,121] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Operators As we have seen, in certain cases, Prolog allows us to use operator notations that are more user friendly Recall, for instance, the arithmetic expressions such as 2+2 which internally means +(2,2) Prolog also has a mechanism to add your own operators

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Properties of operators Infix operators –Functors written between their arguments –Examples: + - = ==, ;. --> Prefix operators –Functors written before their argument –Example: - (to represent negative numbers) Postfix operators –Functors written after their argument –Example: ++ in the C programming language

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Precedence Every operator has a certain precedence to work out ambiguous expressions For instance, does 2+3*3 mean 2+(3*3), or (2+3)*3? Because the precedence of + is greater than that of *, Prolog chooses + to be the main functor of 2+3*3

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Associativity Prolog uses associativity to disambiguate operators with the same precedence value Example: Does this mean (2+3)+4 or 2+(3+4)? –Left associative –Right associative Operators can also be defined as non- associative, in which case you are forced to use bracketing in ambiguous cases –Examples in Prolog: :- -->

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining operators Prolog lets you define your own operators Operator definitions look like this: –Precedence: number between 0 and 1200 –Type: the type of operator :- op(Precedence, Type, Name).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Types of operators in Prolog yfx left-associative, infix xfy right-associative, infix xfx non-associative, infix fx non-associative, prefix fy right-associative, prefix xf non-associative, postfix yf left-associative, postfix

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Operators in SWI Prolog

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Next lecture Cuts and negation –How to control Prolog`s backtracking behaviour with the help of the cut predicate –Explain how the cut can be packaged into a more structured form, namely negation as failure