File: GenericLens.hs

package info (click to toggle)
haskell-inspection-testing 0.5.0.3-1
  • links: PTS
  • area: main
  • in suites: forky, trixie
  • size: 208 kB
  • sloc: haskell: 2,003; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE RankNTypes, DeriveGeneric, TypeApplications, DataKinds, ExistentialQuantification, TemplateHaskell #-}
{-# OPTIONS_GHC -dsuppress-idinfo #-}
module GenericLens (main) where

import GHC.Generics
import Data.Generics.Product
import Test.Inspection

data Record = MkRecord { fieldA :: Int
                       , fieldB :: Bool
                       } deriving Generic

type Lens' s a = forall f. Functor f => (a -> f a) -> s -> f s

fieldALensManual :: Lens' Record Int
fieldALensManual f (MkRecord a b) = (\a -> MkRecord a b) <$> f a

-- Coyoneda optimization

data Coyoneda f b = forall a. Coyoneda (a -> b) (f a)

instance Functor (Coyoneda f) where
    fmap f (Coyoneda g fa) = Coyoneda (f . g) fa

inj :: Functor f => Coyoneda f a -> f a
inj (Coyoneda f a) = fmap f a

proj :: Functor f => f a -> Coyoneda f a
proj fa = Coyoneda id fa

ravel :: Functor f => ((a -> Coyoneda f b) -> (s -> Coyoneda f t))
                   -> (a -> f b) -> (s -> f t)
ravel coy f s = inj $ coy (\a -> proj (f a)) s

-- the examples

fieldALensGeneric :: Lens' Record Int
fieldALensGeneric = field @"fieldA"

fieldALensGenericYoneda :: Lens' Record Int
fieldALensGenericYoneda = ravel (field @"fieldA")

main :: IO ()
main = return ()

-- the check
inspect $ 'fieldALensManual === 'fieldALensGeneric
inspect $ 'fieldALensManual === 'fieldALensGenericYoneda