{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{- |
   Module      : Text.Pandoc.Parsing.Future
   Copyright   : Copyright (C) 2006-2023 John MacFarlane
   License     : GPL-2.0-or-later
   Maintainer  : John MacFarlane <jgm@berkeley.edu>

Future type for parsing.
-}

module Text.Pandoc.Parsing.Future
  ( Future (..)
  , runF
  , askF
  , asksF
  , returnF
  )
where

import Prelude hiding (Applicative(..))
import Control.Applicative (Applicative(..))
import Control.Monad.Reader
  ( asks, runReader, MonadReader(ask), Reader, ReaderT(ReaderT) )

-- | Reader monad wrapping the parser state. This is used to possibly
-- delay evaluation until all relevant information has been parsed and
-- made available in the parser state.
newtype Future s a = Future { forall s a. Future s a -> Reader s a
runDelayed :: Reader s a }
  deriving (Applicative (Future s)
Applicative (Future s) =>
(forall a b. Future s a -> (a -> Future s b) -> Future s b)
-> (forall a b. Future s a -> Future s b -> Future s b)
-> (forall a. a -> Future s a)
-> Monad (Future s)
forall s. Applicative (Future s)
forall a. a -> Future s a
forall s a. a -> Future s a
forall a b. Future s a -> Future s b -> Future s b
forall a b. Future s a -> (a -> Future s b) -> Future s b
forall s a b. Future s a -> Future s b -> Future s b
forall s a b. Future s a -> (a -> Future s b) -> Future s b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall s a b. Future s a -> (a -> Future s b) -> Future s b
>>= :: forall a b. Future s a -> (a -> Future s b) -> Future s b
$c>> :: forall s a b. Future s a -> Future s b -> Future s b
>> :: forall a b. Future s a -> Future s b -> Future s b
$creturn :: forall s a. a -> Future s a
return :: forall a. a -> Future s a
Monad, Functor (Future s)
Functor (Future s) =>
(forall a. a -> Future s a)
-> (forall a b. Future s (a -> b) -> Future s a -> Future s b)
-> (forall a b c.
    (a -> b -> c) -> Future s a -> Future s b -> Future s c)
-> (forall a b. Future s a -> Future s b -> Future s b)
-> (forall a b. Future s a -> Future s b -> Future s a)
-> Applicative (Future s)
forall s. Functor (Future s)
forall a. a -> Future s a
forall s a. a -> Future s a
forall a b. Future s a -> Future s b -> Future s a
forall a b. Future s a -> Future s b -> Future s b
forall a b. Future s (a -> b) -> Future s a -> Future s b
forall s a b. Future s a -> Future s b -> Future s a
forall s a b. Future s a -> Future s b -> Future s b
forall s a b. Future s (a -> b) -> Future s a -> Future s b
forall a b c.
(a -> b -> c) -> Future s a -> Future s b -> Future s c
forall s a b c.
(a -> b -> c) -> Future s a -> Future s b -> Future s c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall s a. a -> Future s a
pure :: forall a. a -> Future s a
$c<*> :: forall s a b. Future s (a -> b) -> Future s a -> Future s b
<*> :: forall a b. Future s (a -> b) -> Future s a -> Future s b
$cliftA2 :: forall s a b c.
(a -> b -> c) -> Future s a -> Future s b -> Future s c
liftA2 :: forall a b c.
(a -> b -> c) -> Future s a -> Future s b -> Future s c
$c*> :: forall s a b. Future s a -> Future s b -> Future s b
*> :: forall a b. Future s a -> Future s b -> Future s b
$c<* :: forall s a b. Future s a -> Future s b -> Future s a
<* :: forall a b. Future s a -> Future s b -> Future s a
Applicative, (forall a b. (a -> b) -> Future s a -> Future s b)
-> (forall a b. a -> Future s b -> Future s a)
-> Functor (Future s)
forall a b. a -> Future s b -> Future s a
forall a b. (a -> b) -> Future s a -> Future s b
forall s a b. a -> Future s b -> Future s a
forall s a b. (a -> b) -> Future s a -> Future s b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall s a b. (a -> b) -> Future s a -> Future s b
fmap :: forall a b. (a -> b) -> Future s a -> Future s b
$c<$ :: forall s a b. a -> Future s b -> Future s a
<$ :: forall a b. a -> Future s b -> Future s a
Functor)

instance Semigroup a => Semigroup (Future s a) where
  <> :: Future s a -> Future s a -> Future s a
(<>) = (a -> a -> a) -> Future s a -> Future s a -> Future s a
forall a b c.
(a -> b -> c) -> Future s a -> Future s b -> Future s c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)

instance (Semigroup a, Monoid a) => Monoid (Future s a) where
  mempty :: Future s a
mempty = a -> Future s a
forall a. a -> Future s a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. Monoid a => a
mempty
  mappend :: Future s a -> Future s a -> Future s a
mappend = Future s a -> Future s a -> Future s a
forall a. Semigroup a => a -> a -> a
(<>)

-- | Run a delayed action with the given state.
runF :: Future s a -> s -> a
runF :: forall s a. Future s a -> s -> a
runF = Reader s a -> s -> a
forall r a. Reader r a -> r -> a
runReader (Reader s a -> s -> a)
-> (Future s a -> Reader s a) -> Future s a -> s -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Future s a -> Reader s a
forall s a. Future s a -> Reader s a
runDelayed

askF :: Future s s
askF :: forall s. Future s s
askF = Reader s s -> Future s s
forall s a. Reader s a -> Future s a
Future Reader s s
forall r (m :: * -> *). MonadReader r m => m r
ask

asksF :: (s -> a) -> Future s a
asksF :: forall s a. (s -> a) -> Future s a
asksF s -> a
f = Reader s a -> Future s a
forall s a. Reader s a -> Future s a
Future (Reader s a -> Future s a) -> Reader s a -> Future s a
forall a b. (a -> b) -> a -> b
$ (s -> a) -> Reader s a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks s -> a
f

returnF :: Monad m => a -> m (Future s a)
returnF :: forall (m :: * -> *) a s. Monad m => a -> m (Future s a)
returnF = Future s a -> m (Future s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Future s a -> m (Future s a))
-> (a -> Future s a) -> a -> m (Future s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Future s a
forall a. a -> Future s a
forall (m :: * -> *) a. Monad m => a -> m a
return