Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Can I use Representable10 and Update10 with structures containing lists? #7

Open
patrickt opened this issue Feb 24, 2022 · 1 comment

Comments

@patrickt
Copy link

patrickt commented Feb 24, 2022

Hi there,

First, congratulations on a really cool package. I’m excited to explore whether it’s a solution to this problem we’ve encountered while building the Semantic framework: we want to generate syntax trees that have shape functors attached to them, but working with these structures in a manner that’s comparably fluent to plain-old-data-types is tricky.

I’ve been experimenting with a JSON grammar, and have run into a problem: I can’t figure out how to specify that one syntax node should contain a list of others. I have these two types:

newtype EscapeSequence (f :: Type -> Type) = EscapeSequence { text :: Ap10 Text f }
  deriving stock (Eq, Ord, Read, Show, Generic, Generic1)
  deriving anyclass (NFData, Hashable)
  deriving (Portray, Diff) via Wrapped Generic (EscapeSequence f)
  deriving (Foldable10, Traversable10, Constrained10 c, Functor10, Applicative10, Representable10, Update10) via Wrapped1 Generic1 EscapeSequence

newtype StringContent (f :: Type -> Type) = StringContents { contents :: EscapeSequence f }
  deriving stock (Eq, Ord, Read, Show, Generic, Generic1)
  deriving anyclass (NFData, Hashable)
  deriving (Portray, Diff) via Wrapped Generic (StringContent f)
  deriving (Foldable10, Traversable10, Constrained10 c, Functor10, Applicative10, Representable10, Update10) via Wrapped1 Generic1 StringContent

and I would like StringContent to contain an [EscapeSequence f], or some sort of structure that allows one StringContent to hold multiple EscapeSequence values. (Note that in this case I could just remove the type parameter from EscapeSequence, but lists etc. appear elsewhere in syntax where this wouldn’t be an option.) I’ve tried using a plain [EscapeSequence f] field, which since [] is not Representable, precludes using Representable10 and Update10. I’ve tried using Cofree, and also tried using Compose and :.: and ListT to glom on an additional list-layer. None of these things seem to work—the only solution I’ve found is to remove the Representable10 and Update10 derivings. While I could dispense with them, it would be a huge win if I could retain that typeclass machinery, as I’d love to use ten-lens. Do you have any suggestions here?

@awpr
Copy link
Contributor

awpr commented Jul 22, 2022

Sorry for the long delay -- I'm struggling to convince GitHub to notify me when things happen in these repos. I've just squashed another potential reason for it not to notify me, but who knows if it's the last.

It seems to be unavoidably true that a version of StringContent with a list of EscapeSequences can't be Representable10 or Update10: those are about having a fixed, statically-known "shape" w.r.t. the uses of f, and having a list in that position means the type has a variable shape. So anything that actually requires the full power of Representable10 or Update10 actually won't work on this type.

That said, there could be situations where some functionality was convenient to derive from Representable10 but didn't actually need it, so depending on what in particular you wanted to use it for, it might be possible to come up with a suitable generalization that works for not-quite-Representable10 types.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants