@@ -13,7 +13,9 @@
1313module Main where
1414
1515import GHC.Generics
16+import Data.Profunctor
1617import Data.Generics.Product
18+import Data.Generics.Sum
1719import Test.Inspection
1820
1921main :: IO ()
@@ -34,7 +36,14 @@ data Record3 a = MkRecord3
3436 } deriving (Generic, Show)
3537
3638type Lens' s a = Lens s s a a
39+type Prism' s a = Prism s s a a
3740type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
41+type Prism s t a b =
42+ forall p f . (Choice p, Applicative f) => p a (f b) -> p s (f t)
43+
44+prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
45+prism bt seta = dimap seta (either pure (fmap bt)) . right'
46+{-# INLINE prism #-}
3847
3948fieldALensManual :: Lens' Record Int
4049fieldALensManual f (MkRecord a b) = (\a' -> MkRecord a' b) <$> f a
@@ -53,6 +62,19 @@ subtypeLensManual f record
5362 MkRecord _ b -> MkRecord (case ds of {MkRecord2 g1 -> g1}) b
5463 ) (f (MkRecord2 (case record of {MkRecord a _ -> a})))
5564
65+data Sum1 = A Char | B Int | C () | D () deriving (Generic, Show)
66+
67+sum1PrismManual :: Prism' Sum1 Int
68+sum1PrismManual = prism g f
69+ where
70+ f s1 = case s1 of
71+ B i -> Right i
72+ s -> Left s
73+ g = B
74+
75+
76+
77+
5678--------------------------------------------------------------------------------
5779-- * Tests
5880-- The inspection-testing plugin checks that the following equalities hold, by
@@ -81,6 +103,9 @@ typeChangingGenericPos = position @1
81103typeChangingGenericCompose :: Lens (Record3 (Record3 a)) (Record3 (Record3 b)) a b
82104typeChangingGenericCompose = field @"fieldA" . field @"fieldA"
83105
106+sum1PrismB :: Prism' Sum1 Int
107+sum1PrismB = _Ctor @"B"
108+
84109inspect $ 'fieldALensManual === 'fieldALensName
85110inspect $ 'fieldALensManual === 'fieldALensType
86111inspect $ 'fieldALensManual === 'fieldALensPos
@@ -88,3 +113,5 @@ inspect $ 'subtypeLensManual === 'subtypeLensGeneric
88113inspect $ 'typeChangingManual === 'typeChangingGeneric
89114inspect $ 'typeChangingManual === 'typeChangingGenericPos
90115inspect $ 'typeChangingManualCompose === 'typeChangingGenericCompose
116+
117+--inspect $ 'sum1PrismManual === 'sum1PrismB