[m-dev.] for review: stream I/O

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Oct 2 19:18:28 AEDT 2000


On 02-Oct-2000, Peter Ross <petdr at miscrit.be> wrote:
> On Mon, Oct 02, 2000 at 04:40:12PM +1100, Simon Taylor wrote:
> > 
> > Peter Ross wrote:
> > > Estimated hours taken: 40
> > > 
> > > Implement generic stream based I/O.
> >  
> > > string.m:
> > >     Define an instance of the stream typeclass which works on strings.
> > 
> > > Index: string.m
> > > ===================================================================
> > 
> > > @@ -362,6 +369,33 @@
> > >  
> > >  :- implementation.
> > >  :- import_module bool, std_util, int, float, require.
> > > +
> > > +:- instance stream(string) where [].
> > > +:- instance stream__input(string) where [
> > > +	pred(read_character/3) is read_char
> > > +].
> > > +:- instance stream__output(string) where [
> > > +	pred(write_character/3) is write_char
> > > +].
> > > +:- instance stream__duplex(string) where [].
> > > +:- instance stream__side_effect_free(string) where [].
> > > +
> > > +:- pred read_char(stream__result(char)::out, string::di, string::uo) is det.
> > > +
> > > +read_char(Result, String0, String) :-
> > > +	( string__first_char(String0, Char, String1) ->
> > > +		Result = ok(Char),
> > > +		copy(String1, String)
> > > +	;
> > > +		Result = eof,
> > > +		copy(String0, String)
> > > +	).
> > > +		
> > > +:- pred write_char(char::in, string::di, string::uo) is det.
> > > +
> > > +write_char(Char, String0, String) :-
> > > +	string__append(String0, string__char_to_string(Char), String1),
> > > +	copy(String1, String).
> > 
> > I think this is far too inefficient to be useful.
> > If you still want to commit this, it would be better to add an
> > abstract type `:- type string__stream ---> stream(string).',
> > and make that an instance of the stream typeclasses instead.
> > That would make it easier to change the implementation to
> > something more efficient later.
> > 
> Yes I realise that this is highly inefficient, but the nice thing about
> it is that you can change the implementation to be whatever you want.
> 
> There definately should be a smarter string implementation which uses a
> buffer which grows as necessary to do it's I/O in.

I think Simon's point is that what is useful and appropriate as
an implementation for strings is not necessarily the same as what
would be useful and appropriate as an implementation for string streams.

For strings, there are important interoperability advantages to using the
same representation as other languages that you want to interoperate with.
And for strings, the frequency of destructive update operations is likely
to be much rarer than for string streams, so you might want to use an
different data structure.

I agree with Simon that as currently implemented, string streams are
likely to be too inefficient to be useful, and so I don't think they
should be added to the standard library in their current form.

-- 
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