[mercury-users] Request for comments on this code
Nicolas Pelletier
nicolas.pelletier3 at free.fr
Mon Jul 10 03:13:36 AEST 2006
Peter Moulder <Peter.Moulder at infotech.monash.edu.au> writes:
> 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),
I tried it but the compiler does not seem to have support for it yet
(0.13.0-beta-2006-07-05).
nicolas at reglisse:~/dev/dames$ mmc --make --verbose-error-messages queens
Making Mercury/int3s/queens.int3
Making Mercury/cs/queens.c
Uncaught Mercury exception:
Software Error: polymorphism.m: Sorry, not implemented: determinism inference for higher order predicate terms.
Stack dump not available in this grade.
** Error making `Mercury/cs/queens.c'.
>> clock(R, ! IO),
>
[skipping explanation of issues in my use of the clock predicate]
Thank you for your explanation; I was not careful enough when using clock.
> 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
> )
As this code is in the det mode predicate main, I cannot afford to
call false and used error instead. However, the code is rejected (see
below): the (clock(R0, ! IO) -> part is taken to mean that the call to
clock may fail, and thus !.IO on that line does not get instantiated
with the right mode.
main(! IO) :-
command_line_arguments(Args, ! IO),
(Args = [StringNum], to_int(StringNum, N)
->
(clock(R0, ! IO)
->
(solutions((pred(S :: out) is nondet) :- queens(N, S), Q)
->
clock(R1, ! IO),
foldl(print_nl, Q, ! IO),
length(Q, Nq),
format("%u solutions: %u seconds",
[i(Nq), i((R1 - R0) / clocks_per_sec)], L),
print_nl(L, ! IO)
;
error("No solutions")
)
;
error("Cannot get current time")
)
;
error("Usage: queens <number_of_queens>")
).
I do not see a '&&' operator in the builtin operators section of the manual.
>> 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.
Okay, I'm removing it. Thank you for you various comments.
--
Nicolas
--------------------------------------------------------------------------
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