[mercury-users] Re: integer enumerations

Peter Schachte schachte at cs.mu.OZ.AU
Wed Jul 26 13:44:38 AEST 2000


On Wed, Jul 26, 2000 at 01:03:51PM +1000, Fergus Henderson wrote:
> On 26-Jul-2000, Michael Day <mcda at students.cs.mu.OZ.AU> wrote:
> > Alternatively, how about a pragma hack for discriminated union types for
> > easy interfacing with C libraries...
> > 
> > :- type some_c_enum
> > 	--->	foo	= some hack to set value to 1
> > 	;	bar	= some hack to set value to 2
> > 	;	baz	= some hack to set value to 17
> > 	.

> For enumerations where the enumeration values have been explicitly specified
> in the C code, it's probably best not to map them to Mercury enums.
> Instead, you can map them to `int' plus a set of constants in Mercury,
> e.g.
> 
> 	:- type some_c_enum == int.
> 
> 	:- func foo = some_c_enum.
> 	:- func bar = some_c_enum.
> 	:- func baz = some_c_enum.
> 	foo = 1.
> 	bar = 2.
> 	baz = 17.

This doesn't look too strong on info hiding.  How about:

	:- module someenum.

	:- interface.

	:- type some_c_enum.

	:- func foo = some_c_enum.
	:- func bar = some_c_enum.
	:- func baz = some_c_enum.


	:- implementation.

	:- type some_c_enum ---> some_c_enum(int).

	foo = some_c_enum(1).
	bar = some_c_enum(2).
	baz = some_c_enum(17).
	
Unfortunately, you can't help but leak the fact that you haven't implemented
this type as an enumeration, but I guess that's not too bad.

The real weakness of this approach (to dredge up an old discussion) is that
there's no way to convince the compiler that foo, bar, and baz are exclusive
and exhaustive, so you're not going to get determinism where you expect to.


> One difficulty, though, is that there's a fundamental incompatibility
> between Mercury enumerations and C enumerations: C allows enumerations
> to have values which are not members of the enumeration.  For example,
> if you have
> 
> 	enum some_c_enum { foo = 1, bar = 2, baz = 17 };
> 
> then according to the C standard `foo | bar' and `31' are both valid
> values of the type `enum some_c_enum'.

Ok, so foo, bar, and baz are exclusive, but not exhaustive.


-- 
Peter Schachte <schachte at cs.mu.OZ.AU>  Politicians are the same all over.
http://www.cs.mu.oz.au/~schachte/      They promise to build a bridge even
Phone:  +61 3 8344 9166                where there is no river.
Fax:    +61 3 9348 1184                    -- Nikita Krushchev 
--------------------------------------------------------------------------
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