aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/log.lux
blob: 687f23478bd19a072a51fae2f8c75141e59e3b13 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;module:
  lux
  (lux/control monoid
               ["A" applicative #*]
               functor
               ["M" monad #*]))

(type: #export (Log l a)
  [l a])

(struct: #export Functor<Log> (All [l]
                                (Functor (Log l)))
  (def: (map f fa)
    (let [[log datum] fa]
      [log (f datum)])))

(struct: #export (Applicative<Log> mon) (All [l]
                                          (-> (Monoid l) (Applicative (Log l))))
  (def: functor Functor<Log>)

  (def: (wrap x)
    [(:: mon unit) x])

  (def: (apply ff fa)
    (let [[log1 f] ff
          [log2 a] fa]
      [(:: mon append log1 log2) (f a)])))

(struct: #export (Monad<Log> mon) (All [l]
                                    (-> (Monoid l) (Monad (Log l))))
  (def: applicative (Applicative<Log> mon))

  (def: (join mma)
    (let [[log1 [log2 a]] mma]
      [(:: mon append log1 log2) a])))

(def: #export (log l)
  (All [l] (-> l (Log l Unit)))
  [l []])

(struct: #export (LogT Monoid<l> Monad<M>)
  (All [l M] (-> (Monoid l) (Monad M) (Monad (All [a] (M (Log l a))))))
  (def: applicative (A;compA (get@ #M;applicative Monad<M>) (Applicative<Log> Monoid<l>)))
  (def: (join MlMla)
    (do Monad<M>
      [[l1 Mla] (: (($ +1) (Log ($ +0) (($ +1) (Log ($ +0) ($ +2)))))
                   MlMla)
       [l2 a] Mla]
      (wrap [(:: Monoid<l> append l1 l2) a]))))

(def: #export (lift-log Monoid<l> Monad<M>)
  (All [l M a] (-> (Monoid l) (Monad M) (-> (M a) (M (Log l a)))))
  (lambda [ma]
    (do Monad<M>
      [a ma]
      (wrap [(:: Monoid<l> unit) a]))))