<- is the monadic bind operator inside do notation, binding a pattern to the value produced by a monadic action.

Syntax

(do
  ...
  (⟨pattern⟩ <- ⟨monadic-expr⟩)
  ...
  ⟨final-expr⟩)

Semantics

  • (⟨pattern⟩ <- ⟨monadic-expr⟩) is valid only as a non-final clause of a do expression; a do block cannot end with a <- clause.
  • ⟨monadic-expr⟩ must have type (:m :a), where :m is the Monad type constructor of the enclosing do block, and ⟨pattern⟩ must have type :a.
  • ⟨pattern⟩ may be a variable, _, a constructor pattern (such as (Tuple x y)), or an = alias pattern.
  • The clause desugars to a call to coalton/classes:>>=:
    (>>= ⟨monadic-expr⟩
         (fn (G)
           (match G
             (⟨pattern⟩ ⟨remaining-do-body⟩))))
    
  • Variables bound by ⟨pattern⟩ are in scope for all subsequent clauses and ⟨final-expr⟩ in the do block.
  • return is not permitted inside ⟨monadic-expr⟩.

Examples

;; Binding variables and destructuring a Tuple inside `do`
(coalton
  (do
    (x <- (Some (the Integer 10)))
    ((Tuple y z) <- (Some (Tuple 20 12)))
    (pure (+ x (+ y z)))))
; => 42

Other notes

  • For pure (non-monadic) bindings inside a do block, use (let ⟨pattern⟩ = ⟨expr⟩) instead of <-.
  • See do and need.