[mercury-users] About functional notation and IO

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Jun 18 16:40:22 AEST 1999


On 14-Jun-1999, Gustavo A. Ospina <gos at info.fundp.ac.be> wrote:
> Could be an alternative to use DCG notation the use of a pragma to declare
> predicates "that will use IO", and to hide the parameters "state of world"?

Mercury already has something like that: `impure'.  See the "Impurity"
section of the "Pragmas" chapter of the Mercury language reference manual.

You could implement an I/O library using `impure' rather than
using `io__state', if you wanted.

> It would be something like this fool example.
...
> % This is my idea
> :- pragma io_pred hi.
> :- pred hi is det.
> 
> hi :-
> 	io__write("Hi").

Using `impure', it would look like this:

	:- impure pred hi is det.
	hi :-
		impure io__write("Hi").

Note that the call is annotated with `impure', not just the pred declaration.
If you omit the `impure' annotation on the call, then you get a compile error.

> The main idea is eliminate the confusing use of DCG and the {}, cause of
> many ocasional (but easy to fix) errors in compiling. Predicates using IO
> would be distinguished by the pragma declaration, which would make
> unnecessary the use of {}. In my view, the declarative semantics of IO is
> not altered.

The purpose of the {} in DCGs and the `impure' annotation on calls to impure
goals is to allow the reader to tell at a glance which calls might do I/O
or have side-effects and which ones don't.

Yes, these annotations make writing code very slightly more difficult,
but IMHO they make reading code easier.  And code is read far more often
than it is written.

Cheers,
	Fergus.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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