@@ -47,6 +47,7 @@ import Data.Generics.Internal.Void
4747import Data.Generics.Internal.Families
4848import Data.Generics.Product.Internal.Positions
4949import Data.Generics.Product.Internal.GLens
50+import Data.Generics.Internal.Errors
5051
5152import Data.Kind (Constraint, Type)
5253import Data.Type.Bool (type (&&))
@@ -114,9 +115,15 @@ class HasPosition' (i :: Nat) s a | s i -> a where
114115class HasPosition0 (i :: Nat) s t a b where
115116 position0 :: VL.Lens s t a b
116117
118+-- |
119+-- >>> getPosition @2 human
120+-- 50
117121getPosition :: forall i s a. HasPosition' i s a => s -> a
118122getPosition s = s ^. position' @i
119123
124+-- |
125+-- >>> setPosition @2 60 human
126+-- Human {name = "Tunyasz", age = 60, address = "London"}
120127setPosition :: forall i s a. HasPosition' i s a => a -> s -> s
121128setPosition = VL.set (position' @i)
122129
@@ -126,6 +133,12 @@ instance
126133 , cs ~ CRep s
127134 , Coercible (Rep s) cs
128135 , GLens' (HasTotalPositionPSym i) cs a
136+ , Defined (Rep s)
137+ (NoGeneric s '[ 'Text "arising from a generic lens focusing on the field at"
138+ , 'Text "position " ':<>: QuoteType i ':<>: 'Text " of type " ':<>: QuoteType a
139+ ':<>: 'Text " in " ':<>: QuoteType s
140+ ])
141+ (() :: Constraint)
129142 ) => HasPosition' i s a where
130143 position' f s = VL.ravel (repLens . coerced @cs @cs . glens @(HasTotalPositionPSym i)) f s
131144 {-# INLINE position' #-}
@@ -136,10 +149,7 @@ class (~~) (a :: k) (b :: k) | a -> b, b -> a
136149instance (a ~ b) => (~~) a b
137150
138151instance -- see Note [Changing type parameters]
139− ( Generic s
140− , Generic t
141− , ErrorUnless i s (0 <? i && i <=? Size (Rep s))
142− , GLens (HasTotalPositionPSym i) (CRep s) (CRep t) a b
152+ ( ErrorUnless i s (0 <? i && i <=? Size (Rep s))
143153 , HasTotalPositionP i (CRep s) ~~ 'Just a
144154 , HasTotalPositionP i (CRep t) ~~ 'Just b
145155 , HasTotalPositionP i (CRep (Indexed s)) ~~ 'Just a'
@@ -148,6 +158,7 @@ instance -- see Note [Changing type parameters]
148158 , s ~~ Infer t b' a
149159 , Coercible (CRep s) (Rep s)
150160 , Coercible (CRep t) (Rep t)
161+ , HasPosition0 i s t a b
151162 ) => HasPosition i s t a b where
152163
153164 position = position0 @i
@@ -166,14 +177,12 @@ instance {-# OVERLAPPING #-} HasPosition f (Void1 a) (Void1 b) a b where
166177 position = undefined
167178
168179instance
169− ( Generic s
170− , Generic t
171− , ErrorUnless i s (0 <? i && i <=? Size (Rep s))
172− , GLens (HasTotalPositionPSym i) (CRep s) (CRep t) a b
180+ ( ErrorUnless i s (0 <? i && i <=? Size (Rep s))
173181 , UnifyHead s t
174182 , UnifyHead t s
175183 , Coercible (CRep s) (Rep s)
176184 , Coercible (CRep t) (Rep t)
185+ , HasPosition0 i s t a b
177186 ) => HasPosition_ i s t a b where
178187
179188 position_ = position0 @i
@@ -188,6 +197,12 @@ instance
188197 , GLens (HasTotalPositionPSym i) (CRep s) (CRep t) a b
189198 , Coercible (CRep s) (Rep s)
190199 , Coercible (CRep t) (Rep t)
200+ , Defined (Rep s)
201+ (NoGeneric s '[ 'Text "arising from a generic lens focusing on the field at"
202+ , 'Text "position " ':<>: QuoteType i ':<>: 'Text " of type " ':<>: QuoteType a
203+ ':<>: 'Text " in " ':<>: QuoteType s
204+ ])
205+ (() :: Constraint)
191206 ) => HasPosition0 i s t a b where
192207 position0 = VL.ravel (repLens . coerced @(CRep s) @(CRep t) . glens @(HasTotalPositionPSym i))
193208 {-# INLINE position0 #-}