{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
module PlutusIR.Transform.Inline.Inline (inline, inlinePass, inlinePassSC, InlineHints (..)) where
import PlutusCore qualified as PLC
import PlutusCore.Annotation
import PlutusCore.Name.Unique
import PlutusCore.Quote
import PlutusCore.Rename (dupable)
import PlutusIR
import PlutusIR.Analysis.Builtins
import PlutusIR.Analysis.Size (termSize)
import PlutusIR.Analysis.Usages qualified as Usages
import PlutusIR.Analysis.VarInfo qualified as VarInfo
import PlutusIR.Contexts (AppContext (..), fillAppContext, splitApplication)
import PlutusIR.MkPir (mkLet)
import PlutusIR.Pass
import PlutusIR.Transform.Inline.CallSiteInline (callSiteInline)
import PlutusIR.Transform.Inline.Utils
import PlutusIR.Transform.Rename ()
import PlutusIR.TypeCheck qualified as TC
import PlutusPrelude
import Control.Lens (forMOf, traverseOf)
import Control.Monad.Extra
import Control.Monad.Reader (runReaderT)
import Control.Monad.State (evalStateT, modify')
import Control.Monad.State.Class (gets)
import Data.Maybe
inlinePassSC
:: forall uni fun ann m
. (PLC.Typecheckable uni fun, PLC.GEq uni, Ord ann, ExternalConstraints TyName Name uni fun m)
=> Bool
-> TC.PirTCConfig uni fun
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Pass m TyName Name uni fun ann
inlinePassSC :: forall (uni :: * -> *) fun ann (m :: * -> *).
(Typecheckable uni fun, GEq uni, Ord ann,
ExternalConstraints TyName Name uni fun m) =>
Bool
-> PirTCConfig uni fun
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Pass m TyName Name uni fun ann
inlinePassSC Bool
ic PirTCConfig uni fun
tcconfig InlineHints Name ann
hints BuiltinsInfo uni fun
binfo =
Pass m TyName Name uni fun ann
forall name tyname (m :: * -> *) a (uni :: * -> *) fun.
(HasUnique name TermUnique, HasUnique tyname TypeUnique,
MonadQuote m, Ord a) =>
Pass m tyname name uni fun a
renamePass Pass m TyName Name uni fun ann
-> Pass m TyName Name uni fun ann -> Pass m TyName Name uni fun ann
forall a. Semigroup a => a -> a -> a
<> Bool
-> PirTCConfig uni fun
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Pass m TyName Name uni fun ann
forall (uni :: * -> *) fun ann (m :: * -> *).
(Typecheckable uni fun, GEq uni, Ord ann,
ExternalConstraints TyName Name uni fun m) =>
Bool
-> PirTCConfig uni fun
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Pass m TyName Name uni fun ann
inlinePass Bool
ic PirTCConfig uni fun
tcconfig InlineHints Name ann
hints BuiltinsInfo uni fun
binfo
inlinePass
:: forall uni fun ann m
. (PLC.Typecheckable uni fun, PLC.GEq uni, Ord ann, ExternalConstraints TyName Name uni fun m)
=> Bool
-> TC.PirTCConfig uni fun
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Pass m TyName Name uni fun ann
inlinePass :: forall (uni :: * -> *) fun ann (m :: * -> *).
(Typecheckable uni fun, GEq uni, Ord ann,
ExternalConstraints TyName Name uni fun m) =>
Bool
-> PirTCConfig uni fun
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Pass m TyName Name uni fun ann
inlinePass Bool
ic PirTCConfig uni fun
tcconfig InlineHints Name ann
hints BuiltinsInfo uni fun
binfo =
String
-> Pass m TyName Name uni fun ann -> Pass m TyName Name uni fun ann
forall (m :: * -> *) tyname name (uni :: * -> *) fun a.
String
-> Pass m tyname name uni fun a -> Pass m tyname name uni fun a
NamedPass String
"inline" (Pass m TyName Name uni fun ann -> Pass m TyName Name uni fun ann)
-> Pass m TyName Name uni fun ann -> Pass m TyName Name uni fun ann
forall a b. (a -> b) -> a -> b
$
(Term TyName Name uni fun ann -> m (Term TyName Name uni fun ann))
-> [Condition TyName Name uni fun ann]
-> [BiCondition TyName Name uni fun ann]
-> Pass m TyName Name uni fun ann
forall (m :: * -> *) tyname name (uni :: * -> *) fun a.
(Term tyname name uni fun a -> m (Term tyname name uni fun a))
-> [Condition tyname name uni fun a]
-> [BiCondition tyname name uni fun a]
-> Pass m tyname name uni fun a
Pass
(Bool
-> InlineHints Name ann
-> BuiltinsInfo uni fun
-> Term TyName Name uni fun ann
-> m (Term TyName Name uni fun ann)
forall tyname name (uni :: * -> *) fun ann (m :: * -> *).
ExternalConstraints tyname name uni fun m =>
Bool
-> InlineHints name ann
-> BuiltinsInfo uni fun
-> Term tyname name uni fun ann
-> m (Term tyname name uni fun ann)
inline Bool
ic InlineHints Name ann
hints BuiltinsInfo uni fun
binfo )
[Condition TyName Name uni fun ann
forall tyname name a (uni :: * -> *) fun.
(HasUnique tyname TypeUnique, HasUnique name TermUnique, Ord a) =>
Condition tyname name uni fun a
GloballyUniqueNames, PirTCConfig uni fun -> Condition TyName Name uni fun ann
forall (uni :: * -> *) fun a.
(Typecheckable uni fun, GEq uni) =>
PirTCConfig uni fun -> Condition TyName Name uni fun a
Typechecks PirTCConfig uni fun
tcconfig]
[Condition TyName Name uni fun ann
-> BiCondition TyName Name uni fun ann
forall tyname name (uni :: * -> *) fun a.
Condition tyname name uni fun a
-> BiCondition tyname name uni fun a
ConstCondition Condition TyName Name uni fun ann
forall tyname name a (uni :: * -> *) fun.
(HasUnique tyname TypeUnique, HasUnique name TermUnique, Ord a) =>
Condition tyname name uni fun a
GloballyUniqueNames, Condition TyName Name uni fun ann
-> BiCondition TyName Name uni fun ann
forall tyname name (uni :: * -> *) fun a.
Condition tyname name uni fun a
-> BiCondition tyname name uni fun a
ConstCondition (PirTCConfig uni fun -> Condition TyName Name uni fun ann
forall (uni :: * -> *) fun a.
(Typecheckable uni fun, GEq uni) =>
PirTCConfig uni fun -> Condition TyName Name uni fun a
Typechecks PirTCConfig uni fun
tcconfig)]
inline
:: forall tyname name uni fun ann m
. ExternalConstraints tyname name uni fun m
=> Bool
-> InlineHints name ann
-> BuiltinsInfo uni fun
-> Term tyname name uni fun ann
-> m (Term tyname name uni fun ann)
inline :: forall tyname name (uni :: * -> *) fun ann (m :: * -> *).
ExternalConstraints tyname name uni fun m =>
Bool
-> InlineHints name ann
-> BuiltinsInfo uni fun
-> Term tyname name uni fun ann
-> m (Term tyname name uni fun ann)
inline Bool
ic InlineHints name ann
hints BuiltinsInfo uni fun
binfo Term tyname name uni fun ann
t = let
inlineInfo :: InlineInfo tyname name uni fun ann
inlineInfo :: InlineInfo tyname name uni fun ann
inlineInfo = VarsInfo tyname name uni ann
-> Usages
-> InlineHints name ann
-> BuiltinsInfo uni fun
-> Bool
-> InlineInfo tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
VarsInfo tyname name uni ann
-> Usages
-> InlineHints name ann
-> BuiltinsInfo uni fun
-> Bool
-> InlineInfo tyname name uni fun ann
InlineInfo VarsInfo tyname name uni ann
vinfo Usages
usgs InlineHints name ann
hints BuiltinsInfo uni fun
binfo Bool
ic
vinfo :: VarsInfo tyname name uni ann
vinfo = Term tyname name uni fun ann -> VarsInfo tyname name uni ann
forall name tyname (uni :: * -> *) fun a.
(HasUnique name TermUnique, HasUnique tyname TypeUnique) =>
Term tyname name uni fun a -> VarsInfo tyname name uni a
VarInfo.termVarInfo Term tyname name uni fun ann
t
usgs :: Usages.Usages
usgs :: Usages
usgs = Term tyname name uni fun ann -> Usages
forall name tyname (uni :: * -> *) fun a.
(HasUnique name TermUnique, HasUnique tyname TypeUnique) =>
Term tyname name uni fun a -> Usages
Usages.termUsages Term tyname name uni fun ann
t
in Quote (Term tyname name uni fun ann)
-> m (Term tyname name uni fun ann)
forall a. Quote a -> m a
forall (m :: * -> *) a. MonadQuote m => Quote a -> m a
liftQuote (Quote (Term tyname name uni fun ann)
-> m (Term tyname name uni fun ann))
-> Quote (Term tyname name uni fun ann)
-> m (Term tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ (StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
-> InlinerState tyname name uni fun ann
-> Quote (Term tyname name uni fun ann))
-> InlinerState tyname name uni fun ann
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
-> Quote (Term tyname name uni fun ann)
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
-> InlinerState tyname name uni fun ann
-> Quote (Term tyname name uni fun ann)
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT InlinerState tyname name uni fun ann
forall a. Monoid a => a
mempty (StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
-> Quote (Term tyname name uni fun ann))
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
-> Quote (Term tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ (ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
-> InlineInfo tyname name uni fun ann
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann))
-> InlineInfo tyname name uni fun ann
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
-> InlineInfo tyname name uni fun ann
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT InlineInfo tyname name uni fun ann
inlineInfo (ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann))
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
-> StateT
(InlinerState tyname name uni fun ann)
(QuoteT Identity)
(Term tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ Term tyname name uni fun ann
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm Term tyname name uni fun ann
t
processTerm
:: forall tyname name uni fun ann. InliningConstraints tyname name uni fun
=> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm :: forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm = Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
handleTerm (Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann))
-> (Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann))
-> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< LensLike
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity)))
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Type tyname uni ann)
(Type tyname uni ann)
-> LensLike
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity)))
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Type tyname uni ann)
(Type tyname uni ann)
forall (f :: * -> *) s t a b.
LensLike f s t a b -> LensLike f s t a b
traverseOf LensLike
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity)))
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Type tyname uni ann)
(Type tyname uni ann)
forall tyname name (uni :: * -> *) fun a (f :: * -> *).
Applicative f =>
(Type tyname uni a -> f (Type tyname uni a))
-> Term tyname name uni fun a -> f (Term tyname name uni fun a)
termSubtypes Type tyname uni ann
-> InlineM tyname name uni fun ann (Type tyname uni ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Type tyname uni ann
-> InlineM tyname name uni fun ann (Type tyname uni ann)
applyTypeSubstitution where
handleTerm ::
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
handleTerm :: Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
handleTerm = \case
v :: Term tyname name uni fun ann
v@(Var ann
_ name
n) -> Term tyname name uni fun ann
-> Maybe (Term tyname name uni fun ann)
-> Term tyname name uni fun ann
forall a. a -> Maybe a -> a
fromMaybe Term tyname name uni fun ann
v (Maybe (Term tyname name uni fun ann)
-> Term tyname name uni fun ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe (Term tyname name uni fun ann))
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> name
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe (Term tyname name uni fun ann))
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
name
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
substName name
n
Let ann
ann Recursivity
NonRec NonEmpty (Binding tyname name uni fun ann)
bs Term tyname name uni fun ann
t -> case NonEmpty (Binding tyname name uni fun ann)
bs of
Binding tyname name uni fun ann
b :| [] -> do
Maybe (Binding tyname name uni fun ann)
b' <- Term tyname name uni fun ann
-> Binding tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> Binding tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
processSingleBinding Term tyname name uni fun ann
t Binding tyname name uni fun ann
b
Term tyname name uni fun ann
t' <- Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm Term tyname name uni fun ann
t
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann))
-> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ ann
-> Recursivity
-> [Binding tyname name uni fun ann]
-> Term tyname name uni fun ann
-> Term tyname name uni fun ann
forall a tyname name (uni :: * -> *) fun.
a
-> Recursivity
-> [Binding tyname name uni fun a]
-> Term tyname name uni fun a
-> Term tyname name uni fun a
mkLet ann
ann Recursivity
NonRec (Maybe (Binding tyname name uni fun ann)
-> [Binding tyname name uni fun ann]
forall a. Maybe a -> [a]
maybeToList Maybe (Binding tyname name uni fun ann)
b') Term tyname name uni fun ann
t'
Binding tyname name uni fun ann
b :| [Binding tyname name uni fun ann]
rest -> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
handleTerm (ann
-> Recursivity
-> NonEmpty (Binding tyname name uni fun ann)
-> Term tyname name uni fun ann
-> Term tyname name uni fun ann
forall tyname name (uni :: * -> *) fun a.
a
-> Recursivity
-> NonEmpty (Binding tyname name uni fun a)
-> Term tyname name uni fun a
-> Term tyname name uni fun a
Let ann
ann Recursivity
NonRec (Binding tyname name uni fun ann
-> NonEmpty (Binding tyname name uni fun ann)
forall a. a -> NonEmpty a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Binding tyname name uni fun ann
b) (ann
-> Recursivity
-> [Binding tyname name uni fun ann]
-> Term tyname name uni fun ann
-> Term tyname name uni fun ann
forall a tyname name (uni :: * -> *) fun.
a
-> Recursivity
-> [Binding tyname name uni fun a]
-> Term tyname name uni fun a
-> Term tyname name uni fun a
mkLet ann
ann Recursivity
NonRec [Binding tyname name uni fun ann]
rest Term tyname name uni fun ann
t))
Term tyname name uni fun ann
t -> do
let (Term tyname name uni fun ann
hd, AppContext tyname name uni fun ann
args) = Term tyname name uni fun ann
-> (Term tyname name uni fun ann,
AppContext tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
Term tyname name uni fun ann
-> (Term tyname name uni fun ann,
AppContext tyname name uni fun ann)
splitApplication Term tyname name uni fun ann
t
processArgs ::
AppContext tyname name uni fun ann ->
InlineM tyname name uni fun ann (AppContext tyname name uni fun ann)
processArgs :: AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
processArgs (TermAppContext Term tyname name uni fun ann
arg ann
ann AppContext tyname name uni fun ann
ctx) = do
Term tyname name uni fun ann
processedArg <- Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm Term tyname name uni fun ann
arg
AppContext tyname name uni fun ann
processedArgs <- AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
processArgs AppContext tyname name uni fun ann
ctx
AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann))
-> AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ Term tyname name uni fun ann
-> ann
-> AppContext tyname name uni fun ann
-> AppContext tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Term tyname name uni fun ann
-> ann
-> AppContext tyname name uni fun ann
-> AppContext tyname name uni fun ann
TermAppContext Term tyname name uni fun ann
processedArg ann
ann AppContext tyname name uni fun ann
processedArgs
processArgs (TypeAppContext Type tyname uni ann
ty ann
ann AppContext tyname name uni fun ann
ctx) = do
AppContext tyname name uni fun ann
processedArgs <- AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
processArgs AppContext tyname name uni fun ann
ctx
Type tyname uni ann
ty' <- Type tyname uni ann
-> InlineM tyname name uni fun ann (Type tyname uni ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Type tyname uni ann
-> InlineM tyname name uni fun ann (Type tyname uni ann)
applyTypeSubstitution Type tyname uni ann
ty
AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann))
-> AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ Type tyname uni ann
-> ann
-> AppContext tyname name uni fun ann
-> AppContext tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Type tyname uni ann
-> ann
-> AppContext tyname name uni fun ann
-> AppContext tyname name uni fun ann
TypeAppContext Type tyname uni ann
ty' ann
ann AppContext tyname name uni fun ann
processedArgs
processArgs AppContext tyname name uni fun ann
AppContextEnd = AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AppContext tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
AppContext tyname name uni fun ann
AppContextEnd
case AppContext tyname name uni fun ann
args of
AppContext tyname name uni fun ann
AppContextEnd -> LensLike
(WrappedMonad
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))))
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
-> Term tyname name uni fun ann
-> (Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann))
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall (m :: * -> *) s t a b.
LensLike (WrappedMonad m) s t a b -> s -> (a -> m b) -> m t
forMOf LensLike
(WrappedMonad
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))))
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun a (f :: * -> *).
Applicative f =>
(Term tyname name uni fun a -> f (Term tyname name uni fun a))
-> Term tyname name uni fun a -> f (Term tyname name uni fun a)
termSubterms Term tyname name uni fun ann
t Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm
AppContext tyname name uni fun ann
_ -> do
Term tyname name uni fun ann
hd' <- Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm Term tyname name uni fun ann
hd
AppContext tyname name uni fun ann
args' <- AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (AppContext tyname name uni fun ann)
processArgs AppContext tyname name uni fun ann
args
let reconstructed :: Term tyname name uni fun ann
reconstructed = Term tyname name uni fun ann
-> AppContext tyname name uni fun ann
-> Term tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Term tyname name uni fun ann
-> AppContext tyname name uni fun ann
-> Term tyname name uni fun ann
fillAppContext Term tyname name uni fun ann
hd' AppContext tyname name uni fun ann
args'
case Term tyname name uni fun ann
hd' of
Var ann
_ name
name -> do
(InlinerState tyname name uni fun ann
-> Maybe (InlineVarInfo tyname name uni fun ann))
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe (InlineVarInfo tyname name uni fun ann))
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (name
-> InlinerState tyname name uni fun ann
-> Maybe (InlineVarInfo tyname name uni fun ann)
forall name tyname (uni :: * -> *) fun ann.
HasUnique name TermUnique =>
name
-> InlinerState tyname name uni fun ann
-> Maybe (InlineVarInfo tyname name uni fun ann)
lookupVarInfo name
name) ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe (InlineVarInfo tyname name uni fun ann))
-> (Maybe (InlineVarInfo tyname name uni fun ann)
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann))
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a b.
ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
-> (a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
b)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just InlineVarInfo tyname name uni fun ann
varInfo -> do
Maybe (Term tyname name uni fun ann)
maybeInlined <-
Size
-> InlineVarInfo tyname name uni fun ann
-> AppContext tyname name uni fun ann
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe (Term tyname name uni fun ann))
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Size
-> InlineVarInfo tyname name uni fun ann
-> AppContext tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
callSiteInline
(Term tyname name uni fun ann -> Size
forall tyname name (uni :: * -> *) fun ann.
Term tyname name uni fun ann -> Size
termSize Term tyname name uni fun ann
reconstructed)
InlineVarInfo tyname name uni fun ann
varInfo
AppContext tyname name uni fun ann
args'
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann))
-> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ Term tyname name uni fun ann
-> Maybe (Term tyname name uni fun ann)
-> Term tyname name uni fun ann
forall a. a -> Maybe a -> a
fromMaybe Term tyname name uni fun ann
reconstructed Maybe (Term tyname name uni fun ann)
maybeInlined
Maybe (InlineVarInfo tyname name uni fun ann)
Nothing -> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term tyname name uni fun ann
reconstructed
Term tyname name uni fun ann
_ -> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term tyname name uni fun ann
reconstructed
processSingleBinding
:: forall tyname name uni fun ann. InliningConstraints tyname name uni fun
=> Term tyname name uni fun ann
-> Binding tyname name uni fun ann
-> InlineM tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
processSingleBinding :: forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> Binding tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
processSingleBinding Term tyname name uni fun ann
body = \case
(TermBind ann
ann Strictness
s v :: VarDecl tyname name uni ann
v@(VarDecl ann
_ name
n Type tyname uni ann
_) Term tyname name uni fun ann
rhs0) -> do
Term tyname name uni fun ann
-> ann
-> Strictness
-> name
-> Term tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> ann
-> Strictness
-> name
-> Term tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
maybeAddSubst Term tyname name uni fun ann
body ann
ann Strictness
s name
n Term tyname name uni fun ann
rhs0 InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
-> (Maybe (Term tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann)))
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall a b.
ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
-> (a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
b)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe (Term tyname name uni fun ann)
Nothing -> Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Binding tyname name uni fun ann)
forall a. Maybe a
Nothing
Just Term tyname name uni fun ann
rhs -> do
(InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' ((InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
())
-> (InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
()
forall a b. (a -> b) -> a -> b
$
name
-> InlineVarInfo tyname name uni fun ann
-> InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann
forall name tyname (uni :: * -> *) fun ann.
HasUnique name TermUnique =>
name
-> InlineVarInfo tyname name uni fun ann
-> InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann
extendVarInfo
name
n
(Strictness
-> InlineTerm tyname name uni fun ann
-> InlineVarInfo tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Strictness
-> InlineTerm tyname name uni fun ann
-> InlineVarInfo tyname name uni fun ann
MkVarInfo Strictness
s (Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
Done (Term tyname name uni fun ann
-> Dupable (Term tyname name uni fun ann)
forall a. a -> Dupable a
dupable Term tyname name uni fun ann
rhs)))
Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann)))
-> Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall a b. (a -> b) -> a -> b
$ Binding tyname name uni fun ann
-> Maybe (Binding tyname name uni fun ann)
forall a. a -> Maybe a
Just (Binding tyname name uni fun ann
-> Maybe (Binding tyname name uni fun ann))
-> Binding tyname name uni fun ann
-> Maybe (Binding tyname name uni fun ann)
forall a b. (a -> b) -> a -> b
$ ann
-> Strictness
-> VarDecl tyname name uni ann
-> Term tyname name uni fun ann
-> Binding tyname name uni fun ann
forall tyname name (uni :: * -> *) fun a.
a
-> Strictness
-> VarDecl tyname name uni a
-> Term tyname name uni fun a
-> Binding tyname name uni fun a
TermBind ann
ann Strictness
s VarDecl tyname name uni ann
v Term tyname name uni fun ann
rhs
(TypeBind ann
ann v :: TyVarDecl tyname ann
v@(TyVarDecl ann
_ tyname
n Kind ann
_) Type tyname uni ann
rhs) -> do
Maybe (Type tyname uni ann)
maybeRhs' <- tyname
-> Type tyname uni ann
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
tyname
-> Type tyname uni ann
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
maybeAddTySubst tyname
n Type tyname uni ann
rhs
Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann)))
-> Maybe (Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall a b. (a -> b) -> a -> b
$ ann
-> TyVarDecl tyname ann
-> Type tyname uni ann
-> Binding tyname name uni fun ann
forall tyname name (uni :: * -> *) fun a.
a
-> TyVarDecl tyname a
-> Type tyname uni a
-> Binding tyname name uni fun a
TypeBind ann
ann TyVarDecl tyname ann
v (Type tyname uni ann -> Binding tyname name uni fun ann)
-> Maybe (Type tyname uni ann)
-> Maybe (Binding tyname name uni fun ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Type tyname uni ann)
maybeRhs'
Binding tyname name uni fun ann
b ->
Binding tyname name uni fun ann
-> Maybe (Binding tyname name uni fun ann)
forall a. a -> Maybe a
Just (Binding tyname name uni fun ann
-> Maybe (Binding tyname name uni fun ann))
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Binding tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Binding tyname name uni fun ann))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LensLike
(WrappedMonad
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))))
(Binding tyname name uni fun ann)
(Binding tyname name uni fun ann)
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
-> Binding tyname name uni fun ann
-> (Term tyname name uni fun ann
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann))
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Binding tyname name uni fun ann)
forall (m :: * -> *) s t a b.
LensLike (WrappedMonad m) s t a b -> s -> (a -> m b) -> m t
forMOf LensLike
(WrappedMonad
(ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))))
(Binding tyname name uni fun ann)
(Binding tyname name uni fun ann)
(Term tyname name uni fun ann)
(Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun a (f :: * -> *).
Applicative f =>
(Term tyname name uni fun a -> f (Term tyname name uni fun a))
-> Binding tyname name uni fun a
-> f (Binding tyname name uni fun a)
bindingSubterms Binding tyname name uni fun ann
b Term tyname name uni fun ann
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm
maybeAddSubst
:: forall tyname name uni fun ann. InliningConstraints tyname name uni fun
=> Term tyname name uni fun ann
-> ann
-> Strictness
-> name
-> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
maybeAddSubst :: forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> ann
-> Strictness
-> name
-> Term tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
maybeAddSubst Term tyname name uni fun ann
body ann
ann Strictness
s name
n Term tyname name uni fun ann
rhs0 = do
Term tyname name uni fun ann
rhs <- Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Term tyname name uni fun ann
-> InlineM tyname name uni fun ann (Term tyname name uni fun ann)
processTerm Term tyname name uni fun ann
rhs0
InlineHints name ann
hints <- Getting
(InlineHints name ann)
(InlineInfo tyname name uni fun ann)
(InlineHints name ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(InlineHints name ann)
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting
(InlineHints name ann)
(InlineInfo tyname name uni fun ann)
(InlineHints name ann)
forall tyname name (uni :: * -> *) fun ann (f :: * -> *).
Functor f =>
(InlineHints name ann -> f (InlineHints name ann))
-> InlineInfo tyname name uni fun ann
-> f (InlineInfo tyname name uni fun ann)
iiHints
let hinted :: Bool
hinted = InlineHints name ann -> ann -> name -> Bool
forall name a. InlineHints name a -> a -> name -> Bool
shouldInline InlineHints name ann
hints ann
ann name
n
if Bool
hinted
then InlineTerm tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
forall b.
InlineTerm tyname name uni fun ann
-> InlineM tyname name uni fun ann (Maybe b)
extendAndDrop (Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
Done (Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann)
-> Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
forall a b. (a -> b) -> a -> b
$ Term tyname name uni fun ann
-> Dupable (Term tyname name uni fun ann)
forall a. a -> Dupable a
dupable Term tyname name uni fun ann
rhs)
else
ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
Bool
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM
(Strictness
-> name
-> Term tyname name uni fun ann
-> Term tyname name uni fun ann
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
Bool
forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
Strictness
-> name
-> Term tyname name uni fun ann
-> Term tyname name uni fun ann
-> InlineM tyname name uni fun ann Bool
shouldUnconditionallyInline Strictness
s name
n Term tyname name uni fun ann
rhs Term tyname name uni fun ann
body)
(InlineTerm tyname name uni fun ann
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
forall b.
InlineTerm tyname name uni fun ann
-> InlineM tyname name uni fun ann (Maybe b)
extendAndDrop (Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
Done (Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann)
-> Dupable (Term tyname name uni fun ann)
-> InlineTerm tyname name uni fun ann
forall a b. (a -> b) -> a -> b
$ Term tyname name uni fun ann
-> Dupable (Term tyname name uni fun ann)
forall a. a -> Dupable a
dupable Term tyname name uni fun ann
rhs))
(Maybe (Term tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Term tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann)))
-> Maybe (Term tyname name uni fun ann)
-> InlineM
tyname name uni fun ann (Maybe (Term tyname name uni fun ann))
forall a b. (a -> b) -> a -> b
$ Term tyname name uni fun ann
-> Maybe (Term tyname name uni fun ann)
forall a. a -> Maybe a
Just Term tyname name uni fun ann
rhs)
where
extendAndDrop ::
forall b . InlineTerm tyname name uni fun ann
-> InlineM tyname name uni fun ann (Maybe b)
extendAndDrop :: forall b.
InlineTerm tyname name uni fun ann
-> InlineM tyname name uni fun ann (Maybe b)
extendAndDrop InlineTerm tyname name uni fun ann
t = (InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' (name
-> InlineTerm tyname name uni fun ann
-> InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann
forall name tyname (uni :: * -> *) fun ann.
HasUnique name TermUnique =>
name
-> InlineTerm tyname name uni fun ann
-> InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann
extendTerm name
n InlineTerm tyname name uni fun ann
t) ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
()
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe b)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe b)
forall a b.
ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
b
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe b
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
(Maybe b)
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
maybeAddTySubst
:: forall tyname name uni fun ann . InliningConstraints tyname name uni fun
=> tyname
-> Type tyname uni ann
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
maybeAddTySubst :: forall tyname name (uni :: * -> *) fun ann.
InliningConstraints tyname name uni fun =>
tyname
-> Type tyname uni ann
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
maybeAddTySubst tyname
tn Type tyname uni ann
rhs = do
Usages
usgs <- Getting Usages (InlineInfo tyname name uni fun ann) Usages
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
Usages
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Usages (InlineInfo tyname name uni fun ann) Usages
forall tyname name (uni :: * -> *) fun ann (f :: * -> *).
Functor f =>
(Usages -> f Usages)
-> InlineInfo tyname name uni fun ann
-> f (InlineInfo tyname name uni fun ann)
iiUsages
let typeUsedAtMostOnce :: Bool
typeUsedAtMostOnce = tyname -> Usages -> Int
forall n unique. HasUnique n unique => n -> Usages -> Int
Usages.getUsageCount tyname
tn Usages
usgs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1
if Bool
typeUsedAtMostOnce Bool -> Bool -> Bool
|| Type tyname uni ann -> Bool
forall tyname (uni :: * -> *) ann. Type tyname uni ann -> Bool
trivialType Type tyname uni ann
rhs
then do
(InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann)
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' (tyname
-> Type tyname uni ann
-> InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann
forall tyname (uni :: * -> *) ann name fun.
HasUnique tyname TypeUnique =>
tyname
-> Type tyname uni ann
-> InlinerState tyname name uni fun ann
-> InlinerState tyname name uni fun ann
extendType tyname
tn Type tyname uni ann
rhs)
Maybe (Type tyname uni ann)
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Type tyname uni ann)
forall a. Maybe a
Nothing
else Maybe (Type tyname uni ann)
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
forall a.
a
-> ReaderT
(InlineInfo tyname name uni fun ann)
(StateT (InlinerState tyname name uni fun ann) (QuoteT Identity))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Type tyname uni ann)
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann)))
-> Maybe (Type tyname uni ann)
-> InlineM tyname name uni fun ann (Maybe (Type tyname uni ann))
forall a b. (a -> b) -> a -> b
$ Type tyname uni ann -> Maybe (Type tyname uni ann)
forall a. a -> Maybe a
Just Type tyname uni ann
rhs