hydra icon indicating copy to clipboard operation
hydra copied to clipboard

Generate per-type term coders in Haskell

Open joshsh opened this issue 2 years ago • 5 comments

Often, it is necessary to take an instance of a generated type like LatLon and encode it as a term, or conversely to take a term representing a LatLon and decode it to an instance of the generated type. This encoding is logically defined, but is not materialized as actual Haskell or Java code which can do the encoding/decoding for you.

In many cases, it would be helpful if Hydra were to generate these coders along with the type definition. In other cases, however, this would be overkill, and result in a large amount of code which is never used. The open problem is not so much how to generate the coders, which should be straightfoward, but when to generate them.

joshsh avatar Mar 02 '23 21:03 joshsh

One option here is to add a boolean tag field like generateCoders to the DSL from which the type is being generated. That way the user is able to decide when they want to generate a coder.

aman-dureja avatar Mar 02 '23 22:03 aman-dureja

What a great suggestion. That would be a boolean annotation.

joshsh avatar Mar 02 '23 22:03 joshsh

Some other notes:

  • If TypeA depends on TypeB, and TypeA is marked for coder generation, then a coder must be generated for TypeB as well.
  • Not all types are encodable. E.g. function types
  • The serialization variant of the "epsilon encoding" which maps types to terms and back (see CoreEncoding/CoreDecoding) would be a special case of these generated coders.

joshsh avatar Mar 02 '23 22:03 joshsh