for example:

pure id <*> x /= x

You can fix this by ‘cloning’ the leaf elements though:

L2 f <*> B2 (xs :# ys) = B2 (f xs :# f ys)

B2 (fs :# gs) <*> L2 x = B2 (($x) fs :# ($x) gs)

I think a similar trick works for T4.

]]>`Scan`

instance for `T4`

does indeed depend on there being an `Applicative`

instance. And yes, I’d define it in a zip-like way. Because `T4`

has a sum and its `Applicative`

is zip-like, `(<*>)`

and `liftA2`

will be partial, which is less than ideal. I hadn’t noticed this issue, because I was working out this algorithm for depth-typed trees, which have no (non-degenerate) sums and hence have total `(<*>)`

and `liftA2`

.]]>I think prefixScan for T4 assumes a zip-like applicative instance for T4?

For `T4`

, `prefixScan`

is defined simply as `prefixScanEnc`

, without relying on an `Applicative`

instance.

Guy Blellochâ€™s algorithm works on lists of any length, but your T4 trees can only contain 2^n elements.

In *Programming parallel algorithms*,
right above Figure 11 (containing the `scan`

implementation), Guy says “The particular code shown works only on sequences that have length equal to a power of two, but it is not hard to generalize it to work on sequences of any length.”

How should T4 be fixed so that can also contain any number of elements?

By using a more flexible data type, such as the binary random-access lists in Chris Okasaki’s *Purely Functional Data Structures*, section 9.2.1.
Quoting from that section:

]]>A binary random-access list of size n contains a a tree for each one in the binary representation of n. The rank of each tree corresponds to the rank of the corresponding digit; if the ith bit of n is one, then the random-access list contains a tree of size 2^i.

Guy Blelloch’s algorithm works on lists of any length, but your T4 trees can only contain 2^n elements. How should T4 be fixed so that can also contain any number of elements?

]]>