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

Add support for extensible type attributes #60

Merged
merged 9 commits into from
Jun 5, 2021

Conversation

craigfe
Copy link
Member

@craigfe craigfe commented May 8, 2021

This adds support for attaching arbitrary metadata to type representations, in the form of a new AST node:

  | Attributes : { attrs : 'a Attribute.Map.t; attr_type : 'a t } -> 'a t

where an 'a Attribute.Map.t can store arbitrary data (indexed by the type 'a).

The motivation is to allow interpreters to attach custom info to typereps that is not generally useful (avoiding hard-coding this information directly in the typerep AST). For example, we currently have a variant case Boxed : 'a t -> 'a t that is just a flag for the binary encoders (and must be manually ignored by all the other derivers). The attribute mechanism allows type_binary.ml to define its own Boxed attribute to use instead.

Another use-case for this feature is in allowing interpreters to effectively extend Custom by attaching hand-implemented functions to typereps. As a demonstration, the new type_random.ml deriver now supports custom value generators (a limitation that was raised in #58). This isn't yet exposed to external users (since users can't yet define their own interpreters either).

The implementation of this feature uses Higher, and (for once) I think it's strictly necessary to support the heterogeneous map semantics. I've defined a helper functor that prevents this from spreading further than it needs to.

@craigfe craigfe force-pushed the type-attributes branch from 1895dd0 to 9341b3b Compare May 8, 2021 22:49
@craigfe craigfe changed the title Add support for user-defined type attributes Add support for extensible type attributes May 8, 2021
@mattiasdrp
Copy link
Contributor

I think I really need this PR to finish mine (#57) because the current state of Repr makes it hard to have some functions that are not directly implemented but waiting in a functor.

Copy link
Contributor

@icristescu icristescu left a comment

Choose a reason for hiding this comment

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

Really nice!

Copy link
Contributor

@Ngoguey42 Ngoguey42 left a comment

Choose a reason for hiding this comment

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

I always love to read OCaml black magic! :)

Co-authored-by: Ioana Cristescu  <[email protected]>
@craigfe craigfe force-pushed the type-attributes branch 2 times, most recently from ec367f2 to 684d255 Compare May 17, 2021 14:02
@craigfe
Copy link
Member Author

craigfe commented Jun 5, 2021

Thanks all for the reviews 🙂 Merging now.

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.

4 participants