Forward Iteration

A loop-like construct to iterate on flows

An Iteration on Flows

The Swan language performs operations on sequences of values, called flows; a variable and more generally an expression represents a flow. The forward construct is a loop-like construct allowing to iterate a computation on a finite flow provided by an array.

It can be represented as in the Figure 1:
  1. inputs: arrays considered as finite flows (for instance both input arrays v and w of size n are considered by the forward body as two flows with n values)
  2. forward body: each iteration step takes the corresponding input flows from the inputs, performs computations and produces output or local flows.
  3. outputs: arrays of size n containing all iteration step outputs
In the Figure 1, the output is an array. In general, the output can also be a scalar, corresponding to the last value of the output flow.
Figure 1. forward general iteration scheme


An Operational View of the Forward

Considering the operational view of the forward construct (see Code Generation from a Swan Model) gives the sketch of the corresponding generated code. This operational view can be represented as in Figure 2.
  • inputs: arrays considered as finite flows of n values
  • forward body: at each step, the step function takes the nth values of the input arrays and the state Sn to compute the nth values of the output sequence, and the Sn+1 state
  • outputs: the array containing the n values or the cumulative results.
This representation shows that in the forward construct, the body is re-entered at each iteration step during one activation step. In other words, the iteration steps are executed in sequence. This is one of the main difference with iterators as presented in Differences with Iterators.
Figure 2. Operational View of the forward


Forward Use Cases

The forward construct can be used to perform matrix or arrays manipulations. This construct also allows to implement more complex and different algorithms: all Swan language features can be put in a forward body, and in particular sequential logics (sequences depending on current and past values) like automaton.

In a forward body, the notion of current and previous values refer to the finite flows read or computed by the body. The current value is the value at current iteration step, and a previous value is a value present at a lower iteration step.

For example, an automaton can be evaluated during the n iteration steps, and it is possible to change state at each value in the array. See Using Sequential Operations in a forward where an automaton is used to read a flow of n values contained in an array of size n.

Note: The forward construct is not an imperative loop. It ensures the same guarantees as the other Swan constructs:
  • Single definition of each array element
  • Full definition of arrays
  • No out-of bounds accesses
  • Static array sizes

Forward: A General Iteration Construct

The forward construct is a general construct to implement any iterative algorithm. In particular, an input array is not always required. And as a result of the iterative algorithm, the outputs can be either arrays containing the results of each iteration step (see Forward Construct for Iterative Algorithms ), single flows cumulating the results (accumulators), or only values of the last iteration step.

Multidimensional Iteration

The forward construct allows to iterate on several dimensions, without nesting several forward. An example is presented in Multidimensional Iteration. By default, the forward body is reset at each iteration step, but it can be resumed. The necessity of resuming the body is explained in Forward Resume, as well as the difference between multidimensional forward and nested forward.

Forward Concepts

The next sessions details the different concepts related to the forward construct and illustrate them through typical examples:
  1. Simple Array Operations
  2. Using Sequential Operations in a forward
  3. Multidimensional Iteration
  4. Forward Construct for Iterative Algorithms
  5. Forward Resume
  6. Partial Forward
  7. Differences with Iterators