{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
#ifndef MIN_VERSION_mtl
#define MIN_VERSION_mtl(x,y,z) 0
#endif
-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Machine.Plan
-- Copyright   :  (C) 2012 Edward Kmett, Rúnar Bjarnason
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  Rank-N Types, MPTCs
--
----------------------------------------------------------------------------
module Data.Machine.Plan
  (
  -- * Plans
    Plan
  , runPlan
  , PlanT(..)
  , yield
  , maybeYield
  , await
  , stop
  , awaits
  , exhaust
  ) where

import Control.Applicative
import Control.Category
import Control.Monad (MonadPlus(..))
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
import Control.Monad.State.Class
import Control.Monad.Reader.Class
import Control.Monad.Error.Class
import qualified Control.Monad.Fail as Fail
import Control.Monad.Writer.Class
import Data.Functor.Identity
import Prelude hiding ((.),id)

-------------------------------------------------------------------------------
-- Plans
-------------------------------------------------------------------------------

-- | You can 'construct' a 'Plan' (or 'PlanT'), turning it into a
-- 'Data.Machine.Type.Machine' (or 'Data.Machine.Type.MachineT').
--
newtype PlanT k o m a = PlanT
  { PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT :: forall r.
      (a -> m r) ->                                     -- Done a
      (o -> m r -> m r) ->                              -- Yield o (Plan k o a)
      (forall z. (z -> m r) -> k z -> m r -> m r) ->    -- forall z. Await (z -> Plan k o a) (k z) (Plan k o a)
      m r ->                                            -- Fail
      m r
  }

-- | A @'Plan' k o a@ is a specification for a pure 'Machine', that reads inputs selected by @k@
-- with types based on @i@, writes values of type @o@, and has intermediate results of type @a@.
--
-- A @'Plan' k o a@ can be used as a @'PlanT' k o m a@ for any @'Monad' m@.
--
-- It is perhaps easier to think of 'Plan' in its un-cps'ed form, which would
-- look like:
--
-- @
-- data 'Plan' k o a
--   = Done a
--   | Yield o (Plan k o a)
--   | forall z. Await (z -> Plan k o a) (k z) (Plan k o a)
--   | Fail
-- @
type Plan k o a = forall m. PlanT k o m a

-- | Deconstruct a 'Plan' without reference to a 'Monad'.
runPlan :: PlanT k o Identity a
        -> (a -> r)
        -> (o -> r -> r)
        -> (forall z. (z -> r) -> k z -> r -> r)
        -> r
        -> r
runPlan :: PlanT k o Identity a
-> (a -> r)
-> (o -> r -> r)
-> (forall z. (z -> r) -> k z -> r -> r)
-> r
-> r
runPlan PlanT k o Identity a
m a -> r
kp o -> r -> r
ke forall z. (z -> r) -> k z -> r -> r
kr r
kf = Identity r -> r
forall a. Identity a -> a
runIdentity (Identity r -> r) -> Identity r -> r
forall a b. (a -> b) -> a -> b
$ PlanT k o Identity a
-> (a -> Identity r)
-> (o -> Identity r -> Identity r)
-> (forall z. (z -> Identity r) -> k z -> Identity r -> Identity r)
-> Identity r
-> Identity r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o Identity a
m
  (r -> Identity r
forall a. a -> Identity a
Identity (r -> Identity r) -> (a -> r) -> a -> Identity r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> r
kp)
  (\o
o (Identity r
r) -> r -> Identity r
forall a. a -> Identity a
Identity (o -> r -> r
ke o
o r
r))
  (\z -> Identity r
f k z
k (Identity r
r) -> r -> Identity r
forall a. a -> Identity a
Identity ((z -> r) -> k z -> r -> r
forall z. (z -> r) -> k z -> r -> r
kr (Identity r -> r
forall a. Identity a -> a
runIdentity (Identity r -> r) -> (z -> Identity r) -> z -> r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. z -> Identity r
f) k z
k r
r))
  (r -> Identity r
forall a. a -> Identity a
Identity r
kf)
{-# INLINE runPlan #-}

instance Functor (PlanT k o m) where
  fmap :: (a -> b) -> PlanT k o m a -> PlanT k o m b
fmap a -> b
f (PlanT forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
m) = (forall r.
 (b -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m b
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (b -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m b)
-> (forall r.
    (b -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m b
forall a b. (a -> b) -> a -> b
$ \b -> m r
k -> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
m (b -> m r
k (b -> m r) -> (a -> b) -> a -> m r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
f)
  {-# INLINE fmap #-}

instance Applicative (PlanT k o m) where
  pure :: a -> PlanT k o m a
pure a
a = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\a -> m r
kp o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
_ m r
_ -> a -> m r
kp a
a)
  {-# INLINE pure #-}
  PlanT k o m (a -> b)
m <*> :: PlanT k o m (a -> b) -> PlanT k o m a -> PlanT k o m b
<*> PlanT k o m a
n = (forall r.
 (b -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m b
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (b -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m b)
-> (forall r.
    (b -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m b
forall a b. (a -> b) -> a -> b
$ \b -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> PlanT k o m (a -> b)
-> ((a -> b) -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m (a -> b)
m (\a -> b
f -> PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m a
n (\a
a -> b -> m r
kp (a -> b
f a
a)) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf
  {-# INLINE (<*>) #-}
  PlanT k o m a
m *> :: PlanT k o m a -> PlanT k o m b -> PlanT k o m b
*> PlanT k o m b
n = (forall r.
 (b -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m b
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (b -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m b)
-> (forall r.
    (b -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m b
forall a b. (a -> b) -> a -> b
$ \b -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m a
m (\a
_ -> PlanT k o m b
-> (b -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m b
n b -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf
  {-# INLINE (*>) #-}
  PlanT k o m a
m <* :: PlanT k o m a -> PlanT k o m b -> PlanT k o m a
<* PlanT k o m b
n = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m a
m (\a
a -> PlanT k o m b
-> (b -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m b
n (\b
_ -> a -> m r
kp a
a) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf
  {-# INLINE (<*) #-}

instance Alternative (PlanT k o m) where
  empty :: PlanT k o m a
empty = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
_ o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
_ m r
kf -> m r
kf
  {-# INLINE empty #-}
  PlanT forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
m <|> :: PlanT k o m a -> PlanT k o m a -> PlanT k o m a
<|> PlanT forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
n = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
m a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr ((a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
n a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf)
  {-# INLINE (<|>) #-}

instance Monad (PlanT k o m) where
  return :: a -> PlanT k o m a
return = a -> PlanT k o m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  {-# INLINE return #-}
  PlanT forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
m >>= :: PlanT k o m a -> (a -> PlanT k o m b) -> PlanT k o m b
>>= a -> PlanT k o m b
f = (forall r.
 (b -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m b
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\b -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall r.
(a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
m (\a
a -> PlanT k o m b
-> (b -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT (a -> PlanT k o m b
f a
a) b -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf)
  {-# INLINE (>>=) #-}
  >> :: PlanT k o m a -> PlanT k o m b -> PlanT k o m b
(>>) = PlanT k o m a -> PlanT k o m b -> PlanT k o m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
  {-# INLINE (>>) #-}
#if !(MIN_VERSION_base(4,13,0))
  fail = Fail.fail
#endif

instance Fail.MonadFail (PlanT k o m) where
  fail :: String -> PlanT k o m a
fail String
_ = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\a -> m r
_ o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
_ m r
kf -> m r
kf)

instance MonadPlus (PlanT k o m) where
  mzero :: PlanT k o m a
mzero = PlanT k o m a
forall (f :: * -> *) a. Alternative f => f a
empty
  {-# INLINE mzero #-}
  mplus :: PlanT k o m a -> PlanT k o m a -> PlanT k o m a
mplus = PlanT k o m a -> PlanT k o m a -> PlanT k o m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
  {-# INLINE mplus #-}

instance MonadTrans (PlanT k o) where
  lift :: m a -> PlanT k o m a
lift m a
m = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\a -> m r
kp o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
_ m r
_ -> m a
m m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m r
kp)
  {-# INLINE lift #-}

instance MonadIO m => MonadIO (PlanT k o m) where
  liftIO :: IO a -> PlanT k o m a
liftIO IO a
m = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\a -> m r
kp o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
_ m r
_ -> IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
m m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m r
kp)
  {-# INLINE liftIO #-}

instance MonadState s m => MonadState s (PlanT k o m) where
  get :: PlanT k o m s
get = m s -> PlanT k o m s
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m s
forall s (m :: * -> *). MonadState s m => m s
get
  {-# INLINE get #-}
  put :: s -> PlanT k o m ()
put = m () -> PlanT k o m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> PlanT k o m ()) -> (s -> m ()) -> s -> PlanT k o m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put
  {-# INLINE put #-}
#if MIN_VERSION_mtl(2,1,0)
  state :: (s -> (a, s)) -> PlanT k o m a
state s -> (a, s)
f = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
kp o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
_ m r
_ -> (s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state s -> (a, s)
f m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m r
kp
  {-# INLINE state #-}
#endif

instance MonadReader e m => MonadReader e (PlanT k o m) where
  ask :: PlanT k o m e
ask = m e -> PlanT k o m e
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m e
forall r (m :: * -> *). MonadReader r m => m r
ask
#if MIN_VERSION_mtl(2,1,0)
  reader :: (e -> a) -> PlanT k o m a
reader = m a -> PlanT k o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> PlanT k o m a)
-> ((e -> a) -> m a) -> (e -> a) -> PlanT k o m a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (e -> a) -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
#endif
  local :: (e -> e) -> PlanT k o m a -> PlanT k o m a
local e -> e
f PlanT k o m a
m = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> (e -> e) -> m r -> m r
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local e -> e
f (PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m a
m a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf)

instance MonadWriter w m  => MonadWriter w (PlanT k o m) where
#if MIN_VERSION_mtl(2,1,0)
  writer :: (a, w) -> PlanT k o m a
writer = m a -> PlanT k o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> PlanT k o m a)
-> ((a, w) -> m a) -> (a, w) -> PlanT k o m a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a, w) -> m a
forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
#endif
  tell :: w -> PlanT k o m ()
tell   = m () -> PlanT k o m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> PlanT k o m ()) -> (w -> m ()) -> w -> PlanT k o m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. w -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell

  listen :: PlanT k o m a -> PlanT k o m (a, w)
listen PlanT k o m a
m = (forall r.
 ((a, w) -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m (a, w)
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  ((a, w) -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m (a, w))
-> (forall r.
    ((a, w) -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m (a, w)
forall a b. (a -> b) -> a -> b
$ \(a, w) -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m a
m (((a, w) -> m r
kp ((a, w) -> m r) -> m (a, w) -> m r
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (m (a, w) -> m r) -> (a -> m (a, w)) -> a -> m r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. m a -> m (a, w)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen (m a -> m (a, w)) -> (a -> m a) -> a -> m (a, w)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf

  pass :: PlanT k o m (a, w -> w) -> PlanT k o m a
pass PlanT k o m (a, w -> w)
m = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> PlanT k o m (a, w -> w)
-> ((a, w -> w) -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m (a, w -> w)
m ((a -> m r
kp (a -> m r) -> m a -> m r
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (m a -> m r) -> ((a, w -> w) -> m a) -> (a, w -> w) -> m r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. m (a, w -> w) -> m a
forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass (m (a, w -> w) -> m a)
-> ((a, w -> w) -> m (a, w -> w)) -> (a, w -> w) -> m a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a, w -> w) -> m (a, w -> w)
forall (m :: * -> *) a. Monad m => a -> m a
return) o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf

instance MonadError e m => MonadError e (PlanT k o m) where
  throwError :: e -> PlanT k o m a
throwError = m a -> PlanT k o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> PlanT k o m a) -> (e -> m a) -> e -> PlanT k o m a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
  catchError :: PlanT k o m a -> (e -> PlanT k o m a) -> PlanT k o m a
catchError PlanT k o m a
m e -> PlanT k o m a
k = (forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (a -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m a)
-> (forall r.
    (a -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m a
forall a b. (a -> b) -> a -> b
$ \a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf -> PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT PlanT k o m a
m a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf m r -> (e -> m r) -> m r
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` \e
e -> PlanT k o m a
-> (a -> m r)
-> (o -> m r -> m r)
-> (forall z. (z -> m r) -> k z -> m r -> m r)
-> m r
-> m r
forall (k :: * -> *) o (m :: * -> *) a.
PlanT k o m a
-> forall r.
   (a -> m r)
   -> (o -> m r -> m r)
   -> (forall z. (z -> m r) -> k z -> m r -> m r)
   -> m r
   -> m r
runPlanT (e -> PlanT k o m a
k e
e) a -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
kr m r
kf

-- | Output a result.
yield :: o -> Plan k o ()
yield :: o -> Plan k o ()
yield o
o = (forall r.
 (() -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m ()
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\() -> m r
kp o -> m r -> m r
ke forall z. (z -> m r) -> k z -> m r -> m r
_ m r
_ -> o -> m r -> m r
ke o
o (() -> m r
kp ()))

-- | Like yield, except stops if there is no value to yield.
maybeYield :: Maybe o -> Plan k o ()
maybeYield :: Maybe o -> Plan k o ()
maybeYield Maybe o
m = PlanT k o m ()
-> (o -> PlanT k o m ()) -> Maybe o -> PlanT k o m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PlanT k o m ()
forall (k :: * -> *) o a. Plan k o a
stop (\o
x -> o -> Plan k o ()
forall o (k :: * -> *). o -> Plan k o ()
yield o
x) Maybe o
m

-- | Wait for input.
--
-- @'await' = 'awaits' 'id'@
await :: Category k => Plan (k i) o i
await :: Plan (k i) o i
await = (forall r.
 (i -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k i z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT (k i) o m i
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT (\i -> m r
kp o -> m r -> m r
_ forall z. (z -> m r) -> k i z -> m r -> m r
kr m r
kf -> (i -> m r) -> k i i -> m r -> m r
forall z. (z -> m r) -> k i z -> m r -> m r
kr i -> m r
kp k i i
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id m r
kf)

-- | Wait for a particular input.
--
-- @
-- awaits 'L'  :: 'Plan' ('T' a b) o a
-- awaits 'R'  :: 'Plan' ('T' a b) o b
-- awaits 'id' :: 'Plan' ('Data.Machine.Is.Is' i) o i
-- @
awaits :: k i -> Plan k o i
awaits :: k i -> Plan k o i
awaits k i
h = (forall r.
 (i -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m i
forall (k :: * -> *) o (m :: * -> *) a.
(forall r.
 (a -> m r)
 -> (o -> m r -> m r)
 -> (forall z. (z -> m r) -> k z -> m r -> m r)
 -> m r
 -> m r)
-> PlanT k o m a
PlanT ((forall r.
  (i -> m r)
  -> (o -> m r -> m r)
  -> (forall z. (z -> m r) -> k z -> m r -> m r)
  -> m r
  -> m r)
 -> PlanT k o m i)
-> (forall r.
    (i -> m r)
    -> (o -> m r -> m r)
    -> (forall z. (z -> m r) -> k z -> m r -> m r)
    -> m r
    -> m r)
-> PlanT k o m i
forall a b. (a -> b) -> a -> b
$ \i -> m r
kp o -> m r -> m r
_ forall z. (z -> m r) -> k z -> m r -> m r
kr -> (i -> m r) -> k i -> m r -> m r
forall z. (z -> m r) -> k z -> m r -> m r
kr i -> m r
kp k i
h

-- | @'stop' = 'empty'@
stop :: Plan k o a
stop :: PlanT k o m a
stop = PlanT k o m a
forall (f :: * -> *) a. Alternative f => f a
empty

-- | Run a monadic action repeatedly yielding its results, until it returns Nothing.
exhaust :: Monad m => m (Maybe a) -> PlanT k a m ()
exhaust :: m (Maybe a) -> PlanT k a m ()
exhaust m (Maybe a)
f = do
  Maybe a
x <- m (Maybe a) -> PlanT k a m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe a)
f
  Maybe a -> Plan k a ()
forall o (k :: * -> *). Maybe o -> Plan k o ()
maybeYield Maybe a
x
  m (Maybe a) -> PlanT k a m ()
forall (m :: * -> *) a (k :: * -> *).
Monad m =>
m (Maybe a) -> PlanT k a m ()
exhaust m (Maybe a)
f