effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Support for prefix operators

Open dvdvgt opened this issue 1 year ago • 5 comments

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

  1. The lexer no longer lexes negatives numbers but only unsigned numbers.
  2. In return, the parser is now responsible for figuring out the precedence of arithmetic negation and see to it that 1 - --2 is (intuitively speaking) parsed as Sub(1, Neg(Neg(2)))
  3. In effekt.effekt, prefixNeg and prefixNot would need to be added.

(See feature/prefix-operations branch)

dvdvgt avatar Jun 24 '24 12:06 dvdvgt

Minor nitpick: We might want to call them prefixNeg and prefixNot...

marzipankaiser avatar Jun 24 '24 13:06 marzipankaiser

Yes, right. That's a typo... edited

dvdvgt avatar Jun 24 '24 13:06 dvdvgt

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".

b-studios avatar Jun 24 '24 14:06 b-studios

-(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).

dvdvgt avatar Jun 24 '24 14:06 dvdvgt

Ok, here are two points to address

  1. 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-x would just fail and force them to write 1 - x. Ideally, also 1- x should fail.

  2. - as unary operator. This is mostly personal taste. I have a dislike for prefix and postfix operators. I almost always write x = x + 1 instead of x++. I am also happy to write 0 - x instead of x, 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).

b-studios avatar Jun 24 '24 16:06 b-studios