Documentation on scalaz.Bind

45 views
Skip to first unread message

"Dr. Normen Müller"

unread,
Jun 12, 2015, 6:50:13 AM6/12/15
to sca...@googlegroups.com
Hi,
 
It about the documentation on scalaz.Bind:

>An Apply functor, where a lifted function can introduce new values _and_ new functor context to be incorporated into the lift context.  The essential new operation of Monads.

First, let's have a look at scalaz.Apply

def ap[A,B](fa: => F[A])(f: => F[A => B]): F[B]

If I use the above comment on scalaz.Apply (instead of scalaz.Bind),
... I can identify a lifted function, namely f.
... I can identify the introduction of new values, namely values of type B
... I cannot identify a new functor context, there is only one context, namely F.
... I am unclear about "the lift context"

Well, as this comment is for scalaz.Bind and not for scalaz.Apply and as scalaz.Bind extends scalaz.Apply, it's OK that this comment only partially fits at this place.

So, let’s move on and have a look at scalaz.Bind

def bind[A, B] (fa: F[A]) (f: A => F[B]): F[B]

... I cannot identify a lifted function; f is not a lifted function;
... I can identify the introduction of new values, namely values of type F[B]
... I cannot identify a new functor context, there is only one context, namely F.

I guess one of my main problems is, that I do not quite understand the term “new” here. In other words, my main concerns are with "a new functor context".

Does "new" refer to immutability? 

Let's consider List

def bind[A, B] (fa: List[A]) (f: A => List[B]): List[B]

If "new functor context" refers to the fact, that for each element a of the list fa a new list (of type List[B]) is created (due to immutability of fa), than I'm more or less fine with it… 

However I still think that "new" is a bit misleading here. It's still the same type of context (it's still a List) but a new instance of such a context is created. For me, a "new context" would be something different from List — but also forming a Bind — e.g., Option

Hope one can clear things up a little bit for me ;-) In the end, I’d like to fully understand the comment on scalaz.Bind.

Cheers, /nm

Erik Post

unread,
Jun 12, 2015, 10:05:34 PM6/12/15
to sca...@googlegroups.com
Hi Normen,

Regarding the 'new functor context': bind takes an f: A => F[B]. This means that f gets to determine what sort of F to return. For example, in the case where F is Option, f might return None or Some(...) as it pleases.

For ap, on the contrary, we have a parameter f: => F[A => B]. The function of type A => B here is *already inside* of some given context F. The function has no means of accessing or affecting this context, which is fixed when ap is called. (In the case of bind, it is determined not when bind is called, but when its f parameter is evaluated.) As an example, let's say that f = Some((x: Int) => x.toString). The function Int => String here cannot change the fact that it is inside of a Some; it cannot conceivably return anything to turn that Some into a None.

Hope this helps.

Cheers,
Erik

"Dr. Normen Müller"

unread,
Jun 13, 2015, 1:56:17 AM6/13/15
to sca...@googlegroups.com
Hi, thanks Erik for your help! Cheers, /nm

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+un...@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
Visit this group at http://groups.google.com/group/scalaz.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages