Skip to content

Support empty record definitions in parser#91

Merged
etherz10 merged 2 commits intomasterfrom
adinapoli/issue-83
Mar 17, 2026
Merged

Support empty record definitions in parser#91
etherz10 merged 2 commits intomasterfrom
adinapoli/issue-83

Conversation

@adinapoli
Copy link
Contributor

Fixes #83.

This PR extends the parser to support this syntax:

er :: EmptyRecord
  = record

Under the hood, this generates a Haskell datatype with a single "tag", i.e: data EmptyRecord = EmptyRecord.

This is actively used in this PR and it does the trick. While using it in Atlas I've got GHC to warn about some unused variables; it turns out that in a few places, when we generated TH definitions in the tools for the various instances, we could be generating things like:

toJSON = \x -> object []

(Like it would be the case now for an EmptyRecord). This PR amends the TH generation to use wildP under the right circumstances.

Allow RRFields production to match zero or more fields,
enabling empty record definitions.

Fixed applicativeE to wrap empty lists in return, enabling
proper JSON parsing for empty record types.

Added test cases for empty records, mixed empty and filled records.

Fixes #83.
When a record has zero fields, the TH code generators for various tool
instances would bind variables (e.g. lambda parameters) that were never
referenced in the body. With -Wall/-Wunused-matches, GHC flags these
as warnings in the generated splices.

Fix by using wildcard patterns (wildP) instead of named bindings (varP)
when srFields is empty, across all affected generators:

- Tools/JSON.hs (ToJSON): \x -> object [] => \_ -> object []
- Tools/JSON.hs (FromJSONWithErrs): (Object x) => (Object _)
- Tools/QuickCheck.hs (Arbitrary): sized (\x -> pure Con) => sized (\_ -> ...)
- Tools/DeepSeq.hs (NFData): \x -> () => \_ -> ()
- Tools/Example.hs (Example): sized (\x -> pure Con) => sized (\_ -> ...)
- Tools/Traversal.hs (traversal): \f r -> pure Con => \_ _ -> pure Con
@adinapoli adinapoli requested a review from adamgundry March 16, 2026 10:01
Copy link
Contributor

@adamgundry adamgundry left a comment

Choose a reason for hiding this comment

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

Thanks @adinapoli!

@etherz10 etherz10 merged commit 59ecd88 into master Mar 17, 2026
6 checks passed
@etherz10 etherz10 deleted the adinapoli/issue-83 branch March 17, 2026 11:28
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.

Empty record types should be definable

3 participants