[mercury-users] Some more Queries

Nancy Mazur Nancy.Mazur at cs.kuleuven.ac.be
Thu Nov 7 21:32:38 AEDT 2002


* Noel  Pinto <cool4life at rediffmail.com> [2002-11-07 11:15]:
> Hi,
> 
> I have some more queries. I am trying to consolidate them here, 
> instead of sending separate mails.
> 
> 1) io__open_input(File, Result, IO0, IO)
> The Result is either ok(Stream) or error(ErrorCode)
> Plz tell me what is the Stream in ok(Stream)?? , because in this 
> syntax the input is made in the file.
> What is ErrorCode???

The main advantage of Mercury over the more classical logic programming
language Prolog is the fact that programmers need to provide explicit
declarations. These declarations are not only useful for the compiler
but are essential parts of documentation that can also be helpful for
programmers or in this case library users. 
Don't limit yourself to the comments of the library predicates, read the
"full declarations"! 

[grabbing your hand, and travelling through the code with you]

In this case, the predicate and mode declaration of io__open_input
states: 
:- pred io__open_input(string, io__res(io__input_stream),
			io__state, io__state).
:- mode io__open_input(in, out, di, uo) is det.

So, let's give the reading of this declaration: given an input string
(first argument is a string, and "in" mode), it produces as output
something of the type "io__res(io__input_stream)". 

Now, take a closer look at these types. You have: 

:- type io__res(T)	--->	ok(T)
			;	error(io__error).

and

:- type io__input_stream.

and

:- type io__error.	% Use io__error_message to decode it.

So, the result of opening an input is either
1. ok(T), where T is instantiated by "io__input_stream" (as declared in
the predicate declaration). Which means that if the predicate returns an
ok-term, more specifically ok(Something), than that something is of type
"io__input_stream", i.e. the filehandle you need in your further
processing. 

2. error(io__error), where io__error is a hidden type. If that is what
your predicate returned, then probably, an error occurred (it's not
officially documented like that, but why would the developpers have
chosen names such as "ok" or "error"?). The exact error description will
be in the argument of the error-term. So if you have something returned
as error(Something), then Something will contain a description of the
error message, and as the documentation of the type "io__error"
suggests, use predicate "io__error_message" to decode it. 

To make life easier for you... in your code you could have the following

main --> 
	io__open_input("blabla.txt", Result), 
	(
		{ Result = ok(Stream) }
	-> 
		% then we could successfully open the file... 
		% so here you can use Stream
		io__read(Stream, Input), ... 
	; 
		% or else something has gone wrong
		{ Result = error(Error) }
	-> 
		{ io__error_message(Error, ErrorMsg) }, 
		io__write_string(ErrorMsg)
	), 
	... 


> 2) In io__input_stream_name, for a file stream, the human readable 
> name associated the current input stream is retreived and for 
> stdin a string <standard input> is retreived.
> Why is standard input in brackets?

Because it's a special case and simply here a convention. 

Nancy
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list