• Ingen resultater fundet

Built–in Types

2.3 Types in RSL

2.3.1 Built–in Types

Ids O = new Ids( ) ;

Ids is the translation of ids.

Objects declared with other class expressions than scheme instantiations:

O : ce

Is translated as:

c l a s s I d {/∗T r a n s l a t i o n o f ce∗/}

I d O = new I d ( ) ;

Id is a created identifier from id.

Using Primitive Types and Library Classes in Java

The first advantage of this suggestion is that value literals in RSL can be translated as value literals in Java, thereby avoiding having to create an ob-ject for each value literal in RSL. The second advantage is that expressions using the standard arithmetic operators can be translated using infix expres-sions in Java, rather than method invocations. The reason for this is that the built–in operators in Java cannot be overloaded, and therefore the only expressions allowed to use them are those where the operands are of primitive types. Both these advantages makes it easier to recognize the specification in the translation. A third important advantage of using primitive types for representing the built–in types of RSL is an efficiency issue. It is much more efficient to represent a numerical value in Java using a primitive type than an instance of a class.

The main disadvantage of this solution is that the types for representing numerical values in Java are limited in their size, whereas the numerical types of RSL are infinite, therefore some mechanism must be provided for handling the limit of a type in Java.

The reason that the suggestion is a combination of both primitive types and library classes in Java is that the primitive types cannot be used in collections in Java. Therefore, the translation needs to use the wrapper classes of the primitives types, when dealing with collections in Java.

Using a Collection of Classes Implemented

The main advantage of this suggestion is that each of the types in RSL are only represented as one type in Java and not as a combination of a primitive type and a wrapper class. Another advantage is that the problem of representing infinitely large values of RSL can be handled inside the classes implemented. It should be noted that it is not possible to represent values of infinite size or precision on a machine with a limited storage.

The main disadvantage is that it is not possible to overload the stan-dard operators in Java, therefore all infix operators must be translated as methods rather than operators in Java, which makes it harder to recognize a specification in the translation of a specification.

Choice of representation of built–in types of RSL

Both of the presented suggestions have advantages and disadvantages. In this work the first suggestion have been chosen, because it is the most intuitive solution. There are several possible solutions to the problem with the sizes of the types in Java.

1. Let the user decide how to translate each of the built–in types.

2. Let the user decide the translation of each use of a built–in type via meta data in the specification.

3. Use the widest type to reduce the number of times a limit causes trou-bles.

4. Decide on a type representation in Java, which suits most situations.

The first and third of the four solutions have a problem in case of selecting a large type like BigInteger for representing Int and then having a for–loop construction with values 1 to 5. In this case the selection ofBigInteger would be very inefficient, but it may be needed in other parts of a specification.

The second solution, where different types may be used, presents a problem because some type casts may be needed when assigning a value of a wider type to a narrower one. Type casting should only be performed with great care of a programmer since the value being type cast may be too large to fit within the new type. The fourth solution was chosen for this project, but with the requirement, that it should be relatively easy to change the type in the tool developed.

Type Bool

The typeBoolin RSL represents the boolean values,trueorfalse. In Java, there is a type containing the truth values namelyboolean or wrapped as an object Boolean. The primitive value boolean and the wrapper class Boolean in Java were chosen to represent the RSL type Bool.

Two operators have been defined and a number of connectives for values of the typeBool. They are presented in Table 2.1 along with their translation into Java.

RSL Translation into Java b1 = b2 b1 == b2 orb1.equals(b2)

b16= b2 b1 != b2 or !b1.equals(b2)

∼b1 !b1

b1∧ b2 b1 && b2

b1∨ b2 b1 || b2

b1⇒ b2 if(b1) b2else true

Table 2.1: Operators and connectives for typeBool

The use of == or equals and != or !equals depends on whether the type of the operands involved are of the wrapper class, Boolean, or the primitive typeboolean. == and!=are defined for both objects and the primitive types.

It is therefore necessary to determine, whether to use the operators or the methods. The operators == and != for objects determine whether or not it is the same object, and not whether or not the value of the object is the same. For the connectives this is not a problem since these are not defined for objects and Java can therefore automatically unwrap the objects before using the operator.

Type Int

The type Int in RSL represents the type containing integer values in the range . . . , -2, -1, 0, 1, 2, . . . . In Java, there are several types for repre-senting integer values depending on the range needed. The Java types for representing integer values are shown in Table 2.2 along with their ranges.

Type Range

byte -128 to 127

short -32,768 to 32,767

int -2,147,483,648 to 2,147,438,647

long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 BigInteger no range a

a No limit but in practice limited by the size of the memory of the machine.

Table 2.2: Overview of Java types for representing integer values.

This project will use int and the wrapper class Integer for representing the RSL typeInt. A number of operators have been defined for values of the RSL type Int. These are shown in Table 2.3 along with their translation in Java.

The choice of whether to use == orequals as well as their negated coun-terparts is the same as for type Bool.

The integer division in RSL returns the whole number of times the first operand goes into the second. The same is true for the division operator/in Java. The integer remainder operator\ in RSL handles negative values the same way the Java modulo operator% does, by returning the absolute value of the remainder with the sign of the first operand.

RSL Translation in Java i = j i == j or i .equals(j )

i 6= j i != j or ! i .equals( j )

i >j i >j

i ≥j i >= j

i <j i <j

i ≤j i <= j

i + j i + j

i −j i j

i ∗j i j

i /j i / j

i \j i % j

i ↑j (int) Math.pow(i, j)

reali (double)i

Table 2.3: Operators for the type Int

Type Nat

The type Nat in RSL represents the type containing the natural numbers, which are the same as the values in Int, except that Nat contains only the positive values and zero. The operators for Nat are exactly the same as for Int. The Nat type in RSL is actually a subtype of Int, and the handling of subtypes is discussed in Section 2.3.4.

Type Real

The type Real in RSL represents all the real numbers and may have values like . . . , -2,3, 0.0, 5.7, . . . and with an infinite number of decimals i.e. infinite precission. In Java real numbers are represented by a number of types. Each of them are shown in Table 2.4 along with their ranges.

Type Range

float 3.8×1038 to 3.8×1038 double 1.7×10308 to 1.7×10308 BigDecimal no range a

a

Based on the BigInteger and has the same limitations.

Table 2.4: Overview of Java types for representing real values.

The primitive type double and the wrapper class Double will be used to represent the RSL typeRealin this project. The operators on values of the

type Real are the same as forInt. The only exception is that the ↑ returns a Realinstead of an Int and the translation is therefore Math.pow(i, j). The standard arithmetic operators and relational operators in Java are overloaded and also works for values of type double, therefore the translations are the same as for Int. The conversion operator real is only defined for type Int, and therefore a translation is not defined. The last built–in operator defined for Real is an operator for converting a Real to an Int. This operator is called int and like the operator real it is translated as type casting: (int) r

where r is of type double. Both the RSL operatorint and type casting (int)

returns the integer closest towards zero, i.e. truncation.

Type Char

The typeCharrepresents individual characters in RSL. In Java, there is also a type for representing single characters namely char. Therefore, the trans-lation of type Char in RSL is char and Character in Java. The translation of the two operators for equality and inequality is the same as for the other built–in types described. No other operators have been defined for the RSL type Char. The character set allowed in RSL contains the English alphabet, digits and a number of graphics characters. The graphical characters in RSL are not represented in the same way as in Java, therefore a translation be-tween the two character sets is needed. The treatment of special characters in RSL are not considered in this work, but it would only present a minor extension.

Type Text

The type Text in RSL represents strings of characters. In Java, there is also a type for representing strings of characters and that is the class String.

String is therefore chosen as translation of type Text. In RSL, Text is defined as a list of characters and therefore the standard list operators are also valid for values of typeText. This leads to the idea of translating Text as a list of characters in Java. This would, however, lead to a very inefficient solution since each character would have to be represented by an object, and the possibility of using the standard notation for String in Java would be lost. The standard list operators do not have a direct translation in Java.

They are translated using the methods in the class String.

The standard list operators for Text are translated as shown in Table 2.5.

RSL Translation in Java b String concat(String str )

hd charcharAt(0)

tl String substring(1)

len int length()

elems newRSLSetDefault<Character>(s.toCharArray())a

inds newRSLSetDefault<Integer>.range(1, s.length())a

a For a description of RSLSetDefault please see Section 2.3.2.

Table 2.5: Overview of translations of Textoperations in Java

Type Unit

The typeUnitin RSL represent a single empty value. In RSL, it is often used in the type expression of a function for either representing, that the function has no return value, or that it does not take any arguments. In Java, a method with no return value uses the keyword void. A method in Java that does not have any arguments is just written with empty parentheses after the method identifier. In RSL the two operators for equality and inequality also works for Unit. The use of the equality operator on two expressions evaluating to Unit is true, while use of the inequality operator is false. To obtain the same properties in Java, a special type for representing a single value should be implemented, since values of type void are not allowed in Java.

Translations of the built–in RSL types have been summarized in Table 2.6.

RSL Java

Bool boolean or Boolean

Int int or Integer

Nat No translation.

Real double orDouble

Char char or Character

Text String

Unit No translation. Functions should use the keyword void and ().

Table 2.6: Summary of translation of built–in types in RSL.