20-CS-4003-001 Organization of Programming Languages Fall 2018

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code     Motivation


class Monad m where
  return :: a -> m a

  (>>=) :: m a -> (a -> m b) -> m b

  (>>) :: m a -> m b -> m b
  x >> y = x >>= \_ -> y

  fail :: String -> m a
  fail msg = error msg
  -   Monads are a type class consisting of at least four operators return, >>=, >>, and fail as shown to the left. These and others will be described in detail later. For now we mention 1) the >>= operator (called bind) either returns "Nothing" if it is passed "Nothing" as it's 1st argument; or if it's 1st argument is a "Just ..." it strips off the Just, and passes the contents into the function supplied as it's 2nd argument; 2) the return operator takes a value from a plain type and puts it into a monadic container (that is, does the opposite of >>; 3) the >> operator is essentially the same as >>=, but does not pass the contents of the monad to the function specified as 2nd argument; 4) the fail operator is used when a condition is not satisfied.

The operator filerM filters out all elements of 2nd argument xs that do not fulfill the 1st argument predicate p, where p is a function that returns a value of type Monad m => m Bool. the result is a list inside a monad of the same type. For example, v10 to the left has value Just [2,1]. The type of filterM is filterM :: Monad m => (a -> m Bool) -> [a] -> m [a]

The operator foldM is the monadic version of foldl: that is, it takes a function of type (Monad m) => (a -> b -> m a). To the left the function ./. is defined to take care of divide-by-zero errors. The function divide uses foldM to repeatedly divide by a list of numbers. The value of v11 is Just 2.0 and the value of v12 is Nothing. The type of foldM is foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a

The operator guard returns () if its argument is True, otherwise it returns mzero. The value of v13 is Just 3 and the value of v14 is Nothing.

The operator liftM lets a non-monadic function (1st argument) operate on the contents of a monad (2nd argument). To the left, v15 has value Just 23 and v16 has value Just [24,25]. The type of liftM is liftM :: Monad m => (a -> b) -> m a -> m b

The operator mapM applies a monadic function (1st argument) of type Monad m => (a -> m b) to each element of a list (2nd argument) resulting in a list inside a monad. The type of mapM is mapM :: Monad m => (a -> m b) -> [a] -> m [b]

The operator mzero is the zero of the MonadPlus class. To the left, the value of v17 is [] and the value of v18 is Nothing. Its type is mzero :: MonadPlus m => m a.

The operator mplus is the plus of the MonadPlus class. To the left, v19 has value "ab", v20 has value Just "a", v21 has value Just 10, v22 has value Just 10, v23 has value Just 10.

The operator sequence evaluates all monadic values in the input list from left to right and returns a list of the "contents" of these monads, placing this list in a monad of the same type. Evaluating can be interpreted as "performing an action", for example in the case of print. To the left v27 displays 1 2 3 [(),(),()], and v28 displays Just [1,2,3]. The type of sequence is sequence :: Monad m => [m a] -> m [a].

The operator unless executes a monadic expression (2nd argument) when the first argument evaluates to False. Looking at v29 shows "OK". The type of unless is unless :: Monad m => Bool -> m () -> m ()

The operator when executes a monadic expression (2nd argument) when the first argument evaluates to True. Looking at v30 shows "OK". The type of when is when :: Monad m => Bool -> m () -> m ()

Monads are useful in any situation where the programmer wants to carry out a purely functional computation while a related computation is carried out on the side. In imperative programming the side effects are embedded in the semantics of the programming language; with monads, they are made explicit in the monad definition, thus avoiding errors by action at a distance.