By Stepan Mitkin
Part 7 of 10
There are three ways to express boolean logic in Erlang:
Boolean operators: and, or, xor, not.
more_than_10_times(Left, Right) ->
if (Right =/= 0) and (Left / Right > 10)
-> true
; true
-> false
end.
Short-circuit boolean operators: andalso, orelse. These operators evaluate their second operand only when necessary.
more_than_10_times_ex(Left, Right) ->
if (Right =/= 0) andalso (Left / Right > 10)
-> true
; true
-> false
end.
Guards. Guards may contain comma (,) and semicolon (;) operators. The comma operator is somewhat similar to andalso. Semicolon resembles the orelse operator.
more_than_10_times_g(Left, Right) when Right =/= 0, Left / Right > 10
-> true;
more_than_10_times_g(_, _)
-> false.
There is one common problem about all of these forms. They get totally unreadable when the logic expression gets even moderately complex. They require a lot of hard thinking to figure them out.
Another issue is short-circuit evaluation. Although efficient, the short-circuit operators introduce hidden paths into the algorithm. Depending on the data being processed, guards and short-circuit operators may skip the second operand. The burden of determining which operand is skipped and when fully lies on the reader of the program.
DRAKON has a way to deal with these problems. The standard DRAKON's "If" icons can be arranged to form visual logic formulas. Visual logic formulas can represent any logic expression in an easily comprehensible way.
Here is the visual formula for the short-circuit AND expression.
Text formula for AND:
ConditionA andalso ConditionB
Visual formula for AND:
Here is the visual formula for the short-circuit OR expression.
Text formula for OR:
ConditionA orelse ConditionB
Visual formula for OR:
As you can see, all 4 possible combinations of the operands are present on the diagram. The outcome for each of them can be traced with a finger.
If we want to evaluate both of the operands before invoking the AND operator, it should be done in an explicit way.
Now let us compare a text-based and a visual formula for a more sophisticated logic expression.
Text formula:
M = (A and not B and C) or (D and E and not F)
Visual formula:
The text-based formula definitely occupies less space. But it is the reader's job to decompress it. The complexity is still there, but it is hidden.
The key point of DRAKON is that it does not hide anything. All quirks of the algorithm are visible. Hence the term 'visual programming'.
Note that no additional construct is required to express negation. DRAKON does not have a NOT operator because it is not needed. If you want to invert the outcome of an "If" icon, just swap the YES and NO labels.
With DRAKON, there is no need to remember which logic operators force evaluation of their arguments and which do not. Every path through the algorithm is clearly visible. The recommended way of doing logic with DRAKON is visual formulas.
Contact: drakon.editor@gmail.com