[mercury-users] Working with exported types

Jonathan Morgan jonmmorgan at gmail.com
Thu Jul 27 16:40:14 AEST 2006


On 7/27/06, Peter Schachte <schachte at csse.unimelb.edu.au> wrote:
> On Thu, Jul 27, 2006 at 03:14:40PM +1000, Jonathan Morgan wrote:
> > Say I have an enum something like the following.
> >
> > :- type my_enum --->
> >    value1 ;
> >    value2 ;
> >    value3.
> >
> > If I am working with such a type in foreign code there are a couple of
> > ways I could do it.
> >
> > 1. Assume that value1, value2 and value3 map to the values 0, 1 and 2,
> > and then just test against or assign these values.  Efficient, but may
> > be broken by change in representation.
> >
> > 2. Export Mercury predicates that work on this type, and use these
> > exported predicate exclusively.
>
> If you're only worried about enums, how about writing a little mercury
> program to read the mercury source file and generate a C file
> containing an enum declaration like
>
>         enum my_enum { value1, value2, value3 };
>
> or maybe
>
>         typedef enum { value1, value2, value3 } my_enum;
>
> Then use a little Makefile magic to keep this file up to date when you
> change your mercury source file.
>
> Better still:  build this into the compiler, which knows for sure
> which values are given to the enum constants.

I'm not just worried about enums - however, they are the neatest
example to use.  This example would be workable, under the assumption
that it does start at 0 in both C and Mercury, which seems to be
correct at the moment.  I presume that you would have to write wrapper
functions of some sort for other user-defined types, unless you knew
more about the Mercury implementation than was good for your code.
The reference manual warns that with all of these things the
implementation may change.

Jon
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list