• Aucun résultat trouvé

Translating denotations to representations

modes any

7.3 Translating denotations to representations

The only portion of the conversion between denotation and representation form pro-grams that we have not dened is the translation function Tliteral. This function expects as an argument a literal whose terms are written as denotations, and returns a literal whose terms are written as representations. In the two forms the terms should stand for equivalent abstract objects, where equivalence is dened by the translation predicate

7.3. Translating denotations to representations 153 that appears in each explicitly implemented cluster.

Before proceeding with the denition of Tliteral, we establish the idea behind it by working through an example. Consider the abstract set object that contains the natural numbers 1 and 2. One of its denotations is the set terminsert(0;cons(1;empty)), and one of its representations is thebag termcons(0;cons(1;empty)). We could, in principle, use this second term to stand for the set in a representation form program. Because of overloading, however, we would be unable to tell whether a term in representation form was a set or a bag.

We take a dierent approach instead. We use the termAset(cons(0;cons(1;empty))) to stand for thesetdiscussed above. The abstracting function symbolAset is a syntactic coercion that converts representations of abstract objects to the abstract sort. The translation process involves using the translation predicates to nd the representations that correspond to denotations, and then applying abstracting functions as necessary to preserve sort correctness.

The translation is done beginning at the inside of the term and moving out. The rst step in translating the term insert(0;cons(1;empty)), for example, is to translate the innermost termempty. To do this, it is necessary to regard empty not as a term of sort set, but as a term of sort set dnt, because the translation function is written to expect arguments of sort set dnt. This is possible because of the deliberate overloading of the constructor symbols between set and set dnt.

The set translation predicate setTrans maps empty to the bag term nil. (The nat term 1, which is also an innermost term, requires no translation as it is of an implic-itly implemented sort.) We replace the innermost terms with their representations and consider the next term out, which is now cons(1;nil). With its subterms having been replaced with representations, this term is now a well-sortedset dnt term. Consequently, we can use setTrans to translate it to its representation,insert(0;cons(1;nil)).

Continuing in this fashion for one more step, we obtain the representation version of the original abstract object. Following the application of the abstracting function symbol, the translation is complete. It is possible for abstracting functions to be nested;

this poses no problem.

154 7. Semantics of Denali Although we began by posing the problem of translating literals, we have proceeded by considering the problem of translating terms. This is because the function Tliteral

does nothing more than use a subsidiary function to translate each of the terms in its argument literal.

Tliteral(P(t1;:::;tn)) = P(^t1;:::;^tn)

where ^ti = Tterm(ti;Si) andsig(P) = (Si;:::;Sn) It is the function Tterm that does the term translation.

In the remainder of this section we will describe more rigorously the translation process in general and Tterm in particular. In Section 7.3.1 we show how Tterm can be dened over ground terms, and then in Section 7.3.2 we show howTterm can be extended to cope with variable-containing terms. Finally, in Section 7.3.3, we describe how this translation process can be used to incrementally transform a program that itself contains the translation predicates.

7.3.1 Translating ground terms

The function Tterm takes two arguments. The rst is the denotation t of an abstract object, and the second is the sort S of that term. (This second argument is necessary because of overloading.) Tterm translates t into a representation form term of the same sort.

Tterm(t;S) =

( ^t; if implicit(S) AS(^t); if explicit(S) where ^t = TS(t;S)

There are two cases, depending upon whether the S is implicitly or explicitly imple-mented. In both cases, the function TS is invoked to perform the translation. In the latter case, the abstracting function symbol AS is used to enclose the representation as discussed earlier.

There is one instance of the function TS for every sort S. There are two explicit arguments. The rst is the denotation t of an abstract object, and the second is the sort

R of t. The implicit argument S is the sort of the term in which t is embedded. Tterm

translates t into a representation oft.

7.3. Translating denotations to representations 155 TS(f(t1;:::;tn);R) =

( Tterm(f(t1;:::;tn);R); if S6= R translateS(f(^t1;:::;^tn)); otherwise where ^ti = TS(ti;Ri) and sig(f;R) = (R1;:::;Rn !R)

There are two cases. If R and S are not the same, then t is not the same sort as its parent term. Since the translation of t must be embedded in an abstracting function symbol, the function Tterm is recursively invoked in this case. If R and S are the same, the subterms are recursively translated and the resulting term is then translated by the function translateS.

IfSis an implicitsort, then the functiontranslateSis the identityfunction. Otherwise, the function application translateS(t) is reduced as follows. The query

translateS(t;V)

is formed and solved to obtain a substitution . Here, translateS is the translation predicate from the sortSand V is a fresh variable. The result of the function application is V.

7.3.2 Translating general terms

The denitions of TS and translateS are given above only for ground terms. In this section we show how to extend the translation functions to deal with variables and variable-containing terms. We rst discuss why variables must be treated as a special case, and then give the extensions.

In principle, a set variable, such asS, should be translated to an abstracted variable, such as Aset(B), whereB is abag variable. It is important to ensure that all occurrences of a variable in a program are translated to the same abstracted variable. We ensure this by reusing the variable names of an abstract sort in a denotation form program as the variable names of the representation sort in the converted program.

Thus, the denotation variable S is translated to the representation variable Aset(S).

There is no danger in reinterpretingS as abag variable since all occurrences of S will be translated. We now see how this is accomplished.

The denition of TS can be extended to account for variables as follows. If S is an implicit sort, then

156 7. Semantics of Denali TS(v;R) = v

because such a variable requires no translation. For explicitly implemented sorts S, TS(v;R) = Tterm(v;rep(R))

By invokingTterm, we ensure that the proper abstracting function is wrapped around the variable.

The diculty encountered bytranslateS when applied to terms that contain variables is more subtle. Consider the solution of the query

translateset(insert(N;nil));V).

The substitution that we would like to obtain is hV=cons(N;nili, which is the most general possible solution. However, the substitution hV=cons(1;nil)i, with the variable

N instantiated to 1, is also a solution.

When solving a translation query, we must require that the variables of the term being translated be treated as uninterpreted constants rather than as variables. With this restriction, we avoid the possibility of the program variables being accidentally, and unnecessarily, instantiated.

7.3.3 Translation paradigm

In our discussion of the translation process, we have assumed that the literals and terms being translated are separate from the program that denes the translation predicates.

When translating a Denali program, this will not be the case.

Because of this, Denali programs must be translated incrementally. Before a term of an explicit sort S can be converted to representation form, the translation predicate for

S must itself be translated. This will always be possible so long as terms of a sort S dnt are not permitted to contain objects of sort S. This is in fact a restriction of Denali.