[mercury-users] A suggestion for a tutorial

Nicolas Pelletier nicolas.pelletier3 at free.fr
Mon Jul 3 23:13:35 AEST 2006


Hello again,

as a newbie to Mercury (and not  much experienced in logic programming
either), I  find myself making  the following error  more often than I
would like ;-)

While writing predicates such as this one:

:- pred square_list(int :: in, int :: in, list(int) :: out) is nondet.
square_list(N, M, L) :-
    N > 0 -> member(X, 1 .. M), square_list(N - 1, M, L0), L = [X | L0]
    ; L = [].

I often feel the  impulse to "over-factorize" the  code and write this
erroneous code instead:

:- pred square_list(int :: in, int :: in, list(int) :: out) is nondet.
square_list(0, _, []).
square_list(N, M, L) :-
    member(X, 1 .. M), square_list(N - 1, M, L0), L = [X | L0].

The compiler does not see anything wrong about it (and  I do not blame
it), and I am left puzzled contemplating a run-time error such as:

PC at signal: 135025514 (80c536a)
This may have been caused by a stack overflow, due to unbounded recursion.

The root cause  of   my writing such code    seems to be my   lack  of
understanding of the  nondet mode, but I  guess I am not  alone having
some trouble with modes. So I thought I might  show this error so that
people writing tutorials  or cookbooks could consider  warning newbies
against such misunderstandings.

If there are links I should read instead, please point me to them.

Regards,

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