0% found this document useful (0 votes)
6 views

LPN09

Lecture 9 focuses on the structure and comparison of terms in Prolog, introducing the identity predicate '==/2' and its differences from the unification predicate '=/' and negation predicates. It also covers the representation of lists and arithmetic terms, along with built-in predicates for examining term types and structures. Additionally, the lecture discusses operator properties, precedence, and associativity in Prolog.

Uploaded by

damfont
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

LPN09

Lecture 9 focuses on the structure and comparison of terms in Prolog, introducing the identity predicate '==/2' and its differences from the unification predicate '=/' and negation predicates. It also covers the representation of lists and arithmetic terms, along with built-in predicates for examining term types and structures. Additionally, the lecture discusses operator properties, precedence, and associativity in Prolog.

Uploaded by

damfont
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

• •

Lecture 9: A closer look at terms

• Theory
– Introduce the == predicate
– Take a closer look at term structure
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– Introduce strings in Prolog


– Introduce operators
• Exercises
– Exercises of LPN: 9.1, 9.2, 9.3, 9.4, 9.5
– Practical session

Comparing terms: ==/2

• Prolog contains an important


predicate for comparing terms
• This is the identity predicate
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

==/2
• The identity predicate ==/2
does not instantiate variables,
that is, it behaves differently
from =/2

• • 1
• •

Comparing terms: ==/2

• Prolog contains an important ?- a==a.


predicate for comparing terms yes

• This is the identity predicate


© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- a==b.
==/2 no
• The identity predicate ==/2
?- a=='a'.
does not instantiate variables,
yes
that is, it behaves differently
from =/2 ?- a==X.
X = _443
no

Comparing variables

• Two different uninstantiated


variables are not identical
terms
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• Variables instantiated with a


term T are identical to T

• • 2
• •

Comparing variables

• Two different uninstantiated ?- X==X.


variables are not identical X = _443
yes
terms
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• Variables instantiated with a ?- Y==X.


term T are identical to T Y = _442
X = _443
no

?- a=U, a==U.
U = _443
yes

Comparing terms: \==/2

• The predicate \==/2 is defined


so that it succeeds in
precisely those cases where
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

==/2 fails
• In other words, it succeeds
whenever two terms are not
identical, and fails otherwise

• • 3
• •

Comparing terms: \==/2

• The predicate \==/2 is defined ?- a \== a.


so that it succeeds in no
precisely those cases where
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- a \== b.
==/2 fails yes
• In other words, it succeeds
whenever two terms are not ?- a \== 'a'.
identical, and fails otherwise no

?- a \== X.
X = _443
yes

Terms with a special notation

• Sometimes terms look different, but


Prolog regards them as identical
• For example: a and 'a', but there are
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

many other cases


• Why does Prolog do this?
– Because it makes programming more
pleasant
– More natural way of coding Prolog
programs

• • 4
• •

Arithmetic terms

• Recall lecture 5 where we


introduced arithmetic
• +, -, <, >, etc are functors
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

and expressions such as 2+3


are actually ordinary complex
terms
• The term 2+3 is identical to
the term +(2,3)

Arithmetic terms

• Recall lecture 5 where we ?- 2+3 == +(2,3).


yes
introduced arithmetic
• +, -, <, >, etc are functors ?- -(2,3) == 2-3.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

and expressions such as 2+3 yes


are actually ordinary complex
terms ?- (4<2) == <(4,2).
yes
• The term 2+3 is identical to
the term +(2,3)

• • 5
• •

Summary of comparison predicates

= Unification predicate

\= Negation of unification predicate


© Patrick Blackburn, Johan Bos & Kristina Striegnitz

== Identity predicate

\== Negation of identity predicate

=:= Arithmetic equality predicate

=\= Negation of arithmetic equality predicate

Lists as terms

• Another example of Prolog working with one


internal representation, while showing
another to the user
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• Using the | constructor, ?- [a,b,c,d] == [a|[b,c,d]].


there are many ways yes
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

• • 6
• •

Prolog lists internally

• Internally, lists are built out of two


special terms:
– [] (which represents the empty list)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– ’.’ (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

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,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

and list is any Prolog list. If list has length n,


then .(term,list) has length n+1.

• • 7
• •

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

?- .(f(d,e),[]) == [f(d,e)].
yes

?- .(a,.(b,[])) == [a,b].
yes

?- .(a,.(b,.(f(d,e),[]))) == [a,b,f(d,e)].
yes

Internal list representation

• Works similar to the | notation:


• It represents a list in two parts
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– 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

• • 8
• •

Example of a list as tree

• Example: [a,[b,c],d]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

a .

. .

b
. d []

c []

Examining terms

• We will now look at built-in predicates


that let us examine Prolog terms more
closely
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– Predicates that determine the type of


terms
– Predicates that tell us something about the
internal structure of terms

• • 9
• •

Type of terms

Terms
Terms
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

Simple
SimpleTerms
Terms Complex
ComplexTerms
Terms

Constants
Constants Variables
Variables

Atoms
Atoms Numbers
Numbers

Checking the type of a term


atom/1 Is the argument an atom?
integer/1 … an interger?
float/1 … a floating point number?
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

number/1 … an integer or float?


atomic/1 … a constant?
var/1 … an uninstantiated variable?
nonvar/1 … an instantiated variable or
another term that is not an
uninstantiated variable

• • 10
• •

Type checking: atom/1

?- atom(a).
yes
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- atom(7).
no

?- atom(X).
no

Type checking: atom/1

?- X=a, atom(X).
X=a
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

yes

?- atom(X), X=a.
no

• • 11
• •

Type checking: atomic/1

?- atomic(mia).
yes
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- atomic(5).
yes

?- atomic(loves(vincent,mia)).
no

Type checking: var/1

?- var(mia).
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- var(X).
yes

?- X=5, var(X).
no

• • 12
• •

Type checking: nonvar/1

?- nonvar(X).
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- nonvar(mia).
yes

?- nonvar(23).
yes

The structure of terms

• Given a complex term of unknown


structure, what kind of information
might we want to extract from it?
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• Obviously:
– The functor
– The arity
– The argument
• Prolog provides built-in predicates to
produce this information

• • 13
• •

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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

F = friends
A=2
yes

• • 14
• •

The functor/3 predicate

• The functor/3 predicate gives the


functor and arity of a complex predicate
?- functor(friends(lou,andy),F,A).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

F = friends
A=2
yes

?- functor([lou,andy,vicky],F,A).
F=.
A=2
yes

functor/3 and constants

• What happens when we use functor/3


with constants?
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• • 15
• •

functor/3 and constants

• What happens when we use functor/3


with constants?
?- functor(mia,F,A).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

F = mia
A=0
yes

functor/3 and constants

• What happens when we use functor/3


with constants?
?- functor(mia,F,A).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

F = mia
A=0
yes
?- functor(14,F,A).
F = 14
A=0
yes

• • 16
• •

functor/3 for constructing terms

• You can also use functor/3 to construct


terms:
– ?- functor(Term,friends,2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

Term = friends(_,_)
yes

Checking for complex terms

complexTerm(X):-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

nonvar(X),
functor(X,_,A),
A > 0.

• • 17
• •

Arguments: arg/3

• Prolog also provides us with the


predicate arg/3
• This predicate tells us about the
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

arguments of complex terms


• It takes three arguments:
– A number N
– A complex term T
– The Nth argument of T

Arguments: arg/3

• Prolog also provides us with the


predicate arg/3
• This predicate tells us about the
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

arguments of complex terms


• It takes three arguments:
– A number N
?- arg(2,likes(lou,andy),A).
– A complex term T A = andy
– The Nth argument of T yes

• • 18
• •

Strings

• Strings are represented in Prolog by a


list of character codes
• Prolog offers double quotes for an easy
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

notation for strings


?- S = “Vicky“.
S = [86,105,99,107,121]
yes

Working with strings

• There are several standard predicates


for working with strings
• A particular useful one is atom_codes/2
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- atom_codes(vicky,S).
S = [118,105,99,107,121]
yes

• • 19
• •

Operators

• As we have seen, in certain cases,


Prolog allows us to use operator
notations that are more user friendly
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• 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

Properties of operators

• Infix operators
– Functors written between their arguments
– Examples: + - = == , ; . -->
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• 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

• • 20
• •

Precedence

• Every operator has a certain


precedence to work out ambiguous
expressions
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• 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

Associativity

• Prolog uses associativity to disambiguate


operators with the same precedence value
• Example: 2+3+4
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

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: :- -->

• • 21
• •

Defining operators

• Prolog lets you define your own


operators
• Operator definitions look like this:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

:- op(Precedence, Type, Name).

– Precedence:
number between 0 and 1200
– Type: the type of operator

Types of operators in Prolog

• yfx left-associative, infix


• xfy right-associative, infix
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• xfx non-associative, infix


• fx non-associative, prefix
• fy right-associative, prefix
• xf non-associative, postfix
• yf left-associative, postfix

• • 22
• •

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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

predicate
– Explain how the cut can be packaged into
a more structured form, namely negation
as failure

• • 23

You might also like