aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/trace.lux
blob: 156497e43599d9443f0a4f38972fb1cf7f5f0a71 (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
(.module:
  [library
   [lux "*"
    [abstract
     ["[0]" monoid {"+" [Monoid]}]
     [functor {"+" [Functor]}]
     comonad]
    function]])

(type: .public (Trace t a)
  (Record
   [#monoid (Monoid t)
    #trace (-> t a)]))

(implementation: .public functor
  (All (_ t) (Functor (Trace t)))

  (def: (each f fa)
    (revised@ #trace (composite f) fa)))

(implementation: .public comonad
  (All (_ t) (CoMonad (Trace t)))
  
  (def: &functor ..functor)

  (def: (out wa)
    ((value@ #trace wa)
     (value@ [#monoid #monoid.identity] wa)))

  (def: (disjoint wa)
    (let [monoid (value@ #monoid wa)]
      [#monoid monoid
       #trace (function (_ t1)
                [#monoid monoid
                 #trace (function (_ t2)
                          ((value@ #trace wa)
                           (\ monoid composite t1 t2)))])])))

(def: .public (result context tracer)
  (All (_ t a) (-> t (Trace t a) a))
  (\ tracer trace context))