Haskell – 180 – monadi – 1

Continuo da qui, copio qui.

The most difficult concept to master while learning Haskell is that of understanding and using monads. We can distinguish two subcomponents here: (1) learning how to use existing monads and (2) learning how to write new ones. If you want to use Haskell, you must learn to use existing monads. On the other hand, you will only need to learn to write your own monads if you want to become a “super Haskell guru.” Still, if you can grasp writing your own monads, programming in Haskell will be much more pleasant.

So far we’ve seen two uses of monads. The first use was IO actions: We’ve seen that, by using monads, we can get away from the problems plaguing the RealWorld solution to IO presented in the chapter IO [qui e post seguenti]. The second use was representing different types of computations in the section on Classes-computations [qui]. In both cases, we needed a way to sequence operations and saw that a sufficient definition (at least for computations) was:

class Computation c where
  success :: a -> c a
  failure :: String -> c a
  augment :: c a -> (a -> c b) -> c b
  combine :: c a -> c a -> c a

Let’s see if this definition will enable us to also perform IO. Essentially, we need a way to represent taking a value out of an action and performing some new operation on it (as in the example from the section on Functions-io, rephrased slightly):

main = do
  s <- readFile "somefile"
  putStrLn (show (f s))

But this is exactly what augment does. Using augment, we can write the above code as:

main =  -- note the lack of a "do"
  readFile "somefile" `augment` \s ->
  putStrLn (show (f s))

This certainly seems to be sufficient. And, in fact, it turns out to be more than sufficient.

The definition of a monad is a slightly trimmed-down version of our Computation class. The Monad class has four methods (but the fourth method can be defined in terms of the third):

class Monad m where
  return  :: a -> m a
  fail    :: String -> m a
  (>>=)   :: m a -> (a -> m b) -> m b
  (>>)    :: m a -> m b -> m b

In this definition, return is equivalent to our success; fail is equivalent to our failure; and >>= (read: “bind” ) is equivalent to our augment. The >> (read: “then” ) method is simply a version of >>= that ignores the a. This will turn out to be useful; although, as mentioned before, it can be defined in terms of >>=:

a >> x = a >>= \_ -> x

Fin qui sembra tutto OK 😋


Posta un commento o usa questo indirizzo per il trackback.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: