[mercury-users] Appreciate some help #3:

Fergus Henderson fjh at cs.mu.OZ.AU
Mon May 25 13:30:33 AEST 1998


On 25-May-1998, Thomas Charles CONWAY <conway at cs.mu.OZ.AU> wrote:
> tcklnbrg, you write:
> > How do I define a new type that has several constraints including not
> > being one of several other types?
>
> There isn't a way.

That's correct.  Note that even if there was a way to declare such a type,
it would be difficult for the compiler to verify arbitrary
constraints at compile time.  We have recently discussed the possibilty
of adding support for type invariants, for types such as "sorted list".
However, for types like that, the best you could really hope for given the
current state-of-the-art in type checking is for these additinoal
constraints to be checked at runtime rather than at compile time.

> > In particular, I have defined types: keyword; booleanliteral;
> > nullliteral, etc. and now I need to define another
> > type, identifier, as not a keyword, and not a booleanliteral, and not a
> > null literal , and as a string with constraints on
> > its form.
> > 
> > something like:
> > 
> > :- type keyword ---> <some enumerated words>.
> > :- type booleanliteral ---> true; false.
> > :- type nullliteral ---> null.
> > :- type identifier ---> \+keyword, \+booleanliteral, \+nullliteral, <and
> > some other string-related constraints>.

I'd just define it like this:

	:- type identifier == string.
		% should not be a keyword, boolean literal, or null literal
		% and must satisfy <insert other constraints here>.

> This sounds like tokenizing to me.

Sure does!

> What you might want is a type like:
> 
> :- type token
> 	--->	keyword(keyword)
> 	;	true
> 	;	false
> 	;	null
> 	;	id(string)	% the string is the name of the identifier
> 	.

Yep.  Or, if you want to be a bit more like the code in Ann's original
post:

	:- type token
	--->	keyword(keyword)
	;	booleanliteral(booleanliteral)
	;	null
	;	identifier(identifier).

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the users mailing list