19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

expr<br />

∑<br />

type wrapper<br />

∑<br />

body R, ⊞<br />

e R<br />

e R, +<br />

gs<br />

lv := e,gs () noReduction lv := e NoReduction, ⊕<br />

〈 e | gs 〉 ListE closeList singletonOpen(e) OpenList, ++<br />

Figure 32.2: Desugaring of expressions with generators. Top to bottom: big operators (here ∑ is used as an example;<br />

the appropriate library function is called on the right-hand side), assignments, and comprehensions (here list<br />

comprehensions are shown; with the exception of array comprehensions, other comprehensions are similar to list<br />

comprehensions).<br />

provided GeneratorList into a nested series of calls to generate . For example, when we perform the desugaring<br />

T Z64, +[x ← xs, y ← ys, x ≠ y](x · y) we obtain the following code:<br />

xs.generateZ64, +(fn x ⇒<br />

ys.generateZ64, +(fn y ⇒<br />

if x ≠ y then (x · y) else Identity+ end))<br />

Some example desugarings of expressions with generators are shown in Figure 32.2 for big operators, assignments,<br />

and list comprehensions (set and multiset comprehensions are similar to list comprehensions).<br />

<strong>The</strong> simplest desugarings are the ones for big operators such as ∑ . <strong>The</strong> type of the traversal corresponds to the type<br />

of the result. <strong>The</strong> body expression used is exactly the body expression of the big operator. <strong>The</strong> wrapper function is<br />

named by the big operator itself. For example, the ∑ operator has the following declaration:<br />

opr ∑ R extends CommutativeMonoidR, +(rhs : () → R): R = rhs()<br />

Assignments desugar in a manner similar to big operators. However, they make use of the special NoReduction type,<br />

which is a singleton type which extends CommutativeMonoidNoReduction, ⊕ . We can think of NoReduction<br />

as composing the writes of the assignments in parallel. <strong>The</strong> wrapper noReduction is defined as follows:<br />

noReduction(rhs : () → NoReduction): () = do<br />

rhs()<br />

()<br />

end<br />

Lists also desugar in a similar way. <strong>The</strong> desugaring given in Figure 32.2 makes use of the OpenList type—such a list<br />

is constructed with an updatable tail cell, permitting partially-constructed lists to be appended in constant time using<br />

the ++ (DOUBLE PLUS) operator. <strong>The</strong> closeList operation converts the result into an ordinary non-updatable list.<br />

An array comprehension simply desugars into a factory function call and a series of assignments:<br />

[ i 1 = e 1 | gs 1<br />

i 2 = e 2 | gs 2<br />

. . .<br />

i n = e n | gs n ]<br />

−→<br />

do a = array()<br />

a[i 1 ] := e 1 , gs 1<br />

a[i 2 ] := e 2 , gs 2<br />

. . .<br />

a[i n ] := e n ,gs n<br />

a<br />

end<br />

<strong>The</strong> desugaring of a for loop depends upon the set of reduction variables. We conceptually desugar the for loop<br />

with reduction variables, r 1 , r 2 , . . ., r n , reduced using the reduction operator ⊕ for type (T 1 , T 2 , . . . T n ) as follows:<br />

for gs do block end<br />

−→<br />

223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!