Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gPut depends on the nesting structure of :+: #90

Open
Kritzefitz opened this issue Mar 3, 2020 · 0 comments
Open

gPut depends on the nesting structure of :+: #90

Kritzefitz opened this issue Mar 3, 2020 · 0 comments

Comments

@Kritzefitz
Copy link

The documentation of GHC.Generics says this:

However, users /should not rely on a specific nesting strategy/ for :+: and :*: being used.

From reading the code it seems to me as if the code depends on the structure being balanced.

I was able to construct the following example, which breaks the logic of gPut by nesting all constructor to the left:

{-# LANGUAGE DataKinds, TypeOperators #-}
import Data.Serialize
import GHC.Generics

type MyGeneric a = D1 ('MetaData "Foo" "Ghci2" "interactive" 'False) (((C1 ('MetaCons "Foo1" 'PrefixI 'False) U1 :+: C1 ('MetaCons "Foo2" 'PrefixI 'False) U1) :+: C1 ('MetaCons "Foo3" 'PrefixI 'False) U1) :+: C1 ('MetaCons "Foo1" 'PrefixI 'False) U1) a

x :: MyGeneric a
x = M1 $ L1 $ L1 $ L1 $ M1 U1

y :: MyGeneric a
y = M1 $ L1 $ L1 $ R1 $ M1 U1

test :: Either String (Bool, Bool)
test = (\z -> (z == x, z == y)) <$> runGet gGet (runPut $ gPut x)

main :: IO ()
main = print test

When I run the code it outputs Right (False,True), which shows that the encoding and decoding of x isn't equal to x itself, but equal to y.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant