[mercury-users] Puzzled about about I/O in predicates which may fail

Gregory D. Weber gdweber at indiana.edu
Wed Nov 24 13:44:57 AEDT 2004


It seems to me that by using state variables, I can "fix" an
error involving I/O in predicates which may fail.
If read_problem_decl_semidet is indeed semidet, then

(EXAMPLE 1)

main(!IO) :-
	(
	  read_problem_decl_semidet("credit.decl", Problem, !IO) ->
	  describe_problem(Problem, !IO)
	;
	  io__print("Error reading file\n", !IO)
	).

compiles without any error message.  Moreover, running the program
actually works!

But it seems that Example 1 is equivalent to Example 2 and Example 3,
both of which the compiler finds erroneous:

(EXAMPLE 2)

main(IO0, IO) :-
	(
	  read_problem_decl_semidet("credit.decl", Problem, IO0, IO1) ->
	  describe_problem(Problem, IO1, IO)
	;
	  io__print("Error reading file\n", IO0, IO)
	).

alt_sac.m:026: In clause for `main(di, uo)':
alt_sac.m:026:   in argument 3 of call to predicate `alt_sac:read_problem_decl_semidet/4':
alt_sac.m:026:   mode error: variable `IO0' has instantiatedness `mostly_unique',
alt_sac.m:026:   expected instantiatedness was `unique'.

(EXAMPLE 3)

main -->
	(
	  read_problem_decl_semidet("credit.decl", Problem) ->
	  describe_problem(Problem)
	;
	  io__print("Error reading file\n")
	).

alt_sac.m:025: In clause for `main(di, uo)':
alt_sac.m:025:   in argument 3 of call to predicate `alt_sac:read_problem_decl_semidet/4':
alt_sac.m:025:   mode error: variable `DCG_0' has instantiatedness `mostly_unique',
alt_sac.m:025:   expected instantiatedness was `unique'.

I understand why Examples 2 and 3 are incorrect; it is clearly
explained in the Prolog to Mercury transition guide:

"One of the important consequences of our model for input and output
is that predicates that can fail may not do input or output. This is
because the state of the world must be a unique object, and each IO
operation destructively replaces it with a new state. Since each IO
operation destroys the current state object and produces a new one, it
is not possible for IO to be performed in a context that may fail,
since when failure occurs the old state of the world will have been
destroyed, and since bindings cannot be exported from a failing
computation, the new state of the world is not accessible."

What I don't understand is why example 1 _is_ (apparently) correct.
Is example 1 _not_ equivalent to examples 2 and 3?
Why does using state variables magically "fix" the error?
Is it really incorrect, but the compiler doesn't notice?

-- 
Gregory D. Weber

Associate Professor of Computer Science, Indiana University East
2325 Chester Boulevard, Richmond, Indiana 47374-1289, U.S.A.
Telephone (765) 973-8420          World-Wide Web http://mypage.iu.edu/~gdweber/

----
ASCII plain text is the document format that maximizes readability and 
minimizes hassle and hazard.  It is the format of the official documents
defining Internet protocols (http://www.rfc-editor.org/).


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