determinism inference weakness

Thomas Charles CONWAY conway at cs.mu.OZ.AU
Thu Sep 10 12:50:01 AEST 1998


Don Smith, you write:
> Hi,
> 
> Consider the following definition of partition/4 (as used in qsort). 
> 
>    :- pred partition(int,list(int),list(int),list(int)).
>    :- mode partition(in,in,out,out) is det.
>    partition(_, [], [], []).
>    partition(P, [H|T], L1, L2) :- H < P, L1 = [H|R], partition(P, T, R, L2).
>    partition(P, [H|T], L1, L2) :- H >= P, L2 = [H|R], partition(P, T, L1, R).
> 
> I'm surprised that the compiler can't determine that the tests H<P and H>=P 
> are mutually exclusive and exhaustive.   (Of course, I can write it
> with ->, but I shouldn't have to.)

Currently mutual exclusion is based solely on knowledge of what functors
a variable may be bound. Since the modes of < and >= don't yield any such
information, making the compiler smart about them would require a whole lot
more work. If you want mutual exclusion without using ->; you could write
code like:

partition(_, [], [], []).
partition(P, [H|T], L1, L2) :-
	compare(Res, H, P),
	(
		Res = (<),
		L1 = [H|R], partition(P, T, R, L2)
	;
		Res = (=),
		L2 = [H|R], partition(P, T, L1, R)
	;
		Res = (>),
		L2 = [H|R], partition(P, T, L1, R)
	).

-- 
Thomas Conway <conway at cs.mu.oz.au>
Nail here [] for new monitor.  )O+



More information about the users mailing list