[mercury-users] Hi, and determinism
Thomas Conway
conway at cs.mu.OZ.AU
Mon Jan 29 11:37:08 AEDT 2001
On Mon, Jan 29, 2001 at 10:08:54AM EST, Dan Sheppard wrote:
> I try to avoid cascades of semidet predicates for these two reason
> (which are really a minor recurrance of the prolog "no" problem), I
> wonder if there's some other way of dealing with them? Perhaps there
> are programming styles within the existing language I have failed to
> notice which would help alleviate these problem?
One thing you can do in many cases is make your predicates det and
return a bool. For example, you sometimes want to test if some
propery is true for a given data structure:
:- pred has_prop(my_structure).
:- mode has_prop(in) is semidet.
% Case 1
has_prop(f(...)) :-
.....
% Case 2
has_prop(g(...)) :-
.....
% Case 3
has_prop(h(...)) :-
.....
% Implicitly fail for all the other cases
When you add another case to my_structure, for which has_prop should
be true, you get no warning, you just get unexpected failure. You
can make this det instead:
:- pred has_prop(my_structure, bool).
:- mode has_prop(in, out) is det.
has_prop(f(...), yes) :-
....
has_prop(g(...), yes) :-
....
has_prop(h(...), yes) :-
....
has_prop(i(...), no).
has_prop(j(...), no).
has_prop(k(...), no).
Now, when you add a new case, you'll get a determinism error.
If the first case needs to be:
has_prop(f(...), Res) :-
...Res....
where Res may be expensive to compute, but you can fail early
if you know that you only want to succeed if Res is yes, then
you can add a second mode declaration:
:- mode has_prop(in, in) is semidet.
In your calls you use:
my_prop(X, yes)
Because you still have the det mode declared, you'll get determinism
errors if you don't handle new cases, but you can still get
failure "early" because you know you need the bool argument to
be yes.
Hope this helps.
--
Thomas Conway )O+
<conway at cs.mu.oz.au> 499 User error! Replace user, and press any key.
--------------------------------------------------------------------------
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