Skip to main content

Lightweight Functional Logic Meta-Programming

  • Conference paper
  • First Online:
Programming Languages and Systems (APLAS 2019)

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 11893))

Included in the following conference series:

Abstract

Meta-interpreters in Prolog are a powerful and elegant way to implement language extensions and non-standard semantics. But how can we bring the benefits of Prolog-style meta-interpreters to systems that combine functional and logic programming? In Prolog, a program can access its own structure via reflection, and meta-interpreters are simple to implement because the “pure” core language is small. Can we achieve similar elegance and power for larger systems that combine different paradigms?

In this paper, we present a particular kind of functional logic meta-programming, based on embedding a small first-order logic system in an expressive host language. Embedded logic engines are not new, as exemplified by various systems including miniKanren in Scheme and LogicT in Haskell. However, previous embedded systems generally lack meta-programming capabilities in the sense of meta-interpretation. Indeed, shallow embeddings usually do not support reflection.

Instead of relying on reflection for meta-programming, we show how to adapt popular multi-stage programming techniques to a logic programming setting and use the embedded logic to generate reified first-order structures, which are again simple to interpret. Our system has an appealing power-to-weight ratio, based on the simple and general notion of dynamically scoped mutable variables.

We also show how, in many cases, non-standard semantics can be realized without explicit reification and interpretation, but instead by customizing program execution through the host language. As a key example, we extend our system with a tabling/memoization facility. The need to interact with mutable variables renders this a highly nontrivial challenge, and the crucial insight is to extract symbolic representations of their side effects from memoized rules. We demonstrate that multiple independent semantic modifications can be combined successfully in our system, for example tabling and tracing.

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

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

References

  1. Alvis, C.E., Willcock, J.J., Carter, K.M., Byrd, W.E., Friedman, D.P.: cKanren: miniKanren with constraints. In: Scheme (2011)

    Google Scholar 

  2. Barrett, C., et al.: CVC4. In: Gopalakrishnan, G., Qadeer, S. (eds.) CAV 2011. LNCS, vol. 6806, pp. 171–177. Springer, Heidelberg (2011). https://doi.org/10.1007/978-3-642-22110-1_14

    Chapter  Google Scholar 

  3. Bowen, K.A., Kowalski, R.A.: Amalgamating logic and metalanguage in logic programming. In: Clark, K.L., Tärnlund, S.A. (eds.) Logic Programming, pp. 153–172. Academic Press, Cambridge (1982)

    Google Scholar 

  4. Byrd, W.E.: Relational programming in miniKanren: techniques, applications, and implementations. Ph.D. thesis, Indiana University, September 2009

    Google Scholar 

  5. Byrd, W.E., Ballantyne, M., Rosenblatt, G., Might, M.: A unified approach to solving seven programming problems (functional pearl). PACML 1(ICFP), 8 (2017)

    Article  Google Scholar 

  6. Byrd, W.E., Holk, E., Friedman, D.P.: miniKanren, live and untagged: quine generation via relational interpreters (programming pearl). In: Scheme (2012)

    Google Scholar 

  7. Carro, M., de Guzmàn, P.C.: Tabled logic programming and its applications (2011). http://cliplab.org/~mcarro/Slides/Misc/intro_to_tabling.pdf

  8. Chen, W., Kifer, M., Warren, D.S.: HiLog: a foundation for higher-order logic programming. J. Log. Program. 15(3), 187–230 (1993)

    Article  MathSciNet  Google Scholar 

  9. Claessen, K., Ljunglöf, P.: Typed logical variables in Haskell. Electr. Notes Theor. Comput. Sci. 41(1), 37 (2000). Haskell Workshop

    Google Scholar 

  10. Codish, M., Søndergaard, H.: Meta-circular abstract interpretation in prolog. In: Mogensen, T.Æ., Schmidt, D.A., Sudborough, I.H. (eds.) The Essence of Computation. LNCS, vol. 2566, pp. 109–134. Springer, Heidelberg (2002). https://doi.org/10.1007/3-540-36377-7_6

    Chapter  Google Scholar 

  11. Desouter, B., Van Dooren, M., Schrijvers, T.: Tabling as a library with delimited control. Theory Pract. Logic Program. 15(4–5), 419–433 (2015)

    Article  MathSciNet  Google Scholar 

  12. Felleisen, M.: Transliterating prolog into scheme. Technical report, 182, Indiana University Computer Science Department (1985)

    Google Scholar 

  13. Friedman, D.P., Byrd, W.E., Kiselyov, O.: The Reasoned Schemer. MIT Press, Cambridge (2005)

    Book  Google Scholar 

  14. Friedman, D.P., Byrd, W.E., Kiselyov, O., Hemann, J.: The Reasoned Schemer, 2nd edn. MIT Press, Cambridge (2018)

    Google Scholar 

  15. Gallaire, H., Lasserre, C.: Metalevel control of logic programs. In: Clark, K.L., Tärnlund, S.A. (eds.) Logic Programming, pp. 173–185. Academic Press, Cambridge (1982)

    Google Scholar 

  16. Gupta, G., Costa, V.S.: Cuts and side-effects in and-or parallel prolog. J. Logic Program. 27(1), 45–71 (1996)

    Article  MathSciNet  Google Scholar 

  17. Hanus, M.: Functional logic programming: from theory to curry. In: Voronkov, A., Weidenbach, C. (eds.) Programming Logics. LNCS, vol. 7797, pp. 123–168. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-37651-1_6

    Chapter  MATH  Google Scholar 

  18. Haynes, C.T.: Logic continuations. J. Log. Program. 4(2), 157–176 (1987)

    Article  Google Scholar 

  19. Hill, P.M., Lloyd, J.W.: The Gödel Programming Language. MIT Press, Cambridge (1994)

    MATH  Google Scholar 

  20. Hinze, R.: Prological features in a functional setting axioms and implementations. In: FLOPS (1998)

    Google Scholar 

  21. Kosarev, D., Boulytchev, D.: Typed embedding of relational language in OCaml. In: 2016 ML Family Workshop, September 2016

    Google Scholar 

  22. Lloyd, J.W.: Declarative programming in Escher. Technical report, CSTR-95-013, Department of Computer Science, University of Bristol, June 1995

    Google Scholar 

  23. Martens, B., Schreye, D.D.: Why untyped nonground metaprogramming is not (much of) a problem. J. Logic Program. 22(1), 47–99 (1995)

    Article  MathSciNet  Google Scholar 

  24. Martin-Martin, E.: Type classes in functional logic programming. In: PEPM (2011)

    Google Scholar 

  25. de Moura, L., Bjørner, N.: Z3: an efficient SMT solver. In: Ramakrishnan, C.R., Rehof, J. (eds.) TACAS 2008. LNCS, vol. 4963, pp. 337–340. Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-78800-3_24

    Chapter  Google Scholar 

  26. Naish, L.: Higher-order logic programming in Prolog. Technical report, 96/2, University of Melbourne (1996)

    Google Scholar 

  27. O’Keefe, R.A.: The Craft of Prolog. MIT Press, Cambridge (1990)

    Google Scholar 

  28. Oliveira, B.C., Moors, A., Odersky, M.: Type classes as objects and implicits. In: OOPSLA (2010)

    Article  Google Scholar 

  29. Robinson, J.A., Sibert, E.E.: LOGLISP: an alternative to PROLOG. In: Hayes, J., Michie, D., Pao, Y.H. (eds.) Machine Intelligence 10, pp. 399–419. Ellis Horwood Ltd., Chichester (1982)

    Google Scholar 

  30. Rompf, T.: The essence of multi-stage evaluation in LMS. In: Lindley, S., McBride, C., Trinder, P., Sannella, D. (eds.) A List of Successes That Can Change the World. LNCS, vol. 9600, pp. 318–335. Springer, Cham (2016). https://doi.org/10.1007/978-3-319-30936-1_17

    Chapter  Google Scholar 

  31. Rompf, T., Amin, N., Moors, A., Haller, P., Odersky, M.: Scala-virtualized: linguistic reuse for deep embeddings. Higher-Order Symb. Comput. 25(1), 165–207 (2013)

    Article  Google Scholar 

  32. Somogyi, Z., Henderson, F.J., Conway, T.C.: Mercury, an efficient purely declarative logic programming language. In: Proceedings of the Australian Computer Science Conference, pp. 499–512 (1995)

    Google Scholar 

  33. Spivey, J.M., Seres, S.: Embedding prolog in Haskell. In: Meijer, E. (ed.) Proceedings of the 1999 Haskell Workshop. Technical report UU-CS-1999-28, Department of Computer Science, University of Utrecht (1999)

    Google Scholar 

  34. Sterling, L., Shapiro, E.: The Art of Prolog, 2nd Edn. Advanced Programming Techniques. MIT Press, Cambridge (1994)

    Google Scholar 

  35. Sterling, L., Yalcinalp, L.U.: Explaining prolog based expert systems using a layered meta-interpreter. In: IJCAI 1989, pp. 66–71 (1989)

    Google Scholar 

  36. Warren, D.H.D.: Higher-order extensions to Prolog: are they needed? In: Hayes, J., Michie, D., Pao, Y.H. (eds.) Machine Intelligence 10, pp. 441–454. Ellis Horwood Ltd., Chichester (1982)

    Google Scholar 

  37. Warren, D.S.: Memoing for logic programs. Commun. ACM 35(3), 93–111 (1992)

    Article  Google Scholar 

Download references

Acknowledgments

Research reported in this publication was supported in part by the National Center For Advancing Translational Sciences of the National Institutes of Health under Award Number OT2TR002517, by DARPA under agreement number AFRLFA8750-15-2-0092, by NSF under Awards 1553471, 1564207, 1918483, by the Department of Energy under Award DE-SC0018050, as well as by gifts from Google, Facebook, and VMware. The views expressed are those of the authors and do not reflect the official policy or position of the National Institutes of Health, Department of Defense, Department of Energy, National Science Foundation, or the U.S. Government. The U.S. Government is authorized to reproduce and distribute reprints for Governmental purposes notwithstanding any copyright notation thereon.

Author information

Authors and Affiliations

Authors

Corresponding authors

Correspondence to Nada Amin , William E. Byrd or Tiark Rompf .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2019 Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Amin, N., Byrd, W.E., Rompf, T. (2019). Lightweight Functional Logic Meta-Programming. In: Lin, A. (eds) Programming Languages and Systems. APLAS 2019. Lecture Notes in Computer Science(), vol 11893. Springer, Cham. https://doi.org/10.1007/978-3-030-34175-6_12

Download citation

  • DOI: https://doi.org/10.1007/978-3-030-34175-6_12

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-030-34174-9

  • Online ISBN: 978-3-030-34175-6

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics