Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

prolog reading a file the ISO style

37 views
Skip to first unread message

levi...@gmail.com

unread,
May 10, 2008, 7:08:52 AM5/10/08
to
I have seen many posts reading a file the Edinborough style, but very
few about the ISO style.

I tried to write a simple program which reads a file, and prints it to
the standard output.


fussal(X):-
% see("parse.pl"),
open("parse.pl",read,[alias(input)]),
repeat,
read(input,T),print(T),T=end_of_file,!,X=false.

Result of running:


| fussal(X).
ERROR: open/3: Type error: `atom' expected, found `[112, 97, 114, 115,
101, 46, 112, 108]'
Exception: (8) open([112, 97, 114, 115, 101, 46, 112, 108], read,
[alias(input)]) ?

levi...@gmail.com

unread,
May 10, 2008, 7:21:55 AM5/10/08
to
Actually I tried the Edinbrough style as well, but I run into errors:

fussal(X):-
X = "parse.pl",
see("parse.pl"),
seeing(X),
read(T),display(T).

Running it:

| fussal(X).
ERROR: see/1: Type error: `atom' expected, found `[112, 97, 114, 115,
101, 46, 112, 108]'
Exception: (8) see([112, 97, 114, 115, 101, 46, 112, 108]) ? creep
6 ?-

Bart Demoen

unread,
May 10, 2008, 7:34:38 AM5/10/08
to
levi...@gmail.com wrote:

> fussal(X):-
> % see("parse.pl"),
> open("parse.pl",read,[alias(input)]),
> repeat,
> read(input,T),print(T),T=end_of_file,!,X=false.
>
> Result of running:
>
>
> | fussal(X).
> ERROR: open/3: Type error: `atom' expected, found `[112, 97, 114, 115,
> 101, 46, 112, 108]'
> Exception: (8) open([112, 97, 114, 115, 101, 46, 112, 108], read,
> [alias(input)]) ?

The message is saying that it expects an atom where it gets something else.
And that it is in the open/3 predicate.
That should ring a bell, no ?

Replace the " by '

Cheers

Bart Demoen

Chip Eastham

unread,
May 10, 2008, 7:35:36 AM5/10/08
to

Try using single quotes rather than the double quotes. This
allows an atom to be formed using varied characters besides
the default syntax for an atom.

Double quotes turn the contents into a list of characters,
as you've seen above.

regards, chip

levi...@gmail.com

unread,
May 10, 2008, 8:45:03 AM5/10/08
to
On May 10, 1:34 pm, Bart Demoen <b...@cs.kuleuven.ac.be> wrote:

Thanks for the comments. I wasn't aware of the differences between '
and "

You just highlighted my ignorance of the Prolog type system. (I know,
Prolog is called untyped. )

levi...@gmail.com

unread,
May 10, 2008, 9:02:48 AM5/10/08
to
On May 10, 1:34 pm, Bart Demoen <b...@cs.kuleuven.ac.be> wrote:

Another problem: it doesn't find the file (although I checked, it's
there). I even tried using double blackslahses, but I got:

ERROR: seeing/1: Domain error: `stream' expected, found `C:\Documents
and Settings\Levente\My Documents\parse.pl'

I even tried to change "\" to "/".

The current version of my code looks like:

fussal(X):-
X = 'C:\Documents and Settings\Levente\My Documents\parse.pl',
see(X),
seeing(X),
read(T),display(T).

levi...@gmail.com

unread,
May 10, 2008, 2:33:15 PM5/10/08
to
I fixed my program. I copy it hereby for the benefit of my fellow
noobs:

fussal(X):-
X = 'c:/parse.pl',
see(X),
% This will give an error, because it expects a stream;
% seeing(X),
repeat,
get_char(T),print(T),T=end_of_file,!,seen.


The conclusions:
1,
If you omit the "seen" from the end of the end of the last line, the
next time you run the program you'll experience that the program only
reads "end_of_file" signs. This is because the "cursor position" of
file reading is assigned to the opened stream. (And a stream acts like
a global variable.)

2,
read/1 is for parsing prolog files. (It can handle the DCG syntax as
well.)

Jan Wielemaker

unread,
May 11, 2008, 3:57:27 AM5/11/08
to

But you started with ISO style. The typical loop using read/2 is:

process(File) :-
open(File, read, In),
read(In, Term1),
process_stream(Term1, In),
close(In).

process_stream(end_of_file, _) :- !.
process_stream(Term, In) :-
<do something with Term>
read(In, Term2),
process_stream(Term2, In).

ISO says nothing on how files are specified, though most systems accept
a (quoted) atom holding the filename. Generally they accept OS native
syntax, which means in Windows you need 'C:\\Program Files\\...'. Often
they also accept the POSIX / syntax as a portable syntax.

Cheers --- Jan

levi...@gmail.com

unread,
May 16, 2008, 8:53:43 AM5/16/08
to
On May 11, 9:57 am, Jan Wielemaker <j...@nospam.ct.xs4all.nl> wrote:

Thank you.

0 new messages