liveBook · Manning

Do notation

As explained in Type-Driven Development with Idris

Idris syntax for writing sequences of effectful computations in a more readable form than explicit use of the bind operator.

Idris do notation makes sequences of computations easier to read than explicit applications of the bind operator, `>>=`. A do block lists computations in order, allowing later steps to use earlier results. Idris translates the notation into applications of `>>=`; the translation is syntactic rather than specific to IO. section 5.1.3 section 6.3.7

printLength : IO ()
printLength = do putStr "Input string: "
                 input <- getLine                        #1
                 let len = length input                  #2
                 putStrLn (show len)

This `IO ()` program prompts for a string, reads it, and prints its length. section 5.1.3

In `input <- getLine`, the action `getLine : IO String` is executed and its result is bound to `input`. By contrast, `let len = length input` names the result of a pure expression. An action can also appear without a binding when its result is not needed, as with the prompt above. section 5.1.3

From do to bind

The block `do x <- getLine; putStrLn x` corresponds to `getLine >>= \x => putStrLn x`. The continuation receives the string produced by the first action. At the REPL, use `:exec` to execute an IO action. section 5.1.3 section 5.1.1

The same syntax works with `Maybe`. Its bind passes a `Just` value to the next computation, but returns `Nothing` when a step fails. This lets a do block concentrate on the successful path instead of nesting case expressions. section 6.3.7

maybeAdd : Maybe Int -> Maybe Int -> Maybe Int
maybeAdd x y = do x_val <- x                           #1
                  y_val <- y                           #2
                  Just (x_val + y_val)

Both inputs must be `Just` values for `maybeAdd` to produce their sum; either `Nothing` makes the result `Nothing`. section 6.3.7

A binding can also match a pattern, as in `(str1, str2) <- readPair`. For a pattern such as `Just num <- readNumber`, account for the `Nothing` alternative: a definition can type-check yet fail at runtime if a case is missing. Use `:total` to check for missing cases. section 5.2.2

readNumbers : IO (Maybe (Nat, Nat))
readNumbers =   
  do Just num1_ok <- readNumber | Nothing => pure Nothing          #1
     Just num2_ok <- readNumber | Nothing => pure Nothing          #2
     pure (Just (num1_ok, num2_ok))                                #3

Each pattern binding supplies a `Nothing` alternative, so invalid input returns `pure Nothing` instead of causing an unmatched case. section 5.2.2

Because do notation translates to `>>=`, it can also be used in other contexts that define the operator, including custom data types. section 6.3.7 section 11.4

How Type-Driven Development with Idris covers it

How deep it goes
Explains translation to bind, pure and action bindings, and pattern alternatives; defers the general interface behind bind.
Examples
Works through printing input length, adding Maybe values, and reading two validated numbers.
What it assumes
Introduces IO sequencing with bind before do notation, then extends it to Maybe without requiring the general interface explanation.
What you can do after
Apply it in Idris to sequence IO or Maybe computations and handle alternatives in pattern bindings.
How it connects
Connects bind, IO, Maybe, pattern matching, pure expressions, and totality checking.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.