Safe Haskell | Unsafe |
---|---|
Language | Haskell2010 |
Synopsis
- newtype CodeT q a = UnsafeCodeT (q Type)
- unsafeCodeTCoerce :: Quote q => q Type -> CodeT q a
- unTypeCodeT :: CodeT m a -> m Type
- appCodeT :: Quote m => CodeT m f -> CodeT m x -> CodeT m (f x)
- sigCode :: Quote q => Code q a -> CodeT q a -> Code q a
- sigCodeT :: Quote q => CodeT q (a :: k) -> CodeT q k -> CodeT q a
- type CodeTQ = CodeT Q
- unsafeCodeTName :: Quote m => Name -> CodeT m a
- unsafeCodeTNameD :: String -> String -> String -> forall m. Quote m => CodeT m a
- unsafeCodeTNameTC :: String -> String -> String -> forall m. Quote m => CodeT m a
- class LiftT a where
CodeT
CodeT
is to Type
what Code
is to Exp
.
Because current GHC Haskell doesn't have true dependent types,
we often use singletons pattern to emulate them.
To a first approximation
type emulates
dependent function CodeT
q a -> ...forall (a ::
.Code
q Type
) -> ...
There aren't typed type quotes, i.e. we cannot write [t|| Int ||]
,
instead we have to resort to unsafe interface for now:
>>>
let ty = UnsafeCodeT [t| Int |] :: Quote q => CodeT q Int
>>>
unTypeCodeT ty >>= print . ppr
GHC.Types.Int
... or we can "safely" create many CodeT
values using LiftT
type-class:
>>>
let ty' = codeT :: Quote q => CodeT q Int
>>>
unTypeCodeT ty' >>= print . ppr
GHC.Types.Int
UnsafeCodeT (q Type) | Unsafely convert an untyped code representation into a typed code representation. Prefer using |
unsafeCodeTCoerce :: Quote q => q Type -> CodeT q a Source #
Unsafely convert an untyped code representation into a typed code representation.
unTypeCodeT :: CodeT m a -> m Type Source #
Extract the untyped representation from the typed representation.
appCodeT :: Quote m => CodeT m f -> CodeT m x -> CodeT m (f x) Source #
>>>
let ty = appCodeT (codeT @Maybe) (codeT @Char)
>>>
unTypeCodeT ty >>= print . ppr
GHC.Maybe.Maybe GHC.Types.Char
sigCode :: Quote q => Code q a -> CodeT q a -> Code q a Source #
>>>
let e = sigCode [|| 'x' ||] codeT
>>>
unTypeCode e >>= print . ppr
'x' :: GHC.Types.Char
sigCodeT :: Quote q => CodeT q (a :: k) -> CodeT q k -> CodeT q a Source #
>>>
let ty = sigCodeT (codeT @Bool) codeT
>>>
unTypeCodeT ty >>= print . ppr
(GHC.Types.Bool :: GHC.Prim.TYPE (GHC.Types.BoxedRep GHC.Types.Lifted))
unsafeCodeTName :: Quote m => Name -> CodeT m a Source #
Unsafely convert a (type) name into a typed code representation.
The namespace of Name
is not checked.
>>>
let ty = unsafeCodeTName ''Bool
>>>
unTypeCodeT ty >>= print . ppr
GHC.Types.Bool
unsafeCodeTNameD :: String -> String -> String -> forall m. Quote m => CodeT m a Source #
Unsafely create a data constructor name and convert it into a typed code representation.
unsafeCodeTNameTC :: String -> String -> String -> forall m. Quote m => CodeT m a Source #
Unsafely create a type constructor name and convert it into a typed code representation.
LiftT
Implicitly create CodeT
values.
This packages provides some LiftT
instances for types in GHC bundled libs.
There is no instance for Constraint
, as Constraint
is not apart from Type
.
See the GHC issue https://gitlab.haskell.org/ghc/ghc/-/issues/24279.
The codet-plugin can automatically
create instances for type constructors. (The provided f x
instance does most of the work).