Skip to main content Accessibility help
×
Home

Scala for generic programmers: Comparing Haskell and Scala support for generic programming

  • BRUNO C. D. S. OLIVEIRA (a1) and JEREMY GIBBONS (a2)

Abstract

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’, Lämmel and Peyton Jones's ‘Scrap your Boilerplate with Class’, and Gibbons's ‘Origami Programming’.

Copyright

References

Hide All
Agrawal, R., Demichiel, L. G. & Lindsay, B. G. (1991) Static type checking of multi-methods. In Proceedings of Object Oriented Programming Systems Languages and Applications. Phoenix, AZ, USA, pp. 113128.
Brown, N. C. C. & Sampson, A. T. (2009) Alloy: Fast generic transformations for Haskell. In Haskell Symposium. Edinburgh, pp. 105116.
Bruce, K., Cardelli, L., Castagna, G., The Hopkins Object Group, Leavens, G. T. & Pierce B. (1995) On binary methods, Theory Pract. Object Syst., 1 (3): 221242.
Buchlovsky, P. & Thielecke, H. (2006) A type-theoretic reconstruction of the Visitor pattern, Electron. Notes Theor. Comput. Sci., 155, 309329 (Mathematical Foundations of Programming Semantics).
Cheney, J. & Hinze, R. (2002) A lightweight implementation of generics and dynamics. In Haskell Workshop. Pittsburgh, pp. 90104.
Cockett, R. & Fukushima, T. (May 1992) About Charity. Department of Computer Science, University of Calgary.
Cook, W. R. (1989) A denotational Semantics of Inheritance. Ph.D. thesis, Brown University.
Dijkstra, A. & Swierstra, S. D. (2005) Making Implicit Parameters Explicit. Tech. rept. UU-CS-2005-032. Department of Information and Computing Sciences, Utrecht University.
Gamma, E., Helm, R., Johnson, R. & Vlissides, J. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
Gibbons, J. (2003) Origami programming. In The Fun of Programming, Gibbons, J. & de Moor, O. (eds), Palgrave Macmillan, pp. 4160.
Gibbons, J. (2006) Design patterns as higher-order datatype-generic programs. In Workshop on Generic Programming, pp. 1–12.
Gibbons, J. & de Moor, O. (eds) (2003) The Fun of Programming. Cornerstones in Computing Serie. Palgrave Macmillan.
Gibbons, J. & Oliveira, B. C. D. S. (2009) The essence of the Iterator pattern, J. Funct. Program., 19, 377402.
Gibbons, J. & Paterson, R. (2009) Parametric datatype-genericity. In Workshop on Generic Programming, Jansson, P. & Schupp, S. (eds). Edinburgh, pp. 8593.
Hall, C. V., Hammond, K., Peyton, J., Simon, L. & Wadler, P. L. (1996) Type classes in Haskell, ACM Trans. Program. Lang. Syst., 18 (2), 109138.
Harper, R. & Lillibridge, M. (January 1994) A type-theoretic approach to higher-order modules with sharing. In Principles of Programming Languages, pp. 123–137.
Hinze, R. & Peyton Jones, S. (2000) Derivable type classes, In Haskell Workshop: Electronic Notes in Theoretical Computer Science, 41 (1), 535.
Hinze, R. (2000) Polytypic values possess polykinded types. In LNCS 1837: Proceedings of the Fifth International Conference on Mathematics of Program Construction, Backhouse, R. & Oliveira, J. N. (eds). Springer-Verlag, pp. 227.
Hinze, R. (2003) Fun with phantom types. In The Fun of Programming, Gibbons, J. & de Moor, O. (eds), Palgrave Macmillan.
Hinze, R. (2006) Generics for the masses, J. Funct. Program., 16 (4–5), 451483.
Hinze, R. & Jeuring, J. (2002) Generic Haskell: Practice and theory. In LNCS 2793: Summer School on Generic Programming.
Hinze, R. & Löh, A. (2007) Generic programming, now! LNCS 4719: Datatype-Generic Programming.
Hinze, R. & Löh, A. (2009) Generic programming in 3D, Sci. Comput. Program., 74 (8), 590628.
Hinze, R., Löh, A. & Oliveira, B. C. D. S. (2006) ‘Scrap your Boilerplate’ reloaded. LNCS 3945: Functional and Logic Programming, pp. 13–29.
Hughes, J. (1999) Restricted data types in Haskell. Meijer, E. (ed), In Haskell Workshop. Paris.
Jansson, P. (May 2000) Functional Polytypic Programming. Ph.D. thesis, Computing Science, Chalmers University of Technology and Göteborg University, Sweden.
Kahl, W. & Scheffczyk, J. (2001) Named instances for Haskell type classes. In Haskell Workshop. Firenze, pp. 7799.
Lämmel, R. & PeytonJones, S. Jones, S. (2003) Scrap your boilerplate: A practical design pattern for generic programming. In Types in Language Design and Implementation, pp. 26–37.
Lämmel, R. & PeytonJones, S. Jones, S. (2005) Scrap your boilerplate with class: Extensible generic functions. In International Conference on Functional Programming, pp. 204–215.
Lämmel, R., Visser, J. & Kort, J. (July 2000) Dealing with large bananas. In Workshop on Generic Programming, Jeuring, J. (ed), pp. 46–59.
Leroy, X. (1994) Manifest types, modules and separate compilation. In Principles of Programming Languages, pp. 109–122.
Lieberherr, K. (1996) Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns. PWS Publishing.
Löh, A. (2004) Exploring Generic Haskell. Ph.D. thesis, Utrecht University.
Löh, A. & Hinze, R. (2006) Open data types and open functions. In Principles and Practice of Declarative Programming, pp. 133–144.
McBride, C. & Paterson, R. (2008) Applicative programming with effects, J. Funct. Program., 18 (1), pp. 113.
Meertens, L. (1992) Paramorphisms, Form. Asp. Comput., 4 (5), 413425.
Meijer, E. & Jeuring, J. (1995) Merging monads and folds for functional programming. In LNCS 925: Advanced Functional Programming. Springer-Verlag.
Meijer, E., Fokkinga, M. & Paterson, R. (1991) Functional programming with bananas, lenses, envelopes and barbed wire. Hughes, J. (ed), In LNCS 523: Functional Programming Languages and Computer Architecture, pp. 124–144.
Mitchell, N. & Runciman, C. (2007) Uniform boilerplate and list processing. In Haskell Workshop, pp. 49–60.
Moors, A. (June 2007) Code-follows-type Programming in Scala [online]. Available at: http://www.cs.kuleuven.be/~adriaan/?q=cft_intro
Moors, A., Piessens, F. & Joosen, W. (2006) An object-oriented approach to datatype-generic programming. In Workshop on Generic Programming, pp. 96–106.
Moors, A., Piessens, F. & Odersky, M. (2008) Generics of a higher kind, Object-Oriented Program. Syst. Lang. Appl.
Odersky, M. (2006a) An Overview of the Scala Programming Language (second edition). Tech. rept. IC/2006/001. EPFL Lausanne, Switzerland.
Odersky, M. (July 2006b) Poor Man's Type Classes [online]. Available at: http://lamp.epfl.ch/~odersky/talks/wg2.8-boston06.pdf
Odersky, M. (May 2007a) Scala by Example [online]. Available at: http://scala.epfl.ch/docu/files/ScalaIntro.pdf
Odersky, M. (May 2007b) The Scala Language Specification, version 2.4 [online]. Available at: http://scala.epfl.ch/docu/files/ScalaReference.pdf
Odersky, M. & Läufer, K. (1996) Putting type annotations to work. In Principles of Programming Languages, pp. 54–67.
Odersky, M. & Zenger, M. (2005) Scalable component abstractions. In Object Oriented Programming, Systems, Languages and Applications, pp. 41–57.
Odersky, M., Zenger, C. & Zenger, M. (2001) Colored local type inference. In Principles of Programming Languages, pp. 41–53.
Odersky, M., Spoon, L. & Venners, B. (2008) Programming in Scala: A Comprehensive Step-by-Step Guide. 1st ed. Artima.
Oliveira, B. & Gibbons, J. (2005) TypeCase: a design pattern for type-indexed functions. In Haskell Workshop. New York, NY, USA: ACM Press, pp. 98109.
Oliveira, B. C. D. S. (2007) Genericity, Extensibility and Type-Safety in the Visitor Pattern. DPhil thesis, University of Oxford.
Oliveira, B. C. D. S. (July 2009a) Modular visitor components: A practical solution to the expression families problem. Drossopoulou, S. (ed), In 23rd European Conference on Object Oriented Programming (ECOOP), pp. 269–293.
Oliveira, B. C. D. S. (September 2009b) Scala for Generic Programmers: Source code [online]. Available at: http://www.comlab.ox.ac.uk/projects/gip/Scala.tgz
Oliveira, B. C. D. S. & Sulzmann, M. (April 2008) Objects to Unify Type Classes and GADTs [online]. Available at: http://www.comlab.ox.ac.uk/people/Bruno.Oliveira/objects.pdf
Oliveira, B. C. D. S., Hinze, R. & Löh, A. (April 2006) Extensible and modular generics for the masses. In Trends in Functional Programming, pp. 109–138.
Oliveira, B. C. D. S., Moors, A. & Odersky, M. (October 2010) Type classes as objects and implicits. In Systems, Programming, Languages and Applications: Software for Humanity (SPLASH), Rinard, M. (ed).
PeytonJones, S. Jones, S., Vytiniotis, D., Weirich, S. & Washburn, G. (2006) Simple unification-based type inference for GADTs. In International Conference on Functional Programming, pp. 50–61.
Rodriguez, A., Jeuring, J., Jansson, P., Gerdes, A., Kiselyov, O. & Oliveira, B. C. D. S. (2008) Comparing libraries for generic programming in Haskell. In Haskell Symposium.
Schärli, N., Ducasse, S., Nierstrasz, O. & Black, A. (July 2003) Traits: Composable units of behavior. In LNCS 2743: European Conference on Object-Oriented Programming. pp. 248–274.
Schinz, M. (May 2007) A Scala Tutorial for Java Programmers [online]. Available at: http://scala.epfl.ch/docu/files/ScalaTutorial.pdf
Sheard, T. & Peyton Jones, S. (2002) Template meta-programming for Haskell. In Haskell Workshop.
Sulzmann, M. & Wang, M. (2006) Modular generic programming with extensible superclasses. In Workshop on Generic Programming. New York, NY, USA: ACM, pp. 5565.
Wadler, P. (1993) Monads for functional programming. Program Design Calculi. Springer-Verlag.
Wadler, P. (November 1998) The Expression Problem. Java Genericity Mailing list [online]. Available at: http://www.cse.ohio-state.edu/~gb/cis888.07g/java-genericity/20
Washburn, G. (May 2008) Revisiting Higher-Rank Impredicative Polymorphism in Scala [online]. Available at: http://existentialtype.net/2008/05/26/revisiting-higher-rank-impredicat%ive-polymorphism-in-scala/
Weirich, S. (2006) RepLib: a library for derivable type classes. In Haskell Workshop, pp. 1–12.

Scala for generic programmers: Comparing Haskell and Scala support for generic programming

  • BRUNO C. D. S. OLIVEIRA (a1) and JEREMY GIBBONS (a2)

Metrics

Altmetric attention score

Full text views

Total number of HTML views: 0
Total number of PDF views: 0 *
Loading metrics...

Abstract views

Total abstract views: 0 *
Loading metrics...

* Views captured on Cambridge Core between <date>. This data will be updated every 24 hours.

Usage data cannot currently be displayed

Scala for generic programmers: Comparing Haskell and Scala support for generic programming

  • BRUNO C. D. S. OLIVEIRA (a1) and JEREMY GIBBONS (a2)
Submit a response

Discussions

No Discussions have been published for this article.

×

Reply to: Submit a response


Your details


Conflicting interests

Do you have any conflicting interests? *