3

TLDR: Is there a Haskell library that offers function definitions (preferably with concise notation or naming) for handling common patterns of multi-argument function composition such as those in APL?

Full Question:

I'm new to Haskell and I have recently found myself asking the types of composition questions that other new Haskell users have asked such as how to compose a unary function with a binary function or how to compose two binary functions in a specific way. The point-free solutions to both of these are fairly unintuitive (either (.)(.)(.) or LiftM2 (.)) despite the goal of the function composition pattern being fairly clear (and probably quite common).

I have spent a little time with the APL language and really loved how simple and powerful the function composition model is. For example, here is a diagram of many composition patterns that can be accomplished in extremely terse notation that is actually fairly readable after one spends some time in the language.

Any of these APL patterns could be accomplished in Haskell by writing a simple lambda expression. For example, I have the following line in one of my Haskell files

(...) = (.)(.)(.)

which alternatively (and probably more clearly) could have been written

(...) = \f g x y = f (g x y)

or with a clearer name as

_1_atop_2 = \f g x y = f (g x y)

so that I can easily compose a unary function with a binary function in point-free style in the main portions of my code. For example, to define a distance function between real values, I just compose absolute value with subtraction:

dist = abs ... (-)

or

dist = _1_atop_2 abs (-)

(In this case, the more explicit style of dist x y = abs $ x - y is arguably clearer, but in more elaborate cases the point free is nicer.)

My question is this: Is there any Haskell library that offers definitions of these composition patterns? I can obviously just create my own module with the trivial lambda definitions for these patterns, but I am wondering if there is some standard notation/naming convention for doing this.

j_v_wow_d
  • 511
  • 2
  • 11
  • Your APL Wiki link goes to a page that doesn't have the indicated bookmark/id. Did you mean https://aplwiki.com/wiki/Train#3-trains? – Adám Jan 06 '23 at 08:34
  • I was mostly interested in the diagram of the different possible composition patterns, so I did intend the link to be the tacit programming wiki page. I guess I misspoke since some but not all of those composition patterns are trains. – j_v_wow_d Jan 06 '23 at 08:41
  • OK, but you should fix the link URL and possibly update the post with correct terminology. – Adám Jan 06 '23 at 08:46
  • I made the updates. Thanks for pointing this out – j_v_wow_d Jan 06 '23 at 08:50
  • Some relevant links: [semantic editor combinators](http://conal.net/blog/posts/semantic-editor-combinators), [`composition-prelude`](https://hackage.haskell.org/package/composition-prelude-3.0.0.2/docs/Control-Composition.html), [`composition`](https://hackage.haskell.org/package/composition-1.0.2.2/docs/Data-Composition.html), [`composition-extra`](https://hackage.haskell.org/package/composition-extra), [`data-aviary`](https://hackage.haskell.org/package/data-aviary-0.4.0/docs/Data-Aviary-Birds.html). – Daniel Wagner Jan 06 '23 at 17:23

1 Answers1

5

As far as I know, there isn't a single Haskell library that provides them all. While some are built-in, you'd have to collect the rest from various libraries. Here's table 19 (Combinators in Haskell) from Conor Hoekstra's Combinatory Logic and Combinators in Array Languages:

Name Library
I id Prelude
K const Prelude
W join Control.Monad
C flip Prelude
B (.) Prelude
S (<*>) / ap Control.Applicative
B₁ (.:) Data.Composition
Ψ on Data.Function
Φ liftA2 Control.Applicative
A ($) Prelude
Adám
  • 6,573
  • 20
  • 37