{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module GHC.TcPlugin.API.Internal
(
MonadTcPlugin(..), MonadTcPluginWork
, unsafeLiftThroughTcM
, TcPlugin(..), TcPluginStage(..)
, TcPluginSolver
, TcPluginM(..)
, TcPluginErrorMessage(..)
, TcPluginRewriter
, MonadThings(..)
, askRewriteEnv
, askDeriveds
, askEvBinds
, mkTcPlugin
, mkTcPluginErrorTy
)
where
import Data.Coerce
( Coercible )
import Data.Kind
( Type )
import GHC.TypeLits
( TypeError, ErrorMessage(..) )
import Control.Monad.Trans.Reader
( ReaderT(..) )
import qualified GHC.Builtin.Names
as GHC.TypeLits
( errorMessageTypeErrorFamName
, typeErrorTextDataConName
, typeErrorAppendDataConName
, typeErrorVAppendDataConName
, typeErrorShowTypeDataConName
)
import qualified GHC.Builtin.Types
as GHC
( constraintKind )
import qualified GHC.Core.DataCon
as GHC
( promoteDataCon )
import qualified GHC.Core.TyCon
as GHC
( TyCon )
import qualified GHC.Core.TyCo.Rep
as GHC
( PredType, Type(..), TyLit(..) )
import qualified GHC.Core.Type
as GHC
( mkTyConApp, typeKind )
import qualified GHC.Data.FastString
as GHC
( fsLit )
import qualified GHC.Tc.Plugin
as GHC
( tcLookupDataCon, tcLookupTyCon )
import qualified GHC.Tc.Types
as GHC
( TcM, TcPlugin(..), TcPluginM
, TcPluginSolver
#ifdef HAS_REWRITING
, TcPluginRewriter
#else
, getEvBindsTcPluginM
#endif
, runTcPluginM, unsafeTcPluginTcM
)
#ifdef HAS_REWRITING
import GHC.Tc.Types
( TcPluginSolveResult
, TcPluginRewriteResult
, RewriteEnv
)
#endif
import qualified GHC.Tc.Types.Constraint
as GHC
( Ct )
import qualified GHC.Tc.Types.Evidence
as GHC
( EvBindsVar )
import qualified GHC.Types.Unique.FM
as GHC
( UniqFM )
#if MIN_VERSION_ghc(9,1,0)
import GHC.Types.TyThing
( MonadThings(..) )
#else
import GHC.Driver.Types
( MonadThings(..) )
#endif
#ifndef HAS_REWRITING
import GHC.TcPlugin.API.Internal.Shim
( TcPluginSolveResult, TcPluginRewriteResult(..)
, RewriteEnv
, shimRewriter
)
#endif
data TcPluginStage
= Init
| Solve
| Rewrite
| Stop
type TcPluginSolver
= [GHC.Ct]
-> [GHC.Ct]
-> TcPluginM Solve TcPluginSolveResult
type TcPluginRewriter
= [GHC.Ct]
-> [GHC.Type]
-> TcPluginM Rewrite TcPluginRewriteResult
data TcPlugin = forall s. TcPlugin
{ ()
tcPluginInit :: TcPluginM Init s
, ()
tcPluginSolve :: s -> TcPluginSolver
, ()
tcPluginRewrite
:: s -> GHC.UniqFM
#if MIN_VERSION_ghc(9,0,0)
GHC.TyCon
#endif
TcPluginRewriter
, ()
tcPluginStop :: s -> TcPluginM Stop ()
}
data family TcPluginM (s :: TcPluginStage) :: Type -> Type
newtype instance TcPluginM Init a =
TcPluginInitM { forall a. TcPluginM 'Init a -> TcPluginM a
tcPluginInitM :: GHC.TcPluginM a }
deriving newtype ( (forall a b. (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b)
-> (forall a b. a -> TcPluginM 'Init b -> TcPluginM 'Init a)
-> Functor (TcPluginM 'Init)
forall a b. a -> TcPluginM 'Init b -> TcPluginM 'Init a
forall a b. (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b
fmap :: forall a b. (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b
$c<$ :: forall a b. a -> TcPluginM 'Init b -> TcPluginM 'Init a
<$ :: forall a b. a -> TcPluginM 'Init b -> TcPluginM 'Init a
Functor, Functor (TcPluginM 'Init)
Functor (TcPluginM 'Init) =>
(forall a. a -> TcPluginM 'Init a)
-> (forall a b.
TcPluginM 'Init (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b)
-> (forall a b c.
(a -> b -> c)
-> TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init c)
-> (forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b)
-> (forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init a)
-> Applicative (TcPluginM 'Init)
forall a. a -> TcPluginM 'Init a
forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init a
forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b
forall a b.
TcPluginM 'Init (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b
forall a b c.
(a -> b -> c)
-> TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> TcPluginM 'Init a
pure :: forall a. a -> TcPluginM 'Init a
$c<*> :: forall a b.
TcPluginM 'Init (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b
<*> :: forall a b.
TcPluginM 'Init (a -> b) -> TcPluginM 'Init a -> TcPluginM 'Init b
$cliftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init c
liftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init c
$c*> :: forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b
*> :: forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b
$c<* :: forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init a
<* :: forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init a
Applicative, Applicative (TcPluginM 'Init)
Applicative (TcPluginM 'Init) =>
(forall a b.
TcPluginM 'Init a -> (a -> TcPluginM 'Init b) -> TcPluginM 'Init b)
-> (forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b)
-> (forall a. a -> TcPluginM 'Init a)
-> Monad (TcPluginM 'Init)
forall a. a -> TcPluginM 'Init a
forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b
forall a b.
TcPluginM 'Init a -> (a -> TcPluginM 'Init b) -> TcPluginM 'Init b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b.
TcPluginM 'Init a -> (a -> TcPluginM 'Init b) -> TcPluginM 'Init b
>>= :: forall a b.
TcPluginM 'Init a -> (a -> TcPluginM 'Init b) -> TcPluginM 'Init b
$c>> :: forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b
>> :: forall a b.
TcPluginM 'Init a -> TcPluginM 'Init b -> TcPluginM 'Init b
$creturn :: forall a. a -> TcPluginM 'Init a
return :: forall a. a -> TcPluginM 'Init a
Monad )
#ifdef HAS_DERIVEDS
newtype instance TcPluginM Solve a =
TcPluginSolveM { tcPluginSolveM :: BuiltinDefs -> GHC.EvBindsVar -> [GHC.Ct] -> GHC.TcPluginM a }
deriving ( Functor, Applicative, Monad )
via ( ReaderT BuiltinDefs ( ReaderT GHC.EvBindsVar ( ReaderT [GHC.Ct] GHC.TcPluginM ) ) )
#else
newtype instance TcPluginM Solve a =
TcPluginSolveM { forall a.
TcPluginM 'Solve a -> BuiltinDefs -> EvBindsVar -> TcPluginM a
tcPluginSolveM :: BuiltinDefs -> GHC.EvBindsVar -> GHC.TcPluginM a }
deriving ( (forall a b. (a -> b) -> TcPluginM 'Solve a -> TcPluginM 'Solve b)
-> (forall a b. a -> TcPluginM 'Solve b -> TcPluginM 'Solve a)
-> Functor (TcPluginM 'Solve)
forall a b. a -> TcPluginM 'Solve b -> TcPluginM 'Solve a
forall a b. (a -> b) -> TcPluginM 'Solve a -> TcPluginM 'Solve b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> TcPluginM 'Solve a -> TcPluginM 'Solve b
fmap :: forall a b. (a -> b) -> TcPluginM 'Solve a -> TcPluginM 'Solve b
$c<$ :: forall a b. a -> TcPluginM 'Solve b -> TcPluginM 'Solve a
<$ :: forall a b. a -> TcPluginM 'Solve b -> TcPluginM 'Solve a
Functor, Functor (TcPluginM 'Solve)
Functor (TcPluginM 'Solve) =>
(forall a. a -> TcPluginM 'Solve a)
-> (forall a b.
TcPluginM 'Solve (a -> b)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b)
-> (forall a b c.
(a -> b -> c)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve c)
-> (forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b)
-> (forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve a)
-> Applicative (TcPluginM 'Solve)
forall a. a -> TcPluginM 'Solve a
forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve a
forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b
forall a b.
TcPluginM 'Solve (a -> b)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b
forall a b c.
(a -> b -> c)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> TcPluginM 'Solve a
pure :: forall a. a -> TcPluginM 'Solve a
$c<*> :: forall a b.
TcPluginM 'Solve (a -> b)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b
<*> :: forall a b.
TcPluginM 'Solve (a -> b)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b
$cliftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve c
liftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve c
$c*> :: forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b
*> :: forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b
$c<* :: forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve a
<* :: forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve a
Applicative, Applicative (TcPluginM 'Solve)
Applicative (TcPluginM 'Solve) =>
(forall a b.
TcPluginM 'Solve a
-> (a -> TcPluginM 'Solve b) -> TcPluginM 'Solve b)
-> (forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b)
-> (forall a. a -> TcPluginM 'Solve a)
-> Monad (TcPluginM 'Solve)
forall a. a -> TcPluginM 'Solve a
forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b
forall a b.
TcPluginM 'Solve a
-> (a -> TcPluginM 'Solve b) -> TcPluginM 'Solve b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b.
TcPluginM 'Solve a
-> (a -> TcPluginM 'Solve b) -> TcPluginM 'Solve b
>>= :: forall a b.
TcPluginM 'Solve a
-> (a -> TcPluginM 'Solve b) -> TcPluginM 'Solve b
$c>> :: forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b
>> :: forall a b.
TcPluginM 'Solve a -> TcPluginM 'Solve b -> TcPluginM 'Solve b
$creturn :: forall a. a -> TcPluginM 'Solve a
return :: forall a. a -> TcPluginM 'Solve a
Monad )
via ( ReaderT BuiltinDefs ( ReaderT GHC.EvBindsVar GHC.TcPluginM ) )
#endif
newtype instance TcPluginM Rewrite a =
TcPluginRewriteM { forall a.
TcPluginM 'Rewrite a -> BuiltinDefs -> RewriteEnv -> TcPluginM a
tcPluginRewriteM :: BuiltinDefs -> RewriteEnv -> GHC.TcPluginM a }
deriving ( (forall a b.
(a -> b) -> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b)
-> (forall a b. a -> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a)
-> Functor (TcPluginM 'Rewrite)
forall a b. a -> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a
forall a b.
(a -> b) -> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b.
(a -> b) -> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b
fmap :: forall a b.
(a -> b) -> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b
$c<$ :: forall a b. a -> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a
<$ :: forall a b. a -> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a
Functor, Functor (TcPluginM 'Rewrite)
Functor (TcPluginM 'Rewrite) =>
(forall a. a -> TcPluginM 'Rewrite a)
-> (forall a b.
TcPluginM 'Rewrite (a -> b)
-> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b)
-> (forall a b c.
(a -> b -> c)
-> TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b
-> TcPluginM 'Rewrite c)
-> (forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b)
-> (forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a)
-> Applicative (TcPluginM 'Rewrite)
forall a. a -> TcPluginM 'Rewrite a
forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a
forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b
forall a b.
TcPluginM 'Rewrite (a -> b)
-> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b
forall a b c.
(a -> b -> c)
-> TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b
-> TcPluginM 'Rewrite c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> TcPluginM 'Rewrite a
pure :: forall a. a -> TcPluginM 'Rewrite a
$c<*> :: forall a b.
TcPluginM 'Rewrite (a -> b)
-> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b
<*> :: forall a b.
TcPluginM 'Rewrite (a -> b)
-> TcPluginM 'Rewrite a -> TcPluginM 'Rewrite b
$cliftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b
-> TcPluginM 'Rewrite c
liftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b
-> TcPluginM 'Rewrite c
$c*> :: forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b
*> :: forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b
$c<* :: forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a
<* :: forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite a
Applicative, Applicative (TcPluginM 'Rewrite)
Applicative (TcPluginM 'Rewrite) =>
(forall a b.
TcPluginM 'Rewrite a
-> (a -> TcPluginM 'Rewrite b) -> TcPluginM 'Rewrite b)
-> (forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b)
-> (forall a. a -> TcPluginM 'Rewrite a)
-> Monad (TcPluginM 'Rewrite)
forall a. a -> TcPluginM 'Rewrite a
forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b
forall a b.
TcPluginM 'Rewrite a
-> (a -> TcPluginM 'Rewrite b) -> TcPluginM 'Rewrite b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b.
TcPluginM 'Rewrite a
-> (a -> TcPluginM 'Rewrite b) -> TcPluginM 'Rewrite b
>>= :: forall a b.
TcPluginM 'Rewrite a
-> (a -> TcPluginM 'Rewrite b) -> TcPluginM 'Rewrite b
$c>> :: forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b
>> :: forall a b.
TcPluginM 'Rewrite a
-> TcPluginM 'Rewrite b -> TcPluginM 'Rewrite b
$creturn :: forall a. a -> TcPluginM 'Rewrite a
return :: forall a. a -> TcPluginM 'Rewrite a
Monad )
via ( ReaderT BuiltinDefs ( ReaderT RewriteEnv GHC.TcPluginM ) )
newtype instance TcPluginM Stop a =
TcPluginStopM { forall a. TcPluginM 'Stop a -> TcPluginM a
tcPluginStopM :: GHC.TcPluginM a }
deriving newtype ( (forall a b. (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b)
-> (forall a b. a -> TcPluginM 'Stop b -> TcPluginM 'Stop a)
-> Functor (TcPluginM 'Stop)
forall a b. a -> TcPluginM 'Stop b -> TcPluginM 'Stop a
forall a b. (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b
fmap :: forall a b. (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b
$c<$ :: forall a b. a -> TcPluginM 'Stop b -> TcPluginM 'Stop a
<$ :: forall a b. a -> TcPluginM 'Stop b -> TcPluginM 'Stop a
Functor, Functor (TcPluginM 'Stop)
Functor (TcPluginM 'Stop) =>
(forall a. a -> TcPluginM 'Stop a)
-> (forall a b.
TcPluginM 'Stop (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b)
-> (forall a b c.
(a -> b -> c)
-> TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop c)
-> (forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b)
-> (forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop a)
-> Applicative (TcPluginM 'Stop)
forall a. a -> TcPluginM 'Stop a
forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop a
forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b
forall a b.
TcPluginM 'Stop (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b
forall a b c.
(a -> b -> c)
-> TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> TcPluginM 'Stop a
pure :: forall a. a -> TcPluginM 'Stop a
$c<*> :: forall a b.
TcPluginM 'Stop (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b
<*> :: forall a b.
TcPluginM 'Stop (a -> b) -> TcPluginM 'Stop a -> TcPluginM 'Stop b
$cliftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop c
liftA2 :: forall a b c.
(a -> b -> c)
-> TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop c
$c*> :: forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b
*> :: forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b
$c<* :: forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop a
<* :: forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop a
Applicative, Applicative (TcPluginM 'Stop)
Applicative (TcPluginM 'Stop) =>
(forall a b.
TcPluginM 'Stop a -> (a -> TcPluginM 'Stop b) -> TcPluginM 'Stop b)
-> (forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b)
-> (forall a. a -> TcPluginM 'Stop a)
-> Monad (TcPluginM 'Stop)
forall a. a -> TcPluginM 'Stop a
forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b
forall a b.
TcPluginM 'Stop a -> (a -> TcPluginM 'Stop b) -> TcPluginM 'Stop b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b.
TcPluginM 'Stop a -> (a -> TcPluginM 'Stop b) -> TcPluginM 'Stop b
>>= :: forall a b.
TcPluginM 'Stop a -> (a -> TcPluginM 'Stop b) -> TcPluginM 'Stop b
$c>> :: forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b
>> :: forall a b.
TcPluginM 'Stop a -> TcPluginM 'Stop b -> TcPluginM 'Stop b
$creturn :: forall a. a -> TcPluginM 'Stop a
return :: forall a. a -> TcPluginM 'Stop a
Monad )
askEvBinds :: TcPluginM Solve GHC.EvBindsVar
askEvBinds :: TcPluginM 'Solve EvBindsVar
askEvBinds = (BuiltinDefs -> EvBindsVar -> TcPluginM EvBindsVar)
-> TcPluginM 'Solve EvBindsVar
forall a.
(BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM 'Solve a
TcPluginSolveM
\ BuiltinDefs
_defs
EvBindsVar
evBinds
#ifdef HAS_DERIVEDS
_deriveds
#endif
-> EvBindsVar -> TcPluginM EvBindsVar
forall a. a -> TcPluginM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure EvBindsVar
evBinds
askDeriveds :: TcPluginM Solve [GHC.Ct]
askDeriveds :: TcPluginM 'Solve [Ct]
askDeriveds =
#ifdef HAS_DERIVEDS
TcPluginSolveM \ _defs _evBinds deriveds -> pure deriveds
#else
[Ct] -> TcPluginM 'Solve [Ct]
forall a. a -> TcPluginM 'Solve a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
#endif
askRewriteEnv :: TcPluginM Rewrite RewriteEnv
askRewriteEnv :: TcPluginM 'Rewrite RewriteEnv
askRewriteEnv = (BuiltinDefs -> RewriteEnv -> TcPluginM RewriteEnv)
-> TcPluginM 'Rewrite RewriteEnv
forall a.
(BuiltinDefs -> RewriteEnv -> TcPluginM a) -> TcPluginM 'Rewrite a
TcPluginRewriteM ( \ BuiltinDefs
_ RewriteEnv
rewriteEnv -> RewriteEnv -> TcPluginM RewriteEnv
forall a. a -> TcPluginM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RewriteEnv
rewriteEnv )
class ( Monad m, ( forall x y. Coercible x y => Coercible (m x) (m y) ) ) => MonadTcPlugin (m :: Type -> Type) where
{-# MINIMAL liftTcPluginM, unsafeWithRunInTcM #-}
liftTcPluginM :: GHC.TcPluginM a -> m a
unsafeLiftTcM :: GHC.TcM a -> m a
unsafeLiftTcM = TcPluginM a -> m a
forall a. TcPluginM a -> m a
forall (m :: * -> *) a. MonadTcPlugin m => TcPluginM a -> m a
liftTcPluginM (TcPluginM a -> m a) -> (TcM a -> TcPluginM a) -> TcM a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcM a -> TcPluginM a
forall a. TcM a -> TcPluginM a
GHC.unsafeTcPluginTcM
unsafeWithRunInTcM :: ( ( forall a. m a -> GHC.TcM a ) -> GHC.TcM b ) -> m b
instance MonadTcPlugin ( TcPluginM Init ) where
liftTcPluginM :: forall a. TcPluginM a -> TcPluginM 'Init a
liftTcPluginM = TcPluginM a -> TcPluginM 'Init a
forall a. TcPluginM a -> TcPluginM 'Init a
TcPluginInitM
unsafeWithRunInTcM :: forall b.
((forall a. TcPluginM 'Init a -> TcM a) -> TcM b)
-> TcPluginM 'Init b
unsafeWithRunInTcM (forall a. TcPluginM 'Init a -> TcM a) -> TcM b
runInTcM
= TcM b -> TcPluginM 'Init b
forall a. TcM a -> TcPluginM 'Init a
forall (m :: * -> *) a. MonadTcPlugin m => TcM a -> m a
unsafeLiftTcM (TcM b -> TcPluginM 'Init b) -> TcM b -> TcPluginM 'Init b
forall a b. (a -> b) -> a -> b
$ (forall a. TcPluginM 'Init a -> TcM a) -> TcM b
runInTcM
#ifdef HAS_REWRITING
( TcPluginM a -> TcM a
forall a. TcPluginM a -> TcM a
GHC.runTcPluginM (TcPluginM a -> TcM a)
-> (TcPluginM 'Init a -> TcPluginM a) -> TcPluginM 'Init a -> TcM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcPluginM 'Init a -> TcPluginM a
forall a. TcPluginM 'Init a -> TcPluginM a
tcPluginInitM )
#else
( ( `GHC.runTcPluginM` ( error "tcPluginInit: cannot access EvBindsVar" ) ) . tcPluginInitM )
#endif
instance MonadTcPlugin ( TcPluginM Solve ) where
liftTcPluginM :: forall a. TcPluginM a -> TcPluginM 'Solve a
liftTcPluginM = (BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM 'Solve a
forall a.
(BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM 'Solve a
TcPluginSolveM
#ifdef HAS_DERIVEDS
. ( \ ma _defs _evBinds _deriveds -> ma )
#else
((BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM 'Solve a)
-> (TcPluginM a -> BuiltinDefs -> EvBindsVar -> TcPluginM a)
-> TcPluginM a
-> TcPluginM 'Solve a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ( \ TcPluginM a
ma BuiltinDefs
_defs EvBindsVar
_evBinds -> TcPluginM a
ma )
#endif
unsafeWithRunInTcM :: forall b.
((forall a. TcPluginM 'Solve a -> TcM a) -> TcM b)
-> TcPluginM 'Solve b
unsafeWithRunInTcM (forall a. TcPluginM 'Solve a -> TcM a) -> TcM b
runInTcM
= (BuiltinDefs -> EvBindsVar -> TcPluginM b) -> TcPluginM 'Solve b
forall a.
(BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM 'Solve a
TcPluginSolveM
\ BuiltinDefs
builtinDefs
EvBindsVar
evBinds
#ifdef HAS_DERIVEDS
deriveds
#endif
->
TcM b -> TcPluginM b
forall a. TcM a -> TcPluginM a
GHC.unsafeTcPluginTcM (TcM b -> TcPluginM b) -> TcM b -> TcPluginM b
forall a b. (a -> b) -> a -> b
$ (forall a. TcPluginM 'Solve a -> TcM a) -> TcM b
runInTcM
#ifdef HAS_REWRITING
( TcPluginM a -> TcM a
forall a. TcPluginM a -> TcM a
GHC.runTcPluginM
(TcPluginM a -> TcM a)
-> (TcPluginM 'Solve a -> TcPluginM a)
-> TcPluginM 'Solve a
-> TcM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ( \ BuiltinDefs -> EvBindsVar -> TcPluginM a
f -> BuiltinDefs -> EvBindsVar -> TcPluginM a
f BuiltinDefs
builtinDefs EvBindsVar
evBinds )
((BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM a)
-> (TcPluginM 'Solve a -> BuiltinDefs -> EvBindsVar -> TcPluginM a)
-> TcPluginM 'Solve a
-> TcPluginM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcPluginM 'Solve a -> BuiltinDefs -> EvBindsVar -> TcPluginM a
forall a.
TcPluginM 'Solve a -> BuiltinDefs -> EvBindsVar -> TcPluginM a
tcPluginSolveM )
#else
( ( `GHC.runTcPluginM` evBinds )
. ( \ f -> f builtinDefs evBinds deriveds )
. tcPluginSolveM
)
#endif
instance MonadTcPlugin ( TcPluginM Rewrite ) where
liftTcPluginM :: forall a. TcPluginM a -> TcPluginM 'Rewrite a
liftTcPluginM = (BuiltinDefs -> RewriteEnv -> TcPluginM a) -> TcPluginM 'Rewrite a
forall a.
(BuiltinDefs -> RewriteEnv -> TcPluginM a) -> TcPluginM 'Rewrite a
TcPluginRewriteM ((BuiltinDefs -> RewriteEnv -> TcPluginM a)
-> TcPluginM 'Rewrite a)
-> (TcPluginM a -> BuiltinDefs -> RewriteEnv -> TcPluginM a)
-> TcPluginM a
-> TcPluginM 'Rewrite a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ( \ TcPluginM a
ma BuiltinDefs
_ RewriteEnv
_ -> TcPluginM a
ma )
unsafeWithRunInTcM :: forall b.
((forall a. TcPluginM 'Rewrite a -> TcM a) -> TcM b)
-> TcPluginM 'Rewrite b
unsafeWithRunInTcM (forall a. TcPluginM 'Rewrite a -> TcM a) -> TcM b
runInTcM
= (BuiltinDefs -> RewriteEnv -> TcPluginM b) -> TcPluginM 'Rewrite b
forall a.
(BuiltinDefs -> RewriteEnv -> TcPluginM a) -> TcPluginM 'Rewrite a
TcPluginRewriteM \ BuiltinDefs
builtinDefs RewriteEnv
rewriteEnv ->
TcM b -> TcPluginM b
forall a. TcM a -> TcPluginM a
GHC.unsafeTcPluginTcM (TcM b -> TcPluginM b) -> TcM b -> TcPluginM b
forall a b. (a -> b) -> a -> b
$ (forall a. TcPluginM 'Rewrite a -> TcM a) -> TcM b
runInTcM
#ifdef HAS_REWRITING
( TcPluginM a -> TcM a
forall a. TcPluginM a -> TcM a
GHC.runTcPluginM
#else
( ( `GHC.runTcPluginM` ( error "tcPluginRewrite: cannot access EvBindsVar" ) )
#endif
(TcPluginM a -> TcM a)
-> (TcPluginM 'Rewrite a -> TcPluginM a)
-> TcPluginM 'Rewrite a
-> TcM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ( \ BuiltinDefs -> RewriteEnv -> TcPluginM a
f -> BuiltinDefs -> RewriteEnv -> TcPluginM a
f BuiltinDefs
builtinDefs RewriteEnv
rewriteEnv )
((BuiltinDefs -> RewriteEnv -> TcPluginM a) -> TcPluginM a)
-> (TcPluginM 'Rewrite a
-> BuiltinDefs -> RewriteEnv -> TcPluginM a)
-> TcPluginM 'Rewrite a
-> TcPluginM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcPluginM 'Rewrite a -> BuiltinDefs -> RewriteEnv -> TcPluginM a
forall a.
TcPluginM 'Rewrite a -> BuiltinDefs -> RewriteEnv -> TcPluginM a
tcPluginRewriteM )
instance MonadTcPlugin ( TcPluginM Stop ) where
liftTcPluginM :: forall a. TcPluginM a -> TcPluginM 'Stop a
liftTcPluginM = TcPluginM a -> TcPluginM 'Stop a
forall a. TcPluginM a -> TcPluginM 'Stop a
TcPluginStopM
unsafeWithRunInTcM :: forall b.
((forall a. TcPluginM 'Stop a -> TcM a) -> TcM b)
-> TcPluginM 'Stop b
unsafeWithRunInTcM (forall a. TcPluginM 'Stop a -> TcM a) -> TcM b
runInTcM
= TcM b -> TcPluginM 'Stop b
forall a. TcM a -> TcPluginM 'Stop a
forall (m :: * -> *) a. MonadTcPlugin m => TcM a -> m a
unsafeLiftTcM (TcM b -> TcPluginM 'Stop b) -> TcM b -> TcPluginM 'Stop b
forall a b. (a -> b) -> a -> b
$ (forall a. TcPluginM 'Stop a -> TcM a) -> TcM b
runInTcM
#ifdef HAS_REWRITING
( TcPluginM a -> TcM a
forall a. TcPluginM a -> TcM a
GHC.runTcPluginM (TcPluginM a -> TcM a)
-> (TcPluginM 'Stop a -> TcPluginM a) -> TcPluginM 'Stop a -> TcM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcPluginM 'Stop a -> TcPluginM a
forall a. TcPluginM 'Stop a -> TcPluginM a
tcPluginStopM )
#else
( ( `GHC.runTcPluginM` ( error "tcPluginStop: cannot access EvBindsVar" ) ) . tcPluginStopM )
#endif
unsafeLiftThroughTcM :: MonadTcPlugin m => ( GHC.TcM a -> GHC.TcM b ) -> m a -> m b
unsafeLiftThroughTcM :: forall (m :: * -> *) a b.
MonadTcPlugin m =>
(TcM a -> TcM b) -> m a -> m b
unsafeLiftThroughTcM TcM a -> TcM b
f m a
ma = ((forall a. m a -> TcM a) -> TcM b) -> m b
forall b. ((forall a. m a -> TcM a) -> TcM b) -> m b
forall (m :: * -> *) b.
MonadTcPlugin m =>
((forall a. m a -> TcM a) -> TcM b) -> m b
unsafeWithRunInTcM \ forall a. m a -> TcM a
runInTcM -> TcM a -> TcM b
f ( m a -> TcM a
forall a. m a -> TcM a
runInTcM m a
ma )
mkTcPlugin
:: TcPlugin
-> GHC.TcPlugin
mkTcPlugin :: TcPlugin -> TcPlugin
mkTcPlugin ( TcPlugin
{ tcPluginInit :: ()
tcPluginInit = TcPluginM 'Init s
tcPluginInit :: TcPluginM Init userDefs
, s -> TcPluginSolver
tcPluginSolve :: ()
tcPluginSolve :: s -> TcPluginSolver
tcPluginSolve
, s -> UniqFM TyCon TcPluginRewriter
tcPluginRewrite :: ()
tcPluginRewrite :: s -> UniqFM TyCon TcPluginRewriter
tcPluginRewrite
, s -> TcPluginM 'Stop ()
tcPluginStop :: ()
tcPluginStop :: s -> TcPluginM 'Stop ()
tcPluginStop
}
) =
GHC.TcPlugin
{ tcPluginInit :: TcPluginM (TcPluginDefs s)
GHC.tcPluginInit = TcPluginM 'Init s -> TcPluginM (TcPluginDefs s)
adaptUserInit TcPluginM 'Init s
tcPluginInit
#ifdef HAS_REWRITING
, tcPluginSolve :: TcPluginDefs s -> TcPluginSolver
GHC.tcPluginSolve = (s -> TcPluginSolver) -> TcPluginDefs s -> TcPluginSolver
adaptUserSolve s -> TcPluginSolver
tcPluginSolve
, tcPluginRewrite :: TcPluginDefs s -> UniqFM TyCon TcPluginRewriter
GHC.tcPluginRewrite = (s -> UniqFM TyCon TcPluginRewriter)
-> TcPluginDefs s -> UniqFM TyCon TcPluginRewriter
adaptUserRewrite s -> UniqFM TyCon TcPluginRewriter
tcPluginRewrite
#else
, GHC.tcPluginSolve = adaptUserSolveAndRewrite
tcPluginSolve tcPluginRewrite
#endif
, tcPluginStop :: TcPluginDefs s -> TcPluginM ()
GHC.tcPluginStop = (s -> TcPluginM 'Stop ()) -> TcPluginDefs s -> TcPluginM ()
adaptUserStop s -> TcPluginM 'Stop ()
tcPluginStop
}
where
adaptUserInit :: TcPluginM Init userDefs -> GHC.TcPluginM ( TcPluginDefs userDefs )
adaptUserInit :: TcPluginM 'Init s -> TcPluginM (TcPluginDefs s)
adaptUserInit TcPluginM 'Init s
userInit = do
BuiltinDefs
tcPluginBuiltinDefs <- TcPluginM BuiltinDefs
initBuiltinDefs
s
tcPluginUserDefs <- TcPluginM 'Init s -> TcPluginM s
forall a. TcPluginM 'Init a -> TcPluginM a
tcPluginInitM TcPluginM 'Init s
userInit
TcPluginDefs s -> TcPluginM (TcPluginDefs s)
forall a. a -> TcPluginM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( TcPluginDefs { BuiltinDefs
tcPluginBuiltinDefs :: BuiltinDefs
tcPluginBuiltinDefs :: BuiltinDefs
tcPluginBuiltinDefs, s
tcPluginUserDefs :: s
tcPluginUserDefs :: s
tcPluginUserDefs })
#ifdef HAS_REWRITING
adaptUserSolve :: ( userDefs -> TcPluginSolver )
-> TcPluginDefs userDefs
-> GHC.TcPluginSolver
adaptUserSolve :: (s -> TcPluginSolver) -> TcPluginDefs s -> TcPluginSolver
adaptUserSolve s -> TcPluginSolver
userSolve ( TcPluginDefs { s
tcPluginUserDefs :: forall s. TcPluginDefs s -> s
tcPluginUserDefs :: s
tcPluginUserDefs, BuiltinDefs
tcPluginBuiltinDefs :: forall s. TcPluginDefs s -> BuiltinDefs
tcPluginBuiltinDefs :: BuiltinDefs
tcPluginBuiltinDefs } )
= \ EvBindsVar
evBindsVar [Ct]
givens [Ct]
wanteds -> do
TcPluginM 'Solve TcPluginSolveResult
-> BuiltinDefs -> EvBindsVar -> TcPluginM TcPluginSolveResult
forall a.
TcPluginM 'Solve a -> BuiltinDefs -> EvBindsVar -> TcPluginM a
tcPluginSolveM ( s -> TcPluginSolver
userSolve s
tcPluginUserDefs [Ct]
givens [Ct]
wanteds )
BuiltinDefs
tcPluginBuiltinDefs EvBindsVar
evBindsVar
adaptUserRewrite :: ( userDefs -> GHC.UniqFM GHC.TyCon TcPluginRewriter )
-> TcPluginDefs userDefs -> GHC.UniqFM GHC.TyCon GHC.TcPluginRewriter
adaptUserRewrite :: (s -> UniqFM TyCon TcPluginRewriter)
-> TcPluginDefs s -> UniqFM TyCon TcPluginRewriter
adaptUserRewrite s -> UniqFM TyCon TcPluginRewriter
userRewrite ( TcPluginDefs { s
tcPluginUserDefs :: forall s. TcPluginDefs s -> s
tcPluginUserDefs :: s
tcPluginUserDefs, BuiltinDefs
tcPluginBuiltinDefs :: forall s. TcPluginDefs s -> BuiltinDefs
tcPluginBuiltinDefs :: BuiltinDefs
tcPluginBuiltinDefs })
= (TcPluginRewriter -> TcPluginRewriter)
-> UniqFM TyCon TcPluginRewriter -> UniqFM TyCon TcPluginRewriter
forall a b. (a -> b) -> UniqFM TyCon a -> UniqFM TyCon b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
( \ TcPluginRewriter
userRewriter RewriteEnv
rewriteEnv [Ct]
givens [TcType]
tys ->
TcPluginM 'Rewrite TcPluginRewriteResult
-> BuiltinDefs -> RewriteEnv -> TcPluginM TcPluginRewriteResult
forall a.
TcPluginM 'Rewrite a -> BuiltinDefs -> RewriteEnv -> TcPluginM a
tcPluginRewriteM ( TcPluginRewriter
userRewriter [Ct]
givens [TcType]
tys ) BuiltinDefs
tcPluginBuiltinDefs RewriteEnv
rewriteEnv
)
( s -> UniqFM TyCon TcPluginRewriter
userRewrite s
tcPluginUserDefs )
#else
adaptUserSolveAndRewrite
:: ( userDefs -> TcPluginSolver )
-> ( userDefs -> GHC.UniqFM
#if MIN_VERSION_ghc(9,0,0)
GHC.TyCon
#endif
TcPluginRewriter
)
-> TcPluginDefs userDefs
-> GHC.TcPluginSolver
adaptUserSolveAndRewrite userSolve userRewrite ( TcPluginDefs { tcPluginUserDefs, tcPluginBuiltinDefs } )
= \ givens deriveds wanteds -> do
evBindsVar <- GHC.getEvBindsTcPluginM
shimRewriter
givens deriveds wanteds
( fmap
( \ userRewriter rewriteEnv gs tys ->
tcPluginRewriteM ( userRewriter gs tys )
tcPluginBuiltinDefs rewriteEnv
)
( userRewrite tcPluginUserDefs )
)
( \ gs ds ws ->
tcPluginSolveM ( userSolve tcPluginUserDefs gs ws )
tcPluginBuiltinDefs evBindsVar ds
)
#endif
adaptUserStop :: ( userDefs -> TcPluginM Stop () ) -> TcPluginDefs userDefs -> GHC.TcPluginM ()
adaptUserStop :: (s -> TcPluginM 'Stop ()) -> TcPluginDefs s -> TcPluginM ()
adaptUserStop s -> TcPluginM 'Stop ()
userStop ( TcPluginDefs { s
tcPluginUserDefs :: forall s. TcPluginDefs s -> s
tcPluginUserDefs :: s
tcPluginUserDefs } ) =
TcPluginM 'Stop () -> TcPluginM ()
forall a. TcPluginM 'Stop a -> TcPluginM a
tcPluginStopM (TcPluginM 'Stop () -> TcPluginM ())
-> TcPluginM 'Stop () -> TcPluginM ()
forall a b. (a -> b) -> a -> b
$ s -> TcPluginM 'Stop ()
userStop s
tcPluginUserDefs
class MonadTcPlugin m => MonadTcPluginWork m where
{-# MINIMAL #-}
askBuiltins :: m BuiltinDefs
askBuiltins = [Char] -> m BuiltinDefs
forall a. HasCallStack => [Char] -> a
error [Char]
"askBuiltins: no default implementation"
instance MonadTcPluginWork ( TcPluginM Solve ) where
askBuiltins :: TcPluginM 'Solve BuiltinDefs
askBuiltins = (BuiltinDefs -> EvBindsVar -> TcPluginM BuiltinDefs)
-> TcPluginM 'Solve BuiltinDefs
forall a.
(BuiltinDefs -> EvBindsVar -> TcPluginM a) -> TcPluginM 'Solve a
TcPluginSolveM
\ BuiltinDefs
builtinDefs
EvBindsVar
_evBinds
#ifdef HAS_DERIVEDS
_deriveds
#endif
-> BuiltinDefs -> TcPluginM BuiltinDefs
forall a. a -> TcPluginM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure BuiltinDefs
builtinDefs
instance MonadTcPluginWork ( TcPluginM Rewrite ) where
askBuiltins :: TcPluginM 'Rewrite BuiltinDefs
askBuiltins = (BuiltinDefs -> RewriteEnv -> TcPluginM BuiltinDefs)
-> TcPluginM 'Rewrite BuiltinDefs
forall a.
(BuiltinDefs -> RewriteEnv -> TcPluginM a) -> TcPluginM 'Rewrite a
TcPluginRewriteM \ BuiltinDefs
builtinDefs RewriteEnv
_evBinds -> BuiltinDefs -> TcPluginM BuiltinDefs
forall a. a -> TcPluginM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure BuiltinDefs
builtinDefs
instance TypeError ( 'Text "Cannot emit new work in 'tcPluginInit'." )
=> MonadTcPluginWork ( TcPluginM Init ) where
askBuiltins :: TcPluginM 'Init BuiltinDefs
askBuiltins = [Char] -> TcPluginM 'Init BuiltinDefs
forall a. HasCallStack => [Char] -> a
error [Char]
"Cannot emit new work in 'tcPluginInit'."
instance TypeError ( 'Text "Cannot emit new work in 'tcPluginStop'." )
=> MonadTcPluginWork ( TcPluginM Stop ) where
askBuiltins :: TcPluginM 'Stop BuiltinDefs
askBuiltins = [Char] -> TcPluginM 'Stop BuiltinDefs
forall a. HasCallStack => [Char] -> a
error [Char]
"Cannot emit new work in 'tcPluginStop'."
data TcPluginErrorMessage
= Txt !String
| PrintType !GHC.Type
| (:|:) !TcPluginErrorMessage !TcPluginErrorMessage
| (:-:) !TcPluginErrorMessage !TcPluginErrorMessage
infixl 5 :|:
infixl 6 :-:
mkTcPluginErrorTy :: MonadTcPluginWork m => TcPluginErrorMessage -> m GHC.PredType
mkTcPluginErrorTy :: forall (m :: * -> *).
MonadTcPluginWork m =>
TcPluginErrorMessage -> m TcType
mkTcPluginErrorTy TcPluginErrorMessage
msg = do
builtinDefs :: BuiltinDefs
builtinDefs@( BuiltinDefs { TyCon
typeErrorTyCon :: TyCon
typeErrorTyCon :: BuiltinDefs -> TyCon
typeErrorTyCon } ) <- m BuiltinDefs
forall (m :: * -> *). MonadTcPluginWork m => m BuiltinDefs
askBuiltins
let
errorMsgTy :: GHC.PredType
errorMsgTy :: TcType
errorMsgTy = BuiltinDefs -> TcPluginErrorMessage -> TcType
interpretErrorMessage BuiltinDefs
builtinDefs TcPluginErrorMessage
msg
TcType -> m TcType
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TcType -> m TcType) -> TcType -> m TcType
forall a b. (a -> b) -> a -> b
$ TyCon -> [TcType] -> TcType
GHC.mkTyConApp TyCon
typeErrorTyCon [ TcType
GHC.constraintKind, TcType
errorMsgTy ]
instance ( Monad (TcPluginM s), MonadTcPlugin (TcPluginM s) )
=> MonadThings (TcPluginM s) where
lookupThing :: Name -> TcPluginM s TyThing
lookupThing = TcM TyThing -> TcPluginM s TyThing
forall a. TcM a -> TcPluginM s a
forall (m :: * -> *) a. MonadTcPlugin m => TcM a -> m a
unsafeLiftTcM (TcM TyThing -> TcPluginM s TyThing)
-> (Name -> TcM TyThing) -> Name -> TcPluginM s TyThing
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> TcM TyThing
forall (m :: * -> *). MonadThings m => Name -> m TyThing
lookupThing
data BuiltinDefs =
BuiltinDefs
{ BuiltinDefs -> TyCon
typeErrorTyCon :: !GHC.TyCon
, BuiltinDefs -> TyCon
textTyCon :: !GHC.TyCon
, BuiltinDefs -> TyCon
showTypeTyCon :: !GHC.TyCon
, BuiltinDefs -> TyCon
concatTyCon :: !GHC.TyCon
, BuiltinDefs -> TyCon
vcatTyCon :: !GHC.TyCon
}
data TcPluginDefs s
= TcPluginDefs
{ forall s. TcPluginDefs s -> BuiltinDefs
tcPluginBuiltinDefs :: !BuiltinDefs
, forall s. TcPluginDefs s -> s
tcPluginUserDefs :: !s
}
initBuiltinDefs :: GHC.TcPluginM BuiltinDefs
initBuiltinDefs :: TcPluginM BuiltinDefs
initBuiltinDefs = do
TyCon
typeErrorTyCon <- Name -> TcPluginM TyCon
GHC.tcLookupTyCon Name
GHC.TypeLits.errorMessageTypeErrorFamName
TyCon
textTyCon <- DataCon -> TyCon
GHC.promoteDataCon (DataCon -> TyCon) -> TcPluginM DataCon -> TcPluginM TyCon
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcPluginM DataCon
GHC.tcLookupDataCon Name
GHC.TypeLits.typeErrorTextDataConName
TyCon
showTypeTyCon <- DataCon -> TyCon
GHC.promoteDataCon (DataCon -> TyCon) -> TcPluginM DataCon -> TcPluginM TyCon
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcPluginM DataCon
GHC.tcLookupDataCon Name
GHC.TypeLits.typeErrorShowTypeDataConName
TyCon
concatTyCon <- DataCon -> TyCon
GHC.promoteDataCon (DataCon -> TyCon) -> TcPluginM DataCon -> TcPluginM TyCon
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcPluginM DataCon
GHC.tcLookupDataCon Name
GHC.TypeLits.typeErrorAppendDataConName
TyCon
vcatTyCon <- DataCon -> TyCon
GHC.promoteDataCon (DataCon -> TyCon) -> TcPluginM DataCon -> TcPluginM TyCon
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcPluginM DataCon
GHC.tcLookupDataCon Name
GHC.TypeLits.typeErrorVAppendDataConName
BuiltinDefs -> TcPluginM BuiltinDefs
forall a. a -> TcPluginM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( BuiltinDefs { TyCon
typeErrorTyCon :: TyCon
textTyCon :: TyCon
showTypeTyCon :: TyCon
concatTyCon :: TyCon
vcatTyCon :: TyCon
typeErrorTyCon :: TyCon
textTyCon :: TyCon
showTypeTyCon :: TyCon
concatTyCon :: TyCon
vcatTyCon :: TyCon
.. } )
interpretErrorMessage :: BuiltinDefs -> TcPluginErrorMessage -> GHC.PredType
interpretErrorMessage :: BuiltinDefs -> TcPluginErrorMessage -> TcType
interpretErrorMessage ( BuiltinDefs { TyCon
typeErrorTyCon :: BuiltinDefs -> TyCon
textTyCon :: BuiltinDefs -> TyCon
showTypeTyCon :: BuiltinDefs -> TyCon
concatTyCon :: BuiltinDefs -> TyCon
vcatTyCon :: BuiltinDefs -> TyCon
typeErrorTyCon :: TyCon
textTyCon :: TyCon
showTypeTyCon :: TyCon
concatTyCon :: TyCon
vcatTyCon :: TyCon
.. } ) = TcPluginErrorMessage -> TcType
go
where
go :: TcPluginErrorMessage -> GHC.PredType
go :: TcPluginErrorMessage -> TcType
go ( Txt [Char]
str ) =
TyCon -> [TcType] -> TcType
GHC.mkTyConApp TyCon
textTyCon [ TyLit -> TcType
GHC.LitTy (TyLit -> TcType) -> ([Char] -> TyLit) -> [Char] -> TcType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FastString -> TyLit
GHC.StrTyLit (FastString -> TyLit) -> ([Char] -> FastString) -> [Char] -> TyLit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> FastString
GHC.fsLit ([Char] -> TcType) -> [Char] -> TcType
forall a b. (a -> b) -> a -> b
$ [Char]
str ]
go ( PrintType TcType
ty ) =
TyCon -> [TcType] -> TcType
GHC.mkTyConApp TyCon
showTypeTyCon [ (() :: Constraint) => TcType -> TcType
TcType -> TcType
GHC.typeKind TcType
ty, TcType
ty ]
go ( TcPluginErrorMessage
msg1 :|: TcPluginErrorMessage
msg2 ) =
TyCon -> [TcType] -> TcType
GHC.mkTyConApp TyCon
concatTyCon [ TcPluginErrorMessage -> TcType
go TcPluginErrorMessage
msg1, TcPluginErrorMessage -> TcType
go TcPluginErrorMessage
msg2 ]
go ( TcPluginErrorMessage
msg1 :-: TcPluginErrorMessage
msg2 ) =
TyCon -> [TcType] -> TcType
GHC.mkTyConApp TyCon
vcatTyCon [ TcPluginErrorMessage -> TcType
go TcPluginErrorMessage
msg1, TcPluginErrorMessage -> TcType
go TcPluginErrorMessage
msg2 ]