blob: ae644c94f2a819e7157f28c81bde01b1508bf969 (
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
|
(;module:
lux
(lux/control monoid
["A" applicative #*]
functor
["M" monad #*]))
(type: #export (Log l a)
{#;doc "Represents a value with an associated 'log' value to record arbitrary information."}
{#log l
#value 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)
{#;doc "Set the log to a particular value."}
(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)))))
(function [ma]
(do Monad<M>
[a ma]
(wrap [(:: Monoid<l> unit) a]))))
|