1

For example, imagine a function like this:

int solveSomeEquation(int y)
{
    y = (int x) * 2;
    return x;
}

Using symbolic algebra, the compiler would determine that x = y / 2. Even better, it would complain that int is not sufficient for storing the result of y / 2. Imagine this functionality extending to solving ODEs with constraints and/or boundary conditions, and integration using symbolic or numeric methods (at run time) where needed. I, for one, would love to see something like this:

int areaOfUnitSemiCircle()
{
    auto semiCircleFunc = [](double x){ return abs((1 - x^2)^0.5); };
    semiCircleFunc = (auto semiCircleIntegralFunc)'; // single quote means derivative
    return semiCircleIntegralFunc(1) - semiCircleIntegralFunc(-1);
}

simplified to:

int areaOfUnitSemiCircle()
{
    return Pi/2;
}

Furthermore, it would be able to not only simplify expressions locally inside a function, but perform whole program optimization. It could rearrange expressions to improve numerical stability (reduce the effect of floating point error) or even eliminate them entirely by using a different representation. BigInt, IEEE1394's quadruple-precision float, sets defined by a predicate, integration over piecewise functions. Probably loads more could be done.

Such a thing is possible, no? I know you can perform stuff like this using Mathematica's or MatLab's programming language functions, but they are called BY the source code. I want it to be applied TO the source code by the compiler. (Do these languages have this sort of thing? I don't know.) I love using Mathematica at work when I have an excuse, but then I end up with magical-looking functions in C++ that make no sense without referring to an external Mathematica notebook. I'd like to be able to be a mathematician and a programmer in one language, one environment. Does such a thing exist?

Brent
  • 4,153
  • 4
  • 30
  • 63
  • 1
    Have you looked at [proof assistants](http://en.wikipedia.org/wiki/Proof_assistant) such as [Cog](http://en.wikipedia.org/wiki/Coq) with [code extraction](http://coq.inria.fr/refman/Reference-Manual027.html) or [Isabelle](http://en.wikipedia.org/wiki/Isabelle_(theorem_prover)) with [code extraction](http://wwwbroy.in.tum.de/~berghofe/papers/TYPES2002_slides.pdf)? – Guy Coder Apr 13 '13 at 21:41

1 Answers1

0

Well, Maxima [1] is certainly capable of constructing and manipulating expressions, and making functions out of them. Functions are just expressions too, so it's straightforward to take some expression, manipulate it, construct a function, and call the function, and doing all of these things with variables instead of specific values. For example, here is your circle area function:

(%i1) display2d : false $
(%i2) a(r) := ''(4 * integrate (sqrt (r^2 - x^2), x, 0, r));
Is r positive, negative or zero?
p;
(%o2) a(r):=%pi*r^2
(%i3) a(10);
(%o3) 100*%pi
(%i4) a(h);
(%o4) %pi*h^2

The general idea of a programming language being able to work on itself originated in Lisp, which is still a good language for such exercises. Maxima is implemented in Lisp. I recommend learning Lisp, you can learn a lot about programming in general from it even if you end up working with other languages.

[1] http://maxima.sourceforge.net

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48