简体   繁体   中英

terms of IO inside Haskell

Reading the internal

https://wiki.haskell.org/IO_inside

class Monad m where
    unit :: a -> m a
    bind :: m a -> (a -> m b) -> m b

I observe the term unit seems no longer used, and pure is the term now. Why? because I think pure is not adequate for such a recursive structure.

(>>=) is called bind here, and how do we call (>>) ?

The name pure is based on the idea that the monad (or applicative) represents some kind of effectful, "impure" computation in general, and that if you'd like to use a pure value/calculation as a component in your larger impure computation, you can embed the pure value/calculation within the impure computation with pure .

For example, if you have some applicative expression:

f <*> a <*> b

where f , a , and b are all impure applicative computations, and you'd like to replace the impure computation a with a pure value, you write:

f <*> pure 2 <*> b

and it's clear that you're passing a "pure" value of 2, or a "pure 2" as the first argument to the applicative function f .

As per @chepner's comment, the name unit was never used in actual Haskell implementations of monads or applicatives. That name comes from the mathematical definition, where monads can be defined as a type of monoid, and the "unit" operation is the identity element for the monoid. Identity elements are sometimes called units, presumably because the multiplicative identity in, say, the real numbers is "1" AKA unity AKA a unit.

Also as per @chepner, for historical reasons, there are two Haskell names for this operation -- pure is the the name used in the Applicative typeclass while return is the name used in the Monad typeclass. For all correct implementations of monads, these two operations do the same thing. The name return was probably chosen with the idea that a monadic computation of type Monad m => ma is a little "program" that might deal with some effects and then eventually return an a . If all you want to do is return an x:: a without dealing with any effects, you just return x .

The operator >> doesn't really have an official name. People usually use "and then" or simply "then" (see this answer ). This comes from the idea that, when a monad is interpretable as a sequence of operations (eg, the IO monad), the effect of a >> b is to first do a and then do b . There's also an operator *> that does the same thing (again, mostly for historical reasons -- >> for Monad and *> for Applicative ), and it's also usually called "then".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM