[mercury-users] reading term
Peter Ross
pro at missioncriticalit.com
Mon Feb 7 23:04:11 AEDT 2005
On Mon, Feb 07, 2005 at 09:20:16PM +1100, Julien Fischer wrote:
> ReadResult = ok(X `with_type` list({int,int,int})),
> io.print(X, !IO)
I think the with_type is the key point that may be causing problems
for Elmar.
In Prolog, read/1 can return any valid term. Of course any predicates
which attempts to use this term will fail, if the term is not of the
correct type.
Mercury being strongly typed, the type checker must be able to assign
a type to every variable. As a side effect of this the io__read/3
predicate gets passed as a hidden argument a description of the type
we are trying to read as a term. This ensures that io__read/3 will only
accept terms of the correct type.
Juliens code has the problem that the type checker cannot infer the type
of the variable X, because at no point do we call a predicate which
constrains the type of X.
This is why in Juliens program one must use a `with_type` annotation to
constrain the type of X.
Another approach would be change the snippet of code to be
ReadResult = ok(X),
print_int_tuple(X, !IO)
with the following code being added to the system.
:- pred print_int_tuple({int, int, int}::in, io::di, io::uo) is det.
print_int_tuple(X, !IO) :-
io.print(X, !IO).
Here the type of X is constrained by the print_int_tuple pred
declaration.
So in summary Prolog accepts any term, while Mercury will only read
terms of the correct type, after it proves using type checking the type
of term which will be read in.
Pete
--------------------------------------------------------------------------
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