<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Semantic Subtyping for Objects and Classes</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Ornela Dardha</string-name>
          <email>ornela.dardha@gmail.com</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Daniele Gorla</string-name>
          <email>gorla@di.uniroma1.it</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Daniele Varacca</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>PPS - Université Paris Diderot</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>CNRS (France)</string-name>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Dipartimento di Informatica, Sapienza Università di Roma</institution>
          ,
          <country country="IT">Italy</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>There are two approaches for defining subtyping relations: the syntactic and the semantic one. In the semantic approach one starts from a model of the language of interest and an interpretation of types as subsets of the model. The subtyping relation is then defined as inclusion of sets denoting types. An orthogonal issue, typical of object-oriented languages, is the issue of nominal vs. structural subtyping. We aim to integrate structural subtyping with boolean connectives and semantic subtyping for a object-oriented core language and define a Java-like programming platform that exploits the benefits of both approaches, expressible in terms of code reuse and of compactness of program writing.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>A type system for a programming language is a set of deduction rules that allows to derive a
type for the terms of the language. Usually, type systems are based on a subtyping relation
on types. There are two approaches for defining the subtyping relation: the syntactic approach
and the semantic one. The syntactic approach is more common and consists in defining the
subtyping relation by means of a formal system of deductive rules. One proceeds as follows:
first define the language, then the set of syntactic types and finally the subtyping relation by
inference rules. In the semantic approach, instead, one starts from a model of the language
and an interpretation of types as subsets of the model. The subtyping relation is then defined
as inclusion of sets denoting types.</p>
      <p>The semantic approach has received less attention than the syntactic one as it is more
technical and constraining: it is not trivial to define the interpretation of the types as subsets
of a model. On the other hand, it presents several advantages: for instance it allows a
natural definition of boolean operators. Also the meaning of the types is more intuitive for the
programmer.</p>
      <p>
        The first use of the semantic approach goes back to 15 years ago, as in [
        <xref ref-type="bibr" rid="ref1 ref2">1, 2</xref>
        ]. Hosoya
and Pierce have also adopted this approach in [
        <xref ref-type="bibr" rid="ref3 ref4 ref5">3, 4, 5</xref>
        ] to define XDuce, an XML-oriented
language designed specifically to transform XML documents in other XML documents satisfying
certain properties. The values of this language are fragments of XML documents; types are
interpreted as sets of documents, more precisely as sets of values. The subtyping relation is
established as inclusion of these sets. The type-system contains boolean types, product types
and recursive types. There are no function types and no functions in the language.
      </p>
      <p>
        Castagna et al. in [
        <xref ref-type="bibr" rid="ref6 ref7 ref8">6, 7, 8</xref>
        ] extend the XDuce language with first-class functions and arrow
types and define and implement a higher-order language named CDuce adopting the semantic
approach to subtyping. The starting point of their framework is a higher-order calculus with
pairs and projections. The set of types is extended with intersection, union and negation types
interpreted in a set-theoretic way.
      </p>
      <p>
        The semantic approach can also be applied to the calculus [
        <xref ref-type="bibr" rid="ref10 ref9">9, 10</xref>
        ]. Castagna, Varacca
and De Nicola in [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ] have used this technique to define the C language, a variant of the
asynchronous calculus, where channel types are augmented with boolean connectives
interpreted in an obvious way.
      </p>
      <p>
        We aim at applying the semantic subtyping approach to an object-oriented core language.
Our starting point is the language Featherweight Java [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ], that is the functional fragment of
Java. Following the line of [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ], we have defined from scratch the subtyping relation
semantically, giving many important theoretical results. To understand the advantages of the usage
of boolean connectives in an object-oriented language, let us consider the following example:
suppose we are working with polygons as triangles, rectangles, rumbles etc. We want to define
a method that takes a polygon and returns the length of its longest diagonal; of course this can
be done only if the polygon has at least four sides. In Java this problem can be implemented
in di erent ways, for example:
class Polygon f
      </p>
      <p>g
class T riangle extends Polygon f</p>
      <p>g
class Other_Polygons extends Polygon f
:
:
:
real diagonal(Other_Polygons p) f: : :g
:
:
:
g
class Rectangle extends Other_Polygons f</p>
      <p>g
class Rumble extends Other_Polygons f
:
:
:
g
Another way to implement the problem is by means of an interface:
public interface Diagonal f</p>
      <p>real diagonal(Polygon p);
g
class Polygon f</p>
      <p>g
class T riangle extends Polygon f</p>
      <p>g
class Rectangle extends Polygon implements Diagonal f
g
class Rumble extends Polygon implements Diagonal f
:
:
:
g
Let us now suppose that the class hierarchy has Polygon as the parent class and all other
geometric figure classes that extend Polygon. Let us assume that this hierarchy is given and
that it is not possible to change it afterwards. In this scenario it is more di cult to define such a
method. The only way to do it in Java is by defining the method diagonal in the class Polygon
and using an instanceof, for example inside a try-catch that throws an exception at run time
if the argument passed to the method is a triangle.</p>
      <p>With the use of boolean types, instead, the solution to the problem is more elegant: it
is enough to define a method that has an argument of type “Polygon AND NOT Triangle”
(Polygon ^ : T riangle) that permits the typechecker to control at compile time the restrictions
on types. A way to implement the problem using the boolean connectives is as follows:
class Polygon extends Ob ject f
class Diagonal extends Ob ject f</p>
      <p>real diagonal(Polygon ^ :T riangle p)f: : :g
g
At this point, it is enough to invoke the method diagonal on an argument of type Polygon:
if the polygon is not a Triangle, then the method returns the length of the longest diagonal;
otherwise, if the polygon is a triangle, then there will be a type error at compile time.</p>
      <p>
        As an orthogonal issue, typical of object-oriented languages, is the nominal vs. structural
subtyping question. In a language where the subtyping is nominal, A is a subtype of B if
and only if it is declared to be so, that is if the class A extends (or implements) the class
(or interface) B; these relations must be declared by the programmer and are based on the
names of the classes and interfaces concerned. Recently, a new approach has emerged, the
one based on structural subtyping [
        <xref ref-type="bibr" rid="ref13 ref14 ref15">13, 14, 15</xref>
        ]. In this approach, the subtyping relation is
established only by analyzing the structure of a class, i.e. its fields and methods: a class A is a
subtype of a class B if and only if the fields and methods of A are a superset of the fields and
methods of B, and their types in A are subtypes of the types in B. The definition of structural
subtyping as inclusion of sets corresponding to fields and methods of a class brings us back to
the definition of semantic subtyping, to which it fits perfectly. Moreover, with a minor e ort,
it is also possible to include in the framework the choice of using nominal subtyping without
a ecting the theory underlying our framework.
      </p>
      <p>The aim of our work is to integrate structural subtyping with boolean connectives and
semantic subtyping and define a Java-like programming platform that exploits the benefits of
both approaches, expressible in terms of code reuse and of compactness of program writing.
From the theoretical side, this calls for the definition of a set-theoretic model for the types and
the study of the subtyping relation upon this model. So, algorithms for deciding the subtyping
relation are needed and have to be explicitly developed. From the practical side, this calls for
the development of a prototype programming environment where writing and evaluating the
performances of code written in the new formalism.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>Alexander</given-names>
            <surname>Aiken and Edward L. Wimmers</surname>
          </string-name>
          .
          <article-title>Type inclusion constraints and type inference</article-title>
          .
          <source>In FPCA '93: Proceedings of the conference on Functional programming languages and computer architecture</source>
          , pages
          <fpage>31</fpage>
          -
          <lpage>41</lpage>
          , New York, NY, USA,
          <year>1993</year>
          . ACM.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <surname>Flemming</surname>
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Damm</surname>
          </string-name>
          .
          <article-title>Subtyping with union types, intersection types and recursive types</article-title>
          .
          <source>In TACS '94: Proceedings of the International Conference on Theoretical Aspects of Computer Software</source>
          , pages
          <fpage>687</fpage>
          -
          <lpage>706</lpage>
          , London, UK,
          <year>1994</year>
          . Springer-Verlag.
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>Haruo</given-names>
            <surname>Hosoya</surname>
          </string-name>
          and
          <string-name>
            <given-names>Benjamin C.</given-names>
            <surname>Pierce</surname>
          </string-name>
          .
          <article-title>Regular expression pattern matching for xml</article-title>
          .
          <source>SIGPLAN Not</source>
          .,
          <volume>36</volume>
          (
          <issue>3</issue>
          ):
          <fpage>67</fpage>
          -
          <lpage>80</lpage>
          ,
          <year>2001</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>Haruo</given-names>
            <surname>Hosoya</surname>
          </string-name>
          , Jérôme Vouillon, and
          <string-name>
            <surname>Benjamin</surname>
            <given-names>C.</given-names>
          </string-name>
          <string-name>
            <surname>Pierce</surname>
          </string-name>
          .
          <article-title>Regular expression types for xml</article-title>
          .
          <source>ACM Trans. Program. Lang. Syst.</source>
          ,
          <volume>27</volume>
          (
          <issue>1</issue>
          ):
          <fpage>46</fpage>
          -
          <lpage>90</lpage>
          ,
          <year>2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>Haruo</given-names>
            <surname>Hosoya</surname>
          </string-name>
          and
          <string-name>
            <given-names>Benjamin C.</given-names>
            <surname>Pierce</surname>
          </string-name>
          .
          <article-title>Xduce: A statically typed xml processing language</article-title>
          .
          <source>ACM Trans. Internet Technol.</source>
          ,
          <volume>3</volume>
          (
          <issue>2</issue>
          ):
          <fpage>117</fpage>
          -
          <lpage>148</lpage>
          ,
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>Giuseppe</given-names>
            <surname>Castagna</surname>
          </string-name>
          and
          <string-name>
            <given-names>Alain</given-names>
            <surname>Frisch</surname>
          </string-name>
          .
          <article-title>A gentle introduction to semantic subtyping</article-title>
          .
          <source>In PPDP '05: Proceedings of the 7th ACM SIGPLAN international conference on Principles and practice of declarative programming</source>
          , pages
          <fpage>198</fpage>
          -
          <lpage>199</lpage>
          , New York, NY, USA,
          <year>2005</year>
          . ACM.
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>Giuseppe</given-names>
            <surname>Castagna</surname>
          </string-name>
          .
          <article-title>Semantic subtyping: Challenges, perspectives, and open problems</article-title>
          .
          <source>In ICTCS</source>
          , pages
          <fpage>1</fpage>
          -
          <lpage>20</lpage>
          ,
          <year>2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>Alain</given-names>
            <surname>Frisch</surname>
          </string-name>
          , Giuseppe Castagna, and
          <string-name>
            <given-names>Véronique</given-names>
            <surname>Benzaken</surname>
          </string-name>
          .
          <article-title>Semantic subtyping: Dealing set-theoretically with function, union, intersection, and negation types</article-title>
          .
          <source>J. ACM</source>
          ,
          <volume>55</volume>
          (
          <issue>4</issue>
          ):
          <fpage>1</fpage>
          -
          <lpage>64</lpage>
          ,
          <year>2008</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>Robin</given-names>
            <surname>Milner</surname>
          </string-name>
          .
          <article-title>Communicating and mobile systems: the sity Press</article-title>
          , New York, NY, USA,
          <year>1999</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>Davide</given-names>
            <surname>Sangiorgi</surname>
          </string-name>
          and
          <string-name>
            <given-names>David</given-names>
            <surname>Walker</surname>
          </string-name>
          .
          <article-title>The Pi-Calculus: A Theory of Mobile Processes</article-title>
          . Cambridge University Press, New York, NY, USA,
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [11]
          <string-name>
            <surname>Giuseppe</surname>
            <given-names>Castagna</given-names>
          </string-name>
          , Rocco De Nicola, and
          <string-name>
            <given-names>Daniele</given-names>
            <surname>Varacca</surname>
          </string-name>
          .
          <article-title>Semantic subtyping for the pi-calculus</article-title>
          .
          <source>Theor. Comput. Sci.</source>
          ,
          <volume>398</volume>
          (
          <issue>1-3</issue>
          ):
          <fpage>217</fpage>
          -
          <lpage>242</lpage>
          ,
          <year>2008</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [12]
          <string-name>
            <surname>Atsushi</surname>
            <given-names>Igarashi</given-names>
          </string-name>
          , Benjamin C. Pierce, and
          <string-name>
            <given-names>Philip</given-names>
            <surname>Wadler</surname>
          </string-name>
          .
          <article-title>Featherweight java: a minimal core calculus for java and gj</article-title>
          .
          <source>ACM Trans. Program. Lang. Syst.</source>
          ,
          <volume>23</volume>
          (
          <issue>3</issue>
          ):
          <fpage>396</fpage>
          -
          <lpage>450</lpage>
          ,
          <year>2001</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>Joseph</given-names>
            <surname>Gil</surname>
          </string-name>
          and
          <string-name>
            <given-names>Itay</given-names>
            <surname>Maman</surname>
          </string-name>
          .
          <article-title>Whiteoak: introducing structural typing into java</article-title>
          .
          <source>In OOPSLA '08: Proceedings of the 23rd ACM SIGPLAN conference on Object-oriented programming systems languages and applications</source>
          , pages
          <fpage>73</fpage>
          -
          <lpage>90</lpage>
          , New York, NY, USA,
          <year>2008</year>
          . ACM.
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          [14]
          <string-name>
            <given-names>Donna</given-names>
            <surname>Malayeri</surname>
          </string-name>
          and
          <string-name>
            <given-names>Jonathan</given-names>
            <surname>Aldrich</surname>
          </string-name>
          .
          <article-title>Integrating nominal and structural subtyping</article-title>
          .
          <source>In ECOOP '08: Proceedings of the 22nd European conference on Object-Oriented Programming</source>
          , pages
          <fpage>260</fpage>
          -
          <lpage>284</lpage>
          , Berlin, Heidelberg,
          <year>2008</year>
          . Springer-Verlag.
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          [15]
          <string-name>
            <given-names>Donna</given-names>
            <surname>Malayeri</surname>
          </string-name>
          and
          <string-name>
            <given-names>Jonathan</given-names>
            <surname>Aldrich</surname>
          </string-name>
          .
          <article-title>Is structural subtyping useful? an empirical study</article-title>
          .
          <source>In ESOP</source>
          , pages
          <fpage>95</fpage>
          -
          <lpage>111</lpage>
          ,
          <year>2009</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>