Lightweight Functional Logic Meta-Programming

  • Nada AminEmail author
  • William E. ByrdEmail author
  • Tiark RompfEmail author
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 11893)


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. 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. 2.
    Barrett, C., et al.: CVC4. In: Gopalakrishnan, G., Qadeer, S. (eds.) CAV 2011. LNCS, vol. 6806, pp. 171–177. Springer, Heidelberg (2011). Scholar
  3. 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. 4.
    Byrd, W.E.: Relational programming in miniKanren: techniques, applications, and implementations. Ph.D. thesis, Indiana University, September 2009Google Scholar
  5. 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)CrossRefGoogle Scholar
  6. 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. 7.
    Carro, M., de Guzmàn, P.C.: Tabled logic programming and its applications (2011).
  8. 8.
    Chen, W., Kifer, M., Warren, D.S.: HiLog: a foundation for higher-order logic programming. J. Log. Program. 15(3), 187–230 (1993)MathSciNetCrossRefGoogle Scholar
  9. 9.
    Claessen, K., Ljunglöf, P.: Typed logical variables in Haskell. Electr. Notes Theor. Comput. Sci. 41(1), 37 (2000). Haskell WorkshopGoogle Scholar
  10. 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). Scholar
  11. 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)MathSciNetCrossRefGoogle Scholar
  12. 12.
    Felleisen, M.: Transliterating prolog into scheme. Technical report, 182, Indiana University Computer Science Department (1985)Google Scholar
  13. 13.
    Friedman, D.P., Byrd, W.E., Kiselyov, O.: The Reasoned Schemer. MIT Press, Cambridge (2005)CrossRefGoogle Scholar
  14. 14.
    Friedman, D.P., Byrd, W.E., Kiselyov, O., Hemann, J.: The Reasoned Schemer, 2nd edn. MIT Press, Cambridge (2018)Google Scholar
  15. 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. 16.
    Gupta, G., Costa, V.S.: Cuts and side-effects in and-or parallel prolog. J. Logic Program. 27(1), 45–71 (1996)MathSciNetCrossRefGoogle Scholar
  17. 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). Scholar
  18. 18.
    Haynes, C.T.: Logic continuations. J. Log. Program. 4(2), 157–176 (1987)CrossRefGoogle Scholar
  19. 19.
    Hill, P.M., Lloyd, J.W.: The Gödel Programming Language. MIT Press, Cambridge (1994)zbMATHGoogle Scholar
  20. 20.
    Hinze, R.: Prological features in a functional setting axioms and implementations. In: FLOPS (1998)Google Scholar
  21. 21.
    Kosarev, D., Boulytchev, D.: Typed embedding of relational language in OCaml. In: 2016 ML Family Workshop, September 2016Google Scholar
  22. 22.
    Lloyd, J.W.: Declarative programming in Escher. Technical report, CSTR-95-013, Department of Computer Science, University of Bristol, June 1995Google Scholar
  23. 23.
    Martens, B., Schreye, D.D.: Why untyped nonground metaprogramming is not (much of) a problem. J. Logic Program. 22(1), 47–99 (1995)MathSciNetCrossRefGoogle Scholar
  24. 24.
    Martin-Martin, E.: Type classes in functional logic programming. In: PEPM (2011)Google Scholar
  25. 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). Scholar
  26. 26.
    Naish, L.: Higher-order logic programming in Prolog. Technical report, 96/2, University of Melbourne (1996)Google Scholar
  27. 27.
    O’Keefe, R.A.: The Craft of Prolog. MIT Press, Cambridge (1990)Google Scholar
  28. 28.
    Oliveira, B.C., Moors, A., Odersky, M.: Type classes as objects and implicits. In: OOPSLA (2010)CrossRefGoogle Scholar
  29. 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. 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). Scholar
  31. 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)CrossRefGoogle Scholar
  32. 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. 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. 34.
    Sterling, L., Shapiro, E.: The Art of Prolog, 2nd Edn. Advanced Programming Techniques. MIT Press, Cambridge (1994)Google Scholar
  35. 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. 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. 37.
    Warren, D.S.: Memoing for logic programs. Commun. ACM 35(3), 93–111 (1992)CrossRefGoogle Scholar

Copyright information

© Springer Nature Switzerland AG 2019

Authors and Affiliations

  1. 1.Harvard UniversityCambridgeUSA
  2. 2.University of Alabama at BirminghamBirminghamUSA
  3. 3.Purdue UniversityWest LafayetteUSA

Personalised recommendations