[mercury-users] Disjunctions vs. multiple head clauses

Ralph Becket rafe at cs.mu.OZ.AU
Wed Apr 7 14:08:55 AEST 2004


Hi Peter,

Peter Hawkins, Wednesday,  7 April 2004:
> Hi...
> 
> Is there any difference in efficiency between the two predicates bar1
> and bar2 in the following? In Prolog there would (usually) be, but
> does the same apply to Mercury?
> 
> :- type foo ---> fa ; fb ; fc ; fd ; ... ; fz.
> 
> :- pred bar1(foo, int).
> :- mode bar1(in, out) is det.
> 
> bar1(fa, 1).
> bar1(fb, 2).
> bar1(fc, 3).
> ...
> bar1(fz, 26).
> 
> :- pred bar2(foo, int).
> :- mode bar2(in, out) is det.
> 
> bar2(F, N) :-
> 	( F = fa, N = 1
> 	; F = fb, N = 2
> 	; ...
> 	; F = fz, N = 26
> 	).

These two definitions are equivalent (in fact the compiler starts by
turning bar1 into something just like bar2).  Unlike Prolog, which
usually only performs first-argument top-level term indexing, Mercury
performs term indexing on all arguments and even on argument subterms.

If you have a large collection of facts with simple data fields (i.e.
ints, strings and floats) and want to squeeze the last drop of
performance out of the compiler, look up "fact tables" in the
"implementation-dependent extensions" section of the reference manual:
http://www.mercury.cs.mu.oz.au/information/doc-latest/mercury_ref/Fact-tables.html#Fact%20tables

Just as a point of style, if you only have one mode of the form `(in,
out) is det' then it's usually better style to use a function rather
than a predicate.

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