[m-dev.] Bulk stream operations (was Re: [m-rev.] for review: bit buffers)

Simon Taylor staylr at gmail.com
Wed May 16 14:44:19 AEST 2007


On 15-May-2007, Ian MacLarty <maclarty at csse.unimelb.edu.au> wrote:
> On Mon, May 14, 2007 at 04:17:12PM +1000, Simon Taylor wrote:
> > library/stream.m:
> > 	Add classes bulk_writer and bulk_reader, which support
> > 	reading and writing multiple items at once into a store.
> 
> Can you explain why you have created a new typeclass and not used the existing
> stream typeclasses?  It should be possible to create a buffered bit stream
> using a mutvar attached to the stream state.

On reflection, bulk output could be done using an instance as below:
	:- type bitmap.slice ---> slice(bitmap, bit_index, num_bits).
	:- instance stream.writer(io.binary_output_stream, bitmap.slice, io).

The reason for adding the bulk_writer class was to express the uniqueness of
the input argument, but the loss of uniqueness of the input happens with
other predicates like io.write anyway.  I'll remove it.

Input is a bit more difficult.  How can I efficiently read a block of
bytes from a stream without defining a new class?  Adding the bulk_reader
class allows streams that support transfer of blocks of data to be used
efficiently, while other streams can fill the block manually.  Given
that the most commonly used streams support block operations, it's
important to handle those operations well.

> One of the motivating reasons for parameterising readers and writers over the
> unit type was so that both text and binary streams could be handled using the
> same typeclass.  If you define a new typeclass, then some reuse potential is
> lost (for example you cannot use stream.input_stream_fold).

I'm not sure what you mean; a stream can be an instance of both reader
and bulk_reader. bulk_get is used in cases where you don't want to use
input_stream_fold.  Ideally you could build another stream over the top
of them to which you can apply stream.input_stream_fold.  Unfortunately,
Mercury doesn't allow the instances below because of the type variable
arguments.  That restriction also stops us defining useful classes like
stream concatenation and interleaving.

	:- instance reader(read_buffer_stream, bool,
			read_buffer(Stream, State, Error), Error).
	:- instance bulk_reader(read_buffer_stream, bit_index, bitmap,
			read_buffer(Stream, State, Error), Error).
	:- instance bulk_reader(read_buffer_stream, bit_index, word,
			read_buffer(Stream, State, Error), Error).
	:- instance reader(unmarshal_stream, T,
		unmarshal_state(Stream, State, Error),
		unmarshal_error(Error)).
	
Simon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the developers mailing list