[mercury-users] Request for comments on this code

Peter Moulder Peter.Moulder at infotech.monash.edu.au
Sun Jul 9 15:12:49 AEST 2006


On Mon, Jul 03, 2006 at 02:57:53PM +0200, Nicolas Pelletier wrote:

>         solutions((pred (S :: out)) is nondet :- queens(N, S), Q),

Incidentally, queens/2 has just one mode, with its last argument being
`out', so it's fine to write:

	  solutions(queens(N), Q),


>         clock(R, ! IO),

There are two issues with this use of clock.  First, you're currently
relying on Un*x behaviour, where clock results start at zero.  For
greater portability, you should make a pair of calls to clock (before &
after doing the work you want to measure), and subtract.  Even on
Unix-like systems, you may wish to use this approach so as not to count
any start-up time associated with your current compilation grade.

The second issue is that Mercury implementations are by default free
to reorder conjuncts (*see (mercury_ref)Semantics).  I'm sure most
implementations will keep the solutions call before the clock call,
but this isn't guaranteed; parallelizing implementations in particular
might notice that the solutions call and the clock call are completely
independent and might benefit from being put in separate threads
(especially if it doesn't know that `clock' calls are always quick,
unlike some other I/O calls that can block arbitrarily long).

You can force an ordering by using if-then-else: Mercury guarantees that
the `if' goal is executed entirely before the `then' goal (modulo any
backtracking).  We want R0 to be calculated before the solutions call,
and the solutions call to be before R1, so we use two if-then-else's:

  ( clock(R0, !IO) ->
  	( solutions(queens(N), Q) ->
		clock(R1, !IO),
		...
	 ; false
	)
   ; false
  )

(I think that there was a proposal for a `&&' operator to make the above
more readable; I can't remember the fate of that proposal.)

>         flush_output(! IO)

Shouldn't be necessary given that it's just before program exit --
just as fflush(stdout) isn't necessary in `hello world' in C.

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