data T t where
A :: Show (t a) => t a -> T t
B :: Coercible Int (t a) => t a -> T t
f :: T t -> String
f (A t) = show t
g :: T t -> Int
g (B t) = coerce t
Why does f
compile but g
generate an error like follows? I'm using GHC 8.4.
• Couldn't match representation of type ‘Int’ with that of ‘t a’
Inaccessible code in
a pattern with constructor:
B :: forall k (t :: k -> *) (a :: k).
Coercible Int (t a) =>
t a -> T t,
in an equation for ‘g’
• In the pattern: B t
In an equation for ‘g’: g (B t) = coerce t
Also, are Coercible
constraints zero-cost even when they are embedded in GADTs?
UPD: Compiler bug: https://ghc.haskell.org/trac/ghc/ticket/15431
Coercible Int (t a)
. The error message becomes a tiny bit clearer if you swap outCoercible Int (t a)
forInt ~ t a
. OTOH, something likeCoercible [Int] (t a)
is fine, since you are coercing from from type constructor to type constructor. – Deteriorateg (B t) = 42
already triggers the error. It seems the issue lies in pattern matching, not in the use ofcoerce
. Maybe, for some reason, the constraint is required in the pattern matching, instead of provided (as one would expect with GADTs)? – SpaldingInt
triggers "Inaccessible code". UsingIdentity Int
does that as well. Using[Int]
instead is OK. Maybe GHC needs to know thatt
is representational, but there's no way to specify that. – Spalding