Abstract
Pattern matching is an important language construct for data abstraction. Many pattern-match extensions have been developed for extending the range of data types to which pattern matching is applicable. Among them, the pattern-match system proposed by Egi and Nishiwaki features practical pattern matching for non-free data types by providing a user-customizable non-linear pattern-match facility with backtracking. However, they implemented their proposal only in dynamically typed programming languages, and there were no proposals that allow programmers to benefit from both static type systems and expressive pattern matching. This paper proposes a method for implementing this pattern-match facility by meta-programming in Haskell. There are two technical challenges: (i) we need to design a set of typing rules for the pattern-match facility; (ii) we need to embed these typing rules in Haskell to make types of the pattern-match expressions inferable by the Haskell type system. We propose a set of typing rules and show that several GHC extensions, such as multi-parameter type classes, datatype promotion, GADTs, existential types, and view patterns, play essential roles for embedding these typing rules into Haskell. The implementation has already been distributed as a Haskell library miniEgison via Hackage.
Similar content being viewed by others
Notes
We borrow this notation from [8].
The definitions of the nil and join patterns are omitted.
References
ViewPatterns-GHC. https://ghc.haskell.org/trac/ghc/wiki/ViewPatterns (2015). [Online; accessed 14-June-2018]
Egi, S.: Scheme Macros for Non-linear Pattern Matching with Backtracking for Non-free Data Types. In: The Scheme and Functional Programming Workshop (2019)
Egi, S., Nishiwaki, Y.: Non-linear pattern matching with backtracking for non-free data types. In: Asian Symposium on Programming Languages and Systems , Springer, New York, pp. 3–23 (2018)
Egi, S., Nishiwaki, Y.: Functional Programming in Pattern-Match-Oriented Programming Style. In: The Art, Science, and Engineering of Programming. Vol. 4, Issue 3, Article 7 (2020)
Erwig, M.: Active patterns. Implementation of Functional Languages (1996)
Gorski, C.A.: General-games: Library supporting simulation of a number of games. https://hackage.haskell.org/package/general-games (2018). [Online; accessed 10-May-2020]
Hudak, P., Hughes, J., Jones, S. P., Wadler, P.: A history of haskell: Being lazy with class. In: In Proceedings of the 3rd ACM SIGPLAN Conference on History of Programming Languages (HOPL-III , ACM Press, pp. 1–55 (2007)
Jones, S. P., Washburn, G., Weirich, S.: Wobbly Types: Type Inference for Generalised Algebraic Data Types. Tech. rep., Technical Report MS-CIS-05-26, Univ. of Pennsylvania (2004)
Kawata, A.: egison/typed-egison: Typed Egison. https://github.com/egison/typed-egison, 2018. [Online; accessed 10-Oct-2019]
Kiselyov, O., Lämmel, R., Schupke, K.: Strongly Typed Heterogeneous Collections. In Proceedings of the 2004 ACM SIGPLAN workshop on Haskell (2004), ACM, pp. 96–107
Kiselyov, O., Shan, C., Friedman, D. P., Sabry, A.: Backtracking, interleaving, and terminating monad transformers: (functional pearl). In Proceedings of the 10th ACM SIGPLAN International Conference on Functional Programming, ICFP 2005, Tallinn, Estonia, September 26-28, 2005 (2005), pp. 192–203
Kori, M., Egi, S.: egison/egison-haskell: Template Haskell Implementation of Egison Pattern Matching. https://github.com/egison/egison-haskell, 2019. [Online; accessed 21-Feb-2019]
OSullivan, B.: criterion: a Haskell microbenchmarking library. http://www.serpentine.com/criterion/, 2014. [Online; accessed 13-Mar-2020]
Pope, R., Yorgey, B.: first-class-patterns: First class patterns and pattern matching, using type families. https://hackage.haskell.org/package/first-class-patterns, 2019. [Online; accessed 13-Mar-2020]
Sheard, T., Jones, S. P.: Template Meta-programming for Haskell. In Proceedings of the 2002 ACM SIGPLAN workshop on Haskell (2002), ACM, pp. 1–16
Team, G.: 13.1. Language options — Glasgow Haskell Compiler 8.8.1 User’s Guide. https://downloads.haskell.org/~ghc/8.8.1/docs/html/users_guide/glasgow_exts.html, 2019. [Online; accessed 18-Dec-2019]
Tullsen, M.: First class patterns. In Practical Aspects of Declarative Languages, Second International Workshop, PADL 2000, Boston, MA, USA, January 2000, Proceedings (2000), E. Pontelli and V. S. Costa, Eds., vol. 1753 of Lecture Notes in Computer Science, Springer, pp. 1–15
Turner, D.: Some history of functional programming languages. In International Symposium on Trends in Functional Programming (2012), Springer, pp. 1–20
Wadler, P.: Views: A way for pattern matching to cohabit with data abstraction. In Proceedings of the 14th ACM SIGACT-SIGPLAN symposium on Principles of programming languages (1987)
Wright, A. K., and Duba, B. F.: Pattern matching for Scheme. Unpublished manuscript (1993)
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 (2012), ACM, pp. 53–66
Acknowledgements
We thank Yuichi Nishiwaki for constructive discussion while implementing the proposed library.
Author information
Authors and Affiliations
Corresponding author
Ethics declarations
Conflict of interest
The authors declare that they have no conflict of interest.
Additional information
Publisher's Note
Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
About this article
Cite this article
Egi, S., Kawata, A., Kori, M. et al. Embedding Non-linear Pattern Matching with Backtracking for Non-free Data Types into Haskell. New Gener. Comput. 40, 481–506 (2022). https://doi.org/10.1007/s00354-022-00177-z
Received:
Accepted:
Published:
Issue Date:
DOI: https://doi.org/10.1007/s00354-022-00177-z