This made-up syntax is an array comprehension. The variable i takes on all the indices of the array, and the expression (i+1) * (i+1) computes the value of the array for each index.

Fusion with Vectors

With our design of Vector we can give strong guarantees about fusion

Whenever two vector functions are composed, the intermediate vector is removed.

Much stronger guarantee than conventional compiler optimizations

Helps the programmer understand whether his program will be efficient or not.

Fusion caveat

One caveat that is easy to forget

When Vectors are passed around in loops, they will be stored written to memory on each iteration.

Monads

If we are defining a pure language, it is often that we still want side-effects,

for efficiency

on the top level to orchestrate computations

communicate with the environment.

Monads is the standard solution to this problem and it is perfectly possible to add monads to an embedded language using the techniques presented here. There is a paper at IFL this year describing the details.

Combining Deep and Shallow Embeddings - Pros and Cons

Pros

Adding new features is relatively light weight by letting the shallow embedding do most of the work

Typically a single constructor suffices to cover a lot of functionality

Very Haskell-like programming interface for the embedded language

Supports writing instances for many common classes and using Haskell's built-in syntactic sugar

Fusion

Cons

We sometimes have to modify the AST data type

Remedy

Use something like "Datatype A La Carte" to make it easier to add features to the AST. We have a library "Syntactic" which does that and more.

Excercise

One problem with our representation of vectors is that some functions are very inefficient to compute. For example scans.

The core of this problem is that each element is computed independently, we cannot pass any values between these computations.

A better representation would be to base it on the type of array unfold

unfold :: (b -> (a,b)) -> b -> Int -> Array Int a

Excercise - cont.

Excercise:

Add a new constructor to the AST to represent sequential arrays

Write a new type, or add a constructor to the already existing Vector type

Write the Syntactic instance

Write some functions for the new feature, like map and scan.

Does the new representation support fusion? Experiment!

Credits

I can take very little credit for the presented material

Emil Axelsson as written most of the core Feldspar code

The rest of the Feldspar team for discussion

Pan, by Elliott, Finne & de Moor, contains many of the ideas presented here