aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/writer.lux
blob: bfbf6183e10ba466b4e45ad371e573a31e8acf2b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(.using
 [library
  [lux "*"
   ["@" target]
   [abstract
    [monoid {"+" Monoid}]
    [apply {"+" Apply}]
    ["[0]" functor {"+" Functor}]
    ["[0]" monad {"+" Monad do}]]]])

(type: .public (Writer log value)
  (Record
   [#log log
    #value value]))

(def: .public (write message)
  (All (_ log) (-> log (Writer log Any)))
  [message []])

(implementation: .public functor
  (All (_ l)
    (Functor (Writer l)))
  
  (def: (each f fa)
    (let [[log datum] fa]
      [log (f datum)])))

(implementation: .public (apply monoid)
  (All (_ l)
    (-> (Monoid l) (Apply (Writer l))))
  
  (def: &functor ..functor)

  (def: (on fa ff)
    (let [[log1 f] ff
          [log2 a] fa]
      [(# monoid composite log1 log2) (f a)])))

(implementation: .public (monad monoid)
  (All (_ l)
    (-> (Monoid l) (Monad (Writer l))))
  
  (def: &functor ..functor)

  (def: in
    (|>> [(# monoid identity)]))

  (def: (conjoint mma)
    (let [[log1 [log2 a]] mma]
      [(# monoid composite log1 log2) a])))

(implementation: .public (with monoid monad)
  (All (_ l M) (-> (Monoid l) (Monad M) (Monad (All (_ a) (M (Writer l a))))))

  (def: &functor
    (functor.composite (the monad.&functor monad)
                       ..functor))

  (def: in
    (let [writer (..monad monoid)]
      (|>> (# writer in) (# monad in))))
  
  (def: (conjoint MlMla)
    (do monad
      [[l1 Mla] (for @.old
                     (is {.#Apply (Writer (parameter 0)
                                          {.#Apply (Writer (parameter 0)
                                                           (parameter 2))
                                                   (parameter 1)})
                                  (parameter 1)}
                         MlMla)
                     ... On new compiler
                     MlMla)
       [l2 a] Mla]
      (in [(# monoid composite l1 l2) a]))))

(def: .public (lifted monoid monad)
  (All (_ l M a)
    (-> (Monoid l) (Monad M)
        (-> (M a) (M (Writer l a)))))
  (# monad each (|>> [(# monoid identity)])))