foreach with accumulators

The base foreach is not suitable for computing aggregates. We extend it to allow accumulators. The extended foreach takes the form:

		 		 foreach($E_1$ in $D_1$, $\ldots$, $E_n$ in $D_n$, $LocalVars$,$Accs$,$Goal$) 
or

		 		 foreach($E_1$ in $D_1$, $\ldots$, $E_n$ in $D_n$, $Accs$,$LocalVars$,$Goal$) 
where $Accs$ is an accumulator or a list of accumulators. The ordering of $LocalVars$ and $Accs$ is not important since the types are checked at runtime.

One form of an accumulator is ac$(AC,Init)$, where $AC$ is a variable and $Init$ is the initial value for the accumulator before the loop starts. In $Goal$, recurrences can be used to specify how the value of the accumulator in the previous iteration, denoted as $AC$^0, is related to the value of the accumulator in the current iteration, denoted as $AC$^1. Let's use $Goal(AC_i,AC_{i+1})$ to denote an instance of $Goal$ in which $AC$^0 is replaced with a new variable $AC_i$, $AC$^1 is replaced with another new variable $AC_{i+1}$, and all local variables are renamed. Assume that the loop stops after $n$ iterations. Then this foreach means the following sequence of goals:


		 		  $AC_0$ = $Init$, 

$Goal(AC_0,AC_1)$,
$Goal(AC_1,AC_2)$,
$\ldots$,
$Goal(AC_{n-1},AC_n)$,
$AC=AC_n$



Subsections
Neng-Fa Zhou 2012-01-03