witch/src/ghc-9.0/Witch/Lift.hs

72 lines
1.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE ExplicitForAll #-}
module Witch.Lift where
import qualified Data.Typeable as Typeable
import qualified Language.Haskell.TH.Syntax as TH
import qualified Witch.TryCast as TryCast
import qualified Witch.Utility as Utility
2021-04-18 21:01:05 +03:00
-- | This is like 'Utility.unsafeCast' except that it works at compile time
-- rather than runtime.
--
-- > -- Avoid this:
-- > unsafeCast "some literal"
-- >
-- > -- Prefer this:
-- > $$(liftedCast "some literal")
liftedCast
2021-04-11 17:16:45 +03:00
:: forall source target m
2021-04-17 16:48:16 +03:00
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
)
=> source
-> TH.Code m target
liftedCast = TH.liftTyped . Utility.unsafeCast
2021-04-18 21:01:05 +03:00
-- | This is like 'Utility.unsafeFrom' except that it works at compile time
-- rather than runtime.
--
-- > -- Avoid this:
-- > unsafeFrom @s "some literal"
-- >
-- > -- Prefer this:
2021-04-18 22:44:05 +03:00
-- > $$(liftedFrom @s "some literal")
liftedFrom
2021-05-11 02:12:06 +03:00
:: forall source target m
. ( TryCast.TryCast source target
2021-04-17 16:48:16 +03:00
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
)
=> source
-> TH.Code m target
liftedFrom = liftedCast
2021-04-18 21:01:05 +03:00
-- | This is like 'Utility.unsafeInto' except that it works at compile time
-- rather than runtime.
--
-- > -- Avoid this:
-- > unsafeInto @t "some literal"
-- >
-- > -- Prefer this:
2021-04-18 22:44:05 +03:00
-- > $$(liftedInto @t "some literal")
liftedInto
2021-05-11 02:12:06 +03:00
:: forall target source m
. ( TryCast.TryCast source target
2021-04-17 16:48:16 +03:00
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
)
=> source
-> TH.Code m target
liftedInto = liftedCast