Monoid comprehensions
Keywords: main

Next: The monoid comprehension calculus Up: Monoid homomorphisms and comprehensions Previous: Monoid homomorphisms

###
Monoid comprehensions

The concept of monoid homomorphism is useful for expressing a formal semantics of iteration over collections. However, it is not very convenient as a programming construct. A natural notation for such a construct that is both conspicuous and can be expressed in terms of monoid homomorphisms is a monoid comprehension. This notion generalizes the familiar notation used for writing a set in comprehension (as opposed to writing it in extension) using a pattern and a formula describing its elements (as oppposed to listing all its elements). For example, the set comprehension describes the set of pairs (the pattern), verifying the formula (the qualifier).

This notation can be extended to any (primitive or collection) monoid . The syntax of a monoid comprehension is an expression of the form where e is an expression called the head of the comprehension, and Q is called its qualifier and is a sequence , , where each qi is either

- a generator of the form , where x is a variable and e is an expression; or,

- a filter which is a boolean condition.

In a monoid comprehension expression , the monoid operation is called the accumulator.

As for semantics, the meaning of a monoid comprehension is defined in terms of monoid homomorphisms.

DEFINITION 4.2 (Monoid Comprehension) The meaning of a monoid comprehension over a monoid is defined inductively as follows:

such that , , and is a collection monoid.

Note that although the input monoid is explicit, each generator in the qualifier has an implicit collection monoid whose characteristics can be inferred with polymorphic typing rules.

Although Definition 4.2 can be effectively computed using nested loops (i.e., using the iteration semantics (1)), such would be in general rather inefficient. Rather, an optimized implementation can be achieved by various syntactic transformation expressed as rewrite rules. Thus, the principal benefit of using monoid comprehensions is to formulate efficient optimizations on a simple and uniform general syntax of expressions irrespective of specific monoids.

Note that relational joins are immediately expressible as monoid comprehensions. Indeed, the join of two sets S and T using a function f and a predicate p is simply:

| (2)

Typically, a relational join will take f to be a record constructor. For example, if we write a record whose fields have values ei for , as , then a standard relational join is obtained with, say, , and p(x,y) may be any condition such as .

Clearly, monoid comprehensions can immediately express queries using all usual relational operators (and, indeed, object queries as well) and most usual functions. For example,

Note that some of these functions will work only on appropriate types of their arguments. For example, the type of the argument of must be a non-idempotent monoid, and so must the type of the second argument of . Thus, will add up the elements of a bag or a list, and will tally the number of occurrences of an element in a bag or a list. Applying either or to a set will be caught as a type error.

Next: The monoid comprehension calculus Up: Monoid homomorphisms and comprehensions Previous: Monoid homomorphisms