aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/specification/lux/abstract/monad.lux
blob: 1a602b292af39703f5489e072039d570b4835319 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." / (#+ do)]]
  [//
   [functor (#+ Injection Comparison)]])

(def: (left_identity injection comparison (^open "_//."))
  (All [f] (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (do {! random.monad}
    [sample random.nat
     morphism (\ ! map (function (_ diff)
                         (|>> (n.+ diff) _//in))
                 random.nat)]
    (_.test "Left identity."
            ((comparison n.=)
             (|> (injection sample) (_//map morphism) _//join)
             (morphism sample)))))

(def: (right_identity injection comparison (^open "_//."))
  (All [f] (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (do random.monad
    [sample random.nat]
    (_.test "Right identity."
            ((comparison n.=)
             (|> (injection sample) (_//map _//in) _//join)
             (injection sample)))))

(def: (associativity injection comparison (^open "_//."))
  (All [f] (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (do {! random.monad}
    [sample random.nat
     increase (\ ! map (function (_ diff)
                         (|>> (n.+ diff) _//in))
                 random.nat)
     decrease (\ ! map (function (_ diff)
                         (|>> (n.- diff) _//in))
                 random.nat)]
    (_.test "Associativity."
            ((comparison n.=)
             (|> (injection sample) (_//map increase) _//join (_//map decrease) _//join)
             (|> (injection sample) (_//map (|>> increase (_//map decrease) _//join)) _//join)))))

(def: #export (spec injection comparison monad)
  (All [f] (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (<| (_.for [/.Monad])
      ($_ _.and
          (..left_identity injection comparison monad)
          (..right_identity injection comparison monad)
          (..associativity injection comparison monad)
          )))