sourcelibs/Parsing/demos/before-after.xtl
1⍝!/usr/bin/env xetal
2⍝│ Before and after: why libs/Parsing's group check looks at the whole
3⍝│ text, not just its start. Equations once summed (y[i] - p[i])^2 as
4⍝│ y[i] - p[i] -- a silent wrong answer, found only by reading the
5⍝│ output -- because its group rule decided "(y - p)^2" was just a
6⍝│ parenthesized group and never looked at what followed the closing
7⍝│ parenthesis. pa:g_roup? checks the closing parenthesis is the
8⍝│ text's own LAST character, so the same mistake is caught instead
9⍝│ of computed wrong (an actual refusal, not just a wrong Bool, needs
10⍝│ a macro to call; libs/Ranges' reject-*.xtl tests show that side of
11⍝│ it, and this library's own tests/Probe.xtlm wraps pa:g_roup? for
12⍝│ the same reason). Run with "just demo Parsing before-after".
13
14"pa:" u̲se< "Parsing"
15term ← "(y - p)^2"
16
17⍝│ The check any reader might start with: starts with "(" and has a
18⍝│ ")" somewhere. True here, and wrong -- the text keeps going.
19ᵘn̲aiveGroup? ← { t → ("(" m̲atch 1 t̲ake t) ∧ 0 < '+ r̲/ 0 + t m̲ember? ")" }
20
21ᵘn̲aiveGroup? term ⍝ 1: "yes, a group" -- wrong
22ᵖᵃg̲roup? term ⍝ 0: Parsing checks the close is the text's last character
23
24y ← 5.0
25p ← 3.0
26y − p ⍝ 2.0: what the naive reading would compute, silently
27(y − p) ^ 2 ⍝ 4.0: the actual value of (y - p)^2