[mercury-users] How to vary implementations of a pred declaration?

Zoltan Somogyi zs at cs.mu.OZ.AU
Thu Mar 23 13:04:00 AEDT 2006

On 23-Mar-2006, Julien Fischer <juliensf at cs.mu.OZ.AU> wrote:
> > Hi!  I have a rather complicated and lengthy predicate declaration (it's
> > arity 25).  It is implemented by anywhere from 150 to 10000 clauses,
> > depending on which one of the five different executables I'm producing.
> >
> > My question is as follows: is there a way to make this declaration once
> > in one place and then direct the compiler to select the implementation
> > file appropriate for the current task at hand?
> >
> > Copy-and-paste coding is tedious and error-prone (even declarative
> > and pure coding), so I hope there's something like a command-line
> > switch or pre-processing directive that permits selecting the
> > implementation at compile time.  What would that directive be?
> One way would be store each implementation in a module in a different
> subdirectory of the source tree and use the `--generate-source-file-mapping'
> ('-f') option to select between them.

That wouldn't solve the problem of having to duplicate the declaration.

There is another possible solution: have a single large definition
in which each clause is tagged with an extra argument that says which
version of the predicate it applies in. Suppose you have these two versions
of a predicate p:

% version 1:
p(a11, b11).
p(a12, b12).

% version 2:
p(a21, b21).
p(a22, b22).

You can represent this in a single definition like this:

:- type version
	--->	version1
	;	version2.

:- pred p(version, arg1type, arg2type).
:- mode p(in(version1), in, out).
:- mode p(in(version2), in, out).

p(version1, a11, b11).
p(version1, a12, b12).

p(version2, a21, b21).
p(version2, a22, b22).

The caller must supply a constant for the version argument, and the two
mode-specific procedures the compiler creates for the two modes will each
correspond exactly to one of the original versions.

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