    {- | Implementation of monads that allow the computation
    to 'Control.Monad.Prompt.prompt' for further input.

    (c) 2008 Bertram Felgenhauer & Ryan Ingram
    Released as open source under a 3 clause BSD license. See the LICENSE
    file in the source code distribution for further information.

    RecPromptT added by Cale Gibbard, contributed under the same license.

    MonadPrompt monads allow you to pass some object of the prompt
    type in, and get a result of the prompt's answer type out.
    -}

    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE FunctionalDependencies #-}
    module Control.Monad.Prompt (
        MonadPrompt(..),
        Prompt,
          runPromptC,
          runPrompt,
          runPromptM,
        RecPrompt,
          unRecPrompt,
          runRecPromptC,
          runRecPrompt,
          runRecPromptM,
        PromptT,
          runPromptT,
          runPromptTM,
          runPromptTM',
          Lift(..),
          unPromptT,
          liftP,
        RecPromptT,
          unRecPromptT,
          runRecPromptT

    ) where
    import Control.Applicative (Applicative(..))
    import Control.Monad (ap, liftM)
    import Control.Monad.Trans (MonadTrans(..))

    {- |You can construct a monad very simply with prompt, by putting
    all of its effects as terms in a GADT, like the following example:

    @
    data PromptState s a where
        Put :: s -> PromptState s ()
        Get :: PromptState s s
    @

    You then use 'prompt' to access effects:

    @
    postIncrement :: MonadPrompt (PromptState Int) m => m Int
    postIncrement =
      do x <- prompt Get
         prompt (Put (x+1))
         return x
    @

    The advantage of Prompt over implementing effects directly:

    1. Prompt is pure; it is only through the observation function
       runPromptC that you can cause effects.

    2. You don't have to worry about the monad laws; they are
       correct by construction and you cannot break them.

    3. You can implement several observation functions for the same
       type.  See, for example, <http://paste.lisp.org/display/53766>
       where a guessing game is implemented with an IO observation
       function for the user, and an AI observation function that
       plays the game automatically.

    In these ways Prompt is similar to Unimo, but bind and return
    are inlined into the computation, whereas in Unimo they are
    handled as a term calculus.
    See <http://sneezy.cs.nott.ac.uk/fplunch/weblog/?p=89>
    -}
    class Monad m => MonadPrompt p m | m -> p where
        prompt :: p a -> m a
    {-
    For any prompt p, Prompt p is an instance of MonadPrompt p.
    -}
    newtype Prompt p r = Prompt {
        forall (p :: * -> *) r.
    Prompt p r
    -> forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    runP :: forall b . (r -> b) -> (forall a . p a -> (a -> b) -> b) -> b
    }

    instance Monad (Prompt p) where
        return :: forall a. a -> Prompt p a
    return a
    a = (forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p a
    forall (p :: * -> *) r.
    (forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p r
    Prompt ((forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
     -> Prompt p a)
    -> (forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p a
    forall a b. (a -> b) -> a -> b
    $ \a -> b
    done forall a. p a -> (a -> b) -> b
    _   -> a -> b
    done a
    a
        Prompt p a
    f >>= :: forall a b. Prompt p a -> (a -> Prompt p b) -> Prompt p b
    >>= a -> Prompt p b
    g  = (forall b. (b -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p b
    forall (p :: * -> *) r.
    (forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p r
    Prompt ((forall b. (b -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
     -> Prompt p b)
    -> (forall b. (b -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p b
    forall a b. (a -> b) -> a -> b
    $ \b -> b
    done forall a. p a -> (a -> b) -> b
    prm -> Prompt p a
    -> forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    forall (p :: * -> *) r.
    Prompt p r
    -> forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    runP Prompt p a
    f (\a
    x -> Prompt p b
    -> forall b. (b -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    forall (p :: * -> *) r.
    Prompt p r
    -> forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    runP (a -> Prompt p b
    g a
    x) b -> b
    done p a -> (a -> b) -> b
    forall a. p a -> (a -> b) -> b
    prm) p a -> (a -> b) -> b
    forall a. p a -> (a -> b) -> b
    prm

    instance Functor (Prompt p) where
        fmap :: forall a b. (a -> b) -> Prompt p a -> Prompt p b
    fmap = (a -> b) -> Prompt p a -> Prompt p b
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM

    instance Applicative (Prompt p) where
        pure :: forall a. a -> Prompt p a
    pure  = a -> Prompt p a
    forall a. a -> Prompt p a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
        <*> :: forall a b. Prompt p (a -> b) -> Prompt p a -> Prompt p b
    (<*>) = Prompt p (a -> b) -> Prompt p a -> Prompt p b
    forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
    ap

    instance MonadPrompt p (Prompt p) where
        prompt :: forall a. p a -> Prompt p a
    prompt p a
    p = (forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p a
    forall (p :: * -> *) r.
    (forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p r
    Prompt ((forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
     -> Prompt p a)
    -> (forall b. (a -> b) -> (forall a. p a -> (a -> b) -> b) -> b)
    -> Prompt p a
    forall a b. (a -> b) -> a -> b
    $ \a -> b
    done forall a. p a -> (a -> b) -> b
    prm -> p a -> (a -> b) -> b
    forall a. p a -> (a -> b) -> b
    prm p a
    p a -> b
    done

    {- |'runPromptC' is the observation function for prompts.  It takes
    two functions as arguments:

    1. @ret@ will be called with the final result of the computation,
       to convert it to the answer type.

    2. @prm@ will be called if there are any effects; it is passed
       a prompt and a continuation function.  prm can apply
       the effect requested by the prompt and call the continuation.

    In some cases prm can return the answer type directly; it
    may be useful to abort the remainder of the computation, or
    save off the continuation to be called later.  There is a great
    example of using this to implement a UI for peg solitaire in Bertram
    Felgenhauer's post to Haskell-Cafe at
    <http://www.haskell.org/pipermail/haskell-cafe/2008-January/038301.html>
    -}

    runPromptC :: forall p r b. -- prompt, computation result, answer type
                  (r -> b)      -- ^ handler when there is no further computation
               -> (forall a . p a -> (a -> b) -> b)
                                -- ^ handler for prompts
               -> Prompt p r    -- ^ a prompt-based computation
               -> b             -- ^ answer
    runPromptC :: forall (p :: * -> *) r b.
    (r -> b) -> (forall a. p a -> (a -> b) -> b) -> Prompt p r -> b
    runPromptC r -> b
    ret forall a. p a -> (a -> b) -> b
    prm Prompt p r
    p = Prompt p r
    -> forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    forall (p :: * -> *) r.
    Prompt p r
    -> forall b. (r -> b) -> (forall a. p a -> (a -> b) -> b) -> b
    runP Prompt p r
    p r -> b
    ret p a -> (a -> b) -> b
    forall a. p a -> (a -> b) -> b
    prm

    {- |'runPrompt' takes a way of converting prompts to an element in a pure
    fashion and calculates the result of the prompt -}

    runPrompt :: (forall a. p a -> a) -> Prompt p r -> r
    runPrompt :: forall (p :: * -> *) r. (forall a. p a -> a) -> Prompt p r -> r
    runPrompt forall a. p a -> a
    prm = (r -> r) -> (forall a. p a -> (a -> r) -> r) -> Prompt p r -> r
    forall (p :: * -> *) r b.
    (r -> b) -> (forall a. p a -> (a -> b) -> b) -> Prompt p r -> b
    runPromptC r -> r
    forall a. a -> a
    id (\p a
    p a -> r
    cont -> a -> r
    cont (a -> r) -> a -> r
    forall a b. (a -> b) -> a -> b
    $ p a -> a
    forall a. p a -> a
    prm p a
    p)

    {- |'runPromptM' is similar to 'runPrompt' but allows the computation to happen in any monad. -}

    runPromptM :: Monad m => (forall a . p a -> m a) -> Prompt p r -> m r
    runPromptM :: forall (m :: * -> *) (p :: * -> *) r.
    Monad m =>
    (forall a. p a -> m a) -> Prompt p r -> m r
    runPromptM forall a. p a -> m a
    prm = (r -> m r)
    -> (forall a. p a -> (a -> m r) -> m r) -> Prompt p r -> m r
    forall (p :: * -> *) r b.
    (r -> b) -> (forall a. p a -> (a -> b) -> b) -> Prompt p r -> b
    runPromptC r -> m r
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (\p a
    p a -> m r
    cont -> p a -> m a
    forall a. p a -> m a
    prm p a
    p m a -> (a -> m r) -> m r
    forall a b. m a -> (a -> m b) -> m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= a -> m r
    cont)

    {- | 'RecPrompt' is for prompts which are dependent on the prompt monad.

    For example, a 'MonadPlus' prompt:

    @
    data PromptPlus m a where
      PromptZero :: PromptPlus m a
      PromptPlus :: m a -> m a -> PromptPlus m a

    instance MonadPlus (RecPrompt PromptPlus) where
      mzero = prompt PromptZero
      mplus x y = prompt (PromptPlus x y)
    @
    -}
    newtype RecPrompt p r = RecPrompt { forall (p :: (* -> *) -> * -> *) r.
    RecPrompt p r -> Prompt (p (RecPrompt p)) r
    unRecPrompt :: Prompt (p (RecPrompt p)) r }

    instance Monad (RecPrompt p) where
        return :: forall a. a -> RecPrompt p a
    return  = Prompt (p (RecPrompt p)) a -> RecPrompt p a
    forall (p :: (* -> *) -> * -> *) r.
    Prompt (p (RecPrompt p)) r -> RecPrompt p r
    RecPrompt (Prompt (p (RecPrompt p)) a -> RecPrompt p a)
    -> (a -> Prompt (p (RecPrompt p)) a) -> a -> RecPrompt p a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Prompt (p (RecPrompt p)) a
    forall a. a -> Prompt (p (RecPrompt p)) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
        RecPrompt p a
    m >>= :: forall a b. RecPrompt p a -> (a -> RecPrompt p b) -> RecPrompt p b
    >>= a -> RecPrompt p b
    f = Prompt (p (RecPrompt p)) b -> RecPrompt p b
    forall (p :: (* -> *) -> * -> *) r.
    Prompt (p (RecPrompt p)) r -> RecPrompt p r
    RecPrompt (Prompt (p (RecPrompt p)) b -> RecPrompt p b)
    -> Prompt (p (RecPrompt p)) b -> RecPrompt p b
    forall a b. (a -> b) -> a -> b
    $ RecPrompt p a -> Prompt (p (RecPrompt p)) a
    forall (p :: (* -> *) -> * -> *) r.
    RecPrompt p r -> Prompt (p (RecPrompt p)) r
    unRecPrompt RecPrompt p a
    m Prompt (p (RecPrompt p)) a
    -> (a -> Prompt (p (RecPrompt p)) b) -> Prompt (p (RecPrompt p)) b
    forall a b.
    Prompt (p (RecPrompt p)) a
    -> (a -> Prompt (p (RecPrompt p)) b) -> Prompt (p (RecPrompt p)) b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (RecPrompt p b -> Prompt (p (RecPrompt p)) b
    forall (p :: (* -> *) -> * -> *) r.
    RecPrompt p r -> Prompt (p (RecPrompt p)) r
    unRecPrompt (RecPrompt p b -> Prompt (p (RecPrompt p)) b)
    -> (a -> RecPrompt p b) -> a -> Prompt (p (RecPrompt p)) b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> RecPrompt p b
    f)

    instance Functor (RecPrompt p) where
        fmap :: forall a b. (a -> b) -> RecPrompt p a -> RecPrompt p b
    fmap    = (a -> b) -> RecPrompt p a -> RecPrompt p b
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM

    instance Applicative (RecPrompt p) where
        pure :: forall a. a -> RecPrompt p a
    pure    = a -> RecPrompt p a
    forall a. a -> RecPrompt p a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
        <*> :: forall a b. RecPrompt p (a -> b) -> RecPrompt p a -> RecPrompt p b
    (<*>)   = RecPrompt p (a -> b) -> RecPrompt p a -> RecPrompt p b
    forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
    ap

    instance MonadPrompt (p (RecPrompt p)) (RecPrompt p) where
        prompt :: forall a. p (RecPrompt p) a -> RecPrompt p a
    prompt  = Prompt (p (RecPrompt p)) a -> RecPrompt p a
    forall (p :: (* -> *) -> * -> *) r.
    Prompt (p (RecPrompt p)) r -> RecPrompt p r
    RecPrompt (Prompt (p (RecPrompt p)) a -> RecPrompt p a)
    -> (p (RecPrompt p) a -> Prompt (p (RecPrompt p)) a)
    -> p (RecPrompt p) a
    -> RecPrompt p a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . p (RecPrompt p) a -> Prompt (p (RecPrompt p)) a
    forall a. p (RecPrompt p) a -> Prompt (p (RecPrompt p)) a
    forall (p :: * -> *) (m :: * -> *) a. MonadPrompt p m => p a -> m a
    prompt

    {- | Runs a recursive prompt computation. This is similar to 'runPromptC', but for recursive prompt types. -}
    runRecPromptC :: forall p r b. -- prompt, computation result, answer type
                     (r -> b)      -- ^ handler when there is no further computation
                  -> (forall a . p (RecPrompt p) a -> (a -> b) -> b)
                                   -- ^ handler for prompts
                  -> RecPrompt p r -- ^ a prompt-based computation
                  -> b             -- ^ answer
    runRecPromptC :: forall (p :: (* -> *) -> * -> *) r b.
    (r -> b)
    -> (forall a. p (RecPrompt p) a -> (a -> b) -> b)
    -> RecPrompt p r
    -> b
    runRecPromptC r -> b
    ret forall a. p (RecPrompt p) a -> (a -> b) -> b
    prm = (r -> b)
    -> (forall a. p (RecPrompt p) a -> (a -> b) -> b)
    -> Prompt (p (RecPrompt p)) r
    -> b
    forall (p :: * -> *) r b.
    (r -> b) -> (forall a. p a -> (a -> b) -> b) -> Prompt p r -> b
    runPromptC r -> b
    ret p (RecPrompt p) a -> (a -> b) -> b
    forall a. p (RecPrompt p) a -> (a -> b) -> b
    prm (Prompt (p (RecPrompt p)) r -> b)
    -> (RecPrompt p r -> Prompt (p (RecPrompt p)) r)
    -> RecPrompt p r
    -> b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RecPrompt p r -> Prompt (p (RecPrompt p)) r
    forall (p :: (* -> *) -> * -> *) r.
    RecPrompt p r -> Prompt (p (RecPrompt p)) r
    unRecPrompt

    {- | Run a recursive prompt computation in a pure fashion, similar to 'runPrompt'.  -}
    runRecPrompt :: (forall a. p (RecPrompt p) a -> a) -> RecPrompt p r -> r
    runRecPrompt :: forall (p :: (* -> *) -> * -> *) r.
    (forall a. p (RecPrompt p) a -> a) -> RecPrompt p r -> r
    runRecPrompt forall a. p (RecPrompt p) a -> a
    prm = (forall a. p (RecPrompt p) a -> a)
    -> Prompt (p (RecPrompt p)) r -> r
    forall (p :: * -> *) r. (forall a. p a -> a) -> Prompt p r -> r
    runPrompt p (RecPrompt p) a -> a
    forall a. p (RecPrompt p) a -> a
    prm (Prompt (p (RecPrompt p)) r -> r)
    -> (RecPrompt p r -> Prompt (p (RecPrompt p)) r)
    -> RecPrompt p r
    -> r
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RecPrompt p r -> Prompt (p (RecPrompt p)) r
    forall (p :: (* -> *) -> * -> *) r.
    RecPrompt p r -> Prompt (p (RecPrompt p)) r
    unRecPrompt

    {- | Run a recursive prompt computation in an arbitrary monad, similar to 'runPromptM'. -}
    runRecPromptM :: Monad m => (forall a . p (RecPrompt p) a -> m a) -> RecPrompt p r -> m r
    runRecPromptM :: forall (m :: * -> *) (p :: (* -> *) -> * -> *) r.
    Monad m =>
    (forall a. p (RecPrompt p) a -> m a) -> RecPrompt p r -> m r
    runRecPromptM forall a. p (RecPrompt p) a -> m a
    prm = (forall a. p (RecPrompt p) a -> m a)
    -> Prompt (p (RecPrompt p)) r -> m r
    forall (m :: * -> *) (p :: * -> *) r.
    Monad m =>
    (forall a. p a -> m a) -> Prompt p r -> m r
    runPromptM p (RecPrompt p) a -> m a
    forall a. p (RecPrompt p) a -> m a
    prm (Prompt (p (RecPrompt p)) r -> m r)
    -> (RecPrompt p r -> Prompt (p (RecPrompt p)) r)
    -> RecPrompt p r
    -> m r
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RecPrompt p r -> Prompt (p (RecPrompt p)) r
    forall (p :: (* -> *) -> * -> *) r.
    RecPrompt p r -> Prompt (p (RecPrompt p)) r
    unRecPrompt

    {- | Prompt can also be used to define monad transformers.

    You will notice the lack of a @Monad m@ constraint; this is allowed
    because Prompt doesn't use the underlying monad at all; instead
    the observation function (generally implemented via 'runPromptT')
    will have the constraint.

    -}

    newtype PromptT p m a = PromptT { forall (p :: * -> *) (m :: * -> *) a.
    PromptT p m a -> Prompt (Lift p m) a
    unPromptT :: Prompt (Lift p m) a }

    {- | A higher-kinded Either, used in defining 'PromptT'. -}
    data Lift p m a = Effect (p a) | Lift (m a)

    instance Monad (PromptT p m) where
       return :: forall a. a -> PromptT p m a
    return  = Prompt (Lift p m) a -> PromptT p m a
    forall (p :: * -> *) (m :: * -> *) a.
    Prompt (Lift p m) a -> PromptT p m a
    PromptT (Prompt (Lift p m) a -> PromptT p m a)
    -> (a -> Prompt (Lift p m) a) -> a -> PromptT p m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Prompt (Lift p m) a
    forall a. a -> Prompt (Lift p m) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
       PromptT p m a
    m >>= :: forall a b. PromptT p m a -> (a -> PromptT p m b) -> PromptT p m b
    >>= a -> PromptT p m b
    f = Prompt (Lift p m) b -> PromptT p m b
    forall (p :: * -> *) (m :: * -> *) a.
    Prompt (Lift p m) a -> PromptT p m a
    PromptT (Prompt (Lift p m) b -> PromptT p m b)
    -> Prompt (Lift p m) b -> PromptT p m b
    forall a b. (a -> b) -> a -> b
    $ PromptT p m a -> Prompt (Lift p m) a
    forall (p :: * -> *) (m :: * -> *) a.
    PromptT p m a -> Prompt (Lift p m) a
    unPromptT PromptT p m a
    m Prompt (Lift p m) a
    -> (a -> Prompt (Lift p m) b) -> Prompt (Lift p m) b
    forall a b.
    Prompt (Lift p m) a
    -> (a -> Prompt (Lift p m) b) -> Prompt (Lift p m) b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (PromptT p m b -> Prompt (Lift p m) b
    forall (p :: * -> *) (m :: * -> *) a.
    PromptT p m a -> Prompt (Lift p m) a
    unPromptT (PromptT p m b -> Prompt (Lift p m) b)
    -> (a -> PromptT p m b) -> a -> Prompt (Lift p m) b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> PromptT p m b
    f)

    instance Functor (PromptT p m) where
       fmap :: forall a b. (a -> b) -> PromptT p m a -> PromptT p m b
    fmap    = (a -> b) -> PromptT p m a -> PromptT p m b
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM

    instance Applicative (PromptT p m) where
       pure :: forall a. a -> PromptT p m a
    pure    = a -> PromptT p m a
    forall a. a -> PromptT p m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
       <*> :: forall a b. PromptT p m (a -> b) -> PromptT p m a -> PromptT p m b
    (<*>)   = PromptT p m (a -> b) -> PromptT p m a -> PromptT p m b
    forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
    ap

    instance MonadPrompt p (PromptT p m) where
       prompt :: forall a. p a -> PromptT p m a
    prompt  = Prompt (Lift p m) a -> PromptT p m a
    forall (p :: * -> *) (m :: * -> *) a.
    Prompt (Lift p m) a -> PromptT p m a
    PromptT (Prompt (Lift p m) a -> PromptT p m a)
    -> (p a -> Prompt (Lift p m) a) -> p a -> PromptT p m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Lift p m a -> Prompt (Lift p m) a
    forall a. Lift p m a -> Prompt (Lift p m) a
    forall (p :: * -> *) (m :: * -> *) a. MonadPrompt p m => p a -> m a
    prompt (Lift p m a -> Prompt (Lift p m) a)
    -> (p a -> Lift p m a) -> p a -> Prompt (Lift p m) a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . p a -> Lift p m a
    forall (p :: * -> *) (m :: * -> *) a. p a -> Lift p m a
    Effect

    instance MonadTrans (PromptT p) where
       lift :: forall (m :: * -> *) a. Monad m => m a -> PromptT p m a
    lift = Prompt (Lift p m) a -> PromptT p m a
    forall (p :: * -> *) (m :: * -> *) a.
    Prompt (Lift p m) a -> PromptT p m a
    PromptT (Prompt (Lift p m) a -> PromptT p m a)
    -> (m a -> Prompt (Lift p m) a) -> m a -> PromptT p m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Lift p m a -> Prompt (Lift p m) a
    forall a. Lift p m a -> Prompt (Lift p m) a
    forall (p :: * -> *) (m :: * -> *) a. MonadPrompt p m => p a -> m a
    prompt (Lift p m a -> Prompt (Lift p m) a)
    -> (m a -> Lift p m a) -> m a -> Prompt (Lift p m) a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . m a -> Lift p m a
    forall (p :: * -> *) (m :: * -> *) a. m a -> Lift p m a
    Lift

    {- | 'runPromptT' runs a prompt monad transformer. -}
    runPromptT :: forall p m r b.
                  (r -> b)      -- ^ handler when there is no further computation
               -> (forall a . p a -> (a -> b) -> b)
                                -- ^ handler for prompts
               -> (forall a . m a -> (a -> b) -> b)
                                -- ^ handler for lifted computations
               -> PromptT p m r -- ^ a prompt-based computation
               -> b             -- ^ answer
    runPromptT :: forall (p :: * -> *) (m :: * -> *) r b.
    (r -> b)
    -> (forall a. p a -> (a -> b) -> b)
    -> (forall a. m a -> (a -> b) -> b)
    -> PromptT p m r
    -> b
    runPromptT r -> b
    ret forall a. p a -> (a -> b) -> b
    prm forall a. m a -> (a -> b) -> b
    lft = (r -> b)
    -> (forall a. Lift p m a -> (a -> b) -> b)
    -> Prompt (Lift p m) r
    -> b
    forall (p :: * -> *) r b.
    (r -> b) -> (forall a. p a -> (a -> b) -> b) -> Prompt p r -> b
    runPromptC r -> b
    ret Lift p m a -> (a -> b) -> b
    forall a. Lift p m a -> (a -> b) -> b
    prm' (Prompt (Lift p m) r -> b)
    -> (PromptT p m r -> Prompt (Lift p m) r) -> PromptT p m r -> b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . PromptT p m r -> Prompt (Lift p m) r
    forall (p :: * -> *) (m :: * -> *) a.
    PromptT p m a -> Prompt (Lift p m) a
    unPromptT where
       prm' :: Lift p m a -> (a -> b) -> b
    prm' (Effect p a
    e) = p a -> (a -> b) -> b
    forall a. p a -> (a -> b) -> b
    prm p a
    e
       prm' (Lift m a
    a)   = m a -> (a -> b) -> b
    forall a. m a -> (a -> b) -> b
    lft m a
    a

    {- | 'runPromptTM' is a useful variant of runPromptT when interpreting into another monad -}
    runPromptTM :: forall p m r n. (Monad n)
                => (forall a. p a -> n a) -- ^ interpretation for prompts
                -> (forall a. m a -> n a) -- ^ interpretation for lifted computations
                -> PromptT p m r -- ^ a prompt-based computation
                -> n r -- ^ resulting interpretation
    runPromptTM :: forall (p :: * -> *) (m :: * -> *) r (n :: * -> *).
    Monad n =>
    (forall a. p a -> n a)
    -> (forall a. m a -> n a) -> PromptT p m r -> n r
    runPromptTM forall a. p a -> n a
    prm forall a. m a -> n a
    lft = (r -> n r)
    -> (forall a. p a -> (a -> n r) -> n r)
    -> (forall a. m a -> (a -> n r) -> n r)
    -> PromptT p m r
    -> n r
    forall (p :: * -> *) (m :: * -> *) r b.
    (r -> b)
    -> (forall a. p a -> (a -> b) -> b)
    -> (forall a. m a -> (a -> b) -> b)
    -> PromptT p m r
    -> b
    runPromptT r -> n r
    forall a. a -> n a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (\p a
    p a -> n r
    k -> p a -> n a
    forall a. p a -> n a
    prm p a
    p n a -> (a -> n r) -> n r
    forall a b. n a -> (a -> n b) -> n b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= a -> n r
    k) (\m a
    l a -> n r
    k -> m a -> n a
    forall a. m a -> n a
    lft m a
    l n a -> (a -> n r) -> n r
    forall a b. n a -> (a -> n b) -> n b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= a -> n r
    k)

    {- | 'runPromptTM'' specialises runPromptTM further for the case that you're interpreting to the base monad by supplying the identity function as the interpretation
         for lifted computations -}
    runPromptTM' :: forall p m r. (Monad m)
                 => (forall a. p a -> m a) -- ^ interpretation for prompts
                 -> PromptT p m r -- ^ a prompt-based computation
                 -> m r -- ^ resulting interpretation
    runPromptTM' :: forall (p :: * -> *) (m :: * -> *) r.
    Monad m =>
    (forall a. p a -> m a) -> PromptT p m r -> m r
    runPromptTM' forall a. p a -> m a
    prm = (forall a. p a -> m a)
    -> (forall a. m a -> m a) -> PromptT p m r -> m r
    forall (p :: * -> *) (m :: * -> *) r (n :: * -> *).
    Monad n =>
    (forall a. p a -> n a)
    -> (forall a. m a -> n a) -> PromptT p m r -> n r
    runPromptTM p a -> m a
    forall a. p a -> m a
    prm m a -> m a
    forall a. a -> a
    forall a. m a -> m a
    id

    {- | You can also lift any Prompt computation into a PromptT (or more generally, any appropriate MonadPrompt instance). This is the kind of place where the advantage of being able to use multiple observation functions on Prompt really shows. -}

    liftP :: (MonadPrompt p m) => Prompt p r -> m r
    liftP :: forall (p :: * -> *) (m :: * -> *) r.
    MonadPrompt p m =>
    Prompt p r -> m r
    liftP = (forall a. p a -> m a) -> Prompt p r -> m r
    forall (m :: * -> *) (p :: * -> *) r.
    Monad m =>
    (forall a. p a -> m a) -> Prompt p r -> m r
    runPromptM p a -> m a
    forall a. p a -> m a
    forall (p :: * -> *) (m :: * -> *) a. MonadPrompt p m => p a -> m a
    prompt

    {- | A recursive variant of the prompt monad transformer. -}
    newtype RecPromptT p m a =
      RecPromptT { forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    RecPromptT p m a -> Prompt (Lift (p (RecPromptT p m)) m) a
    unRecPromptT :: Prompt (Lift (p (RecPromptT p m)) m) a }

    instance Monad (RecPromptT p m) where
       return :: forall a. a -> RecPromptT p m a
    return  = Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    RecPromptT (Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a)
    -> (a -> Prompt (Lift (p (RecPromptT p m)) m) a)
    -> a
    -> RecPromptT p m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall a. a -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
       RecPromptT p m a
    m >>= :: forall a b.
    RecPromptT p m a -> (a -> RecPromptT p m b) -> RecPromptT p m b
    >>= a -> RecPromptT p m b
    f = Prompt (Lift (p (RecPromptT p m)) m) b -> RecPromptT p m b
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    RecPromptT (Prompt (Lift (p (RecPromptT p m)) m) b -> RecPromptT p m b)
    -> Prompt (Lift (p (RecPromptT p m)) m) b -> RecPromptT p m b
    forall a b. (a -> b) -> a -> b
    $ RecPromptT p m a -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    RecPromptT p m a -> Prompt (Lift (p (RecPromptT p m)) m) a
    unRecPromptT RecPromptT p m a
    m Prompt (Lift (p (RecPromptT p m)) m) a
    -> (a -> Prompt (Lift (p (RecPromptT p m)) m) b)
    -> Prompt (Lift (p (RecPromptT p m)) m) b
    forall a b.
    Prompt (Lift (p (RecPromptT p m)) m) a
    -> (a -> Prompt (Lift (p (RecPromptT p m)) m) b)
    -> Prompt (Lift (p (RecPromptT p m)) m) b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (RecPromptT p m b -> Prompt (Lift (p (RecPromptT p m)) m) b
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    RecPromptT p m a -> Prompt (Lift (p (RecPromptT p m)) m) a
    unRecPromptT (RecPromptT p m b -> Prompt (Lift (p (RecPromptT p m)) m) b)
    -> (a -> RecPromptT p m b)
    -> a
    -> Prompt (Lift (p (RecPromptT p m)) m) b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> RecPromptT p m b
    f)

    instance Functor (RecPromptT p m) where
       fmap :: forall a b. (a -> b) -> RecPromptT p m a -> RecPromptT p m b
    fmap    = (a -> b) -> RecPromptT p m a -> RecPromptT p m b
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM

    instance Applicative (RecPromptT p m) where
       pure :: forall a. a -> RecPromptT p m a
    pure    = a -> RecPromptT p m a
    forall a. a -> RecPromptT p m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
       <*> :: forall a b.
    RecPromptT p m (a -> b) -> RecPromptT p m a -> RecPromptT p m b
    (<*>)   = RecPromptT p m (a -> b) -> RecPromptT p m a -> RecPromptT p m b
    forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
    ap

    instance MonadPrompt (p (RecPromptT p m)) (RecPromptT p m) where
       prompt :: forall a. p (RecPromptT p m) a -> RecPromptT p m a
    prompt  = Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    RecPromptT (Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a)
    -> (p (RecPromptT p m) a -> Prompt (Lift (p (RecPromptT p m)) m) a)
    -> p (RecPromptT p m) a
    -> RecPromptT p m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Lift (p (RecPromptT p m)) m a
    -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall a.
    Lift (p (RecPromptT p m)) m a
    -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall (p :: * -> *) (m :: * -> *) a. MonadPrompt p m => p a -> m a
    prompt (Lift (p (RecPromptT p m)) m a
     -> Prompt (Lift (p (RecPromptT p m)) m) a)
    -> (p (RecPromptT p m) a -> Lift (p (RecPromptT p m)) m a)
    -> p (RecPromptT p m) a
    -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . p (RecPromptT p m) a -> Lift (p (RecPromptT p m)) m a
    forall (p :: * -> *) (m :: * -> *) a. p a -> Lift p m a
    Effect

    instance MonadTrans (RecPromptT p) where
       lift :: forall (m :: * -> *) a. Monad m => m a -> RecPromptT p m a
    lift = Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a
    RecPromptT (Prompt (Lift (p (RecPromptT p m)) m) a -> RecPromptT p m a)
    -> (m a -> Prompt (Lift (p (RecPromptT p m)) m) a)
    -> m a
    -> RecPromptT p m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Lift (p (RecPromptT p m)) m a
    -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall a.
    Lift (p (RecPromptT p m)) m a
    -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall (p :: * -> *) (m :: * -> *) a. MonadPrompt p m => p a -> m a
    prompt (Lift (p (RecPromptT p m)) m a
     -> Prompt (Lift (p (RecPromptT p m)) m) a)
    -> (m a -> Lift (p (RecPromptT p m)) m a)
    -> m a
    -> Prompt (Lift (p (RecPromptT p m)) m) a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . m a -> Lift (p (RecPromptT p m)) m a
    forall (p :: * -> *) (m :: * -> *) a. m a -> Lift p m a
    Lift

    {- | Run a recursive prompt monad transformer. -}
    runRecPromptT :: forall p r b m.
                     (r -> b)      -- ^ handler when there is no further computation
                  -> (forall a . p (RecPromptT p m) a -> (a -> b) -> b)
                                   -- ^ handler for prompts
                  -> (forall a . m a -> (a -> b) -> b)
                                   -- ^ handler for lifted computations
                  -> RecPromptT p m r -- ^ a prompt-based computation
                  -> b             -- ^ answer
    runRecPromptT :: forall (p :: (* -> *) -> * -> *) r b (m :: * -> *).
    (r -> b)
    -> (forall a. p (RecPromptT p m) a -> (a -> b) -> b)
    -> (forall a. m a -> (a -> b) -> b)
    -> RecPromptT p m r
    -> b
    runRecPromptT r -> b
    ret forall a. p (RecPromptT p m) a -> (a -> b) -> b
    prm forall a. m a -> (a -> b) -> b
    lft = (r -> b)
    -> (forall a. Lift (p (RecPromptT p m)) m a -> (a -> b) -> b)
    -> Prompt (Lift (p (RecPromptT p m)) m) r
    -> b
    forall (p :: * -> *) r b.
    (r -> b) -> (forall a. p a -> (a -> b) -> b) -> Prompt p r -> b
    runPromptC r -> b
    ret Lift (p (RecPromptT p m)) m a -> (a -> b) -> b
    forall a. Lift (p (RecPromptT p m)) m a -> (a -> b) -> b
    prm' (Prompt (Lift (p (RecPromptT p m)) m) r -> b)
    -> (RecPromptT p m r -> Prompt (Lift (p (RecPromptT p m)) m) r)
    -> RecPromptT p m r
    -> b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RecPromptT p m r -> Prompt (Lift (p (RecPromptT p m)) m) r
    forall (p :: (* -> *) -> * -> *) (m :: * -> *) a.
    RecPromptT p m a -> Prompt (Lift (p (RecPromptT p m)) m) a
    unRecPromptT where
       prm' :: Lift (p (RecPromptT p m)) m a -> (a -> b) -> b
    prm' (Effect p (RecPromptT p m) a
    e) = p (RecPromptT p m) a -> (a -> b) -> b
    forall a. p (RecPromptT p m) a -> (a -> b) -> b
    prm p (RecPromptT p m) a
    e
       prm' (Lift m a
    a)   = m a -> (a -> b) -> b
    forall a. m a -> (a -> b) -> b
    lft m a
    a