Hostname: page-component-7479d7b7d-qlrfm Total loading time: 0 Render date: 2024-07-10T23:59:14.508Z Has data issue: false hasContentIssue false

Scala for generic programmers

Comparing Haskell and Scala support for generic programming

Published online by Cambridge University Press:  21 October 2010

BRUNO C. D. S. OLIVEIRA
Affiliation:
ROSAEC Center, Seoul National University, 599 Gwanak-ro, Gwanak-gu, Seoul 151-744, South Korea (e-mail: bruno@ropas.snu.ac.kr)
JEREMY GIBBONS
Affiliation:
Oxford University Computing Laboratory, Wolfson Building, Parks Road, Oxford OX1 3QD, UK (e-mail: jg@comlab.ox.ac.uk)
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

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’.

Type
Articles
Copyright
Copyright © Cambridge University Press 2010

References

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.Google Scholar
Brown, N. C. C. & Sampson, A. T. (2009) Alloy: Fast generic transformations for Haskell. In Haskell Symposium. Edinburgh, pp. 105116.Google Scholar
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.CrossRefGoogle Scholar
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).CrossRefGoogle Scholar
Cheney, J. & Hinze, R. (2002) A lightweight implementation of generics and dynamics. In Haskell Workshop. Pittsburgh, pp. 90104.CrossRefGoogle Scholar
Cockett, R. & Fukushima, T. (May 1992) About Charity. Department of Computer Science, University of Calgary.Google Scholar
Cook, W. R. (1989) A denotational Semantics of Inheritance. Ph.D. thesis, Brown University.CrossRefGoogle Scholar
Dijkstra, A. & Swierstra, S. D. (2005) Making Implicit Parameters Explicit. Tech. rept. UU-CS-2005-032. Department of Information and Computing Sciences, Utrecht University.Google Scholar
Gamma, E., Helm, R., Johnson, R. & Vlissides, J. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.Google Scholar
Gibbons, J. (2003) Origami programming. In The Fun of Programming, Gibbons, J. & de Moor, O. (eds), Palgrave Macmillan, pp. 4160.CrossRefGoogle Scholar
Gibbons, J. (2006) Design patterns as higher-order datatype-generic programs. In Workshop on Generic Programming, pp. 1–12.CrossRefGoogle Scholar
Gibbons, J. & de Moor, O. (eds) (2003) The Fun of Programming. Cornerstones in Computing Serie. Palgrave Macmillan.CrossRefGoogle Scholar
Gibbons, J. & Oliveira, B. C. D. S. (2009) The essence of the Iterator pattern, J. Funct. Program., 19, 377402.CrossRefGoogle Scholar
Gibbons, J. & Paterson, R. (2009) Parametric datatype-genericity. In Workshop on Generic Programming, Jansson, P. & Schupp, S. (eds). Edinburgh, pp. 8593.Google Scholar
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.CrossRefGoogle Scholar
Harper, R. & Lillibridge, M. (January 1994) A type-theoretic approach to higher-order modules with sharing. In Principles of Programming Languages, pp. 123–137.CrossRefGoogle Scholar
Hinze, R. & Peyton Jones, S. (2000) Derivable type classes, In Haskell Workshop: Electronic Notes in Theoretical Computer Science, 41 (1), 535.Google Scholar
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.Google Scholar
Hinze, R. (2003) Fun with phantom types. In The Fun of Programming, Gibbons, J. & de Moor, O. (eds), Palgrave Macmillan.Google Scholar
Hinze, R. (2006) Generics for the masses, J. Funct. Program., 16 (4–5), 451483.CrossRefGoogle Scholar
Hinze, R. & Jeuring, J. (2002) Generic Haskell: Practice and theory. In LNCS 2793: Summer School on Generic Programming.CrossRefGoogle Scholar
Hinze, R. & Löh, A. (2007) Generic programming, now! LNCS 4719: Datatype-Generic Programming.CrossRefGoogle Scholar
Hinze, R. & Löh, A. (2009) Generic programming in 3D, Sci. Comput. Program., 74 (8), 590628.CrossRefGoogle Scholar
Hinze, R., Löh, A. & Oliveira, B. C. D. S. (2006) ‘Scrap your Boilerplate’ reloaded. LNCS 3945: Functional and Logic Programming, pp. 13–29.Google Scholar
Hughes, J. (1999) Restricted data types in Haskell. Meijer, E. (ed), In Haskell Workshop. Paris.Google Scholar
Jansson, P. (May 2000) Functional Polytypic Programming. Ph.D. thesis, Computing Science, Chalmers University of Technology and Göteborg University, Sweden.Google Scholar
Kahl, W. & Scheffczyk, J. (2001) Named instances for Haskell type classes. In Haskell Workshop. Firenze, pp. 7799.Google Scholar
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.CrossRefGoogle Scholar
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.CrossRefGoogle Scholar
Lämmel, R., Visser, J. & Kort, J. (July 2000) Dealing with large bananas. In Workshop on Generic Programming, Jeuring, J. (ed), pp. 46–59.Google Scholar
Leroy, X. (1994) Manifest types, modules and separate compilation. In Principles of Programming Languages, pp. 109–122.CrossRefGoogle Scholar
Lieberherr, K. (1996) Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns. PWS Publishing.Google Scholar
Löh, A. (2004) Exploring Generic Haskell. Ph.D. thesis, Utrecht University.Google Scholar
Löh, A. & Hinze, R. (2006) Open data types and open functions. In Principles and Practice of Declarative Programming, pp. 133–144.CrossRefGoogle Scholar
McBride, C. & Paterson, R. (2008) Applicative programming with effects, J. Funct. Program., 18 (1), pp. 113.CrossRefGoogle Scholar
Meertens, L. (1992) Paramorphisms, Form. Asp. Comput., 4 (5), 413425.CrossRefGoogle Scholar
Meijer, E. & Jeuring, J. (1995) Merging monads and folds for functional programming. In LNCS 925: Advanced Functional Programming. Springer-Verlag.Google Scholar
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.CrossRefGoogle Scholar
Mitchell, N. & Runciman, C. (2007) Uniform boilerplate and list processing. In Haskell Workshop, pp. 49–60.CrossRefGoogle Scholar
Moors, A. (June 2007) Code-follows-type Programming in Scala [online]. Available at: http://www.cs.kuleuven.be/~adriaan/?q=cft_introGoogle Scholar
Moors, A., Piessens, F. & Joosen, W. (2006) An object-oriented approach to datatype-generic programming. In Workshop on Generic Programming, pp. 96–106.CrossRefGoogle Scholar
Moors, A., Piessens, F. & Odersky, M. (2008) Generics of a higher kind, Object-Oriented Program. Syst. Lang. Appl.CrossRefGoogle Scholar
Odersky, M. (2006a) An Overview of the Scala Programming Language (second edition). Tech. rept. IC/2006/001. EPFL Lausanne, Switzerland.Google Scholar
Odersky, M. (July 2006b) Poor Man's Type Classes [online]. Available at: http://lamp.epfl.ch/~odersky/talks/wg2.8-boston06.pdfGoogle Scholar
Odersky, M. (May 2007a) Scala by Example [online]. Available at: http://scala.epfl.ch/docu/files/ScalaIntro.pdfGoogle Scholar
Odersky, M. (May 2007b) The Scala Language Specification, version 2.4 [online]. Available at: http://scala.epfl.ch/docu/files/ScalaReference.pdfGoogle Scholar
Odersky, M. & Läufer, K. (1996) Putting type annotations to work. In Principles of Programming Languages, pp. 54–67.CrossRefGoogle Scholar
Odersky, M. & Zenger, M. (2005) Scalable component abstractions. In Object Oriented Programming, Systems, Languages and Applications, pp. 41–57.CrossRefGoogle Scholar
Odersky, M., Zenger, C. & Zenger, M. (2001) Colored local type inference. In Principles of Programming Languages, pp. 41–53.CrossRefGoogle Scholar
Odersky, M., Spoon, L. & Venners, B. (2008) Programming in Scala: A Comprehensive Step-by-Step Guide. 1st ed. Artima.Google Scholar
Oliveira, B. & Gibbons, J. (2005) TypeCase: a design pattern for type-indexed functions. In Haskell Workshop. New York, NY, USA: ACM Press, pp. 98109.CrossRefGoogle Scholar
Oliveira, B. C. D. S. (2007) Genericity, Extensibility and Type-Safety in the Visitor Pattern. DPhil thesis, University of Oxford.Google Scholar
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.CrossRefGoogle Scholar
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.tgzCrossRefGoogle Scholar
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.pdfGoogle Scholar
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.Google Scholar
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).CrossRefGoogle Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
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.CrossRefGoogle Scholar
Schinz, M. (May 2007) A Scala Tutorial for Java Programmers [online]. Available at: http://scala.epfl.ch/docu/files/ScalaTutorial.pdfGoogle Scholar
Sheard, T. & Peyton Jones, S. (2002) Template meta-programming for Haskell. In Haskell Workshop.CrossRefGoogle Scholar
Sulzmann, M. & Wang, M. (2006) Modular generic programming with extensible superclasses. In Workshop on Generic Programming. New York, NY, USA: ACM, pp. 5565.Google Scholar
Wadler, P. (1993) Monads for functional programming. Program Design Calculi. Springer-Verlag.Google Scholar
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/20Google Scholar
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/Google Scholar
Weirich, S. (2006) RepLib: a library for derivable type classes. In Haskell Workshop, pp. 1–12.CrossRefGoogle Scholar
Submit a response

Discussions

No Discussions have been published for this article.