macro librarylibs/Integrals/src/Integrals.xtlm
Integrals: a definite integral written as a paper prints it, computed by a quadrature rule -- "integral tau:=a..b by trapezoid 64 | body"
Import it with an alias of your choice: "integ:" u_se< "Integrals". Put libs/Integrals/src on XETAL_PATH ("just path"); the reference is libs/Integrals/docs. Names with m: are macros; those under h: are private to this file.
A macro is a function from the source text written left and right of its call to the source that replaces the call, before the program is compiled (X_eTaL MC10); xetal expand FILE shows the result. The call is X_eTaL's Saga 42 shape (its decisions 2 to 4, 2026-10-17): integral read like sum, the rule and the grid as option words with a documented default, improper integrals refused. Its reader uses all of its text or refuses it. Decision 1 puts integral in X_eTaL-libraries' Equations in the end; this library is the same call standing alone, lifted whole when that lands (the body here is X_eTaL, there the equation grammar).
The macros
ᵐi̲ntegral< : Unit -> Char -> Char
The value of a definite integral by a quadrature rule: "integral tau:=a..b by trapezoid 64 | body". The variable and its bounds (any X_eTaL giving a Float; a whole-number literal is made Float), then the rule (trapezoid or simpson) and the grid (how many steps; simpson needs an even number), then | and the body: X_eTaL over the variable, which the macro binds to the grid's points as one vector, a, a + h, ..., b, so the body is evaluated once for the whole grid. Without "by" the rule is trapezoid 64. A bound of inf is refused: write a truncation (0..20) and show the tail is small, or change variables. Trapezoid is exact on lines, simpson on cubics; at a jump of the body the sample takes the body's value at that point as written.
"integ:" u̲se< "Integrals"
@ ⁱⁿᵗᵉᵍi̲ntegral< "integral x:=0..1 by simpson 2 | x ^ 2" 0.3333333333333333
ᵐi̲ntegral< ← { @ text → ʰw̲rite ʰr̲ead text }
ᵐt̲ex< : Unit -> Char -> Char
The LaTeX of the same call, a string: \int_{a}^{b} body \, dx, the bounds and the body as written (a Greek letter's name is \tau).
"integ:" u̲se< "Integrals"
@ ⁱⁿᵗᵉᵍt̲ex< "integral tau:=0..1 | e_xp tau" \int_{0}^{1} e_xp tau \, d\tau
ᵐt̲ex< ← { @ text → "\"" c̲at (ʰl̲atex ʰr̲ead text) c̲at "\"" }
Reading the call
ʰr̲ead : Char -> (Char, Char, Char, Char, Char, Char)
The call's parts: (variable, low, high, rule, grid, body), each text, every rule checked.
ʰr̲ead ← { text → t ← ᵗt̲rim text _ ← (0 < t̲ally t) ʰr̲equire "an empty integral: write integral tau:=a..b | body" bar ← ʰf̲irst "|" ʰo̲utside t _ ← (0 < bar) ʰr̲equire "the integral, then | and the body: " c̲at t head ← ᵗt̲rim (bar − 1) t̲ake t body ← ᵗt̲rim bar d̲rop t _ ← (0 < t̲ally body) ʰr̲equire "an empty body after |: " c̲at t _ ← ("integral " ᵗp̲refix? head) ʰr̲equire "an integral starts with the word integral: " c̲at head rest ← ᵗt̲rim 8 d̲rop head at ← ʰf̲irst ":=" ʰo̲utside rest _ ← (0 < at) ʰr̲equire "the variable and its bounds, tau:=a..b, not " c̲at rest var ← ᵗt̲rim (at − 1) t̲ake rest _ ← (ʰn̲ame? var) ʰr̲equire "the variable is a lowercase name: " c̲at var after ← ᵗt̲rim (at + 1) d̲rop rest by ← ʰf̲irst " by " ʰo̲utside after range ← ᵗt̲rim by ʰu̲pTo after opt ← ᵗt̲rim (by, 4) ʰp̲ast after dots ← ʰf̲irst ".." ʰo̲utside range _ ← (0 < dots) ʰr̲equire "the bounds are low..high: " c̲at range low ← ᵗt̲rim (dots − 1) t̲ake range high ← ᵗt̲rim (dots + 1) d̲rop range _ ← ((0 < t̲ally low) ∧ 0 < t̲ally high) ʰr̲equire "a bound is missing: " c̲at range _ ← (n̲ot (ʰi̲nfinite? low) ∨ ʰi̲nfinite? high) ʰr̲equire "an improper integral (a bound of inf): write a truncation, 0..20 say, and show the tail is small, or change variables so the bounds are finite: " c̲at range (rule, n) ← ʰo̲ption opt (var, ʰf̲loat low, ʰf̲loat high, rule, n, body) }
ʰo̲ption : Char -> (Char, Char)
The rule and the grid from the text after "by" ("" for none, the default trapezoid 64): exactly the two words, a rule the library has, a whole number of steps, even for simpson.
ʰo̲ption ← { opt → 0 = t̲ally opt ? ("trapezoid", "64") ws ← ᵗw̲ords opt _ ← (2 = t̲ally ws) ʰr̲equire "after by: the rule and the grid, trapezoid 64 or simpson 32, not " c̲at opt rule ← d̲isclose f̲irst ws n ← d̲isclose 2 s̲elect ws _ ← ((rule m̲atch "trapezoid") ∨ rule m̲atch "simpson") ʰr̲equire "the rule is trapezoid or simpson, not " c̲at rule _ ← ((0 < t̲ally n) ∧ '∧ r̲/ n m̲ember? ⎕D) ʰr̲equire "the grid is a whole number of steps: " c̲at n k ← f̲irst f̲loor n̲umbers n _ ← (0 < k) ʰr̲equire "the grid needs at least one step: " c̲at n _ ← (n̲ot (rule m̲atch "simpson") ∧ 1 = k m̲od 2) ʰr̲equire "simpson needs an even number of steps, not " c̲at n (rule, n) }
ʰi̲nfinite? : Truthy a => Char -> a
Whether a bound is written infinite (inf, -inf, infinity, oo).
ʰi̲nfinite? ← { b → '∨ r̲/ '{ w → (d̲isclose w) m̲atch b } e̲ach ᵗw̲ords "inf -inf +inf infinity -infinity oo -oo" }
ʰf̲loat : Char -> Char
A bound as Float code: a whole-number literal gets f_loat, any other expression stands as written.
ʰf̲loat ← { b → '∧ r̲/ b m̲ember? ⎕D c̲at "-" ? "f_loat " c̲at b b }
Writing the code
ʰw̲rite : (Char, Char, Char, Char, Char, Char) -> Char
The expansion: the bounds, the step h, the variable bound to the grid (n + 1 points), the rule's weights, and h times the weighted sum of the body over the grid.
ʰw̲rite ← { (var, low, high, rule, n, body) → a ← ⎕G̲ENSYM "a" b ← ⎕G̲ENSYM "b" h ← ⎕G̲ENSYM "h" w ← ⎕G̲ENSYM "w" k ← f̲irst f̲loor n̲umbers n "{ @ ->\n " c̲at a c̲at " := " c̲at low c̲at "\n " c̲at b c̲at " := " c̲at high c̲at "\n " c̲at h c̲at " := (" c̲at b c̲at " - " c̲at a c̲at ") / " c̲at n c̲at ".0\n " c̲at var c̲at " := " c̲at a c̲at " + " c̲at h c̲at " * f_loat o_ffsets " c̲at (f̲ormat k + 1) c̲at "\n " c̲at w c̲at " := " c̲at (rule ʰw̲eights k) c̲at "\n " c̲at h c̲at " * '+ r_/ " c̲at w c̲at " * " c̲at body c̲at "\n} @" }
ʰw̲eights : Char -> Int -> Char
The rule's weights over k steps (k + 1 points), as code: trapezoid 1/2, 1, ..., 1, 1/2; simpson (1, 4, 2, 4, ..., 4, 1) / 3.
ʰw̲eights ← { rule k → rule m̲atch "simpson" ? "(1.0 c_at (" c̲at (f̲ormat k − 1) c̲at " r_eshape 4.0 2.0) c_at 1.0) / 3.0" "0.5 c_at (" c̲at (f̲ormat k − 1) c̲at " r_eshape 1.0) c_at 0.5" }
The same call as LaTeX
ʰl̲atex : (Char, Char, Char, Char, Char, Char) -> Char
\int_{a}^{b} body \, dvar, the parts as written; a Greek letter's name (tau, theta, ...) becomes its command.
ʰl̲atex ← { (var, low, high, rule, n, body) → "\\\\int_{" c̲at (ʰp̲lain low) c̲at "}^{" c̲at (ʰp̲lain high) c̲at "} " c̲at body c̲at " \\\\, d" c̲at ʰg̲reek var }
ʰp̲lain : Char -> Char
A bound without the f_loat the reader added to a literal.
ʰp̲lain ← { b → "f_loat " ᵗp̲refix? b ? 7 d̲rop b b }
ʰg̲reek : Char -> Char
A Greek letter's name as its LaTeX command; any other name as is.
ʰg̲reek ← { v → '∨ r̲/ '{ w → (d̲isclose w) m̲atch v } e̲ach ᵗw̲ords greek ? "\\\\" c̲at v v }
greek : Char
greek ← "alpha beta gamma delta epsilon theta lambda mu nu xi pi rho sigma tau phi chi psi omega"
Text helpers
ʰr̲equire : Truthy a => a -> Char -> Char
Nothing when ok holds; else the call is refused with message, at its text (the right side).
ʰr̲equire ← { ok message → ok ? "" "bad-integral right" ⎕R̲EJECT message }
ʰn̲ame? : Truthy a => Char -> a
Whether v is a lowercase name: a lowercase letter, then letters or digits.
ʰn̲ame? ← { v → 0 = t̲ally v ? 0 = 1 ((f̲irst v) m̲ember? "abcdefghijklmnopqrstuvwxyz") ∧ '∧ r̲/ v m̲ember? "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" }
ʰq̲uoted : Char -> Int
1 at each character of s inside a string literal (its quotes included).
ʰq̲uoted ← { s → ('+ s̲\ 0 + s m̲ember? "\"") m̲od 2 }
ʰo̲utside : Char -> Char -> Int
The positions in s where sep starts outside brackets, braces, parentheses and strings.
ʰo̲utside ← { sep s → q ← ʰq̲uoted s d ← '+ s̲\ ((0 + s m̲ember? "([{") − 0 + s m̲ember? ")]}") × 1 − q ps ← sep ᵗf̲ind s (((ps s̲elect d) = 0) ∧ 0 = ps s̲elect q) r̲eplicate ps }
ʰf̲irst : Int -> Int
The first of the positions ps, or 0 for none.
ʰf̲irst ← { ps → 0 = t̲ally ps ? 0 f̲irst ps }