{-# LANGUAGE DeriveFunctor              #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE TypeFamilies               #-}
-- |
--
-- Types for the succinct data structure decoder
--
module Waargonaut.Decode.Types
  ( ParseFn
  , Cursor
  , CursorHistory
  , Decoder (..)
  , DecodeResult (..)
  , JCurs (..)
  , mkCursor
  , jsonTypeAt
  , JsonType(..)
  ) where

import           Control.Lens                                   (Rewrapped,
                                                                 Wrapped (..),
                                                                 iso)
import           Control.Monad.Except                           (MonadError (..))
import           Control.Monad.Morph                            (MFunctor (..),
                                                                 MMonad (..))
import           Control.Monad.Reader                           (MonadReader,
                                                                 ReaderT (..))
import           Control.Monad.State                            (MonadState)
import           Control.Monad.Trans.Class                      (MonadTrans (lift))

import           Data.Functor.Alt                               (Alt (..))
import qualified Data.Text                                      as Text

import           Data.ByteString                                (ByteString)

import           HaskellWorks.Data.Json.Standard.Cursor.Fast   (Cursor,fromByteStringViaBlanking)
import           HaskellWorks.Data.Json.Standard.Cursor.Generic (cursorRank)
import           HaskellWorks.Data.Json.Standard.Cursor.Type                    (JsonType (..),
                                                                 JsonTypeAt (..))
import           HaskellWorks.Data.Positioning                  (Count)

import           Waargonaut.Decode.Internal                     (CursorHistory', DecodeError (..),
                                                                 DecodeResultT (..),
                                                                 ZipperMove (BranchFail),
                                                                 recordZipperMove)

import           Waargonaut.Types                               (Json)

-- | We define the index of our 'CursorHistory'' to be the 'HaskellWorks.Data.Positioning.Count'.
type CursorHistory =
  CursorHistory' Count

-- | Convenience alias for the type of the function we will use to parse
-- the input string into the 'Json' structure.
type ParseFn =
  ByteString -> Either DecodeError Json

-- | 'Decoder' type that is used directly to convert 'Json' structures to other
-- data types.
--
newtype Decoder f a = Decoder
  { Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder :: ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
  }
  deriving a -> Decoder f b -> Decoder f a
(a -> b) -> Decoder f a -> Decoder f b
(forall a b. (a -> b) -> Decoder f a -> Decoder f b)
-> (forall a b. a -> Decoder f b -> Decoder f a)
-> Functor (Decoder f)
forall a b. a -> Decoder f b -> Decoder f a
forall a b. (a -> b) -> Decoder f a -> Decoder f b
forall (f :: * -> *) a b.
Functor f =>
a -> Decoder f b -> Decoder f a
forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> Decoder f a -> Decoder f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Decoder f b -> Decoder f a
$c<$ :: forall (f :: * -> *) a b.
Functor f =>
a -> Decoder f b -> Decoder f a
fmap :: (a -> b) -> Decoder f a -> Decoder f b
$cfmap :: forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> Decoder f a -> Decoder f b
Functor

instance Monad f => Applicative (Decoder f) where
  pure :: a -> Decoder f a
pure     a
a = (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder ((ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
 -> Decoder f a)
-> (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall a b. (a -> b) -> a -> b
$ \ParseFn
_ JCurs
_ -> a -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
  Decoder f (a -> b)
aToB <*> :: Decoder f (a -> b) -> Decoder f a -> Decoder f b
<*> Decoder f a
a = (ParseFn -> JCurs -> DecodeResultT Count DecodeError f b)
-> Decoder f b
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder ((ParseFn -> JCurs -> DecodeResultT Count DecodeError f b)
 -> Decoder f b)
-> (ParseFn -> JCurs -> DecodeResultT Count DecodeError f b)
-> Decoder f b
forall a b. (a -> b) -> a -> b
$ \ParseFn
p JCurs
c ->
    Decoder f (a -> b)
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f (a -> b)
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder Decoder f (a -> b)
aToB ParseFn
p JCurs
c DecodeResultT Count DecodeError f (a -> b)
-> DecodeResultT Count DecodeError f a
-> DecodeResultT Count DecodeError f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder Decoder f a
a ParseFn
p JCurs
c

instance Monad f => Alt (Decoder f) where
  Decoder f a
a <!> :: Decoder f a -> Decoder f a -> Decoder f a
<!> Decoder f a
b = (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder ((ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
 -> Decoder f a)
-> (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall a b. (a -> b) -> a -> b
$ \ParseFn
p JCurs
c -> DecodeResultT Count DecodeError f a
-> (DecodeError -> DecodeResultT Count DecodeError f a)
-> DecodeResultT Count DecodeError f a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError (Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder Decoder f a
a ParseFn
p JCurs
c) ((DecodeError -> DecodeResultT Count DecodeError f a)
 -> DecodeResultT Count DecodeError f a)
-> (DecodeError -> DecodeResultT Count DecodeError f a)
-> DecodeResultT Count DecodeError f a
forall a b. (a -> b) -> a -> b
$ \DecodeError
e -> do
    ZipperMove -> Count -> DecodeResultT Count DecodeError f ()
forall i (m :: * -> *).
MonadState (CursorHistory' i) m =>
ZipperMove -> i -> m ()
recordZipperMove (Text -> ZipperMove
BranchFail (Text -> ZipperMove) -> (String -> Text) -> String -> ZipperMove
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack (String -> ZipperMove) -> String -> ZipperMove
forall a b. (a -> b) -> a -> b
$ DecodeError -> String
forall a. Show a => a -> String
show DecodeError
e) (GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> Count
forall t v w. GenericCursor t v w -> Count
cursorRank (GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> Count)
-> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> Count
forall a b. (a -> b) -> a -> b
$ JCurs -> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1)
unJCurs JCurs
c)
    Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder Decoder f a
b ParseFn
p JCurs
c

instance Monad f => Monad (Decoder f) where
  return :: a -> Decoder f a
return      = a -> Decoder f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Decoder f a
a >>= :: Decoder f a -> (a -> Decoder f b) -> Decoder f b
>>= a -> Decoder f b
aToFb = (ParseFn -> JCurs -> DecodeResultT Count DecodeError f b)
-> Decoder f b
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder ((ParseFn -> JCurs -> DecodeResultT Count DecodeError f b)
 -> Decoder f b)
-> (ParseFn -> JCurs -> DecodeResultT Count DecodeError f b)
-> Decoder f b
forall a b. (a -> b) -> a -> b
$ \ParseFn
p JCurs
c -> do
    a
r <- Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder Decoder f a
a ParseFn
p JCurs
c
    Decoder f b
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f b
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder (a -> Decoder f b
aToFb a
r) ParseFn
p JCurs
c

instance Monad f => MonadError DecodeError (Decoder f) where
  throwError :: DecodeError -> Decoder f a
throwError DecodeError
e        = (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder (\ParseFn
_ JCurs
_ -> DecodeError -> DecodeResultT Count DecodeError f a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError DecodeError
e)
  catchError :: Decoder f a -> (DecodeError -> Decoder f a) -> Decoder f a
catchError Decoder f a
d DecodeError -> Decoder f a
handle = (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder ((ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
 -> Decoder f a)
-> (ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
forall a b. (a -> b) -> a -> b
$ \ParseFn
p JCurs
c ->
    DecodeResultT Count DecodeError f a
-> (DecodeError -> DecodeResultT Count DecodeError f a)
-> DecodeResultT Count DecodeError f a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError (Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder Decoder f a
d ParseFn
p JCurs
c) (\DecodeError
e -> Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
forall (f :: * -> *) a.
Decoder f a
-> ParseFn -> JCurs -> DecodeResultT Count DecodeError f a
runDecoder (DecodeError -> Decoder f a
handle DecodeError
e) ParseFn
p JCurs
c)

instance MonadTrans Decoder where
  lift :: m a -> Decoder m a
lift m a
fa = (ParseFn -> JCurs -> DecodeResultT Count DecodeError m a)
-> Decoder m a
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder (\ ParseFn
_ JCurs
_ -> m a -> DecodeResultT Count DecodeError m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
fa)

instance MFunctor Decoder where
  hoist :: (forall a. m a -> n a) -> Decoder m b -> Decoder n b
hoist forall a. m a -> n a
nat (Decoder ParseFn -> JCurs -> DecodeResultT Count DecodeError m b
pjdr) = (ParseFn -> JCurs -> DecodeResultT Count DecodeError n b)
-> Decoder n b
forall (f :: * -> *) a.
(ParseFn -> JCurs -> DecodeResultT Count DecodeError f a)
-> Decoder f a
Decoder (\ParseFn
p -> (forall a. m a -> n a)
-> DecodeResultT Count DecodeError m b
-> DecodeResultT Count DecodeError n b
forall k (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
       (b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist forall a. m a -> n a
nat (DecodeResultT Count DecodeError m b
 -> DecodeResultT Count DecodeError n b)
-> (JCurs -> DecodeResultT Count DecodeError m b)
-> JCurs
-> DecodeResultT Count DecodeError n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseFn -> JCurs -> DecodeResultT Count DecodeError m b
pjdr ParseFn
p)

-- | Wrapper type for the 'SuccinctCursor'
newtype JCurs = JCurs
  { JCurs -> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1)
unJCurs :: Cursor
  } deriving Position -> JCurs -> Maybe JsonType
JCurs -> Maybe JsonType
(Position -> JCurs -> Maybe JsonType)
-> (JCurs -> Maybe JsonType) -> JsonTypeAt JCurs
forall a.
(Position -> a -> Maybe JsonType)
-> (a -> Maybe JsonType) -> JsonTypeAt a
jsonTypeAt :: JCurs -> Maybe JsonType
$cjsonTypeAt :: JCurs -> Maybe JsonType
jsonTypeAtPosition :: Position -> JCurs -> Maybe JsonType
$cjsonTypeAtPosition :: Position -> JCurs -> Maybe JsonType
JsonTypeAt

instance JCurs ~ t => Rewrapped JCurs t

instance Wrapped JCurs where
  type Unwrapped JCurs = Cursor
  _Wrapped' :: p (Unwrapped JCurs) (f (Unwrapped JCurs)) -> p JCurs (f JCurs)
_Wrapped' = (JCurs -> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1))
-> (GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> JCurs)
-> Iso
     JCurs
     JCurs
     (GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1))
     (GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso JCurs -> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1)
unJCurs GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> JCurs
JCurs

-- | Take a 'ByteString' input and build an index of the JSON structure inside
--
mkCursor :: ByteString -> JCurs
mkCursor :: ByteString -> JCurs
mkCursor = GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> JCurs
JCurs (GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1) -> JCurs)
-> (ByteString
    -> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1))
-> ByteString
-> JCurs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> GenericCursor ByteString CsPoppy1 (RangeMin CsPoppy1)
fromByteStringViaBlanking

-- | Provide some of the type parameters that the underlying 'DecodeResultT'
-- requires. This contains the state and error management as we walk around our
-- zipper and decode our JSON input.
--
-- Addtionally we keep our parsing function in a 'ReaderT' such that it's
-- accessible for all of the decoding steps.
--
newtype DecodeResult f a = DecodeResult
  { DecodeResult f a
-> ReaderT ParseFn (DecodeResultT Count DecodeError f) a
unDecodeResult :: ReaderT ParseFn (DecodeResultT Count DecodeError f) a
  }
  deriving ( a -> DecodeResult f b -> DecodeResult f a
(a -> b) -> DecodeResult f a -> DecodeResult f b
(forall a b. (a -> b) -> DecodeResult f a -> DecodeResult f b)
-> (forall a b. a -> DecodeResult f b -> DecodeResult f a)
-> Functor (DecodeResult f)
forall a b. a -> DecodeResult f b -> DecodeResult f a
forall a b. (a -> b) -> DecodeResult f a -> DecodeResult f b
forall (f :: * -> *) a b.
Functor f =>
a -> DecodeResult f b -> DecodeResult f a
forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> DecodeResult f a -> DecodeResult f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> DecodeResult f b -> DecodeResult f a
$c<$ :: forall (f :: * -> *) a b.
Functor f =>
a -> DecodeResult f b -> DecodeResult f a
fmap :: (a -> b) -> DecodeResult f a -> DecodeResult f b
$cfmap :: forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> DecodeResult f a -> DecodeResult f b
Functor
           , Functor (DecodeResult f)
a -> DecodeResult f a
Functor (DecodeResult f)
-> (forall a. a -> DecodeResult f a)
-> (forall a b.
    DecodeResult f (a -> b) -> DecodeResult f a -> DecodeResult f b)
-> (forall a b c.
    (a -> b -> c)
    -> DecodeResult f a -> DecodeResult f b -> DecodeResult f c)
-> (forall a b.
    DecodeResult f a -> DecodeResult f b -> DecodeResult f b)
-> (forall a b.
    DecodeResult f a -> DecodeResult f b -> DecodeResult f a)
-> Applicative (DecodeResult f)
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
DecodeResult f a -> DecodeResult f b -> DecodeResult f a
DecodeResult f (a -> b) -> DecodeResult f a -> DecodeResult f b
(a -> b -> c)
-> DecodeResult f a -> DecodeResult f b -> DecodeResult f c
forall a. a -> DecodeResult f a
forall a b.
DecodeResult f a -> DecodeResult f b -> DecodeResult f a
forall a b.
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
forall a b.
DecodeResult f (a -> b) -> DecodeResult f a -> DecodeResult f b
forall a b c.
(a -> b -> c)
-> DecodeResult f a -> DecodeResult f b -> DecodeResult f c
forall (f :: * -> *). Monad f => Functor (DecodeResult f)
forall (f :: * -> *) a. Monad f => a -> DecodeResult f a
forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> DecodeResult f b -> DecodeResult f a
forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
forall (f :: * -> *) a b.
Monad f =>
DecodeResult f (a -> b) -> DecodeResult f a -> DecodeResult f b
forall (f :: * -> *) a b c.
Monad f =>
(a -> b -> c)
-> DecodeResult f a -> DecodeResult f b -> DecodeResult f 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
<* :: DecodeResult f a -> DecodeResult f b -> DecodeResult f a
$c<* :: forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> DecodeResult f b -> DecodeResult f a
*> :: DecodeResult f a -> DecodeResult f b -> DecodeResult f b
$c*> :: forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
liftA2 :: (a -> b -> c)
-> DecodeResult f a -> DecodeResult f b -> DecodeResult f c
$cliftA2 :: forall (f :: * -> *) a b c.
Monad f =>
(a -> b -> c)
-> DecodeResult f a -> DecodeResult f b -> DecodeResult f c
<*> :: DecodeResult f (a -> b) -> DecodeResult f a -> DecodeResult f b
$c<*> :: forall (f :: * -> *) a b.
Monad f =>
DecodeResult f (a -> b) -> DecodeResult f a -> DecodeResult f b
pure :: a -> DecodeResult f a
$cpure :: forall (f :: * -> *) a. Monad f => a -> DecodeResult f a
$cp1Applicative :: forall (f :: * -> *). Monad f => Functor (DecodeResult f)
Applicative
           , Applicative (DecodeResult f)
a -> DecodeResult f a
Applicative (DecodeResult f)
-> (forall a b.
    DecodeResult f a -> (a -> DecodeResult f b) -> DecodeResult f b)
-> (forall a b.
    DecodeResult f a -> DecodeResult f b -> DecodeResult f b)
-> (forall a. a -> DecodeResult f a)
-> Monad (DecodeResult f)
DecodeResult f a -> (a -> DecodeResult f b) -> DecodeResult f b
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
forall a. a -> DecodeResult f a
forall a b.
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
forall a b.
DecodeResult f a -> (a -> DecodeResult f b) -> DecodeResult f b
forall (f :: * -> *). Monad f => Applicative (DecodeResult f)
forall (f :: * -> *) a. Monad f => a -> DecodeResult f a
forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> (a -> DecodeResult f b) -> DecodeResult f 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
return :: a -> DecodeResult f a
$creturn :: forall (f :: * -> *) a. Monad f => a -> DecodeResult f a
>> :: DecodeResult f a -> DecodeResult f b -> DecodeResult f b
$c>> :: forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> DecodeResult f b -> DecodeResult f b
>>= :: DecodeResult f a -> (a -> DecodeResult f b) -> DecodeResult f b
$c>>= :: forall (f :: * -> *) a b.
Monad f =>
DecodeResult f a -> (a -> DecodeResult f b) -> DecodeResult f b
$cp1Monad :: forall (f :: * -> *). Monad f => Applicative (DecodeResult f)
Monad
           , MonadReader ParseFn
           , MonadError DecodeError
           , MonadState CursorHistory
           )

instance MonadTrans DecodeResult where
  lift :: m a -> DecodeResult m a
lift = ReaderT ParseFn (DecodeResultT Count DecodeError m) a
-> DecodeResult m a
forall (f :: * -> *) a.
ReaderT ParseFn (DecodeResultT Count DecodeError f) a
-> DecodeResult f a
DecodeResult (ReaderT ParseFn (DecodeResultT Count DecodeError m) a
 -> DecodeResult m a)
-> (m a -> ReaderT ParseFn (DecodeResultT Count DecodeError m) a)
-> m a
-> DecodeResult m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecodeResultT Count DecodeError m a
-> ReaderT ParseFn (DecodeResultT Count DecodeError m) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DecodeResultT Count DecodeError m a
 -> ReaderT ParseFn (DecodeResultT Count DecodeError m) a)
-> (m a -> DecodeResultT Count DecodeError m a)
-> m a
-> ReaderT ParseFn (DecodeResultT Count DecodeError m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> DecodeResultT Count DecodeError m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance MFunctor DecodeResult where
  hoist :: (forall a. m a -> n a) -> DecodeResult m b -> DecodeResult n b
hoist forall a. m a -> n a
nat (DecodeResult ReaderT ParseFn (DecodeResultT Count DecodeError m) b
dr) = ReaderT ParseFn (DecodeResultT Count DecodeError n) b
-> DecodeResult n b
forall (f :: * -> *) a.
ReaderT ParseFn (DecodeResultT Count DecodeError f) a
-> DecodeResult f a
DecodeResult ((forall a.
 DecodeResultT Count DecodeError m a
 -> DecodeResultT Count DecodeError n a)
-> ReaderT ParseFn (DecodeResultT Count DecodeError m) b
-> ReaderT ParseFn (DecodeResultT Count DecodeError n) b
forall k (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
       (b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist ((forall a. m a -> n a)
-> DecodeResultT Count DecodeError m a
-> DecodeResultT Count DecodeError n a
forall k (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
       (b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist forall a. m a -> n a
nat) ReaderT ParseFn (DecodeResultT Count DecodeError m) b
dr)

instance MMonad DecodeResult where
  embed :: (forall a. m a -> DecodeResult n a)
-> DecodeResult m b -> DecodeResult n b
embed forall a. m a -> DecodeResult n a
f (DecodeResult ReaderT ParseFn (DecodeResultT Count DecodeError m) b
dr) = ReaderT ParseFn (DecodeResultT Count DecodeError n) b
-> DecodeResult n b
forall (f :: * -> *) a.
ReaderT ParseFn (DecodeResultT Count DecodeError f) a
-> DecodeResult f a
DecodeResult (ReaderT ParseFn (DecodeResultT Count DecodeError n) b
 -> DecodeResult n b)
-> ((ParseFn -> DecodeResultT Count DecodeError n b)
    -> ReaderT ParseFn (DecodeResultT Count DecodeError n) b)
-> (ParseFn -> DecodeResultT Count DecodeError n b)
-> DecodeResult n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ParseFn -> DecodeResultT Count DecodeError n b)
-> ReaderT ParseFn (DecodeResultT Count DecodeError n) b
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((ParseFn -> DecodeResultT Count DecodeError n b)
 -> DecodeResult n b)
-> (ParseFn -> DecodeResultT Count DecodeError n b)
-> DecodeResult n b
forall a b. (a -> b) -> a -> b
$ \ParseFn
p ->
    (forall a. m a -> DecodeResultT Count DecodeError n a)
-> DecodeResultT Count DecodeError m b
-> DecodeResultT Count DecodeError n b
forall (t :: (* -> *) -> * -> *) (n :: * -> *) (m :: * -> *) b.
(MMonad t, Monad n) =>
(forall a. m a -> t n a) -> t m b -> t n b
embed ((ReaderT ParseFn (DecodeResultT Count DecodeError n) a
 -> ParseFn -> DecodeResultT Count DecodeError n a)
-> ParseFn
-> ReaderT ParseFn (DecodeResultT Count DecodeError n) a
-> DecodeResultT Count DecodeError n a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT ParseFn (DecodeResultT Count DecodeError n) a
-> ParseFn -> DecodeResultT Count DecodeError n a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ParseFn
p (ReaderT ParseFn (DecodeResultT Count DecodeError n) a
 -> DecodeResultT Count DecodeError n a)
-> (m a -> ReaderT ParseFn (DecodeResultT Count DecodeError n) a)
-> m a
-> DecodeResultT Count DecodeError n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecodeResult n a
-> ReaderT ParseFn (DecodeResultT Count DecodeError n) a
forall (f :: * -> *) a.
DecodeResult f a
-> ReaderT ParseFn (DecodeResultT Count DecodeError f) a
unDecodeResult (DecodeResult n a
 -> ReaderT ParseFn (DecodeResultT Count DecodeError n) a)
-> (m a -> DecodeResult n a)
-> m a
-> ReaderT ParseFn (DecodeResultT Count DecodeError n) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> DecodeResult n a
forall a. m a -> DecodeResult n a
f) (DecodeResultT Count DecodeError m b
 -> DecodeResultT Count DecodeError n b)
-> DecodeResultT Count DecodeError m b
-> DecodeResultT Count DecodeError n b
forall a b. (a -> b) -> a -> b
$ ReaderT ParseFn (DecodeResultT Count DecodeError m) b
-> ParseFn -> DecodeResultT Count DecodeError m b
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT ParseFn (DecodeResultT Count DecodeError m) b
dr ParseFn
p