{-# OPTIONS_GHC -Wno-orphans #-}

module Dhall.Syntax.Instances.Monad () where

import Dhall.Syntax.Binding
import Dhall.Syntax.Expr
import Dhall.Syntax.FunctionBinding
import Dhall.Syntax.Instances.Applicative ()
import Dhall.Syntax.Operations
import Dhall.Syntax.RecordField

import qualified Lens.Family as Lens

instance Monad (Expr s) where
    return :: forall a. a -> Expr s a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure

    Expr s a
expression >>= :: forall a b. Expr s a -> (a -> Expr s b) -> Expr s b
>>= a -> Expr s b
k = case Expr s a
expression of
        Embed a
a     -> a -> Expr s b
k a
a
        Let Binding s a
a Expr s a
b     -> forall s a. Binding s a -> Expr s a -> Expr s a
Let (Binding s a -> Binding s b
adaptBinding Binding s a
a) (Expr s a
b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k)
        Note s
a Expr s a
b    -> forall s a. s -> Expr s a -> Expr s a
Note s
a (Expr s a
b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k)
        Record Map Text (RecordField s a)
a    -> forall s a. Map Text (RecordField s a) -> Expr s a
Record forall a b. (a -> b) -> a -> b
$ RecordField s a -> RecordField s b
bindRecordKeyValues forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text (RecordField s a)
a
        RecordLit Map Text (RecordField s a)
a -> forall s a. Map Text (RecordField s a) -> Expr s a
RecordLit forall a b. (a -> b) -> a -> b
$ RecordField s a -> RecordField s b
bindRecordKeyValues forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text (RecordField s a)
a
        Lam Maybe CharacterSet
cs FunctionBinding s a
a Expr s a
b  -> forall s a.
Maybe CharacterSet -> FunctionBinding s a -> Expr s a -> Expr s a
Lam Maybe CharacterSet
cs (FunctionBinding s a -> FunctionBinding s b
adaptFunctionBinding FunctionBinding s a
a) (Expr s a
b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k)
        Field Expr s a
a FieldSelection s
b   -> forall s a. Expr s a -> FieldSelection s -> Expr s a
Field (Expr s a
a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k) FieldSelection s
b
        Expr s a
_ -> forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
Lens.over forall (f :: * -> *) s a t b.
Applicative f =>
(Expr s a -> f (Expr t b)) -> Expr s a -> f (Expr t b)
unsafeSubExpressions (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k) Expr s a
expression
      where
        bindRecordKeyValues :: RecordField s a -> RecordField s b
bindRecordKeyValues (RecordField Maybe s
s0 Expr s a
e Maybe s
s1 Maybe s
s2) =
            forall s a.
Maybe s -> Expr s a -> Maybe s -> Maybe s -> RecordField s a
RecordField Maybe s
s0 (Expr s a
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k) Maybe s
s1 Maybe s
s2

        adaptBinding :: Binding s a -> Binding s b
adaptBinding (Binding Maybe s
src0 Text
c Maybe s
src1 Maybe (Maybe s, Expr s a)
d Maybe s
src2 Expr s a
e) =
            forall s a.
Maybe s
-> Text
-> Maybe s
-> Maybe (Maybe s, Expr s a)
-> Maybe s
-> Expr s a
-> Binding s a
Binding Maybe s
src0 Text
c Maybe s
src1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. (a, Expr s a) -> (a, Expr s b)
adaptBindingAnnotation Maybe (Maybe s, Expr s a)
d) Maybe s
src2 (Expr s a
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k)

        adaptFunctionBinding :: FunctionBinding s a -> FunctionBinding s b
adaptFunctionBinding (FunctionBinding Maybe s
src0 Text
label Maybe s
src1 Maybe s
src2 Expr s a
type_) =
            forall s a.
Maybe s
-> Text -> Maybe s -> Maybe s -> Expr s a -> FunctionBinding s a
FunctionBinding Maybe s
src0 Text
label Maybe s
src1 Maybe s
src2 (Expr s a
type_ forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k)

        adaptBindingAnnotation :: (a, Expr s a) -> (a, Expr s b)
adaptBindingAnnotation (a
src3, Expr s a
f) = (a
src3, Expr s a
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Expr s b
k)