Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the heck is "-<", anyway?

Tags:

haskell

monads

The other day I came across an operator, -<, appearing in the context of binding a value to a variable in a monad; specifically, it looks like this passes a value to a function which is then assigned (and possibly evaluated?) to a variable existing in the scope of a monad.

Example usage:

myMonad = do
    myVar <- f -< x

A few questions about this: first off, what's it called? The operator itself is un-googleable (Google doesn't like < or >), and the one name I've found, "arrow application", usually links to the other kind of Arrow present in Haskell, which seems unrelated.

Second, why is this operator not explained in most of the learning resources out there? Neither the "Gentle Introduction to Monads" nor LYAH give it so much as a passing mention, and most lists of Haskell operators forego it as well. (Is it considered bad practice?)

Finally, is this equivalent to using parentheses, as in, (myVar <- f -< x) == (myVar <- (f x))? Or is it more like using let myVar = (f x)? In either case, if this is how it's used, why use -< over parens or $?

like image 874
Jules Avatar asked May 16 '15 13:05

Jules


1 Answers

Yes, that is the same kind of arrow. There is sugar syntax for arrows just like there is sugar syntax for monads. You can't use Google to search for -<, but you can use Hoogle.

See: GHC Language Features, Arrow tutorial.

Arrow syntax is indeed poorly documented. The reason that you can't use $ instead of -< is that -< injects a pure value into the arrow, but -> is only one example of an arrow. So, $ works fine if your arrow is ->. There are also monadic arrows (the Kleisli constructor creates those) and you can make your own arrows, and in these cases -< is different.

like image 60
Dietrich Epp Avatar answered Sep 20 '22 16:09

Dietrich Epp