aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/trace.lux
blob: 1b2f87ddfdc673a5061d758a7b892fea473795f1 (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
(.module:
  [lux #*
   [abstract
    ["." monoid (#+ Monoid)]
    [functor (#+ Functor)]
    comonad]
   function])

(type: #export (Trace t a)
  {#monoid (Monoid t)
   #trace (-> t a)})

(implementation: #export functor (All [t] (Functor (Trace t)))
  (def: (map f fa)
    (update@ #trace (compose f) fa)))

(implementation: #export comonad (All [t] (CoMonad (Trace t)))
  (def: &functor ..functor)

  (def: (unwrap wa)
    ((get@ #trace wa)
     (get@ [#monoid #monoid.identity] wa)))

  (def: (split wa)
    (let [monoid (get@ #monoid wa)]
      {#monoid monoid
       #trace (function (_ t1)
                {#monoid monoid
                 #trace (function (_ t2)
                          ((get@ #trace wa)
                           (\ monoid compose t1 t2)))})})))

(def: #export (run context tracer)
  (All [t a] (-> t (Trace t a) a))
  (\ tracer trace context))