[mercury-users] Beginner question about pairs

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Jun 18 07:45:28 AEST 2001


On 17-Jun-2001, Dave Slutzkin <dave_slutzkin at yahoo.com> wrote:
> --- Dave Slutzkin <dave_slutzkin at yahoo.com> wrote:
> 
> I apologise for the waste of space...  As always, it
> takes fifteen minutes after you post for the solution
> to hit you.  My mistake was assuming that a compiler
> error message is actually useful :-).  It actually
> applied to a different line.  (But it seems that a
> clause is parsed without any whitespace.)

The reason that the line number in this error message
is misleading is due to two things:

	(1) For this particular error, the compiler only knows which
	    clause the error occurred in, not which exact line.

	(2) The compiler only keeps track of starting line numbers,
	    not ranges.

> > :- pred
> > check_place(pair(int,int),list(pair(int,int))).
> > :- mode check_place(in,in) is det.
> > check_place(_,[]).
> > % line 69 follows...
> > check_place(X-Y,[(OldX-OldY)|Rest]) :-
> > 	cptemp(X,Y,OldX,OldY) ,
> > 	(if (Y=OldY) then fail else true) ,
> > 	(if abs(minus(OldX,X))=abs(minus(OldY,Y)) then fail
> > else true) ,
> > 	check_place(X-Y,Rest).
> 
> The error:
> 
> > n_queens.m:069: In clause for predicate
> > `n_queens:check_place/2':
> > n_queens.m:069:   error: ambiguous overloading
> > causes
> > type ambiguity.
> > n_queens.m:069:   Possible type assignments include:
> > n_queens.m:069: V_10 :: int or (pred int)
> 
> This was because the compiler wasn't sure whether the
> two expressions containing 'abs' referred to the func
> (with type = int) or the pred (with type pred int)
> abs.  To resolve it, I changed to the predicate
> version of abs.
...
> Was there another way to do this?  Am I correct in my
> diagnosis?

Your diagnosis is correct.

Another way to resolve the ambiguity is to add an explicit
type qualifier "`with_type` int",
i.e.

	(if abs(...) = abs(...) `with_type` int then fail else true)

This requires version 0.10 or later.

BTW, rather than using if-then-else here, you can write that more concisely as

	not (abs(...) = abs(...) `with_type` int)

or

	abs(...) \= abs(...) `with_type` int

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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