Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:jlists [2025/04/28 18:39] – Simplified jlist parsing logic ahelwer | creating:jlists [2026/11/19 03:00] (current) – Minor change to interpretation code of OR ahelwer | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| [[https:// | [[https:// | ||
| TLA⁺ doesn' | TLA⁺ doesn' | ||
| - | However, TLA⁺ //does// have something | + | However, TLA⁺ //does// have something |
| When specifying a system' | When specifying a system' | ||
| For the former we use conjunction (aka logical " | For the former we use conjunction (aka logical " | ||
| Line 64: | Line 64: | ||
| '' | '' | ||
| - | In this way our jlist parsing logic will only add another expression if it finds another vertically-aligned ''/ | + | In this way our jlist parsing logic will only add another expression if it finds another vertically-aligned ''/ |
| This all... kind of works? | This all... kind of works? | ||
| You can parse a surprisingly broad set of jlists with just our simple logic! | You can parse a surprisingly broad set of jlists with just our simple logic! | ||
| Line 88: | Line 88: | ||
| Unfortunately not. | Unfortunately not. | ||
| Here's the proverbial wrench: TLA⁺ also allows '' | Here's the proverbial wrench: TLA⁺ also allows '' | ||
| - | We skipped defining them in the expressions chapter where we learned how to parse operators of different precedences; | + | We skipped defining them in the expressions chapter where we learned how to parse operators of different precedences; |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| - | private | + | |
| - | new Operator(Fix.PREFIX, | + | new Operator(Fix.PREFIX, |
| new Operator(Fix.PREFIX, | new Operator(Fix.PREFIX, | ||
| - | new Operator(Fix.PREFIX, | + | new Operator(Fix.PREFIX, |
| new Operator(Fix.INFIX, | new Operator(Fix.INFIX, | ||
| new Operator(Fix.INFIX, | new Operator(Fix.INFIX, | ||
| Line 126: | Line 126: | ||
| What does this change mean in practical terms? | What does this change mean in practical terms? | ||
| First, it means we cannot easily write the formal TLA⁺ grammar in Backus-Naur Form (BNF) as we learned to do in [[https:// | First, it means we cannot easily write the formal TLA⁺ grammar in Backus-Naur Form (BNF) as we learned to do in [[https:// | ||
| - | Although grammar notation [[https:// | + | Although grammar notation [[https:// |
| Thus formal TLA⁺ grammars exclude jlists and use BNF to define the non-jlist parts of the language, then use plain language to describe how jlists work. | Thus formal TLA⁺ grammars exclude jlists and use BNF to define the non-jlist parts of the language, then use plain language to describe how jlists work. | ||
| - | Second - returning to our parser implementation here - it means that our '' | + | Second - returning to our parser implementation here - it means that our '' |
| Ultimately the shape of this state is a stack of nested jlists. | Ultimately the shape of this state is a stack of nested jlists. | ||
| Line 138: | Line 138: | ||
| We'll be using Java's '' | We'll be using Java's '' | ||
| It only needs to hold the jlist column. | It only needs to hold the jlist column. | ||
| - | Define a new class variable | + | Define a new instance field near the top of the '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| private final List< | private final List< | ||
| Line 184: | Line 184: | ||
| This works! | This works! | ||
| We can parse jlists again! | We can parse jlists again! | ||
| - | It's a bit tricky to figure out how this changes things in practice, so let's take a close look at our infix operator parsing code in '' | + | It's a bit tricky to figure out how this changes things in practice, so let's take a close look at our infix operator parsing code in '' |
| <code java> | <code java> | ||
| Expr expr = operatorExpression(prec + 1); | Expr expr = operatorExpression(prec + 1); | ||
| Line 190: | Line 190: | ||
| Token operator = previous(); | Token operator = previous(); | ||
| Expr right = operatorExpression(op.highPrec + 1); | Expr right = operatorExpression(op.highPrec + 1); | ||
| + | checkPrecOverlap(op); | ||
| expr = new Expr.Binary(expr, | expr = new Expr.Binary(expr, | ||
| if (!op.assoc) return expr; | if (!op.assoc) return expr; | ||
| Line 249: | Line 250: | ||
| This puts you in rarified air. | This puts you in rarified air. | ||
| Only a handful of people in the world possess this knowledge, and now you are among them. | Only a handful of people in the world possess this knowledge, and now you are among them. | ||
| + | |||
| + | ====== Evaluation ====== | ||
| + | |||
| + | Now that we can parse jlists, let's interpret them. | ||
| + | Similar to the logical operators covered in the book, conjunction lists short-circuit. | ||
| + | That means conjuncts are evaluated in order and, if a single conjunct is false, evaluation immediately stops and returns false. | ||
| + | In an odd contrast, disjunction lists do //not// short-circuit; | ||
| + | |||
| + | Add conjunction list evaluation logic to '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | return set; | ||
| + | case AND: | ||
| + | for (Expr conjunct : expr.parameters) { | ||
| + | Object result = evaluate(conjunct); | ||
| + | checkBooleanOperand(expr.operator, | ||
| + | if (!(boolean)result) return false; | ||
| + | } | ||
| + | return true; | ||
| + | default: | ||
| + | // Unreachable. | ||
| + | return null; | ||
| + | </ | ||
| + | |||
| + | Then add the disjunction list logic right below that: | ||
| + | <code java [highlight_lines_extra=" | ||
| + | return true; | ||
| + | case OR: | ||
| + | boolean result = false; | ||
| + | for (Expr disjunct : expr.parameters) { | ||
| + | Object junctResult = evaluate(disjunct); | ||
| + | checkBooleanOperand(expr.operator, | ||
| + | if ((boolean)junctResult) { | ||
| + | result = true; | ||
| + | } | ||
| + | } | ||
| + | return result; | ||
| + | default: | ||
| + | // Unreachable. | ||
| + | return null; | ||
| + | </ | ||
| + | |||
| + | Remember we also parsed the ''/ | ||
| + | This is a bit annoying! | ||
| + | They should function in the exact same way as their respective jlists, but now we have to copy a duplicate of our evaluation logic to '' | ||
| + | So, we'll perform a trick which also has its parallel in chapter 9 of the book: [[https:// | ||
| + | We will flatten infix ''/ | ||
| + | |||
| + | In the '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | Expr expr = operatorExpression(prec + 1); | ||
| + | while ((op = matchOp(Fix.INFIX, | ||
| + | Token operator = previous(); | ||
| + | Expr right = operatorExpression(op.highPrec + 1); | ||
| + | checkPrecOverlap(op); | ||
| + | expr = flattenInfix(expr, | ||
| + | if (!op.assoc) return expr; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Define '' | ||
| + | <code java> | ||
| + | private Expr flattenInfix(Expr left, Token op, Expr right) { | ||
| + | if (op.type == AND) { | ||
| + | | ||
| + | } else if (op.type == OR) { | ||
| + | | ||
| + | } else { | ||
| + | return new Expr.Binary(left, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | The helper returns a regular binary expression except when the operator is '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | private Expr flattenInfix(Expr left, Token op, Expr right) { | ||
| + | if (op.type == AND) { | ||
| + | List< | ||
| + | conjuncts.add(left); | ||
| + | conjuncts.add(right); | ||
| + | return new Expr.Variadic(op, | ||
| + | } else if (op.type == OR) { | ||
| + | List< | ||
| + | disjuncts.add(left); | ||
| + | disjuncts.add(right); | ||
| + | return new Expr.Variadic(op, | ||
| + | } else { | ||
| + | return new Expr.Binary(left, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====== Further Desugaring ====== | ||
| + | |||
| + | This is all right, but it could be even better! | ||
| + | An expression like '' | ||
| + | <code haskell> | ||
| + | /\ /\ /\ 1 | ||
| + | /\ 2 | ||
| + | /\ 3 | ||
| + | /\ 4 | ||
| + | </ | ||
| + | Which is quite a lot of nesting! | ||
| + | It would be nice if it were instead translated to a single flat jlist with four conjuncts. | ||
| + | This rewrite is safe because conjunction & disjunction are associative. | ||
| + | So, define a new '' | ||
| + | Here's what it looks like: | ||
| + | <code java> | ||
| + | private Expr flattenJLists(Token op, List< | ||
| + | List< | ||
| + | for (Expr junct : juncts) { | ||
| + | if (junct instanceof Expr.Variadic vjunct | ||
| + | && vjunct.operator.type == op.type) { | ||
| + | flattened.addAll(vjunct.parameters); | ||
| + | } else { | ||
| + | flattened.add(junct); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | return new Expr.Variadic(op, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Replace the calls to '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | private Expr flattenInfix(Expr left, Token op, Expr right) { | ||
| + | if (op.type == AND) { | ||
| + | List< | ||
| + | conjuncts.add(left); | ||
| + | conjuncts.add(right); | ||
| + | return flattenJLists(op, | ||
| + | } else if (op.type == OR) { | ||
| + | List< | ||
| + | disjuncts.add(left); | ||
| + | disjuncts.add(right); | ||
| + | return flattenJLists(op, | ||
| + | } else { | ||
| + | return new Expr.Binary(left, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Do the same in the jlist parsing loop in '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | if (match(AND, OR)) { | ||
| + | Token op = previous(); | ||
| + | jlists.push(op.column); | ||
| + | List< | ||
| + | do { | ||
| + | juncts.add(expression()); | ||
| + | } while (matchBullet(op.type, | ||
| + | jlists.pop(); | ||
| + | return flattenJLists(op, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Infix ''/ | ||
| + | So concludes this lynchpin tutorial on conjunction & disjunction lists. | ||
| + | Good job making it to the end! | ||
| If your code got out of sync during this tutorial, you can find its expected state [[https:// | If your code got out of sync during this tutorial, you can find its expected state [[https:// | ||
| Continue on the [[creating: | Continue on the [[creating: | ||
| Line 264: | Line 422: | ||
| [[creating: | [[creating: | ||
| - | |||