module PlutusIR.Check.Uniques
    ( checkProgram
    , checkTerm
    , UniqueError (..)
    , AsUniqueError (..)
    ) where

import PlutusCore.Error
import PlutusCore.Name.Unique
import PlutusIR.Analysis.Definitions
import PlutusIR.Core

import Control.Monad (when)
import Control.Monad.Error.Lens
import Control.Monad.Except (MonadError)

import Data.Foldable

checkProgram
    :: (Ord ann,
        HasUnique name TermUnique,
        HasUnique tyname TypeUnique,
        AsUniqueError e ann,
        MonadError e m)
    => (UniqueError ann -> Bool)
    -> Program tyname name uni fun ann
    -> m ()
checkProgram :: forall ann name tyname e (m :: * -> *) (uni :: * -> *) fun.
(Ord ann, HasUnique name TermUnique, HasUnique tyname TypeUnique,
 AsUniqueError e ann, MonadError e m) =>
(UniqueError ann -> Bool)
-> Program tyname name uni fun ann -> m ()
checkProgram UniqueError ann -> Bool
p (Program ann
_ Version
_ Term tyname name uni fun ann
t) = (UniqueError ann -> Bool) -> Term tyname name uni fun ann -> m ()
forall ann name tyname e (m :: * -> *) (uni :: * -> *) fun.
(Ord ann, HasUnique name TermUnique, HasUnique tyname TypeUnique,
 AsUniqueError e ann, MonadError e m) =>
(UniqueError ann -> Bool) -> Term tyname name uni fun ann -> m ()
checkTerm UniqueError ann -> Bool
p Term tyname name uni fun ann
t

checkTerm
    :: (Ord ann,
        HasUnique name TermUnique,
        HasUnique tyname TypeUnique,
        AsUniqueError e ann,
        MonadError e m)
    => (UniqueError ann -> Bool)
    -> Term tyname name uni fun ann
    -> m ()
checkTerm :: forall ann name tyname e (m :: * -> *) (uni :: * -> *) fun.
(Ord ann, HasUnique name TermUnique, HasUnique tyname TypeUnique,
 AsUniqueError e ann, MonadError e m) =>
(UniqueError ann -> Bool) -> Term tyname name uni fun ann -> m ()
checkTerm UniqueError ann -> Bool
p Term tyname name uni fun ann
t = do
    (UniqueInfos ann
_, [UniqueError ann]
errs) <- Term tyname name uni fun ann
-> m (UniqueInfos ann, [UniqueError ann])
forall ann name tyname (m :: * -> *) (uni :: * -> *) fun.
(Ord ann, HasUnique name TermUnique, HasUnique tyname TypeUnique,
 Monad m) =>
Term tyname name uni fun ann
-> m (UniqueInfos ann, [UniqueError ann])
runTermDefs Term tyname name uni fun ann
t
    [UniqueError ann] -> (UniqueError ann -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [UniqueError ann]
errs ((UniqueError ann -> m ()) -> m ())
-> (UniqueError ann -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \UniqueError ann
e -> Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (UniqueError ann -> Bool
p UniqueError ann
e) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ AReview e (UniqueError ann) -> UniqueError ann -> m ()
forall e (m :: * -> *) t x.
MonadError e m =>
AReview e t -> t -> m x
throwing AReview e (UniqueError ann)
forall r ann. AsUniqueError r ann => Prism' r (UniqueError ann)
Prism' e (UniqueError ann)
_UniqueError UniqueError ann
e