[mercury-users] Some more Queries

Ralph Becket rafe at cs.mu.OZ.AU
Sun Nov 10 10:39:55 AEDT 2002


Noel  Pinto, Saturday,  9 November 2002:
> :- module test3.
> 
> :- interface.
> :- import_module io.
> :- pred main(io__state::di, io__state::uo) is det.
> 
> :- implementation.
> 
> main -->
> 	io__open_binary_input("blah.txt", Res),
> 	(
> 		{ Res = ok(Str)}
> 	->
> 		io__read_binary(Str, ResStr),
> 		(
> 			{ResStr = ok(Binr)}
> 		->
> 			io__write_string(" Binary representation of a term 
> 			is : "),
> 			io__write_binary(Binr), nl
[...]

The problem here is that the compiler does not have enough information
to work out what type Binr is supposed to have - it can only get as far
as giving it a type *variable*, T.  This in turn means that it can only
work out that the type of ResStr should be io__result(T).  The language
specification insists that variables may only have types with type
variables that occur in the signature of the predicate they belong to.
Since the signature of main/2 contains no type variables, your program
is in error.

Put more simply, the compiler needs to know what type of value is
expected by io__read_binary (it actually supplies a hidden argument
containing this information so io__read_binary knows what to look for).
Similarly for io__write_binary.

The solution is to ensure that Binr has a fixed type.  For example, if
you want to read and write binary strings, then change the first
occurrence of `Binr' to `Binr `with_type` string' in your program.

One point to be aware of: at the moment we don't do anything different
for binary representations - they in fact just call the plain text IO
predicates.  One day we'll refine this so that binary IO should output
terms that are (hopefully) shorter and faster to read in.

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