summaryrefslogtreecommitdiff
path: root/libraries/ghc-prim
diff options
context:
space:
mode:
authorKrzysztof Gogolewski <krzysztof.gogolewski@tweag.io>2022-05-14 01:07:36 +0200
committerKrzysztof Gogolewski <krzysztof.gogolewski@tweag.io>2022-05-27 16:44:48 +0200
commit3bd7d5d668b316f517a66c72fcf9bc7a36cc6ba4 (patch)
tree186c133c0259d87f04b79c2017b7ee858b1b380a /libraries/ghc-prim
parented37027f713bb6563fd98d144a39211339fd91a5 (diff)
downloadhaskell-3bd7d5d668b316f517a66c72fcf9bc7a36cc6ba4.tar.gz
Use a class to check validity of withDictwip/withdict
This moves handling of the magic 'withDict' function from the desugarer to the typechecker. Details in Note [withDict]. I've extracted a part of T16646Fail to a separate file T16646Fail2, because the new error in 'reify' hides the errors from 'f' and 'g'. WithDict now works with casts, this fixes #21328. Part of #19915
Diffstat (limited to 'libraries/ghc-prim')
-rw-r--r--libraries/ghc-prim/GHC/Magic/Dict.hs14
-rw-r--r--libraries/ghc-prim/changelog.md7
2 files changed, 10 insertions, 11 deletions
diff --git a/libraries/ghc-prim/GHC/Magic/Dict.hs b/libraries/ghc-prim/GHC/Magic/Dict.hs
index 12861db568..560ab3956f 100644
--- a/libraries/ghc-prim/GHC/Magic/Dict.hs
+++ b/libraries/ghc-prim/GHC/Magic/Dict.hs
@@ -1,7 +1,7 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ImpredicativeTypes #-}
-{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
@@ -18,7 +18,7 @@
-- Portability : non-portable (GHC Extensions)
--
-- Defines the 'withDict' function. For more information, see
--- @Note [withDict]@ in "GHC.HsToCore.Expr" in GHC.
+-- @Note [withDict]@ in "GHC.Tc.Instance.Class" in GHC.
-- The definition of 'withDict' is located in a separate module from
-- "GHC.Magic" because 'withDict' is @Unsafe@ (it threatens type class
-- coherence) while "GHC.Magic" is @Trustworthy@.
@@ -28,9 +28,8 @@
--
-----------------------------------------------------------------------------
-module GHC.Magic.Dict (withDict) where
+module GHC.Magic.Dict (WithDict(..)) where
-import GHC.Prim.Panic (panicError)
import GHC.Types (RuntimeRep, TYPE)
-- | @'withDict' d f@ provides a way to call a type-class–overloaded function
@@ -38,7 +37,6 @@ import GHC.Types (RuntimeRep, TYPE)
--
-- 'withDict' can only be used if the type class has a single method with no
-- superclasses. For more (important) details on how this works, see
--- @Note [withDict]@ in "GHC.HsToCore.Expr" in GHC.
-withDict :: forall {rr :: RuntimeRep} st dt (r :: TYPE rr). st -> (dt => r) -> r
-{-# NOINLINE withDict #-}
-withDict = panicError "Non-rewritten withDict"#
+-- @Note [withDict]@ in "GHC.Tc.Instance.Class" in GHC.
+class WithDict cls meth where
+ withDict :: forall {rr :: RuntimeRep} (r :: TYPE rr). meth -> (cls => r) -> r
diff --git a/libraries/ghc-prim/changelog.md b/libraries/ghc-prim/changelog.md
index 45701629a4..049b254743 100644
--- a/libraries/ghc-prim/changelog.md
+++ b/libraries/ghc-prim/changelog.md
@@ -4,7 +4,8 @@
`GHC.Magic.Dict` instead of `GHC.Prim`. `withDict` now has the type:
```
- withDict :: forall {rr :: RuntimeRep} st dt (r :: TYPE rr). st -> (dt => r) -> r
+ class WithDict cls meth where
+ withDict :: forall {rr :: RuntimeRep} (r :: TYPE rr). meth -> (cls => r) -> r
```
Unlike `magicDict`, `withDict` can be used without defining an
@@ -14,10 +15,10 @@
```
withTypeable :: forall k (a :: k) rep (r :: TYPE rep). ()
=> TypeRep a -> (Typeable a => r) -> r
- withTypeable rep k = withDict @(TypeRep a) @(Typeable a) rep k
+ withTypeable rep k = withDict @(Typeable a) rep k
```
- Note that the explicit type applications are required, as the call to
+ Note that the explicit type application is required, as the call to
`withDict` would be ambiguous otherwise.
- Primitive types and functions which handle boxed values are now levity-polymorphic,