LPN09
LPN09
• Theory
– Introduce the == predicate
– Take a closer look at term structure
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
==/2
• The identity predicate ==/2
does not instantiate variables,
that is, it behaves differently
from =/2
• • 1
• •
?- 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
• • 2
• •
Comparing variables
?- a=U, a==U.
U = _443
yes
==/2 fails
• In other words, it succeeds
whenever two terms are not
identical, and fails otherwise
• • 3
• •
?- 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
• • 4
• •
Arithmetic terms
Arithmetic terms
• • 5
• •
= Unification predicate
== Identity predicate
Lists as terms
• • 6
• •
• • 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
• • 8
• •
• Example: [a,[b,c],d]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
a .
. .
b
. d []
c []
Examining 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
• • 10
• •
?- atom(a).
yes
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- atom(7).
no
?- atom(X).
no
?- X=a, atom(X).
X=a
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
yes
?- atom(X), X=a.
no
• • 11
• •
?- atomic(mia).
yes
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- atomic(5).
yes
?- atomic(loves(vincent,mia)).
no
?- var(mia).
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- var(X).
yes
?- X=5, var(X).
no
• • 12
• •
?- nonvar(X).
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- nonvar(mia).
yes
?- nonvar(23).
yes
• Obviously:
– The functor
– The arity
– The argument
• Prolog provides built-in predicates to
produce this information
• • 13
• •
F = friends
A=2
yes
• • 14
• •
F = friends
A=2
yes
?- functor([lou,andy,vicky],F,A).
F=.
A=2
yes
• • 15
• •
F = mia
A=0
yes
F = mia
A=0
yes
?- functor(14,F,A).
F = 14
A=0
yes
• • 16
• •
Term = friends(_,_)
yes
complexTerm(X):-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
nonvar(X),
functor(X,_,A),
A > 0.
• • 17
• •
Arguments: arg/3
Arguments: arg/3
• • 18
• •
Strings
?- atom_codes(vicky,S).
S = [118,105,99,107,121]
yes
• • 19
• •
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
Associativity
• • 21
• •
Defining operators
– Precedence:
number between 0 and 1200
– Type: the type of operator
• • 22
• •
Next lecture
predicate
– Explain how the cut can be packaged into
a more structured form, namely negation
as failure
• • 23