I have now (hopefully) implemented functional composition using
operator dot:
//---------------------
fun f(x:int)=>x + x;
fun g(x:int)=> x * x;
// simple case, rhs is a name
val k = f . g;
//println$ 1 . k;
println$ 1 . f . g;
var z = (f . g) 1;
println$ 1 . (f . g);
println$ 1 . (g . f);

// here the rhs is itself a composition and this not a simple name
println$ 1 . (f . (f . f));

fun h (x:int) (y:int) => x + 10 * y;

println$ 1 . (f . (h 1)); // #1
println$ 1 . ((f . h) 1); // #2
println$ 1 . (f . h 1);   // dot has higher precedence, so this is same as #2 
//----------------

You should note the algorithm says: given f.g, first try to apply g to f,
if that fails and g is function and dom(g)=cod(f), then create a
composite function such that

(f . g) x = g( f (x) )

Note the order reversal. This is reverse composition. It is because we want

x . f . g = (x . f) . g = x . (f . g)

and of course (x . f) . g = g (f (x))\

At present, closures of composites don't work, just as closures of
anonymous sums are not working.

I'll make this work soon I hope (both cases). The problem is that
at present, when the need for a closure is discovered in the C++
code generator, it is too late to synthesise one easily: it could be
done, creating an entry in the bound symbol table, but it is risky
because we're in the process of iterating through it, and it's a hash
table, so mutation is dangerous during an iteration. A set wouldn't
have this problem .. 

Anyhow, the solution is to make the closures in the closure making
pass (in mkcls I think), and replace the compositions which will need
to become closures with the closure term, refering to the synthesised
function.

It sounds hard but the closure is just the wrapper:

fun clos (x:t) => g ( f ( x ) );

and the same for the failing sum component closure.

Generally this is all  a pain. Felix can't tolerate lambdas in the
term structure at code generation time, nor indeed at any other
time after binding. All lambdas have to be lifted out and put in
the symbol table with some "name", since closures are only
supported for such functions (if a closure has to be generated
it is nothing more than a new'd class value).

I plan to also implement the parallel composition (product) 
of functions:

  (f * g) (1,2) = (f 1, g 2)

and of course the sum

  (f  + g) case 0 of (int+float) 1 = f 1
  (f + g) case 1 of (int+float) 2.0 = g 2.0

These are standard "categorical operators". There are a couple more:
"delta" in CT is called "dup" in programming: delta x = x,x
and there's an operator which does

  x -> (f x, g x)

i.e. x . dup . (f * g)

and a few others.

Note that whilst "compose" and "parallel_compose" can be
easily defined in the library **for two arguments** generically
it is NOT possible at this time to write a definition for an arbitrary
number of arguments: Felix is polymorphic but not polyadic.

In fact, polyadic fold operators COULD be implemented for
syntactic terms in the parser action code using Scheme.
however this is not real polyadic programming because it
only works on literal terms, not on structured values.
For example you might implement a fold that coped with

 (f * g * h) (1,2,3)

but would fail on

  (f * g * h) x

where x = 1,2,3

since the parser doesn't know the structure (type) of x.

So we have to do this kind of thing in the compiler at present,
in Ocaml. (The above case is easy, it is just f x.mem0, g x.mem1,
h x.mem2, which works for all x, literally tuples, a variable,
or indeed any expression yielding a triple).

I can't figure out how to set it up so the user can write this
kind of rule (in user code as opposed to hard coding into the compiler).

--
john skaller
skal...@users.sourceforge.net





------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to