blob: 6a82df77ccb1bd6ead50cd8a53903cd356ffcb39 (
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
[monad #+ do Monad]))
(type: #export (Writer l a)
{#;doc "Represents a value with an associated 'log' value to record arbitrary information."}
{#log l
#value a})
(struct: #export Functor<Writer> (All [l]
(Functor (Writer l)))
(def: (map f fa)
(let [[log datum] fa]
[log (f datum)])))
(struct: #export (Applicative<Writer> mon) (All [l]
(-> (Monoid l) (Applicative (Writer l))))
(def: functor Functor<Writer>)
(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<Writer> mon) (All [l]
(-> (Monoid l) (Monad (Writer l))))
(def: applicative (Applicative<Writer> 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 (Writer l Unit)))
[l []])
(struct: #export (WriterT Monoid<l> Monad<M>)
(All [l M] (-> (Monoid l) (Monad M) (Monad (All [a] (M (Writer l a))))))
(def: applicative (A;compA (get@ #monad;applicative Monad<M>) (Applicative<Writer> Monoid<l>)))
(def: (join MlMla)
(do Monad<M>
[[l1 Mla] (: (($ +1) (Writer ($ +0) (($ +1) (Writer ($ +0) ($ +2)))))
MlMla)
[l2 a] Mla]
(wrap [(:: Monoid<l> append l1 l2) a]))))
(def: #export (lift Monoid<l> Monad<M>)
(All [l M a] (-> (Monoid l) (Monad M) (-> (M a) (M (Writer l a)))))
(function [ma]
(do Monad<M>
[a ma]
(wrap [(:: Monoid<l> unit) a]))))
|