Skip to main content

Implementing the \(\lambda _{GT}\) Language: A Functional Language with Graphs as First-Class Data

  • Conference paper
  • First Online:
Graph Transformation (ICGT 2023)

Part of the book series: Lecture Notes in Computer Science ((LNCS,volume 13961))

Included in the following conference series:

  • 287 Accesses

Abstract

Several important data structures in programming are beyond trees; for example, difference lists, doubly-linked lists, skip lists, threaded trees, and leaf-linked trees. They can be abstracted into graphs (or more precisely, port hypergraphs). In existing imperative programming languages, these structures are handled with destructive assignments to heaps as opposed to a purely functional programming style. These low-level operations are prone to errors and not straightforward to verify. On the other hand, purely functional languages allow handling data structures declaratively with type safety. However, existing purely functional languages are incapable of dealing with data structures other than trees succinctly. In earlier work, we proposed a new purely functional language, \(\lambda _{GT}\), that handles graphs as immutable, first-class data structures with pattern matching and designed a new type system for the language. This paper presents a prototype implementation of the language constructed in about 500 lines of OCaml code. We believe this serves as a reference interpreter for further investigation, including the design of full-fledged languages based on \(\lambda _{GT}\).

J. Sano—Currently with NTT Laboratories.

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 54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 69.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

Notes

  1. 1.

    It may appear that the \(\overrightarrow{Y}\) of \((\lambda \, x [\overrightarrow{X}].e) (\overrightarrow{Y})\) does not play any role here. However, such a link becomes necessary when the \(\lambda \)-abstraction is made to appear in a data structure (e.g., as in \(\nu Y.(\textrm{Cons} (Y, W, Z), (\lambda \, x [\dots ].\dots ) (Y))\)). This is why \(\lambda \)-abstraction atoms are allowed to have argument links. Once such a function is accessed and \(\beta \)-reduction starts, the role of \(\overrightarrow{Y}\) ends, while the free links inside the abstraction atom start to play key roles.

  2. 2.

    The interpreter is available at https://github.com/sano-jin/lambda-gt-alpha/tree/icgt2023. The visualiser is available at https://sano-jin.github.io/lambda-gt-playground.

References

  1. Bak, C.: GP 2: efficient implementation of a graph programming language. Ph.D. thesis, Department of Computer Science, The University of York (2015), https://etheses.whiterose.ac.uk/12586/

  2. Bak, C., Faulkner, G., Plump, D., Runciman, C.: A reference interpreter for the graph programming language GP 2. In: Rensink, A., Zambon, E. (eds.) Proceedings Graphs as Models, GaM@ETAPS 2015. EPTCS, vol. 181, pp. 48–64 (2015). https://doi.org/10.4204/EPTCS.181.4

  3. Baldan, P., Bertolissi, C., Cirstea, H., Kirchner, C.: A rewriting calculus for cyclic higher-order term graphs. Math. Struct. Comput. Sci. 17(3), 363–406 (2007). https://doi.org/10.1017/S0960129507006093

    Article  MathSciNet  MATH  Google Scholar 

  4. Brus, T.. H.., van Eekelen, M.. C.. J.. D.., van Leer, M.. O.., Plasmeijer, M.. J..: Clean — a language for functional graph rewriting. In: Kahn, Gilles (ed.) FPCA 1987. LNCS, vol. 274, pp. 364–384. Springer, Heidelberg (1987). https://doi.org/10.1007/3-540-18317-5_20

    Chapter  Google Scholar 

  5. Cirstea, Horatiu, Kirchner, Claude, Liquori, Luigi: Matching power. In: Middeldorp, Aart (ed.) RTA 2001. LNCS, vol. 2051, pp. 77–92. Springer, Heidelberg (2001). https://doi.org/10.1007/3-540-45127-7_8

    Chapter  Google Scholar 

  6. Dietl, W., Müller, P.: Universes: lightweight ownership for JML. J. Object Technol. 4(8), 5–32 (2005). https://doi.org/10.5381/jot.2005.4.8.a1

    Article  Google Scholar 

  7. Ehrig, H., Ehrig, K., Prange, U., Taentzer, G.: Fundamentals of Algebraic Graph Transformation. Monographs in Theoretical Computer Science. An EATCS Series, Springer, Berlin (2006). https://doi.org/10.1007/3-540-31188-2

  8. Erwig, M.: Inductive graphs and functional graph algorithms. J. Funct. Program. 11(5), 467–492 (sep 2001). https://doi.org/10.1017/S0956796801004075

  9. Fernández, Maribel, Kirchner, Hélène., Mackie, Ian, Pinaud, Bruno: Visual modelling of complex systems: towards an abstract machine for PORGY. In: Beckmann, Arnold, Csuhaj-Varjú, Erzsébet, Meer, Klaus (eds.) CiE 2014. LNCS, vol. 8493, pp. 183–193. Springer, Cham (2014). https://doi.org/10.1007/978-3-319-08019-2_19

    Chapter  Google Scholar 

  10. Fernández, M., Pinaud, B.: Labelled port graph - a formal structure for models and computations. Electr. Notes Theor. Comput. Sci. 338, 3–21 (2018). https://doi.org/10.1016/j.entcs.2018.10.002

    Article  MathSciNet  MATH  Google Scholar 

  11. Fradet, P., Métayer, D.L.: Structured gamma. Sci. Comput. Program. 31(2), 263–289 (1998). https://doi.org/10.1016/S0167-6423(97)00023-3

    Article  MathSciNet  MATH  Google Scholar 

  12. Ghamarian, A., de Mol, M., Rensink, A., Zambon, E., Zimakova, M.: Modelling and analysis using GROOVE. Int. J. Softw. Tools Technol. Transfer 14(1), 15–40 (2012). https://doi.org/10.1007/s10009-011-0186-x

    Article  Google Scholar 

  13. Jakumeit, E., Buchwald, S., Kroll, M.: GrGen.NET: the expressive, convenient and fast graph rewrite system. Int. J. Softw. Tools Technol. Transfer 12(3), 263–271 (2010). https://doi.org/10.1007/s10009-010-0148-8

  14. Leroy, X., Doligez, D., Frisch, A., Garrigue, J., Rémy, D., Vouillon, J.: The OCaml system release 4.14. INRIA (2022)

    Google Scholar 

  15. Manning, G., Plump, D.: The GP programming system. In: Proceedings of Graph Transformation and Visual Modelling Techniques (GT-VMT 2008). Electronic Communications of the EASST, vol. 10 (2008). https://doi.org/10.14279/tuj.eceasst.10.150

  16. Matsuda, K., Asada, K.: A functional reformulation of UnCAL graph-transformations: or, graph transformation as graph reduction. In: Proceedings of POPL 1997. pp. 71–82. ACM (2017). https://doi.org/10.1145/3018882.3018883

  17. Oliveira, B.C., Cook, W.R.: Functional programming with structured graphs. SIGPLAN Not. 47(9), 77–88 (2012). https://doi.org/10.1145/2398856.2364541

    Article  MATH  Google Scholar 

  18. Rozenberg, G.: Handbook of Graph Grammars and Computing by Graph Transformation, Volume 1: Foundations. World Scientific (1997). https://doi.org/10.1142/3303

  19. Runge, O., Ermel, C., Taentzer, G.: AGG 2.0 - new features for specifying and analyzing algebraic graph transformations. In: Schürr, A., Varró, D., Varró, G. (eds.) Proc. AGTIVE 2011. LNCS, vol. 7233, pp. 81–88. Springer, Berlin, Heidelberg (2012). https://doi.org/10.1007/978-3-642-34176-2_8

  20. Sangiorgi, D., Walker, D.: The Pi-Calculus: A Theory of Mobile Processes. Cambridge University Press (2001)

    Google Scholar 

  21. Sano, J.: Implementing G-Machine in HyperLMNtal. Bachelor’s thesis, Waseda University (2021). https://doi.org/10.48550/arXiv.2103.14698

  22. Sano, J., Ueda, K.: Syntax-driven and compositional syntax and semantics of Hypergraph Transformation System (in Japanese). In: Proceedings of 38nd JSSST Annual Conference (JSSST 2021) (2021). https://jssst.or.jp/files/user/taikai/2021/papers/45-L.pdf

  23. Sano, J., Yamamoto, N., Ueda, K.: Type checking data structures more complex than trees. J. Inf. Process. 31, 112–130 (2023). https://doi.org/10.2197/ipsjjip.31.112

    Article  Google Scholar 

  24. Schürr, A., Winter, A.J., Zündorf, A.: The PROGRES approach: language and environment. In: Handbook of Graph Grammars and Computing by Graph Transformation: Volume 2: Applications, Languages and Tools, chap. 13, pp. 487–550. World Scientific (1997). https://doi.org/10.1142/9789812384720_0002

  25. Ueda, K.: LMNtal as a hierarchical logic programming language. Theoret. Comput. Sci. 410(46), 4784–4800 (2009). https://doi.org/10.1016/j.tcs.2009.07.043

    Article  MathSciNet  MATH  Google Scholar 

  26. Ueda, K.: Towards a substrate framework of computation. In: Concurrent Objects and Beyond, LNCS, vol. 8665, pp. 341–366. Springer (2014). https://doi.org/10.1007/978-3-662-44471-9_15

  27. Ueda, K., Ogawa, S.: HyperLMNtal: an extension of a hierarchical graph rewriting model. KI - Künstliche Intelligenz 26(1), 27–36 (2012). https://doi.org/10.1007/s13218-011-0162-3

    Article  Google Scholar 

  28. Weber, J.H.: Tool support for functional graph rewriting with persistent data structures - GrapeVine. In: Behr, N., Strüber, D. (eds.) Proc. ICGT 2022. LNCS, vol. 13349, pp. 195–206. Springer, Cham (2022). https://doi.org/10.1007/978-3-031-09843-7_11

Download references

Acknowledgements

We would like to thank the anonymous reviewers for their valuable comments. This work was supported by JSPS Grant-in-Aid for Scientific Research (23K11057).

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Jin Sano .

Editor information

Editors and Affiliations

Appendix

Appendix

Our implementation consists of about 500 lines of OCaml code as shown in Table 1.Footnote 2 This is about half the size of the reference interpreter of the graph transformation-based language GP 2 [1], which is about 1,000 lines of Haskell code [2].

Table 1. LOC of the interpreter.

Figure 7 gives the concrete syntax of the language we have implemented. Comments start at %. As usual, let defines a variable that has a value on the right-hand side of =, if \(n = 0\). If \(n \ge 1\), this defines a curried \(n\)-ary function. Recursive \(n\)-ary functions can be defined with let recs. We designed a syntax that is easy to parse and did not yet implement the term-notation abbreviation. We do not intend to provide a full-fledged language but to study the feasibility of the language with a concise implementation.

Fig. 7.
figure 7

Concrete syntax of \(\lambda _{GT}\).

The examples in Sect. 3 are concerned with difference lists, which are powerful and useful data structures, and we have shown the composition and the decomposition of these structures. However, the graphs and their operations in \(\lambda _{GT}\) are not limited to them.

Fig. 8.
figure 8

Map a function to the leaves of a leaf-linked tree.

Fig. 9.
figure 9

Leaf-linked trees.

Fig. 10.
figure 10

Dataflow graph (factorial of \(n\)).

For operations that cannot be performed by simply decomposing and composing graphs, we can use some atoms as markers for matching. For example, a map function that applies a function to the elements of the leaves of a leaf-linked tree can be expressed as Fig. 8. If is a successor function and we have bound the graph of Fig. 9 (a) to , evaluating the program will return the graph of Fig. 9 (b). Figure 11 shows the demo of the program. Figure 11 (a) is the view obtained by pressing the Run button after filling in the code. Figure 11 (b)–(g) shows each graph at the breakpoints, from which computation can be resumed with the Proceed button.

Fig. 11.
figure 11

Demo of mapping a function to the leaves of a leaf-linked tree.

\(\lambda _{GT}\) is capable of handling graphs that include functions. Figure 10 illustrates a dataflow graph that calculates the factorial of an input number. The graph has input and output links from and to the outside and functions at the nodes. There are two kinds of nodes; nodes with an input and an output, and a node with an input and two outputs (a branch). This can be written as Fig. 12, lines 2–28, in \(\lambda _{GT}\).

The evaluator of such a dataflow graph is shown in Fig. 12, lines 31–52. We use a marker M to trace the value on the flow (lines 53–54). Lines 33–37 deal with a node with an input and an output (N2). They apply the embedded function to the value. Lines 39–45 deal with a node with an input and two outputs (N3). They decide which way to go by applying the embedded function to the value. Line 47 terminates the evaluator when the marker is pointing at the output link _Out. We have run the code on our interpreter and checked that the interpreter returns {120(_Z)} (\(= 5!\)) after printing 14 intermediate graphs.

Fig. 12.
figure 12

Embedding a dataflow language in \(\lambda _{GT}\).

We can even build a larger dataflow from components. For example, we can compose the graph connecting the output and the input of them: {nu _X. (dataflow[_In,_X],dataflow[_X,_Out])}. Applying {run[_Z]} to {3(_Z)} and the graph returns {720(_Z)} (\(= (3!)!\)).

Rights and permissions

Reprints and permissions

Copyright information

© 2023 The Author(s), under exclusive license to Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Sano, J., Ueda, K. (2023). Implementing the \(\lambda _{GT}\) Language: A Functional Language with Graphs as First-Class Data. In: Fernández, M., Poskitt, C.M. (eds) Graph Transformation. ICGT 2023. Lecture Notes in Computer Science, vol 13961. Springer, Cham. https://doi.org/10.1007/978-3-031-36709-0_14

Download citation

  • DOI: https://doi.org/10.1007/978-3-031-36709-0_14

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-031-36708-3

  • Online ISBN: 978-3-031-36709-0

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics