aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/trace.lux
blob: a4a778c561ca9286071401e5a9970920eb0d1cca (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
(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monoid (.only Monoid)]
    [functor (.only Functor)]
    comonad]
   function]])

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

(def .public functor
  (All (_ t) (Functor (Trace t)))
  (implementation
   (def (each f fa)
     (revised #trace (composite f) fa))))

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

   (def (out wa)
     ((the #trace wa)
      (the [#monoid monoid.#identity] wa)))

   (def (disjoint wa)
     (let [monoid (the #monoid wa)]
       [#monoid monoid
        #trace (function (_ t1)
                 [#monoid monoid
                  #trace (function (_ t2)
                           ((the #trace wa)
                            (at monoid composite t1 t2)))])]))))

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