{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module PlutusIR.TypeCheck
(
BuiltinTypes (..)
, PirTCConfig (..)
, tccBuiltinTypes
, getDefTypeCheckConfig
, inferType
, checkType
, inferTypeOfProgram
, checkTypeOfProgram
) where
import PlutusCore (ToKind)
import PlutusCore.Quote
import PlutusCore.Rename
import PlutusCore.TypeCheck qualified as PLC
import PlutusIR
import PlutusIR.Error
import PlutusIR.Transform.Rename ()
import PlutusIR.TypeCheck.Internal
import Control.Monad.Except
import Data.Ix
import Universe
getDefTypeCheckConfig
:: forall uni fun m err ann.
( MonadError err m
, AsTypeError err (Term TyName Name uni fun ()) uni fun ann
, PLC.Typecheckable uni fun
)
=> ann -> m (PirTCConfig uni fun)
getDefTypeCheckConfig :: ann -> m (PirTCConfig uni fun)
getDefTypeCheckConfig ann
ann = do
TypeCheckConfig uni fun
configPlc <- ann -> m (TypeCheckConfig uni fun)
forall err (m :: * -> *) term (uni :: * -> *) fun ann.
(MonadError err m, AsTypeError err term uni fun ann,
Typecheckable uni fun) =>
ann -> m (TypeCheckConfig uni fun)
PLC.getDefTypeCheckConfig ann
ann
PirTCConfig uni fun -> m (PirTCConfig uni fun)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PirTCConfig uni fun -> m (PirTCConfig uni fun))
-> PirTCConfig uni fun -> m (PirTCConfig uni fun)
forall a b. (a -> b) -> a -> b
$ TypeCheckConfig uni fun -> AllowEscape -> PirTCConfig uni fun
forall (uni :: * -> *) fun.
TypeCheckConfig uni fun -> AllowEscape -> PirTCConfig uni fun
PirTCConfig TypeCheckConfig uni fun
configPlc AllowEscape
YesEscape
inferType
:: ( AsTypeError e (Term TyName Name uni fun ()) uni fun ann, ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann, MonadError e m, MonadQuote m
, GEq uni, Ix fun
)
=> PirTCConfig uni fun -> Term TyName Name uni fun ann -> m (Normalized (Type TyName uni ()))
inferType :: PirTCConfig uni fun
-> Term TyName Name uni fun ann
-> m (Normalized (Type TyName uni ()))
inferType PirTCConfig uni fun
config = Term TyName Name uni fun ann -> m (Term TyName Name uni fun ann)
forall a (m :: * -> *). (Rename a, MonadQuote m) => a -> m a
rename (Term TyName Name uni fun ann -> m (Term TyName Name uni fun ann))
-> (Term TyName Name uni fun ann
-> m (Normalized (Type TyName uni ())))
-> Term TyName Name uni fun ann
-> m (Normalized (Type TyName uni ()))
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> PirTCConfig uni fun
-> PirTCEnv uni fun e (Normalized (Type TyName uni ()))
-> m (Normalized (Type TyName uni ()))
forall e (m :: * -> *) (uni :: * -> *) fun a.
(MonadError e m, MonadQuote m) =>
PirTCConfig uni fun -> PirTCEnv uni fun e a -> m a
runTypeCheckM PirTCConfig uni fun
config (PirTCEnv uni fun e (Normalized (Type TyName uni ()))
-> m (Normalized (Type TyName uni ())))
-> (Term TyName Name uni fun ann
-> PirTCEnv uni fun e (Normalized (Type TyName uni ())))
-> Term TyName Name uni fun ann
-> m (Normalized (Type TyName uni ()))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term TyName Name uni fun ann
-> PirTCEnv uni fun e (Normalized (Type TyName uni ()))
forall (uni :: * -> *) fun ann e.
(GEq uni, Ix fun,
AsTypeError e (Term TyName Name uni fun ()) uni fun ann,
ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann) =>
Term TyName Name uni fun ann
-> PirTCEnv uni fun e (Normalized (Type TyName uni ()))
inferTypeM
checkType
:: ( AsTypeError e (Term TyName Name uni fun ()) uni fun ann, ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann, MonadError e m, MonadQuote m
, GEq uni, Ix fun
)
=> PirTCConfig uni fun
-> ann
-> Term TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> m ()
checkType :: PirTCConfig uni fun
-> ann
-> Term TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> m ()
checkType PirTCConfig uni fun
config ann
ann Term TyName Name uni fun ann
term Normalized (Type TyName uni ())
ty = do
Term TyName Name uni fun ann
termRen <- Term TyName Name uni fun ann -> m (Term TyName Name uni fun ann)
forall a (m :: * -> *). (Rename a, MonadQuote m) => a -> m a
rename Term TyName Name uni fun ann
term
PirTCConfig uni fun -> PirTCEnv uni fun e () -> m ()
forall e (m :: * -> *) (uni :: * -> *) fun a.
(MonadError e m, MonadQuote m) =>
PirTCConfig uni fun -> PirTCEnv uni fun e a -> m a
runTypeCheckM PirTCConfig uni fun
config (PirTCEnv uni fun e () -> m ()) -> PirTCEnv uni fun e () -> m ()
forall a b. (a -> b) -> a -> b
$ ann
-> Term TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> PirTCEnv uni fun e ()
forall (uni :: * -> *) fun e ann.
(GEq uni, Ix fun, AsTypeErrorExt e uni ann,
AsTypeError e (Term TyName Name uni fun ()) uni fun ann,
ToKind uni, HasUniApply uni) =>
ann
-> Term TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> PirTCEnv uni fun e ()
checkTypeM ann
ann Term TyName Name uni fun ann
termRen Normalized (Type TyName uni ())
ty
inferTypeOfProgram
:: ( AsTypeError e (Term TyName Name uni fun ()) uni fun ann, ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann, MonadError e m, MonadQuote m
, GEq uni, Ix fun
)
=> PirTCConfig uni fun -> Program TyName Name uni fun ann -> m (Normalized (Type TyName uni ()))
inferTypeOfProgram :: PirTCConfig uni fun
-> Program TyName Name uni fun ann
-> m (Normalized (Type TyName uni ()))
inferTypeOfProgram PirTCConfig uni fun
config (Program ann
_ Term TyName Name uni fun ann
term) = PirTCConfig uni fun
-> Term TyName Name uni fun ann
-> m (Normalized (Type TyName uni ()))
forall e (uni :: * -> *) fun ann (m :: * -> *).
(AsTypeError e (Term TyName Name uni fun ()) uni fun ann,
ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann,
MonadError e m, MonadQuote m, GEq uni, Ix fun) =>
PirTCConfig uni fun
-> Term TyName Name uni fun ann
-> m (Normalized (Type TyName uni ()))
inferType PirTCConfig uni fun
config Term TyName Name uni fun ann
term
checkTypeOfProgram
:: ( AsTypeError e (Term TyName Name uni fun ()) uni fun ann, ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann,
MonadError e m, MonadQuote m
, GEq uni, Ix fun
)
=> PirTCConfig uni fun
-> ann
-> Program TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> m ()
checkTypeOfProgram :: PirTCConfig uni fun
-> ann
-> Program TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> m ()
checkTypeOfProgram PirTCConfig uni fun
config ann
ann (Program ann
_ Term TyName Name uni fun ann
term) = PirTCConfig uni fun
-> ann
-> Term TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> m ()
forall e (uni :: * -> *) fun ann (m :: * -> *).
(AsTypeError e (Term TyName Name uni fun ()) uni fun ann,
ToKind uni, HasUniApply uni, AsTypeErrorExt e uni ann,
MonadError e m, MonadQuote m, GEq uni, Ix fun) =>
PirTCConfig uni fun
-> ann
-> Term TyName Name uni fun ann
-> Normalized (Type TyName uni ())
-> m ()
checkType PirTCConfig uni fun
config ann
ann Term TyName Name uni fun ann
term