5

Basically, I need to define infix operator for function composition, flipped g . f manner.

(#) :: (a -> b) -> (b -> c) -> a -> c
(#) = flip (.)

infixl 9 #

Here, temporarily, I just have chosen the symbol: #, but I'm not certain this choice is not problematic in terms of collision to other definitions.

The weird thing is somehow I could not find a Pre-defined infix operator list in Haskell.

I want the composition operator as concise as possible, hopefully, a single character like ., and if possible, I want to make it &. Is it OK?

Is there any guidance or best pracice tutorial? Please advise.

Related Q&A:

What characters are permitted for Haskell operators?

Functor
  • 582
  • 1
  • 9

1 Answers1

11

You don't see a list of Haskell built-in operators for the same reason you don't see a list of all built-in functions. They're everywhere. Some are in Prelude, some are in Control.Monad, etc., etc. Operators aren't special in Haskell; they're ordinary functions with neat syntax. And Haskellers are generally pretty operator-happy in general. Spend any time inside your favorite lens library and you'll find plenty of amusing-looking operators.

In terms of (#), it might be best to avoid. I don't know of any built-in operators called that, but # can be a bit special when it comes to parsing. Specifically, a compiler extension enables # at the end of ordinary identifiers, and GHC defines a lot of built-in (primitive) types following this practice. For instance, Int is the usual (boxed) integer type, whereas Int# is a primitive integer. Most people don't need to interface with this directly, but it is a use that # has in Haskell that would be confused slightly by the addition of your operator.

Your (#) operator is called (>>>) in Haskell, and it works on all Category instances, including functions. Its companion is (<<<), the generalization of (.) to all Category instances. If three characters is too long for you, I've seen it called (|>) in some other languages, but Haskell already uses that operator for something else. If you're not using Data.Sequence, you could use that operator. But personally, I'd just go with (>>>). Any Haskeller will recognize it pretty quickly.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • Thanks! Very informative. How about `&`?? – Functor Feb 11 '22 at 02:10
  • 2
    [`(&)`](https://hackage.haskell.org/package/base-4.16.0.0/docs/Data-Function.html#v:-38-) is already `flip ($)`. – Silvio Mayolo Feb 11 '22 at 02:12
  • Ooops. and right I found `>>>` and actually thought too long. Do you have any recommendations? Obviously, `+` is not. By the way `|>` is pipe operator not composition. – Functor Feb 11 '22 at 02:14
  • 3
    Personally, I don't think "reverse composition" is worth a valuable one-letter symbol, when regular composition already has one. If you're code golfing, use `(#)` since it's just for sport. If you're writing code in the large, then just use `(>>>)` since the extra two characters shouldn't make that much of a difference. – Silvio Mayolo Feb 11 '22 at 02:17
  • Thanks a lot for your advice. Appeciated. – Functor Feb 11 '22 at 02:21
  • You can use [`(;) = (>>>)`](https://www.reddit.com/r/haskelltil/comments/emwl7c/u037e_is_a_valid_operator/) ;) – Iceland_jack Feb 11 '22 at 03:57
  • @Iceland_jack Thanks for your input! Yeah, I've read [Alternative notations](https://en.wikipedia.org/wiki/Function_composition#Alternative_notations) in the Wikipedia article. It would be nice to use instead, but tricky to use Greek character ;) – Functor Feb 11 '22 at 05:58
  • @Iceland_jack I was going to object before reading the link. But now... I won't lie, I kind of like it, even if it's not as inconvenient to type. – chi Feb 11 '22 at 11:05
  • Just make sure you are using a font that distinguishes it from an ordinary semicolon. – chepner Feb 11 '22 at 14:27
  • You can use the chunky semicolon `⨾`, they are fun to try out and don't have to be used professionally, I personally stick to ascii – Iceland_jack Feb 11 '22 at 15:57
  • 1
    early categoricians tried to fix the absurd `.` mathematic notation and used `⨾` which they called "natural composition". `.` is the "wrong" way. they would have spared some errors.. – nicolas Feb 12 '22 at 15:38
  • `|>` is used in Ocaml, F#, and a few other languages. `&` is here but `|>` is more standard in programming. `⨾` in maths – nicolas Feb 12 '22 at 15:41
  • 1
    @nicolas Totally 100% agreed. That's the main point of this question! – Functor Feb 16 '22 at 23:14