• Aucun résultat trouvé

Predicate abstraction in Denali

2.5 Dening multi-valued modes

2.5 Dening multi-valued modes

In this section we describe a syntactic method for dening moded bases and modes. It is not expressively complete; that is, it cannot be used to dene all possible combinations of moded bases and modes. When used within its limitations, however, it is an eective and easily implemented facility.

The techniqueinvolvesgiving, for each sort, a set of mode signatures. These signatures specify the relationship between the modes of the subterms of a term and the mode of the term. Any number of modes may be dened for each sort, but one of them must be any. By convention, variables and all other moded terms are always of mode any.

This convention is observed for the following reason. The mode signature technique indirectly denes the moded base since it is the union of the modeany of each sort. By denition, the moded base must contain all variable terms.

As a rst example, consider the three modes fornats that were dened informally in Section 2.3.1. If we rename the mode all to be any, they can be specied as follows.

0:!gnd s: gnd!gnd

0:!nonVar s: nonVar !nonVar

0:!any s: any !any

There is often an inclusion relationship between modes of the same sort. In the example above, for instance, any contains nonVar, which in turn contains gnd. By explicitly noting the inclusion relationships as part of a mode name presentation, shorter and clearer presentations can be constructed. For example, the mode presentation above can be recast as:

any>nonVar >gnd 0:!gnd

s: gnd!gnd

s: nonVar!nonVar s: any!any

Since 0 is always of mode gnd, the inclusion relationships let us conclude that it is also always of modesnonVar and any.

Let P be a set of mode signatures, and let > be an inclusion ordering. If t is a variable, then M(t) if and only if

48 2. Predicate abstraction in Denali

M =any.

If t is the non-variable term f(t1;:::;tn), thenM(t) if and only if

9f:N1;:::;Nn!N 2P such that 8iNi(ti) and M N.

The mode any must include all others in an inclusion ordering. Under the denition above, this ensures that all moded terms are of mode any.

In the preceding example, the modeany contains all terms of sortnat. Recall that this was not the case with the alternate modings fornat that we constructed in Section 2.3.2.

They can be specied by:

any>gnd 0:!gnd s: gnd!gnd

Heres(X) is unmoded because no mode signature applies to it.

Our nal example is a presentation for the three modes of list. any>enum>gnd

nil: !gnd

cons: gnd;gnd!gnd cons: any;enum!enum cons: any;any !any

Notice that these mode signatures incorporate the modes for nats, and that the mode names are overloaded.

A complete specication of the modes of a program includes a mode presentation for each sort. The set of terms associated by a presentation with a mode name is always a mode, and the union of each of the modes any is always a moded base. When we extend the denitions of moded bases and modes in Chapter 5 to account for equality constraints, this will no longer be the case. At that time we will dene a syntactic check upon mode presentations that gives a partial guarantee of this property.

Our mode denition technique is not suciently powerful to describe arbitrary modes.

For example, the mode that contains exactly the prime numbers cannot be dened using mode signatures. While it is not a complete technique, the advantages of the mode signature technique are its simplicity and ease of checking.

2.6. Related work 49

2.6 Related work

Bi-valued modes were rst introduced by Warren as declarations for a Prolog compiler [Warren 77]. The interfaces of predicates can be annotated with bi-valued modes, per-mitting the Edinburgh Prolog compiler to optimize the compiled code. As long as they are correct, the annotations have no other eect. In fact, the annotations are ignored when uncompiled clauses are interpreted directly.

Bi-valued moding schemeshave appeared in a number of languages designed as succes-sors to Prolog, including Epilog [Porto 82] and Mu-Prolog [Naish 85]. In these languages, the modes are used to control the selection rule as described in this section.

Variants of bi-valued modes have also appeared in logic languages designed for con-current applications. Modes are used in these languages to control the parallel solution of literals possessing shared variables. In such languages, the literals of a query are evaluated in parallel, with the evaluation of ill-moded literals suspended until they are suciently instantiated. This provides a simple form of concurrency control. The mode mechanism of Parlog [Clark 85] is of the bi-valued variety that we have described. In Concurrent Prolog [Shapiro 83], the mode restrictions are attached to the point of call rather than to the point of denition. The same eect is achieved, and some additional exibility is obtained.

Modes have also been used to plan the evaluation of queries. [Dembinski 85] describes a scheme that exploits bi-valued mode declarations to perform intelligent backtracking in a Prolog-like interpreter. In the database language NAIL! [Ullman 85], bi-valued modes are combined with capture rules to preplan the evaluation of database queries. This approach is based on the assumption that the data values are either variable or ground, which makes its use of bi-valued modes entirely appropriate.

3

Equational unication