Passing an algebra to the parser

7.5 Passing an algebra to the parser

  The previous section shows how to implement a parser with an implicit algebra. Since this approach fails when we want to define different parsers with the same result type, we make the algebras explicit. Thus we obtain the following definition of parens:

  parens

  :: ParenthesesAlgebra a -> Parser Char a

  parens (match,empty) = par where

  par = (\_ b _ d -> match b d) <>

  open <> par <> close <> par <|> succeed empty

  Note that it is now easy to define different parsers with the same result type:

  nesting, breadth :: Parser Char Int nesting

  = parens (\b d -> max (1+b) d,0)

  breadth

  = parens (\b d -> d+1,0)

  Chapter 8

  Programming with higher-order folds

  introduction

  In the previous chapters we have seen that algebras play an important role when describing the meaning of a recognised structure (a parse tree). For each recursive datatype T we have a function foldT, and for each constructor of the datatype we have a corresponding function as a component in the algebra. Chapter 6 introduces

  a language in which local declarations are permitted. Evaluating expressions in this language can be done by choosing an appropriate algebra. The domain of that algebra is a higher order (data)type (a (data)type that contains functions). Unfortunately, the resulting code comes as a surprise to many. In this chapter we will illustrate a related formalism, which will make it easier to construct such involved algebras. This related formalism is the attribute grammar formalism. We will not formally define attribute grammars, but instead illustrate the formalism with some examples, and give an informal definition.

  We start with developing a somewhat unconventional way of looking at functional programs, and especially those programs that use functions that recursively descend over datatypes a lot. In our case one may think about these datatypes as abstract syntax trees. When computing a property of such a recursive object (for example, a program) we define two sets of functions: one set that describes how to recursively visit the nodes of the tree, and one set of functions (an algebra) that describes what to compute at each node when visited.

  One of the most important steps in this process is deciding what the carrier type of the algebras is going to be. Once this step has been taken, these types are

  a guideline for further design steps. We will see that such carrier types may be functions themselves, and that deciding on the type of such functions may not always

  be simple. In this chapter we will present a view on recursive computations that will enable us to “design” the carrier type in an incremental way. We will do so by constructing algebras out of other algebras. In this way we define the meaning of a language in a semantically compositional way.

  We will start with the rep min example, which looks a bit artificial, and deals with

  a non-interesting, highly specific problem. However, it has been chosen for its sim- plicity, and to not distract our attention to specific, programming language related, semantic issues. The second example of this chapter demonstrates the techniques on a larger example: a small compiler for part of a programming language.

  Programming with higher-order folds

  data Tree = Leaf Int

  | Bin Tree Tree deriving Show

  type TreeAlgebra a = (Int -> a, a -> a -> a) foldTree :: TreeAlgebra a -> Tree -> a

  foldTree alg(leaf, _ ) (Leaf i)

  = leaf i

  foldTree alg(_

  , bin) (Bin l r) = bin (foldTree alg l)

  (foldTree alg r)

  Listing 9: rm.start.hs

  goals

  In this chapter you will learn:

  • how to write ‘circular’ functional programs, or ‘higher-order folds’; • how to combine algebras; • (informally) the concept of an attribute grammar.

Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

ANTARA IDEALISME DAN KENYATAAN: KEBIJAKAN PENDIDIKAN TIONGHOA PERANAKAN DI SURABAYA PADA MASA PENDUDUKAN JEPANG TAHUN 1942-1945 Between Idealism and Reality: Education Policy of Chinese in Surabaya in the Japanese Era at 1942-1945)

1 29 9

Improving the Eighth Year Students' Tense Achievement and Active Participation by Giving Positive Reinforcement at SMPN 1 Silo in the 2013/2014 Academic Year

7 202 3

Improving the VIII-B Students' listening comprehension ability through note taking and partial dictation techniques at SMPN 3 Jember in the 2006/2007 Academic Year -

0 63 87

The Correlation between students vocabulary master and reading comprehension

16 145 49

The correlation intelligence quatient (IQ) and studenst achievement in learning english : a correlational study on tenth grade of man 19 jakarta

0 57 61

An analysis of moral values through the rewards and punishments on the script of The chronicles of Narnia : The Lion, the witch, and the wardrobe

1 59 47

Improping student's reading comprehension of descriptive text through textual teaching and learning (CTL)

8 140 133

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37

Transmission of Greek and Arabic Veteri

0 1 22