Draft Pitfalls of C# Generics and Their Solution Using Concepts - - PowerPoint PPT Presentation

draft
SMART_READER_LITE
LIVE PREVIEW

Draft Pitfalls of C# Generics and Their Solution Using Concepts - - PowerPoint PPT Presentation

Draft Pitfalls of C# Generics and Their Solution Using Concepts Julia Belyakova, Stanislav Mikhalkovich Institute for Mathematics, Mechanics and Computer Science named after I. I. Vorovich Southern Federal University 28th May, 2015 Draft


slide-1
SLIDE 1

Draft

Pitfalls of C# Generics and Their Solution Using Concepts

Julia Belyakova, Stanislav Mikhalkovich Institute for Mathematics, Mechanics and Computer Science named after I. I. Vorovich Southern Federal University 28th May, 2015
slide-2
SLIDE 2

Draft

Table of Contents

1 Concepts in Generic Programming 2 Concepts for C#: The Goal of Research 3 Pitfalls of C# Generics: Why To Use Concepts 4 Translation of Concepts 5 Conclusion and Future Work
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 2 / 17
slide-3
SLIDE 3

Draft

Concepts in Generic Programming

Mechanisms of Generic Programming

1 Unconstrained C++ Templates. 2 Mechanisms Based on Explicit Constraints:

C# and Java generics, Haskell type classes, ML signatures, Scala traits, etc.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 3 / 17
slide-4
SLIDE 4

Draft

Concepts in Generic Programming

Mechanisms of Generic Programming

1 Unconstrained C++ Templates.

flexibility; expressiveness; late stage of error detection; unclear error messages.

2 Mechanisms Based on Explicit Constraints:

C# and Java generics, Haskell type classes, ML signatures, Scala traits, etc.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 3 / 17
slide-5
SLIDE 5

Draft

Concepts in Generic Programming

Mechanisms of Generic Programming

1 Unconstrained C++ Templates.

flexibility; expressiveness; late stage of error detection; unclear error messages.

2 Mechanisms Based on Explicit Constraints:

C# and Java generics, Haskell type classes, ML signatures, Scala traits, etc. early stage of error detection; error messages in terms of constraints; (in most cases) weaker expressiveness.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 3 / 17
slide-6
SLIDE 6

Draft

Concepts in Generic Programming

Explicit-Constraints-Based Mechanisms

How do they difger? 1 Support difgerent kinds of constraints/requirements (function signatures, associated types, same-type constraints, etc.) 2 Provide difgerent features (retroactive modeling, multi-type constraints, constraints-propagation, etc.) Haskell Type Classes (the most powerful) Scala Traits . . . C#/Java Generics (one of the poorest)
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 4 / 17
slide-7
SLIDE 7

Draft

Concepts in Generic Programming

Explicit-Constraints-Based Mechanisms

How do they difger? 1 Support difgerent kinds of constraints/requirements (function signatures, associated types, same-type constraints, etc.) 2 Provide difgerent features (retroactive modeling, multi-type constraints, constraints-propagation, etc.) Haskell Type Classes (the most powerful) Scala Traits . . . C#/Java Generics (one of the poorest) C++ Concepts
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 4 / 17
slide-8
SLIDE 8

Draft

Concepts in Generic Programming

Concepts

A term “concept” comes from the Standard Template Library (STL). C++ Concepts as a new language construct:

are underway in C++ community since 2000 (Bjarne Bjarne, Gabriel Dos Reis, Douglas Gregor, Jaakko J¨ arvi and others); in respect to expressive power are comparable with Haskell type classes; are as efgective as templates; do not sufger from templates diseases; are treated as a possible substitution of unconstrained templates;
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 5 / 17
slide-9
SLIDE 9

Draft

Concepts in Generic Programming

Concepts

A term “concept” comes from the Standard Template Library (STL). C++ Concepts as a new language construct:

are underway in C++ community since 2000 (Bjarne Bjarne, Gabriel Dos Reis, Douglas Gregor, Jaakko J¨ arvi and others); in respect to expressive power are comparable with Haskell type classes; are as efgective as templates; do not sufger from templates diseases; are treated as a possible substitution of unconstrained templates; are still not included in C++.
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 5 / 17
slide-10
SLIDE 10

Draft

Concepts for C#: The Goal of Research

The Goal of This Study

To introduce concepts into C# language to improve current mechanism of generic programming. C# Generics (based on F-bounded polymorphism): constraints on type parameters of generic classes and methods are expressed in terms of interfaces and subtyping. Concepts can be used with interfaces simultaneously.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 6 / 17
slide-11
SLIDE 11

Draft

Concepts for C#: The Goal of Research

Why C#?

There are two aspects:

1 A Design. The design of concepts proposed is

applicable to C#, Java and any .NET language with GP mechanism based on F-bounded polymorphism.

2 An Implementation. The method of concepts

translation is strongly oriented to .NET Framework. C# is suitable both for syntax demonstration and implementation.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 7 / 17
slide-12
SLIDE 12

Draft

Pitfalls of C# Generics: Why To Use Concepts

Concept Sample I

Concept represents some abstraction; deҥnes a named set of requirements on type parameters. Monoid example concept CMonoid[T] { T binOp(T x, T y); T ident; } s t a t i c T Accumulate <T>(T[] values) where CMonoid[T] using cM { T result = cM.ident; foreach (T val in values) result = cM.binOp(result , val ); return result; }
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 8 / 17
slide-13
SLIDE 13

Draft

Pitfalls of C# Generics: Why To Use Concepts

Concept Sample II

Monoid example in C# i n t e r f a c e IMonoid <T> where T : IMonoid <T> { T binOp(T other ); } s t a t i c T Accumulate <T>( T[] values , T ident ) where T : IMonoid <T> { T result = ident; foreach (T val in values) result = result.binOp(val ); return result; }
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 9 / 17
slide-14
SLIDE 14

Draft

Pitfalls of C# Generics: Why To Use Concepts

C# Pitfalls

lack of retroactive interface implementation; recursive constraints; constraints-compatibility problem; multi-type constraints problem; constraints duplication; verbose type parameters.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 10 / 17
slide-15
SLIDE 15

Draft

Pitfalls of C# Generics: Why To Use Concepts

Constraints Compatibility Problem

Generics c l a s s HashSet <T> (IEqualityComparer <T>) s t a t i c HashSet <T> GetUnion <T>( HashSet <T> s1 , HashSet <T> s2 ){ var us = new HashSet <T>( s1 , s1.Comparer ); us.UnionWith(s2); return us; } // GetUnion(s1 , s2) // != GetUnion(s2 , s1) Generics with Concepts c l a s s HashSet <T> where CEqualityComparable [T] s t a t i c HashSet <T> GetUnion <T>( HashSet <T> s1 , HashSet <T> s2 ){ var us = new HashSet <T>( s1 ); us.UnionWith(s2); return us; } // GetUnion(s1 , s2) // == GetUnion(s2 , s1)
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 11 / 17
slide-16
SLIDE 16

Draft

Translation of Concepts

The Sketch of Translation

Owing to the properties of the .NET Framework:

1 A resultant code of translation is generic. 2 Meta-information is preserved via attributes. Concept — abstract generic class. Type parameters and nested concept requirements — type parameters of this generic class. Generic class — generic class with extra type parameters for concept requirements. Model — class, a subtype of the corresponding abstract generic class of concept. Instantiation of generic class — instantiation of the corresponding generic class with extra type parameters.
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 12 / 17
slide-17
SLIDE 17

Draft

Translation of Concepts

The Advantages of Translation

1 Lowering the run-time expenses due to passing

concepts as types (in contrast to G concepts [4] and Scala “concept pattern” [3]).

2 Modularity can be provided due to preserving full type

information and meta-information.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 13 / 17
slide-18
SLIDE 18

Draft

Conclusion and Future Work

Comparison of “Concepts” Designs Under Garcia et. al. [1]

Feature G C++ C#ext JGI Scl C#cpt multi-type constraints + + ±1 + +2 + associated types + + + – + + same-type constraints + + + – + + subtype constraints – – + + + + retroactive modeling + + ±1 + +3 + multiple models + – ±1 – + + anonymous models – – – – +3 + concept-based overloading + + – – ±4 constraints-compatibility + + – + – + “C#ext” means C# with associated types [2]. “Scl” means Scala [3]. “C#ext” means C# with concepts. 1partially supported via “concept pattern” 2supported via “concept pattern” 3supported via “concept pattern” and implicits 4partially supported by prioritized overlapping implicits
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 14 / 17
slide-19
SLIDE 19

Draft

Conclusion and Future Work

Future Work

Formalization of translation. Implementation of C# compiler for restricted language. Concept syntax “approbation”.

  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 15 / 17
slide-20
SLIDE 20

Draft

References

References

[1] Ronald Garcia, Jaakko Jarvi, Andrew Lumsdaine, Jeremy Siek, and Jeremiah Willcock. An Extended Comparative Study of Language Support for Generic Programming.
  • J. Funct. Program., 17(2):145–205, March 2007.
[2] Jaakko J¨ arvi, Jeremiah Willcock, and Andrew Lumsdaine. Associated Types and Constraint Propagation for Mainstream Object-oriented Generics. In Proceedings of the 20th Annual ACM SIGPLAN Conference on Object-oriented Programming, Systems, Languages, and Applications, OOPSLA ’05, pages 1–19, New York, NY, USA, 2005. ACM. [3] Bruno C.d.S. Oliveira, Adriaan Moors, and Martin Odersky. Type Classes As Objects and Implicits. In Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications, OOPSLA ’10, pages 341–360, New York, NY, USA,
  • 2010. ACM.
[4] Jeremy G. Siek. A Language for Generic Programming. PhD thesis, Indianapolis, IN, USA, 2005. AAI3183499.
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 16 / 17
slide-21
SLIDE 21

Draft

Appendix

Monoid Concept Translation

Monoid Concept concept CMonoid[T] { T binOp(T x, T y); T ident; } abstract c l a s s CMonoid <T> { p u b l i c abstract T binOp(T x, T y); p u b l i c abstract T ident { get; }; } Generic Method s t a t i c T Accumulate <T>( T[] values ) where CMonoid[T] { ... } s t a t i c T Accumulate <T, CMonoid_T >( T[] values ) where CMonoid_T : CMonoid <T> { ... }
  • J. Belyakova, S. Mikhalkovich (SFEDU)
C# Concepts 28th May, 2015 17 / 17