X:Y XandYmatch X::Y XandYareidentical sult goal, for example: X - - PowerPoint PPT Presentation

x y
SMART_READER_LITE
LIVE PREVIEW

X:Y XandYmatch X::Y XandYareidentical sult goal, for example: X - - PowerPoint PPT Presentation

2.1 Data objects 33 data objects ,/\ simple objects structures /\ of Prolog constants variables /\ atoms numbers Figure 2.1 Data objects in Prolog. Atoms and numbers 2.1.1 44 In chapter 1 we have seen some examples of atoms and variables.


slide-1
SLIDE 1

2.1 Data objects 33

simple objects structures

,/\

data objects

variables

/\

  • f Prolog

constants

/\

2.1.1

atoms

numbers

Figure 2.1 Data objects in Prolog.

Atoms and numbers In chapter 1 we have seen some examples of atoms and variables. In general,

however, atoms can take more complicated forms - that is, strings of the follow-

ing characters:

. upper-case letters A,B, . . . ,2 . lower-case letters a, b, . . . , z . digitsO,'J.,2,...,9 . special characters such as * -* I < >: : . &

Atoms can be constructed iri three ways:

(1) Strings of ietters, digits and the underscore character, ,_,, starting with a lower-

case letter:

anna

nil

x25

x_25

x_2548

.x_

xv

alpha_beta procedure missJones

(2) Strings of special characters: +

When using atoms of this form, some care is necessary because some strings

  • f special characters already have a predefined meaning. An example is ,:-,.

44

syntax and semantics of basic con-

  • ects. The topics included are:

ts

am

meanings of a program

lauses and goals.

r Chapter 1. Here the treatment will

in Prolog. The Prolog system

lm by its syntactic form. This is

different forms for each type of

br distinguishing between atoms

upper-case letters whereas atoms

rmation (such as data-type decla-

¡rder to recognize the t¡re of an

slide-2
SLIDE 2

2.1 Data objects 33

simple objects structures

,/\

data objects

variables

/\

  • f Prolog

constants

/\

2.1.1

atoms

numbers

Figure 2.1 Data objects in Prolog.

Atoms and numbers In chapter 1 we have seen some examples of atoms and variables. In general,

however, atoms can take more complicated forms - that is, strings of the follow-

ing characters:

. upper-case letters A,B, . . . ,2 . lower-case letters a, b, . . . , z . digitsO,'J.,2,...,9 . special characters such as * -* I < >: : . &

Atoms can be constructed iri three ways:

(1) Strings of ietters, digits and the underscore character, ,_,, starting with a lower-

case letter:

anna

nil

x25

x_25

x_2548

.x_

xv

alpha_beta procedure missJones

(2) Strings of special characters: +

When using atoms of this form, some care is necessary because some strings

  • f special characters already have a predefined meaning. An example is ,:-,.

44

syntax and semantics of basic con-

  • ects. The topics included are:

ts

am

meanings of a program

lauses and goals.

r Chapter 1. Here the treatment will

in Prolog. The Prolog system

lm by its syntactic form. This is

different forms for each type of

br distinguishing between atoms

upper-case letters whereas atoms

rmation (such as data-type decla-

¡rder to recognize the t¡re of an

Terms Atoms, numbers, variables and structures are all terms.

slide-3
SLIDE 3

: in the current input file that

Lits original form, represented as

getsentence of this section can be

cg system by means of built-in

  • ams. The details of 'consulting,

:ion of Prolog. Here we look at

clogs.

ile F with a goal of the forr¡

program3 will typically have an rgram file. The effect of this goal and loaded into the memory, so

rrther questions from the user.

time during the same session.

this new flle are added into the lementation and other circum-

Lt a procedure defined in the

ry be simply added at the end of

ition of this procedure may be

sult goal, for example:

. If a Prolog implementation also

n a compiled form. This enables

r factor of 5 or 10 between the

ed into memory in the compiled

e:

d, but interpreted programs are

rd traced by Prolog's debugging

IS:

Summary 175

  • facilities. Therefore an interpreter is typically used in the program development

phase, and a compiler is used with the final program.

It should be noted, again, that the details of consulting and compiling files

depend on the implementation of Prolog. usually a prolog implementation also allows the user to enter and edit the program interactively.

5ummary

A Prolog implementation normally provides a set of built-in procedures to

accomplish several useful operations that are not possible in pure prolog. In this chapter, such a set of predicates available in many Prolog implementations was

introduced.

The type of a term can be tested by the following predicates:

var( X)

nonvar( X) atom( X)

integer( X)

float( X) atomic( X) compound( X)

X is a (non-instantiated) variable X is not a variable X is an atom X is an integer X is a real number X is either an atom or a number X is a structure

Terms can be constructed or decomposed:

Term :.. I Functor I Argumentlist]

functor( Term, Functor, Arity)

arg( N, Term, Argument)

name( Atom, CharacterCodes)

Terms can be compared:

X:Y

XandYmatch

X::Y

XandYareidentical

X \:: Y X and Y are not identical

X::: Y

X and Y are arithmetically equal

X:\: Y

X and Y are not arithmetically equal

X < Y

adthmetic value of X is less than Y (related: :<, ), ):)

X @< Y

term X precedes term Y (related: @:<, @>, @>:)

A Prolog program can be viewed as a relational database that can be updated by

the following procedures:

assert( Clause) add Clause to the program asserta( Clause) add at the beginning assertz( Clause) add at the end

retract( Clause) remove a clause that matches Clause

All the obiects that satisfy a given condition can be collected into a list by the

predicates:

bagof( X, P, L) L is the list of all X that satisfy condition P setof( X, P, L) L is the sorted list of all X that satisfy condition P

findall( X, P, L) similar to bagof

slide-4
SLIDE 4

: in the current input file that

Lits original form, represented as

getsentence of this section can be

cg system by means of built-in

  • ams. The details of 'consulting,

:ion of Prolog. Here we look at

clogs.

ile F with a goal of the forr¡

program3 will typically have an rgram file. The effect of this goal and loaded into the memory, so

rrther questions from the user.

time during the same session.

this new flle are added into the lementation and other circum-

Lt a procedure defined in the

ry be simply added at the end of

ition of this procedure may be

sult goal, for example:

. If a Prolog implementation also

n a compiled form. This enables

r factor of 5 or 10 between the

ed into memory in the compiled

e:

d, but interpreted programs are

rd traced by Prolog's debugging

IS:

Summary 175

  • facilities. Therefore an interpreter is typically used in the program development

phase, and a compiler is used with the final program.

It should be noted, again, that the details of consulting and compiling files

depend on the implementation of Prolog. usually a prolog implementation also allows the user to enter and edit the program interactively.

5ummary

A Prolog implementation normally provides a set of built-in procedures to

accomplish several useful operations that are not possible in pure prolog. In this chapter, such a set of predicates available in many Prolog implementations was

introduced.

The type of a term can be tested by the following predicates:

var( X)

nonvar( X) atom( X)

integer( X)

float( X) atomic( X) compound( X)

X is a (non-instantiated) variable X is not a variable X is an atom X is an integer X is a real number X is either an atom or a number X is a structure

Terms can be constructed or decomposed:

Term :.. I Functor I Argumentlist]

functor( Term, Functor, Arity)

arg( N, Term, Argument)

name( Atom, CharacterCodes)

Terms can be compared:

X:Y

XandYmatch

X::Y

XandYareidentical

X \:: Y X and Y are not identical

X::: Y

X and Y are arithmetically equal

X:\: Y

X and Y are not arithmetically equal

X < Y

adthmetic value of X is less than Y (related: :<, ), ):)

X @< Y

term X precedes term Y (related: @:<, @>, @>:)

A Prolog program can be viewed as a relational database that can be updated by

the following procedures:

assert( Clause) add Clause to the program asserta( Clause) add at the beginning assertz( Clause) add at the end

retract( Clause) remove a clause that matches Clause

All the obiects that satisfy a given condition can be collected into a list by the

predicates:

bagof( X, P, L) L is the list of all X that satisfy condition P setof( X, P, L) L is the sorted list of all X that satisfy condition P

findall( X, P, L) similar to bagof

slide-5
SLIDE 5

: in the current input file that

Lits original form, represented as

getsentence of this section can be

cg system by means of built-in

  • ams. The details of 'consulting,

:ion of Prolog. Here we look at

clogs.

ile F with a goal of the forr¡

program3 will typically have an rgram file. The effect of this goal and loaded into the memory, so

rrther questions from the user.

time during the same session.

this new flle are added into the lementation and other circum-

Lt a procedure defined in the

ry be simply added at the end of

ition of this procedure may be

sult goal, for example:

. If a Prolog implementation also

n a compiled form. This enables

r factor of 5 or 10 between the

ed into memory in the compiled

e:

d, but interpreted programs are

rd traced by Prolog's debugging

IS:

Summary 175

  • facilities. Therefore an interpreter is typically used in the program development

phase, and a compiler is used with the final program.

It should be noted, again, that the details of consulting and compiling files

depend on the implementation of Prolog. usually a prolog implementation also allows the user to enter and edit the program interactively.

5ummary

A Prolog implementation normally provides a set of built-in procedures to

accomplish several useful operations that are not possible in pure prolog. In this chapter, such a set of predicates available in many Prolog implementations was

introduced.

The type of a term can be tested by the following predicates:

var( X)

nonvar( X) atom( X)

integer( X)

float( X) atomic( X) compound( X)

X is a (non-instantiated) variable X is not a variable X is an atom X is an integer X is a real number X is either an atom or a number X is a structure

Terms can be constructed or decomposed:

Term :.. I Functor I Argumentlist]

functor( Term, Functor, Arity)

arg( N, Term, Argument)

name( Atom, CharacterCodes)

Terms can be compared:

X:Y

XandYmatch

X::Y

XandYareidentical

X \:: Y X and Y are not identical

X::: Y

X and Y are arithmetically equal

X:\: Y

X and Y are not arithmetically equal

X < Y

adthmetic value of X is less than Y (related: :<, ), ):)

X @< Y

term X precedes term Y (related: @:<, @>, @>:)

A Prolog program can be viewed as a relational database that can be updated by

the following procedures:

assert( Clause) add Clause to the program asserta( Clause) add at the beginning assertz( Clause) add at the end

retract( Clause) remove a clause that matches Clause

All the obiects that satisfy a given condition can be collected into a list by the

predicates:

bagof( X, P, L) L is the list of all X that satisfy condition P setof( X, P, L) L is the sorted list of all X that satisfy condition P

findall( X, P, L) similar to bagof

slide-6
SLIDE 6

: in the current input file that

Lits original form, represented as

getsentence of this section can be

cg system by means of built-in

  • ams. The details of 'consulting,

:ion of Prolog. Here we look at

clogs.

ile F with a goal of the forr¡

program3 will typically have an rgram file. The effect of this goal and loaded into the memory, so

rrther questions from the user.

time during the same session.

this new flle are added into the lementation and other circum-

Lt a procedure defined in the

ry be simply added at the end of

ition of this procedure may be

sult goal, for example:

. If a Prolog implementation also

n a compiled form. This enables

r factor of 5 or 10 between the

ed into memory in the compiled

e:

d, but interpreted programs are

rd traced by Prolog's debugging

IS:

Summary 175

  • facilities. Therefore an interpreter is typically used in the program development

phase, and a compiler is used with the final program.

It should be noted, again, that the details of consulting and compiling files

depend on the implementation of Prolog. usually a prolog implementation also allows the user to enter and edit the program interactively.

5ummary

A Prolog implementation normally provides a set of built-in procedures to

accomplish several useful operations that are not possible in pure prolog. In this chapter, such a set of predicates available in many Prolog implementations was

introduced.

The type of a term can be tested by the following predicates:

var( X)

nonvar( X) atom( X)

integer( X)

float( X) atomic( X) compound( X)

X is a (non-instantiated) variable X is not a variable X is an atom X is an integer X is a real number X is either an atom or a number X is a structure

Terms can be constructed or decomposed:

Term :.. I Functor I Argumentlist]

functor( Term, Functor, Arity)

arg( N, Term, Argument)

name( Atom, CharacterCodes)

Terms can be compared:

X:Y

XandYmatch

X::Y

XandYareidentical

X \:: Y X and Y are not identical

X::: Y

X and Y are arithmetically equal

X:\: Y

X and Y are not arithmetically equal

X < Y

adthmetic value of X is less than Y (related: :<, ), ):)

X @< Y

term X precedes term Y (related: @:<, @>, @>:)

A Prolog program can be viewed as a relational database that can be updated by

the following procedures:

assert( Clause) add Clause to the program asserta( Clause) add at the beginning assertz( Clause) add at the end

retract( Clause) remove a clause that matches Clause

All the obiects that satisfy a given condition can be collected into a list by the

predicates:

bagof( X, P, L) L is the list of all X that satisfy condition P setof( X, P, L) L is the sorted list of all X that satisfy condition P

findall( X, P, L) similar to bagof

If you want to have a clause in your database and you want to be able to add or remove it dynamically (that is, when you query), you must declare it to be dynamic in gprolog with the :- dynamic designation. The compiler needs this designation when it consults your database. You can add or remove any clause not already in your database without the :- designation. gprolog does not have assert/1.

slide-7
SLIDE 7

: in the current input file that

Lits original form, represented as

getsentence of this section can be

cg system by means of built-in

  • ams. The details of 'consulting,

:ion of Prolog. Here we look at

clogs.

ile F with a goal of the forr¡

program3 will typically have an rgram file. The effect of this goal and loaded into the memory, so

rrther questions from the user.

time during the same session.

this new flle are added into the lementation and other circum-

Lt a procedure defined in the

ry be simply added at the end of

ition of this procedure may be

sult goal, for example:

. If a Prolog implementation also

n a compiled form. This enables

r factor of 5 or 10 between the

ed into memory in the compiled

e:

d, but interpreted programs are

rd traced by Prolog's debugging

IS:

Summary 175

  • facilities. Therefore an interpreter is typically used in the program development

phase, and a compiler is used with the final program.

It should be noted, again, that the details of consulting and compiling files

depend on the implementation of Prolog. usually a prolog implementation also allows the user to enter and edit the program interactively.

5ummary

A Prolog implementation normally provides a set of built-in procedures to

accomplish several useful operations that are not possible in pure prolog. In this chapter, such a set of predicates available in many Prolog implementations was

introduced.

The type of a term can be tested by the following predicates:

var( X)

nonvar( X) atom( X)

integer( X)

float( X) atomic( X) compound( X)

X is a (non-instantiated) variable X is not a variable X is an atom X is an integer X is a real number X is either an atom or a number X is a structure

Terms can be constructed or decomposed:

Term :.. I Functor I Argumentlist]

functor( Term, Functor, Arity)

arg( N, Term, Argument)

name( Atom, CharacterCodes)

Terms can be compared:

X:Y

XandYmatch

X::Y

XandYareidentical

X \:: Y X and Y are not identical

X::: Y

X and Y are arithmetically equal

X:\: Y

X and Y are not arithmetically equal

X < Y

adthmetic value of X is less than Y (related: :<, ), ):)

X @< Y

term X precedes term Y (related: @:<, @>, @>:)

A Prolog program can be viewed as a relational database that can be updated by

the following procedures:

assert( Clause) add Clause to the program asserta( Clause) add at the beginning assertz( Clause) add at the end

retract( Clause) remove a clause that matches Clause

All the obiects that satisfy a given condition can be collected into a list by the

predicates:

bagof( X, P, L) L is the list of all X that satisfy condition P setof( X, P, L) L is the sorted list of all X that satisfy condition P

findall( X, P, L) similar to bagof

Recall the mathematical definition of a bag compared to the definition of a set. A bag can have duplicates. A set cannot. For example, {a, b, b} = {a, b}.

slide-8
SLIDE 8

7.1 Constraint satir

7.2 CLP over real r

7.3t Example of CLI

7.4 CLP over finite

Constraint programn

that can be naturall¡

such problems consis

isfy the constraints.

(CLP) combines the c

isfaction is embedde ming with constraini

  • f thinking about p

programming examp

Constraint sat

Motivation

Let us look at som the query:

?- X+1:5. In Prolog this match

is that X is a numbe

more desirable. Usir

this interpretation.

convention for CLP

The constraint abor usual Prolog goals. ' 'understands' opera

  • r inequations of cr

i:,-t:.:i::1.,

ir

tra

7 .1.1

?-{x +1:5}.

x:4

176 Chapter 6 Built-in Predicates

repeat is a control facility that generates an unlimited number of alternatives fo¡

backtracking.

Input and output (other than that associated with querylng the program) are

done using built-in procedures. This chapter introduced a simple repertoire of

such procedures that can be found in many Prolog implementations.

This repertoire assumes that files are sequential. There is tlre current input streøm

and the current output stream. The computer keyboard and display are treated as files called user.

Switching between streams is done by:

see( File)

File becomes the current input stream

tell( File)

File becomes the current output stream

seen

close the current input stream

told

close the current output stream Files are read and written in two ways:

as sequences of characters

as sequences of terms

Built-in procedures for reading and writing characters and terms are:

read( Term)

write( Term) put( CharCode)

geto( CharCode) get( CharCode)

input next term

  • utput Term
  • utput character with the given ASCII code

input next character input next 'printable' character

Two procedures help formatting:

nl

  • utput new line

tab( N)

  • utput N blanks

Many Prolog implementations provide additional facilities to handle non-

sequential flles, windows, provide graphics primitives, input information frorn

the mouse, etc.

Reference to Prolog standard

For some of the predicates mentioned in this chapter, ISO standard for Prolog (Del:lnsaú et al.

1996) recommends different names from those used in most Prolog implementations. However, the predicates are conceptually the same, so compatibility is only a matter of

  • renaming. The concerned predicates in this chapter are: see(Filename), tell(Filenarne),

get(Code), put(Code), name(Atom,Codelist). The corresponding predicate names ill

the standard are: set-input(Filename), set-ouqrut(Filename), get-code(Code),

put_code(Code), atom_codes(Atom,Codelist).

Deransart, P., Ed-Bdali, A. and Ceroni, L. (1996) Prolog: The Standard. Berlin: Springel-Verla9'

slide-9
SLIDE 9

Summary of read( X)

  • The next term T is read and matched with X.
  • If X is a variable, then X is instantiated to T.
  • If there is no match, the goad read( X) fails with no

backtracking.

  • If <control-d> is read from the keyboard, or the end of

file is reached in a file, X is instantiated to end_of_file.

slide-10
SLIDE 10
  • utput streøms. L

input stream. Da

put stream. Botl

names of other f

At any time

'active': one for

input stream and

tion these two s

current input str

see( Filename) .Quch a goal succe

a side effect, that

typical example r

reads something

tell( file3), write-on_file( tell( user), The current outp tell( Filename)

A sequence of

succeeding outp'

see( filel),

read_from_file

see( user),

closes the currer

We will assu. many Prolog irr

tial files, each rt the current posi

position will m<

start reading at

end of a file, 1

end_of_file.

Writing is sir

mation at the e

ward and to ove

The goal

seen

closes the currer

told

164 Chapter 6 Built-in Predicates

:;&þ Input and output

The method of communication between the user and the program that we have been using up to now consists of user questions to the program and prograrn

answers in terms of instantiations of variables. This method of communication is

simple and suffices to get the information in and out. However, it is often not sufficient because it is too rigid. Extensions to this basic communication method

are needed in the following areas:

' input of data in forms other than questions -for example, in the form of English

sentences,

. output of information in any format desired, and ' input from and output to any computer file or device and not just the keyboard

and display. We will now look at built-in facilities for reading data from computer files and

for outputting data to files. These procedures can also be used for formatting

data objects in the program to achieve a desired output representation of these

  • bjects. We will also look at facilities for reading programs and for constructing

and decomposing atoms. Built-in predicates for input and output depend on the

implementation of Prolog. we will study here a simple and handy repertoire of

such predicates, which is part of many Prolog implementations. However, the

manual should be consulted for details and specificities. Many prolog imple- mentations provide various additional facilities not covered here. Such extra

facilities handle windows, provide graphics primitives for drawing on the screen,

input information from the mouse, and so on. 6.7.1

Communication with files

We will first consider the question of directing input and output to files, and then how data can be input and output in different forms.

Figure 6.5 shows a general situation in which a Prolog program communicates

with several files. The program can, in principle, read data ftom several input

files, also called input streams, and output data to several output files, also called

Input

user

file I

file 2

user

file 3 file 4 Ouþut

streams st¡eams

Figure 6.5 Communication between a Prolog program and several files.

User

terminal

slide-11
SLIDE 11

7.1 Constraint satir

7.2 CLP over real r

7.3t Example of CLI

7.4 CLP over finite

Constraint programn

that can be naturall¡

such problems consis

isfy the constraints.

(CLP) combines the c

isfaction is embedde ming with constraini

  • f thinking about p

programming examp

Constraint sat

Motivation

Let us look at som the query:

?- X+1:5. In Prolog this match

is that X is a numbe

more desirable. Usir

this interpretation.

convention for CLP

The constraint abor usual Prolog goals. ' 'understands' opera

  • r inequations of cr

i:,-t:.:i::1.,

ir

tra

7 .1.1

?-{x +1:5}.

x:4

176 Chapter 6 Built-in Predicates

repeat is a control facility that generates an unlimited number of alternatives fo¡

backtracking.

Input and output (other than that associated with querylng the program) are

done using built-in procedures. This chapter introduced a simple repertoire of

such procedures that can be found in many Prolog implementations.

This repertoire assumes that files are sequential. There is tlre current input streøm

and the current output stream. The computer keyboard and display are treated as files called user.

Switching between streams is done by:

see( File)

File becomes the current input stream

tell( File)

File becomes the current output stream

seen

close the current input stream

told

close the current output stream Files are read and written in two ways:

as sequences of characters

as sequences of terms

Built-in procedures for reading and writing characters and terms are:

read( Term)

write( Term) put( CharCode)

geto( CharCode) get( CharCode)

input next term

  • utput Term
  • utput character with the given ASCII code

input next character input next 'printable' character

Two procedures help formatting:

nl

  • utput new line

tab( N)

  • utput N blanks

Many Prolog implementations provide additional facilities to handle non-

sequential flles, windows, provide graphics primitives, input information frorn

the mouse, etc.

Reference to Prolog standard

For some of the predicates mentioned in this chapter, ISO standard for Prolog (Del:lnsaú et al.

1996) recommends different names from those used in most Prolog implementations. However, the predicates are conceptually the same, so compatibility is only a matter of

  • renaming. The concerned predicates in this chapter are: see(Filename), tell(Filenarne),

get(Code), put(Code), name(Atom,Codelist). The corresponding predicate names ill

the standard are: set-input(Filename), set-ouqrut(Filename), get-code(Code),

put_code(Code), atom_codes(Atom,Codelist).

Deransart, P., Ed-Bdali, A. and Ceroni, L. (1996) Prolog: The Standard. Berlin: Springel-Verla9'