# Special Objects

The term model is usually restricted to compounds, atoms and numbers. We extend the term model by reference objects, small floats and decimals. The reference objects can then partic-ipate in the proof search and can be used to wrap objects from the application. Among the possible uses we find resource access and synchronization primitives. The small floats and decimals give better support to some scientific and financial calculations.

The ISO Prolog core standard allows the introduction of additional data types in a Prolog system in non-strict mode. The standard requires that the syntax and behaviour of the data types is defined. Compared to the ISO Prolog core standard we have introduced three new data types with the following corresponding syntax:

• Small Floats: Small floats are internally represented as the Java Float. The syntax for small floats is a float with a prepended small float indicator (“0f”). Examples are: 3.14 is a float, 0f3.14 is a small float.
• Decimal: Decimals are internally represented as the Java class BigDecimal or Long. The syntax for decimals is a float with a prepended decimal indicator (“0d”). Examples: 3.14 is a float, 0d3.14 is a decimal.
• Reference: References are internally represented as a Java object of any class. The syntax for references is a hexadecimal integer with a prepended reference indicator (“0r”). Examples: 254 is an integer, 0rFE is a reference.

The implementation of small floats and decimals differs from the implementation of refer-ences in that small floats and decimals can always be ordered and in that decimals can be parsed. Otherwise the implementation was done along similar lines as for references. For the identity test the equals() method of the Java Float, Java Long and Java BigDecimal was directly used. This method is sensitive to the scale of the BigDecimal. Thus small floats and decimals give a slightly different identity test as one might expect from floats. This can be seen here:

?- 3.14 = 3.1400.
Yes
?- 0f3 = 3.0.
No
?- 0d3.14 = 0d3.1400.
No

The reference data type is a replacement for the various pointers found in the ISO Prolog core standard such as file handles. Since the ISO Prolog core standard pointers are memory addresses and thus numbers they are typically orderable. We lose this property for our reference data types in general in favour of the safer Java memory model. Also we do not make any assumptions whether the Java object is part of a name space. The consequence here is that our reference data objects cannot be currently read in from a file or from the user input.

A great deal of work for the small floats and decimals had to be put into the arithmetic opera-tions. Arithmetic operations are not available for references but they are available for small floats and decimals. The basic arithmetic operations and the rounding operations could be extended to small floats and decimals. Small floats and decimals can also participate in trigonometric operations but they are simply narrowed up or down to floats.

Although arithmetic comparison between small floats and floats is supported, the result might look counter intuitive since due to the smaller precision equality between converted data and the original data might not hold. This can be seen here:

?- 0f3 =:= 3.0.
Yes
?- 0f3.14 =:= 3.14.
No

What concerns the arithmetic comparison of decimals, contrary to the equality it is not sensi-tive to the scale anymore since the compareTo() method of the Java class BigDecimal is used. This can be seen here:

?- 3.14 =:= 3.1400.
Yes
?- 0d3.14 =:= 0d3.1400.
Yes

A comparison of the functionality introduced by the new data types can be found below. As can be seen above the reference objects share many properties with the other data types of the Prolog standard. Most of the properties automatically result because the reference objects implement equal(), hashCode() and compareTo():

Table 1: Comparison Jekejeke and ISO Data Types
Jekejeke ISO w r a u t < = c f + \ s
Variable Variable x x x x x x x x x

Integer Integer x x x x x x x x x x x
Small Float
n/a x x x x x x x x x x
Float Float x x x x x x x x x x
x
Decimal n/a x x x x x x x x x x

Object n/a x
x x x
x x x

Comparable
n/a x
x x x x
x x x

Atom Atom x x x x x x x x x

Compound Compound x x x x x x x x x

Legend:
w = write; r = read; a = atomic, number, etc..; u = unify, etc..;
t = term_hash; < = <, @<, etc..; = = ==, =:=, etc..;
c = assert, clause, etc..; f = functor, =.., etc..; + = +, div, etc..;
\ = /\, etc..; s = sin, cos, etc..

Reference objects implement these methods by delegating to the Java object. They already allow reference objects to participate in unification, clause indexing, term equality and term hashes. Reference objects can also participate in lexical comparisons when they implement the comparable interface.

It is also possible to use copied terms that use reference objects and to even assert clauses that contain the reference objects. What concerns the garbage collection of the reference objects, the implementation is such that we fully rely on the Java runtime system. So when there is no more clause or search pointing to a Prolog reference then also the Java object is not anymore referenced from the Prolog system.  It might still be referenced from elsewhere such as the application, but if there is no such reference it will be eligible for garbage collection.

The new data types are not only available from within the Prolog system. The application programmer can also work with references and decimals. Small floats and decimals can be accessed directly as sub classes from the Java Number class via the application programming interface. Reference data types can also be accessed directly via their original Java class. Foreign predicates that have small floats, decimals or references as arguments are also possible. Otherwise no major changes to the application programming interface were necessary.