[mercury-users] Functional square root approximation : help needed

Robert Bossy bossy at ccr.jussieu.fr
Fri Apr 27 01:58:49 AEST 2001


Hello,

> [localhost:~/src/mercury/sqrt] metaperl% mmc -E --infer-all sqrt.m
> sqrt.m:072: Error: syntax error in predicate mode declaration: _1(in, in, out).

This comes from:
> :- mode GoodEnough(in, in, out) is det.
> GoodEnough(Guess,X,Bool) :-

In mercury it makes a very big difference between identificators
beginning with an uppercase or a lowercase character, this is inherited
from prolog syntax.
Capitalized : (logical) variables, also variable type parameter
Uncapitalized : type, function and predicate names, also inst and mode
names

A quick look at the standard library that mercury predicate names are
conventionally all lowercase with underscores between words, I suggest:

:- mode good_enough(in, in, out) is det.


> sqrt.m:004: In definition of predicate `sqrt:main'/2:
> sqrt.m:004:   error: undefined type `in'/0.
> sqrt.m:004: In definition of predicate `sqrt:main'/2:
> sqrt.m:004:   error: undefined type `out'/0.

This comes from:
> :- pred main(in, out) is det.

The ":- pred" declaration introduces a predicate and the type of its
arguments. So the compiler understood 'in' as 'out' type names, not as,
I guess you wanted to, modes.
The correct declaration would be:
:- mode main(in, out) is det.

However this still won't work because main/2 is a special predicate that
must be declared as:
:- mode main(di, uo) is det.

However (again), this still won't work because the compiler will infer
main types as:
:- pred main(T1,T2).

Meaning that main is polymorphic on its 2 arguments, now main/2 is a
special predicate that must be declared as:
:- pred main(io__state,io__state).

This means you'll have to import the 'io' module:
:- interface.
:- import_module io.

Thus, the full declaration of main is:
:- interface.
:- import_module io.

:- pred main(io__state, io__state).
:- mode main(di, uo) is det.

or, in a one liner style:
:- interface.
:- import_module io.

:- pred main(io__state::di, io__state::uo) is det.




I didn't read your code in detail, maybe other errors will arise...
I will make some more general suggestions & remarks:

1) main/2 is a special predicate, a starter: it takes io__state
arguments. That means it interacts with the rest of your computer by
performing input/output. I suggest you use main/2 only for result
display. Something like:

main -->
	{ guess_sqrt_wrapper_predicate(5,X) },
	print(X),
	nl.

So you separate calculus from display, it's good habit.


2) Emacs has a prolog major mode with a nice mercury minor mode that
even does identation, the adress is somewhere in this list archive but
the pointed url appears to be down so I can post it. Browsing the
archive you can also find a syntax file for vi.




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