[Haskell-cafe] Question on Coerce

Jaro Reinders jaro.reinders at gmail.com
Wed Dec 2 16:19:25 UTC 2020


Would it be possible to extend Coercible? I have also always wanted a coercible 
between the fixed point of the F-algebra and a recursive data type, e.g.:

	newtype Fix f = Fix (f (Fix f))

	data List a = Nil | Cons a (List a)

	data ListF a f = NilF | ConsF a f deriving Functor

	type List' a = Fix (ListF a)

I would like Coercible (List a) (Fix (ListF a)), then you don't have to 
manually write a conversion function when using a general catamorphism:

	cata :: (Functor f, Coercible a (Fix f)) => (f b -> b) -> (a -> b)

I have already tried this with unsafeCoerce and that works, much to my surprise.

It would be even nicer if GHC provided a built-in F-algebras, so that I 
wouldn't even need to define ListF manually.

(I know that these F-algebras can be generated automatically with Template 
Haskell, but that is too heavyweight for most of my use cases)

On 12/2/20 4:24 PM, Georgi Lyubenov wrote:
> You can only coerce between types that guaranteedly have the same runtime 
> representation (newtypes), and "structures of them". Look up Coercible for more 
> information.
> 
> In this case, there is no way to know that two arbitrary types a and b have the 
> same representation, so you can't coerce between them.
> 
> =======
> Georgi
> 
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
> 


More information about the Haskell-Cafe mailing list