effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Make && and || by name

Open b-studios opened this issue 1 year ago • 2 comments

We can make these two logical operators by name, by simply desugaring them to if.

This could either happen before Typer (that is in Parser), or after Typer (that is in the translation to core).

EXPR1 || EXPR2 = if (EXPR1) true else EXPR2
EXPR1 && EXPR2 = if (EXPR1) EXPR2 else false

Doing it in Parser has the advantage that it is really just a few lines of code to implement. Doing it in core.Transformer has the advantage that we can selectively perform this translation on effekt.infixOr and effekt.InfixAnd and the operators can be overloaded (for instance for logic programming).

We could also do the translation selectively and only translate it if EXPR2 is a Stmt or a side effecting Expr (EXPR1 does not matter, since it is forced anyway).

b-studios avatar Feb 03 '24 16:02 b-studios

EXPR1 || EXPR2 = infixOr { () => EXPR1 } { () => EXPR2 }
EXPR1 && EXPR2 = infixAnd { () => EXPR1 } { () => EXPR2 }

def infixOr { first:  () => Boolean } { second: () => Boolean }: Boolean = 
   if (first()) true else second()

b-studios avatar Feb 13 '24 17:02 b-studios

The second is probably better. In the future, then, we need to optimize the inliner to make sure this is actually inlined.

b-studios avatar Feb 13 '24 17:02 b-studios