Support for prefix operators
Currently, there is neither support for prefix operations like arithmetic negation, that is, -1 works but -(1+2) does not, nor for boolean negation like !true. Perhaps this is something we would like to add in the future.
What is your opinion on this?
Changes
Among others, the following would need to change
- The lexer no longer lexes negatives numbers but only unsigned numbers.
- In return, the parser is now responsible for figuring out the precedence of arithmetic negation and see to it that
1 - --2is (intuitively speaking) parsed asSub(1, Neg(Neg(2))) - In effekt.effekt,
prefixNegandprefixNotwould need to be added.
(See feature/prefix-operations branch)
Minor nitpick: We might want to call them prefixNeg and prefixNot...
Yes, right. That's a typo... edited
Why would we want to write -(1+2)? For prefixNot I can understand it, but -(1+2) is a smell in my opinion. Is there a usecase where you think this is better than 0 - (1 + 2)?
Another prefix operator that could be useful is bitwise negation ~x where typically we wouldn't desugar it into "subtraction".
-(x + y) is perfectly valid and very commonly used mathematically expression. I have never seen someone write 0 - (x + y) instead. Do you have an example where this appears as a code smell?
For example, the normal distribution is defined as N(x; mu, sigma) ~ e^(-((mu - sigma)^2 / (2 * sigma^2)). I have never seen it given as N(x; mu, sigma) ~ e^(0 - ((mu - sigma)^2 / (2 * sigma^2)) instead. If someone wanted to implement this in Effekt, I would imagine the normal user would like to stick as close as possible to common mathematical notations and not having to think about how to desugar it.
Edit: A perhaps easier example: negative fractions. It is more convenient and more common to write - (1 / 2) instead of 0 - (1 / 2).
Ok, here are two points to address
-
spaces: a lot of Effekt code is written by students (in theses, etc.). Since we do not have a nice formatter, they end up using the most creative way to use whitespaces. I would be very happy if
1-xwould just fail and force them to write1 - x. Ideally, also1- xshould fail. -
-as unary operator. This is mostly personal taste. I have a dislike for prefix and postfix operators. I almost always writex = x + 1instead ofx++. I am also happy to write0 - xinstead ofx, like here:
https://github.com/effekt-lang/effekt/blob/a4d5f589f1a59a8091f03410f75df735ae77cecc/libraries/common/effekt.effekt#L386-L387
Actually, I typically write -1 * ... instead of 0 - ...and I think it works pretty well in your example as well.
The normal distribution is a bit of a red hering, since you use other operators that typically also don't exist in PLs (like ^ for exponentiation).