aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/trace.lux
blob: 72a6eb4d32fdfdc2892016372ceb3d89254382c0 (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
(.using
 [library
  [lux "*"
   [abstract
    ["[0]" monoid (.only Monoid)]
    [functor (.only 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)
    ((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)
                           (# monoid composite t1 t2)))])])))

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