[mercury-users] type error in argument(s) of functor `string:det_to_int/1'

Robert Bossy bossy at ccr.jussieu.fr
Sat Apr 28 21:54:20 AEST 2001


Terrence Brannon a écrit :
> 
> [localhost:mercury/primality/divisors] metaperl% mmc -E --infer-all divisors.m
> divisors.m:010: In clause for predicate `divisors:main/2':
> divisors.m:010:   in unification of variable `I'
> divisors.m:010:   and term `string:det_to_int(W)':
> divisors.m:010:   type error in argument(s) of functor `string:det_to_int/1'.
> divisors.m:010:   Argument 1 (W) has type `(io:result(string))',
> divisors.m:010:   expected type was `string'.
> [localhost:mercury/primality/divisors] metaperl%
> 
> ===== here is line 10
> 
> main --> io__read_line_as_string(W), { I = string__det_to_int(W) }, io__write_string(test(I)).

It is a typical type error. Take a look at the io__read_line_as_string
and string__det_to_int declarations in the library reference manual:

:- pred io__read_line_as_string(io__result(string), io__state,
io__state).

The first argument must have type io__result(string), so the variable W
must have type io__result(string). Now:

:- func string__det_to_int(string) = int.

The first argument must have type string, so the variable W must have
type string.
A variable (as any term) cannot have two different types, just one.


If you look at the io__result/1 type declaration:

:- type io__result(T)   --->    ok(T)
                        ;       eof
                        ;       error(io__error).

You must write a switch on the value returned by io__read_line_as_string
which can be:
either	ok(S) and S has type string, meaning the operation went well and
S is the result string
either	eof meaning end of file was foud before end of line
either	error(E) and E has type io__error, meaning there was an IO error
while reading the line
 

The correct clause for main would be something like:

main -->
	io__read_line_as_string(W0),
% That's a switch
	(
		{ W0 = eof) },
		print("Nothing could be read"), nl
	;
		{ W0 = error(Err) },
		print(io__error_message(Err)), nl
	;
% Here comes your code
		{ W0 = ok(W),
		  I = string__det_to_int(W) },
		io__write_string(test(I))
	).



Remember, Mercury terms are (very) strongly typed at compile time. This
seems to be painful but it saves a lot of debugging because once
compiled, the code works as expected.


Regards,

-- 
sig(Robert,Bossy) :-
      bossy at ccr.jussieu.fr
.
--------------------------------------------------------------------------
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