-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComonad.hs
More file actions
76 lines (50 loc) · 1.86 KB
/
Copy pathComonad.hs
File metadata and controls
76 lines (50 loc) · 1.86 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-- https://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/
{-# LANGUAGE RankNTypes #-}
module Comonad where
import Prelude hiding ((.), id)
import Control.Category
class Functor w => Comonad w where
extract :: w a -> a
duplicate :: w a -> w (w a)
f <==> g = \x -> f x == g x
type ComonadLaw = forall a w . (Eq (w a), Eq (w (w (w a))), Comonad w)
=> w a -> Bool
law1 :: ComonadLaw
law1 = (extract . duplicate) <==> id
law2 :: ComonadLaw
law2 = (fmap extract . duplicate) <==> id
law3 :: ComonadLaw
law3 = (fmap duplicate . duplicate) <==> (duplicate . duplicate)
data CoState v a = CoState v (v -> a)
instance Functor (CoState v) where
fmap g (CoState v f) = CoState v (g . f)
instance Comonad (CoState v) where
extract (CoState v f) = f v
duplicate (CoState v f) = CoState v (\u -> CoState u f)
type CoAlgebra c a = a -> c a
type CoAlgebraComonadLaw = forall w a . (Eq a, Eq (w (w a)), Comonad w)
=> (a -> w a) -> a -> Bool
law4 :: CoAlgebraComonadLaw
law4 f = (extract . f) <==> id
law5 :: CoAlgebraComonadLaw
law5 f = (duplicate . f) <==> (fmap f . f)
-- Lenses are coalgebras of a costate comonad
{-
QUOTE:
Pierce lenses are pairs of functions between source and target data
type S and T a get function is S -> T and a source function
is a S x T -> S
The story is that the GET is some projection of the data in the source
and so in order to be able to update the source given a modify view, one
needs the original copy of the source to be able to recunstruct the missing
information.
-}
type Get s t = s -> t
type Put s t = s -> (t -> s)
type Lens' s t = (Get s t, Put s t)
type Lens'' s t = s -> (t, t -> s)
type Lens s t = s -> CoState s (t -> s)
data LensW s t = L (Lens s t)
instance Category LensW where
id = L (\s -> CoState s (const id))
(L g) . (L f) = L _