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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
Notes
- 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.
All data types and functions are subscripted to indicate the evolution.
- 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.
Backticks are used to use functions or constructors in an infix fashion [18, Sect. 4.3.3].
- 5.
Resulting in the following constructor: .
References
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
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
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
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
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
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
Cheney, J., Hinze, R.: First-class phantom types. Tech. Rep. TR2003-1901, Cornell University (2003). https://ecommons.cornell.edu/handle/1813/5614
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
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)
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
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
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
Läufer, K.: Combining type classes and existential types. In: Proceedings of the Latin American Informatic Conference (PANEL). ITESM-CEM, Monterrey, Mexico (1994)
Läufer, K.: Type classes with existential types. J. Funct. Program. 6(3), 485–518 (1996). https://doi.org/10.1017/S0956796800001817
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
Najd, S., Peyton Jones, S.: Trees that grow. J. Univ. Comput. Sci. 23(1), 42–62 (2017)
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
Peyton Jones, S. (ed.): Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press, Cambridge (2003)
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
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
Swierstra, W.: Data types à la carte. J. Funct. Program. 18(4), 423–436 (2008). https://doi.org/10.1017/S0956796808006758
Team, G.: Data. Dynamic (2021). https://hackage.haskell.org/package/base-4.14.1.0/docs/Data-Dynamic.html. Accessed 24 Feb 2021
Team, G.: GHC User’s Guide Documentation (2021). https://downloads.haskell.org/~ghc/latest/docs/users%20guide.pdf. Accessed 24 Feb 2021
Wadler, P.: The expression problem (1998-11-12). https://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt. Accessed 24 Feb 2021
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
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
Corresponding author
Editor information
Editors and Affiliations
Appendix
Appendix
1.1 Data Type Definitions
1.2 Smart Constructors
1.3 Semantics Classes and Data Types
1.4 \( GDict \) instances
1.5 Evaluator Instances
1.6 Printer Instances
1.7 Optimisation Instances
Rights and permissions
Copyright information
© 2022 Springer Nature Switzerland AG
About this paper
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)