Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
creating:evaluation [2025/04/19 19:45] – Use internal wiki links ahelwer | creating:evaluation [2025/04/27 17:32] (current) – Removed infix conjunction & disjunction operators ahelwer | ||
---|---|---|---|
Line 193: | Line 193: | ||
We use '' | We use '' | ||
It's incredible how much we get for free here; Java sets even implement value-based de-duplication and equality comparison for us for arbitrarily-nested sets! | It's incredible how much we get for free here; Java sets even implement value-based de-duplication and equality comparison for us for arbitrarily-nested sets! | ||
- | |||
- | The only binary operators remaining are logical '' | ||
- | We actually want '' | ||
- | Add the highlighted lines near the top of the '' | ||
- | |||
- | <code java [highlight_lines_extra=" | ||
- | public Object visitBinaryExpr(Expr.Binary expr) { | ||
- | Object left = evaluate(expr.left); | ||
- | switch (expr.operator.type) { | ||
- | case AND: | ||
- | if (!(boolean)left) return false; | ||
- | Object right = evaluate(expr.right); | ||
- | return (boolean)right; | ||
- | default: | ||
- | break; | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | In an odd contrast, our '' | ||
- | We add it in with the rest of the operators in '' | ||
- | |||
- | <code java [highlight_lines_extra=" | ||
- | Object right = evaluate(expr.right); | ||
- | switch (expr.operator.type) { | ||
- | case OR: | ||
- | return (boolean)left || (boolean)right; | ||
- | case DOT_DOT: | ||
- | </ | ||
- | |||
- | Why are these different? | ||
- | It has to do with how they are used in TLA⁺ formulas. | ||
- | Actions are usually defined as a conjunction of expressions serving as guards, where later expressions could result in a runtime error if evaluated in some cases. | ||
- | For example, consider a variable '' | ||
- | People often write expressions like '' | ||
- | It would be a runtime error to evaluate '' | ||
- | It is thus useful to use conjunction as a guard stopping expression interpretation if the first operand is not true. | ||
- | In contrast, disjunction is used in TLA⁺ to express nondeterminism and so both branches of the disjunct need to be evaluated to see whether their expressions satisfy alternative next states. | ||
- | You can read [[https:// | ||
==== Evaluating Other Operators ==== | ==== Evaluating Other Operators ==== | ||
Line 311: | Line 272: | ||
</ | </ | ||
- | We also need some helpers | + | We also need a helper |
<code java> | <code java> | ||
Line 317: | Line 278: | ||
if (operand instanceof Boolean) return; | if (operand instanceof Boolean) return; | ||
throw new RuntimeError(operator, | throw new RuntimeError(operator, | ||
- | } | ||
- | |||
- | private void checkBooleanOperands(Token operator, Object left, Object right) { | ||
- | if (left instanceof Boolean && right instanceof Boolean) return; | ||
- | throw new RuntimeError(operator, | ||
} | } | ||
</ | </ | ||
Line 382: | Line 338: | ||
checkSetOperand(expr.operator, | checkSetOperand(expr.operator, | ||
return ((Set<?> | return ((Set<?> | ||
- | </ | ||
- | Disjunction: | ||
- | <code java [highlight_lines_extra=" | ||
- | case OR: | ||
- | checkBooleanOperands(expr.operator, | ||
- | return (boolean)left || (boolean)right; | ||
- | </ | ||
- | Conjunction: | ||
- | <code java [highlight_lines_extra=" | ||
- | case AND: | ||
- | checkBooleanOperand(expr.operator, | ||
- | if (!(boolean)left) return false; | ||
- | Object right = evaluate(expr.right); | ||
- | checkBooleanOperand(expr.operator, | ||
- | return (boolean)right; | ||
</ | </ | ||
Finally, '' | Finally, '' | ||
Line 487: | Line 428: | ||
</ | </ | ||
- | If you got out of sync, you can find a snapshot of the expected state of the code in [[https:// | + | If you got out of sync, you can find a snapshot of the expected state of the code in [[https:// |
Next tutorial: [[creating: | Next tutorial: [[creating: | ||