This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

### Publishers

Scribd Selects Books

Hand-picked favorites from

our editors

our editors

Scribd Selects Audiobooks

Hand-picked favorites from

our editors

our editors

Scribd Selects Comics

Hand-picked favorites from

our editors

our editors

Scribd Selects Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

P. 1

Prolog Notes|Views: 82|Likes: 0

Published by Andreas Michael

Prolog Notes

Prolog Notes

See more

See less

https://www.scribd.com/doc/125995220/Prolog-Notes

12/13/2015

text

original

Given a complex term of which you don’t know what it looks like, what kind of infor-

mation would be interesting to get? Probably, what’s the functor, what’s the arity and

what do the arguments look like. Prolog provides built-in predicates that answer these

questions. The ﬁrst two are answered by the predicate` functor/3`. Given a complex

term` functor/3` will tell us what the functor and the arity of this term are.

`?- functor(f(a,b),F,A).
`

`A = 2
`

`F = f
`

`yes
`

`?- functor(a,F,A).
`

`A = 0
`

`F = a
`

`yes
`

`?- functor([a,b,c],X,Y).
`

`X = ’.’
`

`Y = 2
`

`yes
`

So, we can use the predicate` functor` to ﬁnd out the functor and the arity of a term,

but we can also use it to* construct* terms, by specifying the second and third argument

and leaving the ﬁrst undetermined. The query

`?- functor(T,f,8).
`

for example, returns the following answer:

`T = f(_G286, _G287, _G288, _G289, _G290, _G291, _G292, _G293)
`

`yes
`

Note, that either the ﬁrst argument or the second and third argument have to be instanti-

ated. So, Prolog would answer with an error message to the query` functor(T,f,N)`. If

you think about what the query means, Prolog is reacting in a sensible way. The query

**134
**

Chapter 9. A Closer Look at Terms

is asking Prolog to construct a complex term without telling it how many arguments to

provide and that is something Prolog can just not do.

In the previous section, we saw built-in predicates for testing whether something is

an atom, a number, a constant, or a variable. So, to make the list complete, we were

actually missing a predicate for testing whether something is a complex term. Now,

we can deﬁne such a predicate by making use of the predicate` functor`. All we have

to do is to check that the term is instantiated and that it has arguments, i.e. that its arity

is greater than zero. Here is the predicate deﬁnition.

`complexterm(X) :-
`

`nonvar(X),
`

`functor(X,_,A),
`

`A > 0.
`

In addition to the predicate` functor` there is the predicate` arg/3` which tells us about

arguments of complex terms. It takes a number* N* and a complex term* T* and returns

the* Nth* argument of* T* in its third argument. It can be used to access the value of an

argument

`?- arg(2,loves(vincent,mia),X).
`

`X = mia
`

`yes
`

or to instantiate an argument.

`?- arg(2,loves(vincent,X),mia).
`

`X = mia
`

`yes
`

Trying to access an argument which doesn’t exist, of course fails.

`?- arg(2,happy(yolanda),X).
`

`no
`

The third useful built-in predicate for analyzing term structure is` ’=..’/2`. It takes a

complex term and returns a list that contains the functor as ﬁrst element and then all

the arguments. So, when asked the query` ’=..’(loves(vincent,mia),X)` Prolog

will answer` X = [loves,vincent,mia]`. This predicate is also called univ and can

be used as an inﬁx operator. Here are a couple of examples.

`?- cause(vincent,dead(zed)) =.. X.
`

`X = [cause, vincent, dead(zed)]
`

`Yes
`

`?- X =.. [a,b(c),d].
`

`X = a(b(c), d)
`

`Yes
`

`?- footmassage(Y,mia) =.. X.
`

`Y = _G303
`

`X = [footmassage, _G303, mia]
`

`Yes
`

9.3. Examining Terms

**135
**

Univ (`’=..’`) is always useful when something has to be done to all arguments

of a complex term. Since it returns the arguments as a list, normal list processing

strategies can be used to traverse the arguments. As an example, let’s deﬁne a pred-

icate called` copy_term` which makes a copy of a term replacing variables that occur

in the original term by new variables in the copy. The copy of` dead(zed)` should

be` dead(zed)`, for instance. And the copy of` jeallou(marcellus,X)` should be

`jeallous(marcellus,_G235)`; i.e. the variable` X` in the original term has been re-

places by some new variable.

So, the predicate` copy_term` has two arguments. It takes any Prolog term in the ﬁrst

argument and returns a copy of this Prolog term in the second argument. In case the

input argument is an atom or a number, the copying is simple: the same term should

be returned.

`copy_term(X,X) :- atomic(X).
`

In case the input term is a variable, the copy should be a new variable.

`copy_term(X,_) :- var(X).
`

With these two clauses we have deﬁned how to copy simple terms. What about com-

plex terms? Well,` copy_term` should return a complex term with the same functor and

arity and all arguments of this new complex term should be copies of the correspond-

ing arguments in the input term. That means, we have to look at all arguments of the

input term and copy them with recursive calls to` copy_term`. Here is the Prolog code

for this third clause:

`copy_term(X,Y) :-
`

`nonvar(X),
`

`functor(X,F,A),
`

`A > 0,
`

`functor(Y,F,A),
`

`X =.. [F|ArgsX],
`

`Y =.. [F|ArgsY],
`

`copy_terms_in_list(ArgsX,ArgsY).
`

`copy_terms_in_list([],[]).
`

`copy_terms_in_list([HIn|TIn],[HOut|TOut]) :-
`

`copy_term(HIn,Hout),
`

`copy_terms_in_list(TIn,TOut).
`

So, we ﬁrst check whether the input term is a complex term: it is not a variable and

its arity is greater than 0. We then request that the copy should have the same functor

and arity. Finally, we have to copy all arguments of the input term. To do so, we use

univ to collect the arguments into a list and then use a simple list processing predicate

`copy_terms_in_list` to one by one copy the elements of this list.

Here is the whole code for` copy_term`:

**136
**

Chapter 9. A Closer Look at Terms

`copy_term(X,_) :- var(X).
`

`copy_term(X,X) :- atomic(X).
`

`copy_term(X,Y) :-
`

`nonvar(X),
`

`functor(X,F,A),
`

`functor(Y,F,A),
`

`A > 0,
`

`X =.. [F|ArgsX],
`

`Y =.. [F|ArgsY],
`

`copy_terms_in_list(ArgsX,ArgsY).
`

`copy_terms_in_list([],[]).
`

`copy_terms_in_list([HIn|TIn],[HOut|TOut]) :-
`

`copy_term(HIn,Hout),
`

`copy_terms_in_list(TIn,TOut).
`

Article CRM

Article BPR Methods

TCO - Cloud Computing

How E-Services Satisfy Customer Needs

How E-Services Satisfy Customer Needs

Value Co-creation

Introduction to KM

Value Co-creation (1)

Value Co-creation (1)

Talent 3.0

BPR[1]

McKinsey Quarterly

Mode 2

Mode 1

Problem_A

odigos thermomonosis

Energy Efficiency Policies and Measures in Cyprus

Worked Examples of u Value

AI Foundation of Intelligent Agent2

6 Trees

Άσκηση 9

AIBook.01

Mathimatika b Gymnasiou b Tefchos[1]

Mathimatika a Gymnasiou a Tefchos

- Read and print without ads
- Download to keep your version
- Edit, email or read offline

Prolog

Prolog

Prolog

Lisp Book

Tutorial Prolog

Basic Lisp Techniques - David J. Cooper Jr

Lisp

Auto Lisp

prolog

Emacs Lisp

Prolog

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CANCEL

OK

You've been reading!

NO, THANKS

OK

scribd

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->