Skip to content

Add Binary (Solo a) instance - #248

Open
NopeCharon wants to merge 1 commit into
haskell:masterfrom
NopeCharon:solo
Open

NopeCharon wants to merge 1 commit into
haskell:masterfrom
NopeCharon:solo

Conversation

@NopeCharon

Copy link
Copy Markdown

Closes #188.

Solo a is encoded exactly like a (no tag, no length prefix), consistent with () and the other tuple instances.

The instance requires base >= 4.16, which is where Data.Tuple starts exporting Solo. GHC 9.0 only has it in ghc-prim's GHC.Tuple, and binary doesn't depend on ghc-prim. Since base-4.18 the data constructor is MkSolo and the Solo pattern synonym is deprecated, so the instance matches on MkSolo there (to avoid -Wdeprecations warnings) and on Solo for base-4.16/4.17.

Tests: a round-trip property in the QuickCheck suite and a wire-format golden vector checking that Solo Word16 encodes like Word16.

Solo, the canonical lifted 1-tuple, is exported from Data.Tuple since
base-4.16 but had no Binary instance, unlike () and the larger tuples.

Add an instance that encodes Solo a exactly like a, without a tag or
length prefix, consistent with the other tuple instances. It is only
defined for base >= 4.16, where Data.Tuple exports Solo; binary does
not depend on ghc-prim, which is where GHC 9.0 (base-4.15) defines it.
Since base-4.18 the data constructor is MkSolo and the Solo pattern
synonym is deprecated, so pattern match on MkSolo there to avoid
-Wdeprecations warnings, and on Solo for base-4.16 and base-4.17.

Add a round-trip QuickCheck test and a wire-format golden vector
pinning the encoding of Solo Word16 to that of Word16.

Closes haskell#188
Comment thread tests/QC.hs
Comment on lines +1474 to +1478
#if MIN_VERSION_base(4,18,0)
, testWithGen "Solo Int" (MkSolo <$> (arbitrary :: Gen Int))
#elif MIN_VERSION_base(4,16,0)
, testWithGen "Solo Int" (Solo <$> (arbitrary :: Gen Int))
#endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use testWithGen here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QuickCheck only has an Arbitrary (Solo a) instance since 2.17 and the test suite still allows QuickCheck >= 2.9, so I used an explicit generator like the Identity and Monoid.Sum tests below instead of adding an orphan instance or CPP on the QuickCheck version.

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

Successfully merging this pull request may close these issues.

Add Solo instance

2 participants