aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/store.lux
blob: 8e4c0746d3fbae9982d8f92ea36b4c3632c96d05 (plain)
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
(.using
 [library
  [lux (.except)
   [abstract
    [functor (.only Functor)]
    comonad]
   [type
    implicit]]])

(type: .public (Store s a)
  (Record
   [#cursor s
    #peek (-> s a)]))

(def (extend f wa)
  (All (_ s a b) (-> (-> (Store s a) b) (Store s a) (Store s b)))
  [#cursor (the #cursor wa)
   #peek (function (_ s) (f (has #cursor s wa)))])

(def .public functor
  (All (_ s) (Functor (Store s)))
  (implementation
   (def (each f fa)
     (extend (function (_ store)
               (f (at store peek (at store cursor))))
             fa))))

(def .public comonad
  (All (_ s) (CoMonad (Store s)))
  (implementation
   (def functor
     ..functor)

   (def (out wa)
     (a/an peek (a/an cursor)))

   (def disjoint
     (extend id))))

(def .public (peeks trans store)
  (All (_ s a) (-> (-> s s) (Store s a) a))
  (|> (a/an cursor) trans (a/an peek)))

(def .public (seek cursor store)
  (All (_ s a) (-> s (Store s a) (Store s a)))
  (at (a/an disjoint store) peek cursor))

(def .public (seeks change store)
  (All (_ s a) (-> (-> s s) (Store s a) (Store s a)))
  (|> store (a/an disjoint) (peeks change)))

(def .public (experiment Functor<f> change store)
  (All (_ f s a) (-> (Functor f) (-> s (f s)) (Store s a) (f a)))
  (at Functor<f> each (a/an peek) (change (a/an cursor))))