Plutonomicon / plutarch-core

Plutarch 2.0
MIT License
19 stars 6 forks source link

Allow attaching metadata to datatype definitions #36

Open L-as opened 1 year ago

L-as commented 1 year ago

Haskell already allows us to attach a small amount of metadata in the form of strictness-annotations and similar, however, it seems useful to distinguish between Haskell metadata and embedded metadata. The Haskell constructor taking a lazy argument doesn't necessarily mean the embedded constructor should, or vice versa.

There are two main ideas, the first one is adding a fake constructor with metadata:

newtype PMetadata metadata = PMetadata Void#

data PMyType ef = PMyType (ef /$ PInteger) (ef /$ PInteger)
  | PMyTypeMeta (PMetadata '[PScottEncoded])
  deriving stock (Generic)
  deriving anyclass (PHasRepr)

This essentially means the constructor doesn't exist, since you don't have to worry about it when matching, and you can't construct it either.

The other trick is attaching metadata to individual fields like so:

data PMyType ef = PMyType (ef /$ PInteger) (PStrictField ef /$ PInteger)
  deriving stock (Generic)
  deriving anyclass (PHasRepr)

This doesn't affect PHs, nor how users interact with the type, but it allows backends to see that it should be strict.