• No results found

2.4 Advances over Previous Amortised Analysis Techniques

4.3.3 Circular Data Structures

The rules for sum and list types in the memory consistency Definition 4.9 on page 54 do not allow the location ` to be reused within their premises. It is important to note that

H

\` is quite different from

H

[` 7→ Bad], since the latter allows ` to be consistent with list and sum types (i.e. any boxed type), while the former implies that ` is never consistent with any type. Of course, it is still possible that a location of a sum or list type occurs multiple times in the derivation for memory consistency, due to the branching of the derivation tree that occurs for pair types. Hence, the imposed requirement is only that the digraph representing the memory layout must be acyclic. Or in other words, aliased data is allowed as long as there are no infinite cycles, like a circular list. Program Example 4.4.1 will show us how acyclic aliased data can be created and analysed inLF, by constructing lists which share their tails.

The reason for excluding circular data structures in the definition of consistent mem- ory configurations is simplicity: all derivations of memory consistency become finite. This is justified by the fact that circular data structures cannot be constructed within

LF, due to the absence of both recursive let definitions and imperative update. Since

memory consistency is meant to be an invariant for evaluation, we thus record the property that circular data cannot be produced therein.

Circular data structures arenot a problem for the amortised analysis technique per se. We have already shown how to treat circular data that may either be created through imperative update in [HJ06], or through general recursive let-definition in [JLHH10]. Furthermore, the corresponding definition of memory consistency for the higher- order system in Chapter 6 does allow cyclic data structures. While our higher- order programs cannot create cyclic data structures either, the higher-order analysis requires a coinductive definition for memory consistency regardless, so there would be no gain in regarding memory configurations containing cyclic data as inconsistent. We could hence equally allow cyclic data inLFat the expense of using a coinductive definition for memory consistency. Since allowing cyclic data is independent from the higher-order extension, let us briefly discuss the implications of dealing with cyclic

data at this point, as if it were allowed for LF, instead of deferring this discussion

to Chapter 6 or later. Of course, we only have proof that the following observations hold for the higher-order system Arthur, but since LF is practically a subset of Arthur, we strongly believe them to be true anyway.

It is conceivable that anLFprogram receives circular data through its input. Recur-

sion over a cyclic data is possibly infinite, so the resource consumption may suddenly become infinite as well. However, the program itself has not changed, and thus the result of the analysis still applies regardless of the input supplied.

Recall from Section 2.3 that our analysis will determine a rational number for each input, and that it guarantees that the resource usage during the execution of the program is smaller than the sum of all potentials, where the potential of a certain input is its inferred rational number multiplied by the number of corresponding data nodes that a reachable from the root. So a data node that can be reached in two different ways counts twice, while a node that is reachable in three different ways counts thrice and so on. This is necessary, since a functional program cannot distinguish between aliased data and duplicated data, so it matters how often a certain node can be reached through recursion.

Therefore it follows that the potential associated with cyclic data is necessarily either zero or infinite. In other words, the analysis can tell us that is safe to supply circular data for arguments whose annotated types indicate a potential of zero. Since the potential is the number of reachable nodes (infinite) times the annotation (zero), the overall potential for that data structure remains zero, thus the size of that input plays no role in the inferred resource bound and neither whether or not it is cyclic. Con- sequently, any well-typed program in systems that allow the creation of cyclic data will always enforce zero potential for potentially cyclic data that is newly generated. Note though, that [HJ06] relaxes this limitation somewhat by allowing potential to be ascribed to an initial acyclic fragment of a cyclic data structure.

On the other hand, supplying cyclic data in a position with a non-zero annotation causes the upper cost bound to become infinite. However, we can still obtain some useful information aboutliveness orprogress properties of the program. For example, the analysis ensures that a certain amount of that circular input (or an input stream) must be processed by the program before it allocates more memory or requires more processor clock cycles. If each input node is associated with a potential of 13, then the program can use one unit of a resource for every three input elements processed, whereas a potential of 5 would allow the program to spend 5 for each processed input element. So despite the overall potential being infinite in both cases, the presented analysis still delivers useful information.