[mercury-users] problem with types

Tyson Dowd trd at cs.mu.OZ.AU
Tue Sep 15 23:30:38 AEST 1998


On 14-Sep-1998, ingenuit <at at ingenuity-sw.com> wrote:
> I need to create a Mercury type for the operators
> of another computer language, Java.  The Java operators
> are just different enough that they conflict with Mercury's
> parsing of such.
> 
> :- type javaOperator
> 	--->
> 	{;}	%this succeeds
> 	; {<<}	% these and most of the others fail
> 	;"+=".

Not sure what you mean by fail (I don't think you mean fail as in
not(succeed)).  You can't use "+=" like that.

Here's a working version.


:- module java.

:- interface.

:- type javaOperator 
	--->	{ ; }  
	;	{ << }
	;	{ += }.

:- import_module io.

:- pred main(io__state::di, io__state::uo) is det.

:- implementation.

main -->
	{ Operator = (<<) },
	io__write(Operator),
	{ Operator2 = (+=) },
	io__write(Operator2).

Notice that you need to put parentheses around the operators,
because they sometimes have a higher precedence than operators such
as "=" and "{ ... }", and so will make it difficult to figure out
whether you mean
	<<(Operator,=)
or
	=(Operator,<<)

> 
> I'd like to use something close to the original java
> operators, but even this fails:
> 
> :- type operator(string).
> 
> :- type javaOperator
> 	--->
> 	operator(";")	%fails
> 	;operator("<<").
> 
> .....

You can't define a type with constant strings like that.  You also
can't say things like :- type foo ---> foo(87) ; bar(21).

The closest you can get is
	:- type javaOperator == string.

Where you use ";" "<<" etc as the strings.  But this isn't a good idea
because you won't get nice switches, and strings are an inefficient
data representation in this case.

It would be cool if you could have a subtype of strings, so you could
say that javaOperator is a string, but it can only have values
";" "<<", etc, etc.  But this isn't possible in Mercury (it causes
a lot of problems if you try to add it to the system).

> 
> io__write("<<"), % this succeeds, however.   why???
> 

In this case, you are writing the value "<<", which is of type string.

> ....
> 
> 
> If I have to put abstract names on the java tokens in the
> incoming java token stream it will reduce readability, increase
> programing effort and reduce maintainability.
> 
> Is there another way to "escape" the java operators sufficiently
> to use them as types?

Using (operator) in code, { operator } in type definitions and forgetting
about "operator" should help.

(It would be nice to have a sample program to show people how this
works and put it in the "samples" directory...)

-- 
Those who would give up essential liberty to purchase a little temporary
safety deserve neither liberty nor safety.     - Benjamin Franklin

Tyson Dowd   <tyson at tyse.net>   http://tyse.net



More information about the users mailing list