{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

module Hyper.Syntax.App
    ( App (..)
    , appFunc
    , appArg
    , W_App (..)
    , MorphWitness (..)
    ) where

import Hyper
import Hyper.Class.Optic (HSubset (..), HSubset')
import Hyper.Infer
import Hyper.Syntax.FuncType
import Hyper.Unify (UnifyGen, unify)
import Hyper.Unify.New (newTerm, newUnbound)
import Text.PrettyPrint ((<+>))
import Text.PrettyPrint.HughesPJClass (Pretty (..), maybeParens)

import Hyper.Internal.Prelude

-- | A term for function applications.
--
-- @App expr@s express function applications of @expr@s.
--
-- Apart from the data type, an 'Infer' instance is also provided.
data App expr h = App
    { forall (expr :: HyperType) (h :: AHyperType).
App expr h -> h :# expr
_appFunc :: h :# expr
    , forall (expr :: HyperType) (h :: AHyperType).
App expr h -> h :# expr
_appArg :: h :# expr
    }
    deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (expr :: HyperType) (h :: AHyperType) x.
Rep (App expr h) x -> App expr h
forall (expr :: HyperType) (h :: AHyperType) x.
App expr h -> Rep (App expr h) x
$cto :: forall (expr :: HyperType) (h :: AHyperType) x.
Rep (App expr h) x -> App expr h
$cfrom :: forall (expr :: HyperType) (h :: AHyperType) x.
App expr h -> Rep (App expr h) x
Generic)

makeLenses ''App
makeZipMatch ''App
makeHContext ''App
makeHMorph ''App
makeHTraversableApplyAndBases ''App
makeCommonInstances [''App]

instance RNodes e => RNodes (App e)
instance (c (App e), Recursively c e) => Recursively c (App e)
instance RTraversable e => RTraversable (App e)

instance Pretty (h :# expr) => Pretty (App expr h) where
    pPrintPrec :: PrettyLevel -> Rational -> App expr h -> Doc
pPrintPrec PrettyLevel
lvl Rational
p (App h :# expr
f h :# expr
x) =
        forall a. Pretty a => PrettyLevel -> Rational -> a -> Doc
pPrintPrec PrettyLevel
lvl Rational
10 h :# expr
f
            Doc -> Doc -> Doc
<+> forall a. Pretty a => PrettyLevel -> Rational -> a -> Doc
pPrintPrec PrettyLevel
lvl Rational
11 h :# expr
x
            forall a b. a -> (a -> b) -> b
& Bool -> Doc -> Doc
maybeParens (Rational
p forall a. Ord a => a -> a -> Bool
> Rational
10)

type instance InferOf (App e) = ANode (TypeOf e)

instance
    ( Infer m expr
    , HasInferredType expr
    , HSubset' (TypeOf expr) (FuncType (TypeOf expr))
    , UnifyGen m (TypeOf expr)
    ) =>
    Infer m (App expr)
    where
    {-# INLINE inferBody #-}
    inferBody :: forall (h :: HyperType).
(App expr # InferChild m h)
-> m (App expr # h, InferOf (App expr) # UVarOf m)
inferBody (App 'AHyperType (InferChild m h) :# expr
func 'AHyperType (InferChild m h) :# expr
arg) =
        do
            InferredChild h ('AHyperType expr)
argI InferOf (GetHyperType ('AHyperType expr)) # UVarOf m
argR <- forall (m :: * -> *) (h :: HyperType) (t :: AHyperType).
InferChild m h t -> m (InferredChild (UVarOf m) h t)
inferChild 'AHyperType (InferChild m h) :# expr
arg
            InferredChild h ('AHyperType expr)
funcI InferOf (GetHyperType ('AHyperType expr)) # UVarOf m
funcR <- forall (m :: * -> *) (h :: HyperType) (t :: AHyperType).
InferChild m h t -> m (InferredChild (UVarOf m) h t)
inferChild 'AHyperType (InferChild m h) :# expr
func
            UVarOf m # TypeOf expr
funcRes <- forall (m :: * -> *) (t :: HyperType).
UnifyGen m t =>
m (UVarOf m # t)
newUnbound
            (forall (expr :: HyperType) (h :: AHyperType).
(h :# expr) -> (h :# expr) -> App expr h
App h ('AHyperType expr)
funcI h ('AHyperType expr)
argI, forall (c :: HyperType) (h :: AHyperType). (h :# c) -> ANode c h
MkANode UVarOf m # TypeOf expr
funcRes)
                forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (forall (m :: * -> *) (t :: HyperType).
UnifyGen m t =>
(t # UVarOf m) -> m (UVarOf m # t)
newTerm (forall (s :: HyperType) (t :: HyperType) (a :: HyperType)
       (b :: HyperType) (h :: HyperType).
HSubset s t a b =>
Prism (s # h) (t # h) (a # h) (b # h)
hSubset forall t b. AReview t b -> b -> t
# forall (typ :: HyperType) (h :: AHyperType).
(h :# typ) -> (h :# typ) -> FuncType typ h
FuncType (InferOf (GetHyperType ('AHyperType expr)) # UVarOf m
argR forall s t a b. s -> ALens s t a b -> a
^# ALens' (InferOf expr # UVarOf m) (UVarOf m # TypeOf expr)
l) UVarOf m # TypeOf expr
funcRes) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) (t :: HyperType).
Unify m t =>
(UVarOf m # t) -> (UVarOf m # t) -> m (UVarOf m # t)
unify (InferOf (GetHyperType ('AHyperType expr)) # UVarOf m
funcR forall s t a b. s -> ALens s t a b -> a
^# ALens' (InferOf expr # UVarOf m) (UVarOf m # TypeOf expr)
l))
        where
            l :: ALens' (InferOf expr # UVarOf m) (UVarOf m # TypeOf expr)
l = forall (t :: HyperType) (v :: HyperType).
HasInferredType t =>
Proxy t -> ALens' (InferOf t # v) (v # TypeOf t)
inferredType (forall {k} (t :: k). Proxy t
Proxy @expr)