archives

Deriving Functions to Work on Different Types of Data

In Mathematics one often writes f(X) instead of {f(x) | x in X} where X is a set. Array languages like J allow you to write a+b for elementwise addition of arrays. Mathematicians also write f+g = lambda x -> f(x) + g(x) for pointwise addition of functions. Other examples are using functions on symbolic data or on time varying values (like in FRP).

Do you have ideas on how to use this notation in programming languages? Are there languages that allow you to do this in a general way? (i.e. without defining every operator +, *, /, etc. to work on different types of data seperately) Can this work in both statically and dynamically typed languages? How can you handle cases like a + f where a and f aren't of the same type, for example a is a time-varying value and b is a function?

Another example:

I'd expect A+B where A and B are sets to produce {a+b | a in A, b in B}. What should A+f do?

1) {lambda x -> a+x | a in A}, a set of functions
2) lambda x -> {a+x | a in A}, a function that returns sets
3) Error.

Maybe it's possible to determine what to do from the context.

Thank you for your ideas.