skip to main content
article
Free Access

Fast partial evaluation of pattern matching in strings

Published:01 July 2006Publication History
Skip Abstract Section

Abstract

We show how to obtain all of Knuth, Morris, and Pratt's linear-time string matcher by specializing a quadratic-time string matcher with respect to a pattern string. Although it has been known for fifteen years how to obtain this linear matcher by partial evaluation of a quadratic one, how to obtain it in linear time has remained an open problem.Obtaining a linear matcher by the partial evaluation of a quadratic one is achieved by performing its backtracking at specialization time and memoizing its results. We show (1) how to rewrite the source matcher such that its static intermediate computations can be shared at specialization time and (2) how to extend the memoization capabilities of a partial evaluator to static functions. Such an extended partial evaluator, if its memoization is implemented efficiently, specializes the rewritten source matcher in linear time. Finally, we show that the method also applies to a variant of Boyer and Moore's string matcher.

References

  1. Ager, M. S., Danvy, O., and Rohde, H. K. 2002. On obtaining Knuth, Morris, and Pratt's string matcher by partial evaluation. In Proceedings of the ACM SIGPLAN Asian Symposium on Partial Evaluation and Semantics-Based Program Manipulation (W. N. Chin Ed.), 32--46. Extended version available as Tech. rep. BRICS-RS-02-32.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Aho, A. V., Hopcroft, J. E., and Ullman, J. D. 1974. The Design and Analysis of Computer Algorithms. Addison Wesley, Reading, Mass.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Alpuente, M., Falaschi, M., Juliàn, P., and Vidal, G. 1997. Specialization of inductively sequential functional logic programs. In Proceedings of the ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation, C. Consel, ed. 151--162.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Amtoft, T. 1993. Sharing of computations. Ph.D. thesis, DAIMI, Department of Computer Science, University of Aarhus, Aarhus, Denmark. Tech. rep. PB-453.]]Google ScholarGoogle Scholar
  5. Amtoft, T., Consel, C., Danvy, O., and Malmkjær, K. 2002. The abstraction and instantiation of string-matching programs. In The Essence of Computation: Complexity, Analysis, Transformation. Essays Dedicated to Neil D. Jones, T. æ. Mogensen et al., eds. LNCS vol. 2566, Springer 332--357.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Bellman, R. 1957. Dynamic Programming. Princeton University Press, Princeton, N. J.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Bird, R. S. 1977. Improving programs by the introduction of recursion. Commun. ACM 20, 11 (Nov.), 856--863.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Bondorf, A. and Danvy, O. 1991. Automatic autoprojection of recursive equations with global variables and abstract data types. Sci. Compu. Program. 16, 151--195.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Boyer, R. S. and Moore, J. S. 1977. A fast string searching algorithm. Commun. ACM 20, 10, 762--772.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Bulyonkov, M. A. 1984. Polyvariant mixed computation for analyzer programs. Acta Informatica 21, 473--484.]]Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Charras, C. and Lecroq, T. 1997. Exact string matching algorithms. http://www-igm.univ-mlv.fr/~lecroq/string/.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Chen, W. and Warren, D. S. 1996. Tabled evaluation with delaying for general logic programs. J. ACM 43, 1 (Jan.), 20--74.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Chin, W.-N., ed. 2002. ACM SIGPLAN Asian Symposium on Partial Evaluation and Semantics-Based Program Manipulation.]]Google ScholarGoogle Scholar
  14. Colussi, L. 1991. Correctness and efficiency of pattern matching algorithms. Inf. Comput. 95, 225--251.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Consel, C. and Danvy, O. 1989. Partial evaluation of pattern matching in strings. Inf. Process. Lett. 30, 2 (Jan.), 79--86.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Consel, C. and Danvy, O. 1993. Tutorial notes on partial evaluation. In Proceedings of the 20th Annual ACM Symposium on Principles of Programming Languages, S. L. Graham, ed. 493--501.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Dijkstra, E. W. 1976. A Discipline of Programming. Prentice-Hall, Upper Saddle River, N.J.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Futamura, Y., Konishi, Z., and Glück, R. 2002a. Automatic generation of efficient string matching algorithms by generalized partial computation. In Proceedings of the ACM SIGPLAN Asian Symposium on Partial Evaluation and Semantics-Based Program Manipulation (W. N. Chin Ed.) 1--8.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Futamura, Y., Konishi, Z., and Glück, R. 2002b. Program transformation system based on generalized partial computation. New Gen. Comput. 20, 1, 75--99.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Futamura, Y. and Nogi, K. 1988. Generalized partial computation. In Partial Evaluation and Mixed Computation, D. Bjørner et al. eds. North-Holland, Amsterdam, the Netherlands, 133--151.]]Google ScholarGoogle Scholar
  21. Glück, R. and Jørgensen, J. 1994. Generating optimizing specializers. In Proceedings of the 5th IEEE International Conference on Computer Languages, H. Bal, ed. 183--194.]]Google ScholarGoogle Scholar
  22. Glück, R. and Klimov, A. 1993. Occam's razor in metacomputation: The notion of a perfect process tree. In Proceedings of the 3th International Workshop on Static Analysis WSA'93, P. Cousot et al., eds. LNCS vol. 724 Springer, Berlin, 112--123.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Grobauer, B. and Lawall, J. L. 2002. Partial evaluation of pattern matching in strings, revisited. Nordic J. Comput. 8, 4, 437--462.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Hernández, M. and Rosenblueth, D. A. 2003. Disjunctive partial deduction of a right-to-left string-matching algorithm. Inf. Process. Lett. 87, 235--241.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Hernández, M. and Rosenblueth, D. A. 2001. Development reuse and the logic program derivation of two string-matching algorithms. In Proceedings of the 3rd International ACM SIGPLAN Conference on Principles and Practice of Declarative Programming (PPDP'01), H. Søndergaard, ed. 38--48.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Holst, C. K. and Gomard, C. K. 1991. Partial evaluation is fuller laziness. In Proceedings of the ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation. SIGPLAN Notices (N. Jones and P. Hudak Eds.), vol. 26, 9, 223--233.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Hudak, P. and Jones, N. D., eds. 1991. ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation. SIGPLAN Notices, vol. 26, no 9.]]Google ScholarGoogle Scholar
  28. Jones, N. D., Gomard, C. K., and Sestoft, P. 1993. Partial Evaluation and Automatic Program Generation. Prentice-Hall, UK. http://www.dina.kvl.dk/sestoft/pebook/.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Kaneko, K. and Takeichi, M. 1993. Derivation of a Knuth-Morris-Pratt algorithm by fully lazy partial computation. Adv. Softw. Sci. Technol. 5, 11--24.]]Google ScholarGoogle ScholarCross RefCross Ref
  30. Kelsey, R., Clinger, W., and Rees, J. 1998. Revised5 report on the algorithmic language scheme. Higher-Order Symbol. Comput. 11, 1, 7--105.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. Knuth, D. E., Morris, J. H., and Pratt, V. R. 1977. Fast pattern matching in strings. SIAM J. Comput. 6, 2, 323--350.]]Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Lafave, L. and Gallagher, J. P. 1997. Constraint-Based partial evaluation of rewriting-based functional logic programs. In Proceeding of the 7th International Workshop on Program Synthesis and Transformation, N. E. Fuchs, ed. LNCS vol. 1463. Springer, Berlin, 168--188.]]Google ScholarGoogle Scholar
  33. Mogensen, T. A. 1989. Binding time aspects of partial evaluation. Ph.D. thesis, DIKU, Computer Science Department, University of Copenhagen, Copenhagen, Denmark.]]Google ScholarGoogle Scholar
  34. Mogensen, T. A. 2000. Glossary for partial evaluation and related topics. Higher-Order Symbol. Comput. 13, 4, 355--368.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. Queinnec, C. and Geffroy, J.-M. 1992. Partial evaluation applied to pattern matching with intelligent backtrack. In Proceedings of the 2nd International Workshop on Static Analysis WSA'92. Bigre J. vol. 81-82, 109--117.]]Google ScholarGoogle Scholar
  36. Sagonas, K., Swift, T., and Warren, D. S. 1994. XSB as an efficient deductive database engine. In Proceedings of the ACM SIGMOD International Conference on the Management of Data. 442--453.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Secher, J. P. 2002. Driving-Based program transformation in theory and practice. Ph.D. thesis, DIKU, Computer Science Department, University of Copenhagen, Copenhagen, Denmark. DIKU Rapport D-486.]]Google ScholarGoogle Scholar
  38. Smith, D. A. 1991. Partial evaluation of pattern matching in constraint logic programming languages. In Proceedings of the ACM SIGPLAN Asian Symposium on Partial Evaluation and Semantics-Based Program Manipulation (N. Jones and P. Hudak Eds.) 62--71.]] Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Sørensen, M. H. 1994. Turchin's supercompiler revisited. An operational theory of positive information propagation. M.S. thesis, DIKU, Computer Science Department, University of Copenhagen Copenhogen, Denmark. DIKU Rapport 94/17.]]Google ScholarGoogle Scholar
  40. Sørensen, M. H., Glück, R., and Jones, N. D. 1996. A positive supercompiler. J. Functional Program. 6, 6, 811--838.]]Google ScholarGoogle ScholarCross RefCross Ref
  41. Takeichi, M. and Akama, Y. 1990. Deriving a functional Knuth-Morris-Pratt algorithm. J. Inf. Process. 13, 4, 522--528.]] Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Fast partial evaluation of pattern matching in strings

          Recommendations

          Comments

          Login options

          Check if you have access through your login credentials or your institution to get full access on this article.

          Sign in

          Full Access

          • Published in

            cover image ACM Transactions on Programming Languages and Systems
            ACM Transactions on Programming Languages and Systems  Volume 28, Issue 4
            July 2006
            217 pages
            ISSN:0164-0925
            EISSN:1558-4593
            DOI:10.1145/1146809
            Issue’s Table of Contents

            Copyright © 2006 ACM

            Publisher

            Association for Computing Machinery

            New York, NY, United States

            Publication History

            • Published: 1 July 2006
            Published in toplas Volume 28, Issue 4

            Permissions

            Request permissions about this article.

            Request Permissions

            Check for updates

            Qualifiers

            • article

          PDF Format

          View or Download as a PDF file.

          PDF

          eReader

          View online with eReader.

          eReader
          About Cookies On This Site

          We use cookies to ensure that we give you the best experience on our website.

          Learn more

          Got it!