Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use withDict to implement bindIP on recent GHCs #166

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Language/Haskell/TH/Desugar/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ import Data.Generics hiding ( Fixity )
import Data.Traversable
import Data.Maybe
import GHC.Classes ( IP )

#if MIN_VERSION_base(4,17,0)
import GHC.Exts ( withDict )
#else
import Unsafe.Coerce ( unsafeCoerce )
#endif

----------------------------------------
-- TH manipulations
Expand Down Expand Up @@ -423,16 +428,20 @@ extractBoundNamesPat (UnboxedSumP pat _ _) = extractBoundNamesPat pat
-- General utility
----------------------------------------

-- dirty implementation of explicit-to-implicit conversion
newtype MagicIP name a r = MagicIP (IP name a => r)

-- | Get an implicit param constraint (@IP name a@, which is the desugared
-- form of @(?name :: a)@) from an explicit value.
--
-- This function is only available with GHC 8.0 or later.
bindIP :: forall name a r. a -> (IP name a => r) -> r
#if MIN_VERSION_base(4,17,0)
bindIP = withDict @(IP name a)
#else
bindIP val k = (unsafeCoerce (MagicIP @name k) :: a -> r) val

-- dirty implementation of explicit-to-implicit conversion
newtype MagicIP name a r = MagicIP (IP name a => r)
#endif

-- like GHC's
splitAtList :: [a] -> [b] -> ([b], [b])
splitAtList [] x = ([], x)
Expand Down