Dependent templates

We've already seen how template parameters can be used to construct parameterized circuits, as well as change the size of arrays in the process port list. In addition, they can also be used to specify parameters used in the template itself.

template<pint N; pint data[N]>
defproc test(chan!(int) out)
{
    chp {
       (; i : N : out!data[i])
     }
}

In the example above, the process test takes a parameter N, followed by a parameter array of size N.

pint x[3];
x[0] = 1; x[1] = 3; x[2] = 5;
test<3,x> t;

In the above example, the CHP body for instance t would be expanded into

  out!1; out!3; out!5

Dependent templates have left to right evaluation order. In the example above this means that t.N is created and assigned value 3, and then after that the value of t.N can be used to infer the type of pint d[N]. Switching the order would result in an error:

template<pint data[N]; pint N> defproc test(chan!(int) out);
-[ERROR]-> 	The identifier `N' does not exist in the current scope

Parameters can be used in expressions as well. For example, the memory definition in the ACT standard library is:

export template<pint N, W>
defproc ram (chan?(int<2>) rd; chan?(int<std::ceil_log2(N)>) addr;
             chan?(int<W>) din; chan!(int<W>) dout)
{
  ...
}

The first template parameter N corresponds to the number of elements in the memory, and W corresponds to the bit-width of the data in the memory. The bit-width of the address channel addr is computed from N by a standard library parameter function that is used to compute the smallest number of address bits needed to be able to hold the address values.