magic-lang / magic

0 stars 2 forks source link

Arithmetic simplifications #115

Open davidhesselbom opened 9 years ago

davidhesselbom commented 9 years ago

E.g. a = a + b can and should be written as a += b in many cases. Would it be possible for magic to detect this kind of thing?

davidpiuva commented 8 years ago

The algebraic rules can be quite complex and would require functional purity on all functions bound to the operands. a = (a + b) + c <=> a = a + (b + c) <=> a += b + c. a = a * b + c is not equivalent to a =* b + c because of precedence.

If A is a collection then we cannot use += because the language looks up by value. array[x] = array[x] + 1 will read by value, add and write by value.

There are also cases of aliasing that can confuse a compiler. a = b + c => a += c if &a = &b.