[mercury-users] Predicate to count the number of solutions

Chris King colanderman at gmail.com
Thu Apr 14 04:01:03 AEST 2011

Mercury has trouble inferring higher-order modes.  You'll want to write out

:- mode cnt(pred(out) is nondet) = out.

- Chris

On Apr 13, 2011 1:22 PM, "Vladimir Gubarkov" <xonixx at gmail.com> wrote:


I'm trying to create predicate for counting the whole number of solutions.
I'm trying

:- module t2.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module list, solutions, int.

take(E, !L) :- list.delete(!.L, E, !:L).

:- pred perm(list(T), list(T)).
:- mode perm(in, out) is multi.
perm([], []).
perm(L @ [_|_], [E|EE]) :- take(E, L, L1), t2.perm(L1, EE).

cnt(P) = N :-
 promise_equivalent_solutions [N] (
unsorted_aggregate(P, (pred(_V::in, Acc0:int::in, Acc:int::out) is det :-
 Acc = Acc0 + 1), 0, N)

main(!IO) :- write_int(cnt(t2.perm(1..5)), !IO).

I want it to show me answer=120, but getting compilation error:

Making Mercury\cs\t2.c
t2.m:011: Inferred :- pred take(T, list.list(T), list.list(T)).
t2.m:011: Inferred :- mode take(out, in, out).
t2.m:018: Inferred :- func cnt((pred T)) = int.
t2.m:020: In clause for `cnt(in) = out':
t2.m:020:   in call to predicate `solutions.unsorted_aggregate'/4:
t2.m:020:   mode error: arguments
t2.m:020:   `TypeInfo_for_T, TypeCtorInfo_19, P, V_8, V_9, N' have the
t2.m:020:   following insts:
t2.m:020:     ground,
t2.m:020:     unique(<type_ctor_info for .int/0>),
t2.m:020:     ground,
t2.m:020:     /* unique */(pred((ground >> ground), (ground >> ground),
t2.m:020:     >> ground)) is det),
t2.m:020:     unique(0),
t2.m:020:     free
t2.m:020:   which does not match any of the modes for predicate
t2.m:020:   `solutions.unsorted_aggregate'/4.
Error: system command received signal 1.
** Error making `Mercury\cs\t2.c'.

Is it possible to make it work?

Thank you,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20110413/36abee1c/attachment.html>

More information about the users mailing list