0 | module Data.Profunctor.Representable
 1 |
 2 | import Control.Applicative.Const
 3 | import Control.Monad.Identity
 4 | import Data.Morphisms
 5 | import Data.Profunctor
 6 | import Data.Profunctor.Costrong
 7 | import Data.Profunctor.Sieve
 8 |
 9 | %default total
10 |
11 |
12 | ------------------------------------------------------------------------------
13 | -- Interfaces
14 | ------------------------------------------------------------------------------
15 |
16 |
17 | ||| A profunctor `p` is representable if it is isomorphic to `Star f` for some `f`.
18 | public export
19 | interface (Sieve p fStrong p) => Representable p f | p where
20 |   tabulate : (a -> f b) -> p a b
21 |
22 |
23 | ||| A profunctor `p` is corepresentable if it is isomorphic to `Costar f` for some `f`.
24 | public export
25 | interface Cosieve p f => Corepresentable p f | p where
26 |   cotabulate : (f a -> b) -> p a b
27 |
28 |
29 | export
30 | tabulated : (Representable q f, Representable r g) => forall p. Profunctor p =>
31 |               p (q a b) (r a' b') -> p (a -> f b) (a' -> g b')
32 | tabulated = dimap tabulate sieve
33 |
34 | export
35 | cotabulated : (Corepresentable q f, Corepresentable r g) => forall p. Profunctor p =>
36 |               p (q a b) (r a' b') -> p (f a -> b) (g a' -> b')
37 | cotabulated = dimap cotabulate cosieve
38 |
39 |
40 | ------------------------------------------------------------------------------
41 | -- Implementations
42 | ------------------------------------------------------------------------------
43 |
44 |
45 | export
46 | Representable Morphism Identity where
47 |   tabulate f = Mor (runIdentity . f)
48 |
49 | ||| A named implementation of `Representable` for function types.
50 | ||| Use this to avoid having to use a type wrapper like `Morphism`.
51 | export
52 | [Function] Representable (\a,b => a -> b) Prelude.id
53 |     using Sieve.Function Strong.Function where
54 |   tabulate = id
55 |
56 | export
57 | Functor f => Representable (Kleislimorphism f) f where
58 |   tabulate = Kleisli
59 |
60 | export
61 | Functor f => Representable (Star f) f where
62 |   tabulate = MkStar
63 |
64 | export
65 | Representable (Forget r) (Const r) where
66 |   tabulate = MkForget . (runConst .)
67 |
68 | export
69 | Corepresentable Morphism Identity where
70 |   cotabulate f = Mor (f . Id)
71 |
72 | namespace Corepresentable
73 |   ||| A named implementation of `Corepresentable` for function types.
74 |   ||| Use this to avoid having to use a type wrapper like `Morphism`.
75 |   export
76 |   [Function] Corepresentable (\a,b => a -> b) Prelude.id using Cosieve.Function where
77 |     cotabulate = id
78 |
79 | export
80 | Functor f => Corepresentable (Costar f) f where
81 |   cotabulate = MkCostar
82 |
83 | export
84 | Corepresentable (Coforget r) (Const r) where
85 |   cotabulate = MkCoforget . (. MkConst)
86 |