Lightweight Functional Logic Meta-Programming
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.
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.
- 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
- 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 2009Google 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
- 9.Claessen, K., Ljunglöf, P.: Typed logical variables in Haskell. Electr. Notes Theor. Comput. Sci. 41(1), 37 (2000). Haskell WorkshopGoogle Scholar
- 12.Felleisen, M.: Transliterating prolog into scheme. Technical report, 182, Indiana University Computer Science Department (1985)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
- 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 2016Google Scholar
- 22.Lloyd, J.W.: Declarative programming in Escher. Technical report, CSTR-95-013, Department of Computer Science, University of Bristol, June 1995Google Scholar
- 24.Martin-Martin, E.: Type classes in functional logic programming. In: PEPM (2011)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
- 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
- 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