{-# OPTIONS_GHC -Wno-orphans #-}

module Dhall.Syntax.Instances.Applicative () where

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

import qualified Lens.Family as Lens

instance Applicative (Expr s) where
    pure :: forall a. a -> Expr s a
pure = forall s a. a -> Expr s a
Embed

    Expr s (a -> b)
expression <*> :: forall a b. Expr s (a -> b) -> Expr s a -> Expr s b
<*> Expr s a
k = case Expr s (a -> b)
expression of
        Embed a -> b
a     -> a -> b
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Expr s a
k
        Let Binding s (a -> b)
a Expr s (a -> b)
b     -> forall s a. Binding s a -> Expr s a -> Expr s a
Let (forall {a}. Binding s (a -> a) -> Binding s a
adaptBinding Binding s (a -> b)
a) (Expr s (a -> b)
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k)
        Note s
a Expr s (a -> b)
b    -> forall s a. s -> Expr s a -> Expr s a
Note s
a (Expr s (a -> b)
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k)
        Record Map Text (RecordField s (a -> b))
a    -> forall s a. Map Text (RecordField s a) -> Expr s a
Record forall a b. (a -> b) -> a -> b
$ forall {a}. RecordField s (a -> a) -> RecordField s a
adaptRecordField forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text (RecordField s (a -> b))
a
        RecordLit Map Text (RecordField s (a -> b))
a -> forall s a. Map Text (RecordField s a) -> Expr s a
RecordLit forall a b. (a -> b) -> a -> b
$ forall {a}. RecordField s (a -> a) -> RecordField s a
adaptRecordField forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text (RecordField s (a -> b))
a
        Lam Maybe CharacterSet
cs FunctionBinding s (a -> b)
a Expr s (a -> b)
b  -> forall s a.
Maybe CharacterSet -> FunctionBinding s a -> Expr s a -> Expr s a
Lam Maybe CharacterSet
cs (forall {a}. FunctionBinding s (a -> a) -> FunctionBinding s a
adaptFunctionBinding FunctionBinding s (a -> b)
a) (Expr s (a -> b)
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k)
        Field Expr s (a -> b)
a FieldSelection s
b   -> forall s a. Expr s a -> FieldSelection s -> Expr s a
Field (Expr s (a -> b)
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k) FieldSelection s
b
        Expr s (a -> b)
_ -> 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 (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k) Expr s (a -> b)
expression
      where
        adaptRecordField :: RecordField s (a -> a) -> RecordField s a
adaptRecordField (RecordField Maybe s
s0 Expr s (a -> 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 -> a)
e forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k) Maybe s
s1 Maybe s
s2

        adaptBinding :: Binding s (a -> a) -> Binding s a
adaptBinding (Binding Maybe s
src0 Text
c Maybe s
src1 Maybe (Maybe s, Expr s (a -> a))
d Maybe s
src2 Expr s (a -> 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} {b}. (a, Expr s (a -> b)) -> (a, Expr s b)
adaptBindingAnnotation Maybe (Maybe s, Expr s (a -> a))
d) Maybe s
src2 (Expr s (a -> a)
e forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k)

        adaptFunctionBinding :: FunctionBinding s (a -> a) -> FunctionBinding s a
adaptFunctionBinding (FunctionBinding Maybe s
src0 Text
label Maybe s
src1 Maybe s
src2 Expr s (a -> 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 -> a)
type_ forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expr s a
k)

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