# [mercury-users] Tagged datatypes and boxing

Richard A. O'Keefe ok at hermes.otago.ac.nz
Mon Sep 27 12:56:49 AEST 1999

```I suggested that subranges could be handled by assertions.

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

Open-endedness is precisely what I want at this stage in the discussion.
The whole point is that I don't *know* what everyone will find appropriate
for constraining variables.

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

Yes, why not?

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.

But that is a special purpose syntactic hack that locks out future
improvements.  Primality may be going a bit far, but I _have_ wanted
divisibility (m divides X) and other constraints as well as just
subranges.  It isn't necessary that every possible constraint be
supported to begin with; what is necessary is that potentially
sensible constraints should not arbitrarily be ruled out.

There have been, and still are, programming languages in which
multiple comparisons (like 0 =< X =< 255) are expressible and
work correctly.  I can see no reason why Mercury should not be
one of them.  Make just two changes:

E0 R1 E1 ... Rn En
if and only if v0 R1 v1, v1 R2 v2, ..., vn_1 Rn vn
where v0 ... vn are the values of E0 ... En

(2) Adopt a syntax vaguely like
:- TypeName subtype (Var : Name :- Comparison)
where Comparison is a numeric comparison

Then you have

:- integer subtype (X: nat8 :- 0 =< X < 256).

and there is syntactic "room" to allow more general constraints,
such as

:- list(T) subtype (X: nonempty_list(T) :- X \== []).