Download A Beginners Guide to Visual Prolog

Transcript
modes, ANSI mode and Unicode mode. The difference is that ANSI mode uses ANSI coding
and Unicode mode uses Unicode coding.
The second attribute, the position, takes care of where you are in an input or output stream. It offers
e.g. the possibility to go back in an input stream to a previous read character. More on this in the next
sections.
11.4 Standard Input and Output: the class StdIO
In VIP there is the class Standard Input and Output. It is called class “stdIO” (without the quotes) and
we have used it already. When a program starts, the stdIO consists of an input stream, an output
stream and an error stream. The input stream is standard connected to the keyboard. When the
strategy of the project is “console”, then the output stream and the error stream are connected to the
console window (the Win32 console). When the project strategy is “GUI” then the output and error
stream are connected to the Message Window.
To begin with, let us take a look at an example program. We create (strategy: “console”) a project
with one class called “database”. The program will read some data from the keyboard, assert them to a
data base and when we end the program, it will write the contents of the database to a file. I put the
predicates in a module “database”. Here is the code, including some clarifying comments.
class database
open core
predicates
classInfo : core::classInfo.
storeNames : (unsigned Counter) -> unsigned Number procedure (i).
fill : () procedure ().
end class database
And in the implementation “database.pro” I add this code
class facts - persons
% the facts are stored in a database with the name “persons”
person : (string Name).
clauses
storeNames(N) = N :-
% this predicates reads a line and adds a fact to the local %
database
stdIO::write("\nInput Name (<Enter> '0' to stop): "),
'0' = stdIO::readchar(),
% the program looks if the first character is ‘0'
!.
% if it is, the program stops
storeNames(I) = Number :stdIO::ungetchar(),
% if not, the program puts back the first character
Term = stdIO::readline(),
% and reads the complete inputline
assert(person(Term)),
% and adds the new person to the database
N=I+1,
% raises the counter by one
196