[mercury-users] &&/if-then-else semantics (was Re: Request for comments on this code
Peter Moulder
Peter.Moulder at infotech.monash.edu.au
Fri Jul 14 17:44:58 AEST 2006
On Tue, Jul 11, 2006 at 06:13:29PM +1000, Peter Schachte wrote:
> On Tue, Jul 11, 2006 at 01:47:42PM +1000, Julien Fischer wrote:
> > On Mon, 10 Jul 2006, Peter Schachte wrote:
> > > Isn't && only advisory?
> >
> > What would be the point of it if it were only advisory?
>
> Telling Mercury a preferred order for two goals. A suggestion to the
> compiler.
Plain `,' already acts as a weak suggestion in practice (Mercury
implementations tend not to reorder conjuncts without some evidence
of benefit), and has some guarantees when the user requests strict
sequential operational semantics (*see (mercury_ref)Semantics).
> What do you want && for?
The most common use of the operational guarantees of if-then-else that
I've seen are when the condition goal is semidet, and we don't want the
`then' goal (or `else' goal) to execute even speculatively until we know
that the condition goal is true (or false, respectively): whether because
this goal would otherwise be slow, or not complete (recurse infinitely
or call error/1), or cause a segfault or memory corruption etc. (the
various unsafe_blah procedures in the Mercury library, or custom foreign
code).
Many of the the same apply to &&/||, e.g.
‘(0 =< I, I < N) && unsafe_blah(I)’, or
‘( ( 0 =< I, I < N ) || error("index out of bounds") ), REST_OF_CLAUSE’.
The case when the condition goal (the left operand of &&/||) is det is
rarer; this clock case may be the first time I've come across it. Other
cases might be:
- io.write_string("Calculating something difficult, please wait...\n", !IO)
% (or sync disks, or checkpoint our state to disk)
&& calculate_something_difficult(X)
Though each of these three examples involves I/O, which Julien
believes to be illegal in the current compiler, because uniqueness
analysis occurs before the compiler "knows" that the I/O call is
det.
- consume(Variable_that_occupies_most_available_memory)
&& do_something_requiring_lots_of_memory().
Memory concerns are less common these days than previously, but OTOH
data is becoming bigger, and it's becoming more common to run out of
address space on our 32-bit systems. Actually, I ran out of memory
with a mercury program just last night, with 1GB RAM and 2GB swap
space on an otherwise-unloaded computer. The mercury compiler
itself has memory difficulties when compiling computer-generated
source files.
Must go now. There may be other non-I/O examples involving being
careful when we spend CPU or memory.
pjrm.
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at csse.unimelb.edu.au
administrative address: owner-mercury-users at csse.unimelb.edu.au
unsubscribe: Address: mercury-users-request at csse.unimelb.edu.au Message: unsubscribe
subscribe: Address: mercury-users-request at csse.unimelb.edu.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list