Record with expected fields - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Sridhar Ratnakumar

My library may ask the user code to supply a product type that must have at least two types of a certain type (say, path and parsed). The user may define it as this:

data Article path parsed
  = Article
    { _article_path :: path
    , _article_parsed :: parsed
    , _article_title :: Text
    , _article_description :: Maybe Text
    }

This doesn't look right though. Is there a type sytem feature I can use to enforce this constraint? So users can define their product types however they want, except to make sure they support those two fields (which the library should be able to access)?

Sridhar Ratnakumar

(The library will fill-in the rest of the fields by loading some JSON, whose structure is defined by the user, based on the fields they use)

Ben Kolera

I mean, I'd probably go down a DSum + Classy Lens approach, but that's just me. :slight_smile:

Ben Kolera

DSum to link a tag to a result type, and using classy lenses to make sure that the target datatype can at least return path and parsed.

Ben Kolera

A full blown extensible records may be too much weight for what you need.

TheMatten

@Sridhar Ratnakumar Have them provide foo :: d path parsed -> (path, parsed) method - they shouldn't be able to come up with those out of thin air, right?

Sridhar Ratnakumar

@Ben Kolera I have used DSum/DMap before, but have a hard time figuring out how they fit in this case.

Sridhar Ratnakumar

@TheMatten I'll try to frame the question better.

Sridhar Ratnakumar

Found another library that's possibly relevant: https://github.com/i-am-tom/higgledy

Higher-kinded data via generics. Contribute to i-am-tom/higgledy development by creating an account on GitHub.
Sridhar Ratnakumar

Okay, an update. I ended up sidestepping this problem entirely by designing my types differently. In particular, I detached the Document type from the Markup type class. Separation of concerns, ftw. https://github.com/srid/rib/pull/51/files

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