[mercury-users] Some more Queries

Ralph Becket rafe at cs.mu.OZ.AU
Mon Nov 11 15:05:46 AEDT 2002


Noel  Pinto, Monday, 11 November 2002:
> 
> I did follow your suggestions and wrote the code as below...
> :- 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 `with_type` io__result(string)),
> 		(
> 			{ResStr = ok(Binr `with_type` string)}
> 		->
		^^
This is an if-then-else, not a switch.  I'm pretty certain the code I
posted to you was a switch.

Even though other people may, don't use (_ -> ; _), use
(if _ then _ else _) in your code.  It makes it much clearer what's
going on in your code.

A switch would look like this

	(
		{ Res = ok(Str) },
		...
	;
		{ Res = eof },
		...
	;
		{ Res = error(Err) },
		...
	)

Every possible binding of Res is tested for at the start of some arm of
the switch, which otherwise looks exactly like an ordinary disjunction.

> 			io__write_string(" Binary representation of a term 
> 			is : "),
> 			io__write_binary(Binr `with_type` string), nl
> 		;
> 			( { ResStr = eof }
> 		->
> 			io__write_string(" End of File.\n")
> 		;
> 			{ ResStr = error(Err1)},

The Mercury compiler will not deduce that this unification must succeed
at this point, but it does know that ResStr will be bound to *something*
here, so it infers that this unification may fail - i.e. is semidet.

That means that this whole "else" goal is semidet and this propagates
all the way up to the determinism of main/2 - at which point the
compiler spots that you said main/2 should in fact be det, hence the
error message.

- 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