Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 609
user833970 Avatar asked Aug 12 '20 13:08

user833970


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)).

like image 127
chepner Avatar answered Sep 22 '22 00:09

chepner