Is there a standard name (and library implementation) for a Monad that hides computation behind a constructor?

Viewed 147

I am wondering if this monad has a standard name in the Haskell ecosystem

data Delay a = Wait (Delay a) | Done a deriving (Show, Eq, Functor)

instance Monad Delay where
  return a = Done a

  (Done a) >>= f = f a
  (Wait da) >>= f = Wait (da >>= f)

It is useful so that possibly non-terminating computation can be "paused".

The end goal is to combine it with LogicT so I can search over possibly non terminating functions. I rolled my own implementation originally but the book-keeping was out of hand, especially since I have other monadic effects in the mix.

1 Answers

Delay looks like it is isomorphic to Iter from Control.Monad.Trans.Iter in the free library.

newtype IterT m a = IterT { runIterT :: m (Either a (IterT m a)) }

type Iter = IterT Identity

instance Monad m => Monad (IterT m) where
  return = pure
  IterT m >>= k = IterT $ m >>= either (runIterT . k) (return . Right . (>>= k))
  fail _ = never

Specifically, Done a corresponds to IterT Identity (Left a) and Wait (Delay a) to IterT Identity (Right (IterT Identity a)).

Related