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

Simon Taylor stayl at cs.mu.OZ.AU
Mon Oct 2 16:40:12 AEDT 2000


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.

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