-- | The user-facing API of the normalizer.

module PlutusCore.Normalize
    ( normalizeType
    , normalizeTypesIn
    , normalizeTypesInProgram
    ) where

import PlutusCore.Core
import PlutusCore.Name.Unique
import PlutusCore.Normalize.Internal
import PlutusCore.Rename

import Control.Monad ((>=>))

-- See Note [Normalization].
-- | Normalize a 'Type'.
normalizeType
    :: (HasUnique tyname TypeUnique, MonadNormalizeType uni m)
    => Type tyname uni ann -> m (Normalized (Type tyname uni ann))
normalizeType :: forall tyname (uni :: * -> *) (m :: * -> *) ann.
(HasUnique tyname TypeUnique, MonadNormalizeType uni m) =>
Type tyname uni ann -> m (Normalized (Type tyname uni ann))
normalizeType = Type tyname uni ann -> m (Type tyname uni ann)
forall a (m :: * -> *). (Rename a, MonadQuote m) => a -> m a
forall (m :: * -> *).
MonadQuote m =>
Type tyname uni ann -> m (Type tyname uni ann)
rename (Type tyname uni ann -> m (Type tyname uni ann))
-> (Type tyname uni ann -> m (Normalized (Type tyname uni ann)))
-> Type tyname uni ann
-> m (Normalized (Type tyname uni ann))
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NormalizeTypeT m tyname uni ann (Normalized (Type tyname uni ann))
-> m (Normalized (Type tyname uni ann))
forall (m :: * -> *) tyname (uni :: * -> *) ann a.
NormalizeTypeT m tyname uni ann a -> m a
runNormalizeTypeT (NormalizeTypeT m tyname uni ann (Normalized (Type tyname uni ann))
 -> m (Normalized (Type tyname uni ann)))
-> (Type tyname uni ann
    -> NormalizeTypeT
         m tyname uni ann (Normalized (Type tyname uni ann)))
-> Type tyname uni ann
-> m (Normalized (Type tyname uni ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type tyname uni ann
-> NormalizeTypeT
     m tyname uni ann (Normalized (Type tyname uni ann))
forall tyname (uni :: * -> *) (m :: * -> *) ann.
(HasUnique tyname TypeUnique, MonadNormalizeType uni m) =>
Type tyname uni ann
-> NormalizeTypeT
     m tyname uni ann (Normalized (Type tyname uni ann))
normalizeTypeM

-- | Normalize every 'Type' in a 'Term'.
normalizeTypesIn
    :: (HasUnique tyname TypeUnique, HasUnique name TermUnique, MonadNormalizeType uni m)
    => Term tyname name uni fun ann -> m (Term tyname name uni fun ann)
normalizeTypesIn :: forall tyname name (uni :: * -> *) (m :: * -> *) fun ann.
(HasUnique tyname TypeUnique, HasUnique name TermUnique,
 MonadNormalizeType uni m) =>
Term tyname name uni fun ann -> m (Term tyname name uni fun ann)
normalizeTypesIn = Term tyname name uni fun ann -> m (Term tyname name uni fun ann)
forall a (m :: * -> *). (Rename a, MonadQuote m) => a -> m a
forall (m :: * -> *).
MonadQuote m =>
Term tyname name uni fun ann -> m (Term tyname name uni fun ann)
rename (Term tyname name uni fun ann -> m (Term tyname name uni fun ann))
-> (Term tyname name uni fun ann
    -> m (Term tyname name uni fun ann))
-> Term tyname name uni fun ann
-> m (Term tyname name uni fun ann)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NormalizeTypeT m tyname uni ann (Term tyname name uni fun ann)
-> m (Term tyname name uni fun ann)
forall (m :: * -> *) tyname (uni :: * -> *) ann a.
NormalizeTypeT m tyname uni ann a -> m a
runNormalizeTypeT (NormalizeTypeT m tyname uni ann (Term tyname name uni fun ann)
 -> m (Term tyname name uni fun ann))
-> (Term tyname name uni fun ann
    -> NormalizeTypeT m tyname uni ann (Term tyname name uni fun ann))
-> Term tyname name uni fun ann
-> m (Term tyname name uni fun ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term tyname name uni fun ann
-> NormalizeTypeT m tyname uni ann (Term tyname name uni fun ann)
forall tyname (uni :: * -> *) (m :: * -> *) name fun ann.
(HasUnique tyname TypeUnique, MonadNormalizeType uni m) =>
Term tyname name uni fun ann
-> NormalizeTypeT m tyname uni ann (Term tyname name uni fun ann)
normalizeTypesInM

-- | Normalize every 'Type' in a 'Program'.
normalizeTypesInProgram
    :: (HasUnique tyname TypeUnique, HasUnique name TermUnique, MonadNormalizeType uni m)
    => Program tyname name uni fun ann -> m (Program tyname name uni fun ann)
normalizeTypesInProgram :: forall tyname name (uni :: * -> *) (m :: * -> *) fun ann.
(HasUnique tyname TypeUnique, HasUnique name TermUnique,
 MonadNormalizeType uni m) =>
Program tyname name uni fun ann
-> m (Program tyname name uni fun ann)
normalizeTypesInProgram (Program ann
x Version
v Term tyname name uni fun ann
t) = ann
-> Version
-> Term tyname name uni fun ann
-> Program tyname name uni fun ann
forall tyname name (uni :: * -> *) fun ann.
ann
-> Version
-> Term tyname name uni fun ann
-> Program tyname name uni fun ann
Program ann
x Version
v (Term tyname name uni fun ann -> Program tyname name uni fun ann)
-> m (Term tyname name uni fun ann)
-> m (Program tyname name uni fun ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Term tyname name uni fun ann -> m (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) (m :: * -> *) fun ann.
(HasUnique tyname TypeUnique, HasUnique name TermUnique,
 MonadNormalizeType uni m) =>
Term tyname name uni fun ann -> m (Term tyname name uni fun ann)
normalizeTypesIn Term tyname name uni fun ann
t