extern "C" {
    int printf( const char *, ... );
}

context sumable( type T ) {
    const T 0;
    T ?+?( T, T );
    T ?++( T * );
    [T] ?+=?( T *, T );
};

forall( type T | sumable( T ) )
T sum( int n, T a[] ) {
    T total = 0;			// instantiate T, select 0
    for ( int i = 0; i < n; i += 1 )
	total = total + a[i];		// select +
    return total;
}

int main() {
    const int size = 10, low = 0, High = 10;
    int si, ai[10]; // size
    int i;
    for ( i = low; i < High; i += 1 ) {
	si += i;
	ai[i] = i;
    }
    printf( "sum from %d to %d is %d, check %d\n",
	    low, High, sum( size, ai ), si );
    double sd, ad[10]; // size
    for ( i = low; i < High; i += 1 ) {
	double d = i / (double)size;
	sd += d;
	ad[i] = d;
    }
    printf( "sum from %g to %g is %g, check %g\n",
	    low / (double)size, High / (double)size, sum( size, ad ), sd );
}

// Local Variables: //
// compile-command: "../../bin/cfa sum.c" //
// End: //
