Polytypic Syntax Tree Operations

  • Arjen van Weelden
  • Sjaak Smetsers
  • Rinus Plasmeijer
Part of the Lecture Notes in Computer Science book series (LNCS, volume 4015)


Polytypic functional programming has the advantage that it can derive code for generic functions automatically. However, it is not clear whether it is useful for anything other than the textbook examples, and the generated polytypic code is usually too slow for real-life programs. As a real-life test, we derive a polytypic parser for the Haskell 98 syntax and look into other front-end compiler syntax tree operations.

We present a types–as–grammar approach, which uses polytypic programming (in both Generic Haskell and Clean) to automatically derive the code for a parser based on the syntax tree type, without using external tools. Moreover, we show that using polytypic programming can even be useful for data–specific syntax tree operations in a (functional) compiler, such as scope checking and type inference.

Simple speed tests show that the performance of polytypic parsers can be abominable for real-life inputs. However, we show that much performance can be recovered by applying (extended) fusion optimization on the generated code. We actually have a derived parser whose speed is close to one generated by a specialized Haskell parser generator.


Type Inference Syntax Tree Functional Language External Tool Fusion Optimization 
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.
    Löh, A., Clarke, D., Jeuring, J.: Dependency-style Generic Haskell. In: Proceedings of the eighth ACM SIGPLAN International Conference on Functional Programming ICFP 2003, pp. 141–152. ACM Press, New York (2003)CrossRefGoogle Scholar
  2. 2.
    Peyton Jones, S.: Haskell 98 language and libraries: the Revised Report. Cambridge University Press, Cambridge (2003), Google Scholar
  3. 3.
    Alimarine, A., Plasmeijer, R.: A Generic Programming Extension for Clean. In: Arts, T., Mohnen, M. (eds.) IFL 2002. LNCS, vol. 2312, pp. 168–186. Springer, Heidelberg (2002)CrossRefGoogle Scholar
  4. 4.
    Jansson, P., Jeuring, J.: Polytypic compact printing and parsing. In: Swierstra, S.D. (ed.) ESOP 1999. LNCS, vol. 1576, pp. 273–287. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  5. 5.
    Jansson, P., Jeuring, J.: Polytypic data conversion programs. Science of Computer Programming 43(1), 35–75 (2002)MATHCrossRefMathSciNetGoogle Scholar
  6. 6.
    van Weelden, A., Plasmeijer, R.: A functional shell that dynamically combines compiled code. In: Trinder, P., Michaelson, G.J., Peña, R. (eds.) IFL 2003. LNCS, vol. 3145, pp. 36–52. Springer, Heidelberg (2004)CrossRefGoogle Scholar
  7. 7.
    Alimarine, A., Smetsers, S.: Improved fusion for optimizing generics. In: Hermenegildo, M.V., Cabeza, D. (eds.) PADL 2004. LNCS, vol. 3350, pp. 203–218. Springer, Heidelberg (2005)CrossRefGoogle Scholar
  8. 8.
    Hinze, R.: Generic Programs and proofs, Habilitationsschrift, Universität Bonn (2000)Google Scholar
  9. 9.
    van Wijngaarden, A.: Orthogonal design and description of a formal language. Technical Report MR 76, Mathematisch Centrum, Amsterdam (1965)Google Scholar
  10. 10.
    Baars, A.I., Swierstra, S.D.: Type-safe, self inspecting code. In: Proceedings of the ACM SIGPLAN workshop on Haskell, pp. 69–79. ACM Press, New York (2004)CrossRefGoogle Scholar
  11. 11.
    Koopman, P., Plasmeijer, R.: Layered Combinator Parsers with a Unique State. In: Arts, T., Mohnen, M. (eds.) Proceedings of the 13th International workshop on the Implementation of Functional Languages, IFL 2001, Ericsson Computer Science Laboratory, pp. 157–172 (2001)Google Scholar
  12. 12.
    Leijen, D., Meijer, E.: Parsec: Direct style monadic parser combinators for the real world. Technical Report UU-CS-2001-35, Departement of Computer Science, Universiteit Utrecht (2001),
  13. 13.
    Gill, A., Marlow, S.: Happy: The parser generator for Haskell (2001),
  14. 14.
    Hinze, R., Peyton Jones, S.: Derivable Type Classes. In: Hutton, G. (ed.) 2000 ACM SIGPLAN Haskell Workshop, Montreal, Canada. ENTCS, vol. 41(1). Elsevier Science, Amsterdam (2001)Google Scholar
  15. 15.
    Atanassow, F., Clarke, D., Jeuring, J.: Scripting XML with Generic Haskell. Technical report uu-cs-2003, University of Utrecht (2003)Google Scholar
  16. 16.
    Hutton, G.: Higher-order Functions for Parsing. Journal of Functional Programming 2(3), 323–343 (1992)MATHCrossRefMathSciNetGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2006

Authors and Affiliations

  • Arjen van Weelden
    • 1
  • Sjaak Smetsers
    • 1
  • Rinus Plasmeijer
    • 1
  1. 1.Institute for Computing and Information SciencesRadboud University NijmegenThe Netherlands

Personalised recommendations