A Generic Functional Representation of Sorted Trees Supporting Attribution

(Haskell Can Do It)
  • Jean-Marie Gaillourdet
  • Patrick Michel
  • Arnd Poetzsch-Heffter
  • Nicole Rauch
Part of the Lecture Notes in Computer Science book series (LNCS, volume 7797)


Many important tasks and algorithms in computer science build on sorted trees. Typical examples are the translation of programs represented by syntax trees or the processing of (recursive) data structures following some XML schema. In purely functional programming, sorted trees are usually implemented by terms of recursive data types such that a term represents a tree node and the subterms represent its children. The drawback of this representation is that the context of a tree node is not accessible and has to be managed by different means, e.g., by additional arguments of the functions working on the tree.

In this paper, we present a pattern for the realization of sorted trees that overcomes this drawback. The technique is fully declarative. In contrast to competing patterns for trees such as Zippers, it supports pattern matching on the tree data structure. Functions on tree nodes can be used to express the decoration of trees by attribute values in a flexible way. In particular, links between tree nodes can easily be defined and attributions can be modularized into phases with clear interfaces. Techniques adapted from the “Scrap your boilerplate” approach allow for the high-level specification of contextual constraints. We show how our approach can be realized in Haskell and discuss tool support.


Type Class Tree Node Tree Representation Functional Programming Syntax Tree 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Augusteijn, L.: The Elegant Compiler Generator System. In: Deransart, P., Jourdan, M. (eds.) Attribute Grammars and their Applications. LNCS, vol. 461, pp. 238–254. Springer, Heidelberg (1990)CrossRefGoogle Scholar
  2. 2.
    Deransart, P., Jourdan, M., Lorho, B. (eds.): Attribute Grammars. LNCS, vol. 323. Springer, Heidelberg (1988)zbMATHGoogle Scholar
  3. 3.
    Ganzinger, H.: Increasing modularity and language-independency in automatically generated compilers. Science of Computer Programming (1983)Google Scholar
  4. 4.
    Ganzinger, H., Giegerich, R.: Attribute coupled grammars. In: SIGPLAN 1984 Symposium on Compiler Construction, SIGPLAN Notices, vol. 19(6), pp. 157–170. ACM Press (1984)Google Scholar
  5. 5.
    Ganzinger, H., Giegerich, R., Möncke, U., Wilhelm, R.: A truly generative semantics-directed compiler generator. In: SIGPLAN 1982: Proceedings of the 1982 SIGPLAN Symposium on Compiler Construction, pp. 172–184. ACM Press (1982)Google Scholar
  6. 6.
    Hedin, G.: An Overview of Door Attribute Grammars. In: Fritzson, P.A. (ed.) CC 1994. LNCS, vol. 786, pp. 31–51. Springer, Heidelberg (1994)CrossRefGoogle Scholar
  7. 7.
    Hendriks, P.R.H.: ASF system user’s guide. Technical Report CS-R8823, CWI (1988)Google Scholar
  8. 8.
    Hinze, R.: Memo functions, polytypically! In: Proceedings of the 2nd Workshop on Generic Programming, pp. 17–32 (2000)Google Scholar
  9. 9.
    Hinze, R., Jeuring, J.: Weaving a web. J. Funct. Program. 11(6), 681–689 (2001)MathSciNetCrossRefzbMATHGoogle Scholar
  10. 10.
    Huet, G.: The Zipper. Journal of Functional Programming 7(5), 549–554 (1997)MathSciNetCrossRefzbMATHGoogle Scholar
  11. 11.
    Johnson, T.: Attribute Grammars as a Functional Programming Paradigm. In: Kahn, G. (ed.) FPCA 1987. LNCS, vol. 274, pp. 154–173. Springer, Heidelberg (1987)CrossRefGoogle Scholar
  12. 12.
    Jourdan, M., Parigot, D.: The FNC-2 system: Advances in attribute grammars technology. Technical Report No. 834, INRIA (April 1988)Google Scholar
  13. 13.
    Kastens, U.: Attributed Grammars as a Specification Method. In: Alblas, H., Melichar, B. (eds.) SAGA School 1991. LNCS, vol. 545, pp. 16–47. Springer, Heidelberg (1991)CrossRefGoogle Scholar
  14. 14.
    Kastens, U., Hutt, B., Zimmermann, E. (eds.): GAG: A Practical Compiler Generator. LNCS, vol. 141. Springer, Heidelberg (1982)zbMATHGoogle Scholar
  15. 15.
    Knuth, D.E.: The semantics of context-free languages. Mathematical Systems Theory (1968)Google Scholar
  16. 16.
    Lämmel, R.: Scrap your boilerplate with XPath-like combinators. In: POPL 2007, Proceedings. ACM Press (January 2007)Google Scholar
  17. 17.
    Lämmel, R., Peyton Jones, S.: Scrap your boilerplate: a practical design pattern for generic programming. In: ACM SIGPLAN Notices, Proceedings of the ACM SIGPLAN Workshop on Types in Language Design and Implementation TLDI, vol. 38(3), pp. 26–37 (2003)Google Scholar
  18. 18.
    Lämmel, R., Peyton Jones, S.: Scrap more boilerplate: reflection, zips, and generalised casts. In: Proceedings of the ACM SIGPLAN International Conference on Functional Programming (ICFP 2004), pp. 244–255. ACM Press (2004)Google Scholar
  19. 19.
    Michel, P.: Adding position structures to Katja. Technical Report 353/06, University of Kaiserslautern (June 2005)Google Scholar
  20. 20.
    Peyton Jones, S., Marlow, S., Elliott, C.: Stretching the Storage Manager: Weak Pointers and Stable Names in Haskell. In: Koopman, P., Clack, C. (eds.) IFL 1999. LNCS, vol. 1868, pp. 37–58. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  21. 21.
    Poetzsch-Heffter, A.: Prototyping realistic programming languages based on formal specifications. Acta Informatica 34, 737–772 (1997)MathSciNetCrossRefzbMATHGoogle Scholar
  22. 22.
    Poetzsch-Heffter, A., Rauch, N.: Application and Formal Specification of Sorted Term-Position Algebras. In: Fiadeiro, J.L., Mosses, P.D., Yu, Y. (eds.) WADT 2004. LNCS, vol. 3423, pp. 201–217. Springer, Heidelberg (2005)CrossRefGoogle Scholar
  23. 23.
    Reps, T., Marceau, C., Teitelbaum, T.: Remote attribute updating for language-based editors. In: ACM-SIGPLAN ACM-SIGACT (ed.) Thirteenth Annual ACM Symposium on Principles of Programming Languages, pp. 1–13. ACM Press (January 1986)Google Scholar
  24. 24.
    Viera, M., Swierstra, S.D., Swierstra, W.: Attribute grammars fly first-class: how to do aspect oriented programming in haskell. In: Proceedings of the 14th ACM SIGPLAN International Conference on Functional Programming, ICFP 2009, pp. 245–256. ACM, New York (2009)Google Scholar
  25. 25.
    Vogt, H., Swierstra, S., Kuiper, M.: Higher order attribute grammars. In: SIGPLAN 1989 Conference on Progamming Language Design and Implementation, SIGPLAN Notices, vol. 24(7), pp. 131–145. ACM Press (1989)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2013

Authors and Affiliations

  • Jean-Marie Gaillourdet
    • 1
  • Patrick Michel
    • 1
  • Arnd Poetzsch-Heffter
    • 1
  • Nicole Rauch
    • 1
  1. 1.University of KaiserslauternGermany

Personalised recommendations