APLicative Programming with Naperian Functors

  • Jeremy GibbonsEmail author
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 10201)


Much of the expressive power of array-oriented languages such as Iverson’s APL and J comes from their implicit lifting of scalar operations to act on higher-ranked data, for example to add a value to each element of a vector, or to add two compatible matrices pointwise. It is considered a shape error to attempt to combine arguments of incompatible shape, such as a 3-vector with a 4-vector. APL and J are dynamically typed, so such shape errors are caught only at run-time. Recent work by Slepak et al. develops a custom type system for an array-oriented language, statically ruling out such errors. We show here that such a custom language design is unnecessary: the requisite compatibility checks can already be captured in modern expressive type systems, as found for example in Haskell; moreover, generative type-driven programming can exploit that static type information constructively to automatically induce the appropriate liftings. We show also that the structure of multi-dimensional data is inherently a matter of Naperian applicative functors—lax monoidal functors, with strength, commutative up to isomorphism under composition—that also support traversal.


Sparse Representation Type Inference Monoidal Functor Type Safety Functional Programming Language 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.



This paper has benefitted from helpful suggestions from Tim Zakian, Matthew Pickering, Sam Lindley, Andres Löh, Wouter Swierstra, Conor McBride, Simon Peyton Jones, the participants at IFIP WG2.1 Meeting #74 and WG2.11 Meeting #16, and the anonymous reviewers, to all of whom I am very grateful. The work was partially supported by EPSRC grant EP/K020919/1 on A Theory of Least Change for Bidirectional Transformations.


  1. 1.
    Abe, A., Sumii, E.: A simple and practical linear algebra library interface with static size checking. In: Kiselyov, O., Garrigue, J. (eds.) ML Family Workshop. EPTCS, vol. 198, pp. 1–21 (2014)Google Scholar
  2. 2.
    Axelsson, E., Claessen, K., Sheeran, M., Svenningsson, J., Engdal, D., Persson, A.: The design and implementation of feldspar. In: Hage, J., Morazán, M.T. (eds.) IFL 2010. LNCS, vol. 6647, pp. 121–136. Springer, Heidelberg (2011). doi: 10.1007/978-3-642-24276-2_8 CrossRefGoogle Scholar
  3. 3.
    Bird, R., Gibbons, J., Mehner, S., Voigtländer, J., Schrijvers, T.: Understanding idiomatic traversals backwards and forwards. In: Haskell Symposium. ACM (2013)Google Scholar
  4. 4.
    Bird, R., Meertens, L.: Nested datatypes. In: Jeuring, J. (ed.) MPC 1998. LNCS, vol. 1422, pp. 52–67. Springer, Heidelberg (1998). doi: 10.1007/BFb0054285 CrossRefGoogle Scholar
  5. 5.
    Chakravarty, M.M.T., Keller, G., Lee, S., McDonell, T.L., Grover, V.: Accelerating Haskell array codes with multicore GPUs. In: Declarative Aspects of Multicore Programming, pp. 3–14. ACM (2011)Google Scholar
  6. 6.
    Claessen, K., Sheeran, M., Svensson, B.J.: Expressive array constructs in an embedded GPU kernel programming language. In: Declarative Aspects of Multicore Programming, pp. 21–30. ACM (2012)Google Scholar
  7. 7.
    Eaton, F.: Statically typed linear algebra in Haskell (demo). In: Haskell Workshop, pp. 120–121. ACM (2006)Google Scholar
  8. 8.
    Elliott, C.: Functional images. In: Gibbons, J., de Moor, O. (eds.) The Fun of Programming. Cornerstones in Computing, pp. 131–150. Palgrave, Basingstoke (2003)CrossRefGoogle Scholar
  9. 9.
    Elliott, C., Hudak, P.: Functional reactive animation. In: International Conference on Functional Programming. ACM (1997)Google Scholar
  10. 10.
    Elsman, M., Dybdal, M.: Compiling a subset of APL into a typed intermediate language. In: Workshop on Libraries, Languages, and Compilers for Array Programming, pp. 101–106. ACM (2014)Google Scholar
  11. 11.
    Felleisen, M., Findler, R.B., Flatt, M.: Semantics Engineering with PLT Redex. MIT Press, Cambridge (2009)zbMATHGoogle Scholar
  12. 12.
    Finkel, R.A., Bentley, J.L.: Quad trees: a data structure for retrieval on composite keys. Acta Informatica 4(1), 1–9 (1974)CrossRefzbMATHGoogle Scholar
  13. 13.
    GHC Team: Glasgow Haskell Compiler.
  14. 14.
    Gibbons, J.: APLicative programming with Naperian functors: Haskell code, January 2017.
  15. 15.
    Gibbons, J., de Moor, O.: The Fun of Programming. Cornerstones in Computing. Palgrave, Basingstoke (2003)CrossRefGoogle Scholar
  16. 16.
    Guibas, L.J., Wyatt, D.K.: Compilation and delayed evaluation in APL. In: Principles of Programming Languages, pp. 1–8. ACM (1978)Google Scholar
  17. 17.
    Peter Hancock. What is a Naperian container? June 2005.
  18. 18.
    Hinze, R.: Manufacturing datatypes. J. Funct. Program. 11(5), 493–524 (2001)MathSciNetCrossRefzbMATHGoogle Scholar
  19. 19.
    Hinze, R.: Fun with phantom types. In: Gibbons, J., de Moor, O. (eds.) The Fun of Programming. Cornerstones in Computing, pp. 245–262. Palgrave, Basingstoke (2003)CrossRefGoogle Scholar
  20. 20.
    Hudak, P., Jones, M.P.: Haskell vs Ada vs C++ vs Awk vs ...: an experiment in software prototyping productivity. Department of Computer Science, Yale, July 1994Google Scholar
  21. 21.
    Iverson, K.E.: A Programming Language. Wiley, Hoboken (1962)CrossRefzbMATHGoogle Scholar
  22. 22.
    Jay, C.B., Cockett, J.R.B.: Shapely types and shape polymorphism. In: Sannella, D. (ed.) ESOP 1994. LNCS, vol. 788, pp. 302–316. Springer, Heidelberg (1994). doi: 10.1007/3-540-57880-3_20 CrossRefGoogle Scholar
  23. 23.
    Jsoftware, Inc. Jsoftware: High performance development platform (2016).
  24. 24.
    Keller, G., Chakravarty, M., Leschchinskiy, R., Jones, S.P., Lippmeier, B.: Regular, shape-polymorphic parallel arrays in Haskell. In: International Conference on Functional Programming, pp. 261–272. ACM (2010)Google Scholar
  25. 25.
    Lindley, S., McBride, C.: Hasochism: the pleasure and pain of dependently typed Haskell programming. In: Haskell Symposium, pp. 81–92. ACM (2013)Google Scholar
  26. 26.
    McBride, C., Paterson, R.: Applicative programming with effects. J. Funct. Program. 18(1), 1–13 (2008)CrossRefzbMATHGoogle Scholar
  27. 27.
    McKinna, J.: Why dependent types matter. In: Principles of Programming Languages, p. 1. ACM (2006). psztxa/publ/ydtm.pdf
  28. 28.
    Mycroft, A.: Polymorphic type schemes and recursive definitions. In: Paul, M., Robinet, B. (eds.) Programming 1984. LNCS, vol. 167, pp. 217–228. Springer, Heidelberg (1984). doi: 10.1007/3-540-12925-1_41 CrossRefGoogle Scholar
  29. 29.
    Okasaki, C.: Purely Functional Data Structures. CUP, Cambridge (1998)CrossRefzbMATHGoogle Scholar
  30. 30.
    Jones, S.P., Launchbury, J.: Unboxed values as first class citizens in a non-strict functional language. In: Functional Programming Languages and Computer Architecture, pp. 636–666. ACM (1991)Google Scholar
  31. 31.
    Eriksson, A.S., Jansson, P.: An Agda formalisation of the transitive closure of block matrices (extended abstract). In: Type-Driven Development, pp. 60–61. ACM (2016)Google Scholar
  32. 32.
    Scholz, S.-B.: Functional array programming in SaC. In: Horváth, Z. (ed.) CEFP 2005. LNCS, vol. 4164, pp. 62–99. Springer, Heidelberg (2006). doi: 10.1007/11894100_3 CrossRefGoogle Scholar
  33. 33.
    Serrano, A., Hage, J., Bahr, P.: Type families with class, type classes with family. In: Haskell Symposium, pp. 129–140. ACM (2015)Google Scholar
  34. 34.
    Slepak, J., Shivers, O., Manolios, P.: An array-oriented language with static rank polymorphism. In: Shao, Z. (ed.) ESOP 2014. LNCS, vol. 8410, pp. 27–46. Springer, Heidelberg (2014). doi: 10.1007/978-3-642-54833-8_3 CrossRefGoogle Scholar
  35. 35.
    Thiemann, P., Chakravarty, M.M.T.: Agda meets accelerate. In: Hinze, R. (ed.) IFL 2012. LNCS, vol. 8241, pp. 174–189. Springer, Heidelberg (2013). doi: 10.1007/978-3-642-41582-1_11 CrossRefGoogle Scholar
  36. 36.
    Trojahner, K., Grelck, C.: Dependently typed array programs don’t go wrong. J. Logic Algebraic Program. 78(7), 643–664 (2009)MathSciNetCrossRefzbMATHGoogle Scholar
  37. 37.
    Veldhuizen, T.L.: Arrays in Blitz++. In: Caromel, D., Oldehoeft, R.R., Tholburn, M. (eds.) ISCOPE 1998. LNCS, vol. 1505, pp. 223–230. Springer, Heidelberg (1998). doi: 10.1007/3-540-49372-7_24 CrossRefGoogle Scholar
  38. 38.
    Xi, H., Pfenning, F.: Eliminating array bound checking through dependent types. In: Programming Language Design and Implementation. ACM (1998)Google Scholar
  39. 39.
    Yorgey, B.A., Weirich, S., Cretin, J., Jones, S.P., Vytiniotis, D., Magalhães, J.P.: Giving Haskell a promotion. In: Types in Language Design and Implementation, pp. 53–66. ACM (2012)Google Scholar

Copyright information

© Springer-Verlag GmbH Germany 2017

Authors and Affiliations

  1. 1.University of OxfordOxfordUK

Personalised recommendations