New keywords `then` and `else` to improve readability (1)
Currently
and
andor
must be used with both conditions and actions, resulting in code that reads somewhat strangely:X == 1 or Y == 2 or False() => Z
I propose to separate the
else
keyword fromelse error on
and allow it to be used on its own. It would behave the same asor
but be right-associative, allowing the above code to be written as:(X == 1 or Y == 2) else False() => Z
The
error on
construct would remain on its own, and can still be used in the same situations:(X == 1 or Y == 2) else error "Error!" on Z
Or in new situations:
e@X() :- where error "Error!" on e
I also propose to introduce a new keyword
then
which would be the complement toelse
. It would behave likeand
but be right-associative. The following code:X == 1 and Y == 2 and True() => Z
Can then be written as:
(X == 1 and Y == 2) then True() => Z
Apart from improving readability, an added benefit is that this allows a construct that looks like an
if-else
:Submitted by D. Pelsmaeker on 19 June 2014 at 12:33(X == 1 and Y == 2) then True() => Z else False() => Z
Issue Log
Makes sense
Do you want to try adding these to TS yourself? https://github.com/metaborg/ts
If
then
andelse
are indeed sugar forand
andor
, it should be easy to add to TS using just some desugaring transformations.
I’ve attempted it, but it’s not really possible currently as all tasks are eagerly evaluated.
A and B
should not evaluateB
ifA
fails, but currently it does. Equivalently,A or B
should not evaluate taskB
ifA
doesn’t fail.Therefore, if I split
else
fromerror on
, then theerror on
task will always be evaluated and always display an error. (I could keep theelse error on
but thenelse
doesn’t add any benefits.)
(I know this is a year old, but just FYI)
If the error on is the only reason you need lazy evaluation, you can work around it, though you won’t be able to do it with just desugaring.
DefineA then B
asnot(A) or (A and B)
, andA else B
asA or B
whereB
depends onnot(A)
. That last part cannot be defined in sugar I guess.
Log in to post comments