@@ -11,8 +11,9 @@ module Strongweak.Generic
11
11
weakenGeneric
12
12
, strengthenGeneric
13
13
14
- -- * Generic wrapper
14
+ -- * Generic deriving via wrappers
15
15
, GenericallySW (.. )
16
+ , GenericallySW0 (.. )
16
17
) where
17
18
18
19
import Strongweak.Weaken.Generic
@@ -23,6 +24,8 @@ import Strongweak.Strengthen ( Strengthen(strengthen) )
23
24
import GHC.Generics
24
25
import Data.Kind ( Type )
25
26
27
+ import Strongweak.Strength
28
+
26
29
{- $generic-derivation-compatibility
27
30
28
31
The 'Strengthen' and 'Weaken' generic derivers allow you to derive instances
@@ -47,8 +50,8 @@ bad idea) do not require it.
47
50
48
51
Note that the generics only handle one "layer" at a time. If you have a data
49
52
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.
52
55
-}
53
56
54
57
{- | @DerivingVia@ wrapper for strongweak instances.
@@ -70,12 +73,8 @@ deriving via (GenericallySW (XYZ 'Strong) (XYZ 'Weak)) instance Weaken (XYZ 'Str
70
73
deriving via (GenericallySW (XYZ 'Strong) (XYZ 'Weak)) instance Strengthen (XYZ 'Strong)
71
74
@
72
75
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.
77
77
-}
78
-
79
78
newtype GenericallySW s (w :: Type ) = GenericallySW { unGenericallySW :: s }
80
79
81
80
instance
@@ -91,3 +90,25 @@ instance
91
90
, Weaken (GenericallySW s w )
92
91
) => Strengthen (GenericallySW s w ) where
93
92
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