Skip to content

Commit 6b470f9

Browse files
committed
Strongweak.Generic: +GenericallySW0
1 parent c114cf0 commit 6b470f9

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/Strongweak/Generic.hs

+29-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ module Strongweak.Generic
1111
weakenGeneric
1212
, strengthenGeneric
1313

14-
-- * Generic wrapper
14+
-- * Generic deriving via wrappers
1515
, GenericallySW(..)
16+
, GenericallySW0(..)
1617
) where
1718

1819
import Strongweak.Weaken.Generic
@@ -23,6 +24,8 @@ import Strongweak.Strengthen ( Strengthen(strengthen) )
2324
import GHC.Generics
2425
import Data.Kind ( Type )
2526

27+
import Strongweak.Strength
28+
2629
{- $generic-derivation-compatibility
2730
2831
The 'Strengthen' and 'Weaken' generic derivers allow you to derive instances
@@ -47,8 +50,8 @@ bad idea) do not require it.
4750
4851
Note that the generics only handle one "layer" at a time. If you have a data
4952
type with nested 'Strongweak.Strengthen.SW' uses, these generics will fail with
50-
a type error. Write the instance manually instead. (I can't think of an easy way
51-
to handle this, but TODO all the same.)
53+
a type error. Either use 'Strongweak.WeakenN.WeakenN', or write the instances
54+
manually.
5255
-}
5356

5457
{- | @DerivingVia@ wrapper for strongweak instances.
@@ -70,12 +73,8 @@ deriving via (GenericallySW (XYZ 'Strong) (XYZ 'Weak)) instance Weaken (XYZ 'Str
7073
deriving via (GenericallySW (XYZ 'Strong) (XYZ 'Weak)) instance Strengthen (XYZ 'Strong)
7174
@
7275
73-
TODO can't figure out a way around multiple standalone deriving declarations :(
74-
75-
TODO maybe GenericallySW1? but even so instances differ between weaken and
76-
strengthen (weaken needs nothing) so it's kinda better this way. :)
76+
Regrettably, use of this requires UndecidableInstances.
7777
-}
78-
7978
newtype GenericallySW s (w :: Type) = GenericallySW { unGenericallySW :: s }
8079

8180
instance
@@ -91,3 +90,25 @@ instance
9190
, Weaken (GenericallySW s w)
9291
) => Strengthen (GenericallySW s w) where
9392
strengthen = fmap GenericallySW . strengthenGeneric
93+
94+
-- | 'GenericallySW' where the type takes a 'Strength' in its last type var.
95+
--
96+
-- Shorter instances for types of a certain shape.
97+
--
98+
-- Regrettably, use of this requires UndecidableInstances.
99+
newtype GenericallySW0 (f :: Strength -> Type)
100+
= GenericallySW0 { unGenericallySW0 :: f Strong }
101+
102+
instance
103+
( Generic (f Strong), Generic (f Weak)
104+
, GWeaken (Rep (f Strong)) (Rep (f Weak))
105+
) => Weaken (GenericallySW0 f) where
106+
type Weakened (GenericallySW0 f) = f Weak
107+
weaken = weakenGeneric . unGenericallySW0
108+
109+
instance
110+
( Generic (f Strong), Generic (f Weak)
111+
, GStrengthenD (Rep (f Weak)) (Rep (f Strong))
112+
, Weaken (GenericallySW0 f)
113+
) => Strengthen (GenericallySW0 f) where
114+
strengthen = fmap GenericallySW0 . strengthenGeneric

0 commit comments

Comments
 (0)