Fri Apr 23 12:06:31 EDT 2010

Term.hs and Function.hs -> generate C code

Problem: given a pure function on a number typeclass (one that can be
tested in Haskell), compile it to C using the Term and Function
abstractions.  Forget about reductions for now.

How to get from Term -> Function?

Problem: Term and Function are just representations.  The type class
mapping is done in Ai.

*Main> let x = (var "b") + (var "a") in (x * x)
(mul (add b a) (add b a))

The default show of Term is a parenthesized tree representation.
Compiling to Function introduces memoization.  It's default rep is SSA
assembly form.

test1 = compile [realPart c, imagPart c] where
    c = a * b
    a = var "ar" :+ var "ai"
    b = var "br" :+ var "bi"

*Main> :t test1
test1 :: Function

*Main> test1
r0 <- mul ar br
r1 <- mul ai bi
r2 <- negate r1
r3 <- add r0 r2
r4 <- mul ar bi
r5 <- mul ai br
r6 <- add r4 r5
out: r3 r6

The Function type can be converted to C syntax using the `printC'

*Main> printC test1
float fun() {
    float r0 = mul(ar, br);
    float r1 = mul(ai, bi);
    float r2 = negate(r1);
    float r3 = add(r0, r2);
    float r4 = mul(ar, bi);
    float r5 = mul(ai, br);
    float r6 = add(r4, r5);
    return(r3, r6);

Or more directly:

*Main> printC $ compile [var "a" + var "b"]
float fun() {
    float r0 = add(a, b);

Some practical problems that need to be solved:
  - C infix syntax.  Either in Haskell, or using C macros.
  - Convert functions calls to loops for Jack block processing.

Infix syntax seems to be simplest to remove using C macros in the
current ad-hoc Function printing.