sourcelib/Combinators.xtlm

1⍝│ Combinators as macros: the birds of the Combinators library written 2⍝│ in at compile time instead of applied when the program runs. Import 3⍝│ both under one alias: "c:" u_se< "Combinators" finds Combinators.xtl 4⍝│ (c:Y_, c:B_, ...) and this file (c:Y_<, c:B_<). 5 6⍝━━ Recursion 7 8⍝│ Define the function named on the left by the lambda on the right, 9⍝│ whose first parameter is the function itself: the knot c:Y_ ties 10⍝│ when the program runs is tied here, by writing the name in place of 11⍝│ the self parameter. The result is plain named recursion, with no 12⍝│ lazy parameter and no combinator left to run. 13⍝│ takes: text text 14⍝│ uses: parsed 15⍝│ >> "c:" u_se< "Combinators" 16⍝│ >> "u:f_act" c:Y_< "{ ~s_elf n -> n <= 1 ? 1; n * s_elf n - 1 }" 17⍝│ >> u:f_act 5 18⍝│ 120 19ᵐY̲< ← { name lambda → 20 n̲ot ⎕S̲TATEMENT @ ? "misplaced-macro call" ⎕R̲EJECT "c:Y_< defines a function: it stands as a statement of its own" 21 t ← ʰt̲rim lambda 22 a ← w̲here "->" ʰs̲tarts t 23 (n̲ot "{" m̲atch 1 t̲ake t) ∨ (0 = t̲ally a) ? "bad-macro-argument right" ⎕R̲EJECT yShape 24 ps ← ʰw̲ords ((f̲irst a) − 2) t̲ake 1 d̲rop t 25 2 > t̲ally ps ? "bad-macro-argument right" ⎕R̲EJECT yShape 26 self ← ʰs̲trip d̲isclose f̲irst ps 27 rest ← ʰj̲oin '{ p → " " c̲at d̲isclose p } m̲ap 1 d̲rop ps 28 body ← ((f̲irst a) + 1) d̲rop t 29 (ʰt̲rim name) c̲at " := {" c̲at rest c̲at " ->" c̲at (self ʰw̲ord ʰt̲rim name) body 30} 31 32⍝━━ Composition 33 34⍝│ The function on the left applied to the result of the one on the 35⍝│ right, as one lambda written in place (the Bluebird, B f g x = 36⍝│ f (g x)); either may be a name, a derived function or a lambda. 37⍝│ takes: text text 38⍝│ >> "c:" u_se< "Combinators" 39⍝│ >> u:s_umTo := "'+ r_/" c:B_< "r_ange" 40⍝│ >> u:s_umTo 4 41⍝│ 10 42ᵐB̲< ← { f g → "{ " c̲at (ʰt̲rim f) c̲at " " c̲at (ʰt̲rim g) c̲at " _r }" } 43 44yShape ← "c:Y_< takes a lambda whose first parameter is the function itself: { s_elf n -> ... }" 45 46⍝ The text t without white space at its ends. 47ʰt̲rim ← { t → 48 m ← n̲ot t m̲ember? " \n\t" 49 0 = '+ r̲/ 0 + m ? "" 50 i ← w̲here m 51 (1 + (f̲irst r̲ev i) − f̲irst i) t̲ake ((f̲irst i) − 1) d̲rop t 52} 53 54⍝ The words of t: runs of characters other than white space, boxed. 55ʰw̲ords ← { t → (n̲ot t m̲ember? " \n\t") p̲artition t } 56 57⍝ The boxed texts b joined into one text. 58ʰj̲oin ← { b → d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ b } 59 60⍝ A parameter's name without the lazy mark ~. 61ʰs̲trip ← { p → (f̲irst p) = f̲irst "~" ? 1 d̲rop p◆ p } 62 63⍝ Where the text w starts in t (1 at each such position). 64ʰs̲tarts ← { w t → '{ i → w m̲atch (t̲ally w) t̲ake (i − 1) d̲rop t } e̲ach r̲ange t̲ally t } 65 66⍝ Characters that continue a name. 67nameChars ← "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_:" 68 69⍝ t with each whole word w (not part of a longer name) replaced by r. 70ʰw̲ord ← { w r t → 71 n ← t̲ally w 72 e ← " " c̲at t c̲at " " 73 ok̲ ← { i → (i + n − 1) > t̲ally t ? 0◆ n̲ot ((i s̲elect e) m̲ember? nameChars) ∨ (i + n + 1) s̲elect e m̲ember? nameChars } 74 h ← (w ʰs̲tarts t) ∧ 'ok̲ e̲ach r̲ange t̲ally t 75 c ← 0 < ('+ s̲\ 0 + h) − '+ s̲\ 0 + (n r̲eshape 0) c̲at (0 − n) d̲rop 0 + h 76 p̲ ← { i → 0 < i s̲elect h ? r◆ (i s̲elect c) ? ""◆ i s̲elect t } 77 ʰj̲oin 'p̲ m̲ap r̲ange t̲ally t 78}