[m-dev.] io streams

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Dec 23 13:41:08 AEDT 1999


On 23-Dec-1999, Michael Day <mcda at cat.cs.mu.OZ.AU> wrote:
> 
> 	% Interface for input streams.
> 
> :- typeclass stream__input(S) <= stream__stream(S) where
> 	[
> 		pred stream__read_char(stream__result(char), S, S),
> 		mode stream__read_char(out, di, uo) is det,
> 
> 		pred stream__read_chars(stream__result(list(char)), int, S, S),
> 		mode stream__read_chars(out, in, di, uo) is det,
> 
> 		pred stream__read_chars(stream__result(list(char)), S, S),
> 		mode stream__read_chars(out, di, uo) is det,
> 
> 		pred stream__putback_char(char, S, S),
> 		mode stream__putback_char(in, di, uo) is det
> 	].

I notice that, unlike the current I/O predicates, these take a single
pair for the stream state, rather than taking one argument for the
stream name and an io__state pair.

This is more like the Concurrent Clean model where the io__state is
split into several sub-states.  For this model to work, and be `det'
rather than `cc_multi', you may need to be careful to ensure that the
user can't open two output streams that both refer to the same file.
The procedures for dealing with stdout/stderr seem quite problematical
in this respect:

> 	% Predicates for opening the standard streams.
...
> :- some [S] pred stream__stdout(S, io__state, io__state) => stream__output(S).
> :- mode stream__stdout(uo, di, uo) is det.

For example, if I write

	:- use_module io, stream.

	:- pred main(io__state::di, io__state::uo) is det.
	main -->
		stream__stdout(StdoutA0),
		stream__stdout(StdoutB0),
		{ stream__write_char('x', StdoutA0, _StdoutA1) },
		{ stream__write_char('y', StdoutB0, _StdoutB1) }.
		
then the compiler might decide to reorder the two calls to write_char,
so the eventual output might be `yx' rather than `xy'.
On a parallel implementation, it might differ from run to run.
Yet main is declared `det', not `cc_multi'.

Even worse, the calls to `stream__write_char' are `det', and their
output arguments are unused, so the compiler could (and if you
use the `--no-fully-strict' option, will) optimize those calls
away!


One thing that this interface does not mention is text vs. binary issues.
Is this interface intended only for text I/O?

I'd also like to see the procedures for creating file I/O streams.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list