CS计算机代考程序代写 prolog database compiler flex .

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

Declarative Programming
Non- & Meta-logical features of Prolog

Geraint A. Wiggins

Professor of Computational Creativity
Department of Computer Science

Vrije Universiteit Brussel

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

Input/Output (IO)

▶ Input/Output is a problem for declarative programming
languages

▶ Output: we cannot give a truth value to the action of
printing something

▶ Input: we could, maybe, view input as coming from a
changing Prolog database, but this still gives us problems
in understanding what the program means

▶ The problems get worse when we get on to flexible
computation rules, later!

▶ In fact, there are ways to tell a declarative story about
I/O, but we will not cover them here.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

IO Facilities in Prolog

▶ Two ways of thinking about IO:
▶ file-based;
▶ stream-based

▶ File-based IO is nice and simple, but is not
well-thought-out and can be confusing

▶ Stream-based IO is more complicated, but much more
reliable

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

File-based IO in Prolog

▶ File-based IO is based on the idea of a “current file”
(which may be the terminal)

▶ You can have several files open at once, but you can only
read from or write to one at a time

▶ Each file has a pointer, which marks where the last
operation took place

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

File-based IO in Prolog (2)

▶ We have nine basic predicates:
see/1 choose the current reading file (and open it

if necessary)
seen/0 close the current reading file

seeing/1 is this the current reading file?
read/1 read a term from the current reading file
tell/1 choose the current writing file (and open it if

necessary)
told/0 close the current writing file

telling/1 is this the current writing file?
write/1 write a term to the current writing file

nl/0 write a newline to the current writing file
▶ The terminal is a file called user.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

File-based IO in Prolog (3)
| ?- tell(example).
true
| ?- write(example(term)), write( ‘.’ ).
true
| ?- told.
true
| ?- see(example).
true
| ?- seeing(File).
File = example ?
true
| ?- read(Term).
Term = example(term) ?
true
| ?- seen, seeing(File).
File = user ?
true

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

Stream-based IO in Prolog

▶ We associate each open file with an explicit pointer, with
which we refer to it subsequently.

▶ There are too many predicates to learn usefully – see the
manual for details

▶ The basic stream handling predicates are:
open/3 Arguments: a file specification (+);

a mode (read/write/append) (+); a
pointer (-)

close/1 Argument: a pointer (+)
current_input/1 Argument: a pointer (?)
set_input/1 Argument: a pointer (-)
read/2 Arguments: a pointer (+); a term (?)
write/2 Arguments: a pointer (+); a term (?)

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

General IO in Prolog

▶ Most of the reading and writing predicates come in two
versions:

▶ to the current file/stream (eg write/1)
▶ to a named stream (eg write/2)

▶ The first argument is conventionally the stream pointer

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

The format Predicate

▶ The most useful writing predicates are format/2 and
format/3

▶ format/2 has two arguments:
▶ a format specification (atom or string);
▶ a list of arguments

▶ For example, the predicate call
format( ‘v\n~w – ~w+\n’,

[term1, term2] ).
will print out to the terminal:

v
term1 – term2+

▶ See the manual for more details of format specifications

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

.
.
.

.

Arithmetic Operators

▶ Recall that =/2 means “unify”
▶ We need arithmetic functions, even in logic programs
▶ Here are the basic arithmetic predicates

is/2 computes the value of the arithmetic
expression in its second argument (+) and
unifies it with the first argument (?)

>= > =:= =\= < =