jcpetruzza / barbies

BSD 3-Clause "New" or "Revised" License
92 stars 15 forks source link

deriving FunctorB: Cannot eta-reduce the representation type enough #53

Closed ulidtko closed 3 months ago

ulidtko commented 3 months ago

Hi @jcpetruzza, thanks for publishing & maintaining the library. :pray:

Quick posterity note, just to document a pitfall.

Say I'm defining whatever barbie for use with the Harg library from @alexpeits.

If I follow barbies' docs closely, I write

newtype EnvFileSource f = EnvFileSource (f Harg.ConfigFile)
  deriving stock Generic
instance FunctorB EnvFileSource
instance TraversableB EnvFileSource
instance ApplicativeB EnvFileSource

And all is good with this one :tada:

However, if I follow Harg's docs first, I instead write

newtype EnvFileSource f = EnvFileSource (f Harg.ConfigFile)
  deriving stock Generic
  deriving (FunctorB, TraversableB, ApplicativeB)

— and this elicits a rather exotic complaint out of GHC 9.6.4 (I hadn't seen such before):

• Can't make a derived instance of ‘FunctorB EnvFileSource’ (even with cunning GeneralizedNewtypeDeriving): cannot eta-reduce the representation type enough • In the newtype declaration for ‘EnvFileSource’

I do have a vague idea how eta-reduction can't chew on this newtype's RHS; but just to zoom out to top-level context — as a user of Harg library, it's genuinely puzzling to get this error.

Not sure if it's as expected or not; I will certainly ping Harg to fix the docs there.

TL;DR for the impatient: use standalone instance FunctorB YourThing instead of deriving FunctorB.

ulidtko commented 3 months ago

Follow-up: deriving FunctorB et al actually works, but requires DeriveAnyClass — while I had only DerivingVia enabled.

This is good too:

{-# LANGUAGE DeriveAnyClass #-}

newtype EnvFileSource f = EnvFileSource (f Harg.ConfigFile)
  deriving stock Generic
  deriving anyclass (FunctorB, TraversableB, ApplicativeB)