combining structural and nominal subtyping

Download Combining Structural and Nominal Subtyping

Post on 25-Feb-2016

52 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Combining Structural and Nominal Subtyping. Donna Malayeri. Our goal: combine the benefits of structural and nominal subtyping. Structural subtyping a type T is a subtype of U if T’s methods are a superset of U’s methods - PowerPoint PPT Presentation

TRANSCRIPT

  • Combining Structural and Nominal SubtypingDonna Malayeri

  • Our goal: combine the benefits of structural and nominal subtypingStructural subtypinga type T is a subtype of U if Ts methods are a superset of Us methodsso, any class with a serialize() method would be a subtype of Serializable

    Nominal subtyping a type T is a subtype of U only if Ts methods are a superset of Us methods and T has been declared as a subtype of U

  • Benefits of structural subtypingStructural subtyping is flexible and compositionalallows unanticipated reuseno unnecessary proliferation of typesuseful for data persistence & distributed computing

    Example: XML

  • An examplejava.util.Collection includes 15 methodsbut 6 of these methods are marked optional: boolean add(E o) boolean addAll(Collection c) boolean retainAll(Collection c) A better solution: put these in a separate interface

  • But this would lead to an explosion of types!ImmutableCollectionMutableCollectionReadWriteCollectionImmutableAbstractListMutableAbstractListReadWriteAbstractListReadWriteArrayList.... and so on

  • Structural subtyping would solve this problemDont need to name all possible combinations of interesting methodsDont need to specify that a class implements a particular interface ahead of timeEasy to later define new subsets of methods that are interestinge.g. Iterable = has method Iterator iterator()

  • More examplesThe classes org.eclipse.swt.widgets.Scrollbar and org.eclipse.swt.widgets.Sliderhave many methods with the same Javadoc:

    boolean isEnabled()void setEnabled(boolean enabled)int getMinimum()int getMaximum()void setMinimum(int value)void setMaximum(int value)int getPageIncrement()void setPageIncrement(int value)int getSelection()void setSelection(int selection)int getThumb()void setThumb(int value)int getIncrement()void setIncrement(int value)void setValues(int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement)

  • Other examplesmany classes have the methodsString getText() void setText(String string)

    ButtonLabelLinkItemTextComboGroup...but theres no IText interface

  • Other examplesmany classes have the methodsvoid setImage(Image image)Image getImage()

    ButtonCaretDecorationsItemLabelMenuItemTabItemTableColumnTableItemTrayItemTreeColumn TreeItemToolItem...but theres no IHasImage interface

  • And, in the JDTall of these classes haveString getElementName()

    IImportDeclarationIJavaElementILocalVariableIMethodIPackageDeclarationIPackageFragmentITypeIField...but theres no IElement interface

  • So, structural subtyping has definite practical uses

  • Nominal subtyping has benefits tooallows programmers to explicitly express design intentcan prevent accidental subtypingnecessary for efficient run-time subtyping tests and external/multimethod dispatch

    ExamplesJava classes ML datatypes & dispatch

  • Nominal subtyping in Unityabstract brand Window (...) extends Topconcrete brand Textbox (...) extends Windowconcrete brand StaticText (...) extends Windowconcrete brand ScrollingTextbox (...) extends Textbox

  • Why do we care about external methods?

  • What is an external method?conceptually part of an existing classperforms dispatch on objects of that class typedoesnt have to be in the same compilation unit as the class

    closely related concept: multi-methodsmethod dispatch can depend on any subset of functions arguments

  • External methodsSuppose you want to be able to extend both classes and methods easilyVisitor doesnt solve the problem; adding new classes is hard

    Alternatives to external methodsmanually do a typecase (e.g., instanceof tests)make do with Visitor

  • External methods example

    abstract brand Window (...) extends Topconcrete brand Textbox (...) extends Windowconcrete brand StaticText (...) extends Windowfun paint (t : Textbox) = ...fun paint (t : StaticText) = ...

    concrete brand ScrollingTextbox (...) extends Textbox

    fun paint ( t : ScrollingTextbox) = ...new type + new method

  • Theres lots of existing work on external methodsCecil language specificationMillstein et al (TOPLAS 04)Clifton et al (TOPLAS 06)Lee and Chambers (ECOOP 06)

    Im extending this work

  • Structural subtyping in Unityval win = (title=MyWindow)win : (title : string)

    val textbox = (title=MyTextbox, text=SSSG is fun)textbox : (title : string, text : string)

    val scrollWin = (title=ScrolllingWindow, scroll=myScrollbar)scrollWin : (title : string, scroll: Scrollbar)

    scrollWin win(title:string, scroll:Scrollbar) (title:string)

    textbox win(title:string, text:string) (title:string)

  • Nominal subtyping in Unityabstract brand Window (...) extends Topconcrete brand Textbox (...) extends Windowconcrete brand StaticText (...) extends Windowconcrete brand ScrollingTextbox (...) extends Textbox

  • ...combined with structural subtypingabstract brand Window (title : string) extends Topconcrete brand Textbox(title : string, currentPos : int) extends Windowconcrete brand StaticText(title : string, text : string) extends Windowconcrete brand ScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

  • ...combined with structural subtypingabstract brand Window (title : string) extends Topconcrete brand Textbox(title : string, currentPos : int) extends Windowconcrete brand StaticText(title : string, text : string) extends Windowconcrete brand ScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

  • ...combined with structural subtypingSubtyping relationshipsWindow (title : string, s : Scrollbar) Window (title : string)

    Textbox (...) Window (title : string)ScrollingTextbox (...) Textbox (...)ScrollingTextbox (...) Window (title : string, s : Scrollbar)

    StaticText (...) Window (title : string)StaticText (..., s : Scrollbar) Window (title : string, s : Scrollbar)

    abstract brand Window (title : string) extends Topconcrete brand Textbox(title : string, currentPos : int) extends Windowconcrete brand StaticText(title : string, text : string) extends Windowconcrete brand ScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

  • Writing functionsfixed number of charactersunlimited characters; scrolls if neededeach branch is semantically an external method

  • Comparison to Java UnityJava

  • SummaryUnity supports structural and nominal subtyping in a unified type system

    Future work:extensible brands & external methodspolymorphism/row polymorphismThank you

    this illustrates the declared typing aspect of the system.

    this illustrates the declared typing aspect of the system.

    this illustrates the declared typing aspect of the system.

    this illustrates the declared typing aspect of the system.

    this illustrates the declared typing aspect of the system.

    Recall our running example. now we want to write some code to implement some functionality.I said before that names come with an implicit behavioral specificationin this case, a suppose that a regular textbox accepts only a fixed number of characters, while a scrollingtextbox is unbounded.

    Here we have notionally two methods, but its represented in our calculus by a case analysis. Note that unlike ML-style case analysis, the ordering of branches does not matterthe execution behavior is to pick the single most specific branch.

    This is an example where the by-name aspect of the system is exploited, and the function insertChar does dispatch on the tag of its input value. Scrolling if necessary (perhaps by calling a scroll function). But that scroll function can operate on ANY window, provided it has a scrollbar.- note that in Java, you had to muck around with all these interfaces, just to get the structure of the program working properly. necessarily some code duplication (traits do remove some of this), but not in the relevant places (just for the structure, not hte behavior)

    the important observation here is that java can only do this if you KNOW ahead of time what you even want.even if you dont know ahead of time, you can create clean unity example that is robust to a set of changes.to do: cake

Recommended

View more >