monadic 计算中的短路 "uninteresting" 个案例

Short-circuiting "uninteresting" cases in monadic computation

我有一个像这样使用 Data.Maybe.monad 的函数:

typeCheck ν (f · e) =
  typeCheck ν e >>= λ { (u , e′) →
  typeCheck ν f >>= λ { (u′ ▷ t , f′) →
  u !≡ₜ u′      >>= λ { refl →
  pure (, (f′ · e′)) };
  _ → nothing }}

有没有办法消除 _ → nothing 案例,或者至少将其进一步向上移动(类似于 Idris)以获得类似于以下内容的内容:

typeCheck ν (f · e) =
  typeCheck ν e >>= λ { (u , e′) →
  typeCheck ν f >>= λ { _ → nothing; (u′ ▷ t , f′) →
  u ≡!ₜ u′      >>= λ { refl →
  pure (, (f′ · e′)) }}}

Agda 现在有 do-notation。文档中的示例:

infer Γ (app e e₁) = do
  s ofType A => B ← infer Γ e
    where _ ofType nat → typeError "numbers cannot be applied to arguments"
  t ofType A₁     ← infer Γ e₁
  refl            ← A =?= A₁
  pure (app s t ofType B)