Guarantees about shape of Generic's Rep

Alexey Khudyakov alexey.skladnoy at gmail.com
Tue Apr 20 17:39:33 UTC 2021


Hi!

I have a question about guarantees that GHC provides about shape of generic
representation of data type. To be more concrete if we have two
single-constructor data types with N fields will their binary trees 
built from
(:*:) be balanced in same way? Same for (:+:) binary tree.

I want to use generics to write conversions between data types with same 
shape:

 > λ> data Foo = Foo Int Char deriving (Show,Generic)
 > λ> to . coerce . from $ Foo 1 'c' :: (Int,Char)
 > (1,'c')
 > λ> to . coerce . from $ Foo 1 'c' :: (Sum Int,Char)
 > (Sum {getSum = 1},'c')
 > λ> to . coerce . from $ (Sum (1::Int), 'c') :: Foo
 > Foo 1 'c'

Coercion between Reps for different types is only possible when they 
have same
shape. Quick experiments shows that they're same. I think that they 
should be
same but documentation is silent about this.


Thanks in advance,
Alexey


More information about the Glasgow-haskell-users mailing list