sourcelib/Combinators.xtl

1⍝ Combinators: the birds Raymond Smullyan names in "To Mock a 2⍝ Mockingbird" that type-check (a standard library, built into xetal). 3⍝ Import it with an alias of your choice: "c:" u_se< "Combinators". 4⍝ docs/birds.md lists them all; the self-applying birds (L, M, M2, U) 5⍝ have no finite type and are in demos/birds-untyped.xtl. 6⍝ 7⍝ How the definitions read: a x̲ b is x applied to a and then b (every 8⍝ function is curried); (e)_ v applies the function e computes; a 9⍝ function passed along as a value is quoted, 'y̲. 10 11ˡI̲ ← { x → x } ⍝ Idiot Bird (identity) 12ˡK̲ ← { x y → x } ⍝ Kestrel 13ˡT̲ ← { x y̲ → y̲ x } ⍝ Thrush 14ˡW̲ ← { x̲ y → y x̲ y } ⍝ Warbler 15ˡW̲1 ← { x y̲ → x y̲ x } ⍝ Converse Warbler 16 17ˡB̲ ← { x̲ y̲ z → x̲ y̲ z } ⍝ Bluebird: compose 18ˡB̲1 ← { x̲ y̲ z w → x̲ z y̲ w } ⍝ Blackbird 19ˡB̲2 ← { x̲ y̲ z w v → x̲ (z y̲ w)_ v } ⍝ Bunting 20ˡB̲3 ← { x̲ y̲ z̲ w → x̲ y̲ z̲ w } ⍝ Becard 21ˡC̲ ← { x̲ y z → z x̲ y } ⍝ Cardinal: swap 22ˡD̲ ← { x̲ y z̲ w → y x̲ z̲ w } ⍝ Dove 23ˡD̲1 ← { x̲ y z w̲ v → (y x̲ z)_ w̲ v } ⍝ Dickcissel 24ˡD̲2 ← { x̲ y̲ z w̲ v → (y̲ z) x̲ w̲ v } ⍝ Dovekie 25ˡE̲ ← { x̲ y z̲ w v → y x̲ w z̲ v } ⍝ Eagle 26ˡE̲h ← { x̲ a̲ b c d̲ e f → (b a̲ c) x̲ e d̲ f } ⍝ Bald Eagle 27ˡF̲ ← { x y z̲ → y z̲ x } ⍝ Finch 28ˡG̲ ← { x̲ y̲ z w → w x̲ y̲ z } ⍝ Goldfinch 29ˡH̲ ← { x̲ y z → (y x̲ z)_ y } ⍝ Hummingbird 30ˡJ̲ ← { x̲ y z w → y x̲ w x̲ z } ⍝ Jay 31ˡO̲ ← { x̲ y̲ → y̲ x̲ 'y̲ } ⍝ Owl 32ˡQ̲ ← { x̲ y̲ z → y̲ x̲ z } ⍝ Queer Bird 33ˡQ̲1 ← { x̲ y z̲ → x̲ z̲ y } ⍝ Quixotic Bird 34ˡQ̲2 ← { x y̲ z̲ → y̲ z̲ x } ⍝ Quizzical Bird 35ˡQ̲3 ← { x̲ y z̲ → z̲ x̲ y } ⍝ Quirky Bird 36ˡQ̲4 ← { x y̲ z̲ → z̲ y̲ x } ⍝ Quacky Bird 37ˡR̲ ← { x y̲ z → z y̲ x } ⍝ Robin 38ˡS̲ ← { x̲ y̲ z → z x̲ y̲ z } ⍝ Starling 39ˡV̲ ← { x y z̲ → x z̲ y } ⍝ Vireo: a pair 40 41⍝ Once removed: the first argument passes through, then the bird. 42ˡC̲s ← { x̲ y z w → (y x̲ w)_ z } 43ˡR̲s ← { x̲ y z w → (z x̲ w)_ y } 44ˡF̲s ← { x̲ y z w → (w x̲ z)_ y } 45ˡV̲s ← { x̲ y z w → (w x̲ y)_ z } 46ˡW̲s ← { x̲ y z → (y x̲ z)_ z } 47 48⍝ Twice removed: the first two pass through. 49ˡC̲ss ← { x̲ y z w v → ((y x̲ z)_ v)_ w } 50ˡR̲ss ← { x̲ y z w v → ((y x̲ w)_ v)_ z } 51ˡF̲ss ← { x̲ y z w v → ((y x̲ v)_ w)_ z } 52ˡV̲ss ← { x̲ y z w v → ((y x̲ v)_ z)_ w } 53ˡW̲ss ← { x̲ y z w → ((y x̲ z)_ w)_ w } 54 55⍝ The Sage Bird by recursion: Y f is f (Y f). It works because a 56⍝ function's parameter is evaluated only when used. 57ˡY̲ ← { f̲ → f̲ ˡY̲ 'f̲ }