for review: Add implementation of reference types, plus examples

Peter Schachte pets at students.cs.mu.OZ.AU
Wed Jan 28 18:55:03 AEDT 1998


> > :- pred between(int, int, int).
> > :- mode between(out, in, in) is nondet.
> > 
> > between(I, Low, High) :-
> > 	Low =< High,
> > 	(   I = Low
> > 	;   between(I, Low+1, High)
> > 	).
> 
> I've implemented that too often myself.
> That predicate is worth putting in library/int.m, I think.
> 
> The following implementation
> 
> 	between(I, Low, High) :-
> 		( Low = High ->
> 			I = Low
> 		;
> 			Low < High,
> 			(	
> 				I = Low
> 			;	
> 				Low1 is Low + 1,
> 				between(I, Low1, High)
> 			)
> 		).
> 
> creates fewer choice points, so it is probably a better choice
> for a standard library implementation of it.

Ok.  But I think:

int__between(I, Low, High) :-
	(   Low < High ->
		(   
			I = Low
		;   
			Low1 is Low + 1,
			between(I, Low1, High)
		)
	;
		Low = High,
		I = Low
	).

is a bit better still, because it only needs to perform one test each time
around the loop.

However, for such a library predicate, I think it's pretty important to have
a int__between(in, in, in) is semidet mode.  People will expect it.  It's an
implied mode, so we can't even stop them from using it.  And I expect this
code will mode check fine, it's just that it'll "count" in order to check if
Low =< I =< High.  This doesn't seem like a very good implementation of
range testing :-(.  Ideally, we'd have different code for the different
modes (here's that need again!), but pragma c_code isn't a very attractive
way to implement the generation mode of this predicate.

Is there an attractive solution I don't see?  Or should I just forget
putting this in the library?


-Peter Schachte			| We don't know who discovered water, but we
pets at cs.mu.OZ.AU		| are certain it wasn't a fish. -- John Culkin 
http://www.cs.mu.oz.au/~pets/	| 
PGP key available on request	| 




More information about the developers mailing list