@@ -13,7 +13,9 @@
13module Main where
13module Main where
15import GHC.Generics
15import GHC.Generics
16+import Data.Profunctor
16import Data.Generics.Product
17import Data.Generics.Product
18+import Data.Generics.Sum
17import Test.Inspection
19import Test.Inspection
19main :: IO ()
21main :: IO ()
@@ -34,7 +36,14 @@ data Record3 a = MkRecord3
34 } deriving (Generic, Show)
36 } deriving (Generic, Show)
36type Lens' s a = Lens s s a a
38type Lens' s a = Lens s s a a
39+type Prism' s a = Prism s s a a
37type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
40type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
42+ forall p f . (Choice p, Applicative f) => p a (f b) -> p s (f t)
44+prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
45+prism bt seta = dimap seta (either pure (fmap bt)) . right'
39fieldALensManual :: Lens' Record Int
48fieldALensManual :: Lens' Record Int
40fieldALensManual f (MkRecord a b) = (\a' -> MkRecord a' b) <$> f a
49fieldALensManual f (MkRecord a b) = (\a' -> MkRecord a' b) <$> f a
@@ -53,6 +62,19 @@ subtypeLensManual f record
53 MkRecord _ b -> MkRecord (case ds of {MkRecord2 g1 -> g1}) b
62 MkRecord _ b -> MkRecord (case ds of {MkRecord2 g1 -> g1}) b
54 ) (f (MkRecord2 (case record of {MkRecord a _ -> a})))
63 ) (f (MkRecord2 (case record of {MkRecord a _ -> a})))
65+data Sum1 = A Char | B Int | C () | D () deriving (Generic, Show)
67+sum1PrismManual :: Prism' Sum1 Int
68+sum1PrismManual = prism g f
56--------------------------------------------------------------------------------
78--------------------------------------------------------------------------------
58-- The inspection-testing plugin checks that the following equalities hold, by
80-- The inspection-testing plugin checks that the following equalities hold, by
@@ -81,6 +103,9 @@ typeChangingGenericPos = position @1
81typeChangingGenericCompose :: Lens (Record3 (Record3 a)) (Record3 (Record3 b)) a b
103typeChangingGenericCompose :: Lens (Record3 (Record3 a)) (Record3 (Record3 b)) a b
82typeChangingGenericCompose = field @"fieldA" . field @"fieldA"
104typeChangingGenericCompose = field @"fieldA" . field @"fieldA"
106+sum1PrismB :: Prism' Sum1 Int
107+sum1PrismB = _Ctor @"B"
84inspect $ 'fieldALensManual === 'fieldALensName
109inspect $ 'fieldALensManual === 'fieldALensName
85inspect $ 'fieldALensManual === 'fieldALensType
110inspect $ 'fieldALensManual === 'fieldALensType
86inspect $ 'fieldALensManual === 'fieldALensPos
111inspect $ 'fieldALensManual === 'fieldALensPos
@@ -88,3 +113,5 @@ inspect $ 'subtypeLensManual === 'subtypeLensGeneric
88inspect $ 'typeChangingManual === 'typeChangingGeneric
113inspect $ 'typeChangingManual === 'typeChangingGeneric
89inspect $ 'typeChangingManual === 'typeChangingGenericPos
114inspect $ 'typeChangingManual === 'typeChangingGenericPos
90inspect $ 'typeChangingManualCompose === 'typeChangingGenericCompose
115inspect $ 'typeChangingManualCompose === 'typeChangingGenericCompose
117+--inspect $ 'sum1PrismManual === 'sum1PrismB