Skip to main content

Deep Embedding with Class

  • Conference paper
  • First Online:
Trends in Functional Programming (TFP 2022)

Part of the book series: Lecture Notes in Computer Science ((LNCS,volume 13401))

Included in the following conference series:

  • 244 Accesses

Abstract

The two flavours of DSL embedding are shallow and deep embedding. In functional languages, shallow embedding models the language constructs as functions in which the semantics are embedded. Adding semantics is therefore cumbersome while adding constructs is a breeze. Upgrading the functions to type classes lifts this limitation to a certain extent.

Deeply embedded languages represent their language constructs as data and the semantics are functions on it. As a result, the language constructs are embedded in the semantics, hence adding new language constructs is laborious where adding semantics is trouble free.

This paper shows that by abstracting the semantics functions in deep embedding to type classes, it is possible to easily add language constructs as well. So-called classy deep embedding results in DSLs that are extensible both in language constructs and in semantics while maintaining a concrete abstract syntax tree. Additionally, little type-level trickery or complicated boilerplate code is required to achieve this.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
$34.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or eBook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 1.

    Lubbers, M. (2022): Literate Haskell/lhs2 source code of the paper “Deep Embedding with Class”: TFP 2022. Zenodo. https://doi.org/10.5281/zenodo.6650880.

  2. 2.

    All data types and functions are subscripted to indicate the evolution.

  3. 3.

    In this case \(\textbf{newtype}\)s are used instead of regular \(\textbf{data}\) declarations. A \(\textbf{newtype}\) is a special data type with a single constructor containing a single value only to which it is isomorphic. It allows the programmer to define separate class instances that the instances of the isomorphic type without any overhead. During compilation the constructor is completely removed [18, Sect. 4.2.3].

  4. 4.

    Backticks are used to use functions or constructors in an infix fashion [18, Sect. 4.3.3].

  5. 5.

    Resulting in the following constructor: .

References

  1. Abadi, M., Cardelli, L., Pierce, B., Plotkin, G.: Dynamic typing in a statically typed language. ACM Trans. Program. Lang. Syst. 13(2), 237–268 (1991). https://doi.org/10.1145/103135.103138

    Article  Google Scholar 

  2. Baars, A.I., Swierstra, S.D.: Typing dynamic typing. In: Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming, pp. 157–166. ICFP 2002, Association for Computing Machinery, New York (2002). https://doi.org/10.1145/581478.581494

  3. Bolingbroke, M.: Constraint kinds for GHC (2011). http://blog.omega-prime.co.uk/2011/09/10/constraint-kinds-for-ghc/. Blog post. Accessed 09 Sep 2021

  4. Boulton, R., Gordon, A., Gordon, M., Harrison, J., Herbert, J., Tassel, J.V.: Experience with embedding hardware description languages in HOL. In: Stavridou, V., Melham, T.F., Boute, R.T. (eds.) IFIP TC10/WG, vol. 10, pp. 129–156. Elsevier, Amsterdam, NL (1992). Event-place: Nijmegen, NL

    Google Scholar 

  5. Carette, J., Kiselyov, O., Shan, C.C.: Finally tagless, partially evaluated: tagless staged interpreters for simpler typed languages. J. Funct. Program. 19(5), 509–543 (2009). https://doi.org/10.1017/S0956796809007205

    Article  MathSciNet  MATH  Google Scholar 

  6. Cheney, J., Hinze, R.: A lightweight implementation of generics and dynamics. In: Proceedings of the 2002 ACM SIGPLAN Workshop on Haskell, pp. 90–104. Association for Computing Machinery, Pittsburgh, PA (2002). https://doi.org/10.1145/581690.581698

  7. Cheney, J., Hinze, R.: First-class phantom types. Tech. Rep. TR2003-1901, Cornell University (2003). https://ecommons.cornell.edu/handle/1813/5614

  8. Gibbons, J., Wu, N.: Folding domain-specific languages: deep and shallow embeddings (functional pearl). In: Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming, pp. 339–347. ICFP 2014, Association for Computing Machinery, New York (2014). https://doi.org/10.1145/2628136.2628138

  9. Hinze, R.: Fun with phantom types. In: Gibbons, J., de Moor, O. (eds.) The Fun of Programming, pp. 245–262. Bloomsbury Publishing, Palgrave, Cornerstones of Computing (2003)

    Google Scholar 

  10. Kiselyov, O.: Typed tagless final interpreters. In: Gibbons, J. (ed.) Generic and Indexed Programming. LNCS, vol. 7470, pp. 130–174. Springer, Heidelberg (2012). https://doi.org/10.1007/978-3-642-32202-0_3

    Chapter  Google Scholar 

  11. Krishnamurthi, S., Felleisen, M., Friedman, D.P.: Synthesizing object-oriented and functional design to promote re-use. In: Jul, E. (ed.) ECOOP 1998. LNCS, vol. 1445, pp. 91–113. Springer, Heidelberg (1998). https://doi.org/10.1007/BFb0054088

    Chapter  Google Scholar 

  12. Löh, A., Hinze, R.: Open data types and open functions. In: Proceedings of the 8th ACM SIGPLAN International Conference on Principles and Practice of Declarative Programming, pp. 133–144. PPDP 2006, Association for Computing Machinery, New York (2006). https://doi.org/10.1145/1140335.1140352

  13. Läufer, K.: Combining type classes and existential types. In: Proceedings of the Latin American Informatic Conference (PANEL). ITESM-CEM, Monterrey, Mexico (1994)

    Google Scholar 

  14. Läufer, K.: Type classes with existential types. J. Funct. Program. 6(3), 485–518 (1996). https://doi.org/10.1017/S0956796800001817

    Article  MathSciNet  MATH  Google Scholar 

  15. Mitchell, J.C., Plotkin, G.D.: Abstract types have existential type. ACM Trans. Program. Lang. Syst. 10(3), 470–502 (1988). https://doi.org/10.1145/44501.45065

    Article  Google Scholar 

  16. Najd, S., Peyton Jones, S.: Trees that grow. J. Univ. Comput. Sci. 23(1), 42–62 (2017)

    MathSciNet  Google Scholar 

  17. Odersky, M., Läufer, K.: Putting type annotations to work. In: Proceedings of the 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pp. 54–67. POPL 1996, Association for Computing Machinery, New York (1996). https://doi.org/10.1145/237721.237729

  18. Peyton Jones, S. (ed.): Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press, Cambridge (2003)

    MATH  Google Scholar 

  19. Reynolds, J.C.: User-defined types and procedural data structures as complementary approaches to data abstraction. In: Gries, D. (ed.) Programming Methodology: A Collection of Articles by Members of IFIP WG2.3, pp. 309–317. Springer, New York (1978). https://doi.org/10.1007/978-1-4612-6315-9_22

  20. Svenningsson, J., Axelsson, E.: Combining deep and shallow embedding for EDSL. In: Loidl, H.-W., Peña, R. (eds.) TFP 2012. LNCS, vol. 7829, pp. 21–36. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-40447-4_2

    Chapter  Google Scholar 

  21. Swierstra, W.: Data types à la carte. J. Funct. Program. 18(4), 423–436 (2008). https://doi.org/10.1017/S0956796808006758

    Article  MathSciNet  MATH  Google Scholar 

  22. Team, G.: Data. Dynamic (2021). https://hackage.haskell.org/package/base-4.14.1.0/docs/Data-Dynamic.html. Accessed 24 Feb 2021

  23. Team, G.: GHC User’s Guide Documentation (2021). https://downloads.haskell.org/~ghc/latest/docs/users%20guide.pdf. Accessed 24 Feb 2021

  24. Wadler, P.: The expression problem (1998-11-12). https://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt. Accessed 24 Feb 2021

  25. Yorgey, B.A., Weirich, S., Cretin, J., Peyton Jones, S., Vytiniotis, D., Magalhães, J.P.: Giving haskell a promotion. In: Proceedings of the 8th ACM SIGPLAN Workshop on Types in Language Design and Implementation, pp. 53–66. TLDI 2012, Association for Computing Machinery, New York (2012). https://doi.org/10.1145/2103786.2103795

Download references

Acknowledgements

This research is partly funded by the Royal Netherlands Navy. Furthermore, I would like to thank Pieter and Rinus for the fruitful discussions, Ralf for inspiring me to write a functional pearl, and the anonymous reviewers for their valuable and honest comments.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Mart Lubbers .

Editor information

Editors and Affiliations

Appendix

Appendix

1.1 Data Type Definitions

figure az

1.2 Smart Constructors

figure ba

1.3 Semantics Classes and Data Types

figure bb
figure bc

1.4 \( GDict \) instances

figure bd

1.5 Evaluator Instances

figure be
figure bf
figure bg
figure bh
figure bi

1.6 Printer Instances

figure bj
figure bk
figure bl
figure bm
figure bn

1.7 Optimisation Instances

figure bo
figure bp
figure bq
figure br

Rights and permissions

Reprints and permissions

Copyright information

© 2022 Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Lubbers, M. (2022). Deep Embedding with Class. In: Swierstra, W., Wu, N. (eds) Trends in Functional Programming. TFP 2022. Lecture Notes in Computer Science, vol 13401. Springer, Cham. https://doi.org/10.1007/978-3-031-21314-4_3

Download citation

  • DOI: https://doi.org/10.1007/978-3-031-21314-4_3

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-031-21313-7

  • Online ISBN: 978-3-031-21314-4

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics