# [mercury-users] Tagged datatypes and boxing

Peter Schachte schachte at cs.mu.OZ.AU
Fri Sep 24 14:12:52 AEST 1999

```On Fri, Sep 24, 1999 at 11:21:52AM +1200, Richard A. O'Keefe wrote:
> This is one of the things Pascal (and better still, Ada) got RIGHT:
> if you want integers of a particular size, state the bounds in a
> *human* oriented way.

Agreed completely.

>  What's a good way?  Well, assertions.
>
> type nat8 = X:integer where 0 <= X & X <= 255.
> type odd8 = X:integer where 0 < X & X < 512 & X mod 2 =:= 1.
>
> (Yep.  odd8 values can be fitted quite nicely into 8 bits.)

What predicates/functions are you allowed to use in these constraints?
This seems a little too open-ended to me.  How about:

type smallprime = X:integer where 0 < X & X < 1024 & prime(X).

or even just

type inscrutable = X:integer where 0 < X & X < 512 & X mod 2 =< 1.

I think just handling good ol' subranges would be a big improvement.
Maybe something like:

:- type nat8 = 0..255.

If you want to get fancy you could introduce something like

:- type odd8 = 1..511 by 2. % or maybe "step 2"

I'm not sure that's worth the trouble, though, as putting in support
for this would be a bit complicated.  Either you'd have to convert
these things to proper ints when loading into registers and convert
them back when storing, or you'd have to play all sorts of tricks in
implementing arithmetic.  Doable, but the benefit seems small compared
to the cost.  Eg, you can't just add 3 odd8's, you have to add 1 to
the result.  In fact, what should you get when you add 2 odd8's?  An
even9?

> I'm sick of "C envy".

I think that should be written "C-ness envy."

--
Peter Schachte                     Lisa, if you don't like your job you
mailto:schachte at cs.mu.OZ.AU        don't strike. You just go in every day
http://www.cs.mu.oz.au/~schachte/  and do it really half-assed. That's the
PGP: finger schachte at 128.250.37.3  American way. -- Homer Simpson
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au