Scala for generic programmers

Datatype-generic programming (DGP) involves parametrization of programs by the shape of data, in the form of type constructors such as ‘list of’. Most approaches to DGP are developed in pure functional programming languages such as Haskell. We argue that the functional object-oriented language Scala is in many ways a better choice. Not only does Scala provide equivalents of all the necessary functional programming features (such as parametric polymorphism, higher-order functions, higher-kinded type operations, and type-and constructor-classes), but it also provides the most useful features of object-oriented languages (such as subtyping, overriding, traditional single inheritance, and multiple inheritance in the form of traits). Common Haskell techniques for DGP can be conveniently replicated in Scala, whereas the extra expressivity provides some important additional benefits in terms of extensibility and reuse. We illustrate this by comparing two simple approaches in Haskell, pointing out their limitations and showing how equivalent approaches in Scala address some of these limitations. Finally, we present three case studies on how to implement in Scala real DGP approaches from the literature: Hinze's ‘Generics for the Masses’, Lammel and Peyton Jones's ‘Scrap your Boilerplate with Class’, and Gibbons's ‘Origami Programming’.

[1]  James Cheney,et al.  A lightweight implementation of generics and dynamics , 2002, Haskell '02.

[2]  Andres Löh,et al.  Exploring generic Haskell , 2004 .

[3]  Simon L. Peyton Jones,et al.  Scrap your boilerplate: a practical design pattern for generic programming , 2003, TLDI '03.

[4]  Conor McBride,et al.  Applicative programming with effects , 2008, J. Funct. Program..

[5]  Bruno C. d. S. Oliveira,et al.  Modular Visitor Components , 2009, ECOOP.

[6]  Roland Carl Backhouse,et al.  Workshop on generic programming , 1998 .

[7]  Bruno C. d. S. Oliveira Modular Visitor Components: A Practical Solution to the Expression Families Problem , 2009, ECOOP 2009.

[8]  Martin Odersky,et al.  Putting type annotations to work , 1996, POPL '96.

[9]  Bruce G. Lindsay,et al.  Static type checking of multi-methods , 1991, OOPSLA '91.

[10]  Lambert G. L. T. Meertens,et al.  Paramorphisms , 2005, Formal Aspects of Computing.

[11]  Martin Odersky,et al.  Scalable component abstractions , 2005, OOPSLA '05.

[12]  Jeremy Gibbons,et al.  Design patterns as higher-order datatype-generic programs , 2006, WGP '06.

[13]  Bruno C. d. S. Oliveira,et al.  "Scrap Your Boilerplate" Reloaded , 2006, FLOPS.

[14]  Ralf Hinze,et al.  Fun with phantom types , 2003 .

[15]  Martin Odersky,et al.  An Overview of the Scala Programming Language , 2004 .

[16]  Bruno C. d. S. Oliveira,et al.  The essence of the Iterator pattern , 2006, J. Funct. Program..

[17]  Patrik Jansson Functional Polytypic Programming , 2000 .

[18]  Manfred Broy Program Design Calculi , 1993, NATO ASI Series.

[19]  William R. Cook,et al.  A denotational semantics of inheritance , 1989 .

[20]  Ralf Hinze,et al.  Generic Programming, Now! , 2006, SSDGP.

[21]  Bruno C. d. S. Oliveira,et al.  Comparing libraries for generic programming in haskell , 2008, Haskell '08.

[22]  A. Dijkstra,et al.  Making Implicit Parameters Explicit , 2005 .

[23]  GibbonsJeremy,et al.  The essence of the iterator pattern , 2009 .

[24]  Bruno C. d. S. Oliveira,et al.  Scala for generic programmers , 2008, WGP '08.

[25]  Philip Wadler Monads for Functional Programming , 1995, Advanced Functional Programming.

[26]  Jeremy Gibbons,et al.  Parametric datatype-genericity , 2009, WGP '09.

[27]  Ralf Hinze Generics for the masses , 2004, ICFP '04.

[28]  Wolfram Kahl,et al.  Named Instances for Haskell Type Classes , 2001 .

[29]  Simon L. Peyton Jones,et al.  Simple unification-based type inference for GADTs , 2006, ICFP '06.

[30]  B. C. Oliveira Objects to Unify Type Classes and GADTs , 2008 .

[31]  Martin Odersky,et al.  Colored local type inference , 2001, POPL '01.

[32]  Martin Odersky,et al.  Generics of a higher kind , 2008, OOPSLA.

[33]  Meng Wang,et al.  Modular generic programming with extensible superclasses , 2006, WGP '06.

[34]  Karl Lieberherr,et al.  Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns , 1995 .

[35]  Martin Odersky,et al.  Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition , 2010 .

[36]  Simon L. Peyton Jones,et al.  Scrap your boilerplate with class: extensible generic functions , 2005, ICFP '05.

[37]  Simon L. Peyton Jones,et al.  Derivable Type Classes , 2001, Haskell.

[38]  Mark Lillibridge,et al.  A type-theoretic approach to higher-order modules with sharing , 1994, POPL '94.

[39]  Alexander A. Stepanov,et al.  Generic Programming , 1988, ISSAC.

[40]  Neil Brown,et al.  Alloy: fast generic transformations for Haskell , 2009, Haskell.

[41]  AgrawalRakesh,et al.  Static type checking of multi-methods , 1991 .

[42]  Ralf Lämmel,et al.  Dealing with large bananas , 2000 .

[43]  Bruno C. d. S. Oliveira,et al.  Extensible and modular generics for the masses , 2006, Trends in Functional Programming.

[44]  Ralf Hinze,et al.  Generic programming in 3D , 2009, Sci. Comput. Program..

[45]  Maarten M. Fokkinga,et al.  Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire , 1991, FPCA.

[46]  Ralf Hinze,et al.  Generic Haskell: Practice and Theory , 2003, Generic Programming.

[47]  Simon L. Peyton Jones,et al.  Scrap Your Boilerplate , 2003, APLAS.

[48]  Martin Odersky,et al.  Type classes as objects and implicits , 2010, OOPSLA.

[49]  Ralf Hinze,et al.  Open data types and open functions , 2006, PPDP '06.

[50]  Bruno C. d. S. Oliveira,et al.  TypeCase: a design pattern for type-indexed functions , 2005, Haskell '05.

[51]  Oscar Nierstrasz,et al.  Traits: Composable Units of Behaviour , 2002, ECOOP.

[52]  Simon L. Peyton Jones,et al.  Template meta-programming for Haskell , 2002, Haskell '02.

[53]  Luca Cardelli,et al.  On Binary Methods , 1995, Theory Pract. Object Syst..

[54]  Oege de Moor,et al.  The Fun of Programming , 2003 .

[55]  Xavier Leroy,et al.  Manifest types, modules, and separate compilation , 1994, POPL '94.

[56]  Johan Jeuring,et al.  Merging Monads and Folds for Functional Programming , 1995, Advanced Functional Programming.

[57]  Bruno César dos Santos Oliveira Genericity, extensibility and type : safety in the VISITOR pattern , 2007 .

[58]  Stephanie Weirich RepLib: a library for derivable type classes , 2006, Haskell '06.

[59]  Colin Runciman,et al.  Uniform boilerplate and list processing , 2007, Haskell '07.

[60]  Martin Odersky,et al.  An Overview of the Scala Programming Language Second Edition , 2006 .

[61]  Ralph Johnson,et al.  design patterns elements of reusable object oriented software , 2019 .

[62]  Hayo Thielecke,et al.  A Type-theoretic Reconstruction of the Visitor Pattern , 2006, MFPS.

[63]  Wouter Joosen,et al.  An object-oriented approach to datatype-generic programming , 2006, WGP '06.

[64]  Ralf Hinze Polytypic Values Possess Polykinded Types , 2000, MPC.

[66]  François Monin,et al.  Principles and Practice of Declarative Programming , 1999, Lecture Notes in Computer Science.

[67]  Apostolos Syropoulos,et al.  Steps in Scala: The expression problem , 2010 .

[68]  Simon L. Peyton Jones,et al.  Type classes in Haskell , 1994, TOPL.