Re: Can I coerce type constructors to Any?

Andreas Klebinger via Haskell-Cafe <[email protected]> Thu, 22 Jan 2026 23:20:28 +0100
Newsgroups gmane.comp.lang.haskell.cafe
Message-ID <[email protected]>
 > (I don't think what Andreas said can *literally* be correct, because 
surely it's safe to unsafeCoerce from a to b when `Coercible a b` is in 
scope?)

Yes I was perhaps a bit too restrictive. The docs in fact explicitly 
mention this case among some others. I don't see how it could go wrong here.
In fact the docs mention this case in particular! 
(https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Unsafe-Coerce.html#v:unsafeCoerce):

 > (superseded) Casting between two types which have exactly the same 
structure: between a newtype of T and T, or between types which differ 
only in "phantom" type parameters. It is now preferred to use |coerce 
<https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Data-Coerce.html#v:coerce>| 
from |Data.Coerce|, which is type-safe.

But really there is no reason to use dirty tricks like unsafeCoerce in 
this situation to begin with when `coerce` is right there!

-----------------

As for the question about `Any x` or `f Any` I'm not sure.

The basic idea of unsafeCoerce is really to be able to pass along the 
underlying pointer using `Any` as long as we don't use it without 
turning it back into it's original type first.

Trying to use `Any` for type level trickery seems suspicious to me, and 
I would not be surprised to run into GHC bugs or worse using it that way.
The concrete example sadly doesn't seem to compile. And type level edge 
cases are really not my speciality so maybe someone else can make more 
definite statements there.

_______________________________________________
Haskell-Cafe mailing list -- [email protected]
To (un)subscribe, modify options or view archives go to:
Only members subscribed via the mailman list are allowed to post.