[mercury-users] Tagged datatypes and boxing

Peter Schachte schachte at cs.mu.OZ.AU
Sat Sep 25 01:22:20 AEST 1999


> > [...] Another possibility is to allow some 
> > kinds of "pragma"
> > to suggest to the compiler how a type is best represented.  eg:
> > 
> > type color = <integer range/RGB triple/...> pragma packed.
> 
> I would be quite happy to see a facility like this.

You can have it ... almost.  All you have to do is declare the type
abstractly, define it as = int or something like that, and then define
functions to act as constructors, each with 2 modes.  E.g.:

	:- interface.
	:- type color.
	:- func color(int, int, int) = color.
	:- mode color( in,  in,  in) = out is det.
	:- mode color(out, out, out) =  in is det.

	:- implementation.

	% I'd like to write something like
	color(R::in,G::in,B::in) =
		(R /\ 255) + ((G /\ 255) << 8) + ((B /\ 255) << 16).
	color(R::out,G::out,B::out) = C :-
		R = (C      ) /\ 255,
		G = (C >>  8) /\ 255,
		B = (C >> 16) /\ 255.
	% But instead I'd have to write pragma c_code.  Maybe someone can see
	% how to write this as reversible Mercury code.

The only two problems I see are:

    1)	This sure looks uglier than

	:- type color ---> color(0..255,0..255,0..255).
	:- pragma representation color = ....	% I'm too lazy to
						% propose something

	That could be fixed by a preprocessor or an extra compiler
	pass.

    2)	If you have more than 1 constructor for your type, you can't
	tell Mercury that your pseudo-constructor functions are
	mutually exclusive and exhaustive.  Hmmmm.  Does this sound
	familiar? :-)

-- 
Peter Schachte                     Any Idiot can face a crisis; it is this
mailto:schachte at cs.mu.OZ.AU        day-to-day living that wears you out.
http://www.cs.mu.oz.au/~schachte/      -- Anton Chekhov 
PGP: finger schachte at 128.250.37.3  
--------------------------------------------------------------------------
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